E2e test for run with json

This commit is contained in:
Phani Teja Marupaka
2020-06-08 20:51:01 -07:00
parent b39c522cc1
commit e994b3b566
9 changed files with 76 additions and 122 deletions

View File

@@ -722,33 +722,24 @@ func (rn *RNode) UnmarshalJSON(b []byte) error {
return nil
}
// ConvertJSONToYAMLNode parses input json string and returns equivalent yaml node
func ConvertJSONToYAMLNode(jsonStr string) (*RNode, error) {
yml, err := ConvertJSONToYAMLString(jsonStr)
// ConvertJSONToYamlNode parses input json string and returns equivalent yaml node
func ConvertJSONToYamlNode(jsonStr string) (*RNode, error) {
var body map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &body)
if err != nil {
return nil, err
}
node, err := Parse(yml)
yml, err := yaml.Marshal(body)
if err != nil {
return nil, err
}
node, err := Parse(string(yml))
if err != nil {
return nil, err
}
return node, nil
}
// ConvertJSONToYAMLString parses input json string and returns equivalent yaml string
func ConvertJSONToYAMLString(jsonStr string) (string, error) {
var body map[string]interface{}
err := json.Unmarshal([]byte(jsonStr), &body)
if err != nil {
return "", err
}
yml, err := yaml.Marshal(body)
if err != nil {
return "", err
}
return string(yml), nil
}
// checkKey returns true if all elems have the key
func checkKey(key string, elems []*Node) bool {
count := 0

View File

@@ -147,7 +147,7 @@ hello: world
}
}
func TestConvertJSONToYAMLNode(t *testing.T) {
func TestConvertJSONToYamlNode(t *testing.T) {
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}`
expected := `enum:
- allowedValue1
@@ -156,7 +156,7 @@ maxLength: 15
type: string
`
node, err := ConvertJSONToYAMLNode(inputJSON)
node, err := ConvertJSONToYamlNode(inputJSON)
if !assert.NoError(t, err) {
t.FailNow()
}
@@ -166,32 +166,3 @@ type: string
}
assert.Equal(t, expected, actual)
}
func TestConvertJSONToYAMLString(t *testing.T) {
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}`
expected := `enum:
- allowedValue1
- allowedValue2
maxLength: 15
type: string
`
actual, err := ConvertJSONToYAMLString(inputJSON)
if !assert.NoError(t, err) {
t.FailNow()
}
assert.Equal(t, expected, actual)
}
// error if there are multiple json blobs in input string
func TestConvertJSONToYAMLStringError(t *testing.T) {
inputJSON := `{"type": "string", "maxLength": 15, "enum": ["allowedValue1", "allowedValue2"]}
{"type": "string", "maxLength": 5, "enum": ["allowedValue2", "allowedValue3"]}`
expected := `invalid character '{' after top-level value`
_, err := ConvertJSONToYAMLString(inputJSON)
if !assert.Error(t, err) {
t.FailNow()
}
assert.Equal(t, expected, err.Error())
}