diff options
Diffstat (limited to 'src/test/java')
6 files changed, 1320 insertions, 0 deletions
diff --git a/src/test/java/org/openecomp/sdc/impl/DistributionClientTest.java b/src/test/java/org/openecomp/sdc/impl/DistributionClientTest.java new file mode 100644 index 0000000..9e6461d --- /dev/null +++ b/src/test/java/org/openecomp/sdc/impl/DistributionClientTest.java @@ -0,0 +1,546 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import java.io.IOException; +import java.net.MalformedURLException; +import java.security.GeneralSecurityException; +import java.util.ArrayList; +import java.util.List; + +import org.junit.After; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.mockito.Mockito; +import org.openecomp.sdc.api.IDistributionClient; +import org.openecomp.sdc.api.consumer.IConfiguration; +import org.openecomp.sdc.api.notification.IArtifactInfo; +import org.openecomp.sdc.api.results.IDistributionClientResult; +import org.openecomp.sdc.http.AsdcConnectorClient; +import org.openecomp.sdc.http.TopicRegistrationResponse; +import org.openecomp.sdc.impl.ArtifactInfoImpl; +import org.openecomp.sdc.impl.DistributionClientFactory; +import org.openecomp.sdc.impl.DistributionClientImpl; +import org.openecomp.sdc.impl.DistributionClientResultImpl; +import org.openecomp.sdc.utils.ArtifactTypeEnum; +import org.openecomp.sdc.utils.ArtifactsUtils; +import org.openecomp.sdc.utils.DistributionActionResultEnum; +import org.openecomp.sdc.utils.TestConfiguration; +import org.openecomp.sdc.utils.TestNotificationCallback; +import org.openecomp.sdc.utils.Wrapper; + +import com.att.nsa.apiClient.credentials.ApiCredential; +import com.att.nsa.apiClient.http.HttpException; +import com.att.nsa.cambria.client.CambriaClient.CambriaApiException; +import com.att.nsa.cambria.client.CambriaClientBuilders; +import com.att.nsa.cambria.client.CambriaIdentityManager; +import com.att.nsa.cambria.client.CambriaTopicManager; + +import fj.data.Either; + +public class DistributionClientTest { + + static CambriaIdentityManager cc; + static List<String> serverList; + DistributionClientImpl client = new DistributionClientImpl(); + IConfiguration testConfiguration = new TestConfiguration(); + AsdcConnectorClient connector = Mockito.mock(AsdcConnectorClient.class); + + @BeforeClass + public static void setup() { + serverList = new ArrayList<String>(); + serverList.add("uebsb91sfdc.it.att.com:3904"); + serverList.add("uebsb92sfdc.it.att.com:3904"); + serverList.add("uebsb93sfdc.it.att.com:3904"); + + } + + @After + public void afterTest() { + client.stop(); + } + + @Test + public void validateConfigurationTest() { + DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(new Wrapper<IDistributionClientResult>(), testConfiguration); + Assert.assertEquals(DistributionActionResultEnum.SUCCESS, validationResult); + Assert.assertEquals(testConfiguration.getPollingInterval(), client.configuration.getPollingInterval()); + Assert.assertEquals(testConfiguration.getPollingTimeout(), client.configuration.getPollingTimeout()); + } + + @Test + public void validateConfigurationToDefaultTest() { + TestConfiguration userConfig = new TestConfiguration(); + userConfig.setPollingInterval(1); + userConfig.setPollingTimeout(2); + DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(new Wrapper<IDistributionClientResult>(), userConfig); + Assert.assertEquals(DistributionActionResultEnum.SUCCESS, validationResult); + Assert.assertEquals(15, client.configuration.getPollingInterval()); + Assert.assertEquals(15, client.configuration.getPollingTimeout()); + } + + @Test + public void validateConfigurationFqdnTest() { + + String[] validFqdns = { "myHostname", "myHostname:80", "myHostname:8080", "172.20.43.118", "172.20.43.118:8080", "ueb01hydc.it.att.com", "ueb01hydc.it.att.com:8080", "ueb01hydc.it", "my-good.and-simple.fqdn" }; + + String[] invalidFqdns = { "myHostname:808080", /* 70 letters */"abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij", "not**good", "very#not#good#" }; + + boolean validationResult = true; + + for (int i = 0; i < validFqdns.length; i++) { + validationResult = client.isValidFqdn(validFqdns[i]); + assertEquals("assertion failed for FQDN " + validFqdns[i] + " expected to be valid, actual invalid", true, validationResult); + } + + for (int i = 0; i < invalidFqdns.length; i++) { + validationResult = client.isValidFqdn(invalidFqdns[i]); + assertEquals("assertion failed for FQDN " + invalidFqdns[i] + " expected to be invalid, actual valid", false, validationResult); + } + + } + + @Test + public void validateConfigurationPasswordTest() { + Wrapper<IDistributionClientResult> errorWrapper = new Wrapper<>(); + TestConfiguration testPassword = new TestConfiguration(); + testPassword.setPassword(null); + DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(errorWrapper, testPassword); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult); + + testPassword.setPassword(""); + validationResult = client.validateAndInitConfiguration(errorWrapper, testPassword); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult); + + } + + @Test + public void validateConfigurationUserTest() { + Wrapper<IDistributionClientResult> errorWrapper = new Wrapper<>(); + TestConfiguration testUser = new TestConfiguration(); + testUser.setUser(null); + DistributionActionResultEnum validationResult = client.validateAndInitConfiguration(errorWrapper, testUser); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult); + + testUser.setUser(""); + validationResult = client.validateAndInitConfiguration(errorWrapper, testUser); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult); + + } + + @Test + public void initWithMocksBadConfigurationTest() throws HttpException, CambriaApiException, IOException { + + // connectorMock + Either<List<String>, IDistributionClientResult> serversResult = Either.left(serverList); + Mockito.when(connector.getServerList()).thenReturn(serversResult); + + TopicRegistrationResponse topics = new TopicRegistrationResponse(); + topics.setDistrNotificationTopicName("notificationTopic"); + topics.setDistrStatusTopicName("statusTopic"); + Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.left(topics); + Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); + + client.asdcConnector = connector; + + // cambriaMock + + CambriaIdentityManager cambriaMock = Mockito.mock(CambriaIdentityManager.class); + Mockito.when(cambriaMock.createApiKey(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(new ApiCredential("public", "secret")); + client.cambriaIdentityManager = cambriaMock; + + // no password + TestConfiguration testPassword = new TestConfiguration(); + testPassword.setPassword(null); + IDistributionClientResult validationResult = client.init(testPassword, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult.getDistributionActionResult()); + + testPassword.setPassword(""); + validationResult = client.init(testPassword, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_PASSWORD, validationResult.getDistributionActionResult()); + + // no username + TestConfiguration testUser = new TestConfiguration(); + testUser.setUser(null); + validationResult = client.init(testUser, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult.getDistributionActionResult()); + + testUser.setUser(""); + validationResult = client.init(testUser, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_USERNAME, validationResult.getDistributionActionResult()); + + // no ASDC server fqdn + TestConfiguration testServerFqdn = new TestConfiguration(); + testServerFqdn.setAsdcAddress(null); + validationResult = client.init(testServerFqdn, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_ASDC_FQDN, validationResult.getDistributionActionResult()); + + testServerFqdn.setAsdcAddress(""); + validationResult = client.init(testServerFqdn, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_ASDC_FQDN, validationResult.getDistributionActionResult()); + + testServerFqdn.setAsdcAddress("this##is##bad##fqdn"); + validationResult = client.init(testServerFqdn, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_INVALID_ASDC_FQDN, validationResult.getDistributionActionResult()); + + // no consumerId + TestConfiguration testConsumerId = new TestConfiguration(); + testConsumerId.setComsumerID(null); + validationResult = client.init(testConsumerId, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_CONSUMER_ID, validationResult.getDistributionActionResult()); + + testConsumerId.setComsumerID(""); + validationResult = client.init(testConsumerId, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_CONSUMER_ID, validationResult.getDistributionActionResult()); + + // no environmentName + TestConfiguration testEnv = new TestConfiguration(); + testEnv.setEnvironmentName(null); + validationResult = client.init(testEnv, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_ENVIRONMENT_NAME, validationResult.getDistributionActionResult()); + + testEnv.setEnvironmentName(""); + validationResult = client.init(testEnv, new TestNotificationCallback()); + Assert.assertEquals(DistributionActionResultEnum.CONF_MISSING_ENVIRONMENT_NAME, validationResult.getDistributionActionResult()); + + Mockito.verify(connector, Mockito.times(0)).getServerList(); + Mockito.verify(cambriaMock, Mockito.times(0)).createApiKey(Mockito.anyString(), Mockito.anyString()); + Mockito.verify(connector, Mockito.times(0)).registerAsdcTopics(Mockito.any(ApiCredential.class)); + } + + @Test + public void initFailedConnectAsdcTest() throws HttpException, CambriaApiException, IOException { + // cambriaMock + + CambriaIdentityManager cambriaMock = Mockito.mock(CambriaIdentityManager.class); + Mockito.when(cambriaMock.createApiKey(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(new ApiCredential("public", "secret")); + client.cambriaIdentityManager = cambriaMock; + + TestConfiguration badAsdcConfig = new TestConfiguration(); + badAsdcConfig.setAsdcAddress("badhost:8080"); + + IDistributionClientResult init = client.init(badAsdcConfig, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, init.getDistributionActionResult()); + + badAsdcConfig = new TestConfiguration(); + badAsdcConfig.setAsdcAddress("localhost:8181"); + + init = client.init(badAsdcConfig, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.ASDC_CONNECTION_FAILED, init.getDistributionActionResult()); + + } + + @Test + public void getConfigurationTest() throws HttpException, CambriaApiException, IOException { + // connectorMock + Either<List<String>, IDistributionClientResult> serversResult = Either.left(serverList); + Mockito.when(connector.getServerList()).thenReturn(serversResult); + mockArtifactTypeList(); + TopicRegistrationResponse topics = new TopicRegistrationResponse(); + topics.setDistrNotificationTopicName("notificationTopic"); + topics.setDistrStatusTopicName("statusTopic"); + Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.left(topics); + Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); + IDistributionClientResult success = initSuccesResult(); + Mockito.when(connector.unregisterTopics(Mockito.any(ApiCredential.class))).thenReturn(success); + + client.asdcConnector = connector; + + // cambriaMock + + CambriaIdentityManager cambriaMock = Mockito.mock(CambriaIdentityManager.class); + Mockito.when(cambriaMock.createApiKey(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(new ApiCredential("public", "secret")); + client.cambriaIdentityManager = cambriaMock; + + TestConfiguration badAsdcConfig = new TestConfiguration(); + badAsdcConfig.setPollingInterval(-5); + + IDistributionClientResult init = client.init(badAsdcConfig, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.SUCCESS, init.getDistributionActionResult()); + + String confString = client.getConfiguration().toString(); + System.out.println(confString); + + } + + private IDistributionClientResult initSuccesResult() { + return new IDistributionClientResult() { + + @Override + public String getDistributionMessageResult() { + return "success"; + } + + @Override + public DistributionActionResultEnum getDistributionActionResult() { + return DistributionActionResultEnum.SUCCESS; + } + }; + } + + @Test + public void initWithMocksTest() throws HttpException, CambriaApiException, IOException { + + // connectorMock + Either<List<String>, IDistributionClientResult> serversResult = Either.left(serverList); + Mockito.when(connector.getServerList()).thenReturn(serversResult); + mockArtifactTypeList(); + + TopicRegistrationResponse topics = new TopicRegistrationResponse(); + topics.setDistrNotificationTopicName("notificationTopic"); + topics.setDistrStatusTopicName("statusTopic"); + Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.left(topics); + Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); + IDistributionClientResult success = initSuccesResult(); + Mockito.when(connector.unregisterTopics(Mockito.any(ApiCredential.class))).thenReturn(success); + + client.asdcConnector = connector; + + // cambriaMock + + CambriaIdentityManager cambriaMock = Mockito.mock(CambriaIdentityManager.class); + Mockito.when(cambriaMock.createApiKey(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(new ApiCredential("public", "secret")); + client.cambriaIdentityManager = cambriaMock; + + IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.SUCCESS, initResponse.getDistributionActionResult()); + Mockito.verify(connector, Mockito.times(1)).getServerList(); + Mockito.verify(cambriaMock, Mockito.times(1)).createApiKey(Mockito.anyString(), Mockito.anyString()); + Mockito.verify(connector, Mockito.times(1)).registerAsdcTopics(Mockito.any(ApiCredential.class)); + System.out.println(initResponse); + } + + private void mockArtifactTypeList() { + List<String> artifactTypes = new ArrayList<>(); + for (ArtifactTypeEnum artifactType : ArtifactTypeEnum.values()) { + artifactTypes.add(artifactType.name()); + } + + final Either<List<String>, IDistributionClientResult> eitherArtifactTypes = Either.left(artifactTypes); + Mockito.when(connector.getValidArtifactTypesList()).thenReturn(eitherArtifactTypes); + } + + @Test + public void testAlreadyInitTest() throws HttpException, CambriaApiException, IOException { + initWithMocksTest(); + IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.DISTRIBUTION_CLIENT_ALREADY_INITIALIZED, initResponse.getDistributionActionResult()); + } + + @Test + public void initGetServerFailedTest() throws HttpException, CambriaApiException, IOException { + + // connectorMock + IDistributionClientResult getServersResult = new DistributionClientResultImpl(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, "problem"); + Either<List<String>, IDistributionClientResult> serversResult = Either.right(getServersResult); + Mockito.when(connector.getServerList()).thenReturn(serversResult); + + TopicRegistrationResponse topics = new TopicRegistrationResponse(); + topics.setDistrNotificationTopicName("notificationTopic"); + topics.setDistrStatusTopicName("statusTopic"); + Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.left(topics); + Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); + + client.asdcConnector = connector; + + // cambriaMock + + CambriaIdentityManager cambriaMock = Mockito.mock(CambriaIdentityManager.class); + Mockito.when(cambriaMock.createApiKey(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(new ApiCredential("public", "secret")); + client.cambriaIdentityManager = cambriaMock; + + IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.ASDC_SERVER_PROBLEM, initResponse.getDistributionActionResult()); + + Mockito.verify(connector, Mockito.times(1)).getServerList(); + Mockito.verify(cambriaMock, Mockito.times(0)).createApiKey(Mockito.anyString(), Mockito.anyString()); + Mockito.verify(connector, Mockito.times(0)).registerAsdcTopics(Mockito.any(ApiCredential.class)); + + System.out.println(initResponse); + } + + @Test + public void initCreateKeysFailedTest() throws HttpException, CambriaApiException, IOException { + + // connectorMock + Either<List<String>, IDistributionClientResult> serversResult = Either.left(serverList); + Mockito.when(connector.getServerList()).thenReturn(serversResult); + mockArtifactTypeList(); + + TopicRegistrationResponse topics = new TopicRegistrationResponse(); + topics.setDistrNotificationTopicName("notificationTopic"); + topics.setDistrStatusTopicName("statusTopic"); + Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.left(topics); + Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); + + client.asdcConnector = connector; + + // cambriaMock + + CambriaIdentityManager cambriaMock = Mockito.mock(CambriaIdentityManager.class); + Mockito.when(cambriaMock.createApiKey(Mockito.any(String.class), Mockito.any(String.class))).thenThrow(new CambriaApiException("failure")); + client.cambriaIdentityManager = cambriaMock; + + IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.UEB_KEYS_CREATION_FAILED, initResponse.getDistributionActionResult()); + + Mockito.verify(connector, Mockito.times(1)).getServerList(); + Mockito.verify(cambriaMock, Mockito.times(1)).createApiKey(Mockito.anyString(), Mockito.anyString()); + Mockito.verify(connector, Mockito.times(0)).registerAsdcTopics(Mockito.any(ApiCredential.class)); + System.out.println(initResponse); + } + + @Test + public void initRegistrationFailedTest() throws HttpException, CambriaApiException, IOException { + + // connectorMock + Either<List<String>, IDistributionClientResult> serversResult = Either.left(serverList); + Mockito.when(connector.getServerList()).thenReturn(serversResult); + mockArtifactTypeList(); + DistributionClientResultImpl failureResult = new DistributionClientResultImpl(DistributionActionResultEnum.BAD_REQUEST, "Bad Request"); + Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsResult = Either.right(failureResult); + Mockito.when(connector.registerAsdcTopics(Mockito.any(ApiCredential.class))).thenReturn(topicsResult); + + client.asdcConnector = connector; + + // cambriaMock + + CambriaIdentityManager cambriaMock = Mockito.mock(CambriaIdentityManager.class); + Mockito.when(cambriaMock.createApiKey(Mockito.any(String.class), Mockito.any(String.class))).thenReturn(new ApiCredential("public", "secret")); + client.cambriaIdentityManager = cambriaMock; + + IDistributionClientResult initResponse = client.init(testConfiguration, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.BAD_REQUEST, initResponse.getDistributionActionResult()); + Mockito.verify(connector, Mockito.times(1)).getServerList(); + Mockito.verify(cambriaMock, Mockito.times(1)).createApiKey(Mockito.anyString(), Mockito.anyString()); + Mockito.verify(connector, Mockito.times(1)).registerAsdcTopics(Mockito.any(ApiCredential.class)); + System.out.println(initResponse); + } + + @Test + public void testStartWithoutInit() { + IDistributionClientResult result = client.start(); + assertTrue(result.getDistributionActionResult() == DistributionActionResultEnum.DISTRIBUTION_CLIENT_NOT_INITIALIZED); + } + + private IArtifactInfo initArtifactInfo() { + ArtifactInfoImpl artifactInfo = new ArtifactInfoImpl(); + artifactInfo.setArtifactURL("/asdc/v1/services/serviceName/0.1/artifacts/aaa.hh"); + artifactInfo.setArtifactChecksum(ArtifactsUtils.getValidChecksum()); + return artifactInfo; + } + + // ########### TESTS TO ADD TO CI START ########### + public void createKeysTestCI() throws MalformedURLException, GeneralSecurityException { + validateConfigurationTest(); + CambriaIdentityManager trueCambria = new CambriaClientBuilders.IdentityManagerBuilder().usingHosts(serverList).build(); + client.cambriaIdentityManager = trueCambria; + DistributionClientResultImpl keysResult = client.createUebKeys(); + Assert.assertEquals(DistributionActionResultEnum.SUCCESS, keysResult.getDistributionActionResult()); + Assert.assertFalse(client.credential.getApiKey().isEmpty()); + Assert.assertFalse(client.credential.getApiSecret().isEmpty()); + + System.out.println(keysResult); + System.out.println("keys: public=" + client.credential.getApiKey() + " | secret=" + client.credential.getApiSecret()); + } + + public void initTestCI() { + IDistributionClient distributionClient = DistributionClientFactory.createDistributionClient(); + IDistributionClientResult init = distributionClient.init(testConfiguration, new TestNotificationCallback()); + assertEquals(DistributionActionResultEnum.SUCCESS, init.getDistributionActionResult()); + + } + + // @Test + public void registerProducerCI() { + + try { + CambriaTopicManager topicManager = new CambriaClientBuilders.TopicManagerBuilder().usingHosts(serverList).authenticatedBy("sSJc5qiBnKy2qrlc", "4ZRPzNJfEUK0sSNBvccd2m7X").build(); + topicManager.allowProducer("ASDC-DISTR-STATUS-TOPIC-TESTER", "1FSVAA3bRjhSKNAI"); + } catch (HttpException | IOException | GeneralSecurityException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + // publish + // StringBuilder sb = new StringBuilder(); + // for (String s : serverList) + // { + // sb.append(s); + // sb.append(","); + // } + // CambriaBatchingPublisher pub = CambriaClientFactory.createSimplePublisher(sb.toString(), "ASDC-DISTR-STATUS-TOPIC-TESTER"); + // pub.setApiCredentials("yPMwjhmOgHUyJEeW", "3RYpgvBsjpA8Y2CHdA1PM8xK" ); + // + // + // try { + // pub.send("MyPartitionKey", "{\"artifactURL\":\"artifactURL_Val\", \"consumerID\" : \"123\", \"distributionID\" : \"AAA\", \"status\" : \"DOWNLOAD_OK\", \"timestamp\" : 1000}"); + // } catch (IOException e) { + // e.printStackTrace(); + // } + // + // finally{ + // + // + // try { + // List<message> stuck = pub.close(15L, TimeUnit.SECONDS); + // assertTrue(stuck.isEmpty()); + // } catch (IOException | InterruptedException e) { + // // TODO Auto-generated catch block + // e.printStackTrace(); + // } + // } + + } + + public void connectorGetServersTestCI() { + AsdcConnectorClient connector = new AsdcConnectorClient(); + connector.init(testConfiguration); + + Either<List<String>, IDistributionClientResult> serverListFromAsdc = connector.getServerList(); + assertTrue(serverListFromAsdc.isLeft()); + assertEquals(serverList, serverListFromAsdc.left().value()); + } + + public void connectorRegisterCI() { + AsdcConnectorClient connector = new AsdcConnectorClient(); + connector.init(testConfiguration); + + ApiCredential creds = new ApiCredential("publicKey", "secretKey"); + Either<TopicRegistrationResponse, DistributionClientResultImpl> topicsFromAsdc = connector.registerAsdcTopics(creds); + assertTrue(topicsFromAsdc.isLeft()); + + } + + public void downloadArtifactTestCI() { + AsdcConnectorClient connector = new AsdcConnectorClient(); + connector.init(testConfiguration); + IArtifactInfo artifactInfo = initArtifactInfo(); + connector.dowloadArtifact(artifactInfo); + + } + // ########### TESTS TO ADD TO CI END ########### + +} diff --git a/src/test/java/org/openecomp/sdc/impl/HeatParserTest.java b/src/test/java/org/openecomp/sdc/impl/HeatParserTest.java new file mode 100644 index 0000000..3b5b1a1 --- /dev/null +++ b/src/test/java/org/openecomp/sdc/impl/HeatParserTest.java @@ -0,0 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.impl; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Test; +import org.openecomp.sdc.utils.heat.HeatParameter; +import org.openecomp.sdc.utils.heat.HeatParameterConstraint; +import org.openecomp.sdc.utils.heat.HeatParser; + +import com.google.common.base.Charsets; +import com.google.common.io.Resources; + +public class HeatParserTest { + + @Test + public void testParametersParsing() throws IOException{ + String resourceName = "heatExample.yaml"; + URL url = Resources.getResource(resourceName); + String heatFileContents = Resources.toString(url, Charsets.UTF_8); + assertNotNull("Didn't find "+resourceName, heatFileContents); + + HeatParser heatParser = new HeatParser(); + //Flat parameter entry + Map<String, HeatParameter> parameters = heatParser.getHeatParameters(heatFileContents); + HeatParameter heatParameter1 = parameters.get("image_name_1"); + validateField("string", heatParameter1.getType(), "type"); + validateField("Image Name", heatParameter1.getLabel(), "label"); + validateField("SCOIMAGE Specify an image name for instance1", heatParameter1.getDescription(), "description"); + validateField("cirros-0.3.1-x86_64", heatParameter1.getDefault(), "default"); + validateField(null, heatParameter1.getConstraints(), "constraints"); + validateField("false", heatParameter1.getHidden(), "hidden"); + + + //Flat parameter entry with constraints + heatParameter1 = parameters.get("network_id"); + validateField("string", heatParameter1.getType(), "type"); + validateField("Network ID", heatParameter1.getLabel(), "label"); + validateField("SCONETWORK Network to be used for the compute instance", heatParameter1.getDescription(), "description"); + validateField(null, heatParameter1.getDefault(), "default"); + validateField("true", heatParameter1.getHidden(), "hidden"); + + //Constraints + List<HeatParameterConstraint> constraints = heatParameter1.getConstraints(); + assertEquals("Number of constraints", 6, constraints.size()); + + //Length + HeatParameterConstraint lengthConstraint = heatParameter1.getLengthConstraint(); + assertNotNull(lengthConstraint); + Map<String, String> expectedMap = new HashMap<>(); + expectedMap.put("min", "6"); + expectedMap.put("max", "8"); + validateField(expectedMap, lengthConstraint.getLength(), "length"); + validateField("Password length must be between 6 and 8 characters.", lengthConstraint.getDescription(), "length description"); + + //Range + HeatParameterConstraint rangeConstraint = heatParameter1.getRangeConstraint(); + assertNotNull(rangeConstraint); + validateField(expectedMap, rangeConstraint.getRange(), "range"); + validateField("Range description", rangeConstraint.getDescription(), "range description"); + + //Allowed values + HeatParameterConstraint allowedValues = heatParameter1.getAllowedValuesConstraint(); + assertNotNull(allowedValues); + List<String> expectedValues = new ArrayList<>(); + expectedValues.add("m1.small"); + expectedValues.add("m1.medium"); + expectedValues.add("m1.large"); + validateField(expectedValues, allowedValues.getAllowed_values(), "allowed_values"); + validateField("Allowed values description", allowedValues.getDescription(), "allowed_values description"); + + //Allowed pattern + List<HeatParameterConstraint> allowedPatternList = heatParameter1.getAllowedPatternConstraint(); + assertNotNull(allowedPatternList); + assertEquals("Allowed pattern list", 2, allowedPatternList.size()); + HeatParameterConstraint allowedPattern = allowedPatternList.get(0); + validateField("[a-zA-Z0-9]+", allowedPattern.getAllowed_pattern(), "allowed_pattern"); + validateField("Password must consist of characters and numbers only.", allowedPattern.getDescription(), "allowed_pattern description"); + allowedPattern = allowedPatternList.get(1); + validateField("[A-Z]+[a-zA-Z0-9]*", allowedPattern.getAllowed_pattern(), "allowed_pattern"); + validateField("Password must start with an uppercase character.", allowedPattern.getDescription(), "allowed_pattern description"); + + //Custom constraint + List<HeatParameterConstraint> customConstraintList = heatParameter1.getCustomConstraintConstraint(); + assertNotNull(customConstraintList); + assertEquals("Custom constraint list", 1, customConstraintList.size()); + HeatParameterConstraint customConstraint = customConstraintList.get(0); + validateField("nova.keypair", customConstraint.getCustom_constraint(), "custom_constraint"); + validateField("Custom description", customConstraint.getDescription(), "custom_constraint description"); + } + + @Test + public void testParametersParsingInvalidYaml() throws IOException{ + String invalidHeatFileContents = "just text"; + HeatParser heatParser = new HeatParser(); + //Flat parameter entry + Map<String, HeatParameter> parameters = heatParser.getHeatParameters(invalidHeatFileContents); + assertNull(parameters); + } + + @Test + public void testParametersParsingNoParamteresSection() throws IOException{ + String heatFileContentsNoParams = "heat_template_version: 2013-05-23\r\n\r\ndescription: Simple template to deploy a stack with two virtual machine instances"; + HeatParser heatParser = new HeatParser(); + //Flat parameter entry + Map<String, HeatParameter> parameters = heatParser.getHeatParameters(heatFileContentsNoParams); + assertNull(parameters); + } + + private void validateField(Object expected, Object actual, String type){ + assertEquals("Field of type "+type+":", expected, actual); + } +} diff --git a/src/test/java/org/openecomp/sdc/impl/NotificationConsumerTest.java b/src/test/java/org/openecomp/sdc/impl/NotificationConsumerTest.java new file mode 100644 index 0000000..281a157 --- /dev/null +++ b/src/test/java/org/openecomp/sdc/impl/NotificationConsumerTest.java @@ -0,0 +1,281 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.impl; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Queue; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.eclipse.jetty.util.ArrayQueue; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.stubbing.Answer; +import org.openecomp.sdc.api.consumer.INotificationCallback; +import org.openecomp.sdc.api.notification.INotificationData; +import org.openecomp.sdc.api.results.IDistributionClientResult; +import org.openecomp.sdc.impl.ArtifactInfoImpl; +import org.openecomp.sdc.impl.DistributionClientImpl; +import org.openecomp.sdc.impl.NotificationConsumer; +import org.openecomp.sdc.utils.ArtifactTypeEnum; +import org.openecomp.sdc.utils.DistributionActionResultEnum; +import org.openecomp.sdc.utils.DistributionClientConstants; + +import com.att.nsa.cambria.client.CambriaConsumer; + +public class NotificationConsumerTest { + private CambriaConsumer cambriaConsumer = mock(CambriaConsumer.class); + private INotificationCallback clientCallback = spy(INotificationCallback.class); + private Queue<Iterable<String>> notificationsQueue = new ArrayQueue<>(100); + private DistributionClientImpl distributionClient = spy(DistributionClientImpl.class); + private List<String> artifactsTypes = Arrays.asList(ArtifactTypeEnum.HEAT.name()); + private List<Boolean> notificationStatusResults = new ArrayList<>(); + final static IDistributionClientResult DISTRIBUTION_SUCCESS_RESULT = buildSuccessResult(); + + private NotificationConsumer createNotificationConsumer() { + return new NotificationConsumer(cambriaConsumer, clientCallback, artifactsTypes, distributionClient); + } + + @Before + public void beforeTest() throws IOException { + Mockito.reset(clientCallback, distributionClient); + when(cambriaConsumer.fetch()).then(new Answer<Iterable<String>>() { + @Override + public Iterable<String> answer(InvocationOnMock invocation) throws Throwable { + if (!notificationsQueue.isEmpty()) { + return notificationsQueue.remove(); + } else { + return new ArrayList<>(); + } + } + }); + when(distributionClient.sendNotificationStatus(Mockito.anyLong(), Mockito.anyString(), Mockito.any(ArtifactInfoImpl.class), Mockito.anyBoolean())).then(new Answer<IDistributionClientResult>() { + @Override + public IDistributionClientResult answer(InvocationOnMock invocation) throws Throwable { + boolean isNotified = (boolean) invocation.getArguments()[3]; + notificationStatusResults.add(Boolean.valueOf(isNotified)); + return DISTRIBUTION_SUCCESS_RESULT; + } + }); + + } + + private static IDistributionClientResult buildSuccessResult() { + return new IDistributionClientResult() { + + @Override + public String getDistributionMessageResult() { + return ""; + } + + @Override + public DistributionActionResultEnum getDistributionActionResult() { + return DistributionActionResultEnum.SUCCESS; + } + }; + } + + @Test + public void testNoNotifiactionsSent() throws InterruptedException { + + ScheduledExecutorService executorPool = Executors.newScheduledThreadPool(DistributionClientConstants.POOL_SIZE); + executorPool.scheduleAtFixedRate(createNotificationConsumer(), 0, 100, TimeUnit.MILLISECONDS); + + Thread.sleep(1000); + executorPool.shutdown(); + + Mockito.verify(clientCallback, Mockito.times(0)).activateCallback(Mockito.any(INotificationData.class)); + + } + + @Test + public void testNonRelevantNotificationSent() throws InterruptedException { + + simulateNotificationFromUEB(getAsdcServiceNotificationWithoutHeatArtifact()); + Mockito.verify(clientCallback, Mockito.times(0)).activateCallback(Mockito.any(INotificationData.class)); + + } + + @Test + public void testRelevantNotificationSent() throws InterruptedException { + simulateNotificationFromUEB(getAsdcServiceNotificationWithHeatArtifact()); + Mockito.verify(clientCallback, Mockito.times(1)).activateCallback(Mockito.any(INotificationData.class)); + + } + + @Test + public void testNonExistingArtifactsNotificationSent() throws InterruptedException { + simulateNotificationFromUEB(getAsdcNotificationWithNonExistentArtifact()); + Mockito.verify(clientCallback, Mockito.times(1)).activateCallback(Mockito.any(INotificationData.class)); + + } + + @Test + public void testNotificationStatusSent() throws InterruptedException { + simulateNotificationFromUEB(getAsdcServiceNotificationWithHeatArtifact()); + + Mockito.verify(distributionClient, Mockito.times(3)).sendNotificationStatus(Mockito.anyLong(), Mockito.anyString(), Mockito.any(ArtifactInfoImpl.class), Mockito.anyBoolean()); + assertTrue(countInstances(notificationStatusResults, Boolean.TRUE) == 1); + assertTrue(countInstances(notificationStatusResults, Boolean.FALSE) == 2); + } + + @Test + public void testNotificationRelatedArtifacts() throws InterruptedException { + List<String> artifactTypesTmp = new ArrayList<>(); + for (ArtifactTypeEnum artifactTypeEnum : ArtifactTypeEnum.values()) { + artifactTypesTmp.add(artifactTypeEnum.name()); + } + artifactsTypes = artifactTypesTmp; + simulateNotificationFromUEB(getAsdcServiceNotificationWithRelatedArtifacts()); + + Mockito.verify(distributionClient, Mockito.times(3)).sendNotificationStatus(Mockito.anyLong(), Mockito.anyString(), Mockito.any(ArtifactInfoImpl.class), Mockito.anyBoolean()); + assertTrue(countInstances(notificationStatusResults, Boolean.TRUE) == 3); + assertTrue(countInstances(notificationStatusResults, Boolean.FALSE) == 0); + } + + @Test + public void testNotificationStatusWithServiceArtifatcs() throws InterruptedException { + simulateNotificationFromUEB(getNotificationWithServiceArtifatcs()); + Mockito.verify(distributionClient, Mockito.times(6)).sendNotificationStatus(Mockito.anyLong(), Mockito.anyString(), Mockito.any(ArtifactInfoImpl.class), Mockito.anyBoolean()); + assertTrue(countInstances(notificationStatusResults, Boolean.TRUE) == 2); + assertTrue(countInstances(notificationStatusResults, Boolean.FALSE) == 4); + + } + + private void simulateNotificationFromUEB(final String notificationFromUEB) throws InterruptedException { + ScheduledExecutorService executorPool = Executors.newScheduledThreadPool(DistributionClientConstants.POOL_SIZE); + executorPool.scheduleAtFixedRate(createNotificationConsumer(), 0, 100, TimeUnit.MILLISECONDS); + + Thread.sleep(200); + + List<String> nonHeatNotification = Arrays.asList(notificationFromUEB); + notificationsQueue.add(nonHeatNotification); + Thread.sleep(800); + executorPool.shutdown(); + } + + private String getAsdcServiceNotificationWithHeatArtifact() { + return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" + + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"resources\" : [{\r\n" + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + " \"artifactType\" : \"YANG_XML\",\r\n" + + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" + + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.env\",\r\n" + + " \"artifactType\" : \"HEAT_ENV\",\r\n" + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]}"; + } + + private String getAsdcNotificationWithNonExistentArtifact() { + return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" + + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"bugabuga\" : \"xyz\",\r\n" + " \"resources\" : [{\r\n" + + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + " \"artifactType\" : \"HEAT\",\r\n" + + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactBuga\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + + " }, {\r\n" + " \"artifactName\" : \"buga.bug\",\r\n" + " \"artifactType\" : \"BUGA_BUGA\",\r\n" + + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]}"; + } + + private String getAsdcServiceNotificationWithRelatedArtifacts() { + return "{\"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" + + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"resources\" : [{\r\n" + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + " \"artifactType\" : \"YANG_XML\",\r\n" + + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" + + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + " \"relatedArtifacts\" : [\r\n" + + " \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\"\r\n" + " ]" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\", \r\n" + " \"relatedArtifacts\" : [\r\n" + + " \"0005bc4a-2c19-452e-be6d-d574a56be4d0\", \r\n" + " \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\"\r\n" + " ]" + " }, {\r\n" + + " \"artifactName\" : \"heat.env\",\r\n" + " \"artifactType\" : \"HEAT_ENV\",\r\n" + + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]}"; + } + + private String getAsdcServiceNotificationWithoutHeatArtifact() { + return "{" + " \"distributionID\" : \"5v1234d8-5b6d-42c4-7t54-47v95n58qb7\"," + " \"serviceName\" : \"srv1\"," + " \"serviceVersion\": \"2.0\"," + " \"serviceUUID\" : \"4e0697d8-5b6d-42c4-8c74-46c33d46624c\"," + + " \"serviceArtifacts\":[" + " {" + " \"artifactName\" : \"ddd.yml\"," + " \"artifactType\" : \"DG_XML\"," + " \"artifactTimeout\" : \"65\"," + + " \"artifactDescription\" : \"description\"," + " \"artifactURL\" :" + " \"/asdc/v1/catalog/services/srv1/2.0/resources/ddd/3.0/artifacts/ddd.xml\" ," + + " \"resourceUUID\" : \"4e5874d8-5b6d-42c4-8c74-46c33d90drw\" ," + " \"checksum\" : \"15e389rnrp58hsw==\"" + " }" + " ]" + "}"; + } + + private String getNotificationWithServiceArtifatcs() { + return "{\r\n" + " \"distributionID\" : \"bcc7a72e-90b1-4c5f-9a37-28dc3cd86416\",\r\n" + " \"serviceName\" : \"Testnotificationser1\",\r\n" + " \"serviceVersion\" : \"1.0\",\r\n" + + " \"serviceUUID\" : \"7f7f94f4-373a-4b71-a0e3-80ae2ba4eb5d\",\r\n" + " \"serviceDescription\" : \"TestNotificationVF1\",\r\n" + " \"serviceArtifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactType\" : \"YANG_XML\",\r\n" + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" + + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.env\",\r\n" + + " \"artifactType\" : \"HEAT_ENV\",\r\n" + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ],\r\n" + " \"resources\" : [{\r\n" + " \"resourceInstanceName\" : \"testnotificationvf11\",\r\n" + + " \"resourceName\" : \"TestNotificationVF1\",\r\n" + " \"resourceVersion\" : \"1.0\",\r\n" + " \"resoucreType\" : \"VF\",\r\n" + " \"resourceUUID\" : \"907e1746-9f69-40f5-9f2a-313654092a2d\",\r\n" + + " \"artifacts\" : [{\r\n" + " \"artifactName\" : \"sample-xml-alldata-1-1.xml\",\r\n" + " \"artifactType\" : \"YANG_XML\",\r\n" + + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/sample-xml-alldata-1-1.xml\",\r\n" + + " \"artifactChecksum\" : \"MTUxODFkMmRlOTNhNjYxMGYyYTI1ZjA5Y2QyNWQyYTk\\u003d\",\r\n" + " \"artifactDescription\" : \"MyYang\",\r\n" + " \"artifactTimeout\" : 0,\r\n" + + " \"artifactUUID\" : \"0005bc4a-2c19-452e-be6d-d574a56be4d0\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.yaml\",\r\n" + + " \"artifactType\" : \"HEAT\",\r\n" + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.yaml\",\r\n" + + " \"artifactChecksum\" : \"ODEyNjE4YTMzYzRmMTk2ODVhNTU2NTg3YWEyNmIxMTM\\u003d\",\r\n" + " \"artifactDescription\" : \"heat\",\r\n" + " \"artifactTimeout\" : 60,\r\n" + + " \"artifactUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\",\r\n" + " \"artifactVersion\" : \"1\"\r\n" + " }, {\r\n" + " \"artifactName\" : \"heat.env\",\r\n" + + " \"artifactType\" : \"HEAT_ENV\",\r\n" + " \"artifactURL\" : \"/asdc/v1/catalog/services/Testnotificationser1/1.0/resourceInstances/testnotificationvf11/artifacts/heat.env\",\r\n" + + " \"artifactChecksum\" : \"NGIzMjExZTM1NDc2NjBjOTQyMGJmMWNiMmU0NTE5NzM\\u003d\",\r\n" + " \"artifactDescription\" : \"Auto-generated HEAT Environment deployment artifact\",\r\n" + + " \"artifactTimeout\" : 0,\r\n" + " \"artifactUUID\" : \"ce65d31c-35c0-43a9-90c7-596fc51d0c86\",\r\n" + " \"artifactVersion\" : \"1\",\r\n" + + " \"generatedFromUUID\" : \"8df6123c-f368-47d3-93be-1972cefbcc35\"\r\n" + " }\r\n" + " ]\r\n" + " }\r\n" + " ]\r\n" + "}"; + } + + private <T> int countInstances(List<T> list, T element) { + int count = 0; + for (T curr : list) { + if (curr.equals(element)) { + count++; + } + } + return count; + } +} diff --git a/src/test/java/org/openecomp/sdc/utils/ArtifactsUtils.java b/src/test/java/org/openecomp/sdc/utils/ArtifactsUtils.java new file mode 100644 index 0000000..5a24849 --- /dev/null +++ b/src/test/java/org/openecomp/sdc/utils/ArtifactsUtils.java @@ -0,0 +1,48 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.utils; + + + +import org.apache.commons.codec.binary.Base64; +import org.openecomp.sdc.impl.mock.DistributionClientDownloadResultStubImpl; +import org.openecomp.sdc.utils.GeneralUtils; + + + +public class ArtifactsUtils { + static DistributionClientDownloadResultStubImpl distributionClientDownloadResultStubImpl = new DistributionClientDownloadResultStubImpl(); + + public static byte [] getArtifactPayload(){ + return distributionClientDownloadResultStubImpl.getArtifactPayload(); + } + + public static String getValidChecksum(){ + + String payloadStr = new String(distributionClientDownloadResultStubImpl.getArtifactPayload()); + + byte[] decodedPayload = Base64.decodeBase64(payloadStr); + String checkSum = GeneralUtils.calculateMD5 (new String(decodedPayload)); + + return checkSum; + } + +} diff --git a/src/test/java/org/openecomp/sdc/utils/TestConfiguration.java b/src/test/java/org/openecomp/sdc/utils/TestConfiguration.java new file mode 100644 index 0000000..e4b0ca8 --- /dev/null +++ b/src/test/java/org/openecomp/sdc/utils/TestConfiguration.java @@ -0,0 +1,272 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.utils; + +import java.util.ArrayList; +import java.util.List; + +import org.openecomp.sdc.api.consumer.IConfiguration; +import org.openecomp.sdc.utils.ArtifactTypeEnum; +import org.openecomp.sdc.utils.DistributionClientConstants; + +public class TestConfiguration implements IConfiguration { + + private String asdcAddress; + private String user; + private String password; + private int pollingInterval = DistributionClientConstants.MIN_POLLING_INTERVAL_SEC; + private int pollingTimeout = DistributionClientConstants.POLLING_TIMEOUT_SEC; + private List<String> relevantArtifactTypes; + private String consumerGroup; + private String environmentName; + private String comsumerID; + private String keyStorePath; + private String keyStorePassword; + private boolean activateServerTLSAuth; + + public TestConfiguration(IConfiguration other) { + this.asdcAddress = other.getAsdcAddress(); + this.comsumerID = other.getConsumerID(); + this.consumerGroup = other.getConsumerGroup(); + this.environmentName = other.getEnvironmentName(); + this.password = other.getPassword(); + this.pollingInterval = other.getPollingInterval(); + this.pollingTimeout = other.getPollingTimeout(); + this.relevantArtifactTypes = other.getRelevantArtifactTypes(); + this.user = other.getUser(); + this.keyStorePath = other.getKeyStorePath(); + this.keyStorePassword = other.getKeyStorePassword(); + this.activateServerTLSAuth = other.activateServerTLSAuth(); + } + + public TestConfiguration() { + this.asdcAddress = "localhost:8443"; + this.comsumerID = "mso-123456"; + this.consumerGroup = "mso-group"; + this.environmentName = "PROD"; + this.password = "password"; + this.pollingInterval = 20; + this.pollingTimeout = 20; + this.relevantArtifactTypes = new ArrayList<String>(); + this.relevantArtifactTypes.add(ArtifactTypeEnum.HEAT.name()); + this.user = "mso-user"; + this.keyStorePath = "etc/asdc-client.jks"; + this.keyStorePassword = "Aa123456"; + this.activateServerTLSAuth = false; + } + + @Override + public String getAsdcAddress() { + return asdcAddress; + } + + @Override + public String getUser() { + return user; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public int getPollingInterval() { + return pollingInterval; + } + + @Override + public int getPollingTimeout() { + return pollingTimeout; + } + + @Override + public List<String> getRelevantArtifactTypes() { + return relevantArtifactTypes; + } + + @Override + public String getConsumerGroup() { + return consumerGroup; + } + + @Override + public String getEnvironmentName() { + return environmentName; + } + + @Override + public String getConsumerID() { + return comsumerID; + } + + @Override + public String getKeyStorePath() { + return keyStorePath; + } + + @Override + public String getKeyStorePassword() { + return keyStorePassword; + } + + public String getComsumerID() { + return comsumerID; + } + + public void setComsumerID(String comsumerID) { + this.comsumerID = comsumerID; + } + + public void setAsdcAddress(String asdcAddress) { + this.asdcAddress = asdcAddress; + } + + public void setUser(String user) { + this.user = user; + } + + public void setPassword(String password) { + this.password = password; + } + + public void setPollingInterval(int pollingInterval) { + this.pollingInterval = pollingInterval; + } + + public void setPollingTimeout(int pollingTimeout) { + this.pollingTimeout = pollingTimeout; + } + + public void setRelevantArtifactTypes(List<String> relevantArtifactTypes) { + this.relevantArtifactTypes = relevantArtifactTypes; + } + + public void setConsumerGroup(String consumerGroup) { + this.consumerGroup = consumerGroup; + } + + public void setEnvironmentName(String environmentName) { + this.environmentName = environmentName; + } + + public void setKeyStorePath(String keyStorePath) { + this.keyStorePath = keyStorePath; + } + + public void setKeyStorePassword(String keyStorePassword) { + this.keyStorePassword = keyStorePassword; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((asdcAddress == null) ? 0 : asdcAddress.hashCode()); + result = prime * result + ((comsumerID == null) ? 0 : comsumerID.hashCode()); + result = prime * result + ((consumerGroup == null) ? 0 : consumerGroup.hashCode()); + result = prime * result + ((environmentName == null) ? 0 : environmentName.hashCode()); + result = prime * result + ((password == null) ? 0 : password.hashCode()); + result = prime * result + pollingInterval; + result = prime * result + pollingTimeout; + result = prime * result + ((relevantArtifactTypes == null) ? 0 : relevantArtifactTypes.hashCode()); + result = prime * result + ((user == null) ? 0 : user.hashCode()); + return result; + } + + @Override + public boolean activateServerTLSAuth() { + + return activateServerTLSAuth; + } + + public void setactivateServerTLSAuth(boolean activateServerTLSAuth) { + this.activateServerTLSAuth = activateServerTLSAuth; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TestConfiguration other = (TestConfiguration) obj; + if (asdcAddress == null) { + if (other.asdcAddress != null) + return false; + } else if (!asdcAddress.equals(other.asdcAddress)) + return false; + if (comsumerID == null) { + if (other.comsumerID != null) + return false; + } else if (!comsumerID.equals(other.comsumerID)) + return false; + if (consumerGroup == null) { + if (other.consumerGroup != null) + return false; + } else if (!consumerGroup.equals(other.consumerGroup)) + return false; + if (environmentName == null) { + if (other.environmentName != null) + return false; + } else if (!environmentName.equals(other.environmentName)) + return false; + if (password == null) { + if (other.password != null) + return false; + } else if (!password.equals(other.password)) + return false; + if (pollingInterval != other.pollingInterval) + return false; + if (pollingTimeout != other.pollingTimeout) + return false; + if (relevantArtifactTypes == null) { + if (other.relevantArtifactTypes != null) + return false; + } else if (!relevantArtifactTypes.equals(other.relevantArtifactTypes)) + return false; + if (user == null) { + if (other.user != null) + return false; + } else if (!user.equals(other.user)) + return false; + if (keyStorePath == null) { + if (other.keyStorePath != null) + return false; + } else if (!keyStorePath.equals(other.keyStorePath)) + return false; + if (keyStorePassword == null) { + if (other.keyStorePassword != null) + return false; + } else if (!keyStorePassword.equals(other.keyStorePassword)) + return false; + + return true; + } + + @Override + public String toString() { + return "TestConfiguration [asdcAddress=" + asdcAddress + ", user=" + user + ", password=" + password + ", pollingInterval=" + pollingInterval + ", pollingTimeout=" + pollingTimeout + ", relevantArtifactTypes=" + relevantArtifactTypes + + ", consumerGroup=" + consumerGroup + ", environmentName=" + environmentName + ", comsumerID=" + comsumerID + "]"; + } +} diff --git a/src/test/java/org/openecomp/sdc/utils/TestNotificationCallback.java b/src/test/java/org/openecomp/sdc/utils/TestNotificationCallback.java new file mode 100644 index 0000000..bef643a --- /dev/null +++ b/src/test/java/org/openecomp/sdc/utils/TestNotificationCallback.java @@ -0,0 +1,34 @@ +/*- + * ============LICENSE_START======================================================= + * sdc-distribution-client + * ================================================================================ + * 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.sdc.utils; + +import org.openecomp.sdc.api.consumer.INotificationCallback; +import org.openecomp.sdc.api.notification.INotificationData; + +public class TestNotificationCallback implements INotificationCallback{ + + @Override + public void activateCallback(INotificationData data) { + System.out.println("notification callback was called"); + + } + +} |