github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/model/app_template_test.go (about) 1 package model_test 2 3 import ( 4 "fmt" 5 "testing" 6 7 "github.com/kyma-incubator/compass/components/director/pkg/str" 8 9 "github.com/kyma-incubator/compass/components/director/internal/model" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestApplicationTemplateInput_ToApplicationTemplate(t *testing.T) { 14 // GIVEN 15 testID := "test" 16 testName := "name" 17 testDescription := str.Ptr("desc") 18 testAppInputJSON := `{"Name": "app"}` 19 testPlaceholders := []model.ApplicationTemplatePlaceholder{ 20 {Name: "a", Description: str.Ptr("c"), JSONPath: str.Ptr("e")}, 21 {Name: "b", Description: str.Ptr("d"), JSONPath: str.Ptr("f")}, 22 } 23 testAccessLevel := model.GlobalApplicationTemplateAccessLevel 24 25 webhookMode := model.WebhookModeSync 26 webhookURL := "foourl" 27 testWebhooks := []*model.WebhookInput{ 28 { 29 Type: model.WebhookTypeConfigurationChanged, 30 URL: &webhookURL, 31 Mode: &webhookMode, 32 }, 33 } 34 expectedTestWebhooks := []model.Webhook{ 35 { 36 ObjectID: testID, 37 ObjectType: model.ApplicationTemplateWebhookReference, 38 Type: testWebhooks[0].Type, 39 URL: testWebhooks[0].URL, 40 Mode: testWebhooks[0].Mode, 41 }, 42 } 43 44 testCases := []struct { 45 Name string 46 Input *model.ApplicationTemplateInput 47 Expected model.ApplicationTemplate 48 }{ 49 { 50 Name: "All properties given", 51 Input: &model.ApplicationTemplateInput{ 52 Name: testName, 53 Description: testDescription, 54 ApplicationNamespace: str.Ptr("ns"), 55 ApplicationInputJSON: testAppInputJSON, 56 Placeholders: testPlaceholders, 57 AccessLevel: testAccessLevel, 58 Labels: map[string]interface{}{"test": "test"}, 59 Webhooks: testWebhooks, 60 }, 61 Expected: model.ApplicationTemplate{ 62 ID: testID, 63 Name: testName, 64 Description: testDescription, 65 ApplicationNamespace: str.Ptr("ns"), 66 ApplicationInputJSON: testAppInputJSON, 67 Placeholders: testPlaceholders, 68 AccessLevel: testAccessLevel, 69 Webhooks: expectedTestWebhooks, 70 }, 71 }, 72 { 73 Name: "Nil", 74 Input: nil, 75 Expected: model.ApplicationTemplate{}, 76 }, 77 } 78 79 for i, testCase := range testCases { 80 t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) { 81 // WHEN 82 result := testCase.Input.ToApplicationTemplate(testID) 83 84 for i, webhook := range result.Webhooks { 85 testCase.Expected.Webhooks[i].ID = webhook.ID 86 } 87 88 // THEN 89 assert.Equal(t, testCase.Expected, result) 90 }) 91 } 92 } 93 94 func TestApplicationFromTemplateInputValues_FindPlaceholderValue(t *testing.T) { 95 // GIVEN 96 var values model.ApplicationFromTemplateInputValues = []*model.ApplicationTemplateValueInput{ 97 {Placeholder: "a", Value: "foo"}, 98 {Placeholder: "b", Value: "bar"}, 99 } 100 101 expectedSuccessRes := "foo" 102 expectedErr := fmt.Errorf("value for placeholder name '%s' not found", "baz") 103 104 testCases := []struct { 105 Name string 106 Input string 107 ExpectedResult *string 108 ExpectedError *error 109 }{ 110 { 111 Name: "Success", 112 Input: "a", 113 ExpectedResult: &expectedSuccessRes, 114 }, 115 { 116 Name: "Error", 117 Input: "baz", 118 ExpectedError: &expectedErr, 119 }, 120 } 121 122 for i, testCase := range testCases { 123 t.Run(fmt.Sprintf("%d: %s", i, testCase.Name), func(t *testing.T) { 124 // WHEN 125 result, err := values.FindPlaceholderValue(testCase.Input) 126 127 // THEN 128 if testCase.ExpectedResult != nil { 129 assert.Equal(t, testCase.ExpectedResult, &result) 130 } 131 132 if testCase.ExpectedError != nil { 133 assert.Equal(t, *testCase.ExpectedError, err) 134 } 135 }) 136 } 137 }