2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
33 "github.com/gorilla/mux"
34 "github.com/stretchr/testify/mock"
35 "github.com/stretchr/testify/require"
36 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
37 "oransc.org/nonrtric/dmaapmediatorproducer/mocks/jobhandler"
40 func TestNewRouter(t *testing.T) {
41 assertions := require.New(t)
43 r := NewRouter(nil, nil)
44 statusRoute := r.Get("health_check")
45 assertions.NotNil(statusRoute)
46 supportedMethods, err := statusRoute.GetMethods()
47 assertions.Equal([]string{http.MethodGet}, supportedMethods)
49 path, _ := statusRoute.GetPathTemplate()
50 assertions.Equal("/health_check", path)
52 addJobRoute := r.Get("add")
53 assertions.NotNil(addJobRoute)
54 supportedMethods, err = addJobRoute.GetMethods()
55 assertions.Equal([]string{http.MethodPost}, supportedMethods)
57 path, _ = addJobRoute.GetPathTemplate()
58 assertions.Equal("/info_job", path)
60 deleteJobRoute := r.Get("delete")
61 assertions.NotNil(deleteJobRoute)
62 supportedMethods, err = deleteJobRoute.GetMethods()
63 assertions.Equal([]string{http.MethodDelete}, supportedMethods)
65 path, _ = deleteJobRoute.GetPathTemplate()
66 assertions.Equal("/info_job/{infoJobId}", path)
68 notFoundHandler := r.NotFoundHandler
69 handler := http.HandlerFunc(notFoundHandler.ServeHTTP)
70 responseRecorder := httptest.NewRecorder()
71 handler.ServeHTTP(responseRecorder, newRequest("GET", "/wrong", nil, t))
72 assertions.Equal(http.StatusNotFound, responseRecorder.Code)
73 assertions.Contains(responseRecorder.Body.String(), "404 not found.")
75 methodNotAllowedHandler := r.MethodNotAllowedHandler
76 handler = http.HandlerFunc(methodNotAllowedHandler.ServeHTTP)
77 responseRecorder = httptest.NewRecorder()
78 handler.ServeHTTP(responseRecorder, newRequest(http.MethodPut, "/status", nil, t))
79 assertions.Equal(http.StatusMethodNotAllowed, responseRecorder.Code)
80 assertions.Contains(responseRecorder.Body.String(), "Method is not supported.")
82 setLogLevelRoute := r.Get("setLogLevel")
83 assertions.NotNil(setLogLevelRoute)
84 supportedMethods, err = setLogLevelRoute.GetMethods()
85 assertions.Equal([]string{http.MethodPut}, supportedMethods)
87 path, _ = setLogLevelRoute.GetPathTemplate()
88 assertions.Equal("/admin/log", path)
91 func TestAddInfoJobHandler(t *testing.T) {
92 assertions := require.New(t)
105 name: "AddInfoJobHandler with correct job, should return OK",
110 InfoJobIdentity: "jobId",
113 InfoTypeIdentity: "type",
116 wantedStatus: http.StatusOK,
119 name: "AddInfoJobHandler with incorrect job info, should return BadRequest",
124 mockReturn: errors.New("error"),
126 wantedStatus: http.StatusBadRequest,
127 wantedBody: "Invalid job info. Cause: error",
130 for _, tt := range tests {
131 t.Run(tt.name, func(t *testing.T) {
132 jobHandlerMock := jobhandler.JobHandler{}
133 jobHandlerMock.On("AddJobFromRESTCall", tt.args.job).Return(tt.args.mockReturn)
135 callbackHandlerUnderTest := NewProducerCallbackHandler(&jobHandlerMock)
137 handler := http.HandlerFunc(callbackHandlerUnderTest.addInfoJobHandler)
138 responseRecorder := httptest.NewRecorder()
139 r := newRequest(http.MethodPost, "/jobs", &tt.args.job, t)
141 handler.ServeHTTP(responseRecorder, r)
143 assertions.Equal(tt.wantedStatus, responseRecorder.Code, tt.name)
144 assertions.Contains(responseRecorder.Body.String(), tt.wantedBody, tt.name)
145 jobHandlerMock.AssertCalled(t, "AddJobFromRESTCall", tt.args.job)
150 func TestDeleteJob(t *testing.T) {
151 assertions := require.New(t)
152 jobHandlerMock := jobhandler.JobHandler{}
153 jobHandlerMock.On("DeleteJobFromRESTCall", mock.Anything).Return(nil)
155 callbackHandlerUnderTest := NewProducerCallbackHandler(&jobHandlerMock)
157 responseRecorder := httptest.NewRecorder()
158 r := mux.SetURLVars(newRequest(http.MethodDelete, "/jobs/", nil, t), map[string]string{"infoJobId": "job1"})
159 handler := http.HandlerFunc(callbackHandlerUnderTest.deleteInfoJobHandler)
160 handler.ServeHTTP(responseRecorder, r)
161 assertions.Equal(http.StatusOK, responseRecorder.Result().StatusCode)
163 assertions.Equal("", responseRecorder.Body.String())
165 jobHandlerMock.AssertCalled(t, "DeleteJobFromRESTCall", "job1")
168 func TestSetLogLevel(t *testing.T) {
169 assertions := require.New(t)
181 name: "Set to valid log level, should return OK",
185 wantedStatus: http.StatusOK,
188 name: "Set to invalid log level, should return BadRequest",
192 wantedStatus: http.StatusBadRequest,
193 wantedBody: "Invalid log level: bad",
196 for _, tt := range tests {
197 t.Run(tt.name, func(t *testing.T) {
198 callbackHandlerUnderTest := NewProducerCallbackHandler(nil)
200 handler := http.HandlerFunc(callbackHandlerUnderTest.setLogLevel)
201 responseRecorder := httptest.NewRecorder()
202 r, _ := http.NewRequest(http.MethodPut, "/admin/log?level="+tt.args.logLevel, nil)
204 handler.ServeHTTP(responseRecorder, r)
206 assertions.Equal(tt.wantedStatus, responseRecorder.Code, tt.name)
207 assertions.Contains(responseRecorder.Body.String(), tt.wantedBody, tt.name)
212 func newRequest(method string, url string, jobInfo *jobs.JobInfo, t *testing.T) *http.Request {
215 bodyAsBytes, _ := json.Marshal(jobInfo)
216 body = ioutil.NopCloser(bytes.NewReader(bodyAsBytes))
218 if req, err := http.NewRequest(method, url, body); err == nil {
221 t.Fatalf("Could not create request due to: %v", err)