2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2021 - Nokia
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.so.client.sdnc;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import java.util.LinkedHashMap;
29 import org.junit.Assert;
30 import org.junit.Rule;
31 import org.junit.Test;
32 import org.junit.rules.ExpectedException;
33 import org.onap.so.client.exception.BadResponseException;
34 import org.onap.so.client.exception.MapperException;
35 import org.springframework.http.HttpHeaders;
36 import org.springframework.http.MediaType;
39 public class SdnCommonTasksTest {
42 SdnCommonTasks sdnCommonTasks = new SdnCommonTasks();
45 public ExpectedException expectedException = ExpectedException.none();
48 public void buildJsonRequestTest() throws MapperException {
49 String jsonStr = sdnCommonTasks.buildJsonRequest("");
50 Assert.assertNotNull(jsonStr);
54 public void buildJsonRequestTestException() throws MapperException {
55 expectedException.expect(MapperException.class);
56 sdnCommonTasks.buildJsonRequest(new Object());
60 public void getHttpHeadersTest() {
61 HttpHeaders result = sdnCommonTasks.getHttpHeaders("auth", true);
63 assertEquals("auth", result.getFirst("Authorization"));
64 assertEquals(MediaType.APPLICATION_JSON.toString(), result.getFirst("Content-Type"));
65 assertEquals(MediaType.APPLICATION_JSON.toString(), result.getFirst("Accept"));
69 public void getHttpHeadersGetRequestTest() {
70 HttpHeaders result = sdnCommonTasks.getHttpHeaders("auth", false);
72 assertEquals("auth", result.getFirst("Authorization"));
73 assertEquals(MediaType.APPLICATION_JSON.toString(), result.getFirst("Accept"));
74 assertFalse(result.containsKey("Content-Type"));
78 public void validateSDNResponseTest() throws BadResponseException {
79 String jsonResponse = "{\"output\":{\"response-code\":\"0\",\"response-message\":\"success\"}}";
80 LinkedHashMap<String, Object> responseMap = new LinkedHashMap<>();
81 LinkedHashMap<String, Object> output = new LinkedHashMap<>();
82 output.put("response-code", "0");
83 output.put("response-message", "success");
84 responseMap.put("output", output);
85 assertEquals(jsonResponse, sdnCommonTasks.validateSDNResponse(responseMap));
89 public void validateSDNResponseTestException() throws BadResponseException {
90 expectedException.expect(BadResponseException.class);
91 LinkedHashMap responseMap = new LinkedHashMap();
92 Assert.assertNotNull(sdnCommonTasks.validateSDNResponse(responseMap));
96 public void validateSDNResponseTestRespCodeNot200() throws BadResponseException {
97 expectedException.expect(BadResponseException.class);
98 LinkedHashMap<String, Object> responseMap = new LinkedHashMap<>();
99 LinkedHashMap<String, Object> output = new LinkedHashMap<>();
100 output.put("response-code", "300");
101 output.put("response-message", "Failed");
102 responseMap.put("output", output);
103 sdnCommonTasks.validateSDNResponse(responseMap);
107 public void validateSDNResponse_emptyWhenNoSerializerFoundForValue() throws Exception {
108 LinkedHashMap<String, Object> output = new LinkedHashMap<>();
109 output.put("key1", new Object());
110 String result = sdnCommonTasks.validateSDNResponse(output);
111 assertThat(result).isEmpty();
115 public void validateSDNGetResponse_success() throws Exception {
116 LinkedHashMap<String, Object> output = new LinkedHashMap<>();
117 output.put("key1", "value1");
118 String result = sdnCommonTasks.validateSDNGetResponse(output);
119 assertThat(result).isEqualTo("{\"key1\":\"value1\"}");
122 @Test(expected = BadResponseException.class)
123 public void validateSDNGetResponse_exWhenNoSerializerFoundForValue() throws Exception {
124 LinkedHashMap<String, Object> output = new LinkedHashMap<>();
125 output.put("key1", new Object());
126 sdnCommonTasks.validateSDNGetResponse(output);
129 @Test(expected = BadResponseException.class)
130 public void validateSDNGetResponse_exWhenMapEmpty() throws Exception {
131 sdnCommonTasks.validateSDNGetResponse(new LinkedHashMap<>());