[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / resources / ResponseBuilderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2019 NOKIA Intellectual Property. All rights reserved.
6  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.dmaap.dbcapi.resources;
22
23 import org.junit.Test;
24 import org.onap.dmaap.dbcapi.model.ApiError;
25
26 import javax.ws.rs.core.Response;
27
28 import static javax.ws.rs.core.Response.Status.NOT_FOUND;
29 import static javax.ws.rs.core.Response.Status.SERVICE_UNAVAILABLE;
30 import static javax.ws.rs.core.Response.Status.UNAUTHORIZED;
31 import static org.junit.Assert.assertEquals;
32
33 public class ResponseBuilderTest {
34
35     private static final String OBJECT = "Objcect";
36     private static final String MESSAGE = "msg";
37     private static final int CODE = 100;
38     private ResponseBuilder responseBuilder = new ResponseBuilder();
39
40     @Test
41     public void success_shouldCreateResponseWithOKStatusCode() {
42
43         Response response = responseBuilder.success(OBJECT);
44
45         assertEquals(OBJECT, response.getEntity());
46         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
47     }
48
49     @Test
50     public void success_shouldCreateResponseWithDefinedStatusCode() {
51
52         Response response = responseBuilder.success(CODE, OBJECT);
53
54         assertEquals(OBJECT, response.getEntity());
55         assertEquals(CODE, response.getStatus());
56     }
57
58     @Test
59     public void unauthorized_shouldCreateCorrectResponse() {
60
61         ApiError error = new ApiError(UNAUTHORIZED.getStatusCode(), MESSAGE, "Authorization");
62         Response response = responseBuilder.unauthorized(MESSAGE);
63
64         assertEquals(error, response.getEntity());
65         assertEquals(error.getCode(), response.getStatus());
66     }
67
68     @Test
69     public void unavailable_shouldCreateCorrectResponse() {
70
71         ApiError error = new ApiError(SERVICE_UNAVAILABLE.getStatusCode(),
72                 "Request is unavailable due to unexpected condition");
73         Response response = responseBuilder.unavailable();
74
75         assertEquals(error, response.getEntity());
76         assertEquals(error.getCode(), response.getStatus());
77     }
78
79     @Test
80     public void notFound_shouldCreateCorrectResponse() {
81         ApiError error = new ApiError(NOT_FOUND.getStatusCode(), "Requested object not found");
82         Response response = responseBuilder.notFound();
83
84         assertEquals(error, response.getEntity());
85         assertEquals(error.getCode(), response.getStatus());
86     }
87
88     @Test
89     public void error_shouldCreateCorrectResponse() {
90         ApiError error = new ApiError(CODE, "Some Error");
91         Response response = responseBuilder.error(error);
92
93         assertEquals(error, response.getEntity());
94         assertEquals(error.getCode(), response.getStatus());
95     }
96 }