720ed7786089e05140ff258640f3c070ad4c074c
[sdc.git] /
1 /*
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
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.be.plugins.etsi.nfv.nsd.generator;
21
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;
31
32 import java.util.ArrayList;
33 import java.util.HashMap;
34 import java.util.List;
35 import java.util.Map;
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
48 class EtsiNfvNsCsarEntryGeneratorTest {
49
50
51     @Mock
52     private EtsiNfvNsdCsarGeneratorFactory etsiNfvNsdCsarGeneratorFactory;
53     @Mock
54     private EtsiNfvNsdCsarGenerator etsiNfvNsdCsarGenerator;
55     @Mock
56     private Service service;
57     @InjectMocks
58     private EtsiNfvNsCsarEntryGenerator etsiNfvNsCsarEntryGenerator;
59
60     private static final String SERVICE_NORMALIZED_NAME = "normalizedName";
61     private static final String CSAR_ENTRY_EMPTY_ASSERT = "Csar Entries should be empty";
62     private static final EtsiVersion nsdVersion = EtsiVersion.VERSION_2_5_1;
63
64     @BeforeEach
65     void setUp() {
66         MockitoAnnotations.initMocks(this);
67         when(etsiNfvNsdCsarGeneratorFactory.create(nsdVersion)).thenReturn(etsiNfvNsdCsarGenerator);
68     }
69
70     @Test
71     void successfullyEntryGenerationTest() throws NsdException {
72         mockServiceComponent();
73         final byte[] expectedNsdCsar = new byte[5];
74         when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenReturn(expectedNsdCsar);
75
76         final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
77
78         assertThat("Csar Entries should contain only one entry", entryMap.size(), is(1));
79         assertThat("Csar Entries should contain the expected entry", entryMap,
80             hasEntry(String.format(NSD_FILE_PATH_FORMAT, ETSI_PACKAGE, SERVICE_NORMALIZED_NAME), expectedNsdCsar));
81     }
82
83     @Test
84     void knownNsdGenerationErrorTest() throws NsdException {
85         mockServiceComponent();
86         when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenThrow(new NsdException(""));
87         final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
88
89         assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
90     }
91
92     @Test
93     void unknownNsdGenerationErrorTest() throws NsdException {
94         mockServiceComponent();
95         when(etsiNfvNsdCsarGenerator.generateNsdCsar(service)).thenThrow(new RuntimeException());
96         final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
97
98         assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
99     }
100
101     @Test
102     void componentNullOrNotAServiceTest() {
103         Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
104         assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
105
106         entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(null);
107         assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
108     }
109
110     @Test
111     void componentNotExpectedCategoryTest() {
112         when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
113         final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
114         final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition();
115         nsComponentCategoryDefinition.setName("notExpectedCategory");
116         categoryDefinitionList.add(nsComponentCategoryDefinition);
117         when(service.getCategories()).thenReturn(categoryDefinitionList);
118         final Map<String, byte[]> entryMap = etsiNfvNsCsarEntryGenerator.generateCsarEntries(service);
119         assertThat(CSAR_ENTRY_EMPTY_ASSERT, entryMap, is(anEmptyMap()));
120     }
121
122     private void mockServiceComponent() {
123         when(service.getName()).thenReturn("anyName");
124         when(service.getComponentType()).thenReturn(ComponentTypeEnum.SERVICE);
125         when(service.getNormalizedName()).thenReturn(SERVICE_NORMALIZED_NAME);
126
127         final Map<String, String> categorySpecificMetadataMap = new HashMap<>();
128         categorySpecificMetadataMap.put(ETSI_VERSION_METADATA, nsdVersion.getVersion());
129         when(service.getCategorySpecificMetadata()).thenReturn(categorySpecificMetadataMap);
130
131         final List<CategoryDefinition> categoryDefinitionList = new ArrayList<>();
132         final CategoryDefinition nsComponentCategoryDefinition = new CategoryDefinition();
133         nsComponentCategoryDefinition.setName(ETSI_NS_COMPONENT_CATEGORY);
134         categoryDefinitionList.add(nsComponentCategoryDefinition);
135         when(service.getCategories()).thenReturn(categoryDefinitionList);
136     }
137 }