github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/fetchrequest/converter_test.go (about) 1 package fetchrequest_test 2 3 import ( 4 "database/sql" 5 "testing" 6 "time" 7 8 "github.com/kyma-incubator/compass/components/director/pkg/apperrors" 9 10 "github.com/kyma-incubator/compass/components/director/internal/repo" 11 12 "github.com/stretchr/testify/require" 13 14 "github.com/kyma-incubator/compass/components/director/internal/domain/fetchrequest" 15 "github.com/kyma-incubator/compass/components/director/internal/domain/fetchrequest/automock" 16 "github.com/kyma-incubator/compass/components/director/internal/model" 17 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 18 "github.com/stretchr/testify/assert" 19 ) 20 21 func TestConverter_ToGraphQL(t *testing.T) { 22 // GIVEN 23 testCases := []struct { 24 Name string 25 Input *model.FetchRequest 26 Expected *graphql.FetchRequest 27 }{ 28 { 29 Name: "All properties given", 30 Input: fixModelFetchRequest(t, "foo", "bar"), 31 Expected: fixGQLFetchRequest(t, "foo", "bar"), 32 }, 33 { 34 Name: "Empty", 35 Input: &model.FetchRequest{}, 36 Expected: &graphql.FetchRequest{ 37 Status: &graphql.FetchRequestStatus{ 38 Condition: graphql.FetchRequestStatusConditionInitial, 39 }, 40 }, 41 }, 42 { 43 Name: "Nil", 44 Input: nil, 45 Expected: nil, 46 }, 47 } 48 49 for _, testCase := range testCases { 50 t.Run(testCase.Name, func(t *testing.T) { 51 authConv := &automock.AuthConverter{} 52 if testCase.Input != nil { 53 authConv.On("ToGraphQL", testCase.Input.Auth).Return(testCase.Expected.Auth, nil) 54 } 55 converter := fetchrequest.NewConverter(authConv) 56 57 // WHEN 58 res, err := converter.ToGraphQL(testCase.Input) 59 60 // then 61 assert.NoError(t, err) 62 assert.Equal(t, testCase.Expected, res) 63 authConv.AssertExpectations(t) 64 }) 65 } 66 } 67 68 func TestConverter_InputFromGraphQL(t *testing.T) { 69 // GIVEN 70 testCases := []struct { 71 Name string 72 Input *graphql.FetchRequestInput 73 Expected *model.FetchRequestInput 74 }{ 75 { 76 Name: "All properties given", 77 Input: fixGQLFetchRequestInput("foo", "bar"), 78 Expected: fixModelFetchRequestInput("foo", "bar"), 79 }, 80 { 81 Name: "Empty", 82 Input: &graphql.FetchRequestInput{}, 83 Expected: &model.FetchRequestInput{}, 84 }, 85 { 86 Name: "Nil", 87 Input: nil, 88 Expected: nil, 89 }, 90 } 91 92 for _, testCase := range testCases { 93 t.Run(testCase.Name, func(t *testing.T) { 94 authConv := &automock.AuthConverter{} 95 if testCase.Input != nil { 96 authConv.On("InputFromGraphQL", testCase.Input.Auth).Return(testCase.Expected.Auth, nil) 97 } 98 converter := fetchrequest.NewConverter(authConv) 99 100 // WHEN 101 res, err := converter.InputFromGraphQL(testCase.Input) 102 103 // then 104 assert.NoError(t, err) 105 assert.Equal(t, testCase.Expected, res) 106 authConv.AssertExpectations(t) 107 }) 108 } 109 } 110 111 func TestConverter_FromEntity(t *testing.T) { 112 timestamp := time.Now() 113 114 // GIVEN 115 testCases := []struct { 116 Name string 117 Input *fetchrequest.Entity 118 Expected *model.FetchRequest 119 ExpectedErrMessage string 120 }{ 121 { 122 Name: "All properties given", 123 Input: fixFullFetchRequestEntity(t, "1", timestamp, model.APISpecFetchRequestReference), 124 Expected: fixFullFetchRequestModel("1", timestamp, model.APISpecFetchRequestReference), 125 ExpectedErrMessage: "", 126 }, 127 { 128 Name: "Empty value", 129 Input: &fetchrequest.Entity{ 130 ID: "2", 131 Auth: sql.NullString{}, 132 StatusTimestamp: timestamp, 133 StatusCondition: string(model.FetchRequestStatusConditionFailed), 134 }, 135 ExpectedErrMessage: "while determining object reference: incorrect Object Reference ID and its type for Entity with ID \"2\"", 136 }, 137 { 138 Name: "Error", 139 Input: &fetchrequest.Entity{ 140 Auth: repo.NewValidNullableString(`{Dd`), 141 SpecID: repo.NewValidNullableString("dd"), 142 }, 143 Expected: &model.FetchRequest{}, 144 ExpectedErrMessage: "while converting Auth: while unmarshalling Auth: invalid character 'D' looking for beginning of object key string", 145 }, 146 } 147 148 for _, testCase := range testCases { 149 t.Run(testCase.Name, func(t *testing.T) { 150 authConv := &automock.AuthConverter{} 151 conv := fetchrequest.NewConverter(authConv) 152 153 // WHEN 154 res, err := conv.FromEntity(testCase.Input, model.APISpecFetchRequestReference) 155 156 if testCase.ExpectedErrMessage != "" { 157 require.Error(t, err) 158 assert.Equal(t, testCase.ExpectedErrMessage, err.Error()) 159 return 160 } 161 162 // then 163 require.NoError(t, err) 164 assert.Equal(t, testCase.Expected, res) 165 }) 166 } 167 } 168 169 func TestConverter_ToEntity(t *testing.T) { 170 timestamp := time.Now() 171 172 // GIVEN 173 testCases := []struct { 174 Name string 175 Input *model.FetchRequest 176 Expected *fetchrequest.Entity 177 ExpectedErrMessage string 178 }{ 179 { 180 Name: "All properties given", 181 Input: fixFullFetchRequestModel("1", timestamp, model.APISpecFetchRequestReference), 182 Expected: fixFullFetchRequestEntity(t, "1", timestamp, model.APISpecFetchRequestReference), 183 }, 184 { 185 Name: "String value", 186 Input: fixFullFetchRequestModel("1", timestamp, model.APISpecFetchRequestReference), 187 Expected: fixFullFetchRequestEntity(t, "1", timestamp, model.APISpecFetchRequestReference), 188 }, 189 { 190 Name: "Empty Auth", 191 Input: &model.FetchRequest{ 192 ID: "2", 193 Status: &model.FetchRequestStatus{ 194 Timestamp: timestamp, 195 Condition: model.FetchRequestStatusConditionFailed, 196 }, 197 }, 198 Expected: &fetchrequest.Entity{ 199 ID: "2", 200 StatusTimestamp: timestamp, 201 StatusCondition: string(model.FetchRequestStatusConditionFailed), 202 }, 203 }, 204 { 205 Name: "Error", 206 Input: &model.FetchRequest{ 207 ID: "2", 208 }, 209 Expected: &fetchrequest.Entity{ 210 ID: "2", 211 }, 212 ExpectedErrMessage: apperrors.NewInvalidDataError("Invalid input model").Error(), 213 }, 214 } 215 216 for _, testCase := range testCases { 217 t.Run(testCase.Name, func(t *testing.T) { 218 authConv := &automock.AuthConverter{} 219 conv := fetchrequest.NewConverter(authConv) 220 221 // WHEN 222 res, err := conv.ToEntity(testCase.Input) 223 224 if testCase.ExpectedErrMessage != "" { 225 require.Error(t, err) 226 assert.Equal(t, testCase.ExpectedErrMessage, err.Error()) 227 return 228 } 229 230 // then 231 require.NoError(t, err) 232 assert.Equal(t, testCase.Expected, res) 233 }) 234 } 235 }