github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/open_resource_discovery/handler_test.go (about) 1 package ord_test 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io" 7 "net/http" 8 "net/http/httptest" 9 "testing" 10 11 ord "github.com/kyma-incubator/compass/components/director/internal/open_resource_discovery" 12 "github.com/kyma-incubator/compass/components/director/internal/open_resource_discovery/automock" 13 "github.com/pkg/errors" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/mock" 16 ) 17 18 func TestHandler_AggregateORDData(t *testing.T) { 19 apiPath := "/aggregate" 20 metricsConfig := ord.MetricsConfig{} 21 testErr := errors.New("test error") 22 23 testCases := []struct { 24 Name string 25 RequestBody ord.AggregationResources 26 ORDService func() *automock.ORDService 27 ExpectedErrorOutput string 28 ExpectedStatusCode int 29 }{ 30 { 31 Name: "Successful ORD data aggregation", 32 RequestBody: ord.AggregationResources{ 33 ApplicationIDs: []string{appID}, 34 ApplicationTemplateIDs: []string{appTemplateID}, 35 }, 36 ORDService: func() *automock.ORDService { 37 svc := &automock.ORDService{} 38 svc.On("ProcessApplications", mock.Anything, metricsConfig, []string{appID}).Return(nil) 39 svc.On("ProcessApplicationTemplates", mock.Anything, metricsConfig, []string{appTemplateID}).Return(nil) 40 return svc 41 }, 42 ExpectedStatusCode: http.StatusOK, 43 }, 44 { 45 Name: "Successful ORD data aggregation - empty appIDs and appTemplateIDs", 46 RequestBody: ord.AggregationResources{ 47 ApplicationIDs: []string{}, 48 ApplicationTemplateIDs: []string{}, 49 }, 50 ORDService: func() *automock.ORDService { 51 svc := &automock.ORDService{} 52 svc.On("ProcessApplications", mock.Anything, metricsConfig, []string{}).Return(nil) 53 svc.On("ProcessApplicationTemplates", mock.Anything, metricsConfig, []string{}).Return(nil) 54 return svc 55 }, 56 ExpectedStatusCode: http.StatusOK, 57 }, 58 { 59 Name: "Aggregation failed for one or more applications", 60 RequestBody: ord.AggregationResources{ 61 ApplicationIDs: []string{}, 62 ApplicationTemplateIDs: []string{}, 63 }, 64 ORDService: func() *automock.ORDService { 65 svc := &automock.ORDService{} 66 svc.On("ProcessApplications", mock.Anything, metricsConfig, []string{}).Return(testErr) 67 return svc 68 }, 69 ExpectedErrorOutput: "ORD data aggregation failed for one or more applications", 70 ExpectedStatusCode: http.StatusInternalServerError, 71 }, 72 { 73 Name: "Aggregation failed for one or more application templates", 74 RequestBody: ord.AggregationResources{ 75 ApplicationIDs: []string{}, 76 ApplicationTemplateIDs: []string{}, 77 }, 78 ORDService: func() *automock.ORDService { 79 svc := &automock.ORDService{} 80 svc.On("ProcessApplications", mock.Anything, metricsConfig, []string{}).Return(nil) 81 svc.On("ProcessApplicationTemplates", mock.Anything, metricsConfig, []string{}).Return(testErr) 82 83 return svc 84 }, 85 ExpectedErrorOutput: "ORD data aggregation failed for one or more application templates", 86 ExpectedStatusCode: http.StatusInternalServerError, 87 }, 88 } 89 for _, testCase := range testCases { 90 t.Run(testCase.Name, func(t *testing.T) { 91 svc := testCase.ORDService() 92 defer mock.AssertExpectationsForObjects(t, svc) 93 94 handler := ord.NewORDAggregatorHTTPHandler(svc, metricsConfig) 95 requestBody, err := json.Marshal(testCase.RequestBody) 96 assert.NoError(t, err) 97 98 request := httptest.NewRequest(http.MethodPost, apiPath, bytes.NewBuffer(requestBody)) 99 writer := httptest.NewRecorder() 100 101 // WHEN 102 handler.AggregateORDData(writer, request) 103 104 // THEN 105 resp := writer.Result() 106 body, err := io.ReadAll(resp.Body) 107 assert.NoError(t, err) 108 109 if len(testCase.ExpectedErrorOutput) > 0 { 110 assert.Contains(t, string(body), testCase.ExpectedErrorOutput) 111 } else { 112 assert.NoError(t, err) 113 } 114 115 assert.Equal(t, testCase.ExpectedStatusCode, resp.StatusCode) 116 }) 117 } 118 }