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
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.
16 * SPDX-License-Identifier: Apache-2.0
17 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation;
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 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.NonManoArtifactType.ONAP_PM_DICTIONARY;
28 import static org.openecomp.sdc.vendorsoftwareproduct.impl.orchestration.csar.validation.NonManoArtifactType.ONAP_VES_EVENTS;
30 import java.io.ByteArrayInputStream;
31 import java.util.Arrays;
32 import java.util.List;
34 import java.util.TreeMap;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.openecomp.sdc.tosca.csar.CSARConstants;
38 import org.openecomp.sdc.tosca.csar.Manifest;
39 import org.openecomp.sdc.tosca.csar.SOL004ManifestOnboarding;
41 public class ManifestBuilderTest {
43 private ManifestBuilder manifestBuilder;
47 manifestBuilder = new ManifestBuilder();
51 public void givenNoManifestInformation_whenBuildingManifest_thenEmptyStringShouldBeReturned() {
52 final String manifest = manifestBuilder.build();
53 assertThat("Manifest should be empty", manifest, isEmptyString());
57 public void givenSourceFiles_whenBuildingManifestWithSources_thenManifestSourceListShouldBeTheSame() {
58 final List<String> expectedSourceList = mockSourceList();
59 mockManifestMetadata();
60 expectedSourceList.forEach(source -> manifestBuilder.withSource(source));
62 final Manifest onboardingManifest = parseManifest();
63 final List<String> actualSourceList = onboardingManifest.getSources();
65 assertThat("Source list should have the same size as expected source items", actualSourceList,
66 hasSize(expectedSourceList.size()));
67 assertThat("Source list should contain all expected source items", actualSourceList,
68 hasItems(expectedSourceList.toArray(new String[0])));
69 assertThat("Source list should contain only expected sources items", expectedSourceList,
70 hasItems(actualSourceList.toArray(new String[expectedSourceList.size()])));
74 public void givenSourceFiles_whenBuildingManifestWithSignedSources_thenManifestSourceListShouldBeTheSame() {
75 final List<String> expectedSourceList = mockSourceList();
76 mockManifestMetadata();
77 expectedSourceList.forEach(sourceArtifact ->
78 manifestBuilder.withSignedSource(sourceArtifact, "anyAlgorithm", "anyHash")
81 final Manifest onboardingManifest = parseManifest();
82 final List<String> sources = onboardingManifest.getSources();
84 assertThat("Source list should have the same size as expected source items", sources,
85 hasSize(expectedSourceList.size()));
86 assertThat("Source list should contain all expected source items", sources,
87 hasItems(expectedSourceList.toArray(new String[0])));
88 assertThat("Source list should contain only expected sources items", expectedSourceList,
89 hasItems(sources.toArray(new String[expectedSourceList.size()])));
93 public void givenMetadata_whenBuildingManifestWithMetadata_thenParsedManifestMetadataShouldBeTheSame() {
94 final Map<String, String> expectedMetadataMap = new TreeMap<>();
95 expectedMetadataMap.put(CSARConstants.PNFD_NAME, "myPnf");
96 expectedMetadataMap.put(CSARConstants.PNFD_PROVIDER, "Acme");
97 expectedMetadataMap.put(CSARConstants.PNFD_ARCHIVE_VERSION, "1.0");
98 expectedMetadataMap.put(CSARConstants.PNFD_RELEASE_DATE_TIME, "2019-03-11T11:25:00+00:00");
100 expectedMetadataMap.forEach((key, value) -> manifestBuilder.withMetaData(key, value));
102 final Manifest onboardingManifest = parseManifest();
103 final Map<String, String> actualMetadataMap = onboardingManifest.getMetadata();
105 assertThat("Metadata should be as expected", actualMetadataMap, is(expectedMetadataMap));
109 public void givenNonManoArtifacts_whenBuildingManifestWithArtifacts_thenParsedManifestNonManoArtifactsShouldBeTheSame() {
110 mockManifestMetadata();
111 mockManifestSource();
113 final Map<String, List<String>> expectedNonManoArtifactMap = new TreeMap<>();
114 expectedNonManoArtifactMap.put(ONAP_VES_EVENTS.getType(), Arrays.asList("Files/Events/MyPnf_Pnf_v1.yaml"));
115 expectedNonManoArtifactMap.put(ONAP_PM_DICTIONARY.getType(), Arrays.asList("Files/Measurements/PM_Dictionary.yaml"));
116 expectedNonManoArtifactMap.put("onap_yang_modules",
117 Arrays.asList("Files/Yang_module/mynetconf.yang", "Files/Yang_module/mynetconf2.yang"));
118 expectedNonManoArtifactMap
119 .put("onap_others", Arrays.asList("Files/Guides/user_guide.txt", "Files/Test/test.txt"));
121 expectedNonManoArtifactMap.forEach((key, artifacts) ->
122 artifacts.forEach(s -> manifestBuilder.withNonManoArtifact(key, s))
125 final Manifest onboardingManifest = parseManifest();
126 final Map<String, List<String>> actualNonManoArtifactMap = onboardingManifest.getNonManoSources();
128 assertThat("Non Mano Sources should be as expected", actualNonManoArtifactMap, is(expectedNonManoArtifactMap));
131 private Manifest parseManifest() {
132 final Manifest onboardingManifest = new SOL004ManifestOnboarding();
133 onboardingManifest.parse(new ByteArrayInputStream(manifestBuilder.build().getBytes()));
134 return onboardingManifest;
137 private List<String> mockSourceList() {
138 return Arrays.asList("pnf_main_descriptor.mf"
139 , "Definitions/pnf_main_descriptor.yaml"
140 , "Definitions/etsi_nfv_sol001_pnfd_2_5_1_types.yaml"
141 , "Definitions/etsi_nfv_sol001_vnfd_2_5_1_types.yaml"
142 , "Files/ChangeLog.txt"
143 , "Files/Events/MyPnf_Pnf_v1.yaml"
144 , "Files/Guides/user_guide.txt"
145 , "Files/Measurements/PM_Dictionary.yaml"
146 , "Files/Scripts/my_script.sh"
147 , "Files/Yang_module/mynetconf.yang"
148 , "TOSCA-Metadata/TOSCA.meta"
152 private void mockManifestMetadata() {
153 manifestBuilder.withMetaData(CSARConstants.PNFD_PROVIDER, "test");
156 private void mockManifestSource() {
157 manifestBuilder.withSource("/test.yaml");