X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Fserver%2Fserver_test.go;h=6fe4d7a70a0bf89e09dd19b101838a2ef6cd2ca2;hb=62f766fc264c9923ec3c2bf4eec80a326621cff1;hp=2fe936fad9709501cb4eb6bc6a571b88f311393f;hpb=fe61c6191ba72b1d8297264c3d61566ea23a70b6;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/server/server_test.go b/dmaap-mediator-producer/internal/server/server_test.go index 2fe936fa..6fe4d7a7 100644 --- a/dmaap-mediator-producer/internal/server/server_test.go +++ b/dmaap-mediator-producer/internal/server/server_test.go @@ -34,36 +34,42 @@ import ( "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs" - "oransc.org/nonrtric/dmaapmediatorproducer/mocks/jobhandler" + "oransc.org/nonrtric/dmaapmediatorproducer/mocks/jobshandler" ) func TestNewRouter(t *testing.T) { assertions := require.New(t) - r := NewRouter() - statusRoute := r.Get("status") + + r := NewRouter(nil, nil) + statusRoute := r.Get("health_check") assertions.NotNil(statusRoute) supportedMethods, err := statusRoute.GetMethods() assertions.Equal([]string{http.MethodGet}, supportedMethods) assertions.Nil(err) + path, _ := statusRoute.GetPathTemplate() + assertions.Equal("/health_check", path) addJobRoute := r.Get("add") assertions.NotNil(addJobRoute) supportedMethods, err = addJobRoute.GetMethods() assertions.Equal([]string{http.MethodPost}, supportedMethods) assertions.Nil(err) + path, _ = addJobRoute.GetPathTemplate() + assertions.Equal("/info_job", path) deleteJobRoute := r.Get("delete") assertions.NotNil(deleteJobRoute) supportedMethods, err = deleteJobRoute.GetMethods() assertions.Equal([]string{http.MethodDelete}, supportedMethods) assertions.Nil(err) + path, _ = deleteJobRoute.GetPathTemplate() + assertions.Equal("/info_job/{infoJobId}", path) notFoundHandler := r.NotFoundHandler handler := http.HandlerFunc(notFoundHandler.ServeHTTP) responseRecorder := httptest.NewRecorder() handler.ServeHTTP(responseRecorder, newRequest("GET", "/wrong", nil, t)) assertions.Equal(http.StatusNotFound, responseRecorder.Code) - assertions.Contains(responseRecorder.Body.String(), "404 not found.") methodNotAllowedHandler := r.MethodNotAllowedHandler @@ -71,68 +77,51 @@ func TestNewRouter(t *testing.T) { responseRecorder = httptest.NewRecorder() handler.ServeHTTP(responseRecorder, newRequest(http.MethodPut, "/status", nil, t)) assertions.Equal(http.StatusMethodNotAllowed, responseRecorder.Code) - assertions.Contains(responseRecorder.Body.String(), "Method is not supported.") -} -func TestStatusHandler(t *testing.T) { - assertions := require.New(t) - responseRecorder := httptest.NewRecorder() - r := newRequest(http.MethodGet, "/status", nil, t) - handler := http.HandlerFunc(statusHandler) - handler.ServeHTTP(responseRecorder, r) - assertions.Equal(http.StatusOK, responseRecorder.Code) - - assertions.Equal("", responseRecorder.Body.String()) + setLogLevelRoute := r.Get("setLogLevel") + assertions.NotNil(setLogLevelRoute) + supportedMethods, err = setLogLevelRoute.GetMethods() + assertions.Equal([]string{http.MethodPut}, supportedMethods) + assertions.Nil(err) + path, _ = setLogLevelRoute.GetPathTemplate() + assertions.Equal("/admin/log", path) } -func TestAddInfoJobHandler(t *testing.T) { +func TestAddInfoJobToJobsHandler(t *testing.T) { assertions := require.New(t) - jobHandlerMock := jobhandler.JobHandler{} - - goodJobInfo := jobs.JobInfo{ - Owner: "owner", - LastUpdated: "now", - InfoJobIdentity: "jobId", - TargetUri: "target", - InfoJobData: "{}", - InfoTypeIdentity: "type", - } - badJobInfo := jobs.JobInfo{ - Owner: "bad", - } - jobHandlerMock.On("AddJob", goodJobInfo).Return(nil) - jobHandlerMock.On("AddJob", badJobInfo).Return(errors.New("error")) - jobs.Handler = &jobHandlerMock type args struct { - responseRecorder *httptest.ResponseRecorder - r *http.Request + job jobs.JobInfo + mockReturn error } tests := []struct { name string args args wantedStatus int wantedBody string - assertFunc assertMockFunk }{ { - name: "AddInfoJobHandler with correct path and method, should return OK", + name: "AddInfoJobToJobsHandler with correct job, should return OK", args: args{ - responseRecorder: httptest.NewRecorder(), - r: newRequest(http.MethodPost, "/jobs", &goodJobInfo, t), + job: jobs.JobInfo{ + Owner: "owner", + LastUpdated: "now", + InfoJobIdentity: "jobId", + TargetUri: "target", + InfoJobData: jobs.Parameters{}, + InfoTypeIdentity: "type", + }, }, wantedStatus: http.StatusOK, - wantedBody: "", - assertFunc: func(mock *jobhandler.JobHandler) { - mock.AssertCalled(t, "AddJob", goodJobInfo) - }, }, { - name: "AddInfoJobHandler with incorrect job info, should return BadRequest", + name: "AddInfoJobToJobsHandler with incorrect job info, should return BadRequest", args: args{ - responseRecorder: httptest.NewRecorder(), - r: newRequest(http.MethodPost, "/jobs", &badJobInfo, t), + job: jobs.JobInfo{ + Owner: "bad", + }, + mockReturn: errors.New("error"), }, wantedStatus: http.StatusBadRequest, wantedBody: "Invalid job info. Cause: error", @@ -140,38 +129,85 @@ func TestAddInfoJobHandler(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - handler := http.HandlerFunc(addInfoJobHandler) - handler.ServeHTTP(tt.args.responseRecorder, tt.args.r) - assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code, tt.name) + jobsHandlerMock := jobshandler.JobsHandler{} + jobsHandlerMock.On("AddJobFromRESTCall", tt.args.job).Return(tt.args.mockReturn) + + callbackHandlerUnderTest := NewProducerCallbackHandler(&jobsHandlerMock) - assertions.Contains(tt.args.responseRecorder.Body.String(), tt.wantedBody, tt.name) + handler := http.HandlerFunc(callbackHandlerUnderTest.addInfoJobHandler) + responseRecorder := httptest.NewRecorder() + r := newRequest(http.MethodPost, "/jobs", &tt.args.job, t) - if tt.assertFunc != nil { - tt.assertFunc(&jobHandlerMock) - } + handler.ServeHTTP(responseRecorder, r) + + assertions.Equal(tt.wantedStatus, responseRecorder.Code, tt.name) + assertions.Contains(responseRecorder.Body.String(), tt.wantedBody, tt.name) + jobsHandlerMock.AssertCalled(t, "AddJobFromRESTCall", tt.args.job) }) } } func TestDeleteJob(t *testing.T) { assertions := require.New(t) - jobHandlerMock := jobhandler.JobHandler{} + jobsHandlerMock := jobshandler.JobsHandler{} + jobsHandlerMock.On("DeleteJobFromRESTCall", mock.Anything).Return(nil) - jobHandlerMock.On("DeleteJob", mock.Anything).Return(nil) - jobs.Handler = &jobHandlerMock + callbackHandlerUnderTest := NewProducerCallbackHandler(&jobsHandlerMock) responseRecorder := httptest.NewRecorder() r := mux.SetURLVars(newRequest(http.MethodDelete, "/jobs/", nil, t), map[string]string{"infoJobId": "job1"}) - handler := http.HandlerFunc(deleteInfoJobHandler) + handler := http.HandlerFunc(callbackHandlerUnderTest.deleteInfoJobHandler) handler.ServeHTTP(responseRecorder, r) - assertions.Equal(http.StatusOK, http.StatusOK) + assertions.Equal(http.StatusOK, responseRecorder.Result().StatusCode) assertions.Equal("", responseRecorder.Body.String()) - jobHandlerMock.AssertCalled(t, "DeleteJob", "job1") + jobsHandlerMock.AssertCalled(t, "DeleteJobFromRESTCall", "job1") } -type assertMockFunk func(mock *jobhandler.JobHandler) +func TestSetLogLevel(t *testing.T) { + assertions := require.New(t) + + type args struct { + logLevel string + } + tests := []struct { + name string + args args + wantedStatus int + wantedBody string + }{ + { + name: "Set to valid log level, should return OK", + args: args{ + logLevel: "Debug", + }, + wantedStatus: http.StatusOK, + }, + { + name: "Set to invalid log level, should return BadRequest", + args: args{ + logLevel: "bad", + }, + wantedStatus: http.StatusBadRequest, + wantedBody: "Invalid log level: bad", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + callbackHandlerUnderTest := NewProducerCallbackHandler(nil) + + handler := http.HandlerFunc(callbackHandlerUnderTest.setLogLevel) + responseRecorder := httptest.NewRecorder() + r, _ := http.NewRequest(http.MethodPut, "/admin/log?level="+tt.args.logLevel, nil) + + handler.ServeHTTP(responseRecorder, r) + + assertions.Equal(tt.wantedStatus, responseRecorder.Code, tt.name) + assertions.Contains(responseRecorder.Body.String(), tt.wantedBody, tt.name) + }) + } +} func newRequest(method string, url string, jobInfo *jobs.JobInfo, t *testing.T) *http.Request { var body io.Reader