Containerization feature of SO
[so.git] / common / src / test / java / org / onap / so / client / aai / AAIRestClientTest.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.aai;
22
23 import static org.hamcrest.CoreMatchers.containsString;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.spy;
31
32 import java.io.IOException;
33 import java.net.URI;
34 import java.net.URISyntaxException;
35 import java.util.HashMap;
36
37 import javax.ws.rs.core.Response;
38
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.rules.ExpectedException;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.runners.MockitoJUnitRunner;
45 import org.onap.aai.domain.yang.GenericVnf;
46 import org.onap.so.client.graphinventory.exceptions.GraphInventoryPatchDepthExceededException;
47
48 import com.fasterxml.jackson.core.JsonParseException;
49 import com.fasterxml.jackson.databind.JsonMappingException;
50 import com.fasterxml.jackson.databind.ObjectMapper;
51
52 @RunWith(MockitoJUnitRunner.class)
53 public class AAIRestClientTest {
54
55         @Mock
56         private AAIProperties props;
57         
58         private ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
59         
60         @Rule
61         public ExpectedException thrown = ExpectedException.none();
62         
63         @Test
64         public void convertObjectToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
65                 AAIRestClient client = new AAIRestClient(props, new URI(""));
66                 GenericVnf vnf = new GenericVnf();
67                 vnf.setIpv4Loopback0Address("");
68                 String result = client.convertObjectToPatchFormat(vnf);
69                 GenericVnf resultObj = mapper.readValue(result.toString(), GenericVnf.class);
70                 assertTrue("expect object to become a String to prevent double marshalling", result instanceof String);
71                 assertNull("expect null because of custom mapper", resultObj.getIpv4Loopback0Address());
72                 
73         }
74         
75         @Test
76         public void convertStringToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
77                 AAIRestClient client = new AAIRestClient(props, new URI(""));
78                 String payload = "{\"ipv4-loopback0-address\":\"\"}";
79                 String result = client.convertObjectToPatchFormat(payload);
80                 
81                 assertEquals("expect no change", payload, result);
82         }
83         
84         @Test
85         public void convertMapToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
86                 AAIRestClient client = new AAIRestClient(props, new URI(""));
87                 HashMap<String, String> map = new HashMap<>();
88                 map.put("ipv4-loopback0-address", "");
89                 String result = client.convertObjectToPatchFormat(map);
90                 
91                 assertEquals("expect string", "{\"ipv4-loopback0-address\":\"\"}", result);
92         }
93         
94         @Test
95         public void failPatchOnComplexObject() throws URISyntaxException {
96                 AAIRestClient client = new AAIRestClient(props, new URI(""));
97                 this.thrown.expect(GraphInventoryPatchDepthExceededException.class); 
98                 this.thrown.expectMessage(containsString("Object exceeds allowed depth for update action"));
99                 client.patch("{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}], \"nested\" : { \"key\" : \"value\" }}");
100
101         }
102         
103         @Test
104         public void hasComplexObjectTest() throws URISyntaxException {
105                 AAIRestClient client = new AAIRestClient(props, new URI(""));
106                 String hasNesting = "{ \"hello\" : \"world\", \"nested\" : { \"key\" : \"value\" } }";
107                 String noNesting = "{ \"hello\" : \"world\" }";
108                 String arrayCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"nestedComplex\" : [{\"key\" : \"value\"}]}";
109                 String empty = "{}";
110                 String arrayCaseSimpleOnly = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"]}";
111                 String relationshipListCaseNesting = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}], \"nested\" : { \"key\" : \"value\" }}";
112                 String relationshipListCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}]}";
113                 String nothing = "";
114                 
115                 assertTrue("expect has nesting", client.hasComplexObject(hasNesting));
116                 assertFalse("expect no nesting", client.hasComplexObject(noNesting));
117                 assertTrue("expect has nesting", client.hasComplexObject(arrayCase));
118                 assertFalse("expect no nesting", client.hasComplexObject(empty));
119                 assertFalse("expect no nesting", client.hasComplexObject(arrayCaseSimpleOnly));
120                 assertFalse("expect no nesting", client.hasComplexObject(relationshipListCase));
121                 assertTrue("expect has nesting", client.hasComplexObject(relationshipListCaseNesting));
122                 assertFalse("expect no nesting", client.hasComplexObject(nothing));
123         }
124 }