2 * ============LICENSE_START=======================================================
\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
11 * http://www.apache.org/licenses/LICENSE-2.0
\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
21 package com.att.nsa.cambria.utils;
\r
23 import static org.junit.Assert.*;
\r
25 import java.io.IOException;
\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
35 import com.att.nsa.cambria.beans.DMaaPContext;
\r
37 public class DMaaPResponseBuilderTest {
\r
39 DMaaPContext dMaapContext;
\r
40 MockHttpServletRequest request;
\r
41 MockHttpServletResponse response;
\r
44 public void setUp() throws Exception {
\r
46 dMaapContext = new DMaaPContext();
\r
47 request = new MockHttpServletRequest();
\r
48 response = new MockHttpServletResponse();
\r
49 dMaapContext.setRequest(request);
\r
50 dMaapContext.setResponse(response);
\r
54 public void tearDown() throws Exception {
\r
58 public void testsetNoCacheHeadings(){
\r
59 DMaaPResponseBuilder.setNoCacheHeadings(dMaapContext);
\r
60 assertEquals("no-cache", response.getHeader("Pragma"));
\r
64 public void testrespondOk() throws JSONException, IOException{
\r
65 JSONObject jsonObject = new JSONObject();
\r
66 jsonObject.put("Name", "Test");
\r
68 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);
\r
69 assertEquals("application/json", response.getContentType());
\r
70 assertEquals(200, response.getStatus());
\r
72 request.setMethod("HEAD");
\r
74 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);
\r
75 assertEquals("application/json", response.getContentType());
\r
76 assertEquals(200, response.getStatus());
\r
80 public void testrespondOkNoContent(){
\r
81 DMaaPResponseBuilder.respondOkNoContent(dMaapContext);
\r
82 assertEquals(204, response.getStatus());
\r
86 public void testrespondOkWithHtml(){
\r
87 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
\r
89 assertEquals("text/html", response.getContentType());
\r
90 assertEquals(200, response.getStatus());
\r
94 public void testrespondWithError(){
\r
95 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");
\r
96 assertEquals(500, response.getStatus());
\r
100 public void testrespondWithJsonError(){
\r
101 JSONObject o = new JSONObject();
\r
102 o.put("status", 500);
\r
103 o.put("message", "InternalServerError");
\r
104 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, o);
\r
105 assertEquals(500, response.getStatus());
\r
109 public void testrespondWithErrorInJson(){
\r
110 DMaaPResponseBuilder.respondWithErrorInJson(dMaapContext, 500, "InternalServerError");
\r
112 assertEquals("application/json", response.getContentType());
\r
113 assertEquals(500, response.getStatus());
\r
117 public void testsendErrorAndBody(){
\r
118 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
\r
120 assertEquals("text/html", response.getContentType());
\r
121 assertEquals(500, response.getStatus());
\r
123 request.setMethod("HEAD");
\r
125 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
\r
127 assertEquals("text/html", response.getContentType());
\r
128 assertEquals(500, response.getStatus());
\r
133 public void testgetStreamForBinaryResponse() throws IOException{
\r
134 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);
\r
136 assertEquals("application/octet-stream", response.getContentType());
\r
137 assertEquals(200, response.getStatus());
\r