aaabb7d87786522ebbf6db6d41cf561b9274e638
[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 testrespondOkWithHtml(){
88                 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
89                 
90                 assertEquals("text/html", response.getContentType());
91                 assertEquals(200, response.getStatus());
92         }
93         
94         @Test
95         public void testrespondWithError(){
96                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");
97                 assertEquals(500, response.getStatus());
98         }
99         
100         @Test
101         public void testrespondWithJsonError(){
102                 JSONObject o = new JSONObject();
103                 o.put("status", 500);
104                 o.put("message", "InternalServerError");
105                 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, o);
106                 assertEquals(500, response.getStatus());
107         }
108         
109         @Test
110         public void testrespondWithErrorInJson(){
111                 DMaaPResponseBuilder.respondWithErrorInJson(dMaapContext, 500, "InternalServerError");
112                 
113                 assertEquals("application/json", response.getContentType());
114                 assertEquals(500, response.getStatus());
115         }
116         
117         @Test
118         public void testsendErrorAndBody(){
119                 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
120                 
121                 assertEquals("text/html", response.getContentType());
122                 assertEquals(500, response.getStatus());
123                 
124                 request.setMethod("HEAD");
125                 
126                 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
127                 
128                 assertEquals("text/html", response.getContentType());
129                 assertEquals(500, response.getStatus());
130                 
131         }
132         
133         @Test
134         public void testgetStreamForBinaryResponse() throws IOException{
135                 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);
136                 
137                 assertEquals("application/octet-stream", response.getContentType());
138                 assertEquals(200, response.getStatus());
139         }
140
141 }