Add kafka jobs to DMaaP Mediator Producer
[nonrtric.git] / dmaap-mediator-producer / internal / server / server_test.go
index d221c93..6fe4d7a 100644 (file)
@@ -30,16 +30,70 @@ import (
        "net/http/httptest"
        "testing"
 
+       "github.com/gorilla/mux"
+       "github.com/stretchr/testify/mock"
        "github.com/stretchr/testify/require"
        "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
-       "oransc.org/nonrtric/dmaapmediatorproducer/mocks"
+       "oransc.org/nonrtric/dmaapmediatorproducer/mocks/jobshandler"
 )
 
-func TestStatusHandler(t *testing.T) {
+func TestNewRouter(t *testing.T) {
        assertions := require.New(t)
+
+       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
+       handler = http.HandlerFunc(methodNotAllowedHandler.ServeHTTP)
+       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.")
+
+       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 TestAddInfoJobToJobsHandler(t *testing.T) {
+       assertions := require.New(t)
+
        type args struct {
-               responseRecorder *httptest.ResponseRecorder
-               r                *http.Request
+               job        jobs.JobInfo
+               mockReturn error
        }
        tests := []struct {
                name         string
@@ -48,66 +102,74 @@ func TestStatusHandler(t *testing.T) {
                wantedBody   string
        }{
                {
-                       name: "StatusHandler with correct path and method, should return OK",
+                       name: "AddInfoJobToJobsHandler with correct job, should return OK",
                        args: args{
-                               responseRecorder: httptest.NewRecorder(),
-                               r:                newRequest("GET", "/", nil, t),
+                               job: jobs.JobInfo{
+                                       Owner:            "owner",
+                                       LastUpdated:      "now",
+                                       InfoJobIdentity:  "jobId",
+                                       TargetUri:        "target",
+                                       InfoJobData:      jobs.Parameters{},
+                                       InfoTypeIdentity: "type",
+                               },
                        },
                        wantedStatus: http.StatusOK,
-                       wantedBody:   "All is well!",
                },
                {
-                       name: "StatusHandler with incorrect path, should return NotFound",
+                       name: "AddInfoJobToJobsHandler with incorrect job info, should return BadRequest",
                        args: args{
-                               responseRecorder: httptest.NewRecorder(),
-                               r:                newRequest("GET", "/wrong", nil, t),
+                               job: jobs.JobInfo{
+                                       Owner: "bad",
+                               },
+                               mockReturn: errors.New("error"),
                        },
-                       wantedStatus: http.StatusNotFound,
-                       wantedBody:   "404 not found.\n",
-               },
-               {
-                       name: "StatusHandler with incorrect method, should return MethodNotAllowed",
-                       args: args{
-                               responseRecorder: httptest.NewRecorder(),
-                               r:                newRequest("PUT", "/", nil, t),
-                       },
-                       wantedStatus: http.StatusMethodNotAllowed,
-                       wantedBody:   "Method is not supported.\n",
+                       wantedStatus: http.StatusBadRequest,
+                       wantedBody:   "Invalid job info. Cause: error",
                },
        }
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
-                       handler := http.HandlerFunc(StatusHandler)
-                       handler.ServeHTTP(tt.args.responseRecorder, tt.args.r)
-                       assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code)
+                       jobsHandlerMock := jobshandler.JobsHandler{}
+                       jobsHandlerMock.On("AddJobFromRESTCall", tt.args.job).Return(tt.args.mockReturn)
+
+                       callbackHandlerUnderTest := NewProducerCallbackHandler(&jobsHandlerMock)
+
+                       handler := http.HandlerFunc(callbackHandlerUnderTest.addInfoJobHandler)
+                       responseRecorder := httptest.NewRecorder()
+                       r := newRequest(http.MethodPost, "/jobs", &tt.args.job, t)
 
-                       assertions.Equal(tt.wantedBody, tt.args.responseRecorder.Body.String())
+                       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 TestCreateInfoJobHandler(t *testing.T) {
+func TestDeleteJob(t *testing.T) {
+       assertions := require.New(t)
+       jobsHandlerMock := jobshandler.JobsHandler{}
+       jobsHandlerMock.On("DeleteJobFromRESTCall", mock.Anything).Return(nil)
+
+       callbackHandlerUnderTest := NewProducerCallbackHandler(&jobsHandlerMock)
+
+       responseRecorder := httptest.NewRecorder()
+       r := mux.SetURLVars(newRequest(http.MethodDelete, "/jobs/", nil, t), map[string]string{"infoJobId": "job1"})
+       handler := http.HandlerFunc(callbackHandlerUnderTest.deleteInfoJobHandler)
+       handler.ServeHTTP(responseRecorder, r)
+       assertions.Equal(http.StatusOK, responseRecorder.Result().StatusCode)
+
+       assertions.Equal("", responseRecorder.Body.String())
+
+       jobsHandlerMock.AssertCalled(t, "DeleteJobFromRESTCall", "job1")
+}
+
+func TestSetLogLevel(t *testing.T) {
        assertions := require.New(t)
-       jobHandlerMock := mocks.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
+               logLevel string
        }
        tests := []struct {
                name         string
@@ -116,49 +178,33 @@ func TestCreateInfoJobHandler(t *testing.T) {
                wantedBody   string
        }{
                {
-                       name: "CreateInfoJobHandler with correct path and method, should return OK",
+                       name: "Set to valid log level, should return OK",
                        args: args{
-                               responseRecorder: httptest.NewRecorder(),
-                               r:                newRequest("POST", "/producer_simulator/info_job", &goodJobInfo, t),
+                               logLevel: "Debug",
                        },
                        wantedStatus: http.StatusOK,
-                       wantedBody:   "",
                },
                {
-                       name: "CreateInfoJobHandler with incorrect job info, should return BadRequest",
+                       name: "Set to invalid log level, should return BadRequest",
                        args: args{
-                               responseRecorder: httptest.NewRecorder(),
-                               r:                newRequest("POST", "/producer_simulator/info_job", &badJobInfo, t),
+                               logLevel: "bad",
                        },
                        wantedStatus: http.StatusBadRequest,
-                       wantedBody:   "Invalid job info. Cause: error",
-               },
-               {
-                       name: "CreateInfoJobHandler with incorrect path, should return NotFound",
-                       args: args{
-                               responseRecorder: httptest.NewRecorder(),
-                               r:                newRequest("GET", "/wrong", nil, t),
-                       },
-                       wantedStatus: http.StatusNotFound,
-                       wantedBody:   "404 not found.",
-               },
-               {
-                       name: "CreateInfoJobHandler with incorrect method, should return MethodNotAllowed",
-                       args: args{
-                               responseRecorder: httptest.NewRecorder(),
-                               r:                newRequest("PUT", "/producer_simulator/info_job", nil, t),
-                       },
-                       wantedStatus: http.StatusMethodNotAllowed,
-                       wantedBody:   "Method is not supported.",
+                       wantedBody:   "Invalid log level: bad",
                },
        }
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
-                       handler := http.HandlerFunc(CreateInfoJobHandler)
-                       handler.ServeHTTP(tt.args.responseRecorder, tt.args.r)
-                       assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code)
+                       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.Contains(tt.args.responseRecorder.Body.String(), tt.wantedBody)
+                       assertions.Equal(tt.wantedStatus, responseRecorder.Code, tt.name)
+                       assertions.Contains(responseRecorder.Body.String(), tt.wantedBody, tt.name)
                })
        }
 }