diff options
Diffstat (limited to 'csarvalidation/src')
-rw-r--r-- | csarvalidation/src/main/java/org/onap/cvc/csar/VTPValidateCSAR.java | 2 | ||||
-rw-r--r-- | csarvalidation/src/test/java/org/onap/cvc/csar/CsarValidatorTest.java | 115 |
2 files changed, 102 insertions, 15 deletions
diff --git a/csarvalidation/src/main/java/org/onap/cvc/csar/VTPValidateCSAR.java b/csarvalidation/src/main/java/org/onap/cvc/csar/VTPValidateCSAR.java index 30eb043..043d4dc 100644 --- a/csarvalidation/src/main/java/org/onap/cvc/csar/VTPValidateCSAR.java +++ b/csarvalidation/src/main/java/org/onap/cvc/csar/VTPValidateCSAR.java @@ -237,7 +237,7 @@ public class VTPValidateCSAR extends OnapCommand { } } - result.setPassed(overallPass); + result.setPassed(result.getErrors().isEmpty()); validation.getResults().add(result); } catch (Exception e) { result.setPassed(false); diff --git a/csarvalidation/src/test/java/org/onap/cvc/csar/CsarValidatorTest.java b/csarvalidation/src/test/java/org/onap/cvc/csar/CsarValidatorTest.java index 2969b42..2c39f74 100644 --- a/csarvalidation/src/test/java/org/onap/cvc/csar/CsarValidatorTest.java +++ b/csarvalidation/src/test/java/org/onap/cvc/csar/CsarValidatorTest.java @@ -1,12 +1,13 @@ /** * Copyright 2017 Huawei Technologies Co., Ltd. - * + * Copyright 2020 Nokia + * <p> * 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 - * + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> * 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. @@ -16,7 +17,13 @@ package org.onap.cvc.csar; +import org.apache.commons.lang3.StringUtils; +import org.assertj.core.api.Assertions; import org.junit.Test; +import org.onap.cli.fw.cmd.OnapCommand; +import org.onap.cli.fw.error.OnapCommandException; +import org.onap.cli.fw.output.OnapCommandResult; +import org.onap.cli.fw.output.OnapCommandResultAttribute; import org.onap.cli.main.OnapCli; import java.net.URISyntaxException; @@ -27,42 +34,122 @@ import static org.onap.cvc.csar.cc.sol004.IntegrationTestUtils.absoluteFilePath; public class CsarValidatorTest { + public static final String NO_CERTIFICATE_RULE = "r130206"; + public static final String OPERATION_STATUS_FAILED = "FAILED"; @Test - public void testAllTestCasesForVNF() throws URISyntaxException { - OnapCli cli = new OnapCli(new String [] { + public void shouldReportThanVnfValidationFailed() throws URISyntaxException { + // given + OnapCliWrapper cli = new OnapCliWrapper(new String[]{ "--product", "onap-dublin", "csar-validate", "--format", "json", "--csar", absoluteFilePath("VoLTE.csar")}); + + // when cli.handle(); - assertEquals(0, cli.getExitCode()); + + // then + final OnapCommandResult onapCommandResult = cli.getCommandResult(); + verifyThatOperation(onapCommandResult, OPERATION_STATUS_FAILED); + verifyThatXRulesFails(onapCommandResult, 7); + verifyThatOperationFinishedWithoutAnyError(cli); } @Test - public void testAllTestCasesForPNF_CsarCase() throws URISyntaxException { - OnapCli cli = new OnapCli(new String [] { + public void shouldReportThatPnfValidationFailedWhenCsarDoNotHaveCertificate_allOtherRulesShouldPass() throws URISyntaxException { + // given + OnapCliWrapper cli = new OnapCliWrapper(new String[]{ "--product", "onap-dublin", "csar-validate", "--format", "json", "--pnf", - "--csar", absoluteFilePath("pnf/r57019/allMandatoryEntriesDefinedInMetadataManifest.csar")}); + "--csar", absoluteFilePath("pnf/r972082/validFile.csar")}); + // when cli.handle(); - assertEquals(0, cli.getExitCode()); - } + // then + final OnapCommandResult onapCommandResult = cli.getCommandResult(); + verifyThatOperation(onapCommandResult, OPERATION_STATUS_FAILED); + verifyThatXRulesFails(onapCommandResult, 1); + verifyThatRuleFails(onapCommandResult, NO_CERTIFICATE_RULE); + verifyThatOperationFinishedWithoutAnyError(cli); + } @Test - public void testAllTestCasesForPNF_ZipCase() throws URISyntaxException { - OnapCli cli = new OnapCli(new String [] { + public void shouldReportThatPnfValidationFailedWhenZipDoNotHaveCertificate_allOtherRulesShouldPass() throws URISyntaxException { + // given + OnapCliWrapper cli = new OnapCliWrapper(new String[]{ "--product", "onap-dublin", "csar-validate", "--format", "json", "--pnf", "--csar", absoluteFilePath("pnf/signed-package-valid-signature.zip")}); + + // when cli.handle(); + + // then + final OnapCommandResult onapCommandResult = cli.getCommandResult(); + verifyThatOperation(onapCommandResult, OPERATION_STATUS_FAILED); + verifyThatXRulesFails(onapCommandResult, 1); + verifyThatRuleFails(onapCommandResult, NO_CERTIFICATE_RULE); + verifyThatOperationFinishedWithoutAnyError(cli); + } + + private void verifyThatXRulesFails(OnapCommandResult onapCommandResult, int noOfRules) { + verifyDataOccurrenceInResult(onapCommandResult, "\"passed\":false", noOfRules); + } + + private void verifyThatRuleFails(OnapCommandResult onapCommandResult, String nameOfRule) { + final String value = String.format("\"passed\":false,\"vnfreqName\":\"%s\"", nameOfRule); + verifyDataOccurrenceInResult(onapCommandResult, value, 1); + } + + private void verifyDataOccurrenceInResult(OnapCommandResult onapCommandResult, String data, int dataOccurrence) { + final String json = getResultValue(onapCommandResult, "results"); + int occurrence = StringUtils.countMatches(json, data); + Assertions.assertThat(occurrence).isEqualTo(dataOccurrence); + } + + private void verifyThatOperation(OnapCommandResult onapCommandResult, String operationStatus) { + final String value = getResultValue(onapCommandResult, "criteria"); + Assertions.assertThat(value).isEqualTo(operationStatus); + } + + private String getResultValue(OnapCommandResult onapCommandResult, String attributeName) { + final OnapCommandResultAttribute results = getOnapCommandResultAttribute(onapCommandResult, attributeName); + return results.getValues().get(0); + } + + private OnapCommandResultAttribute getOnapCommandResultAttribute(OnapCommandResult onapCommandResult, String name) { + return onapCommandResult.getRecords().stream() + .filter(it -> it.getName().equals(name)) + .findFirst() + .orElseThrow(() -> new RuntimeException(String.format("Unable to find '%s' attribute!", name))); + } + + private void verifyThatOperationFinishedWithoutAnyError(OnapCliWrapper cli) { assertEquals(0, cli.getExitCode()); } + class OnapCliWrapper extends OnapCli { + + private OnapCommandResult commandResult; + + public OnapCliWrapper(String[] args) { + super(args); + } + + @Override + public void handleTracking(OnapCommand cmd) throws OnapCommandException { + super.handleTracking(cmd); + this.commandResult = cmd.getResult(); + } + + public OnapCommandResult getCommandResult() { + return this.commandResult; + } + } } |