github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/model/spec_test.go (about) 1 package model_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/kyma-incubator/compass/components/director/internal/model" 8 "github.com/pkg/errors" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestSpecInput_ToSpec(t *testing.T) { 13 // GIVEN 14 id := "id" 15 refID := "ref-id" 16 data := "data" 17 apiType := model.APISpecTypeOdata 18 eventType := model.EventSpecTypeAsyncAPI 19 testCases := []struct { 20 Name string 21 InputID string 22 InputReferenceObjectType model.SpecReferenceObjectType 23 InputReferenceObjectID string 24 SpecInput *model.SpecInput 25 Expected *model.Spec 26 ExpectedErr error 27 }{ 28 { 29 Name: "All properties given for API", 30 InputID: id, 31 InputReferenceObjectID: refID, 32 InputReferenceObjectType: model.APISpecReference, 33 SpecInput: &model.SpecInput{ 34 Data: &data, 35 Format: model.SpecFormatJSON, 36 APIType: &apiType, 37 }, 38 Expected: &model.Spec{ 39 ID: id, 40 ObjectType: model.APISpecReference, 41 ObjectID: refID, 42 Data: &data, 43 Format: model.SpecFormatJSON, 44 APIType: &apiType, 45 }, 46 }, 47 { 48 Name: "API Type missing", 49 InputID: id, 50 InputReferenceObjectID: refID, 51 InputReferenceObjectType: model.APISpecReference, 52 SpecInput: &model.SpecInput{ 53 Data: &data, 54 Format: model.SpecFormatJSON, 55 }, 56 Expected: nil, 57 ExpectedErr: errors.New("API Spec type cannot be empty"), 58 }, 59 { 60 Name: "All properties given for Event", 61 InputID: id, 62 InputReferenceObjectID: refID, 63 InputReferenceObjectType: model.EventSpecReference, 64 SpecInput: &model.SpecInput{ 65 Data: &data, 66 Format: model.SpecFormatJSON, 67 EventType: &eventType, 68 }, 69 Expected: &model.Spec{ 70 ID: id, 71 ObjectType: model.EventSpecReference, 72 ObjectID: refID, 73 Data: &data, 74 Format: model.SpecFormatJSON, 75 EventType: &eventType, 76 }, 77 }, 78 { 79 Name: "Event Type missing", 80 InputID: id, 81 InputReferenceObjectID: refID, 82 InputReferenceObjectType: model.EventSpecReference, 83 SpecInput: &model.SpecInput{ 84 Data: &data, 85 Format: model.SpecFormatJSON, 86 }, 87 Expected: nil, 88 ExpectedErr: errors.New("event spec type cannot be empty"), 89 }, 90 { 91 Name: "Nil", 92 SpecInput: nil, 93 Expected: nil, 94 }, 95 } 96 97 for i, testCase := range testCases { 98 t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) { 99 // WHEN 100 result, err := testCase.SpecInput.ToSpec(testCase.InputID, testCase.InputReferenceObjectType, testCase.InputReferenceObjectID) 101 102 // then 103 if testCase.ExpectedErr != nil { 104 assert.Error(t, err) 105 assert.EqualError(t, err, testCase.ExpectedErr.Error()) 106 } else { 107 assert.NoError(t, err) 108 } 109 110 assert.Equal(t, testCase.Expected, result) 111 }) 112 } 113 }