2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 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.client.sdn.common;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import java.util.LinkedHashMap;
26 import org.junit.Assert;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.onap.so.client.exception.BadResponseException;
31 import org.onap.so.client.exception.MapperException;
32 import org.onap.so.client.sdnc.SdnCommonTasks;
33 import org.springframework.http.HttpHeaders;
34 import org.springframework.http.MediaType;
37 public class SdnCommonTasksTest {
40 SdnCommonTasks sdnCommonTasks = new SdnCommonTasks();
43 public ExpectedException expectedException = ExpectedException.none();
46 public void buildJsonRequestTest() throws MapperException {
47 String jsonStr = sdnCommonTasks.buildJsonRequest("");
48 Assert.assertNotNull(jsonStr);
52 public void buildJsonRequestTestException() throws MapperException {
53 expectedException.expect(MapperException.class);
54 sdnCommonTasks.buildJsonRequest(new Object());
58 public void getHttpHeadersTest() {
59 HttpHeaders result = sdnCommonTasks.getHttpHeaders("auth", true);
61 assertEquals("auth", result.getFirst("Authorization"));
62 assertEquals(MediaType.APPLICATION_JSON.toString(), result.getFirst("Content-Type"));
63 assertEquals(MediaType.APPLICATION_JSON.toString(), result.getFirst("Accept"));
67 public void getHttpHeadersGetRequestTest() {
68 HttpHeaders result = sdnCommonTasks.getHttpHeaders("auth", false);
70 assertEquals("auth", result.getFirst("Authorization"));
71 assertEquals(MediaType.APPLICATION_JSON.toString(), result.getFirst("Accept"));
72 assertFalse(result.containsKey("Content-Type"));
76 public void validateSDNResponseTest() throws BadResponseException {
77 String jsonResponse = "{\"output\":{\"response-code\":\"0\",\"response-message\":\"success\"}}";
78 LinkedHashMap<String, Object> responseMap = new LinkedHashMap<>();
79 LinkedHashMap<String, Object> output = new LinkedHashMap<>();
80 output.put("response-code", "0");
81 output.put("response-message", "success");
82 responseMap.put("output", output);
83 assertEquals(jsonResponse, sdnCommonTasks.validateSDNResponse(responseMap));
87 public void validateSDNResponseTestException() throws BadResponseException {
88 expectedException.expect(BadResponseException.class);
89 LinkedHashMap responseMap = new LinkedHashMap();
90 Assert.assertNotNull(sdnCommonTasks.validateSDNResponse(responseMap));
94 public void validateSDNResponseTestRespCodeNot200() throws BadResponseException {
95 expectedException.expect(BadResponseException.class);
96 LinkedHashMap<String, Object> responseMap = new LinkedHashMap<>();
97 LinkedHashMap<String, Object> output = new LinkedHashMap<>();
98 output.put("response-code", "300");
99 output.put("response-message", "Failed");
100 responseMap.put("output", output);
101 sdnCommonTasks.validateSDNResponse(responseMap);