Merge "fix major sonar bug"
[so.git] / common / src / test / java / org / onap / so / client / graphinventory / GraphInventoryPatchConverterTest.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.graphinventory;
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 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
39 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
40
41 import com.fasterxml.jackson.core.JsonParseException;
42 import com.fasterxml.jackson.databind.JsonMappingException;
43 import com.fasterxml.jackson.databind.ObjectMapper;
44
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class GraphInventoryPatchConverterTest {
48
49         private ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
50
51         @Test
52         public void convertObjectToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
53                 GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter();
54                 GenericVnf vnf = new GenericVnf();
55                 vnf.setIpv4Loopback0Address("");
56                 String result = validator.marshallObjectToPatchFormat(vnf);
57                 GenericVnf resultObj = mapper.readValue(result.toString(), GenericVnf.class);
58                 assertTrue("expect object to become a String to prevent double marshalling", result instanceof String);
59                 assertNull("expect null because of custom mapper", resultObj.getIpv4Loopback0Address());
60                 
61         }
62         
63         @Test
64         public void convertStringToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
65                 GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter();
66                 String payload = "{\"ipv4-loopback0-address\":\"\"}";
67                 String result = validator.marshallObjectToPatchFormat(payload);
68                 
69                 assertEquals("expect no change", payload, result);
70         }
71         
72         @Test
73         public void convertStringToPatchFormatNull_Test() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
74                 GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter();
75                 String payload = "{\"ipv4-loopback0-address\": null}";
76                 String result = validator.marshallObjectToPatchFormat(payload);
77                 System.out.println(result);
78                 assertEquals("expect no change", payload, result);
79         }
80         
81         @Test
82         public void convertMapToPatchFormatTest() throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
83                 GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter();
84                 HashMap<String, String> map = new HashMap<>();
85                 map.put("ipv4-loopback0-address", "");
86                 map.put("ipv4-loopback1-address", "192.168.1.1");
87                 String result = validator.marshallObjectToPatchFormat(map);
88                 
89                 assertEquals("expect string", "{\"ipv4-loopback1-address\":\"192.168.1.1\"}", result);
90         }
91         
92         @Test
93         public void hasComplexObjectTest() {
94                 GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter();
95                 String hasNesting = "{ \"hello\" : \"world\", \"nested\" : { \"key\" : \"value\" } }";
96                 String noNesting = "{ \"hello\" : \"world\" }";
97                 String arrayCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"nestedComplex\" : [{\"key\" : \"value\"}]}";
98                 String empty = "{}";
99                 String arrayCaseSimpleOnly = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"]}";
100                 String relationshipListCaseNesting = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}], \"nested\" : { \"key\" : \"value\" }}";
101                 String relationshipListCase = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}]}";
102                 String nothing = "";
103                 
104                 assertTrue("expect has nesting", validator.hasComplexObject(hasNesting));
105                 assertFalse("expect no nesting", validator.hasComplexObject(noNesting));
106                 assertTrue("expect has nesting", validator.hasComplexObject(arrayCase));
107                 assertFalse("expect no nesting", validator.hasComplexObject(empty));
108                 assertFalse("expect no nesting", validator.hasComplexObject(arrayCaseSimpleOnly));
109                 assertFalse("expect no nesting", validator.hasComplexObject(relationshipListCase));
110                 assertTrue("expect has nesting", validator.hasComplexObject(relationshipListCaseNesting));
111                 assertFalse("expect no nesting", validator.hasComplexObject(nothing));
112         }
113         
114 }