dc4775f1f53627638a13c36d9f8062f0732b06ba
[sdnc/apps.git] /
1 /*\r
2  * ============LICENSE_START===================================================\r
3  * Copyright (c) 2018 Amdocs\r
4  * ============================================================================\r
5  * Licensed under the Apache License, Version 2.0 (the "License");\r
6  * you may not use this file except in compliance with the License.\r
7  * You may obtain a copy of the License at\r
8  *\r
9  *        http://www.apache.org/licenses/LICENSE-2.0\r
10  *\r
11  * Unless required by applicable law or agreed to in writing, software\r
12  * distributed under the License is distributed on an "AS IS" BASIS,\r
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
14  * See the License for the specific language governing permissions and\r
15  * limitations under the License.\r
16  * ============LICENSE_END=====================================================\r
17  */\r
18 \r
19 package org.onap.sdnc.apps.pomba.networkdiscovery.unittest.service.util;\r
20 \r
21 import static org.junit.Assert.assertThat;\r
22 \r
23 import com.bazaarvoice.jolt.JsonUtils;\r
24 import com.bazaarvoice.jolt.exception.JsonUnmarshalException;\r
25 \r
26 import java.util.HashMap;\r
27 import java.util.List;\r
28 import java.util.Map;\r
29 \r
30 import org.hamcrest.CoreMatchers;\r
31 import org.junit.Assert;\r
32 import org.junit.Rule;\r
33 import org.junit.Test;\r
34 import org.junit.rules.ExpectedException;\r
35 import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute;\r
36 import org.onap.sdnc.apps.pomba.networkdiscovery.service.util.TransformationUtil;\r
37 \r
38 public class TransformationUtilTest {\r
39 \r
40     private static final String TEST_RESOURCES = "src/test/resources/jolt/";\r
41 \r
42     @Rule\r
43     public ExpectedException expectedEx = ExpectedException.none();\r
44 \r
45     @Test\r
46     public void testTransformVServer() {\r
47 \r
48         Object sourceObject = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-input.json");\r
49 \r
50         String resultJson = TransformationUtil.transform(JsonUtils.toJsonString(sourceObject), "vserver");\r
51 \r
52         Object expectedObject = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-expected.json");\r
53 \r
54         Assert.assertEquals("Json transformation result does not match expected content",\r
55                 JsonUtils.toPrettyJsonString(expectedObject),\r
56                 JsonUtils.toPrettyJsonString(JsonUtils.jsonToObject(resultJson)));\r
57 \r
58     }\r
59 \r
60     @Test\r
61     public void testTransformL3Network() {\r
62 \r
63         Object sourceObject = JsonUtils.filepathToObject(TEST_RESOURCES + "l3network-input.json");\r
64         String resultJson = TransformationUtil.transform(JsonUtils.toJsonString(sourceObject), "l3-network");\r
65 \r
66         Object expectedObject = JsonUtils.filepathToObject(TEST_RESOURCES + "l3network-expected.json");\r
67 \r
68         Assert.assertEquals("Json transformation result does not match expected content",\r
69                 JsonUtils.toPrettyJsonString(expectedObject),\r
70                 JsonUtils.toPrettyJsonString(JsonUtils.jsonToObject(resultJson)));\r
71 \r
72     }\r
73 \r
74     @Test\r
75     public void testTransformFailureFileNotFound() {\r
76 \r
77         expectedEx.expect(RuntimeException.class);\r
78         expectedEx.expectMessage("Unable to load JSON file");\r
79 \r
80         TransformationUtil.transform("{}", "foobar");\r
81     }\r
82 \r
83     @Test\r
84     public void testTransformFailureInvalidInputJson() {\r
85 \r
86         expectedEx.expect(JsonUnmarshalException.class);\r
87         expectedEx.expectMessage("Unable to unmarshal JSON");\r
88 \r
89         TransformationUtil.transform("xxx", "foobar");\r
90     }\r
91 \r
92     @Test\r
93     public void testToAttributeList() {\r
94         Map<String, String> expectedAttributes = new HashMap<String, String>();\r
95         expectedAttributes.put("name", "norm_bouygues");\r
96         expectedAttributes.put("hostId", "ea1660efbbedda164379afacdc622305c4b88cebfb84119472d286a8");\r
97         expectedAttributes.put("hostStatus", "UP");\r
98         expectedAttributes.put("id", "2c311eae-f542-4173-8a01-582922abd495");\r
99         expectedAttributes.put("status", "ACTIVE");\r
100         expectedAttributes.put("vmState", "active");\r
101         expectedAttributes.put("hostname", "norm-bouygues");\r
102         expectedAttributes.put("inMaintenance", "true");\r
103         expectedAttributes.put("imageId", "c0022890-d91f-422c-91c5-3866edeae768");\r
104         expectedAttributes.put("tenantId", "15ad36d394e744838e947ca90609f805");\r
105         expectedAttributes.put("host", "Setup-NCSO-OTT-E-C2");\r
106 \r
107         Object inputJson = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-expected.json");\r
108         List<Attribute> resultAttributeList = TransformationUtil.toAttributeList(JsonUtils.toJsonString(inputJson));\r
109 \r
110         Map<String, String> resultAttributes = new HashMap<>();\r
111 \r
112         for (Attribute attribute : resultAttributeList) {\r
113             resultAttributes.put(attribute.getName(), attribute.getValue());\r
114         }\r
115         assertThat(expectedAttributes, CoreMatchers.is(resultAttributes));\r
116     }\r
117 \r
118     @Test\r
119     public void testToAttributeListNullJsonValue() {\r
120         Map<String, String> expectedAttributes = new HashMap<String, String>();\r
121         expectedAttributes.put("name", "");\r
122 \r
123         String inputJson = "{\"server\": { \"name\": null }}";\r
124 \r
125         List<Attribute> resultAttributeList = TransformationUtil.toAttributeList(inputJson);\r
126 \r
127         Map<String, String> resultAttributes = new HashMap<>();\r
128 \r
129         for (Attribute attribute : resultAttributeList) {\r
130             resultAttributes.put(attribute.getName(), attribute.getValue());\r
131         }\r
132         assertThat(expectedAttributes, CoreMatchers.is(resultAttributes));\r
133     }\r
134 \r
135 }\r