72f235e2871732b8a5d890af7b215216e6f180dc
[sdc.git] /
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.hasItems;
24 import static org.hamcrest.Matchers.hasSize;
25 import static org.hamcrest.Matchers.is;
26 import static org.hamcrest.Matchers.isEmptyString;
27
28 import java.io.ByteArrayInputStream;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.TreeMap;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.openecomp.sdc.tosca.csar.CSARConstants;
36 import org.openecomp.sdc.tosca.csar.Manifest;
37 import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding;
38
39 public class ManifestBuilderTest {
40
41     private ManifestBuilder manifestBuilder;
42
43     @Before
44     public void setUp() {
45         manifestBuilder = new ManifestBuilder();
46     }
47
48     @Test
49     public void givenNoManifestInformation_whenBuildingManifest_thenEmptyStringShouldBeReturned() {
50         final String manifest = manifestBuilder.build();
51         assertThat("Manifest should be empty", manifest, isEmptyString());
52     }
53
54     @Test
55     public void givenSourceFiles_whenBuildingManifestWithSources_thenManifestSourceListShouldBeTheSame() {
56         final List<String> expectedSourceList = mockSourceList();
57         mockManifestMetadata();
58         expectedSourceList.forEach(source -> manifestBuilder.withSource(source));
59
60         final Manifest onboardingManifest = parseManifest();
61         final List<String> actualSourceList = onboardingManifest.getSources();
62
63         assertThat("Source list should have the same size as expected source items", actualSourceList,
64             hasSize(expectedSourceList.size()));
65         assertThat("Source list should contain all expected source items", actualSourceList,
66             hasItems(expectedSourceList.toArray(new String[0])));
67         assertThat("Source list should contain only expected sources items", expectedSourceList,
68             hasItems(actualSourceList.toArray(new String[expectedSourceList.size()])));
69     }
70
71     @Test
72     public void givenSourceFiles_whenBuildingManifestWithSignedSources_thenManifestSourceListShouldBeTheSame() {
73         final List<String> expectedSourceList = mockSourceList();
74         mockManifestMetadata();
75         expectedSourceList.forEach(sourceArtifact ->
76             manifestBuilder.withSignedSource(sourceArtifact, "anyAlgorithm", "anyHash")
77         );
78
79         final Manifest onboardingManifest = parseManifest();
80         final List<String> sources = onboardingManifest.getSources();
81
82         assertThat("Source list should have the same size as expected source items", sources,
83             hasSize(expectedSourceList.size()));
84         assertThat("Source list should contain all expected source items", sources,
85             hasItems(expectedSourceList.toArray(new String[0])));
86         assertThat("Source list should contain only expected sources items", expectedSourceList,
87             hasItems(sources.toArray(new String[expectedSourceList.size()])));
88     }
89
90     @Test
91     public void givenMetadata_whenBuildingManifestWithMetadata_thenParsedManifestMetadataShouldBeTheSame() {
92         final Map<String, String> expectedMetadataMap = new TreeMap<>();
93         expectedMetadataMap.put(CSARConstants.PNFD_NAME, "myPnf");
94         expectedMetadataMap.put(CSARConstants.PNFD_PROVIDER, "Acme");
95         expectedMetadataMap.put(CSARConstants.PNFD_ARCHIVE_VERSION, "1.0");
96         expectedMetadataMap.put(CSARConstants.PNFD_RELEASE_DATE_TIME, "2019-03-11T11:25:00+00:00");
97
98         expectedMetadataMap.forEach((key, value) -> manifestBuilder.withMetaData(key, value));
99
100         final Manifest onboardingManifest = parseManifest();
101         final Map<String, String> actualMetadataMap = onboardingManifest.getMetadata();
102
103         assertThat("Metadata should be as expected", actualMetadataMap, is(expectedMetadataMap));
104     }
105
106     @Test
107     public void givenNonManoArtifacts_whenBuildingManifestWithArtifacts_thenParsedManifestNonManoArtifactsShouldBeTheSame() {
108         mockManifestMetadata();
109         mockManifestSource();
110
111         final Map<String, List<String>> expectedNonManoArtifactMap = new TreeMap<>();
112         expectedNonManoArtifactMap.put("onap_ves_events", Arrays.asList("Files/Events/MyPnf_Pnf_v1.yaml"));
113         expectedNonManoArtifactMap.put("onap_pm_dictionary", Arrays.asList("Files/Measurements/PM_Dictionary.yaml"));
114         expectedNonManoArtifactMap.put("onap_yang_modules",
115             Arrays.asList("Files/Yang_module/mynetconf.yang", "Files/Yang_module/mynetconf2.yang"));
116         expectedNonManoArtifactMap
117             .put("onap_others", Arrays.asList("Files/Guides/user_guide.txt", "Files/Test/test.txt"));
118
119         expectedNonManoArtifactMap.forEach((key, artifacts) ->
120             artifacts.forEach(s -> manifestBuilder.withNonManoArtifact(key, s))
121         );
122
123         final Manifest onboardingManifest = parseManifest();
124         final Map<String, List<String>> actualNonManoArtifactMap = onboardingManifest.getNonManoSources();
125
126         assertThat("Non Mano Sources should be as expected", actualNonManoArtifactMap, is(expectedNonManoArtifactMap));
127     }
128
129     private Manifest parseManifest() {
130         final Manifest onboardingManifest = new SOL004ManifestOnboarding();
131         onboardingManifest.parse(new ByteArrayInputStream(manifestBuilder.build().getBytes()));
132         return onboardingManifest;
133     }
134
135     private List<String> mockSourceList() {
136         return Arrays.asList("pnf_main_descriptor.mf"
137             , "Definitions/pnf_main_descriptor.yaml"
138             , "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml"
139             , "Definitions/etsi_nfv_sol001_vnfd_2_5_1_types.yaml"
140             , "Files/ChangeLog.txt"
141             , "Files/Events/MyPnf_Pnf_v1.yaml"
142             , "Files/Guides/user_guide.txt"
143             , "Files/Measurements/PM_Dictionary.yaml"
144             , "Files/Scripts/my_script.sh"
145             , "Files/Yang_module/mynetconf.yang"
146             , "TOSCA-Metadata/TOSCA.meta"
147         );
148     }
149
150     private void mockManifestMetadata() {
151         manifestBuilder.withMetaData(CSARConstants.PNFD_PROVIDER, "test");
152     }
153
154     private void mockManifestSource() {
155         manifestBuilder.withSource("/test.yaml");
156     }
157 }