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 org.junit.jupiter.api.Test;
24 import org.openecomp.core.utilities.file.FileContentHandler;
25 import org.openecomp.sdc.datatypes.error.ErrorLevel;
26 import org.openecomp.sdc.datatypes.error.ErrorMessage;
27 import org.openecomp.sdc.vendorsoftwareproduct.exception.OnboardPackageException;
28 import org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.ValidatorFactory;
30 import java.io.IOException;
31 import java.util.List;
33 import java.util.stream.Collectors;
35 import static org.hamcrest.MatcherAssert.assertThat;
36 import static org.hamcrest.Matchers.*;
38 class CsarValidationTest {
41 void shouldNotReturnErrors_whenPnfCsarIsValid() throws OnboardPackageException, IOException {
43 FileContentHandler pnfFileContent = CsarLoader.load("validPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar");
46 Map<String, List<ErrorMessage>> errorsMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent);
49 assertThat(errorsMap, is(anEmptyMap()));
53 void shouldReturnError_whenPMDictionaryContentIsNotCompliantWithSchema() throws OnboardPackageException, IOException {
55 String expectedErrorMessage = "Document number: 1, Path: /pmMetaData/, Message: Key not found: pmHeader";
56 FileContentHandler pnfFileContent = CsarLoader.load("invalidPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/invalidPnfCompliantWithSOL004.csar");
59 Map<String, List<ErrorMessage>> errorMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent);
60 List<ErrorMessage> errorList = errorMap.get("uploadFile");
63 assertThat(errorList, is(not(empty())));
64 assertThat(getActualErrorMessages(errorList).get(0), is(equalTo(expectedErrorMessage)));
65 assertThat(getActualErrorLevel(errorList), is(ErrorLevel.ERROR));
69 void shouldNotReturnErrors_whenPnfCsarContainsIndividualSignatureInManifest() throws OnboardPackageException, IOException {
71 FileContentHandler pnfFileContent = CsarLoader.load(
72 "validPnfWithIndividualSignatureCompliantWithSOL004.csar",
73 "/Files/PNFs/validation/individualSignature/validPnfWithIndividualSignatureCompliantWithSOL004.csar"
77 Map<String, List<ErrorMessage>> errorsMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent);
80 assertThat(errorsMap, is(anEmptyMap()));
84 void shouldReturnErrors_whenPnfCsarContainsIndividualCertificateWithNoSignatureInManifest() throws OnboardPackageException, IOException {
86 List<String> expectedErrorMessage = List.of("Expected 'Signature' entry before 'Certificate' entry;\nAt line 9: 'Certificate: Definitions/pnf_main_descriptor.cert'.");
87 FileContentHandler pnfFileContent = CsarLoader.load(
88 "invalidPnfWithIndividualSignatureCompliantWithSOL004.csar",
89 "/Files/PNFs/validation/individualSignature/invalidPnfWithIndividualSignatureCompliantWithSOL004.csar"
93 Map<String, List<ErrorMessage>> errorsMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent);
94 List<ErrorMessage> errorList = errorsMap.get("uploadFile");
97 assertThat(getActualErrorMessages(errorList), containsInAnyOrder(expectedErrorMessage.toArray()));
98 assertThat(getActualErrorLevel(errorList), is(ErrorLevel.ERROR));
102 private List<String> getActualErrorMessages(List<ErrorMessage> errorList) {
103 return errorList.stream()
104 .map((ErrorMessage::getMessage))
105 .collect(Collectors.toUnmodifiableList());
108 private ErrorLevel getActualErrorLevel(List<ErrorMessage> errorList) {
109 return errorList.get(0).getLevel();