From 578368fce559ad28eb3bc62064e4c39858fa3339 Mon Sep 17 00:00:00 2001 From: "Fraboni, Gino (gf403a)" Date: Wed, 3 May 2017 13:47:43 -0400 Subject: Implement support for v10 model entities. Change-Id: Id2b42b70d7fe0ada6695ac9632b314f21ace193f Signed-off-by: Fraboni, Gino (gf403a) --- .../modelloader/config/ModelLoaderConfigTest.java | 247 +++++++------- .../catalog/VnfCatalogArtifactHandlerTest.java | 198 ++++++------ .../entity/model/ModelArtifactParserTest.java | 356 ++++++++++++--------- .../modelloader/entity/model/ModelSorterTest.java | 271 ++++++++-------- .../modelloader/restclient/AAIRestClientTest.java | 119 ------- .../modelloader/restclient/AaiRestClientTest.java | 121 +++++++ .../service/ModelLoaderServiceTest.java | 115 ------- .../modelloader/util/JsonXmlConverterTest.java | 160 ++++----- 8 files changed, 770 insertions(+), 817 deletions(-) delete mode 100644 src/test/java/org/openecomp/modelloader/restclient/AAIRestClientTest.java create mode 100644 src/test/java/org/openecomp/modelloader/restclient/AaiRestClientTest.java delete mode 100644 src/test/java/org/openecomp/modelloader/service/ModelLoaderServiceTest.java (limited to 'src/test/java/org') diff --git a/src/test/java/org/openecomp/modelloader/config/ModelLoaderConfigTest.java b/src/test/java/org/openecomp/modelloader/config/ModelLoaderConfigTest.java index 4018f14..0d068d5 100644 --- a/src/test/java/org/openecomp/modelloader/config/ModelLoaderConfigTest.java +++ b/src/test/java/org/openecomp/modelloader/config/ModelLoaderConfigTest.java @@ -1,122 +1,125 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * 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.modelloader.config; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - -import java.io.FileInputStream; -import java.io.IOException; -import java.util.List; -import java.util.Properties; - -import org.eclipse.jetty.util.security.Password; -import org.junit.Test; -import org.openecomp.modelloader.restclient.AaiRestClient; - -import org.openecomp.sdc.utils.ArtifactTypeEnum; - -public class ModelLoaderConfigTest { - - @Test - public void testYangModelArtifactType() { - Properties props = new Properties(); - props.setProperty("ml.distribution.ARTIFACT_TYPES", - "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG"); - ModelLoaderConfig config = new ModelLoaderConfig(props); - - List types = config.getRelevantArtifactTypes(); - - System.out.println("ArtifactType: " + types.get(0)); - assertEquals(0, - types.get(0).compareToIgnoreCase(ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.toString())); - - System.out.println("ArtifactType: " + types.get(1)); - assertEquals(0, types.get(1).compareToIgnoreCase(ArtifactTypeEnum.MODEL_QUERY_SPEC.toString())); - - System.out.println("ArtifactType: " + types.get(2)); - assertEquals(0, types.get(2).compareToIgnoreCase(ArtifactTypeEnum.VNF_CATALOG.toString())); - - assertEquals(3, types.size()); - } - - @Test - public void testDecryptPassword() { - Properties props = new Properties(); - String testPass = "youshallnotpass"; - String encryptedTestPass = Password.obfuscate(testPass); - - System.out.println("Encrypt " + testPass + " ==> " + encryptedTestPass); - - props.put(ModelLoaderConfig.PROP_ML_DISTRIBUTION_PASSWORD, encryptedTestPass); - ModelLoaderConfig config = new ModelLoaderConfig(props); - - assertEquals(testPass, config.getPassword()); - } - - @Test - public void testDecryptKeystorePassword() { - Properties props = new Properties(); - String testPass = "youshallnotpass"; - String encryptedTestPass = Password.obfuscate(testPass); - - System.out.println("Encrypt " + testPass + " ==> " + encryptedTestPass); - - props.put(ModelLoaderConfig.PROP_ML_DISTRIBUTION_KEYSTORE_PASSWORD, encryptedTestPass); - ModelLoaderConfig config = new ModelLoaderConfig(props); - - assertEquals(testPass, config.getKeyStorePassword()); - } - - @Test - public void testDecryptAAIPassword() { - - Properties props = new Properties(); - String testPassword = "myvoiceismypassword"; - String encryptedTestPassword = Password.obfuscate(testPassword); - - props.put(ModelLoaderConfig.PROP_AAI_AUTHENTICATION_PASSWORD, encryptedTestPassword); - ModelLoaderConfig config = new ModelLoaderConfig(props); - - assertEquals(testPassword, config.getAaiAuthenticationPassword()); - } - - @Test - public void testNoAAIAuth() throws IOException { - - Properties props = new Properties(); - props.load( - new FileInputStream("src/test/resources/model-loader-empty-auth-password.properties")); - - ModelLoaderConfig config = new ModelLoaderConfig(props); - AaiRestClient aaiClient = new AaiRestClient(config); - - assertFalse("Empty AAI Password should result in no basic authentication", - aaiClient.useBasicAuth()); - - props.load(new FileInputStream("src/test/resources/model-loader-no-auth-password.properties")); - config = new ModelLoaderConfig(props); - aaiClient = new AaiRestClient(config); - - assertFalse("No AAI Password should result in no basic authentication", - aaiClient.useBasicAuth()); - } -} +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.config; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import java.io.FileInputStream; +import java.io.IOException; +import java.util.List; +import java.util.Properties; + +import org.eclipse.jetty.util.security.Password; +import org.junit.Test; +import org.openecomp.modelloader.restclient.AaiRestClient; + +import org.openecomp.sdc.utils.ArtifactTypeEnum; + +public class ModelLoaderConfigTest { + + @Test + public void testYangModelArtifactType() { + Properties props = new Properties(); + props.setProperty("ml.distribution.ARTIFACT_TYPES", + "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG"); + ModelLoaderConfig config = new ModelLoaderConfig(props, null); + + List types = config.getRelevantArtifactTypes(); + + System.out.println("ArtifactType: " + types.get(0)); + assertEquals(0, + types.get(0).compareToIgnoreCase(ArtifactTypeEnum.MODEL_INVENTORY_PROFILE.toString())); + + System.out.println("ArtifactType: " + types.get(1)); + assertEquals(0, types.get(1).compareToIgnoreCase(ArtifactTypeEnum.MODEL_QUERY_SPEC.toString())); + + System.out.println("ArtifactType: " + types.get(2)); + assertEquals(0, types.get(2).compareToIgnoreCase(ArtifactTypeEnum.VNF_CATALOG.toString())); + + assertEquals(3, types.size()); + } + + @Test + public void testDecryptPassword() { + Properties props = new Properties(); + String testPass = "youshallnotpass"; + String encryptedTestPass = Password.obfuscate(testPass); + + System.out.println("Encrypt " + testPass + " ==> " + encryptedTestPass); + + props.put(ModelLoaderConfig.PROP_ML_DISTRIBUTION_PASSWORD, encryptedTestPass); + ModelLoaderConfig config = new ModelLoaderConfig(props, null); + + assertEquals(testPass, config.getPassword()); + } + + @Test + public void testDecryptKeystorePassword() { + Properties props = new Properties(); + String testPass = "youshallnotpass"; + String encryptedTestPass = Password.obfuscate(testPass); + + System.out.println("Encrypt " + testPass + " ==> " + encryptedTestPass); + + props.put(ModelLoaderConfig.PROP_ML_DISTRIBUTION_KEYSTORE_PASSWORD, encryptedTestPass); + ModelLoaderConfig config = new ModelLoaderConfig(props, null); + + assertEquals(testPass, config.getKeyStorePassword()); + } + + @Test + public void testDecryptAAIPassword() { + + Properties props = new Properties(); + String testPassword = "myvoiceismypassword"; + String encryptedTestPassword = Password.obfuscate(testPassword); + + props.put(ModelLoaderConfig.PROP_AAI_AUTHENTICATION_PASSWORD, encryptedTestPassword); + ModelLoaderConfig config = new ModelLoaderConfig(props, null); + + assertEquals(testPassword, config.getAaiAuthenticationPassword()); + } + + @Test + public void testNoAAIAuth() throws IOException { + + Properties props = new Properties(); + props.load( + new FileInputStream("src/test/resources/model-loader-empty-auth-password.properties")); + + ModelLoaderConfig config = new ModelLoaderConfig(props, null); + AaiRestClient aaiClient = new AaiRestClient(config); + + assertFalse("Empty AAI Password should result in no basic authentication", + aaiClient.useBasicAuth()); + + props.load(new FileInputStream("src/test/resources/model-loader-no-auth-password.properties")); + config = new ModelLoaderConfig(props, null); + aaiClient = new AaiRestClient(config); + + assertFalse("No AAI Password should result in no basic authentication", + aaiClient.useBasicAuth()); + } + +} diff --git a/src/test/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifactHandlerTest.java b/src/test/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifactHandlerTest.java index 1b1d955..c81b51d 100644 --- a/src/test/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifactHandlerTest.java +++ b/src/test/java/org/openecomp/modelloader/entity/catalog/VnfCatalogArtifactHandlerTest.java @@ -1,98 +1,100 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * 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.modelloader.entity.catalog; - -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.List; -import java.util.Properties; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.ArgumentCaptor; -import org.mockito.Mockito; -import org.openecomp.modelloader.config.ModelLoaderConfig; -import org.openecomp.modelloader.entity.Artifact; -import org.openecomp.modelloader.restclient.AaiRestClient; -import org.openecomp.modelloader.restclient.AaiRestClient.MimeType; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import com.sun.jersey.api.client.ClientResponse; - -@RunWith(PowerMockRunner.class) -@PrepareForTest({ VnfCatalogArtifactHandler.class, ClientResponse.class, AaiRestClient.class }) -public class VnfCatalogArtifactHandlerTest { - - protected static String CONFIG_FILE = "model-loader.properties"; - - @Test - public void testWithMocks() throws Exception { - - Properties configProperties = new Properties(); - try { - configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); - } catch (IOException e) { - fail(); - } - ModelLoaderConfig config = new ModelLoaderConfig(configProperties); - - ClientResponse mockGetResp = PowerMockito.mock(ClientResponse.class); - PowerMockito.when(mockGetResp.getStatus()).thenReturn(200).thenReturn(200).thenReturn(404) - .thenReturn(404).thenReturn(200); // only second two will be PUT - ClientResponse mockPutResp = PowerMockito.mock(ClientResponse.class); - PowerMockito.when(mockPutResp.getStatus()).thenReturn(201); - - AaiRestClient mockRestClient = PowerMockito.mock(AaiRestClient.class); - PowerMockito.whenNew(AaiRestClient.class).withAnyArguments().thenReturn(mockRestClient); - PowerMockito.when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), - Mockito.any(MimeType.class))).thenReturn(mockGetResp); - PowerMockito.when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), - Mockito.anyString(), Mockito.any(MimeType.class))).thenReturn(mockPutResp); - - VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config); - - String examplePath = "src/test/resources/vnfcatalogexample.xml"; - - byte[] encoded = Files.readAllBytes(Paths.get(examplePath)); - String payload = new String(encoded, "utf-8"); - - VnfCatalogArtifact artifact = new VnfCatalogArtifact(payload); - List artifacts = new ArrayList(); - artifacts.add(artifact); - - String distributionID = "test"; - - assertTrue(vnfCAH.pushArtifacts(artifacts, distributionID)); - // times(2) bc with above get returns should only get to this part twice - ArgumentCaptor argument = ArgumentCaptor.forClass(String.class); - Mockito.verify(mockRestClient, Mockito.times(2)).putResource(Mockito.anyString(), - argument.capture(), Mockito.anyString(), Mockito.any(MimeType.class)); - assertTrue(argument.getAllValues().get(0).contains("5.2.5")); - assertTrue(argument.getAllValues().get(1).contains("5.2.4")); - } -} +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.catalog; + +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import java.util.Properties; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mockito; +import org.openecomp.modelloader.config.ModelLoaderConfig; +import org.openecomp.modelloader.entity.Artifact; +import org.openecomp.modelloader.restclient.AaiRestClient; +import org.openecomp.modelloader.restclient.AaiRestClient.MimeType; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; + +import com.sun.jersey.api.client.ClientResponse; + +@RunWith(PowerMockRunner.class) +@PrepareForTest({ VnfCatalogArtifactHandler.class, ClientResponse.class, AaiRestClient.class }) +public class VnfCatalogArtifactHandlerTest { + + protected static String CONFIG_FILE = "model-loader.properties"; + + @Test + public void testWithMocks() throws Exception { + + Properties configProperties = new Properties(); + try { + configProperties.load(this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILE)); + } catch (IOException e) { + fail(); + } + ModelLoaderConfig config = new ModelLoaderConfig(configProperties, null); + + ClientResponse mockGetResp = PowerMockito.mock(ClientResponse.class); + PowerMockito.when(mockGetResp.getStatus()).thenReturn(200).thenReturn(200).thenReturn(404) + .thenReturn(404).thenReturn(200); // only second two will be PUT + ClientResponse mockPutResp = PowerMockito.mock(ClientResponse.class); + PowerMockito.when(mockPutResp.getStatus()).thenReturn(201); + + AaiRestClient mockRestClient = PowerMockito.mock(AaiRestClient.class); + PowerMockito.whenNew(AaiRestClient.class).withAnyArguments().thenReturn(mockRestClient); + PowerMockito.when(mockRestClient.getResource(Mockito.anyString(), Mockito.anyString(), + Mockito.any(MimeType.class))).thenReturn(mockGetResp); + PowerMockito.when(mockRestClient.putResource(Mockito.anyString(), Mockito.anyString(), + Mockito.anyString(), Mockito.any(MimeType.class))).thenReturn(mockPutResp); + + VnfCatalogArtifactHandler vnfCAH = new VnfCatalogArtifactHandler(config); + + String examplePath = "src/test/resources/vnfcatalogexample.xml"; + + byte[] encoded = Files.readAllBytes(Paths.get(examplePath)); + String payload = new String(encoded, "utf-8"); + + VnfCatalogArtifact artifact = new VnfCatalogArtifact(payload); + List artifacts = new ArrayList(); + artifacts.add(artifact); + + String distributionID = "test"; + + assertTrue(vnfCAH.pushArtifacts(artifacts, distributionID)); + // times(2) bc with above get returns should only get to this part twice + ArgumentCaptor argument = ArgumentCaptor.forClass(String.class); + Mockito.verify(mockRestClient, Mockito.times(2)).putResource(Mockito.anyString(), + argument.capture(), Mockito.anyString(), Mockito.any(MimeType.class)); + assertTrue(argument.getAllValues().get(0).contains("5.2.5")); + assertTrue(argument.getAllValues().get(1).contains("5.2.4")); + } +} diff --git a/src/test/java/org/openecomp/modelloader/entity/model/ModelArtifactParserTest.java b/src/test/java/org/openecomp/modelloader/entity/model/ModelArtifactParserTest.java index f7b7871..994c4db 100644 --- a/src/test/java/org/openecomp/modelloader/entity/model/ModelArtifactParserTest.java +++ b/src/test/java/org/openecomp/modelloader/entity/model/ModelArtifactParserTest.java @@ -1,151 +1,205 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * 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.modelloader.entity.model; - -import static org.junit.Assert.assertTrue; - -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.List; - -import org.junit.Test; -import org.openecomp.modelloader.entity.Artifact; - -public class ModelArtifactParserTest { - - @Test - public void testParseModelFileNoDeps() throws Exception { - final String MODEL_FILE = "src/test/resources/models/vnf-model.xml"; - - try { - byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); - - ModelArtifactParser parser = new ModelArtifactParser(); - List modelList = parser.parse(xmlBytes, "test-artifact"); - - assertTrue(modelList.size() == 1); - - ModelArtifact model = (ModelArtifact) modelList.get(0); - System.out.println(model.toString()); - - assertTrue(model.getNameVersionId().equalsIgnoreCase("model-vid")); - assertTrue(model.getType().toString().equalsIgnoreCase("MODEL")); - assertTrue(model.getDependentModelIds().size() == 0); - } catch (Exception e) { - e.printStackTrace(); - assertTrue(false); - } - } - - @Test - public void testParseModelFileDeps() throws Exception { - final String MODEL_FILE = "src/test/resources/models/wan-connector-model.xml"; - - try { - byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); - - ModelArtifactParser parser = new ModelArtifactParser(); - List modelList = parser.parse(xmlBytes, "test-artifact"); - - assertTrue(modelList.size() == 1); - - ModelArtifact model = (ModelArtifact) modelList.get(0); - System.out.println(model.toString()); - - assertTrue(model.getNameVersionId().equalsIgnoreCase("93d9d45d-7eec-4371-9083-675e4c353de3")); - assertTrue(model.getType().toString().equalsIgnoreCase("MODEL")); - assertTrue(model.getDependentModelIds().size() == 7); - assertTrue(model.getDependentModelIds().contains("d09dd9da-0148-46cd-a947-591afc844d24")); - assertTrue(model.getDependentModelIds().contains("ae16244f-4d29-4801-a559-e25f2db2a4c3")); - assertTrue(model.getDependentModelIds().contains("a6d9de88-4046-4b78-a59e-5691243d292a")); - } catch (Exception e) { - e.printStackTrace(); - assertTrue(false); - } - } - - @Test - public void testParseCompleteModel() throws Exception { - final String MODEL_FILE = "src/test/resources/models/complete-model.xml"; - - try { - byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); - - ModelArtifactParser parser = new ModelArtifactParser(); - List modelList = parser.parse(xmlBytes, "test-artifact"); - - for (Artifact art : modelList) { - ModelArtifact model = (ModelArtifact) art; - System.out.println(model.toString()); - } - - assertTrue(modelList.size() == 3); - - ModelArtifact modelVdc = (ModelArtifact) modelList.get(0); - assertTrue( - modelVdc.getNameVersionId().equalsIgnoreCase("997fc7-fca1-451f-b953-9a1e6197b4d6")); - assertTrue(modelVdc.getType().toString().equalsIgnoreCase("MODEL")); - assertTrue(modelVdc.getDependentModelIds().size() == 1); - assertTrue(modelVdc.getDependentModelIds().contains("93d9d45d-7eec-4371-9083-675e4c353de3")); - - ModelArtifact modelPserver = (ModelArtifact) modelList.get(2); - assertTrue( - modelPserver.getNameVersionId().equalsIgnoreCase("f2b24d95-c582-48d5-b2d6-c5b3a94ce812")); - assertTrue(modelPserver.getType().toString().equalsIgnoreCase("MODEL")); - assertTrue(modelPserver.getDependentModelIds().size() == 2); - assertTrue( - modelPserver.getDependentModelIds().contains("35be1acf-1298-48c6-a128-66850083b8bd")); - assertTrue( - modelPserver.getDependentModelIds().contains("759dbd4a-2473-46f3-a932-48d987c9b4a1")); - } catch (Exception e) { - e.printStackTrace(); - assertTrue(false); - } - } - - @Test - public void testParseNamedQuery() throws Exception { - final String MODEL_FILE = "src/test/resources/models/named-query-wan-connector.xml"; - - try { - byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); - - ModelArtifactParser parser = new ModelArtifactParser(); - List modelList = parser.parse(xmlBytes, "test-artifact"); - - assertTrue(modelList.size() == 1); - - ModelArtifact model = (ModelArtifact) modelList.get(0); - System.out.println(model.toString()); - - assertTrue(model.getNameVersionId().equalsIgnoreCase("94cac189-8d88-4d63-a194-f44214e080ff")); - assertTrue(model.getType().toString().equalsIgnoreCase("NAMED_QUERY")); - assertTrue(model.getDependentModelIds().size() == 4); - assertTrue(model.getDependentModelIds().contains("d09dd9da-0148-46cd-a947-591afc844d24")); - assertTrue(model.getDependentModelIds().contains("997fc7-fca1-451f-b953-9a1e6197b4d6")); - assertTrue(model.getDependentModelIds().contains("897df7ea-8938-42b0-bc57-46e913a4d93b")); - assertTrue(model.getDependentModelIds().contains("f2b24d95-c582-48d5-b2d6-c5b3a94ce812")); - } catch (Exception e) { - e.printStackTrace(); - assertTrue(false); - } - } -} +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.model; + +import static org.junit.Assert.assertTrue; + +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; + +import org.junit.Test; +import org.openecomp.modelloader.entity.Artifact; + +public class ModelArtifactParserTest { + + @Test + public void testParseModelFileNoDeps() throws Exception { + final String MODEL_FILE = "src/test/resources/models/l3-network-widget.xml"; + + try { + byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); + + ModelArtifactParser parser = new ModelArtifactParser(); + List modelList = parser.parse(xmlBytes, "test-artifact"); + + assertTrue(modelList.size() == 1); + + ModelArtifact model = (ModelArtifact) modelList.get(0); + System.out.println(model.toString()); + + assertTrue(model.getModelInvariantId().equalsIgnoreCase("3d560d81-57d0-438b-a2a1-5334dba0651a")); + assertTrue(model.getType().toString().equalsIgnoreCase("MODEL")); + System.out.println(model.getDependentModelIds().size()); + assertTrue(model.getDependentModelIds().size() == 0); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + @Test + public void testParseModelFileDeps() throws Exception { + final String MODEL_FILE = "src/test/resources/models/AAI-stellService-service-1.xml"; + + try { + byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); + + ModelArtifactParser parser = new ModelArtifactParser(); + List modelList = parser.parse(xmlBytes, "test-artifact"); + + assertTrue(modelList.size() == 1); + + ModelArtifact model = (ModelArtifact) modelList.get(0); + System.out.println(model.toString()); + + assertTrue(model.getModelInvariantId().equalsIgnoreCase("fedf9da3-6a74-4813-8fa2-221a98b0e7ad")); + assertTrue(model.getModelVerId().equalsIgnoreCase("e0373537-7f66-4094-9939-e2f5de6ff5f6")); + assertTrue(model.getType().toString().equalsIgnoreCase("MODEL")); + assertTrue(model.getDependentModelIds().size() == 3); + assertTrue(model.getDependentModelIds().contains("5c12984d-db0f-4300-a0e0-9791775cc40f|88bdbadf-db8a-490f-881e-c8effcbc3f66")); + assertTrue(model.getDependentModelIds().contains("959b7c09-9f34-4e5f-8b63-505381db176e|374d0899-bbc2-4403-9320-fe9bebef75c6")); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + @Test + public void testParseCompleteModel() throws Exception { + final String MODEL_FILE = "src/test/resources/models/complete-model.xml"; + + try { + byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); + + ModelArtifactParser parser = new ModelArtifactParser(); + List modelList = parser.parse(xmlBytes, "test-artifact"); + + for (Artifact art : modelList) { + ModelArtifact model = (ModelArtifact) art; + System.out.println(model.toString()); + } + + assertTrue(modelList.size() == 5); + + ModelArtifact model1 = (ModelArtifact)modelList.get(0); + assertTrue(model1.getModelVerId().equalsIgnoreCase("88bdbadf-db8a-490f-881e-c8effcbc3f66")); + assertTrue(model1.getType().toString().equalsIgnoreCase("MODEL")); + assertTrue(model1.getDependentModelIds().size() == 1); + assertTrue(model1.getDependentModelIds().contains("3d560d81-57d0-438b-a2a1-5334dba0651a|9111f20f-e680-4001-b83f-19a2fc23bfc1")); + + ModelArtifact model4 = (ModelArtifact)modelList.get(4); + assertTrue(model4.getModelInvariantId().equalsIgnoreCase("fedf9da3-6a74-4813-8fa2-221a98b0e7ad")); + assertTrue(model4.getType().toString().equalsIgnoreCase("MODEL")); + assertTrue(model4.getDependentModelIds().size() == 3); + assertTrue(model4.getDependentModelIds().contains("5c12984d-db0f-4300-a0e0-9791775cc40f|88bdbadf-db8a-490f-881e-c8effcbc3f66")); + assertTrue(model4.getDependentModelIds().contains("82194af1-3c2c-485a-8f44-420e22a9eaa4|46b92144-923a-4d20-b85a-3cbd847668a9")); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + @Test + public void testParseNamedQuery() throws Exception { + final String MODEL_FILE = "src/test/resources/models/named-query-wan-connector.xml"; + + try { + byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); + + ModelArtifactParser parser = new ModelArtifactParser(); + List modelList = parser.parse(xmlBytes, "test-artifact"); + + assertTrue(modelList.size() == 1); + + ModelArtifact model = (ModelArtifact) modelList.get(0); + System.out.println(model.toString()); + + assertTrue(model.getNameVersionId().equalsIgnoreCase("94cac189-8d88-4d63-a194-f44214e080ff")); + assertTrue(model.getType().toString().equalsIgnoreCase("NAMED_QUERY")); + assertTrue(model.getDependentModelIds().size() == 4); + assertTrue(model.getDependentModelIds().contains("d09dd9da-0148-46cd-a947-591afc844d24")); + assertTrue(model.getDependentModelIds().contains("997fc7-fca1-451f-b953-9a1e6197b4d6")); + assertTrue(model.getDependentModelIds().contains("897df7ea-8938-42b0-bc57-46e913a4d93b")); + assertTrue(model.getDependentModelIds().contains("f2b24d95-c582-48d5-b2d6-c5b3a94ce812")); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + @Test + public void testParseModelFileInvalidArtifact() throws Exception { + final String MODEL_FILE = "src/test/resources/models/invalid-model.xml"; + + try { + byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); + + ModelArtifactParser parser = new ModelArtifactParser(); + List modelList = parser.parse(xmlBytes, "test-artifact"); + + assertTrue(modelList == null || modelList.isEmpty()); + } + catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + @Test + public void testParseModelFileIncompleteArtifact() throws Exception { + final String MODEL_FILE = "src/test/resources/models/incomplete-model.xml"; + + try { + byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); + + ModelArtifactParser parser = new ModelArtifactParser(); + List modelList = parser.parse(xmlBytes, "test-artifact"); + + assertTrue(modelList == null || modelList.isEmpty()); + } + catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + + @Test + public void testParseModelFileIncompleteArtifacts() throws Exception { + final String MODEL_FILE = "src/test/resources/models/incomplete-models.xml"; + + try { + byte[] xmlBytes = Files.readAllBytes(Paths.get(MODEL_FILE)); + + ModelArtifactParser parser = new ModelArtifactParser(); + List modelList = parser.parse(xmlBytes, "test-artifact"); + + assertTrue(modelList == null || modelList.isEmpty()); + } + catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } + +} diff --git a/src/test/java/org/openecomp/modelloader/entity/model/ModelSorterTest.java b/src/test/java/org/openecomp/modelloader/entity/model/ModelSorterTest.java index dea537f..6a9cf88 100644 --- a/src/test/java/org/openecomp/modelloader/entity/model/ModelSorterTest.java +++ b/src/test/java/org/openecomp/modelloader/entity/model/ModelSorterTest.java @@ -1,133 +1,138 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * 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.modelloader.entity.model; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; - -import org.junit.Test; -import org.openecomp.modelloader.entity.Artifact; - -public class ModelSorterTest { - - @Test - public void noModels() { - - List emptyList = Collections.emptyList(); - - ModelSorter sorter = new ModelSorter(); - sorter = new ModelSorter(); - - List sortedList = sorter.sort(emptyList); - assertNotNull(sortedList); - assertEquals(0, sortedList.size()); - - } - - @Test - public void singleModel() { - - List modelList = new ArrayList(); - - ModelArtifact model = new ModelArtifact(); - model.setNameVersionId("aaaaa"); - model.addDependentModelId("xyz"); - modelList.add(model); - - ModelSorter sorter = new ModelSorter(); - sorter = new ModelSorter(); - - List sortedList = sorter.sort(modelList); - assertNotNull(sortedList); - assertEquals(1, sortedList.size()); - - } - - /** - * - * depends on depends on B ------> A -------> C - * - * - * Input list = a, b, c Sorted list = c, a, b - * - */ - @Test - public void multipleModels() { - - List modelList = new ArrayList(); - - ModelArtifact aaaa = new ModelArtifact(); - aaaa.setNameVersionId("aaaa"); - aaaa.addDependentModelId("cccc"); - - ModelArtifact bbbb = new ModelArtifact(); - bbbb.setNameVersionId("bbbb"); - bbbb.addDependentModelId("aaaa"); - - ModelArtifact cccc = new ModelArtifact(); - cccc.setNameVersionId("cccc"); - - modelList.add(aaaa); - modelList.add(bbbb); - modelList.add(cccc); - - ModelSorter sorter = new ModelSorter(); - sorter = new ModelSorter(); - - List sortedList = sorter.sort(modelList); - assertNotNull(sortedList); - assertEquals(3, sortedList.size()); - - assertEquals(cccc, sortedList.get(0)); - assertEquals(aaaa, sortedList.get(1)); - assertEquals(bbbb, sortedList.get(2)); - } - - @Test(expected = RuntimeException.class) - public void circularDependency() { - - List modelList = new ArrayList(); - - ModelArtifact aaaa = new ModelArtifact(); - aaaa.setNameVersionId("aaaa"); - aaaa.addDependentModelId("bbbb"); - - ModelArtifact bbbb = new ModelArtifact(); - bbbb.setNameVersionId("bbbb"); - bbbb.addDependentModelId("aaaa"); - - modelList.add(aaaa); - modelList.add(bbbb); - - ModelSorter sorter = new ModelSorter(); - sorter = new ModelSorter(); - - List sortedList = sorter.sort(modelList); - assertNotNull(sortedList); - assertEquals(3, sortedList.size()); - - } - -} +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.entity.model; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import org.junit.Test; +import org.openecomp.modelloader.entity.Artifact; + +public class ModelSorterTest { + + @Test + public void noModels() { + + List emptyList = Collections.emptyList(); + + ModelSorter sorter = new ModelSorter(); + sorter = new ModelSorter(); + + List sortedList = sorter.sort(emptyList); + assertNotNull(sortedList); + assertEquals(0, sortedList.size()); + + } + + @Test + public void singleModel() { + + List modelList = new ArrayList(); + + ModelArtifact model = new ModelArtifact(); + model.setNameVersionId("aaaaa"); + model.addDependentModelId("xyz"); + modelList.add(model); + + ModelSorter sorter = new ModelSorter(); + sorter = new ModelSorter(); + + List sortedList = sorter.sort(modelList); + assertNotNull(sortedList); + assertEquals(1, sortedList.size()); + + } + + /** + * + * depends on depends on B ------> A -------> C + * + * + * Input list = a, b, c Sorted list = c, a, b + * + */ + @Test + public void multipleModels() { + + List modelList = new ArrayList(); + + ModelArtifact aaaa = new ModelArtifact(); + aaaa.setModelInvariantId("aaaa"); + aaaa.setModelVerId("mvaaaa"); + aaaa.addDependentModelId("cccc|mvcccc"); + + ModelArtifact bbbb = new ModelArtifact(); + bbbb.setModelInvariantId("bbbb"); + bbbb.setModelVerId("mvbbbb"); + bbbb.addDependentModelId("aaaa|mvaaaa"); + + ModelArtifact cccc = new ModelArtifact(); + cccc.setModelInvariantId("cccc"); + cccc.setModelVerId("mvcccc"); + + modelList.add(aaaa); + modelList.add(bbbb); + modelList.add(cccc); + + ModelSorter sorter = new ModelSorter(); + sorter = new ModelSorter(); + + List sortedList = sorter.sort(modelList); + assertNotNull(sortedList); + assertEquals(3, sortedList.size()); + + assertEquals(cccc, sortedList.get(0)); + assertEquals(aaaa, sortedList.get(1)); + assertEquals(bbbb, sortedList.get(2)); + } + + @Test(expected = RuntimeException.class) + public void circularDependency() { + + List modelList = new ArrayList(); + + ModelArtifact aaaa = new ModelArtifact(); + aaaa.setNameVersionId("aaaa"); + aaaa.addDependentModelId("bbbb"); + + ModelArtifact bbbb = new ModelArtifact(); + bbbb.setNameVersionId("bbbb"); + bbbb.addDependentModelId("aaaa"); + + modelList.add(aaaa); + modelList.add(bbbb); + + ModelSorter sorter = new ModelSorter(); + sorter = new ModelSorter(); + + List sortedList = sorter.sort(modelList); + assertNotNull(sortedList); + assertEquals(3, sortedList.size()); + + } + +} diff --git a/src/test/java/org/openecomp/modelloader/restclient/AAIRestClientTest.java b/src/test/java/org/openecomp/modelloader/restclient/AAIRestClientTest.java deleted file mode 100644 index 08d7afe..0000000 --- a/src/test/java/org/openecomp/modelloader/restclient/AAIRestClientTest.java +++ /dev/null @@ -1,119 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * 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.modelloader.restclient; - -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Paths; - -import org.openecomp.modelloader.config.ModelLoaderConfig; -import org.openecomp.modelloader.entity.ArtifactType; -import org.openecomp.modelloader.entity.model.ModelArtifact; - -public class AAIRestClientTest { - - // This test requires a running A&AI system. Uncomment to test locally. - /* - * @Test public void testRestClient() throws Exception { final String - * MODEL_FILE = "src/test/resources/models/vnf-model.xml"; - * - * Properties props = new Properties(); - * props.setProperty("ml.distribution.ARTIFACT_TYPES", - * "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG"); - * props.setProperty("ml.aai.BASE_URL", "https://127.0.0.1:4321"); - * props.setProperty("ml.aai.MODEL_URL", - * "/aai/v8/service-design-and-creation/models/model/"); - * props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12"); - * props.setProperty("ml.aai.KEYSTORE_PASSWORD", - * "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o"); - * - * ModelLoaderConfig config = new ModelLoaderConfig(props, ""); - * - * String payload = readFile(MODEL_FILE); System.out.println("FILE:" + - * payload); - * - * File xmlFile = new File(MODEL_FILE); DocumentBuilderFactory dbFactory = - * DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = - * dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); - * - * // Get the ID of the model String modelId = null; NodeList nodeList = - * doc.getDocumentElement().getChildNodes(); for (int i = 0; i < - * nodeList.getLength(); i++) { Node currentNode = nodeList.item(i); if - * (currentNode.getNodeName().equals("model-name-version-id")) { modelId = - * currentNode.getTextContent(); break; } } - * - * // Add the model try { ModelArtifact model = new ModelArtifact(); - * model.setNameVersionId(modelId); model.setType(ArtifactType.MODEL); - * model.setPayload(payload); - * - * AAIRestClient aaiClient = new AAIRestClient(config); - * - * // GET model System.out.println("Calling GET API ..."); ClientResponse - * getResponse = aaiClient.getResource(getURL(model, config), - * "example-trans-id-0", AAIRestClient.MimeType.XML); System.out.println( - * "GET result: " + getResponse.getStatus()); - * assertTrue(getResponse.getStatus() == - * Response.Status.NOT_FOUND.getStatusCode()); - * - * // Add the model System.out.println("Calling PUT API ..."); ClientResponse - * res = aaiClient.putResource(getURL(model, config), model.getPayload(), - * "example-trans-id-1", AAIRestClient.MimeType.XML); System.out.println( - * "PUT result: " + res.getStatus()); assertTrue(res.getStatus() == - * Response.Status.CREATED.getStatusCode()); - * - * // Delete the model System.out.println("Calling DELETE API ..."); res = - * aaiClient.getAndDeleteResource(getURL(model, config), - * "example-trans-id-3"); System.out.println("DELETE result: " + - * res.getStatus()); assertTrue(res.getStatus() == - * Response.Status.NO_CONTENT.getStatusCode()); } catch (Exception e) { - * e.printStackTrace(); } } - */ - - static String readFile(String path) throws IOException { - byte[] encoded = Files.readAllBytes(Paths.get(path)); - return new String(encoded); - } - - private String getURL(ModelArtifact model, ModelLoaderConfig config) { - String baseURL = config.getAaiBaseUrl().trim(); - String subURL = null; - if (model.getType().equals(ArtifactType.MODEL)) { - subURL = config.getAaiModelUrl().trim(); - } else { - subURL = config.getAaiNamedQueryUrl().trim(); - } - - if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) { - baseURL = baseURL + "/"; - } - - if (baseURL.endsWith("/") && subURL.startsWith("/")) { - baseURL = baseURL.substring(0, baseURL.length() - 1); - } - - if (!subURL.endsWith("/")) { - subURL = subURL + "/"; - } - - String url = baseURL + subURL + model.getNameVersionId(); - return url; - } -} diff --git a/src/test/java/org/openecomp/modelloader/restclient/AaiRestClientTest.java b/src/test/java/org/openecomp/modelloader/restclient/AaiRestClientTest.java new file mode 100644 index 0000000..dc6f9d6 --- /dev/null +++ b/src/test/java/org/openecomp/modelloader/restclient/AaiRestClientTest.java @@ -0,0 +1,121 @@ +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.restclient; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +import org.openecomp.modelloader.config.ModelLoaderConfig; +import org.openecomp.modelloader.entity.ArtifactType; +import org.openecomp.modelloader.entity.model.ModelArtifact; + +public class AaiRestClientTest { + + // This test requires a running A&AI system. Uncomment to test locally. + /* + * @Test public void testRestClient() throws Exception { final String + * MODEL_FILE = "src/test/resources/models/vnf-model.xml"; + * + * Properties props = new Properties(); + * props.setProperty("ml.distribution.ARTIFACT_TYPES", + * "MODEL_INVENTORY_PROFILE,MODEL_QUERY_SPEC,VNF_CATALOG"); + * props.setProperty("ml.aai.BASE_URL", "https://127.0.0.1:4321"); + * props.setProperty("ml.aai.MODEL_URL", + * "/aai/v8/service-design-and-creation/models/model/"); + * props.setProperty("ml.aai.KEYSTORE_FILE", "aai-client-cert.p12"); + * props.setProperty("ml.aai.KEYSTORE_PASSWORD", + * "OBF:1i9a1u2a1unz1lr61wn51wn11lss1unz1u301i6o"); + * + * ModelLoaderConfig config = new ModelLoaderConfig(props, ""); + * + * String payload = readFile(MODEL_FILE); System.out.println("FILE:" + + * payload); + * + * File xmlFile = new File(MODEL_FILE); DocumentBuilderFactory dbFactory = + * DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = + * dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(xmlFile); + * + * // Get the ID of the model String modelId = null; NodeList nodeList = + * doc.getDocumentElement().getChildNodes(); for (int i = 0; i < + * nodeList.getLength(); i++) { Node currentNode = nodeList.item(i); if + * (currentNode.getNodeName().equals("model-name-version-id")) { modelId = + * currentNode.getTextContent(); break; } } + * + * // Add the model try { ModelArtifact model = new ModelArtifact(); + * model.setNameVersionId(modelId); model.setType(ArtifactType.MODEL); + * model.setPayload(payload); + * + * AAIRestClient aaiClient = new AAIRestClient(config); + * + * // GET model System.out.println("Calling GET API ..."); ClientResponse + * getResponse = aaiClient.getResource(getURL(model, config), + * "example-trans-id-0", AAIRestClient.MimeType.XML); System.out.println( + * "GET result: " + getResponse.getStatus()); + * assertTrue(getResponse.getStatus() == + * Response.Status.NOT_FOUND.getStatusCode()); + * + * // Add the model System.out.println("Calling PUT API ..."); ClientResponse + * res = aaiClient.putResource(getURL(model, config), model.getPayload(), + * "example-trans-id-1", AAIRestClient.MimeType.XML); System.out.println( + * "PUT result: " + res.getStatus()); assertTrue(res.getStatus() == + * Response.Status.CREATED.getStatusCode()); + * + * // Delete the model System.out.println("Calling DELETE API ..."); res = + * aaiClient.getAndDeleteResource(getURL(model, config), + * "example-trans-id-3"); System.out.println("DELETE result: " + + * res.getStatus()); assertTrue(res.getStatus() == + * Response.Status.NO_CONTENT.getStatusCode()); } catch (Exception e) { + * e.printStackTrace(); } } + */ + + static String readFile(String path) throws IOException { + byte[] encoded = Files.readAllBytes(Paths.get(path)); + return new String(encoded); + } + + private String getURL(ModelArtifact model, ModelLoaderConfig config) { + String baseURL = config.getAaiBaseUrl().trim(); + String subURL = null; + if (model.getType().equals(ArtifactType.MODEL)) { + subURL = config.getAaiModelUrl().trim(); + } else { + subURL = config.getAaiNamedQueryUrl().trim(); + } + + if ((!baseURL.endsWith("/")) && (!subURL.startsWith("/"))) { + baseURL = baseURL + "/"; + } + + if (baseURL.endsWith("/") && subURL.startsWith("/")) { + baseURL = baseURL.substring(0, baseURL.length() - 1); + } + + if (!subURL.endsWith("/")) { + subURL = subURL + "/"; + } + + String url = baseURL + subURL + model.getNameVersionId(); + return url; + } +} diff --git a/src/test/java/org/openecomp/modelloader/service/ModelLoaderServiceTest.java b/src/test/java/org/openecomp/modelloader/service/ModelLoaderServiceTest.java deleted file mode 100644 index c2893a9..0000000 --- a/src/test/java/org/openecomp/modelloader/service/ModelLoaderServiceTest.java +++ /dev/null @@ -1,115 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * 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.modelloader.service; - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Matchers; -import org.openecomp.modelloader.config.ModelLoaderConfig; -import org.openecomp.modelloader.notification.EventCallback; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; - -import org.openecomp.sdc.api.IDistributionClient; -import org.openecomp.sdc.api.results.IDistributionClientResult; -import org.openecomp.sdc.impl.DistributionClientFactory; -import org.openecomp.sdc.utils.DistributionActionResultEnum; - -@PrepareForTest({ DistributionClientFactory.class }) -@RunWith(PowerMockRunner.class) -public class ModelLoaderServiceTest { - - /* - * //TODO this should be re-added once we come up with a strategy to fail - * gracefully - * - * @Test public void testNonExistentConfiguration(){ - * ModelLoaderService.CONFIG_LOCATION = "FAKELOCATION"; - * - * try{ new ModelLoaderService().start(); }catch(RuntimeException e){ - * assertTrue("Got unexpected message from error log", - * e.getMessage().contains("Failed to load configuration")); return; } - * - * fail("Expecting runtime exception"); } - */ - - @Test - public void testConfigureStartDistributionClient() { - PowerMockito.mockStatic(DistributionClientFactory.class); - - IDistributionClient mockClient = mock(IDistributionClient.class); - ModelLoaderConfig mockConfig = mock(ModelLoaderConfig.class); - - when(DistributionClientFactory.createDistributionClient()).thenReturn(mockClient); - - IDistributionClientResult result = mock(IDistributionClientResult.class); - - when(result.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS); - when(mockClient.init(Matchers. any(), Matchers. any())) - .thenReturn(result); - when(mockClient.start()).thenReturn(result); - - new ModelLoaderService().init(); - - // Validate that the client was initialized and started - verify(mockClient, times(1)).init(Matchers. any(), - Matchers. any()); - verify(mockClient, times(1)).start(); - } - - @Test - public void testInitializeButNotStarted() { - PowerMockito.mockStatic(DistributionClientFactory.class); - - IDistributionClient mockClient = mock(IDistributionClient.class); - ModelLoaderConfig mockConfig = mock(ModelLoaderConfig.class); - - DistributionActionResultEnum failureReason = DistributionActionResultEnum.ASDC_CONNECTION_FAILED; - - when(DistributionClientFactory.createDistributionClient()).thenReturn(mockClient); - - IDistributionClientResult initResult = mock(IDistributionClientResult.class); - when(initResult.getDistributionActionResult()).thenReturn(DistributionActionResultEnum.SUCCESS); - IDistributionClientResult startResult = mock(IDistributionClientResult.class); - when(startResult.getDistributionActionResult()).thenReturn(failureReason); - - when(mockClient.init(Matchers. any(), Matchers. any())) - .thenReturn(initResult); - when(mockClient.start()).thenReturn(startResult); - - // TODO this should be re-added once we come up with a strategy to fail - // gracefully - /* - * try{ new ModelLoaderService().init(mockConfig); }catch(RuntimeException - * e){ assertTrue(e.getMessage().contains(failureReason.toString())); - * return; } - * - * fail("Expecting runtime exception with failure: " + - * failureReason.toString()); - */ - } -} diff --git a/src/test/java/org/openecomp/modelloader/util/JsonXmlConverterTest.java b/src/test/java/org/openecomp/modelloader/util/JsonXmlConverterTest.java index 4654c93..da89c78 100644 --- a/src/test/java/org/openecomp/modelloader/util/JsonXmlConverterTest.java +++ b/src/test/java/org/openecomp/modelloader/util/JsonXmlConverterTest.java @@ -1,79 +1,81 @@ -/*- - * ============LICENSE_START======================================================= - * MODEL LOADER SERVICE - * ================================================================================ - * 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.modelloader.util; - -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.ByteArrayInputStream; -import java.nio.file.Files; -import java.nio.file.Paths; - -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; - -import org.junit.Test; -import org.w3c.dom.Document; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -public class JsonXmlConverterTest { - - @Test - public void testConversion() throws Exception { - final String XML_MODEL_FILE = "src/test/resources/models/vnf-model.xml"; - final String JSON_MODEL_FILE = "src/test/resources/models/vnf-model.json"; - - try { - byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE)); - String originalXML = new String(encoded); - - assertFalse(JsonXmlConverter.isValidJson(originalXML)); - - encoded = Files.readAllBytes(Paths.get(JSON_MODEL_FILE)); - String originalJSON = new String(encoded); - - assertTrue(JsonXmlConverter.isValidJson(originalJSON)); - - String xmlFromJson = JsonXmlConverter.convertJsonToXml(originalJSON); - - // Spot check one of the attributes - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - DocumentBuilder builder = factory.newDocumentBuilder(); - Document doc = builder.parse(new ByteArrayInputStream(xmlFromJson.getBytes())); - NodeList nodeList = doc.getDocumentElement().getChildNodes(); - - String modelVid = "notFound"; - for (int i = 0; i < nodeList.getLength(); i++) { - Node currentNode = nodeList.item(i); - if (currentNode.getNodeName().equals("model-name-version-id")) { - modelVid = currentNode.getTextContent(); - break; - } - } - - assertTrue(modelVid.equals("model-vid")); - } catch (Exception e) { - e.printStackTrace(); - assertTrue(false); - } - } -} +/** + * ============LICENSE_START======================================================= + * Model Loader + * ================================================================================ + * Copyright © 2017 AT&T Intellectual Property. + * Copyright © 2017 Amdocs + * 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========================================================= + * + * ECOMP and OpenECOMP are trademarks + * and service marks of AT&T Intellectual Property. + */ +package org.openecomp.modelloader.util; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.io.ByteArrayInputStream; +import java.nio.file.Files; +import java.nio.file.Paths; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; + +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; + +public class JsonXmlConverterTest { + + @Test + public void testConversion() throws Exception { + final String XML_MODEL_FILE = "src/test/resources/models/l3-network-widget.xml"; + final String JSON_MODEL_FILE = "src/test/resources/models/l3-network-widget.json"; + + try { + byte[] encoded = Files.readAllBytes(Paths.get(XML_MODEL_FILE)); + String originalXML = new String(encoded); + + assertFalse(JsonXmlConverter.isValidJson(originalXML)); + + encoded = Files.readAllBytes(Paths.get(JSON_MODEL_FILE)); + String originalJSON = new String(encoded); + + assertTrue(JsonXmlConverter.isValidJson(originalJSON)); + + String xmlFromJson = JsonXmlConverter.convertJsonToXml(originalJSON); + + // Spot check one of the attributes + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + DocumentBuilder builder = factory.newDocumentBuilder(); + Document doc = builder.parse(new ByteArrayInputStream(xmlFromJson.getBytes())); + NodeList nodeList = doc.getDocumentElement().getChildNodes(); + + String modelVid = "notFound"; + for (int i = 0; i < nodeList.getLength(); i++) { + Node currentNode = nodeList.item(i); + if (currentNode.getNodeName().equals("model-invariant-id")) { + modelVid = currentNode.getTextContent(); + break; + } + } + + assertTrue(modelVid.equals("3d560d81-57d0-438b-a2a1-5334dba0651a")); + } catch (Exception e) { + e.printStackTrace(); + assertTrue(false); + } + } +} -- cgit 1.2.3-korg