a8816e1b042a5fad62b4b94f38ed44af4ad8493b
[so.git] / so-sdn-clients / src / test / java / org / onap / so / client / sdn / common / SdnCommonTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.client.sdn.common;
22
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;
35
36
37 public class SdnCommonTasksTest {
38
39
40     SdnCommonTasks sdnCommonTasks = new SdnCommonTasks();
41
42     @Rule
43     public ExpectedException expectedException = ExpectedException.none();
44
45     @Test
46     public void buildJsonRequestTest() throws MapperException {
47         String jsonStr = sdnCommonTasks.buildJsonRequest("");
48         Assert.assertNotNull(jsonStr);
49     }
50
51     @Test
52     public void buildJsonRequestTestException() throws MapperException {
53         expectedException.expect(MapperException.class);
54         sdnCommonTasks.buildJsonRequest(new Object());
55     }
56
57     @Test
58     public void getHttpHeadersTest() {
59         HttpHeaders result = sdnCommonTasks.getHttpHeaders("auth", true);
60
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"));
64     }
65
66     @Test
67     public void getHttpHeadersGetRequestTest() {
68         HttpHeaders result = sdnCommonTasks.getHttpHeaders("auth", false);
69
70         assertEquals("auth", result.getFirst("Authorization"));
71         assertEquals(MediaType.APPLICATION_JSON.toString(), result.getFirst("Accept"));
72         assertFalse(result.containsKey("Content-Type"));
73     }
74
75     @Test
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));
84     }
85
86     @Test
87     public void validateSDNResponseTestException() throws BadResponseException {
88         expectedException.expect(BadResponseException.class);
89         LinkedHashMap responseMap = new LinkedHashMap();
90         Assert.assertNotNull(sdnCommonTasks.validateSDNResponse(responseMap));
91     }
92
93     @Test
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);
102     }
103
104 }