Catalog alignment
[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.Before;
24 import org.junit.Test;
25 import org.junit.runner.RunWith;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.junit.MockitoJUnitRunner;
29 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
30 import org.openecomp.sdc.be.config.NonManoArtifactType;
31 import org.openecomp.sdc.be.config.NonManoConfiguration;
32 import org.openecomp.sdc.be.config.NonManoFolderType;
33 import org.openecomp.sdc.be.dao.api.ActionStatus;
34 import org.openecomp.sdc.be.model.NodeTypeInfo;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.common.zip.ZipUtils;
37 import org.openecomp.sdc.common.zip.exception.ZipException;
38
39 import java.io.File;
40 import java.net.URISyntaxException;
41 import java.util.Arrays;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Optional;
46
47 import static org.hamcrest.Matchers.equalTo;
48 import static org.hamcrest.Matchers.is;
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertNotNull;
51 import static org.junit.Assert.assertThat;
52 import static org.junit.Assert.assertTrue;
53 import static org.junit.Assert.fail;
54 import static org.mockito.Mockito.when;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public class CsarInfoTest {
58
59     private CsarInfo csarInfo;
60
61     @Mock
62     private User user;
63
64     private static final String CSAR_UUID = "csarUUID";
65     private static final String PAYLOAD_NAME = "mock_service.csar";
66     private static final String RESOURCE_NAME = "resourceName";
67     private static final String MAIN_TEMPLATE_NAME = "Definitions/tosca_mock_vf.yaml";
68     private static final String NEW_NODE_NAME = "new_db";
69     private static final String NODE_TYPE = "tosca.nodes.Compute";
70     private static final String DELIVER_FOR = "tosca.nodes.Root";
71
72     @Before
73     public void setup() throws ZipException, URISyntaxException {
74         // given
75         final File csarFile = new File(CsarInfoTest.class.getClassLoader().getResource(PAYLOAD_NAME).toURI());
76         final Map<String, byte[]> payload = ZipUtils.readZip(csarFile, false);
77         String mainTemplateContent = new String(payload.get(MAIN_TEMPLATE_NAME));
78
79         csarInfo = new CsarInfo(user, CSAR_UUID, payload, RESOURCE_NAME,
80                 MAIN_TEMPLATE_NAME, mainTemplateContent, true);
81     }
82
83     @Test
84     public void add2TimesTheSameNodeTest() {
85
86         try {
87             // when
88             csarInfo.addNodeToQueue(NEW_NODE_NAME);
89             csarInfo.addNodeToQueue(NEW_NODE_NAME);
90             fail("AddNodeToQueue not throw the exception!");
91         } catch (ByActionStatusComponentException e) {
92             List<String> expectParam = Arrays.asList(NEW_NODE_NAME, RESOURCE_NAME);
93
94             // then
95             assertEquals(ActionStatus.CFVC_LOOP_DETECTED, e.getActionStatus());
96             assertTrue(Arrays.stream(e.getParams()).allMatch(expectParam::contains));
97         }
98     }
99
100     @Test
101     public void addMultipleTimesNodeTest() {
102
103         // when
104         csarInfo.addNodeToQueue(NEW_NODE_NAME);
105         csarInfo.removeNodeFromQueue();
106         csarInfo.addNodeToQueue(NEW_NODE_NAME);
107     }
108
109     @Test
110     public void csarCheckNodeTypesInfoTest() {
111
112         // when
113         Map<String, NodeTypeInfo> nodeTypeInfoMap = csarInfo.extractNodeTypesInfo();
114         NodeTypeInfo nodeTypeInfo = nodeTypeInfoMap.get(NODE_TYPE);
115
116         // then
117         assertNotNull(nodeTypeInfo);
118         assertTrue(nodeTypeInfo.getDerivedFrom().contains(DELIVER_FOR));
119         assertEquals(NODE_TYPE, nodeTypeInfo.getType());
120
121         assertEquals(MAIN_TEMPLATE_NAME, csarInfo.getMainTemplateName());
122         assertEquals(csarInfo.getMainTemplateName(), nodeTypeInfo.getTemplateFileName());
123     }
124
125     @Test
126     public void getSoftwareInformationPathTest() {
127         final NonManoConfiguration nonManoConfigurationMock = Mockito.mock(NonManoConfiguration.class);
128         final CsarInfo csarInfo = new CsarInfo(nonManoConfigurationMock);
129         final NonManoFolderType testNonManoFolderType = new NonManoFolderType();
130         testNonManoFolderType.setLocation("sw-location-test");
131         testNonManoFolderType.setType("informational-test");
132         when(nonManoConfigurationMock.getNonManoType(NonManoArtifactType.ONAP_SW_INFORMATION)).thenReturn(testNonManoFolderType);
133         final Map<String, byte[]> csarFileMap = new HashMap<>();
134         final String expectedPath = testNonManoFolderType.getPath() + "/" + "software-file.yaml";
135         csarFileMap.put(expectedPath, new byte[0]);
136         csarInfo.setCsar(csarFileMap);
137         final Optional<String> softwareInformationPath = csarInfo.getSoftwareInformationPath();
138         assertThat("The software information yaml path should be present", softwareInformationPath.isPresent(), is(true));
139         softwareInformationPath.ifPresent(path -> {
140             assertThat("The software information yaml ", path, is(equalTo(expectedPath)));
141         });
142     }
143
144     @Test
145     public void getSoftwareInformationPathTest_emptyCsar() {
146         csarInfo.setCsar(new HashMap<>());
147         final Optional<String> softwareInformationPath = csarInfo.getSoftwareInformationPath();
148         assertThat("The software information yaml path should not be present", softwareInformationPath.isPresent(), is(false));
149     }
150 }