making progress

This commit is contained in:
2023-12-29 15:14:06 +11:00
parent b7094fd83d
commit 100bbb6c0b
2 changed files with 87 additions and 43 deletions

122
main.go
View File

@@ -9,20 +9,16 @@ import (
"github.com/iancoleman/orderedmap" "github.com/iancoleman/orderedmap"
) )
type CommonTestSettings struct { type TestDefinitions struct {
RootUrl string Name string `json:"name"`
Name string TestCases []TestCase `json:"testCases"`
Headers map[string]string // Need to initialise with make BaseUrl string `json:"url"`
Headers map[string]string // Need to initialise with make
//Header HeaderOptions `json:"header"`
} }
type InputData struct { type TestCase struct {
Name string `json:"name"` Name string
TestCases []TestCases `json:"testCases"`
URL string `json:"url"`
Header HeaderOptions `json:"header"`
}
type TestCases struct {
Path string `json:"path"` Path string `json:"path"`
Method string `json:"method"` Method string `json:"method"`
Description string `json:"description"` Description string `json:"description"`
@@ -92,10 +88,11 @@ func TestOutput() {
func main() { func main() {
var s []byte var s []byte
var err error var err error
var ok bool
// Prepare struct to store settings common to all tests // Prepare struct to store settings common to all tests
testSettings := new(CommonTestSettings) testDefinitions := new(TestDefinitions)
testSettings.Headers = make(map[string]string) testDefinitions.Headers = make(map[string]string)
//testCaseDefinition := new(InputData) //testCaseDefinition := new(InputData)
@@ -126,9 +123,11 @@ func main() {
topLevel := o.Keys() topLevel := o.Keys()
fmt.Printf("Found %d top-level keys in json data\n", len(topLevel)) fmt.Printf("Found %d top-level keys in json data\n", len(topLevel))
for i, key := range topLevel { /*
fmt.Printf("[%d] : %s\n", i, key) for i, key := range topLevel {
} fmt.Printf("[%d] : %s\n", i, key)
}
*/
// TODO : Check required top level keys are present // TODO : Check required top level keys are present
if len(topLevel) <= 1 { if len(topLevel) <= 1 {
@@ -136,25 +135,41 @@ func main() {
panic(error) panic(error)
} }
// Get a reference to the node containing all our test cases // Get the name of the group of tests
if data, ok := o.Get("name"); ok {
testDefinitions.Name = data.(string)
fmt.Printf("test name: '%s'\n", testDefinitions.Name)
}
// Get a reference to the node containing each of the test cases
testsInterface, ok := o.Get("testCases") testsInterface, ok := o.Get("testCases")
if !ok { if !ok {
fmt.Printf("No key defining test cases found\n") fmt.Printf("No key defining test cases found\n")
} }
// Test Cases is an array, need to go one level down before we start looking at key/values /*
vs := testsInterface.([]interface{}) // Test Cases is an array, need to go one level down before we start looking at key/values
fmt.Printf("Listing %d test definitions\n", len(vs)) vs := testsInterface.([]interface{})
for i, vInterface := range vs { fmt.Printf("Listing %d test definitions\n", len(vs))
v := vInterface.(orderedmap.OrderedMap) for i, vInterface := range vs {
keys := v.Keys() v := vInterface.(orderedmap.OrderedMap)
fmt.Printf("Test %d\n", i) keys := v.Keys()
for j := range keys { fmt.Printf("Test %d\n", i)
fmt.Printf("[%d] : %s\n", j, keys[j]) for j := range keys {
fmt.Printf("[%d] : %s\n", j, keys[j])
}
} }
} */
return /*
vslice := testsInterface.([]interface{})
vmap := vslice[2].(orderedmap.OrderedMap)
k := vmap.Keys()
for i := range k {
fmt.Printf("[%d] : %s\n", i, k[i])
}
return
*/
// Get the keys for the first test so we know what config options have been specified // Get the keys for the first test so we know what config options have been specified
testCasesMap := testsInterface.(orderedmap.OrderedMap) testCasesMap := testsInterface.(orderedmap.OrderedMap)
@@ -163,16 +178,45 @@ func main() {
// Parse json into our testCaseDefinition // Parse json into our testCaseDefinition
// Parse each key into our config struct // Parse each key into our config struct
for i, key := range testCasesKeys { fmt.Printf("Listing %d test cases\n", len(testCasesKeys))
/* for i, outerKey := range testCasesKeys {
if strings.EqualFold(key, "path") {
fmt.Printf("Found config element for path\n") fmt.Printf("Test %d : %s\n", i, outerKey)
e, _ := configMap.Get(key)
for i, e := range strings.Split(e.(string), ",") { thisTestCase := new(TestCase)
config.keyOrder[i] = e thisTestCase.Name = outerKey
}
if testCase, ok := testCasesMap.Get(outerKey); ok {
// Get the details of the test case
thisTestCaseMap := testCase.(orderedmap.OrderedMap)
// Path
if val, ok := thisTestCaseMap.Get("path"); ok {
thisTestCase.Path = val.(string)
} }
*/ // Method
fmt.Printf("[%d] : %s\n", i, key) if val, ok := thisTestCaseMap.Get("method"); ok {
thisTestCase.Method = val.(string)
}
// Description
if val, ok := thisTestCaseMap.Get("description"); ok {
thisTestCase.Description = val.(string)
}
// Expect - this is more tricky since it is yet another json fragment
/*
for j, key := range thisTestCaseMap.Keys() {
val, _ := thisTestCaseMap.Get(key)
fmt.Printf("[%d] %s : %s\n", j, key, val)
}
*/
}
testDefinitions.TestCases = append(testDefinitions.TestCases, *thisTestCase)
} }
// For debugging, just dump the output
fmt.Printf("%+v\n", testDefinitions)
} }

View File

@@ -1,7 +1,7 @@
{ {
"name": "CBS test cases", "name": "CBS test cases",
"testCases": [ "testCases": {
{ "test1": {
"path": "/hosts", "path": "/hosts",
"method": "GET", "method": "GET",
"description": "Check database query is working", "description": "Check database query is working",
@@ -18,7 +18,7 @@
} }
} }
}, },
{ "test2": {
"path": "/protected/hosts?key=e36689911ed0e9cba50c436b66d664cf9ee4f555e182a48021c21d9f7c640868", "path": "/protected/hosts?key=e36689911ed0e9cba50c436b66d664cf9ee4f555e182a48021c21d9f7c640868",
"method": "POST", "method": "POST",
"description": "Create new host", "description": "Create new host",
@@ -48,7 +48,7 @@
} }
} }
} }
], },
"url": "https://10.63.39.130:443", "url": "https://10.63.39.130:443",
"header": { "header": {
"Content-Type": "application/json" "Content-Type": "application/json"