From 84a209835820238f50d84ad5be5b9badaa5283c5 Mon Sep 17 00:00:00 2001 From: Vodafone Date: Mon, 18 Mar 2019 15:08:33 +0530 Subject: List of Input Parameters for VSP Change-Id: Ie913ead731e120bd69349a4ebec13f4521eaac4d Issue-ID: SDC-2049 Co-authored-by: jguistwite@iconectiv.com Signed-off-by: Vodafone --- .../openecomp-sdc-externaltesting-impl/pom.xml | 114 ++++ .../externaltesting/impl/ClientConfiguration.java | 29 + .../impl/CsarMetadataVariableResolver.java | 210 +++++++ .../impl/ExternalTestingManagerImpl.java | 610 +++++++++++++++++++++ .../impl/RemoteTestingEndpointDefinition.java | 39 ++ .../externaltesting/impl/TestingAccessConfig.java | 30 + .../externaltesting/impl/VariableResolver.java | 27 + .../src/main/resources/factoryConfiguration.json | 3 + .../src/test/data/csar.zip | Bin 0 -> 14993 bytes .../src/test/data/fulldefinition.json | 26 + .../src/test/data/managertestconfiguration.yaml | 13 + .../src/test/data/notfound.json | 6 + .../src/test/data/priorexecution.json | 21 + .../src/test/data/runresult.json | 22 + .../src/test/data/scenarios.json | 10 + .../src/test/data/testcase-sriov.json | 48 ++ .../src/test/data/testcases.json | 16 + .../src/test/data/testconfiguration.yaml | 11 + .../src/test/data/testsuites.json | 6 + .../src/test/data/testtree.json | 119 ++++ .../externaltesting/impl/ConfigurationTests.java | 78 +++ .../impl/CsarMetadataVariableResolverTest.java | 88 +++ .../impl/ExternalTestingManagerImplTests.java | 283 ++++++++++ .../impl/ExternalTestingTestSuite.java | 31 ++ .../src/test/resources/logback-test.xml | 30 + 25 files changed, 1870 insertions(+) create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/pom.xml create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ClientConfiguration.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/CsarMetadataVariableResolver.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImpl.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/RemoteTestingEndpointDefinition.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/TestingAccessConfig.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/VariableResolver.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/resources/factoryConfiguration.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/csar.zip create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/fulldefinition.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/managertestconfiguration.yaml create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/notfound.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/priorexecution.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/runresult.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/scenarios.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testcase-sriov.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testcases.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testconfiguration.yaml create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testsuites.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testtree.json create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ConfigurationTests.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/CsarMetadataVariableResolverTest.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImplTests.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ExternalTestingTestSuite.java create mode 100644 openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/resources/logback-test.xml (limited to 'openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl') diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/pom.xml b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/pom.xml new file mode 100644 index 0000000000..7411cf5cc1 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/pom.xml @@ -0,0 +1,114 @@ + + + + 4.0.0 + + openecomp-sdc-externaltesting-impl + openecomp-sdc-externaltesting-impl + + + + org.openecomp.sdc + openecomp-sdc-lib + 1.4.0-SNAPSHOT + ../.. + + + + + ch.qos.logback + logback-classic + ${logback.version} + + + org.openecomp.sdc + openecomp-sdc-logging-core + ${project.version} + runtime + + + org.openecomp.sdc + openecomp-sdc-logging-api + ${project.version} + + + junit + junit + test + + + org.openecomp.sdc.core + openecomp-utilities-lib + ${project.version} + + + org.openecomp.sdc + openecomp-sdc-externaltesting-api + ${project.version} + + + org.openecomp.sdc.core + openecomp-common-lib + ${project.version} + + + org.mockito + mockito-core + test + + + org.openecomp.sdc.core + openecomp-heat-lib + ${project.version} + + + commons-io + commons-io + ${commons.io.version} + + + org.springframework + spring-web + ${spring.framework.version} + + + org.openecomp.sdc + openecomp-sdc-vendor-software-product-manager + ${project.version} + + + org.projectlombok + lombok + ${lombok.version} + + + org.codehaus.groovy + groovy-all-minimal + ${groovy.minimal.version} + test + + + org.projectlombok + lombok + ${lombok.version} + provided + + + + diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ClientConfiguration.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ClientConfiguration.java new file mode 100644 index 0000000000..2787e7e97f --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ClientConfiguration.java @@ -0,0 +1,29 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import lombok.Data; + +@Data +public class ClientConfiguration { + /** + * Enable/disable state for the feature. Client can use this + * to show/hide menu items. + */ + private boolean enabled; + +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/CsarMetadataVariableResolver.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/CsarMetadataVariableResolver.java new file mode 100644 index 0000000000..191fff0c6c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/CsarMetadataVariableResolver.java @@ -0,0 +1,210 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import lombok.EqualsAndHashCode; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.tuple.Pair; +import org.openecomp.core.externaltesting.api.VtpTestExecutionRequest; +import org.openecomp.core.externaltesting.errors.ExternalTestingException; +import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager; +import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory; +import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager; +import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory; +import org.openecomp.sdc.versioning.VersioningManager; +import org.openecomp.sdc.versioning.VersioningManagerFactory; +import org.openecomp.sdc.versioning.dao.types.Version; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.util.MultiValueMap; + +import javax.annotation.PostConstruct; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; + +/** + * The CSAR Metadata variable resolver is responsible for processing of + * variables in the test request. It looks for variables with the "csar:" prefix + * and extracts the contents of the uploaded CSAR file for a VSP. + */ +public class CsarMetadataVariableResolver implements VariableResolver { + + private Logger logger = LoggerFactory.getLogger(CsarMetadataVariableResolver.class); + + static final String VSP_ID = "vspId"; + static final String VSP_VERSION = "vspVersion"; + static final String CSAR_PREFIX = "csar:"; + + private VersioningManager versioningManager; + private VendorSoftwareProductManager vendorSoftwareProductManager; + private OrchestrationTemplateCandidateManager candidateManager; + + CsarMetadataVariableResolver(VersioningManager versioningManager, + VendorSoftwareProductManager vendorSoftwareProductManager, + OrchestrationTemplateCandidateManager candidateManager) { + this(); + this.versioningManager = versioningManager; + this.vendorSoftwareProductManager = vendorSoftwareProductManager; + this.candidateManager = candidateManager; + } + + CsarMetadataVariableResolver() { + + } + + @PostConstruct + public void init() { + if (versioningManager == null) { + versioningManager = VersioningManagerFactory.getInstance().createInterface(); + } + if (vendorSoftwareProductManager == null) { + vendorSoftwareProductManager = + VspManagerFactory.getInstance().createInterface(); + } + if (candidateManager == null) { + candidateManager = + OrchestrationTemplateCandidateManagerFactory.getInstance().createInterface(); + } + } + + @Override + public boolean resolvesVariablesForRequest(VtpTestExecutionRequest requestItem) { + Map params = requestItem.getParameters(); + + // no params, quickly return. + if (params == null) { + return false; + } + + // no match, quickly return + if (!params.containsKey(VSP_ID) || !params.containsKey(VSP_VERSION)) { + return false; + } + + return (params.keySet().stream().anyMatch(s -> StringUtils.startsWith(s, CSAR_PREFIX))); + } + + @Override + public void resolve(VtpTestExecutionRequest requestItem, MultiValueMap body) { + logger.debug("run {} variable resolver...", this.getClass().getSimpleName()); + Map params = requestItem.getParameters(); + String vspId = params.get(VSP_ID); + String version = params.get(VSP_VERSION); + + try { + extractMetadata(requestItem, body, vspId, version); + } + catch (IOException ex) { + logger.error("metadata extraction failed", ex); + } + } + + /** + * Extract the metadata from the VSP CSAR file. + * @param requestItem item to add metadata to for processing + * @param vspId VSP identifier + * @param version VSP version + */ + @SuppressWarnings("WeakerAccess") + protected void extractMetadata(VtpTestExecutionRequest requestItem, MultiValueMap body, String vspId, String version) throws IOException { + + Version ver = new Version(version); + logger.debug("attempt to retrieve archive for VSP {} version {}", vspId, ver.getId()); + + Optional> ozip = candidateManager.get(vspId, new Version(version)); + if (!ozip.isPresent()) { + ozip = vendorSoftwareProductManager.get(vspId, ver); + } + + if (!ozip.isPresent()) { + List versions = versioningManager.list(vspId); + String knownVersions = versions + .stream() + .map(v -> String.format("%d.%d: %s (%s)", v.getMajor(), v.getMinor(), v.getStatus(), v.getId())) + .collect(Collectors.joining("\n")); + + String detail = String.format("Unable to find archive for VSP ID %s and Version %s. Known versions are:\n%s", + vspId, version, knownVersions); + + throw new ExternalTestingException("Archive Processing Failed", 500, detail); + } + + // safe here to do get. + Pair zip = ozip.get(); + processArchive(requestItem, body, zip.getRight()); + } + + @EqualsAndHashCode(callSuper = false) + private class NamedByteArrayResource extends ByteArrayResource { + private String filename; + private NamedByteArrayResource(byte[] bytes, String filename) { + super(bytes, filename); + this.filename = filename; + } + @Override + public String getFilename() { + return this.filename; + } + + } + + @SuppressWarnings("WeakerAccess") + protected void processArchive(VtpTestExecutionRequest requestItem, MultiValueMap body, byte[] zip) { + try { + ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(zip)); + ZipEntry entry; + while ((entry = zipStream.getNextEntry()) != null) { + String entryName = entry.getName(); + logger.debug("csar contains entry {}", entryName); + Map params = requestItem.getParameters(); + params.forEach((key,val) -> { + if (key.startsWith(CSAR_PREFIX)) { + addToBody(requestItem, body, zipStream, entryName, key); + } + }); + } + } catch (IOException ex) { + logger.error("IO Exception parsing zip", ex); + } + } + + private void addToBody(VtpTestExecutionRequest requestItem, MultiValueMap body, ZipInputStream zipStream, String entryName, String key) { + String filename = key.substring(CSAR_PREFIX.length()); + logger.debug("match {} with {}", entryName, filename); + if (StringUtils.equals(entryName, filename)) { + try { + NamedByteArrayResource res = new NamedByteArrayResource(IOUtils.toByteArray(zipStream), filename); + body.add("file", res); + + // we've added the file to the body. need to replace the value in the request for this + // parameter to match the VTP requirement that it start with a file URL protocol handler. + requestItem.getParameters().put(key, "file://" + entryName); + + } catch (IOException ex) { + logger.error("failed to read zip entry content for {}", entryName, ex); + } + } + } +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImpl.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImpl.java new file mode 100644 index 0000000000..38fb11c6ae --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImpl.java @@ -0,0 +1,610 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonObject; +import com.google.gson.JsonParseException; +import org.apache.commons.lang3.ArrayUtils; +import org.apache.commons.lang3.StringUtils; +import org.onap.sdc.tosca.services.YamlUtil; +import org.openecomp.core.externaltesting.api.*; +import org.openecomp.core.externaltesting.errors.ExternalTestingException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.http.client.SimpleClientHttpRequestFactory; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import javax.annotation.PostConstruct; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.*; +import java.util.stream.Collectors; + +public class ExternalTestingManagerImpl implements ExternalTestingManager { + + private Logger logger = LoggerFactory.getLogger(ExternalTestingManagerImpl.class); + + private static final String HTTP_STATUS = "httpStatus"; + private static final String CODE = "code"; + private static final String ERROR = "error"; + private static final String MESSAGE = "message"; + private static final String DETAIL = "detail"; + private static final String PATH = "path"; + + private static final String CONFIG_FILE_PROPERTY = "configuration.yaml"; + private static final String CONFIG_SECTION = "externalTestingConfig"; + + private static final String VTP_SCENARIOS_URI = "%s/v1/vtp/scenarios"; + private static final String VTP_TESTSUITE_URI = "%s/v1/vtp/scenarios/%s/testsuites"; + private static final String VTP_TESTCASES_URI = "%s/v1/vtp/scenarios/%s/testcases"; + private static final String VTP_TESTCASE_URI = "%s/v1/vtp/scenarios/%s/testsuites/%s/testcases/%s"; + private static final String VTP_EXECUTIONS_URI = "%s/v1/vtp/executions"; + private static final String VTP_EXECUTION_URI = "%s/v1/vtp/executions/%s"; + + private static final String INVALIDATE_STATE_ERROR = "Invalid State"; + private static final String NO_ACCESS_CONFIGURATION_DEFINED = "No access configuration defined"; + + private TestingAccessConfig accessConfig; + private Map endpoints = new HashMap<>(); + + private RestTemplate restTemplate; + + private List variableResolvers; + + public ExternalTestingManagerImpl(@Autowired(required=false) List variableResolvers) { + this.variableResolvers = variableResolvers; + // nothing to do at the moment. + restTemplate = new RestTemplate(); + } + + /** + * Read the configuration from the yaml file for this bean. If we get an exception during load, + * don't force an error starting SDC but log a warning. Do no warm... + */ + @PostConstruct + public void loadConfig() { + + String file = Objects.requireNonNull(System.getProperty(CONFIG_FILE_PROPERTY), + "Config file location must be specified via system property " + CONFIG_FILE_PROPERTY); + try { + Object rawConfig = getExternalTestingAccessConfiguration(file); + if (rawConfig != null) { + accessConfig = new ObjectMapper().convertValue(rawConfig, TestingAccessConfig.class); + accessConfig.getEndpoints() + .stream() + .filter(RemoteTestingEndpointDefinition::isEnabled) + .forEach(e -> endpoints.put(e.getId(), e)); + } + } + catch (IOException ex) { + logger.warn("Unable to initialize external testing configuration. Add '" + CONFIG_SECTION + "' to configuration.yaml with url value. Feature will be hobbled with results hardcoded to empty values.", ex); + } + } + + /** + * Return the configuration of this feature that we want to + * expose to the client. Treated as a JSON blob for flexibility. + */ + @Override + public String getConfig() { + ClientConfiguration cc = null; + if (accessConfig != null) { + cc = accessConfig.getClient(); + } + if (cc == null) { + cc = new ClientConfiguration(); + cc.setEnabled(false); + } + try { + return new ObjectMapper().writeValueAsString(cc); + } catch (JsonProcessingException e) { + logger.error("failed to write client config", e); + return "{\"enabled\":false}"; + } + } + + @Override + public TestTreeNode getTestCasesAsTree() { + TestTreeNode root = new TestTreeNode("root", "root"); + + // quick out in case of non-configured SDC + if (accessConfig == null) { + return root; + } + + for (RemoteTestingEndpointDefinition ep : accessConfig.getEndpoints()) { + if (ep.isEnabled()) { + buildTreeFromEndpoint(ep, root); + } + } + return root; + } + + private void buildTreeFromEndpoint(RemoteTestingEndpointDefinition ep, TestTreeNode root) { + try { + logger.debug("process endpoint {}", ep.getId()); + getScenarios(ep.getId()).stream().filter(s -> + ((ep.getScenarioFilter() == null) || ep.getScenarioFilterPattern().matcher(s.getName()).matches())) + .forEach(s -> { + addScenarioToTree(root, s); + getTestSuites(ep.getId(), s.getName()).forEach(suite -> addSuiteToTree(root, s, suite)); + getTestCases(ep.getId(), s.getName()).forEach(tc -> { + try { + VtpTestCase details = getTestCase(ep.getId(), s.getName(), tc.getTestSuiteName(), tc.getTestCaseName()); + addTestCaseToTree(root, ep.getId(), s.getName(), tc.getTestSuiteName(), details); + } + catch (@SuppressWarnings("squid:S1166") ExternalTestingException ex) { + // Not logging stack trace on purpose. VTP was throwing exceptions for certain test cases. + logger.warn("failed to load test case {}", tc.getTestCaseName()); + } + }); + }); + } + catch (ExternalTestingException ex) { + logger.error("unable to contact testing endpoint {}", ep.getId(), ex); + } + } + + private Optional findNamedChild(TestTreeNode root, String name) { + if (root.getChildren() == null) { + return Optional.empty(); + } + return root.getChildren().stream().filter(n->n.getName().equals(name)).findFirst(); + } + + /** + * Find the place in the tree to add the test case. + * @param root root of the tree. + * @param endpointName name of the endpoint to assign to the test case. + * @param scenarioName scenario to add this case to + * @param testSuiteName suite in the scenario to add this case to + * @param tc test case to add. + */ + private void addTestCaseToTree(TestTreeNode root, String endpointName, String scenarioName, String testSuiteName, VtpTestCase tc) { + // return quickly. + if (tc == null) { + return; + } + findNamedChild(root, scenarioName) + .ifPresent(scenarioNode -> findNamedChild(scenarioNode, testSuiteName) + .ifPresent(suiteNode -> { + massageTestCaseForUI(tc, endpointName, scenarioName); + if (suiteNode.getTests() == null) { + suiteNode.setTests(new ArrayList<>()); + } + suiteNode.getTests().add(tc); + })); + } + + private void massageTestCaseForUI(VtpTestCase testcase, String endpoint, String scenario) { + testcase.setEndpoint(endpoint); + // VTP workaround. + if (testcase.getScenario() == null) { + testcase.setScenario(scenario); + } + + // if no inputs, return. + if (testcase.getInputs() == null) { + return; + } + + // to work around a VTP limitation, + // any inputs that are marked as internal should not be sent to the client. + testcase.setInputs(testcase.getInputs() + .stream() + .filter(input -> (input.getMetadata() == null) || + (!input.getMetadata().containsKey("internal")) || + !"true".equals(input.getMetadata().get("internal").toString())).collect(Collectors.toList())); + } + + /** + * Add the test suite to the tree at the appropriate place if it does not already exist in the tree. + * @param root root of the tree. + * @param scenario scenario under which this suite should be placed + * @param suite test suite to add. + */ + private void addSuiteToTree(final TestTreeNode root, final VtpNameDescriptionPair scenario, final VtpNameDescriptionPair suite) { + findNamedChild(root, scenario.getName()).ifPresent(parent -> { + if (parent.getChildren() == null) { + parent.setChildren(new ArrayList<>()); + } + if (parent.getChildren().stream().noneMatch(n -> StringUtils.equals(n.getName(), suite.getName()))) { + parent.getChildren().add(new TestTreeNode(suite.getName(), suite.getDescription())); + } + }); + } + + /** + * Add the scenario to the tree if it does not already exist. + * @param root root of the tree. + * @param s scenario to add. + */ + private void addScenarioToTree(TestTreeNode root, VtpNameDescriptionPair s) { + logger.debug("addScenario {} to {} with {}", s.getName(), root.getName(), root.getChildren()); + if (root.getChildren() == null) { + root.setChildren(new ArrayList<>()); + } + if (root.getChildren().stream().noneMatch(n->StringUtils.equals(n.getName(),s.getName()))) { + logger.debug("createScenario {} in {}", s.getName(), root.getName()); + root.getChildren().add(new TestTreeNode(s.getName(), s.getDescription())); + } + } + + /** + * Get the list of endpoints defined to the testing manager. + * @return list of endpoints or empty list if the manager is not configured. + */ + public List getEndpoints() { + if (accessConfig != null) { + return accessConfig.getEndpoints().stream() + .filter(RemoteTestingEndpointDefinition::isEnabled) + .map(e -> new VtpNameDescriptionPair(e.getId(), e.getTitle())) + .collect(Collectors.toList()); + } + else { + return new ArrayList<>(); + } + } + + /** + * Get the list of scenarios at a given endpoint. + */ + public List getScenarios(final String endpoint) { + String url = buildEndpointUrl(VTP_SCENARIOS_URI, endpoint, ArrayUtils.EMPTY_STRING_ARRAY); + ParameterizedTypeReference> t = new ParameterizedTypeReference>() {}; + List rv = proxyGetRequestToExternalTestingSite(url, t); + if (rv == null) { + rv = new ArrayList<>(); + } + return rv; + } + + /** + * Get the list of test suites for an endpoint for the given scenario. + */ + public List getTestSuites(final String endpoint, final String scenario) { + String url = buildEndpointUrl(VTP_TESTSUITE_URI, endpoint, new String[] {scenario}); + ParameterizedTypeReference> t = new ParameterizedTypeReference>() {}; + List rv = proxyGetRequestToExternalTestingSite(url, t); + if (rv == null) { + rv = new ArrayList<>(); + } + return rv; + } + + /** + * Get the list of test cases under a scenario. This is the VTP API. It would + * seem better to get the list of cases under a test suite but that is not supported. + */ + @Override + public List getTestCases(String endpoint, String scenario) { + String url = buildEndpointUrl(VTP_TESTCASES_URI, endpoint, new String[] {scenario}); + ParameterizedTypeReference> t = new ParameterizedTypeReference>() {}; + List rv = proxyGetRequestToExternalTestingSite(url, t); + if (rv == null) { + rv = new ArrayList<>(); + } + return rv; + } + + /** + * Get a test case definition. + */ + @Override + public VtpTestCase getTestCase(String endpoint, String scenario, String testSuite, String testCaseName) { + String url = buildEndpointUrl(VTP_TESTCASE_URI, endpoint, new String[] {scenario, testSuite, testCaseName}); + ParameterizedTypeReference t = new ParameterizedTypeReference() {}; + return proxyGetRequestToExternalTestingSite(url, t); + } + + /** + * Return the results of a previous test execution. + * @param endpoint endpoint to query + * @param executionId execution to query. + * @return execution response from testing endpoint. + */ + @Override + public VtpTestExecutionResponse getExecution(String endpoint,String executionId) { + String url = buildEndpointUrl(VTP_EXECUTION_URI, endpoint, new String[] {executionId}); + ParameterizedTypeReference t = new ParameterizedTypeReference() {}; + return proxyGetRequestToExternalTestingSite(url, t); + } + + /** + * Execute a set of tests at a given endpoint. + * @param endpointName name of the endpoint + * @param testsToRun set of tests to run + * @return list of execution responses. + */ + private List execute(final String endpointName, final List testsToRun, String requestId) { + if (accessConfig == null) { + throw new ExternalTestingException(INVALIDATE_STATE_ERROR, 500, NO_ACCESS_CONFIGURATION_DEFINED); + } + + RemoteTestingEndpointDefinition endpoint = accessConfig.getEndpoints().stream() + .filter(e -> StringUtils.equals(endpointName, e.getId())) + .findFirst() + .orElseThrow(() -> new ExternalTestingException("No such endpoint", 500, "No endpoint named " + endpointName + " is defined")); + + // if the endpoint requires an API key, specify it in the headers. + HttpHeaders headers = new HttpHeaders(); + if (endpoint.getApiKey() != null) { + headers.add("X-API-Key", endpoint.getApiKey()); + } + headers.setContentType(MediaType.MULTIPART_FORM_DATA); + + // build the body. + MultiValueMap body = new LinkedMultiValueMap<>(); + try { + // remove the endpoint from the test request since that is a FE/BE attribute + // add the execution profile configured for the endpoint. + testsToRun.forEach(t -> { + t.setEndpoint(null); + t.setProfile(t.getScenario()); // VTP wants a profile. Use the scenario name. + }); + + body.add("executions", new ObjectMapper().writeValueAsString(testsToRun)); + } + catch (Exception ex) { + logger.error("exception converting tests to string", ex); + VtpTestExecutionResponse err = new VtpTestExecutionResponse(); + err.setHttpStatus(500); + err.setCode("500"); + err.setMessage("Execution failed due to " + ex.getMessage()); + return Collections.singletonList(err); + } + + for(VtpTestExecutionRequest test: testsToRun) { + runVariableResolvers(test, body); + } + + + // form and send request. + HttpEntity> requestEntity = new HttpEntity<>(body, headers); + String url = buildEndpointUrl(VTP_EXECUTIONS_URI, endpointName, ArrayUtils.EMPTY_STRING_ARRAY); + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url); + if (requestId != null) { + builder = builder.queryParam("requestId", requestId); + } + ParameterizedTypeReference> t = new ParameterizedTypeReference>() {}; + try { + return proxyRequestToExternalTestingSite(builder.toUriString(), requestEntity, t); + } + catch (ExternalTestingException ex) { + logger.error("exception caught invoking endpoint {}", endpointName, ex); + VtpTestExecutionResponse err = new VtpTestExecutionResponse(); + err.setHttpStatus(ex.getCode()); + err.setCode(""+ex.getCode()); + err.setMessage(ex.getTitle() + ": " + ex.getDetail()); + return Collections.singletonList(err); + } + } + + /** + * Execute tests splitting them across endpoints and collecting the results. + * @param testsToRun list of tests to be executed. + * @return collection of result objects. + */ + @Override + public List execute(final List testsToRun, String requestId) { + if (accessConfig == null) { + throw new ExternalTestingException(INVALIDATE_STATE_ERROR, 500, NO_ACCESS_CONFIGURATION_DEFINED); + } + + // partition the requests by endpoint. + Map> partitions = + testsToRun.stream().collect(Collectors.groupingBy(VtpTestExecutionRequest::getEndpoint)); + + // process each group and collect the results. + return partitions.entrySet().stream() + .flatMap(e -> execute(e.getKey(), e.getValue(), requestId).stream()) + .collect(Collectors.toList()); + } + + /** + * Load the external testing access configuration from the SDC onboarding yaml configuration file. + * @param file filename to retrieve data from + * @return parsed YAML object + * @throws IOException thrown if failure in reading YAML content. + */ + private Object getExternalTestingAccessConfiguration(String file) throws IOException { + Map configuration = Objects.requireNonNull(readConfigurationFile(file), "Configuration cannot be empty"); + Object testingConfig = configuration.get(CONFIG_SECTION); + if (testingConfig == null) { + logger.warn("Unable to initialize external testing access configuration. Add 'testingConfig' to configuration.yaml with url value. Feature will be hobbled with results hardcoded to empty values."); + } + + return testingConfig; + } + + /** + * Load the onboarding yaml config file. + * @param file name of file to load + * @return map containing YAML properties. + * @throws IOException thrown in the event of YAML parse or IO failure. + */ + private static Map readConfigurationFile(String file) throws IOException { + try (InputStream fileInput = new FileInputStream(file)) { + YamlUtil yamlUtil = new YamlUtil(); + return yamlUtil.yamlToMap(fileInput); + } + } + + + /** + * Return URL with endpoint url as prefix. + * @param format format string. + * @param endpointName endpoint to address + * @param args args for format. + * @return qualified url. + */ + private String buildEndpointUrl(String format, String endpointName, String[] args) { + if (accessConfig != null) { + RemoteTestingEndpointDefinition ep = endpoints.values().stream() + .filter(e -> e.getId().equals(endpointName)) + .findFirst() + .orElseThrow(() -> new ExternalTestingException("No such endpoint", 500, "No endpoint named " + endpointName + " is defined") + ); + + Object[] newArgs = ArrayUtils.add(args, 0, ep.getUrl()); + return String.format(format, newArgs); + } + throw new ExternalTestingException(INVALIDATE_STATE_ERROR, 500, NO_ACCESS_CONFIGURATION_DEFINED); + } + + /** + * Proxy a get request to a testing endpoint. + * @param url URL to invoke. + * @param responseType type of response expected. + * @param type of response expected + * @return instance of parsed from the JSON response from endpoint. + */ + private T proxyGetRequestToExternalTestingSite(String url, ParameterizedTypeReference responseType) { + return proxyRequestToExternalTestingSite(url, null, responseType); + } + + /** + * Make the actual HTTP post (using Spring RestTemplate) to an endpoint. + * @param url URL to the endpoint + * @param request optional request body to send + * @param responseType expected type + * @param extended type + * @return instance of expected type + */ + private T proxyRequestToExternalTestingSite(String url, HttpEntity request, ParameterizedTypeReference responseType) { + if (request != null) { + logger.debug("POST request to {} with {} for {}", url, request, responseType.getType().getTypeName()); + } + else { + logger.debug("GET request to {} for {}", url, responseType.getType().getTypeName()); + } + SimpleClientHttpRequestFactory rf = + (SimpleClientHttpRequestFactory) restTemplate.getRequestFactory(); + if (rf != null) { + rf.setReadTimeout(10000); + rf.setConnectTimeout(10000); + } + ResponseEntity re; + try { + if (request != null) { + re = restTemplate.exchange(url, HttpMethod.POST, request, responseType); + } else { + re = restTemplate.exchange(url, HttpMethod.GET, null, responseType); + } + } + catch (HttpStatusCodeException ex) { + // make my own exception out of this. + logger.warn("Unexpected HTTP Status from endpoint {}", ex.getRawStatusCode()); + if ((ex.getResponseHeaders().getContentType() != null) && + ((ex.getResponseHeaders().getContentType().isCompatibleWith(MediaType.APPLICATION_JSON)) || + (ex.getResponseHeaders().getContentType().isCompatibleWith(MediaType.parseMediaType("application/problem+json"))))) { + String s = ex.getResponseBodyAsString(); + logger.warn("endpoint body content is {}", s); + try { + JsonObject o = new GsonBuilder().create().fromJson(s, JsonObject.class); + throw buildTestingException(ex.getRawStatusCode(), o); + } + catch (JsonParseException e) { + logger.warn("unexpected JSON response", e); + throw new ExternalTestingException(ex.getStatusText(), ex.getStatusCode().value(), ex.getResponseBodyAsString(), ex); + } + } + else { + throw new ExternalTestingException(ex.getStatusText(), ex.getStatusCode().value(), ex.getResponseBodyAsString(), ex); + } + } + catch (ResourceAccessException ex) { + throw new ExternalTestingException("IO Error at Endpoint", 500, ex.getMessage(), ex); + } + catch (Exception ex) { + throw new ExternalTestingException(ex.getMessage(), 500, "Generic Exception", ex); + } + if (re != null) { + logger.debug("http status of {} from external testing entity {}", re.getStatusCodeValue(), url); + return re.getBody(); + } + else { + logger.error("null response from endpoint"); + return null; + } + } + + /** + * Errors from the endpoint could conform to the expected ETSI body or not. + * Here we try to handle various response body elements. + * @param statusCode http status code in response. + * @param o JSON object parsed from the http response body + * @return Testing error body that should be returned to the caller + */ + private ExternalTestingException buildTestingException(int statusCode, JsonObject o) { + String code = null; + String message = null; + + if (o.has(CODE)) { + code = o.get(CODE).getAsString(); + } + else if (o.has(ERROR)) { + code = o.get(ERROR).getAsString(); + } + else { + if (o.has(HTTP_STATUS)) { + code = o.get(HTTP_STATUS).getAsJsonPrimitive().getAsString(); + } + } + if (o.has(MESSAGE)) { + if (!o.get(MESSAGE).isJsonNull()) { + message = o.get(MESSAGE).getAsString(); + } + } + else if (o.has(DETAIL)) { + message = o.get(DETAIL).getAsString(); + } + if (o.has(PATH)) { + if (message == null) { + message = o.get(PATH).getAsString(); + } + else { + message = message + " " + o.get(PATH).getAsString(); + } + } + return new ExternalTestingException(code, statusCode, message); + } + + /** + * Resolve variables in the request calling the built-in variable resolvers. + * @param item test execution request item to be resolved. + */ + private void runVariableResolvers(final VtpTestExecutionRequest item, final MultiValueMap body) { + variableResolvers.forEach(vr -> { + if (vr.resolvesVariablesForRequest(item)) { + vr.resolve(item, body); + } + }); + } +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/RemoteTestingEndpointDefinition.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/RemoteTestingEndpointDefinition.java new file mode 100644 index 0000000000..205ca29680 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/RemoteTestingEndpointDefinition.java @@ -0,0 +1,39 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import lombok.Data; + +import java.util.regex.Pattern; + +@Data +class RemoteTestingEndpointDefinition { + private boolean enabled; + private String title; + private String url; + private String id; + private String apiKey; + private String scenarioFilter; + private Pattern scenarioFilterPattern; + + Pattern getScenarioFilterPattern() { + if ((scenarioFilterPattern == null) && (scenarioFilter != null)) { + scenarioFilterPattern = Pattern.compile(scenarioFilter); + } + return scenarioFilterPattern; + } +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/TestingAccessConfig.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/TestingAccessConfig.java new file mode 100644 index 0000000000..f9df5ac81d --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/TestingAccessConfig.java @@ -0,0 +1,30 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import lombok.Data; + +import java.util.List; + +@SuppressWarnings({"unused", "WeakerAccess"}) +@Data +public class TestingAccessConfig { + + private ClientConfiguration client; + private List endpoints; + +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/VariableResolver.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/VariableResolver.java new file mode 100644 index 0000000000..3079898bff --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/java/org/openecomp/core/externaltesting/impl/VariableResolver.java @@ -0,0 +1,27 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import org.openecomp.core.externaltesting.api.VtpTestExecutionRequest; +import org.springframework.util.MultiValueMap; + +public interface VariableResolver { + + boolean resolvesVariablesForRequest(VtpTestExecutionRequest requestItem); + + void resolve(VtpTestExecutionRequest requestItem, MultiValueMap body); +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/resources/factoryConfiguration.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/resources/factoryConfiguration.json new file mode 100644 index 0000000000..10449038bf --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/main/resources/factoryConfiguration.json @@ -0,0 +1,3 @@ +{ + "org.openecomp.core.externaltesting.factory.ExternalTestingManagerFactory":"org.openecomp.sdc.externaltesting.impl.ExternalTestingManagerFactoryImpl" +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/csar.zip b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/csar.zip new file mode 100644 index 0000000000..52de39a93e Binary files /dev/null and b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/csar.zip differ diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/fulldefinition.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/fulldefinition.json new file mode 100644 index 0000000000..3d90bfd007 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/fulldefinition.json @@ -0,0 +1,26 @@ +{ + "id": "certquery", + "title": "VSP Certifications", + "parameters": [ + { + "id": "vspId", + "label": "VSP ID", + "inputType": "text", + "maxLength": 36, + "minLength": 1, + "placeholder": "VSP ID", + "disabled": false, + "required": true + }, + { + "id": "vspVersion", + "label": "VSP Version", + "inputType": "text", + "maxLength": 36, + "minLength": 1, + "placeholder": "VSP Version", + "disabled": false, + "required": true + } + ] +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/managertestconfiguration.yaml b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/managertestconfiguration.yaml new file mode 100644 index 0000000000..d65becedf9 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/managertestconfiguration.yaml @@ -0,0 +1,13 @@ +externalTestingConfig: + client: + enabled: true + endpoints: + - id: vtp + enabled: true + url: http://bogus.ec2-34-237-35-152.compute-1.amazonaws.com + apiKey: FOOBAR + scenarioFilter: c.* + - id: repository + url: http://bogus.ec2-34-237-35-152.compute-1.amazonaws.com + enabled: true + apiKey: FOOBAR diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/notfound.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/notfound.json new file mode 100644 index 0000000000..eef023da6c --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/notfound.json @@ -0,0 +1,6 @@ +{ + "title": "Test not found.", + "error": "Test not found.", + "detail": "This is the detail error text.", + "message": "This is the message field." +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/priorexecution.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/priorexecution.json new file mode 100644 index 0000000000..ca01d3676a --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/priorexecution.json @@ -0,0 +1,21 @@ +{ + "scenario": "compliance", + "testCaseName": "compliance.compliancetests.sriov", + "testSuiteName": "compliancetests", + "executionId": "621c97a1-045c-48bd-baeb-3678b6a97080-1552081390570", + "parameters": { + "vspId": "e1b2ce6d61604a9db5c9845db3a7d6ab", + "vspVersion": "bc248d7f8af24d60a8dc3f0ddb98efca", + "allowSriov": "true", + "csar:MainServiceTemplate.yaml": "", + "csp": "ZZFT", + "profilespec": "gsmafnw14", + "vnftype": "B" + }, + "results": { + "this is": "a fake result" + }, + "status": "COMPLETED", + "startTime": "2019-03-08T21:43:10.527", + "endTime": "2019-03-08T21:43:10.617" +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/runresult.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/runresult.json new file mode 100644 index 0000000000..f729f482ee --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/runresult.json @@ -0,0 +1,22 @@ +[ + { + "scenario": "compliance", + "testCaseName": "computeflavors", + "testSuiteName": "compliancetests", + "executionId": "3053ed10-84e6-4f21-aa62-4ca66242d8d8", + "parameters": { + "vspId": "VSP-ID", + "vspVersion": "VSP-VER", + "allowSriov": "true", + "csp": "ZZFT", + "profilespec": "gsmafnw14", + "vnftype": "B" + }, + "results": { + "hello": "world" + }, + "status": "SUCCESS", + "startTime": 1550780567585, + "endTime": 1550780567585 + } +] \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/scenarios.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/scenarios.json new file mode 100644 index 0000000000..e345754e21 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/scenarios.json @@ -0,0 +1,10 @@ +[ + { + "name": "certification", + "description": "Available Certification Queries" + }, + { + "name": "compliance", + "description": "Available Compliance Tests" + } +] \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testcase-sriov.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testcase-sriov.json new file mode 100644 index 0000000000..3264216bc3 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testcase-sriov.json @@ -0,0 +1,48 @@ +{ + "scenario": "compliance", + "testCaseName": "sriov", + "testSuiteName": "compliancetests", + "description": "SR-IOV Test", + "author": "Jim", + "inputs": [ + { + "name": "vspId", + "description": "VSP ID", + "type": "text", + "metadata": { + "isDisabled": true, + "maxLength": "36", + "minLength": "1" + } + }, + { + "name": "vspVersion", + "description": "VSP Version", + "type": "text", + "metadata": { + "isDisabled": true, + "maxLength": "36", + "minLength": "1" + } + }, + { + "name": "allowSriov", + "description": "Allow SRIOV?", + "type": "select", + "defaultValue": "false", + "metadata": { + "isDisabled": true, + "choices": [ + { + "key": "true", + "label": "Yes" + }, + { + "key": "false", + "label": "No" + } + ] + } + } + ] +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testcases.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testcases.json new file mode 100644 index 0000000000..2bb6414b89 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testcases.json @@ -0,0 +1,16 @@ +[ + { + "scenario": "compliance", + "testCaseName": "sriov", + "testSuiteName": "compliancetests", + "description": "SR-IOV Test", + "author": "Jim" + }, + { + "scenario": "compliance", + "testCaseName": "computeflavors", + "testSuiteName": "compliancetests", + "description": "Compute Flavours Test", + "author": "Jim" + } +] \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testconfiguration.yaml b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testconfiguration.yaml new file mode 100644 index 0000000000..1bf800b8f2 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testconfiguration.yaml @@ -0,0 +1,11 @@ + client: + enabled: true + endpoints: + - id: vtp + enabled: false + url: http://bogus.ec2-34-237-35-152.compute-1.amazonaws.com + apiKey: FOOBAR + - id: repository + url: http://bogus.ec2-34-237-35-152.compute-1.amazonaws.com + enabled: true + apiKey: FOOBAR diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testsuites.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testsuites.json new file mode 100644 index 0000000000..05d6c50366 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testsuites.json @@ -0,0 +1,6 @@ +[ + { + "name": "compliancetests", + "description": "Compliance Tests" + } +] \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testtree.json b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testtree.json new file mode 100644 index 0000000000..06b021077f --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/data/testtree.json @@ -0,0 +1,119 @@ +{ + "name": "root", + "description": "root", + "tests": [], + "children": [ + { + "name": "certification", + "description": "Available Certification Queries", + "children": [ + { + "name": "certificationtests", + "description": "Certification Tests", + "tests": [ + { + "scenario": "certification", + "testCaseName": "certquery", + "testSuiteName": "certificationtests", + "description": "VSP Certifications", + "author": "jguistwite@iconectiv.com", + "inputs": [ + { + "name": "vspId", + "description": "VSP ID", + "type": "text", + "defaultValue": "", + "isOptional": false, + "metadata": { + "maxLength": 36.0, + "minLength": 1.0, + "disabled": true + } + }, + { + "name": "vspVersion", + "description": "Previous VSP Version", + "type": "text", + "defaultValue": "", + "isOptional": false, + "metadata": { + "maxLength": 36.0, + "minLength": 1.0, + "disabled": true + } + } + ], + "endpoint": "opnfv" + } + ], + "children": [] + } + ] + }, + { + "name": "compliance", + "description": "Available Compliance Tests", + "tests": [], + "children": [ + { + "name": "compliancetests", + "description": "Compliance Tests", + "tests": [ + { + "scenario": "compliance", + "testCaseName": "sriov", + "testSuiteName": "compliancetests", + "description": "SR-IOV Test", + "author": "Jim", + "inputs": [ + { + "name": "vspId", + "description": "VSP ID", + "type": "text", + "isOptional": false, + "metadata": { + "isDisabled": true, + "maxLength": "36", + "minLength": "1" + } + }, + { + "name": "vspVersion", + "description": "VSP Version", + "type": "text", + "isOptional": false, + "metadata": { + "isDisabled": true, + "maxLength": "36", + "minLength": "1" + } + }, + { + "name": "allowSriov", + "description": "Allow SRIOV?", + "type": "select", + "defaultValue": "false", + "isOptional": false, + "metadata": { + "isDisabled": true, + "choices": [ + { + "key": "true", + "label": "Yes" + }, + { + "key": "false", + "label": "No" + } + ] + } + } + ], + "endpoint": "vtp" + } + ] + } + ] + } + ] +} \ No newline at end of file diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ConfigurationTests.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ConfigurationTests.java new file mode 100644 index 0000000000..6b530daa09 --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ConfigurationTests.java @@ -0,0 +1,78 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.Assert; +import org.junit.Test; +import org.onap.sdc.tosca.services.YamlUtil; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; + +public class ConfigurationTests { + + @Test + public void testClientConfig() { + // a brain dead test of the setter and getter. + // future tests for more complex config to come. + ClientConfiguration cc = new ClientConfiguration(); + cc.setEnabled(true); + Assert.assertTrue("client configuration setter", cc.isEnabled()); + cc.setEnabled(false); + Assert.assertFalse("client configuration setter", cc.isEnabled()); + } + + @Test + public void testConfig() throws Exception { + try (InputStream fileInput = new FileInputStream(new File("src/test/data/testconfiguration.yaml"))) { + YamlUtil yamlUtil = new YamlUtil(); + Object raw = yamlUtil.yamlToMap(fileInput); + TestingAccessConfig accessConfig = new ObjectMapper().convertValue(raw, TestingAccessConfig.class); + Assert.assertNotNull("client config available", accessConfig.getClient()); + } + } + + @Test + public void testEndpointDefinition() { + RemoteTestingEndpointDefinition def = new RemoteTestingEndpointDefinition(); + def.setId("vtp"); + def.setEnabled(true); + def.setTitle("VTP"); + def.setApiKey("FOOBARBAZ"); + def.setUrl("http://example.com/vtptesting"); + def.setScenarioFilter("c.*"); + + RemoteTestingEndpointDefinition def2 = new RemoteTestingEndpointDefinition(); + def2.setId("vtp"); + def2.setEnabled(true); + def2.setTitle("VTP"); + def2.setUrl("http://example.com/vtptesting"); + def2.setApiKey("FOOBARBAZ"); + def2.setScenarioFilter("c.*"); + + Assert.assertEquals("code", "VTP", def.getTitle()); + Assert.assertEquals("API keys equals", def.getApiKey(), def2.getApiKey()); + Assert.assertEquals("code equals", def.getTitle(), def2.getTitle()); + Assert.assertEquals("url equals", def.getUrl(), def2.getUrl()); + + boolean matches = def.getScenarioFilterPattern().matcher("certification").matches(); + Assert.assertTrue("pattern", matches); + + } +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/CsarMetadataVariableResolverTest.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/CsarMetadataVariableResolverTest.java new file mode 100644 index 0000000000..b8471eb77b --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/CsarMetadataVariableResolverTest.java @@ -0,0 +1,88 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import org.apache.commons.io.IOUtils; +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.openecomp.core.externaltesting.api.VtpTestExecutionRequest; +import org.openecomp.core.externaltesting.errors.ExternalTestingException; +import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager; +import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager; +import org.openecomp.sdc.versioning.VersioningManager; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; + +import java.io.FileInputStream; +import java.util.*; + +public class CsarMetadataVariableResolverTest { + + @Mock + private VersioningManager versioningManager; + + @Mock + private VendorSoftwareProductManager vendorSoftwareProductManager; + + @Mock + private OrchestrationTemplateCandidateManager candidateManager; + + @Test + public void testResolverResolves() throws Exception { + MockitoAnnotations.initMocks(this); + CsarMetadataVariableResolver resolver = new CsarMetadataVariableResolver(versioningManager, + vendorSoftwareProductManager, candidateManager); + resolver.init(); + + VtpTestExecutionRequest doesNotResolve = new VtpTestExecutionRequest(); + Assert.assertFalse("should not resolve empty request", resolver.resolvesVariablesForRequest(doesNotResolve)); + + doesNotResolve.setParameters(new HashMap<>()); + Assert.assertFalse("should not resolve empty parameters", resolver.resolvesVariablesForRequest(doesNotResolve)); + + + + VtpTestExecutionRequest resolves = new VtpTestExecutionRequest(); + resolves.setParameters(new HashMap<>()); + resolves.getParameters().put(CsarMetadataVariableResolver.VSP_VERSION, "1.0"); + resolves.getParameters().put(CsarMetadataVariableResolver.VSP_ID, "vspid"); + resolves.getParameters().put(CsarMetadataVariableResolver.CSAR_PREFIX + "MainServiceTemplate.yaml", ""); + Assert.assertTrue("should resolve", resolver.resolvesVariablesForRequest(resolves)); + + MultiValueMap fakeRequestBody = new LinkedMultiValueMap<>(); + + try { + resolver.resolve(resolves, fakeRequestBody); + } + catch (ExternalTestingException e) { + // exception expected. + } + + // test the metadata extraction on a know CSAR zip. + byte[] zip = IOUtils.toByteArray(new FileInputStream("src/test/data/csar.zip")); + resolver.processArchive(resolves, fakeRequestBody, zip); + Assert.assertTrue("body contains file", fakeRequestBody.containsKey("file")); + LinkedList ll = (LinkedList)fakeRequestBody.get("file"); + Assert.assertEquals("body contains one file", 1, ll.size()); + ByteArrayResource res = (ByteArrayResource)ll.get(0); + Assert.assertEquals("file should have matching name", "MainServiceTemplate.yaml", res.getFilename()); + + } +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImplTests.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImplTests.java new file mode 100644 index 0000000000..be328ea73f --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ExternalTestingManagerImplTests.java @@ -0,0 +1,283 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.commons.io.FileUtils; +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.ArgumentMatchers; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnitRunner; +import org.openecomp.core.externaltesting.api.*; +import org.openecomp.core.externaltesting.errors.ExternalTestingException; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.util.MultiValueMap; +import org.springframework.web.client.HttpServerErrorException; +import org.springframework.web.client.HttpStatusCodeException; +import org.springframework.web.client.ResourceAccessException; +import org.springframework.web.client.RestTemplate; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.Charset; +import java.util.*; + +@RunWith(MockitoJUnitRunner.class) +public class ExternalTestingManagerImplTests { + + @Mock + private RestTemplate restTemplate; + + static { + System.setProperty("configuration.yaml", "src/test/data/testconfiguration.yaml"); + } + + class JUnitExternalTestingManagerImpl extends ExternalTestingManagerImpl { + JUnitExternalTestingManagerImpl() { + super(Collections.singletonList( + new VariableResolver() { + + @Override + public boolean resolvesVariablesForRequest(VtpTestExecutionRequest requestItem) { + return false; + } + + @Override + public void resolve(VtpTestExecutionRequest requestItem, MultiValueMap body) { + + // unit test resolver does nothing for this case. See specific test for resolver. + } + })); + } + } + + @InjectMocks + private ExternalTestingManager mgr = new JUnitExternalTestingManagerImpl(); + + @SuppressWarnings("unchecked") + private ExternalTestingManager configTestManager(boolean loadConfig) throws IOException { + if (loadConfig) { + ((ExternalTestingManagerImpl) mgr).loadConfig(); + } + + ObjectMapper mapper = new ObjectMapper(); + + // read mock data for API calls. + + File scenarioFile = new File("src/test/data/scenarios.json"); + TypeReference> typ = new TypeReference>(){}; + List scenarios = mapper.readValue(scenarioFile, typ); + + File testSuitesFile = new File("src/test/data/testsuites.json"); + List testSuites = mapper.readValue(testSuitesFile, new TypeReference>(){}); + + File testCasesFile = new File("src/test/data/testcases.json"); + List testCases = mapper.readValue(testCasesFile, new TypeReference>(){}); + + File testCaseFile = new File("src/test/data/testcase-sriov.json"); + VtpTestCase testCase = mapper.readValue(testCaseFile, VtpTestCase.class); + + File runResultFile = new File("src/test/data/runresult.json"); + List runResults = mapper.readValue(runResultFile, new TypeReference>(){}); + + File priorExecutionFile = new File("src/test/data/priorexecution.json"); + VtpTestExecutionResponse priorExecution = mapper.readValue(priorExecutionFile, VtpTestExecutionResponse.class); + + // create an error response as well + String notFound = FileUtils.readFileToString(new File("src/test/data/notfound.json"), "UTF-8"); + + ParameterizedTypeReference> listOfPairType = new ParameterizedTypeReference>() {}; + ParameterizedTypeReference> listOfCasesType = new ParameterizedTypeReference>() {}; + ParameterizedTypeReference caseType = new ParameterizedTypeReference() {}; + + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.parseMediaType("application/problem+json")); + HttpStatusCodeException missingException = new HttpServerErrorException(HttpStatus.NOT_FOUND, "Not Found", headers, notFound.getBytes(), Charset.defaultCharset()); + + Mockito + .when(restTemplate.exchange( + ArgumentMatchers.endsWith("/scenarios"), + ArgumentMatchers.eq(HttpMethod.GET), + ArgumentMatchers.any(), + ArgumentMatchers.eq(listOfPairType))) + .thenReturn(new ResponseEntity(scenarios, HttpStatus.OK)); + + Mockito + .when(restTemplate.exchange( + ArgumentMatchers.endsWith("/testsuites"), + ArgumentMatchers.eq(HttpMethod.GET), + ArgumentMatchers.any(), + ArgumentMatchers.eq(listOfPairType))) + .thenReturn(new ResponseEntity(testSuites, HttpStatus.OK)); + + Mockito + .when(restTemplate.exchange( + ArgumentMatchers.endsWith("/testcases"), + ArgumentMatchers.eq(HttpMethod.GET), + ArgumentMatchers.any(), + ArgumentMatchers.eq(listOfCasesType))) + .thenReturn(new ResponseEntity(testCases, HttpStatus.OK)); + + Mockito + .when(restTemplate.exchange( + ArgumentMatchers.endsWith("/sriov"), + ArgumentMatchers.eq(HttpMethod.GET), + ArgumentMatchers.any(), + ArgumentMatchers.eq(caseType))) + .thenReturn(new ResponseEntity(testCase, HttpStatus.OK)); + + + // POST for execution + + Mockito + .when(restTemplate.exchange( + ArgumentMatchers.contains("executions"), + ArgumentMatchers.eq(HttpMethod.POST), + ArgumentMatchers.any(), + ArgumentMatchers.eq(new ParameterizedTypeReference>() {}))) + .thenReturn(new ResponseEntity(runResults, HttpStatus.OK)); + + + Mockito + .when(restTemplate.exchange( + ArgumentMatchers.contains("/executions/"), + ArgumentMatchers.eq(HttpMethod.GET), + ArgumentMatchers.any(), + ArgumentMatchers.eq(new ParameterizedTypeReference() {}))) + .thenReturn(new ResponseEntity(priorExecution, HttpStatus.OK)); + + Mockito + .when(restTemplate.exchange( + ArgumentMatchers.endsWith("/missing"), + ArgumentMatchers.eq(HttpMethod.GET), + ArgumentMatchers.any(), + ArgumentMatchers.eq(caseType))) + .thenThrow(missingException); + + Mockito + .when(restTemplate.exchange( + ArgumentMatchers.endsWith("/sitedown"), + ArgumentMatchers.eq(HttpMethod.GET), + ArgumentMatchers.any(), + ArgumentMatchers.eq(caseType))) + .thenThrow(new ResourceAccessException("Remote site is down")); + + return mgr; + } + + @Test + public void testManager() throws IOException { + System.setProperty("configuration.yaml", "src/test/data/managertestconfiguration.yaml"); + ExternalTestingManager m = configTestManager(true); + + String config = m.getConfig(); + Assert.assertNotNull(config); + + List endpoints = m.getEndpoints(); + Assert.assertEquals("two endpoints", 2, endpoints.size()); + + + // this will exercise the internal APIs as well. + TestTreeNode root = m.getTestCasesAsTree(); + Assert.assertEquals("two scenarios", 2, root.getChildren().size()); + + + // handle case where remote endpoint is down. + try { + m.getTestCase("repository", "scen", "suite", "sitedown"); + Assert.fail("not expected to retrieve sitedown test case"); + } + catch (ExternalTestingException e) { + // expecting this exception. + Assert.assertNotNull(e.getDetail()); + Assert.assertNotEquals(0, e.getCode()); + Assert.assertNotNull(e.getTitle()); + } + + // get a particular test case + try { + m.getTestCase("repository", "scen", "suite", "missing"); + Assert.fail("not expected to retrieve missing test case"); + } + catch (ExternalTestingException e) { + // expecting this exception. + Assert.assertNotNull(e.getDetail()); + Assert.assertNotEquals(0, e.getCode()); + Assert.assertNotNull(e.getTitle()); + } + + + // execute a test. + List requests = new ArrayList<>(); + VtpTestExecutionRequest req = new VtpTestExecutionRequest(); + req.setEndpoint("repository"); + requests.add(req); + + // send a request with the endpoint defined. + List responses = m.execute( requests, "rid"); + Assert.assertEquals(1,responses.size()); + + // send a request for a prior execution. + VtpTestExecutionResponse execRsp = m.getExecution("repository", "execId"); + Assert.assertEquals("COMPLETED", execRsp.getStatus()); + } + + @Test + public void testManagerErrorCases() throws IOException { + ExternalTestingManager m = configTestManager(false); + Map expectedEmptyConfig = new HashMap<>(); + expectedEmptyConfig.put("enabled", false); + String expected = new ObjectMapper().writeValueAsString(expectedEmptyConfig); + String emptyConfig = m.getConfig(); + Assert.assertEquals(expected, emptyConfig); + + try { + m.getEndpoints(); + Assert.assertTrue("should have exception here", true); + } + catch (ExternalTestingException e) { + // eat the exception cause this is what should happen. + } + } + + @Test + public void testExecutionDistribution() throws IOException { + System.setProperty("configuration.yaml", "src/test/data/managertestconfiguration.yaml"); + ExternalTestingManager m = configTestManager(true); + + VtpTestExecutionRequest r1 = new VtpTestExecutionRequest(); + r1.setScenario("scenario1"); + r1.setEndpoint("vtp"); + + VtpTestExecutionRequest r2 = new VtpTestExecutionRequest(); + r2.setScenario("scenario2"); + r2.setEndpoint("vtp"); + + VtpTestExecutionRequest r3 = new VtpTestExecutionRequest(); + r3.setScenario("scenario3"); + r3.setEndpoint("repository"); + + List results = m.execute(Arrays.asList(r1,r2,r3), "rid"); + Assert.assertEquals("three in two out merged", 2, results.size()); + } +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ExternalTestingTestSuite.java b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ExternalTestingTestSuite.java new file mode 100644 index 0000000000..31346a135e --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/java/org/openecomp/core/externaltesting/impl/ExternalTestingTestSuite.java @@ -0,0 +1,31 @@ +/* + * Copyright © 2019 iconectiv + * + * 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. + */ + +package org.openecomp.core.externaltesting.impl; + +import org.junit.runner.RunWith; +import org.junit.runners.Suite; + +@RunWith(Suite.class) +@Suite.SuiteClasses({ + CsarMetadataVariableResolverTest.class, + ExternalTestingManagerImplTests.class, + ConfigurationTests.class +}) +public class ExternalTestingTestSuite { + // nothing to do - just a placeholder. + +} diff --git a/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/resources/logback-test.xml b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/resources/logback-test.xml new file mode 100644 index 0000000000..e0498971dc --- /dev/null +++ b/openecomp-be/lib/openecomp-sdc-externaltesting-lib/openecomp-sdc-externaltesting-impl/src/test/resources/logback-test.xml @@ -0,0 +1,30 @@ + + + + + + + + %d{dd-MMM-yyyy HH:mm:ss:SSS} %-5level %logger{36}.%M\(%line\) - %msg%n + + + + + + + + -- cgit 1.2.3-korg