bump the version
[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.dmf.mr.beans.DMaaPContext;\r
36 import com.att.dmf.mr.utils.DMaaPResponseBuilder;\r
37 \r
38 public class DMaaPResponseBuilderTest {\r
39         \r
40         DMaaPContext dMaapContext;\r
41         MockHttpServletRequest request;\r
42         MockHttpServletResponse response;\r
43 \r
44         @Before\r
45         public void setUp() throws Exception {\r
46                 \r
47                 dMaapContext = new DMaaPContext();\r
48                 request = new MockHttpServletRequest();\r
49                 response = new MockHttpServletResponse();\r
50                 dMaapContext.setRequest(request);\r
51                 dMaapContext.setResponse(response);\r
52         }\r
53 \r
54         @After\r
55         public void tearDown() throws Exception {\r
56         }\r
57 \r
58         @Test\r
59         public void testsetNoCacheHeadings(){           \r
60                 DMaaPResponseBuilder.setNoCacheHeadings(dMaapContext);          \r
61                 assertEquals("no-cache", response.getHeader("Pragma"));\r
62         }\r
63         \r
64         @Test\r
65         public void testrespondOk() throws JSONException, IOException{\r
66                 JSONObject jsonObject = new JSONObject();\r
67                 jsonObject.put("Name", "Test");\r
68                 \r
69                 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);\r
70                 assertEquals("application/json", response.getContentType());\r
71                 assertEquals(200, response.getStatus());\r
72                 \r
73                 request.setMethod("HEAD");\r
74                 \r
75                 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);\r
76                 assertEquals("application/json", response.getContentType());\r
77                 assertEquals(200, response.getStatus());\r
78         }\r
79         \r
80         @Test\r
81         public void testrespondOkNoContent(){\r
82                 DMaaPResponseBuilder.respondOkNoContent(dMaapContext);\r
83                 assertEquals(204, response.getStatus());\r
84         }\r
85         \r
86         @Test\r
87         public void testrespondOkWithHtml(){\r
88                 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");\r
89                 \r
90                 assertEquals("text/html", response.getContentType());\r
91                 assertEquals(200, response.getStatus());\r
92         }\r
93         \r
94         @Test\r
95         public void testrespondWithError(){\r
96                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");\r
97                 assertEquals(500, response.getStatus());\r
98         }\r
99         \r
100         @Test\r
101         public void testrespondWithJsonError(){\r
102                 JSONObject o = new JSONObject();\r
103                 o.put("status", 500);\r
104                 o.put("message", "InternalServerError");\r
105                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, o);\r
106                 assertEquals(500, response.getStatus());\r
107         }\r
108         \r
109         @Test\r
110         public void testrespondWithErrorInJson(){\r
111                 DMaaPResponseBuilder.respondWithErrorInJson(dMaapContext, 500, "InternalServerError");\r
112                 \r
113                 assertEquals("application/json", response.getContentType());\r
114                 assertEquals(500, response.getStatus());\r
115         }\r
116         \r
117         @Test\r
118         public void testsendErrorAndBody(){\r
119                 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");\r
120                 \r
121                 assertEquals("text/html", response.getContentType());\r
122                 assertEquals(500, response.getStatus());\r
123                 \r
124                 request.setMethod("HEAD");\r
125                 \r
126                 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");\r
127                 \r
128                 assertEquals("text/html", response.getContentType());\r
129                 assertEquals(500, response.getStatus());\r
130                 \r
131         }\r
132         \r
133         @Test\r
134         public void testgetStreamForBinaryResponse() throws IOException{\r
135                 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);\r
136                 \r
137                 assertEquals("application/octet-stream", response.getContentType());\r
138                 assertEquals(200, response.getStatus());\r
139         }\r
140 \r
141 }\r