Springboot 2.0 upgrade
[so.git] / common / src / test / java / org / onap / so / client / aai / AAIPatchConverterTest.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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.IOException;
29 import java.net.URI;
30 import java.net.URISyntaxException;
31 import java.util.HashMap;
32
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.aai.domain.yang.GenericVnf;
38
39 import com.fasterxml.jackson.core.JsonParseException;
40 import com.fasterxml.jackson.databind.JsonMappingException;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42
43
44 @RunWith(MockitoJUnitRunner.class)
45 public class AAIPatchConverterTest {
46
47         private ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
48
49         @Test
50         public void convertObjectToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
51                 AAIPatchConverter validator = new AAIPatchConverter();
52                 GenericVnf vnf = new GenericVnf();
53                 vnf.setIpv4Loopback0Address("");
54                 String result = validator.marshallObjectToPatchFormat(vnf);
55                 GenericVnf resultObj = mapper.readValue(result.toString(), GenericVnf.class);
56                 assertTrue("expect object to become a String to prevent double marshalling", result instanceof String);
57                 assertNull("expect null because of custom mapper", resultObj.getIpv4Loopback0Address());
58                 
59         }
60         
61         @Test
62         public void convertStringToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
63                 AAIPatchConverter validator = new AAIPatchConverter();
64                 String payload = "{\"ipv4-loopback0-address\":\"\"}";
65                 String result = validator.marshallObjectToPatchFormat(payload);
66                 
67                 assertEquals("expect no change", payload, result);
68         }
69         
70         @Test
71         public void convertMapToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
72                 AAIPatchConverter validator = new AAIPatchConverter();
73                 HashMap<String, String> map = new HashMap<>();
74                 map.put("ipv4-loopback0-address", "");
75                 map.put("ipv4-loopback1-address", "192.168.1.1");
76                 String result = validator.marshallObjectToPatchFormat(map);
77                 
78                 assertEquals("expect string", "{\"ipv4-loopback1-address\":\"192.168.1.1\"}", result);
79         }
80         
81         @Test
82         public void hasComplexObjectTest() {
83                 AAIPatchConverter validator = new AAIPatchConverter();
84                 String hasNesting = "{ \"hello\" : \"world\", \"nested\" : { \"key\" : \"value\" } }";
85                 String noNesting = "{ \"hello\" : \"world\" }";
86                 String arrayCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"nestedComplex\" : [{\"key\" : \"value\"}]}";
87                 String empty = "{}";
88                 String arrayCaseSimpleOnly = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"]}";
89                 String relationshipListCaseNesting = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}], \"nested\" : { \"key\" : \"value\" }}";
90                 String relationshipListCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}]}";
91                 String nothing = "";
92                 
93                 assertTrue("expect has nesting", validator.hasComplexObject(hasNesting));
94                 assertFalse("expect no nesting", validator.hasComplexObject(noNesting));
95                 assertTrue("expect has nesting", validator.hasComplexObject(arrayCase));
96                 assertFalse("expect no nesting", validator.hasComplexObject(empty));
97                 assertFalse("expect no nesting", validator.hasComplexObject(arrayCaseSimpleOnly));
98                 assertFalse("expect no nesting", validator.hasComplexObject(relationshipListCase));
99                 assertTrue("expect has nesting", validator.hasComplexObject(relationshipListCaseNesting));
100                 assertFalse("expect no nesting", validator.hasComplexObject(nothing));
101         }
102         
103 }