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 testrespondOkWithHtml(){
88 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
90 assertEquals("text/html", response.getContentType());
91 assertEquals(200, response.getStatus());
95 public void testrespondWithError(){
96 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");
97 assertEquals(500, response.getStatus());
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());
110 public void testrespondWithErrorInJson(){
111 DMaaPResponseBuilder.respondWithErrorInJson(dMaapContext, 500, "InternalServerError");
113 assertEquals("application/json", response.getContentType());
114 assertEquals(500, response.getStatus());
118 public void testsendErrorAndBody(){
119 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
121 assertEquals("text/html", response.getContentType());
122 assertEquals(500, response.getStatus());
124 request.setMethod("HEAD");
126 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
128 assertEquals("text/html", response.getContentType());
129 assertEquals(500, response.getStatus());
134 public void testgetStreamForBinaryResponse() throws IOException{
135 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);
137 assertEquals("application/octet-stream", response.getContentType());
138 assertEquals(200, response.getStatus());