9500c9b378fab2c7e10b3d72f90f687d124637ff
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21 package org.onap.sdc.backend.ci.tests.validation.pmdictionary;
22
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;
29
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;
40
41 class CsarValidationTest {
42
43     @Test
44     void shouldNotReturnErrors_whenPnfCsarIsValid() throws OnboardPackageException, IOException {
45         //given
46         FileContentHandler pnfFileContent = CsarLoader.load("validPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar");
47
48         //when
49         final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent);
50
51         //then
52         assertThat(validationResult.getErrors(), is(empty()));
53     }
54
55     @Test
56     void shouldReturnError_whenPMDictionaryContentIsNotCompliantWithSchema() throws OnboardPackageException, IOException {
57         //given
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");
60
61         //when
62         final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent);
63         List<ErrorMessage> errorList = validationResult.getErrors();
64
65         //then
66         assertThat(errorList, is(not(empty())));
67         assertThat(getActualErrorMessages(errorList).get(0), is(equalTo(expectedErrorMessage)));
68         assertThat(getActualErrorLevel(errorList), is(ErrorLevel.ERROR));
69     }
70
71     @Test
72     void shouldNotReturnErrors_whenPnfCsarContainsIndividualSignatureInManifest() throws OnboardPackageException, IOException {
73         //given
74         FileContentHandler pnfFileContent = CsarLoader.load(
75             "validPnfWithIndividualSignatureCompliantWithSOL004.csar",
76             "/Files/PNFs/validation/individualSignature/validPnfWithIndividualSignatureCompliantWithSOL004.csar"
77         );
78
79         //when
80         final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent);
81
82         //then
83         assertThat(validationResult.isValid(), is(true));
84         assertThat(validationResult.getErrors(), is(empty()));
85     }
86
87     @Test
88     void shouldReturnErrors_whenPnfCsarContainsIndividualCertificateWithNoSignatureInManifest() throws OnboardPackageException, IOException {
89         //given
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"
94         );
95
96         //when
97         final ValidationResult validationResult = ValidatorFactory.getValidator(pnfFileContent).validate(pnfFileContent);
98         List<ErrorMessage> errorList = validationResult.getErrors();
99
100         //then
101         assertThat(getActualErrorMessages(errorList), containsInAnyOrder(expectedErrorMessage.toArray()));
102         assertThat(getActualErrorLevel(errorList), is(ErrorLevel.ERROR));
103     }
104
105
106     private List<String> getActualErrorMessages(List<ErrorMessage> errorList) {
107         return errorList.stream()
108             .map((ErrorMessage::getMessage))
109             .collect(Collectors.toUnmodifiableList());
110     }
111
112     private ErrorLevel getActualErrorLevel(List<ErrorMessage> errorList) {
113         return errorList.get(0).getLevel();
114     }
115 }