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