2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 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.so.rest;
23 import static org.junit.Assert.assertEquals;
25 import org.json.JSONObject;
26 import org.junit.Test;
28 import org.apache.http.HttpResponse;
29 import org.apache.http.ProtocolVersion;
30 import org.apache.http.HttpEntity;
31 import org.apache.http.entity.ContentType;
32 import org.apache.http.entity.StringEntity;
33 import org.apache.http.message.BasicHttpResponse;
35 public class APIResponseTest {
38 public void test() throws Exception {
39 JSONObject jsonObject = new JSONObject();
40 jsonObject.put("firstName", "firstName1");
41 jsonObject.put("lastName", "lastName1");
42 String jsonObjectAsString= jsonObject.toString();
43 HttpEntity entity = new StringEntity(jsonObjectAsString, ContentType.APPLICATION_JSON);
44 ProtocolVersion ver = new ProtocolVersion("HTTP", 1, 1);
45 HttpResponse response = new BasicHttpResponse(ver, 1, "Ok");
46 response.setStatusLine(ver, 200);
47 response.setEntity(entity);
48 response.setHeader("name", "value");
49 response.setStatusCode(200);
50 APIResponse apiResponse = new APIResponse(response);
51 assertEquals(200, apiResponse.getStatusCode());
52 assertEquals(jsonObject.toString(), apiResponse.getResponseBodyAsString());
53 assertEquals("value", apiResponse.getFirstHeader("name"));
54 assertEquals(1, apiResponse.getAllHeaders().length);
55 assertEquals(49, apiResponse.getResponseBodyAsByteArray().length);