commiting code for test coverage
[dmaap/messagerouter/msgrtr.git] / src / test / java / com / att / nsa / cambria / utils / DMaaPResponseBuilderTest.java
1 /*-\r
2  * ============LICENSE_START=======================================================\r
3  * ONAP Policy Engine\r
4  * ================================================================================\r
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
6  * ================================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END=========================================================\r
19  */\r
20 \r
21 package com.att.nsa.cambria.utils;\r
22 \r
23 import static org.junit.Assert.*;\r
24 \r
25 import java.io.IOException;\r
26 \r
27 import org.json.JSONException;\r
28 import org.json.JSONObject;\r
29 import org.junit.After;\r
30 import org.junit.Before;\r
31 import org.junit.Test;\r
32 import org.springframework.mock.web.MockHttpServletRequest;\r
33 import org.springframework.mock.web.MockHttpServletResponse;\r
34 \r
35 import com.att.nsa.cambria.beans.DMaaPContext;\r
36 \r
37 public class DMaaPResponseBuilderTest {\r
38         \r
39         DMaaPContext dMaapContext;\r
40         MockHttpServletRequest request;\r
41         MockHttpServletResponse response;\r
42 \r
43         @Before\r
44         public void setUp() throws Exception {\r
45                 \r
46                 dMaapContext = new DMaaPContext();\r
47                 request = new MockHttpServletRequest();\r
48                 response = new MockHttpServletResponse();\r
49                 dMaapContext.setRequest(request);\r
50                 dMaapContext.setResponse(response);\r
51         }\r
52 \r
53         @After\r
54         public void tearDown() throws Exception {\r
55         }\r
56 \r
57         @Test\r
58         public void testsetNoCacheHeadings(){           \r
59                 DMaaPResponseBuilder.setNoCacheHeadings(dMaapContext);          \r
60                 assertEquals("no-cache", response.getHeader("Pragma"));\r
61         }\r
62         \r
63         @Test\r
64         public void testrespondOk() throws JSONException, IOException{\r
65                 JSONObject jsonObject = new JSONObject();\r
66                 jsonObject.put("Name", "Test");\r
67                 \r
68                 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);\r
69                 assertEquals("application/json", response.getContentType());\r
70                 assertEquals(200, response.getStatus());\r
71                 \r
72                 request.setMethod("HEAD");\r
73                 \r
74                 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);\r
75                 assertEquals("application/json", response.getContentType());\r
76                 assertEquals(200, response.getStatus());\r
77         }\r
78         \r
79         @Test\r
80         public void testrespondOkNoContent(){\r
81                 DMaaPResponseBuilder.respondOkNoContent(dMaapContext);\r
82                 assertEquals(204, response.getStatus());\r
83         }\r
84         \r
85         @Test\r
86         public void testrespondOkWithHtml(){\r
87                 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");\r
88                 \r
89                 assertEquals("text/html", response.getContentType());\r
90                 assertEquals(200, response.getStatus());\r
91         }\r
92         \r
93         @Test\r
94         public void testrespondWithError(){\r
95                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");\r
96                 assertEquals(500, response.getStatus());\r
97         }\r
98         \r
99         @Test\r
100         public void testrespondWithJsonError(){\r
101                 JSONObject o = new JSONObject();\r
102                 o.put("status", 500);\r
103                 o.put("message", "InternalServerError");\r
104                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, o);\r
105                 assertEquals(500, response.getStatus());\r
106         }\r
107         \r
108         @Test\r
109         public void testrespondWithErrorInJson(){\r
110                 DMaaPResponseBuilder.respondWithErrorInJson(dMaapContext, 500, "InternalServerError");\r
111                 \r
112                 assertEquals("application/json", response.getContentType());\r
113                 assertEquals(500, response.getStatus());\r
114         }\r
115         \r
116         @Test\r
117         public void testsendErrorAndBody(){\r
118                 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");\r
119                 \r
120                 assertEquals("text/html", response.getContentType());\r
121                 assertEquals(500, response.getStatus());\r
122                 \r
123                 request.setMethod("HEAD");\r
124                 \r
125                 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");\r
126                 \r
127                 assertEquals("text/html", response.getContentType());\r
128                 assertEquals(500, response.getStatus());\r
129                 \r
130         }\r
131         \r
132         @Test\r
133         public void testgetStreamForBinaryResponse() throws IOException{\r
134                 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);\r
135                 \r
136                 assertEquals("application/octet-stream", response.getContentType());\r
137                 assertEquals(200, response.getStatus());\r
138         }\r
139 \r
140 }\r