2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia. All rights reserved.
6 * Modification Copyright (C) 2021 Nokia.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.sdc.backend.ci.tests.validation.pmdictionary;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.containsInAnyOrder;
25 import static org.hamcrest.Matchers.empty;
26 import static org.hamcrest.Matchers.equalTo;
27 import static org.hamcrest.Matchers.is;
28 import static org.hamcrest.Matchers.not;
30 import java.io.IOException;
31 import java.util.List;
32 import java.util.stream.Collectors;
33 import org.junit.jupiter.api.Test;
34 import org.openecomp.core.utilities.file.FileContentHandler;
35 import org.openecomp.sdc.datatypes.error.ErrorLevel;
36 import org.openecomp.sdc.datatypes.error.ErrorMessage;
37 import org.openecomp.sdc.vendorsoftwareproduct.exception.OnboardPackageException;
38 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidationResult;
39 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory;
41 class CsarValidationTest {
44 void shouldNotReturnErrors_whenPnfCsarIsValid() throws OnboardPackageException, IOException {
46 FileContentHandler pnfFileContent = CsarLoader.load("validPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar");
49 final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent);
52 assertThat(validationResult.getErrors(), is(empty()));
56 void shouldReturnError_whenPMDictionaryContentIsNotCompliantWithSchema() throws OnboardPackageException, IOException {
58 String expectedErrorMessage = "Document number: 1, Path: /pmMetaData/, Message: Key not found: pmHeader";
59 FileContentHandler pnfFileContent = CsarLoader.load("invalidPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/invalidPnfCompliantWithSOL004.csar");
62 final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent);
63 List<ErrorMessage> errorList = validationResult.getErrors();
66 assertThat(errorList, is(not(empty())));
67 assertThat(getActualErrorMessages(errorList).get(0), is(equalTo(expectedErrorMessage)));
68 assertThat(getActualErrorLevel(errorList), is(ErrorLevel.ERROR));
72 void shouldNotReturnErrors_whenPnfCsarContainsIndividualSignatureInManifest() throws OnboardPackageException, IOException {
74 FileContentHandler pnfFileContent = CsarLoader.load(
75 "validPnfWithIndividualSignatureCompliantWithSOL004.csar",
76 "/Files/PNFs/validation/individualSignature/validPnfWithIndividualSignatureCompliantWithSOL004.csar"
80 final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent);
83 assertThat(validationResult.isValid(), is(true));
84 assertThat(validationResult.getErrors(), is(empty()));
88 void shouldReturnErrors_whenPnfCsarContainsIndividualCertificateWithNoSignatureInManifest() throws OnboardPackageException, IOException {
90 List<String> expectedErrorMessage = List.of("Expected 'Signature' entry before 'Certificate' entry;\nAt line 9: 'Certificate: Definitions/pnf_main_descriptor.cert'.");
91 FileContentHandler pnfFileContent = CsarLoader.load(
92 "invalidPnfWithIndividualSignatureCompliantWithSOL004.csar",
93 "/Files/PNFs/validation/individualSignature/invalidPnfWithIndividualSignatureCompliantWithSOL004.csar"
97 final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent);
98 List<ErrorMessage> errorList = validationResult.getErrors();
101 assertThat(getActualErrorMessages(errorList), containsInAnyOrder(expectedErrorMessage.toArray()));
102 assertThat(getActualErrorLevel(errorList), is(ErrorLevel.ERROR));
106 private List<String> getActualErrorMessages(List<ErrorMessage> errorList) {
107 return errorList.stream()
108 .map((ErrorMessage::getMessage))
109 .collect(Collectors.toUnmodifiableList());
112 private ErrorLevel getActualErrorLevel(List<ErrorMessage> errorList) {
113 return errorList.get(0).getLevel();