2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.dmaap.mr.cambria.utils;
23 import static org.junit.Assert.*;
25 import java.io.IOException;
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;
35 import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
36 import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
38 public class DMaaPResponseBuilderTest {
40 DMaaPContext dMaapContext;
41 MockHttpServletRequest request;
42 MockHttpServletResponse response;
45 public void setUp() throws Exception {
47 dMaapContext = new DMaaPContext();
48 request = new MockHttpServletRequest();
49 response = new MockHttpServletResponse();
50 dMaapContext.setRequest(request);
51 dMaapContext.setResponse(response);
55 public void tearDown() throws Exception {
59 public void testsetNoCacheHeadings(){
60 DMaaPResponseBuilder.setNoCacheHeadings(dMaapContext);
61 assertEquals("no-cache", response.getHeader("Pragma"));
65 public void testrespondOk() throws JSONException, IOException{
66 JSONObject jsonObject = new JSONObject();
67 jsonObject.put("Name", "Test");
69 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);
70 assertEquals("application/json", response.getContentType());
71 assertEquals(200, response.getStatus());
73 request.setMethod("HEAD");
75 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);
76 assertEquals("application/json", response.getContentType());
77 assertEquals(200, response.getStatus());
81 public void testrespondOkNoContent(){
82 DMaaPResponseBuilder.respondOkNoContent(dMaapContext);
83 assertEquals(204, response.getStatus());
87 public void testrespondOkNoContentError(){
88 dMaapContext.setResponse(null);
89 DMaaPResponseBuilder.respondOkNoContent(dMaapContext);
90 assertNull(dMaapContext.getResponse());
94 public void testrespondOkWithHtml(){
95 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
97 assertEquals("text/html", response.getContentType());
98 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
99 assertEquals(200, response.getStatus());
103 public void testrespondOkWithHtmlError(){
104 dMaapContext.setResponse(null);
105 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
106 assertNull(dMaapContext.getResponse());
110 public void testrespondWithError(){
111 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");
112 assertEquals(500, response.getStatus());
115 @Test(expected=NullPointerException.class)
116 public void testInvalidrespondWithError(){
117 dMaapContext.setResponse(null);
118 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");
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());
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());
141 public void testrespondWithErrorInJson(){
142 DMaaPResponseBuilder.respondWithErrorInJson(dMaapContext, 500, "InternalServerError");
144 assertEquals("application/json", response.getContentType());
145 assertEquals(500, response.getStatus());
149 public void testsendErrorAndBody(){
150 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
152 assertEquals("text/html", response.getContentType());
153 assertEquals(500, response.getStatus());
155 request.setMethod("HEAD");
157 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
159 assertEquals("text/html", response.getContentType());
160 assertEquals(500, response.getStatus());
165 public void testgetStreamForBinaryResponse() throws IOException{
166 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);
168 assertEquals("application/octet-stream", response.getContentType());
169 assertEquals(200, response.getStatus());
172 @Test(expected=NullPointerException.class)
173 public void testgetStreamForBinaryResponseError() throws IOException{
174 dMaapContext.setResponse(null);
175 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);