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
9 * http://www.apache.org/licenses/LICENSE-2.0
\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
19 package org.onap.sdnc.apps.pomba.networkdiscovery.unittest.service.util;
\r
21 import static org.junit.Assert.assertThat;
\r
23 import com.bazaarvoice.jolt.JsonUtils;
\r
24 import com.bazaarvoice.jolt.exception.JsonUnmarshalException;
\r
26 import java.util.HashMap;
\r
27 import java.util.List;
\r
28 import java.util.Map;
\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
38 public class TransformationUtilTest {
\r
40 private static final String TEST_RESOURCES = "src/test/resources/jolt/";
\r
43 public ExpectedException expectedEx = ExpectedException.none();
\r
46 public void testTransformVServer() {
\r
48 Object sourceObject = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-input.json");
\r
50 String resultJson = TransformationUtil.transform(JsonUtils.toJsonString(sourceObject), "vserver");
\r
52 Object expectedObject = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-expected.json");
\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
61 public void testTransformL3Network() {
\r
63 Object sourceObject = JsonUtils.filepathToObject(TEST_RESOURCES + "l3network-input.json");
\r
64 String resultJson = TransformationUtil.transform(JsonUtils.toJsonString(sourceObject), "l3-network");
\r
66 Object expectedObject = JsonUtils.filepathToObject(TEST_RESOURCES + "l3network-expected.json");
\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
75 public void testTransformFailureFileNotFound() {
\r
77 expectedEx.expect(RuntimeException.class);
\r
78 expectedEx.expectMessage("Unable to load JSON file");
\r
80 TransformationUtil.transform("{}", "foobar");
\r
84 public void testTransformFailureInvalidInputJson() {
\r
86 expectedEx.expect(JsonUnmarshalException.class);
\r
87 expectedEx.expectMessage("Unable to unmarshal JSON");
\r
89 TransformationUtil.transform("xxx", "foobar");
\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
107 Object inputJson = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-expected.json");
\r
108 List<Attribute> resultAttributeList = TransformationUtil.toAttributeList(JsonUtils.toJsonString(inputJson));
\r
110 Map<String, String> resultAttributes = new HashMap<>();
\r
112 for (Attribute attribute : resultAttributeList) {
\r
113 resultAttributes.put(attribute.getName(), attribute.getValue());
\r
115 assertThat(expectedAttributes, CoreMatchers.is(resultAttributes));
\r
119 public void testToAttributeListNullJsonValue() {
\r
120 Map<String, String> expectedAttributes = new HashMap<String, String>();
\r
121 expectedAttributes.put("name", "");
\r
123 String inputJson = "{\"server\": { \"name\": null }}";
\r
125 List<Attribute> resultAttributeList = TransformationUtil.toAttributeList(inputJson);
\r
127 Map<String, String> resultAttributes = new HashMap<>();
\r
129 for (Attribute attribute : resultAttributeList) {
\r
130 resultAttributes.put(attribute.getName(), attribute.getValue());
\r
132 assertThat(expectedAttributes, CoreMatchers.is(resultAttributes));
\r