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/stretchr/testify/require"
34 "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
35 "oransc.org/nonrtric/dmaapmediatorproducer/mocks"
38 func TestStatusHandler(t *testing.T) {
39 assertions := require.New(t)
41 responseRecorder *httptest.ResponseRecorder
51 name: "StatusHandler with correct path and method, should return OK",
53 responseRecorder: httptest.NewRecorder(),
54 r: newRequest("GET", "/", nil, t),
56 wantedStatus: http.StatusOK,
57 wantedBody: "All is well!",
60 name: "StatusHandler with incorrect path, should return NotFound",
62 responseRecorder: httptest.NewRecorder(),
63 r: newRequest("GET", "/wrong", nil, t),
65 wantedStatus: http.StatusNotFound,
66 wantedBody: "404 not found.\n",
69 name: "StatusHandler with incorrect method, should return MethodNotAllowed",
71 responseRecorder: httptest.NewRecorder(),
72 r: newRequest("PUT", "/", nil, t),
74 wantedStatus: http.StatusMethodNotAllowed,
75 wantedBody: "Method is not supported.\n",
78 for _, tt := range tests {
79 t.Run(tt.name, func(t *testing.T) {
80 handler := http.HandlerFunc(StatusHandler)
81 handler.ServeHTTP(tt.args.responseRecorder, tt.args.r)
82 assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code)
84 assertions.Equal(tt.wantedBody, tt.args.responseRecorder.Body.String())
89 func TestCreateInfoJobHandler(t *testing.T) {
90 assertions := require.New(t)
91 jobHandlerMock := mocks.JobHandler{}
93 goodJobInfo := jobs.JobInfo{
96 InfoJobIdentity: "jobId",
99 InfoTypeIdentity: "type",
101 badJobInfo := jobs.JobInfo{
104 jobHandlerMock.On("AddJob", goodJobInfo).Return(nil)
105 jobHandlerMock.On("AddJob", badJobInfo).Return(errors.New("error"))
106 jobs.Handler = &jobHandlerMock
109 responseRecorder *httptest.ResponseRecorder
119 name: "CreateInfoJobHandler with correct path and method, should return OK",
121 responseRecorder: httptest.NewRecorder(),
122 r: newRequest("POST", "/producer_simulator/info_job", &goodJobInfo, t),
124 wantedStatus: http.StatusOK,
128 name: "CreateInfoJobHandler with incorrect job info, should return BadRequest",
130 responseRecorder: httptest.NewRecorder(),
131 r: newRequest("POST", "/producer_simulator/info_job", &badJobInfo, t),
133 wantedStatus: http.StatusBadRequest,
134 wantedBody: "Invalid job info. Cause: error",
137 name: "CreateInfoJobHandler with incorrect path, should return NotFound",
139 responseRecorder: httptest.NewRecorder(),
140 r: newRequest("GET", "/wrong", nil, t),
142 wantedStatus: http.StatusNotFound,
143 wantedBody: "404 not found.",
146 name: "CreateInfoJobHandler with incorrect method, should return MethodNotAllowed",
148 responseRecorder: httptest.NewRecorder(),
149 r: newRequest("PUT", "/producer_simulator/info_job", nil, t),
151 wantedStatus: http.StatusMethodNotAllowed,
152 wantedBody: "Method is not supported.",
155 for _, tt := range tests {
156 t.Run(tt.name, func(t *testing.T) {
157 handler := http.HandlerFunc(CreateInfoJobHandler)
158 handler.ServeHTTP(tt.args.responseRecorder, tt.args.r)
159 assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code)
161 assertions.Contains(tt.args.responseRecorder.Body.String(), tt.wantedBody)
166 func newRequest(method string, url string, jobInfo *jobs.JobInfo, t *testing.T) *http.Request {
169 bodyAsBytes, _ := json.Marshal(jobInfo)
170 body = ioutil.NopCloser(bytes.NewReader(bodyAsBytes))
172 if req, err := http.NewRequest(method, url, body); err == nil {
175 t.Fatalf("Could not create request due to: %v", err)