github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/jsonschema/validator_test.go (about)

     1  package jsonschema_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/pkg/jsonschema"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  )
    14  
    15  func TestValidator_ValidateString(t *testing.T) {
    16  	// given
    17  
    18  	validInputJSONSchema := `{
    19  	  "$id": "https://foo.com/bar.schema.json",
    20  	  "title": "foobarbaz",
    21  	  "type": "object",
    22  	  "properties": {
    23  		"foo": {
    24  		  "type": "string",
    25  		  "description": "foo"
    26  		},
    27  		"bar": {
    28  		  "type": "string",
    29  		  "description": "bar"
    30  		},
    31  		"baz": {
    32  		  "description": "baz",
    33  		  "type": "integer",
    34  		  "minimum": 0
    35  		}
    36  	  },
    37        "required": ["foo", "bar", "baz"]
    38  	}`
    39  
    40  	inputJSON := `{
    41  	  "foo": "test",
    42  	  "bar": "test",
    43  	  "baz": 123
    44  	}`
    45  	invalidInputJSON := `{ "abc": 123 }`
    46  
    47  	testCases := []struct {
    48  		Name            string
    49  		InputJSONSchema string
    50  		InputJSON       string
    51  		ExpectedResult  bool
    52  		ExpectedError   error
    53  	}{
    54  		{
    55  			Name:            "Success",
    56  			InputJSONSchema: validInputJSONSchema,
    57  			InputJSON:       inputJSON,
    58  			ExpectedResult:  true,
    59  			ExpectedError:   nil,
    60  		},
    61  		{
    62  			Name:            "JSON schema and json doesn't match",
    63  			InputJSONSchema: validInputJSONSchema,
    64  			InputJSON:       invalidInputJSON,
    65  			ExpectedResult:  false,
    66  			ExpectedError:   nil,
    67  		},
    68  		{
    69  			Name:            "Empty",
    70  			InputJSONSchema: "",
    71  			InputJSON:       "",
    72  			ExpectedResult:  true,
    73  			ExpectedError:   nil,
    74  		},
    75  		{
    76  			Name:            "Invalid json",
    77  			InputJSONSchema: validInputJSONSchema,
    78  			InputJSON:       "{test",
    79  			ExpectedResult:  false,
    80  			ExpectedError:   errors.New("invalid character"),
    81  		},
    82  	}
    83  
    84  	for _, testCase := range testCases {
    85  		t.Run(testCase.Name, func(t *testing.T) {
    86  			svc, err := jsonschema.NewValidatorFromStringSchema(testCase.InputJSONSchema)
    87  			require.NoError(t, err)
    88  
    89  			// when
    90  			result, err := svc.ValidateString(testCase.InputJSON)
    91  			// then
    92  			assert.Equal(t, testCase.ExpectedResult, result.Valid)
    93  			if testCase.ExpectedError != nil {
    94  				require.Error(t, err)
    95  				assert.Contains(t, err.Error(), testCase.ExpectedError.Error())
    96  			} else {
    97  				if !testCase.ExpectedResult {
    98  					assert.NotNil(t, result.Error)
    99  				} else {
   100  					assert.Nil(t, result.Error)
   101  				}
   102  			}
   103  		})
   104  	}
   105  }
   106  
   107  func TestValidator_ValidateRaw(t *testing.T) {
   108  	// given
   109  	validInputJSONSchema := map[string]interface{}{
   110  		"$id":   "https://foo.com/bar.schema.json",
   111  		"title": "foobarbaz",
   112  		"type":  "object",
   113  		"properties": map[string]interface{}{
   114  			"foo": map[string]interface{}{
   115  				"type":        "string",
   116  				"description": "foo",
   117  			},
   118  			"bar": map[string]interface{}{
   119  				"type":        "string",
   120  				"description": "bar",
   121  			},
   122  			"baz": map[string]interface{}{
   123  				"description": "baz",
   124  				"type":        "integer",
   125  				"minimum":     0,
   126  			},
   127  		},
   128  		"required": []interface{}{"foo", "bar", "baz"},
   129  	}
   130  
   131  	inputJSON := map[string]interface{}{
   132  		"foo": "test",
   133  		"bar": "test",
   134  		"baz": 123,
   135  	}
   136  	invalidInputJSON := map[string]interface{}{"abc": 123}
   137  
   138  	testCases := []struct {
   139  		Name            string
   140  		InputJSONSchema interface{}
   141  		InputJSON       interface{}
   142  		ExpectedResult  bool
   143  		ExpectedError   error
   144  	}{
   145  		{
   146  			Name:            "Success",
   147  			InputJSONSchema: validInputJSONSchema,
   148  			InputJSON:       inputJSON,
   149  			ExpectedResult:  true,
   150  			ExpectedError:   nil,
   151  		},
   152  		{
   153  			Name:            "JSON schema and json doesn't match",
   154  			InputJSONSchema: validInputJSONSchema,
   155  			InputJSON:       invalidInputJSON,
   156  			ExpectedResult:  false,
   157  			ExpectedError:   nil,
   158  		},
   159  		{
   160  			Name:            "Empty",
   161  			InputJSONSchema: nil,
   162  			InputJSON:       "anything",
   163  			ExpectedResult:  true,
   164  			ExpectedError:   nil,
   165  		},
   166  	}
   167  
   168  	for _, testCase := range testCases {
   169  		t.Run(testCase.Name, func(t *testing.T) {
   170  			svc, err := jsonschema.NewValidatorFromRawSchema(testCase.InputJSONSchema)
   171  			require.NoError(t, err)
   172  
   173  			// when
   174  			result, err := svc.ValidateRaw(testCase.InputJSON)
   175  			// then
   176  			assert.Equal(t, testCase.ExpectedResult, result.Valid)
   177  			require.Equal(t, testCase.ExpectedError, err)
   178  			if testCase.ExpectedError != nil {
   179  				return
   180  			}
   181  			if testCase.ExpectedResult {
   182  				require.NoError(t, result.Error)
   183  			} else {
   184  				require.Error(t, result.Error)
   185  			}
   186  		})
   187  	}
   188  }
   189  
   190  func TestNewValidatorFromStringSchema_NotValidSchema(t *testing.T) {
   191  	//GIVEN
   192  	stringSchema := `"schema"`
   193  	// WHEN
   194  	_, err := jsonschema.NewValidatorFromStringSchema(stringSchema)
   195  	// THEN
   196  	require.Error(t, err)
   197  	assert.EqualError(t, err, "schema is invalid")
   198  }