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