From 38f720752af4d4aad8c4e467a288d9048659f688 Mon Sep 17 00:00:00 2001 From: Rob Daugherty Date: Wed, 14 Mar 2018 02:07:32 -0400 Subject: AT&T 1712 and 1802 release code This is code from AT&T's 1712 and 1802 releases. Change-Id: Ie1e85851e94bc66c4d9514a0226c221939531a04 Issue-ID: SO-425 Signed-off-by: Rob Daugherty --- .../org/openecomp/mso/bpmn/core/JsonUtilsTest.java | 140 +++++++++++++-------- .../mso/bpmn/core/json/JsonUtilsTest.java | 65 ++++++++++ 2 files changed, 150 insertions(+), 55 deletions(-) create mode 100644 bpmn/MSOCoreBPMN/src/test/java/org/openecomp/mso/bpmn/core/json/JsonUtilsTest.java (limited to 'bpmn/MSOCoreBPMN/src/test/java/org') diff --git a/bpmn/MSOCoreBPMN/src/test/java/org/openecomp/mso/bpmn/core/JsonUtilsTest.java b/bpmn/MSOCoreBPMN/src/test/java/org/openecomp/mso/bpmn/core/JsonUtilsTest.java index 58f1ae264d..9643db7834 100644 --- a/bpmn/MSOCoreBPMN/src/test/java/org/openecomp/mso/bpmn/core/JsonUtilsTest.java +++ b/bpmn/MSOCoreBPMN/src/test/java/org/openecomp/mso/bpmn/core/JsonUtilsTest.java @@ -36,18 +36,24 @@ * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= - */ + */ package org.openecomp.mso.bpmn.core; -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + +import static org.assertj.core.api.Assertions.assertThat; import java.io.File; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; + +import org.json.JSONObject; +import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import org.openecomp.mso.apihandler.common.ValidationException; import org.openecomp.mso.bpmn.core.json.JsonUtils; import org.openecomp.mso.bpmn.core.xml.XmlTool; import org.xmlunit.builder.DiffBuilder; @@ -62,7 +68,7 @@ public class JsonUtilsTest { private static final String EOL = "\n"; private static final String XML_REQ = - "" + EOL + + "" + EOL + " " + EOL + " DEV-VF-0021" + EOL + " CREATE_VF_MODULE" + EOL + @@ -91,9 +97,9 @@ public class JsonUtilsTest { " server1111" + EOL + " " + EOL + "" + EOL; - + private static final String XML_REQ_NO_ATTRS = - "" + EOL + + "" + EOL + " " + EOL + " DELETE_VF_MODULE" + EOL + " PORTAL" + EOL + @@ -114,49 +120,49 @@ public class JsonUtilsTest { "" + EOL; private static final String XML_ARRAY_REQ = - "" + EOL + + "" + EOL + " BROADBAND" + EOL + - " IVLAN" + EOL + - " SHELLUCPE31" + EOL + - " " + EOL + - " " + EOL + - " AVPN" + EOL + - " 1000BASE-T" + EOL + - " ATT" + EOL + - " BT/SLIR/70911" + EOL + - " Active" + EOL + - " WAN1" + EOL + - " ATT" + EOL + - " 100" + EOL + - " ELECTRICAL" + EOL + - " " + EOL + - " " + EOL + - " AVPN" + EOL + - " 10/100/1000BASE-T" + EOL + - " ATT" + EOL + - " AS/KRFN/34611" + EOL + - " Active" + EOL + - " WAN2" + EOL + - " ATT" + EOL + - " 10000" + EOL + - " MMF" + EOL + - " " + EOL + - " " + EOL + - " ASD-987-M31" + EOL + - " USOSTCDALTX0101UJZZ31" + EOL + - " FG-VM00*" + EOL + + " IVLAN" + EOL + + " SHELLUCPE31" + EOL + + " " + EOL + + " " + EOL + + " AVPN" + EOL + + " 1000BASE-T" + EOL + + " ATT" + EOL + + " BT/SLIR/70911" + EOL + + " Active" + EOL + + " WAN1" + EOL + + " ATT" + EOL + + " 100" + EOL + + " ELECTRICAL" + EOL + + " " + EOL + + " " + EOL + + " AVPN" + EOL + + " 10/100/1000BASE-T" + EOL + + " ATT" + EOL + + " AS/KRFN/34611" + EOL + + " Active" + EOL + + " WAN2" + EOL + + " ATT" + EOL + + " 10000" + EOL + + " MMF" + EOL + + " " + EOL + + " " + EOL + + " ASD-987-M31" + EOL + + " USOSTCDALTX0101UJZZ31" + EOL + + " FG-VM00*" + EOL + ""; // JSON request w/ embedded XML will be read from a file private static String jsonReq; private static String jsonReqArray; - + @BeforeClass public static void initialize() throws Exception { jsonReq = readFileToString("src/test/resources/request.json"); jsonReqArray = readFileToString("src/test/resources/requestArray.json"); - } - + } + private static String readFileToString(String path) throws IOException { File file = new File(path); return new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8); @@ -164,16 +170,16 @@ public class JsonUtilsTest { @Test public void shouldConvertXmlToJsonAndBackToSameXml() throws Exception { - // Note: the current version of the JsonUtils.json2xml() method - // does not support converting the JSONObject representation - // of XML attributes (JSONArray) back to XML. So this test will - // only succeed if the original XML does not contain attributes - + // Note: the current version of the JsonUtils.json2xml() method + // does not support converting the JSONObject representation + // of XML attributes (JSONArray) back to XML. So this test will + // only succeed if the original XML does not contain attributes + // given String xmlIn = XmlTool.removeNamespaces(XML_REQ_NO_ATTRS); // when String json = JsonUtils.xml2json(XML_REQ_NO_ATTRS); - String xmlOut = JsonUtils.json2xml(json); + String xmlOut = JsonUtils.json2xml(json); // then Diff diffXml = DiffBuilder.compare(xmlIn).withTest(xmlOut).ignoreWhitespace() .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build(); @@ -233,7 +239,7 @@ public class JsonUtilsTest { assertThat(JsonUtils.getJsonParamValue(json, "vnf-request.vnf-params.param", "name", 2)) .withFailMessage("Expected null for index out of bound").isNull(); } - + @Test public void shouldAddJsonValue() throws Exception { // given @@ -259,7 +265,7 @@ public class JsonUtilsTest { // then String extractedValue = JsonUtils.getJsonValue(jsonUpd, key); assertThat(extractedValue).isEqualTo(oldValue).isNotEqualTo(newValue); - } + } @Test public void shouldUpdateValueInJson() throws Exception { @@ -286,6 +292,11 @@ public class JsonUtilsTest { // then String extractedValue = JsonUtils.getJsonValue(jsonUpd, key); assertThat(extractedValue).isNotEqualTo(oldValue).isNull(); + JSONObject jsonObj = new JSONObject(json); + Integer intValue = JsonUtils.getJsonIntValueForKey(jsonObj, "persona-model-version"); + Assert.assertTrue(intValue == 1); + Boolean boolValue = JsonUtils.getJsonBooleanValueForKey(jsonObj, "is-base-module"); + Assert.assertTrue(boolValue); } @Test @@ -298,29 +309,48 @@ public class JsonUtilsTest { // then assertThat(jsonUpd).isEqualTo(json); } - + @Test public void shouldConvertXmlToJsonAndBackToSameXmlExtractedFromTheRequest() throws Exception { // given - String value = JsonUtils.getJsonValue(jsonReq, "variables.bpmnRequest.value"); - String xmlReq = XmlTool.removeNamespaces(XmlTool.normalize(value)); + String value = JsonUtils.getJsonValue(jsonReq, "variables.bpmnRequest.value"); + String xmlReq = XmlTool.removeNamespaces(XmlTool.normalize(value)); // when - String json = JsonUtils.xml2json(xmlReq); - String xmlOut = JsonUtils.json2xml(json); + String json = JsonUtils.xml2json(xmlReq); + String xmlOut = JsonUtils.json2xml(json); // then Diff diffXml = DiffBuilder.compare(xmlReq).withTest(xmlOut).ignoreWhitespace() .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build(); assertThat(diffXml.hasDifferences()).withFailMessage(diffXml.toString()).isFalse(); - } - + } + @Test public void shouldConvertJsonContainingArrayToXml() throws Exception { // when - String jsonParm = JsonUtils.getJsonNodeValue(jsonReqArray, "requestDetails.requestParameters.ucpeInfo"); - String xmlOut = JsonUtils.json2xml(jsonParm); + String jsonParm = JsonUtils.getJsonNodeValue(jsonReqArray, "requestDetails.requestParameters.ucpeInfo"); + String xmlOut = JsonUtils.json2xml(jsonParm); // then Diff diffXml = DiffBuilder.compare(XML_ARRAY_REQ).withTest(xmlOut).ignoreWhitespace() .withNodeMatcher(new DefaultNodeMatcher(ElementSelectors.byName)).checkForSimilar().build(); assertThat(diffXml.hasDifferences()).withFailMessage(diffXml.toString()).isFalse(); } + + @Test + // Tests the jsonSchemaValidation() method + public void testJsonSchemaValidation() { + try { + String myReqArray = jsonReqArray; + String result = JsonUtils.jsonSchemaValidation(myReqArray, "src/test/resources/requestSchema.json"); + System.out.println("Schema Validation Result: " + result); + Assert.assertTrue(result.contains("success")); + // remove a required parameter from the JSON doc so that validation fails + myReqArray = JsonUtils.delJsonValue(myReqArray, "requestDetails.requestParameters.ucpeInfo.ucpeHostName"); + result = JsonUtils.jsonSchemaValidation(myReqArray, "src/test/resources/requestSchema.json"); + System.out.println("Schema Validation Result: " + result); + Assert.assertTrue(result.contains("failure")); + Assert.assertTrue(result.contains("error: object has missing required properties ([\"ucpeHostName\"])")); + } catch (ValidationException e) { + e.printStackTrace(); + } + } } diff --git a/bpmn/MSOCoreBPMN/src/test/java/org/openecomp/mso/bpmn/core/json/JsonUtilsTest.java b/bpmn/MSOCoreBPMN/src/test/java/org/openecomp/mso/bpmn/core/json/JsonUtilsTest.java new file mode 100644 index 0000000000..7bb10007ac --- /dev/null +++ b/bpmn/MSOCoreBPMN/src/test/java/org/openecomp/mso/bpmn/core/json/JsonUtilsTest.java @@ -0,0 +1,65 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP - SO + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.mso.bpmn.core.json; + +import static org.junit.Assert.assertEquals; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Map; + +import org.camunda.bpm.engine.delegate.DelegateExecution; +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Test; +import org.mockito.Mock; + +public class JsonUtilsTest { + + @Mock public DelegateExecution execution; + private final String fileLocation = "src/test/resources/json-examples/"; + + @Test + public void jsonStringToMapTest() throws IOException { + + JsonUtils utils = new JsonUtils(); + String response = this.getJson("SDNCServiceResponseExample.json"); + String entry = utils.getJsonValue(response, "SDNCServiceResponse.params"); + Map map = utils.jsonStringToMap(execution, entry); + assertEquals(map.get("e2e-vpn-key"), "my-key"); + } + + @Test + public void entryArrayToMapTest() throws IOException { + JsonUtils utils = new JsonUtils(); + String response = this.getJson("SNIROExample.json"); + String entry = utils.getJsonValue(response, "solutionInfo.placementInfo"); + JSONArray arr = new JSONArray(entry); + JSONObject homingDataJson = arr.getJSONObject(0); + JSONArray assignmentInfo = homingDataJson.getJSONArray("assignmentInfo"); + Map map = utils.entryArrayToMap(execution, assignmentInfo.toString(), "variableName", "variableValue"); + assertEquals(map.get("cloudOwner"), "att-aic"); + } + private String getJson(String filename) throws IOException { + return new String(Files.readAllBytes(Paths.get(fileLocation + filename))); + } +} -- cgit 1.2.3-korg