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.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;
40 import java.net.URISyntaxException;
41 import java.util.Arrays;
42 import java.util.HashMap;
43 import java.util.List;
45 import java.util.Optional;
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;
56 @RunWith(MockitoJUnitRunner.class)
57 public class CsarInfoTest {
59 private CsarInfo csarInfo;
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";
73 public void setup() throws ZipException, URISyntaxException {
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));
79 csarInfo = new CsarInfo(user, CSAR_UUID, payload, RESOURCE_NAME,
80 MAIN_TEMPLATE_NAME, mainTemplateContent, true);
84 public void add2TimesTheSameNodeTest() {
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);
95 assertEquals(ActionStatus.CFVC_LOOP_DETECTED, e.getActionStatus());
96 assertTrue(Arrays.stream(e.getParams()).allMatch(expectParam::contains));
101 public void addMultipleTimesNodeTest() {
104 csarInfo.addNodeToQueue(NEW_NODE_NAME);
105 csarInfo.removeNodeFromQueue();
106 csarInfo.addNodeToQueue(NEW_NODE_NAME);
110 public void csarCheckNodeTypesInfoTest() {
113 Map<String, NodeTypeInfo> nodeTypeInfoMap = csarInfo.extractNodeTypesInfo();
114 NodeTypeInfo nodeTypeInfo = nodeTypeInfoMap.get(NODE_TYPE);
117 assertNotNull(nodeTypeInfo);
118 assertTrue(nodeTypeInfo.getDerivedFrom().contains(DELIVER_FOR));
119 assertEquals(NODE_TYPE, nodeTypeInfo.getType());
121 assertEquals(MAIN_TEMPLATE_NAME, csarInfo.getMainTemplateName());
122 assertEquals(csarInfo.getMainTemplateName(), nodeTypeInfo.getTemplateFileName());
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)));
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));