Import VSP with non-standard data types
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / csar / CsarInfoTest.java
1 /*-
2  * ============LICENSE_START===============================================
3  * ONAP SDC
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
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
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=================================================
19  */
20
21 package org.openecomp.sdc.be.components.csar;
22
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;
42 import java.io.File;
43 import java.net.URISyntaxException;
44 import java.util.Arrays;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Optional;
49
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;
59
60 @ExtendWith(MockitoExtension.class)
61 public class CsarInfoTest {
62
63     private CsarInfo csarInfo;
64
65     @Mock
66     private User user;
67
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";
75
76     @BeforeEach
77     public void setup() throws ZipException, URISyntaxException {
78         // given
79         csarInfo = createCsarInfo(PAYLOAD_NAME, MAIN_TEMPLATE_NAME);
80
81         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
82     }
83     
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));
88
89       return new CsarInfo(user, CSAR_UUID, payload, RESOURCE_NAME,
90               mainTemplateName, mainTemplateContent, true);
91   }
92
93     @Test
94     public void add2TimesTheSameNodeTest() {
95
96         try {
97             // when
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);
103
104             // then
105             assertEquals(ActionStatus.CFVC_LOOP_DETECTED, e.getActionStatus());
106             assertTrue(Arrays.stream(e.getParams()).allMatch(expectParam::contains));
107         }
108     }
109
110     @Test
111     public void addMultipleTimesNodeTest() {
112
113         // when
114         csarInfo.addNodeToQueue(NEW_NODE_NAME);
115         csarInfo.removeNodeFromQueue();
116         csarInfo.addNodeToQueue(NEW_NODE_NAME);
117     }
118
119     @Test
120     public void setUpdateTest() {
121
122         csarInfo.setUpdate(true);
123         assertTrue(csarInfo.isUpdate());
124
125         csarInfo.setUpdate(false);
126         assertFalse(csarInfo.isUpdate());
127     }
128
129     @Test
130     public void csarCheckTypesInfoTest() {
131
132         // when
133         Map<String, NodeTypeInfo> nodeTypeInfoMap = csarInfo.extractTypesInfo();
134         NodeTypeInfo nodeTypeInfo = nodeTypeInfoMap.get(NODE_TYPE);
135
136         // then
137         assertNotNull(nodeTypeInfo);
138         assertTrue(nodeTypeInfo.getDerivedFrom().contains(DELIVER_FOR));
139         assertEquals(NODE_TYPE, nodeTypeInfo.getType());
140
141         assertEquals(MAIN_TEMPLATE_NAME, csarInfo.getMainTemplateName());
142         assertEquals(csarInfo.getMainTemplateName(), nodeTypeInfo.getTemplateFileName());
143         
144         Map<String, Object> dataTypes = csarInfo.getDataTypes();
145         assertTrue(dataTypes.containsKey("tosca.datatypes.testDataType.FromMainTemplate"));
146         assertTrue(dataTypes.containsKey("tosca.datatypes.testDataType.FromGlobalSub"));
147     }
148
149     @Test
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)));
165         });
166     }
167
168     @Test
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));
173     }
174     
175     @SuppressWarnings("unchecked")
176     @Test
177     public void testCreateCsarInfoEtsiVnf() throws URISyntaxException, ZipException {
178         final CsarInfo csarInfo = createCsarInfo("etsi_vnf.csar", "Definitions/MainServiceTemplate.yaml");
179         
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));
182         
183         assertTrue(csarInfo.extractTypesInfo().isEmpty());
184     }
185     
186     @Test
187     public void testCreateCsarInfoVnfWithNodeTypeInGlobalSub() throws URISyntaxException, ZipException {
188         final CsarInfo csarInfo = createCsarInfo("nodeTypeInGlobalSub.csar", "Definitions/MainServiceTemplate.yaml");
189
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());        
196     }
197 }