add testcases
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / mr / cambria / utils / DMaaPResponseBuilderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.mr.cambria.utils;
22
23 import static org.junit.Assert.*;
24
25 import java.io.IOException;
26
27 import org.json.JSONException;
28 import org.json.JSONObject;
29 import org.junit.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.springframework.mock.web.MockHttpServletRequest;
33 import org.springframework.mock.web.MockHttpServletResponse;
34
35 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
36 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
37
38 public class DMaaPResponseBuilderTest {
39         
40         DMaaPContext dMaapContext;
41         MockHttpServletRequest request;
42         MockHttpServletResponse response;
43
44         @Before
45         public void setUp() throws Exception {
46                 
47                 dMaapContext = new DMaaPContext();
48                 request = new MockHttpServletRequest();
49                 response = new MockHttpServletResponse();
50                 dMaapContext.setRequest(request);
51                 dMaapContext.setResponse(response);
52         }
53
54         @After
55         public void tearDown() throws Exception {
56         }
57
58         @Test
59         public void testsetNoCacheHeadings(){           
60                 DMaaPResponseBuilder.setNoCacheHeadings(dMaapContext);          
61                 assertEquals("no-cache", response.getHeader("Pragma"));
62         }
63         
64         @Test
65         public void testrespondOk() throws JSONException, IOException{
66                 JSONObject jsonObject = new JSONObject();
67                 jsonObject.put("Name", "Test");
68                 
69                 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);
70                 assertEquals("application/json", response.getContentType());
71                 assertEquals(200, response.getStatus());
72                 
73                 request.setMethod("HEAD");
74                 
75                 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);
76                 assertEquals("application/json", response.getContentType());
77                 assertEquals(200, response.getStatus());
78         }
79         
80         @Test
81         public void testrespondOkNoContent(){
82                 DMaaPResponseBuilder.respondOkNoContent(dMaapContext);
83                 assertEquals(204, response.getStatus());
84         }
85         
86         @Test
87         public void testrespondOkNoContentError(){
88                 dMaapContext.setResponse(null);
89                 DMaaPResponseBuilder.respondOkNoContent(dMaapContext);
90                 assertNull(dMaapContext.getResponse());
91         }
92         
93         @Test
94         public void testrespondOkWithHtml(){
95                 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
96                 
97                 assertEquals("text/html", response.getContentType());
98                 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
99                 assertEquals(200, response.getStatus());
100         }
101         
102         @Test
103         public void testrespondOkWithHtmlError(){
104                 dMaapContext.setResponse(null);
105                 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
106                 assertNull(dMaapContext.getResponse());
107         }
108         
109         @Test
110         public void testrespondWithError(){
111                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");
112                 assertEquals(500, response.getStatus());
113         }
114         
115         @Test(expected=NullPointerException.class)
116         public void testInvalidrespondWithError(){
117                 dMaapContext.setResponse(null);
118                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");
119         }
120         
121         @Test
122         public void testrespondWithJsonError(){
123                 JSONObject o = new JSONObject();
124                 o.put("status", 500);
125                 o.put("message", "InternalServerError");
126                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, o);
127                 assertEquals(500, response.getStatus());
128         }
129         
130         @Test
131         public void testInvalidrespondWithJsonError(){
132                 JSONObject o = new JSONObject();
133                 o.put("status", 500);
134                 o.put("message", "InternalServerError");
135                 dMaapContext.setResponse(null);
136                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, o);
137                 assertNull(dMaapContext.getResponse());
138         }
139         
140         @Test
141         public void testrespondWithErrorInJson(){
142                 DMaaPResponseBuilder.respondWithErrorInJson(dMaapContext, 500, "InternalServerError");
143                 
144                 assertEquals("application/json", response.getContentType());
145                 assertEquals(500, response.getStatus());
146         }
147         
148         @Test
149         public void testsendErrorAndBody(){
150                 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
151                 
152                 assertEquals("text/html", response.getContentType());
153                 assertEquals(500, response.getStatus());
154                 
155                 request.setMethod("HEAD");
156                 
157                 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
158                 
159                 assertEquals("text/html", response.getContentType());
160                 assertEquals(500, response.getStatus());
161                 
162         }
163         
164         @Test
165         public void testgetStreamForBinaryResponse() throws IOException{
166                 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);
167                 
168                 assertEquals("application/octet-stream", response.getContentType());
169                 assertEquals(200, response.getStatus());
170         }
171         
172         @Test(expected=NullPointerException.class)
173         public void testgetStreamForBinaryResponseError() throws IOException{
174                 dMaapContext.setResponse(null);
175                 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);
176         }
177
178 }