3 * ============LICENSE_START=======================================================
4 * Copyright (C) 2020 Nordix Foundation
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
20 package org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.anEmptyMap;
24 import static org.hamcrest.Matchers.hasEntry;
25 import static org.hamcrest.core.Is.is;
26 import static org.mockito.Mockito.when;
27 import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.ETSI_NS_COMPONENT_CATEGORY;
28 import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.ETSI_VERSION_METADATA;
29 import static org.openecomp.sdc.be.plugins.etsi.nfv.nsd.generator.EtsiNfvNsCsarEntryGenerator.NSD_FILE_PATH_FORMAT;
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;
48 class EtsiNfvNsCsarEntryGeneratorTest {
50 private static final String SERVICE_NORMALIZED_NAME = "normalizedName";
51 private static final String CSAR_ENTRY_EMPTY_ASSERT = "Csar Entries should be empty";
52 private static final EtsiVersion nsdVersion = EtsiVersion.VERSION_2_5_1;
54 private EtsiNfvNsdCsarGeneratorFactory etsiNfvNsdCsarGeneratorFactory;
56 private EtsiNfvNsdCsarGenerator etsiNfvNsdCsarGenerator;
58 private Service service;
60 private EtsiNfvNsCsarEntryGenerator etsiNfvNsCsarEntryGenerator;
64 MockitoAnnotations.initMocks(this);
65 when(etsiNfvNsdCsarGeneratorFactory.create(nsdVersion)).thenReturn(etsiNfvNsdCsarGenerator);
69 void successfullyEntryGenerationTest() throws NsdException {
70 mockServiceComponent();
71 final byte[] expectedNsdCsar = new byte[5];
72 when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenReturn(expectedNsdCsar);
73 final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
74 assertThat("Csar Entries should contain only one entry", entryMap.size(), is(1));
75 assertThat("Csar Entries should contain the expected entry", entryMap,
76 hasEntry(String.format(NSD_FILE_PATH_FORMAT, ETSI_PACKAGE, SERVICE_NORMALIZED_NAME), expectedNsdCsar));
80 void knownNsdGenerationErrorTest() throws NsdException {
81 mockServiceComponent();
82 when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenThrow(new NsdException(""));
83 final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
84 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
88 void unknownNsdGenerationErrorTest() throws NsdException {
89 mockServiceComponent();
90 when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenThrow(new RuntimeException());
91 final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
92 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
96 void componentNullOrNotAServiceTest() {
97 Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
98 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
99 entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(null);
100 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
104 void componentNotExpectedCategoryTest() {
105 when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
106 final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
107 final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition();
108 nsComponentCategoryDefinition.setName("notExpectedCategory");
109 categoryDefinitionList.add(nsComponentCategoryDefinition);
110 when(service.getCategories()).thenReturn(categoryDefinitionList);
111 final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
112 assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
115 private void mockServiceComponent() {
116 when(service.getName()).thenReturn("anyName");
117 when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
118 when(service.getNormalizedName()).thenReturn(SERVICE_NORMALIZED_NAME);
119 final Map<String, String> categorySpecificMetadataMap = new HashMap<>();
120 categorySpecificMetadataMap.put(ETSI_VERSION_METADATA, nsdVersion.getVersion());
121 when(service.getCategorySpecificMetadata()).thenReturn(categorySpecificMetadataMap);
122 final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
123 final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition();
124 nsComponentCategoryDefinition.setName(ETSI_NS_COMPONENT_CATEGORY);
125 categoryDefinitionList.add(nsComponentCategoryDefinition);
126 when(service.getCategories()).thenReturn(categoryDefinitionList);