add junit coverage
[so.git] / so-sdn-clients / src / test / java / org / onap / so / client / sdnc / SdnCommonTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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=========================================================
21  */
22
23 package org.onap.so.client.sdnc;
24
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;
37
38
39 public class SdnCommonTasksTest {
40
41
42     SdnCommonTasks sdnCommonTasks = new SdnCommonTasks();
43
44     @Rule
45     public ExpectedException expectedException = ExpectedException.none();
46
47     @Test
48     public void buildJsonRequestTest() throws MapperException {
49         String jsonStr = sdnCommonTasks.buildJsonRequest("");
50         Assert.assertNotNull(jsonStr);
51     }
52
53     @Test
54     public void buildJsonRequestTestException() throws MapperException {
55         expectedException.expect(MapperException.class);
56         sdnCommonTasks.buildJsonRequest(new Object());
57     }
58
59     @Test
60     public void getHttpHeadersTest() {
61         HttpHeaders result = sdnCommonTasks.getHttpHeaders("auth", true);
62
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"));
66     }
67
68     @Test
69     public void getHttpHeadersGetRequestTest() {
70         HttpHeaders result = sdnCommonTasks.getHttpHeaders("auth", false);
71
72         assertEquals("auth", result.getFirst("Authorization"));
73         assertEquals(MediaType.APPLICATION_JSON.toString(), result.getFirst("Accept"));
74         assertFalse(result.containsKey("Content-Type"));
75     }
76
77     @Test
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));
86     }
87
88     @Test
89     public void validateSDNResponseTestException() throws BadResponseException {
90         expectedException.expect(BadResponseException.class);
91         LinkedHashMap responseMap = new LinkedHashMap();
92         Assert.assertNotNull(sdnCommonTasks.validateSDNResponse(responseMap));
93     }
94
95     @Test
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);
104     }
105
106     @Test
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();
112     }
113
114     @Test
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\"}");
120     }
121
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);
127     }
128
129     @Test(expected = BadResponseException.class)
130     public void validateSDNGetResponse_exWhenMapEmpty() throws Exception {
131         sdnCommonTasks.validateSDNGetResponse(new LinkedHashMap<>());
132     }
133 }