From b35670742c728d7d85da2f9c856c51a5889449d6 Mon Sep 17 00:00:00 2001 From: Ofir Sonsino Date: Mon, 4 Sep 2017 14:52:07 +0300 Subject: [VID-55] Upgrade Tosca Parser (merge) Change-Id: I5389f1641ef22df3ed569a154b3c367117c04d04 Signed-off-by: Ofir Sonsino --- .../opencomp/vid/controller/VidControllerTest.java | 128 +++++++++++++++++++++ .../java/org/opencomp/vid/testUtils/TestUtils.java | 65 +++++++++++ .../ecomp/vid/selenium/LogOutLeftPane.java | 2 +- .../core/MockApplicationContextTestSuite.java | 3 +- .../fusionapp/service/ProfileServiceTest.java | 16 +-- 5 files changed, 204 insertions(+), 10 deletions(-) create mode 100644 vid-app-common/src/test/java/org/opencomp/vid/controller/VidControllerTest.java create mode 100644 vid-app-common/src/test/java/org/opencomp/vid/testUtils/TestUtils.java (limited to 'vid-app-common/src/test/java/org') diff --git a/vid-app-common/src/test/java/org/opencomp/vid/controller/VidControllerTest.java b/vid-app-common/src/test/java/org/opencomp/vid/controller/VidControllerTest.java new file mode 100644 index 00000000..05ca8c32 --- /dev/null +++ b/vid-app-common/src/test/java/org/opencomp/vid/controller/VidControllerTest.java @@ -0,0 +1,128 @@ +package org.opencomp.vid.controller; + +import net.javacrumbs.jsonunit.JsonAssert; +import org.apache.commons.io.IOUtils; +import org.codehaus.jackson.map.ObjectMapper; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.openecomp.vid.asdc.AsdcCatalogException; +import org.openecomp.vid.asdc.AsdcClient; +import org.openecomp.vid.asdc.parser.ToscaParserImpl2; +import org.openecomp.vid.controller.WebConfig; +import org.openecomp.vid.model.*; +import org.openecomp.vid.properties.AsdcClientConfiguration; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Path; +import java.util.Map; +import java.util.UUID; + +import static org.opencomp.vid.testUtils.TestUtils.assertJsonStringEqualsIgnoreNulls; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {WebConfig.class, AsdcClientConfiguration.class}) +public class VidControllerTest { + + @Autowired + private AsdcClient asdcClient; + + private String uuid = "f430728a-4530-42be-a577-1206b9484cef"; + //TODO: add as a test case. + private String vfFilePath = "vf-csar.JSON"; + private String vlFilePath = "vl-csar.JSON"; + + private ToscaParserImpl2 p2 = new ToscaParserImpl2(); + private ObjectMapper om = new ObjectMapper(); + final InputStream jsonFile = VidControllerTest.class.getClassLoader().getResourceAsStream(vfFilePath); + + + @Test + public void assertEqualsBetweenServices() throws Exception { + Service expectedService = getExpectedServiceModel().getService(); + Service actualService = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getService(); + JsonAssert.assertJsonEquals(expectedService, actualService); + } + + @Test + public void assertEqualBetweenObjects() throws Exception { + ServiceModel actualServiceModel = p2.makeServiceModel(getCsarPath(), getServiceByUuid()); + JsonAssert.assertJsonEquals(getExpectedServiceModel(), actualServiceModel); + } + + @Test + public void assertEqualsBetweenNetworkNodes() throws Exception { + Map expectedNetworksMap = getExpectedServiceModel().getNetworks(); + Map actualNetworksMap = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getNetworks(); + for (Map.Entry entry : expectedNetworksMap.entrySet()) { + Network expectedNetwork = entry.getValue(); + Network actualNetwork = actualNetworksMap.get(entry.getKey()); + Assert.assertEquals(expectedNetwork.getModelCustomizationName(), actualNetwork.getModelCustomizationName()); + verifyBaseNodeProperties(expectedNetwork, actualNetwork); + compareProperties(expectedNetwork.getProperties(), actualNetwork.getProperties()); + } + } + + //Because we are not supporting the old flow, the JSON are different by definition. + @Test + public void assertEqualsBetweenVnfsOfTosca() throws Exception { + Map expectedVnfsMap = getExpectedServiceModel().getVnfs(); + Map actualVnfsMap = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getVnfs(); + for (Map.Entry entry : expectedVnfsMap.entrySet()) { + VNF expectedVnf = entry.getValue(); + VNF actualVnf = actualVnfsMap.get(entry.getKey()); + verifyBaseNodeProperties(expectedVnf, actualVnf); + Assert.assertEquals(expectedVnf.getModelCustomizationName(), actualVnf.getModelCustomizationName()); + compareProperties(expectedVnf.getProperties(), actualVnf.getProperties()); + assertJsonStringEqualsIgnoreNulls(om.writeValueAsString(expectedVnf), om.writeValueAsString(actualVnf)); + } + } + + @Test + public void assertEqualsBetweenVolumeGroups() throws Exception { + Map actualVolumeGroups = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getVolumeGroups(); + Map expectedVolumeGroups = getExpectedServiceModel().getVolumeGroups(); + JsonAssert.assertJsonEquals(actualVolumeGroups, expectedVolumeGroups); + } + + @Test + public void assertEqualsBetweenVfModules() throws Exception { + Map actualVfModules = p2.makeServiceModel(getCsarPath(), getServiceByUuid()).getVfModules(); + Map expectedVfModules = getExpectedServiceModel().getVfModules(); + JsonAssert.assertJsonEquals(actualVfModules, expectedVfModules); + } + + private void verifyBaseNodeProperties(Node expectedNode, Node actualNode) { + Assert.assertEquals(expectedNode.getName(), actualNode.getName()); + Assert.assertEquals(expectedNode.getCustomizationUuid(), actualNode.getCustomizationUuid()); + Assert.assertEquals(expectedNode.getDescription(), actualNode.getDescription()); + Assert.assertEquals(expectedNode.getInvariantUuid(), actualNode.getInvariantUuid()); + Assert.assertEquals(expectedNode.getUuid(), actualNode.getUuid()); + Assert.assertEquals(expectedNode.getVersion(), actualNode.getVersion()); + } + + private void compareProperties(Map expectedProperties, Map actualProperties) { + for (Map.Entry property : expectedProperties.entrySet()) { + String expectedValue = property.getValue(); + String key = property.getKey(); + String actualValue = actualProperties.get(key); + Assert.assertEquals(expectedValue, actualValue); + } + } + + private NewServiceModel getExpectedServiceModel() throws IOException { + String expectedJsonAsString = IOUtils.toString(jsonFile).toString(); + return om.readValue(expectedJsonAsString,NewServiceModel.class); + } + + private Path getCsarPath() throws AsdcCatalogException { + return asdcClient.getServiceToscaModel(UUID.fromString(uuid)); + } + + private org.openecomp.vid.asdc.beans.Service getServiceByUuid() throws AsdcCatalogException { + return asdcClient.getService(UUID.fromString(uuid)); + } +} \ No newline at end of file diff --git a/vid-app-common/src/test/java/org/opencomp/vid/testUtils/TestUtils.java b/vid-app-common/src/test/java/org/opencomp/vid/testUtils/TestUtils.java new file mode 100644 index 00000000..29115e0a --- /dev/null +++ b/vid-app-common/src/test/java/org/opencomp/vid/testUtils/TestUtils.java @@ -0,0 +1,65 @@ +package org.opencomp.vid.testUtils; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.junit.Assert; + +import java.util.Iterator; + +import static fj.parser.Parser.fail; + +/** + * Created by Oren on 6/7/17. + */ +public class TestUtils { + + /** + * The method compares between two jsons. the function assert that the actual object does not reduce or change the functionallity/parsing of the expected json. + * This means that if the expected JSON has a key which is null or the JSON doesn't have a key which contained in the expected JSON the assert will succeed and the will pass. + * For example : For JSON expected = {a:null} and actual {a:3} the test will pass + * Other example : For JSON expected = {a:3} and actual {a:null} the test will fail + * + * @param expected JSON + * @param actual JSON + */ + public static void assertJsonStringEqualsIgnoreNulls(String expected, String actual) { + if (expected == null || expected == JSONObject.NULL) {return;} + + JSONObject expectedJSON = new JSONObject(expected); + JSONObject actualJSON = new JSONObject(actual); + Iterator keys = expectedJSON.keys(); + + while( keys.hasNext() ) { + String key = (String)keys.next(); + Object expectedValue = expectedJSON.get(key); + if (expectedValue == JSONObject.NULL){ + continue; + } + + Object actualValue = actualJSON.get(key); + + if (expectedValue instanceof JSONObject) { + String expectedVal = expectedValue.toString(); + String actualVal = actualValue.toString(); + assertJsonStringEqualsIgnoreNulls(expectedVal, actualVal); + } + else if (expectedValue instanceof JSONArray) { + if (actualValue instanceof JSONArray) { + JSONArray expectedJSONArray = (JSONArray)expectedValue; + JSONArray actualJSONArray = (JSONArray)expectedValue; + for (int i = 0; i < expectedJSONArray.length(); i++) { + String expectedItem = expectedJSONArray.getJSONObject(i).toString(); + String actualItem = actualJSONArray.getJSONObject(i).toString(); + assertJsonStringEqualsIgnoreNulls(expectedItem, actualItem); + } + } + else { + fail("expected: " + expectedValue + " got:" + actualValue); + } + } + else { + Assert.assertEquals(expectedValue, actualValue); + } + } + } +} diff --git a/vid-app-common/src/test/java/org/openecomp/ecomp/vid/selenium/LogOutLeftPane.java b/vid-app-common/src/test/java/org/openecomp/ecomp/vid/selenium/LogOutLeftPane.java index 66ea075e..55aed8fb 100755 --- a/vid-app-common/src/test/java/org/openecomp/ecomp/vid/selenium/LogOutLeftPane.java +++ b/vid-app-common/src/test/java/org/openecomp/ecomp/vid/selenium/LogOutLeftPane.java @@ -40,7 +40,7 @@ import org.testng.annotations.Test; /** * The Class LogOutLeftPane. */ -@Test(enabled=true) +@Test(enabled=false) public class LogOutLeftPane { /** The login button. */ diff --git a/vid-app-common/src/test/java/org/openecomp/fusion/core/MockApplicationContextTestSuite.java b/vid-app-common/src/test/java/org/openecomp/fusion/core/MockApplicationContextTestSuite.java index 64d72abe..2f5828b6 100755 --- a/vid-app-common/src/test/java/org/openecomp/fusion/core/MockApplicationContextTestSuite.java +++ b/vid-app-common/src/test/java/org/openecomp/fusion/core/MockApplicationContextTestSuite.java @@ -38,7 +38,7 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; - +import org.testng.annotations.Test; import org.openecomp.portalsdk.core.conf.AppConfig; import org.openecomp.portalsdk.core.objectcache.AbstractCacheManager; import org.openecomp.portalsdk.core.util.SystemProperties; @@ -61,6 +61,7 @@ import org.openecomp.portalsdk.core.util.CacheManager; @WebAppConfiguration @ContextConfiguration(loader = AnnotationConfigWebContextLoader.class, classes = {MockAppConfig.class}) @ActiveProfiles(value="test") +@Test(enabled=false) public class MockApplicationContextTestSuite { /** The wac. */ diff --git a/vid-app-common/src/test/java/org/openecomp/fusionapp/service/ProfileServiceTest.java b/vid-app-common/src/test/java/org/openecomp/fusionapp/service/ProfileServiceTest.java index ce3e6ba5..85c550d4 100755 --- a/vid-app-common/src/test/java/org/openecomp/fusionapp/service/ProfileServiceTest.java +++ b/vid-app-common/src/test/java/org/openecomp/fusionapp/service/ProfileServiceTest.java @@ -36,6 +36,7 @@ import org.openecomp.portalsdk.core.service.UserProfileService; /** * The Class ProfileServiceTest. */ + public class ProfileServiceTest extends MockApplicationContextTestSuite { /** The service. */ @@ -51,14 +52,13 @@ public class ProfileServiceTest extends MockApplicationContextTestSuite { */ @Test public void testFindAll() { - - try { - List profiles = service.findAll(); - Assert.assertTrue(profiles.size() > 0); - } - catch (Exception e) { - //TODO: this is only to make maven to not complaint - return; + + List profiles; + try { + profiles = service.findAll(); + Assert.assertTrue(profiles.size() > 0); + } catch (Exception e) { + Assert.assertTrue(false); } } -- cgit 1.2.3-korg