Replaced all tabs with spaces in java and pom.xml
[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 import java.io.IOException;
28 import java.net.URI;
29 import java.net.URISyntaxException;
30 import java.util.HashMap;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.aai.domain.yang.GenericVnf;
36 import org.onap.so.client.aai.AAICommonObjectMapperProvider;
37 import org.onap.so.client.graphinventory.GraphInventoryPatchConverter;
38 import com.fasterxml.jackson.core.JsonParseException;
39 import com.fasterxml.jackson.databind.JsonMappingException;
40 import com.fasterxml.jackson.databind.ObjectMapper;
41
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class GraphInventoryPatchConverterTest {
45
46     private ObjectMapper mapper = new AAICommonObjectMapperProvider().getMapper();
47
48     @Test
49     public void convertObjectToPatchFormatTest()
50             throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
51         GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter();
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()
63             throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
64         GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter();
65         String payload = "{\"ipv4-loopback0-address\":\"\"}";
66         String result = validator.marshallObjectToPatchFormat(payload);
67
68         assertEquals("expect no change", payload, result);
69     }
70
71     @Test
72     public void convertStringToPatchFormatNull_Test()
73             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()
83             throws URISyntaxException, JsonParseException, JsonMappingException, IOException {
84         GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter();
85         HashMap<String, String> map = new HashMap<>();
86         map.put("ipv4-loopback0-address", "");
87         map.put("ipv4-loopback1-address", "192.168.1.1");
88         String result = validator.marshallObjectToPatchFormat(map);
89
90         assertEquals("expect string", "{\"ipv4-loopback1-address\":\"192.168.1.1\"}", result);
91     }
92
93     @Test
94     public void hasComplexObjectTest() {
95         GraphInventoryPatchConverter validator = new GraphInventoryPatchConverter();
96         String hasNesting = "{ \"hello\" : \"world\", \"nested\" : { \"key\" : \"value\" } }";
97         String noNesting = "{ \"hello\" : \"world\" }";
98         String arrayCase =
99                 "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"nestedComplex\" : [{\"key\" : \"value\"}]}";
100         String empty = "{}";
101         String arrayCaseSimpleOnly = "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"]}";
102         String relationshipListCaseNesting =
103                 "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}], \"nested\" : { \"key\" : \"value\" }}";
104         String relationshipListCase =
105                 "{ \"hello\" : \"world\", \"nestedSimple\" : [\"value1\" , \"value2\"], \"relationship-list\" : [{\"key\" : \"value\"}]}";
106         String nothing = "";
107
108         assertTrue("expect has nesting", validator.hasComplexObject(hasNesting));
109         assertFalse("expect no nesting", validator.hasComplexObject(noNesting));
110         assertTrue("expect has nesting", validator.hasComplexObject(arrayCase));
111         assertFalse("expect no nesting", validator.hasComplexObject(empty));
112         assertFalse("expect no nesting", validator.hasComplexObject(arrayCaseSimpleOnly));
113         assertFalse("expect no nesting", validator.hasComplexObject(relationshipListCase));
114         assertTrue("expect has nesting", validator.hasComplexObject(relationshipListCaseNesting));
115         assertFalse("expect no nesting", validator.hasComplexObject(nothing));
116     }
117
118 }