From 7a09b1f87213a49c6f3b266c6421b74b91e6bb90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Papou=C5=A1ek?= Date: Wed, 24 Jul 2024 12:43:16 +0200 Subject: [PATCH] Add service test for v2 endpoint --- internal/service/service_test.go | 59 +++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/internal/service/service_test.go b/internal/service/service_test.go index d0a730c8..e12f4574 100644 --- a/internal/service/service_test.go +++ b/internal/service/service_test.go @@ -27,7 +27,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestService(t *testing.T) { +func TestServiceV1(t *testing.T) { type testCase struct { name string mockData []byte @@ -91,3 +91,60 @@ func TestService(t *testing.T) { }) } } + +func TestServiceV2(t *testing.T) { + type testCase struct { + name string + mockData []byte + expectedAnError bool + } + + testCases := []testCase{ + { + name: "valid remote configuration stored", + mockData: []byte(validRemoteConfigurationJSON), + expectedAnError: false, + }, + { + name: "invalid remote configuration stored", + mockData: []byte("not a remote configuration"), + expectedAnError: true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + store := mockStorage{ + remoteConfig: tc.mockData, + } + repo := service.NewRepository(&store) + svc := service.New(repo) + + // Create the request: + req, err := http.NewRequest("GET", fmt.Sprintf("%s/v2/some_version/gathering_rules", service.APIPrefix), http.NoBody) + if err != nil { + t.Fatal(err) + } + + rr := httptest.NewRecorder() // Used to record the response. + handler := service.NewHandler(svc) + + router := mux.NewRouter() + + handler.Register(router) + + router.ServeHTTP(rr, req) + + if tc.expectedAnError { + assert.Equal(t, http.StatusInternalServerError, rr.Code) + assert.Contains(t, rr.Body.String(), "error") + } else { + assert.Equal(t, http.StatusOK, rr.Code) + assert.Contains( + t, + rr.Body.String(), + `{"conditional_gathering_rules":[{"conditions":["condition 1","condition 2"],"gathering_functions":"the gathering functions"}],"container_logs":[{"namespace":"namespace-1","pod_name_regex":"test regex","previous":true,"messages":["first message","second message"]}],"version":"0.0.1"}`) + } + }) + } +}