2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 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=========================================================
19 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
21 import static org.hamcrest.MatcherAssert.assertThat;
22 import static org.hamcrest.Matchers.anEmptyMap;
23 import static org.hamcrest.Matchers.hasEntry;
24 import static org.hamcrest.core.Is.is;
25 import static org.mockito.Mockito.when;
26 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.config.CategoriesToGenerateNsd;
27 import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.ETSI_VERSION_METADATA;
28 import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.NSD_FILE_PATH_FORMAT;
29 import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.UNSIGNED_CSAR_EXTENSION;
30 import static org.openecomp.sdc.common.api.ArtifactTypeEnum.ETSI_PACKAGE;
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
42 import org.openecomp.sdc.be.model.Service;
43 import org.openecomp.sdc.be.model.category.CategoryDefinition;
44 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.exception.NsdException;
45 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.factory.EtsiNfvNsdCsarGeneratorFactory;
46 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.config.EtsiVersion;
47 import org.openecomp.sdc.be.plugins.etsi.nfv.nsd.model.NsdCsar;
49 class EtsiNfvNsCsarEntryGeneratorTest {
51 private static final String SERVICE_NORMALIZED_NAME = "normalizedName";
52 private static final String CSAR_ENTRY_EMPTY_ASSERT = "Csar Entries should be empty";
53 private static final EtsiVersion nsdVersion = EtsiVersion.VERSION_2_5_1;
55 private EtsiNfvNsdCsarGeneratorFactory etsiNfvNsdCsarGeneratorFactory;
57 private EtsiNfvNsdCsarGenerator etsiNfvNsdCsarGenerator;
59 private Service service;
61 private EtsiNfvNsCsarEntryGenerator etsiNfvNsCsarEntryGenerator;
65 MockitoAnnotations.openMocks(this);
66 when(etsiNfvNsdCsarGeneratorFactory.create(nsdVersion)).thenReturn(etsiNfvNsdCsarGenerator);
70 void successfullyEntryGenerationTest() throws NsdException {
71 mockServiceComponent();
72 final NsdCsar nsdCsar = new NsdCsar(SERVICE_NORMALIZED_NAME);
73 nsdCsar.setCsarPackage(new byte[5]);
74 when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenReturn(nsdCsar);
75 final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
76 assertThat("Csar Entries should contain only one entry", entryMap.size(), is(1));
77 assertThat("Csar Entries should contain the expected entry", entryMap,
78 hasEntry(String.format(NSD_FILE_PATH_FORMAT, ETSI_PACKAGE, SERVICE_NORMALIZED_NAME, UNSIGNED_CSAR_EXTENSION),
79 nsdCsar.getCsarPackage()));
83 void successfullyEntryGenerationWithVersionFromModelNameTest() throws NsdException {
84 mockServiceComponentWithoutMetadata();
85 final NsdCsar nsdCsar = new NsdCsar(SERVICE_NORMALIZED_NAME);
86 nsdCsar.setCsarPackage(new byte[5]);
87 when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenReturn(nsdCsar);
88 final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
89 assertThat("Csar Entries should contain only one entry", entryMap.size(), is(1));
90 assertThat("Csar Entries should contain the expected entry", entryMap,
91 hasEntry(String.format(NSD_FILE_PATH_FORMAT, ETSI_PACKAGE, SERVICE_NORMALIZED_NAME, UNSIGNED_CSAR_EXTENSION),
92 nsdCsar.getCsarPackage()));
96 void knownNsdGenerationErrorTest() throws NsdException {
97 mockServiceComponent();
98 when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenThrow(new NsdException(""));
99 final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
100 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
104 void unknownNsdGenerationErrorTest() throws NsdException {
105 mockServiceComponent();
106 when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenThrow(new RuntimeException());
107 final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
108 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
112 void componentNullOrNotAServiceTest() {
113 Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
114 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
115 entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(null);
116 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
120 void componentNotExpectedCategoryTest() {
121 when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
122 final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
123 final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition();
124 nsComponentCategoryDefinition.setName("notExpectedCategory");
125 categoryDefinitionList.add(nsComponentCategoryDefinition);
126 when(service.getCategories()).thenReturn(categoryDefinitionList);
127 final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
128 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
131 private void mockServiceComponent() {
132 when(service.getName()).thenReturn("anyName");
133 when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
134 when(service.getNormalizedName()).thenReturn(SERVICE_NORMALIZED_NAME);
135 final Map<String, String> categorySpecificMetadataMap = new HashMap<>();
136 categorySpecificMetadataMap.put(ETSI_VERSION_METADATA, nsdVersion.getVersion());
137 when(service.getCategorySpecificMetadata()).thenReturn(categorySpecificMetadataMap);
138 final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
139 final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition();
140 nsComponentCategoryDefinition.setName(CategoriesToGenerateNsd.ETSI_NS_COMPONENT_CATEGORY.getCategoryName());
141 categoryDefinitionList.add(nsComponentCategoryDefinition);
142 when(service.getCategories()).thenReturn(categoryDefinitionList);
145 private void mockServiceComponentWithoutMetadata() {
146 when(service.getName()).thenReturn("anyName");
147 when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
148 when(service.getNormalizedName()).thenReturn(SERVICE_NORMALIZED_NAME);
149 when(service.getModel()).thenReturn("Any Name with Version v2.5.1");
150 final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
151 final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition();
152 nsComponentCategoryDefinition.setName(CategoriesToGenerateNsd.ETSI_NS_COMPONENT_CATEGORY.getCategoryName());
153 categoryDefinitionList.add(nsComponentCategoryDefinition);
154 when(service.getCategories()).thenReturn(categoryDefinitionList);