2390de4780345e766473178a3255346825b020cf
[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 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;
29
30 import java.io.IOException;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.stream.Collectors;
34
35 import static org.hamcrest.MatcherAssert.assertThat;
36 import static org.hamcrest.Matchers.*;
37
38 class CsarValidationTest {
39
40     @Test
41     void shouldNotReturnErrors_whenPnfCsarIsValid() throws OnboardPackageException, IOException {
42         //given
43         FileContentHandler pnfFileContent = CsarLoader.load("validPnfCompliantWithSOL004.csar", "/Files/PNFs/validation/pmdictionary/validPnfCompliantWithSOL004.csar");
44
45         //when
46         Map<String, List<ErrorMessage>> errorsMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent);
47
48         //then
49         assertThat(errorsMap, is(anEmptyMap()));
50     }
51
52     @Test
53     void shouldReturnError_whenPMDictionaryContentIsNotCompliantWithSchema() throws OnboardPackageException, IOException {
54         //given
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");
57
58         //when
59         Map<String, List<ErrorMessage>> errorMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent);
60         List<ErrorMessage> errorList = errorMap.get("uploadFile");
61
62         //then
63         assertThat(errorList, is(not(empty())));
64         assertThat(getActualErrorMessages(errorList).get(0), is(equalTo(expectedErrorMessage)));
65         assertThat(getActualErrorLevel(errorList), is(ErrorLevel.ERROR));
66     }
67
68     @Test
69     void shouldNotReturnErrors_whenPnfCsarContainsIndividualSignatureInManifest() throws OnboardPackageException, IOException {
70         //given
71         FileContentHandler pnfFileContent = CsarLoader.load(
72             "validPnfWithIndividualSignatureCompliantWithSOL004.csar",
73             "/Files/PNFs/validation/individualSignature/validPnfWithIndividualSignatureCompliantWithSOL004.csar"
74         );
75
76         //when
77         Map<String, List<ErrorMessage>> errorsMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent);
78
79         //then
80         assertThat(errorsMap, is(anEmptyMap()));
81     }
82
83     @Test
84     void shouldReturnErrors_whenPnfCsarContainsIndividualCertificateWithNoSignatureInManifest() throws OnboardPackageException, IOException {
85         //given
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"
90         );
91
92         //when
93         Map<String, List<ErrorMessage>> errorsMap = ValidatorFactory.getValidator(pnfFileContent).validateContent(pnfFileContent);
94         List<ErrorMessage> errorList = errorsMap.get("uploadFile");
95
96         //then
97         assertThat(getActualErrorMessages(errorList), containsInAnyOrder(expectedErrorMessage.toArray()));
98         assertThat(getActualErrorLevel(errorList), is(ErrorLevel.ERROR));
99     }
100
101
102     private List<String> getActualErrorMessages(List<ErrorMessage> errorList) {
103         return errorList.stream()
104             .map((ErrorMessage::getMessage))
105             .collect(Collectors.toUnmodifiableList());
106     }
107
108     private ErrorLevel getActualErrorLevel(List<ErrorMessage> errorList) {
109         return errorList.get(0).getLevel();
110     }
111 }