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.dmf.mr.beans.DMaaPContext;
\r
36 import com.att.dmf.mr.utils.DMaaPResponseBuilder;
\r
38 public class DMaaPResponseBuilderTest {
\r
40 DMaaPContext dMaapContext;
\r
41 MockHttpServletRequest request;
\r
42 MockHttpServletResponse response;
\r
45 public void setUp() throws Exception {
\r
47 dMaapContext = new DMaaPContext();
\r
48 request = new MockHttpServletRequest();
\r
49 response = new MockHttpServletResponse();
\r
50 dMaapContext.setRequest(request);
\r
51 dMaapContext.setResponse(response);
\r
55 public void tearDown() throws Exception {
\r
59 public void testsetNoCacheHeadings(){
\r
60 DMaaPResponseBuilder.setNoCacheHeadings(dMaapContext);
\r
61 assertEquals("no-cache", response.getHeader("Pragma"));
\r
65 public void testrespondOk() throws JSONException, IOException{
\r
66 JSONObject jsonObject = new JSONObject();
\r
67 jsonObject.put("Name", "Test");
\r
69 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);
\r
70 assertEquals("application/json", response.getContentType());
\r
71 assertEquals(200, response.getStatus());
\r
73 request.setMethod("HEAD");
\r
75 DMaaPResponseBuilder.respondOk(dMaapContext, jsonObject);
\r
76 assertEquals("application/json", response.getContentType());
\r
77 assertEquals(200, response.getStatus());
\r
81 public void testrespondOkNoContent(){
\r
82 DMaaPResponseBuilder.respondOkNoContent(dMaapContext);
\r
83 assertEquals(204, response.getStatus());
\r
87 public void testrespondOkWithHtml(){
\r
88 DMaaPResponseBuilder.respondOkWithHtml(dMaapContext, "<head></head>");
\r
90 assertEquals("text/html", response.getContentType());
\r
91 assertEquals(200, response.getStatus());
\r
95 public void testrespondWithError(){
\r
96 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, "InternalServerError");
\r
97 assertEquals(500, response.getStatus());
\r
101 public void testrespondWithJsonError(){
\r
102 JSONObject o = new JSONObject();
\r
103 o.put("status", 500);
\r
104 o.put("message", "InternalServerError");
\r
105 DMaaPResponseBuilder.respondWithError(dMaapContext, 500, o);
\r
106 assertEquals(500, response.getStatus());
\r
110 public void testrespondWithErrorInJson(){
\r
111 DMaaPResponseBuilder.respondWithErrorInJson(dMaapContext, 500, "InternalServerError");
\r
113 assertEquals("application/json", response.getContentType());
\r
114 assertEquals(500, response.getStatus());
\r
118 public void testsendErrorAndBody(){
\r
119 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
\r
121 assertEquals("text/html", response.getContentType());
\r
122 assertEquals(500, response.getStatus());
\r
124 request.setMethod("HEAD");
\r
126 DMaaPResponseBuilder.sendErrorAndBody(dMaapContext, 500, "InternalServerError", "text/html");
\r
128 assertEquals("text/html", response.getContentType());
\r
129 assertEquals(500, response.getStatus());
\r
134 public void testgetStreamForBinaryResponse() throws IOException{
\r
135 DMaaPResponseBuilder.getStreamForBinaryResponse(dMaapContext);
\r
137 assertEquals("application/octet-stream", response.getContentType());
\r
138 assertEquals(200, response.getStatus());
\r