Add new SOL004 ETSI Validator
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / orchestration / csar / validation / ManifestBuilderTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
21
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.emptyString;
24 import static org.hamcrest.Matchers.hasItems;
25 import static org.hamcrest.Matchers.hasSize;
26 import static org.hamcrest.Matchers.is;
27 import static org.openecomp.sdc.be.config.NonManoArtifactType.ONAP_PM_DICTIONARY;
28 import static org.openecomp.sdc.be.config.NonManoArtifactType.ONAP_VES_EVENTS;
29 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_ARCHIVE_VERSION;
30 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_NAME;
31 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_PROVIDER;
32 import static org.openecomp.sdc.tosca.csar.ManifestTokenType.PNFD_RELEASE_DATE_TIME;
33
34 import java.io.ByteArrayInputStream;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.TreeMap;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.openecomp.sdc.tosca.csar.Manifest;
42 import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding;
43
44 public class ManifestBuilderTest {
45
46     private ManifestBuilder manifestBuilder;
47
48     @BeforeEach
49     public void setUp() {
50         manifestBuilder = new ManifestBuilder();
51     }
52
53     @Test
54     public void givenNoManifestInformation_whenBuildingManifest_thenEmptyStringShouldBeReturned() {
55         final String manifest = manifestBuilder.build();
56         assertThat("Manifest should be empty", manifest, is(emptyString()));
57     }
58
59     @Test
60     public void givenSourceFiles_whenBuildingManifestWithSources_thenManifestSourceListShouldBeTheSame() {
61         final List<String> expectedSourceList = mockSourceList();
62         mockManifestMetadata();
63         expectedSourceList.forEach(source -> manifestBuilder.withSource(source));
64
65         final Manifest onboardingManifest = parseManifest();
66         final List<String> actualSourceList = onboardingManifest.getSources();
67
68         assertThat("Source list should have the same size as expected source items", actualSourceList,
69             hasSize(expectedSourceList.size()));
70         assertThat("Source list should contain all expected source items", actualSourceList,
71             hasItems(expectedSourceList.toArray(new String[0])));
72         assertThat("Source list should contain only expected sources items", expectedSourceList,
73             hasItems(actualSourceList.toArray(new String[expectedSourceList.size()])));
74     }
75
76     @Test
77     public void givenSourceFiles_whenBuildingManifestWithSignedSources_thenManifestSourceListShouldBeTheSame() {
78         final List<String> expectedSourceList = mockSourceList();
79         mockManifestMetadata();
80         expectedSourceList.forEach(sourceArtifact ->
81             manifestBuilder.withSignedSource(sourceArtifact, "anyAlgorithm", "anyHash")
82         );
83
84         final Manifest onboardingManifest = parseManifest();
85         final List<String> sources = onboardingManifest.getSources();
86
87         assertThat("Source list should have the same size as expected source items", sources,
88             hasSize(expectedSourceList.size()));
89         assertThat("Source list should contain all expected source items", sources,
90             hasItems(expectedSourceList.toArray(new String[0])));
91         assertThat("Source list should contain only expected sources items", expectedSourceList,
92             hasItems(sources.toArray(new String[expectedSourceList.size()])));
93     }
94
95     @Test
96     public void givenMetadata_whenBuildingManifestWithMetadata_thenParsedManifestMetadataShouldBeTheSame() {
97         final Map<String, String> expectedMetadataMap = new TreeMap<>();
98         expectedMetadataMap.put(PNFD_NAME.getToken(), "myPnf");
99         expectedMetadataMap.put(PNFD_PROVIDER.getToken(), "Acme");
100         expectedMetadataMap.put(PNFD_ARCHIVE_VERSION.getToken(), "1.0");
101         expectedMetadataMap.put(PNFD_RELEASE_DATE_TIME.getToken(), "2019-03-11T11:25:00+00:00");
102
103         expectedMetadataMap.forEach((key, value) -> manifestBuilder.withMetaData(key, value));
104
105         final Manifest onboardingManifest = parseManifest();
106         final Map<String, String> actualMetadataMap = onboardingManifest.getMetadata();
107
108         assertThat("Metadata should be as expected", actualMetadataMap, is(expectedMetadataMap));
109     }
110
111     @Test
112     public void givenNonManoArtifacts_whenBuildingManifestWithArtifacts_thenParsedManifestNonManoArtifactsShouldBeTheSame() {
113         mockManifestMetadata();
114         mockManifestSource();
115
116         final Map<String, List<String>> expectedNonManoArtifactMap = new TreeMap<>();
117         expectedNonManoArtifactMap.put(ONAP_VES_EVENTS.getType(), Arrays.asList("Files/Events/MyPnf_Pnf_v1.yaml"));
118         expectedNonManoArtifactMap.put(ONAP_PM_DICTIONARY.getType(), Arrays.asList("Files/Measurements/PM_Dictionary.yaml"));
119         expectedNonManoArtifactMap.put("onap_yang_modules",
120             Arrays.asList("Files/Yang_module/mynetconf.yang", "Files/Yang_module/mynetconf2.yang"));
121         expectedNonManoArtifactMap
122             .put("onap_others", Arrays.asList("Files/Guides/user_guide.txt", "Files/Test/test.txt"));
123
124         expectedNonManoArtifactMap.forEach((key, artifacts) ->
125             artifacts.forEach(s -> manifestBuilder.withNonManoArtifact(key, s))
126         );
127
128         final Manifest onboardingManifest = parseManifest();
129         final Map<String, List<String>> actualNonManoArtifactMap = onboardingManifest.getNonManoSources();
130
131         assertThat("Non Mano Sources should be as expected", actualNonManoArtifactMap, is(expectedNonManoArtifactMap));
132     }
133
134     private Manifest parseManifest() {
135         final Manifest onboardingManifest = new SOL004ManifestOnboarding();
136         onboardingManifest.parse(new ByteArrayInputStream(manifestBuilder.build().getBytes()));
137         return onboardingManifest;
138     }
139
140     private List<String> mockSourceList() {
141         return Arrays.asList("pnf_main_descriptor.mf"
142             , "Definitions/pnf_main_descriptor.yaml"
143             , "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml"
144             , "Definitions/etsi_nfv_sol001_vnfd_2_5_1_types.yaml"
145             , "Files/ChangeLog.txt"
146             , "Files/Events/MyPnf_Pnf_v1.yaml"
147             , "Files/Guides/user_guide.txt"
148             , "Files/Measurements/PM_Dictionary.yaml"
149             , "Files/Scripts/my_script.sh"
150             , "Files/Yang_module/mynetconf.yang"
151             , "TOSCA-Metadata/TOSCA.meta"
152         );
153     }
154
155     private void mockManifestMetadata() {
156         manifestBuilder.withMetaData(PNFD_PROVIDER.getToken(), "provider");
157         manifestBuilder.withMetaData(PNFD_NAME.getToken(), "name");
158         manifestBuilder.withMetaData(PNFD_RELEASE_DATE_TIME.getToken(), "datetime");
159         manifestBuilder.withMetaData(PNFD_ARCHIVE_VERSION.getToken(), "1.0");
160     }
161
162     private void mockManifestSource() {
163         manifestBuilder.withSource("/test.yaml");
164     }
165 }