add427d164d1e6a4fc205347a59c8a1b293df639
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / csar / ServiceCsarInfoTest.java
1 /*
2  * -
3  *  ============LICENSE_START=======================================================
4  *  Copyright (C) 2022 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
9  *
10  *       http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.openecomp.sdc.be.components.csar;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import java.io.File;
27 import java.net.URISyntaxException;
28 import java.util.Map;
29 import org.junit.jupiter.api.BeforeEach;
30 import org.junit.jupiter.api.Test;
31 import org.junit.jupiter.api.extension.ExtendWith;
32 import org.mockito.Mock;
33 import org.mockito.junit.jupiter.MockitoExtension;
34 import org.openecomp.sdc.be.config.ConfigurationManager;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.common.impl.ExternalConfiguration;
37 import org.openecomp.sdc.common.impl.FSConfigurationSource;
38 import org.openecomp.sdc.common.zip.ZipUtils;
39 import org.openecomp.sdc.common.zip.exception.ZipException;
40
41 @ExtendWith(MockitoExtension.class)
42 class ServiceCsarInfoTest {
43
44     private ServiceCsarInfo csarInfo;
45
46     @Mock
47     private User user;
48
49     private static final String CSAR_UUID = "csarUUID";
50     private static final String PAYLOAD_NAME = "csars/serviceWithUnknownTypes.csar";
51     private static final String SERVICE_NAME = "serviceWithDataType";
52     private static final String MAIN_TEMPLATE_NAME = "Definitions/service-Servicewithdatatype-template.yml";
53
54     @BeforeEach
55     void setup() throws ZipException, URISyntaxException {
56         csarInfo = createCsarInfo(PAYLOAD_NAME, MAIN_TEMPLATE_NAME);
57
58         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
59     }
60
61     private ServiceCsarInfo createCsarInfo(final String csarFileName, final String mainTemplateName) throws URISyntaxException, ZipException {
62         final File csarFile = new File(ServiceCsarInfoTest.class.getClassLoader().getResource(csarFileName).toURI());
63         final Map<String, byte[]> payload = ZipUtils.readZip(csarFile, false);
64         String mainTemplateContent = new String(payload.get(mainTemplateName));
65
66 return new ServiceCsarInfo(user, CSAR_UUID, payload, SERVICE_NAME, mainTemplateName, mainTemplateContent, true);
67     }
68
69     @SuppressWarnings("unchecked")
70     @Test
71     void testGetDataTypes() {
72         final Map<String, Object> dataTypes = csarInfo.getDataTypes();
73         assertEquals(184, dataTypes.size());
74         final Map<String, Object> dataTypeDefinition = (Map<String, Object>) dataTypes.get("tosca.datatypes.test_g");
75         assertNotNull(dataTypeDefinition);
76         assertEquals("tosca.datatypes.Root", dataTypeDefinition.get("derived_from"));
77         assertEquals("tosca.datatypes.test_h",
78                 ((Map<String, Object>) ((Map<String, Object>) dataTypeDefinition.get("properties")).get("prop2")).get("type"));
79     }
80
81     @SuppressWarnings("unchecked")
82     @Test
83     void testGetInterfaceTypes() {
84         final Map<String, Object> interfaceTypes = csarInfo.getInterfaceTypes();
85         assertEquals(9, interfaceTypes.size());
86         final Map<String, Object> interfaceTypeDefinition = (Map<String, Object>) interfaceTypes.get(
87             "tosca.interfaces.test.node.lifecycle.Reconfigure");
88         assertNotNull(interfaceTypeDefinition);
89         assertEquals("tosca.interfaces.Root", interfaceTypeDefinition.get("derived_from"));
90         assertEquals("reconfigure", ((Map<String, Object>) interfaceTypeDefinition.get("Reconfigure")).get("description"));
91     }
92
93 }