2 * ============LICENSE_START===============================================
4 * ========================================================================
5 * Copyright (C) 2019 Samsung. All rights reserved.
6 * ========================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=================================================
21 package org.openecomp.sdc.be.components.csar;
23 import org.junit.jupiter.api.BeforeEach;
24 import org.junit.jupiter.api.Test;
25 import org.junit.jupiter.api.extension.ExtendWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.jupiter.MockitoExtension;
29 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
30 import org.openecomp.sdc.be.config.ConfigurationManager;
31 import org.openecomp.sdc.be.config.NonManoArtifactType;
32 import org.openecomp.sdc.be.config.NonManoConfiguration;
33 import org.openecomp.sdc.be.config.NonManoFolderType;
34 import org.openecomp.sdc.be.dao.api.ActionStatus;
35 import org.openecomp.sdc.be.model.NodeTypeInfo;
36 import org.openecomp.sdc.be.model.User;
37 import org.openecomp.sdc.common.impl.ExternalConfiguration;
38 import org.openecomp.sdc.common.impl.FSConfigurationSource;
39 import org.openecomp.sdc.common.zip.ZipUtils;
40 import org.openecomp.sdc.common.zip.exception.ZipException;
41 import com.datastax.oss.driver.shaded.guava.common.collect.Lists;
43 import java.net.URISyntaxException;
44 import java.util.Arrays;
45 import java.util.HashMap;
46 import java.util.List;
48 import java.util.Optional;
50 import static org.hamcrest.Matchers.equalTo;
51 import static org.hamcrest.Matchers.is;
52 import static org.junit.Assert.assertEquals;
53 import static org.junit.Assert.assertNotNull;
54 import static org.junit.Assert.assertThat;
55 import static org.junit.Assert.assertTrue;
56 import static org.junit.Assert.fail;
57 import static org.junit.jupiter.api.Assertions.assertFalse;
58 import static org.mockito.Mockito.when;
60 @ExtendWith(MockitoExtension.class)
61 public class CsarInfoTest {
63 private CsarInfo csarInfo;
68 private static final String CSAR_UUID = "csarUUID";
69 private static final String PAYLOAD_NAME = "mock_service.csar";
70 private static final String RESOURCE_NAME = "resourceName";
71 private static final String MAIN_TEMPLATE_NAME = "Definitions/tosca_mock_vf.yaml";
72 private static final String NEW_NODE_NAME = "new_db";
73 private static final String NODE_TYPE = "tosca.nodes.Compute";
74 private static final String DELIVER_FOR = "tosca.nodes.Root";
77 public void setup() throws ZipException, URISyntaxException {
79 csarInfo = createCsarInfo(PAYLOAD_NAME, MAIN_TEMPLATE_NAME);
81 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
84 private CsarInfo createCsarInfo(final String csarFileName, final String mainTemplateName) throws URISyntaxException, ZipException {
85 final File csarFile = new File(CsarInfoTest.class.getClassLoader().getResource(csarFileName).toURI());
86 final Map<String, byte[]> payload = ZipUtils.readZip(csarFile, false);
87 String mainTemplateContent = new String(payload.get(mainTemplateName));
89 return new CsarInfo(user, CSAR_UUID, payload, RESOURCE_NAME,
90 mainTemplateName, mainTemplateContent, true);
94 public void add2TimesTheSameNodeTest() {
98 csarInfo.addNodeToQueue(NEW_NODE_NAME);
99 csarInfo.addNodeToQueue(NEW_NODE_NAME);
100 fail("AddNodeToQueue not throw the exception!");
101 } catch (ByActionStatusComponentException e) {
102 List<String> expectParam = Arrays.asList(NEW_NODE_NAME, RESOURCE_NAME);
105 assertEquals(ActionStatus.CFVC_LOOP_DETECTED, e.getActionStatus());
106 assertTrue(Arrays.stream(e.getParams()).allMatch(expectParam::contains));
111 public void addMultipleTimesNodeTest() {
114 csarInfo.addNodeToQueue(NEW_NODE_NAME);
115 csarInfo.removeNodeFromQueue();
116 csarInfo.addNodeToQueue(NEW_NODE_NAME);
120 public void setUpdateTest() {
122 csarInfo.setUpdate(true);
123 assertTrue(csarInfo.isUpdate());
125 csarInfo.setUpdate(false);
126 assertFalse(csarInfo.isUpdate());
130 public void csarCheckTypesInfoTest() {
133 Map<String, NodeTypeInfo> nodeTypeInfoMap = csarInfo.extractTypesInfo();
134 NodeTypeInfo nodeTypeInfo = nodeTypeInfoMap.get(NODE_TYPE);
137 assertNotNull(nodeTypeInfo);
138 assertTrue(nodeTypeInfo.getDerivedFrom().contains(DELIVER_FOR));
139 assertEquals(NODE_TYPE, nodeTypeInfo.getType());
141 assertEquals(MAIN_TEMPLATE_NAME, csarInfo.getMainTemplateName());
142 assertEquals(csarInfo.getMainTemplateName(), nodeTypeInfo.getTemplateFileName());
144 Map<String, Object> dataTypes = csarInfo.getDataTypes();
145 assertTrue(dataTypes.containsKey("tosca.datatypes.testDataType.FromMainTemplate"));
146 assertTrue(dataTypes.containsKey("tosca.datatypes.testDataType.FromGlobalSub"));
150 public void getSoftwareInformationPathTest() {
151 final NonManoConfiguration nonManoConfigurationMock = Mockito.mock(NonManoConfiguration.class);
152 final CsarInfo csarInfo = new CsarInfo(nonManoConfigurationMock);
153 final NonManoFolderType testNonManoFolderType = new NonManoFolderType();
154 testNonManoFolderType.setLocation("sw-location-test");
155 testNonManoFolderType.setType("informational-test");
156 when(nonManoConfigurationMock.getNonManoType(NonManoArtifactType.ONAP_SW_INFORMATION)).thenReturn(testNonManoFolderType);
157 final Map<String, byte[]> csarFileMap = new HashMap<>();
158 final String expectedPath = testNonManoFolderType.getPath() + "/" + "software-file.yaml";
159 csarFileMap.put(expectedPath, new byte[0]);
160 csarInfo.setCsar(csarFileMap);
161 final Optional<String> softwareInformationPath = csarInfo.getSoftwareInformationPath();
162 assertThat("The software information yaml path should be present", softwareInformationPath.isPresent(), is(true));
163 softwareInformationPath.ifPresent(path -> {
164 assertThat("The software information yaml ", path, is(equalTo(expectedPath)));
169 public void getSoftwareInformationPathTest_emptyCsar() {
170 csarInfo.setCsar(new HashMap<>());
171 final Optional<String> softwareInformationPath = csarInfo.getSoftwareInformationPath();
172 assertThat("The software information yaml path should not be present", softwareInformationPath.isPresent(), is(false));
175 @SuppressWarnings("unchecked")
177 public void testCreateCsarInfoEtsiVnf() throws URISyntaxException, ZipException {
178 final CsarInfo csarInfo = createCsarInfo("etsi_vnf.csar", "Definitions/MainServiceTemplate.yaml");
180 final String nodeTypeInSubstitutionMapping = (String) ((Map<String, Object>)((Map<String, Object>)csarInfo.getMappedToscaMainTemplate().get("topology_template")).get("substitution_mappings")).get("node_type");
181 assertTrue(((Map<String, Object>) csarInfo.getMappedToscaMainTemplate().get("node_types")).containsKey(nodeTypeInSubstitutionMapping));
183 assertTrue(csarInfo.extractTypesInfo().isEmpty());
187 public void testCreateCsarInfoVnfWithNodeTypeInGlobalSub() throws URISyntaxException, ZipException {
188 final CsarInfo csarInfo = createCsarInfo("nodeTypeInGlobalSub.csar", "Definitions/MainServiceTemplate.yaml");
190 assertEquals(1, csarInfo.extractTypesInfo().size());
191 final NodeTypeInfo nodeTypeInfo = csarInfo.extractTypesInfo().get("tosca.nodes.l3vpn");
192 assertNotNull(nodeTypeInfo);
193 assertEquals("Definitions/GlobalSubstitutionTypesServiceTemplate.yaml", nodeTypeInfo.getTemplateFileName());
194 assertEquals("tosca.nodes.l3vpn", nodeTypeInfo.getType());
195 assertEquals(Lists.newArrayList("tosca.nodes.Root"), nodeTypeInfo.getDerivedFrom());