3 * Copyright (c) 2018 Huawei Intellectual Property.
5 * Modifications Copyright (c) 2019 Samsung
7 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
19 * Unless required by applicable law or agreed to in writing, software
21 * distributed under the License is distributed on an "AS IS" BASIS,
23 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
27 * limitations under the License.
31 package org.openecomp.sdc.be.components.csar;
33 import static org.junit.Assert.assertTrue;
34 import static org.junit.Assert.assertEquals;
35 import static org.junit.Assert.assertNotNull;
36 import static org.mockito.ArgumentMatchers.any;
37 import static org.mockito.ArgumentMatchers.anyString;
38 import static org.mockito.Mockito.when;
40 import fj.data.Either;
42 import java.io.IOException;
43 import java.net.URISyntaxException;
44 import java.nio.file.Files;
45 import java.nio.file.Path;
46 import java.nio.file.Paths;
47 import java.util.Arrays;
48 import java.util.HashMap;
51 import org.junit.Test;
52 import org.junit.runner.RunWith;
53 import org.mockito.InjectMocks;
54 import org.mockito.Mock;
55 import org.mockito.junit.MockitoJUnitRunner;
57 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
58 import org.openecomp.sdc.be.dao.api.ActionStatus;
59 import org.openecomp.sdc.be.impl.ComponentsUtils;
60 import org.openecomp.sdc.be.model.Resource;
61 import org.openecomp.sdc.be.model.User;
62 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
63 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
64 import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
65 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
66 import org.openecomp.sdc.common.util.ZipUtil;
67 import org.openecomp.sdc.exception.ResponseFormat;
69 @RunWith(MockitoJUnitRunner.class)
70 public class CsarBusinessLogicTest {
73 private CsarBusinessLogic test;
76 private CsarOperation csarOperation;
79 private ToscaOperationFacade toscaOperationFacade;
82 private ComponentsUtils componentsUtils;
87 private static final String CSAR_UUID = "csarUUID";
88 private static final String CSAR_ENTRY = "Definitions/tosca_mock_vf.yaml";
89 private static final String CSAR_METADATA = "TOSCA-Metadata/TOSCA.meta";
90 private static final String CSAR_METADATA_CONTENT = "TOSCA-Meta-File-Version: 1.0\n" +
91 "CSAR-Version: 1.1\n" +
92 "Created-By: OASIS TOSCA TC\n" +
93 "Entry-Definitions:" + CSAR_ENTRY;
94 private static final String CSAR_ENTRY_CONTENT = "tosca_definitions_version: tosca_simple_yaml_1_0\n";
96 private static final String RESOURCE_NAME = "resourceName";
97 private static final String PAYLOAD_NAME = "mock_vf.csar";
100 public void testGetCsarInfo() {
102 Resource resource = new Resource();
103 resource.setName(RESOURCE_NAME);
105 Map<String, byte[]> csar_data = new HashMap<>();
106 csar_data.put(CSAR_METADATA, CSAR_METADATA_CONTENT.getBytes());
107 csar_data.put(CSAR_ENTRY, CSAR_ENTRY_CONTENT.getBytes());
108 when(csarOperation.getCsar(anyString(), any(User.class))).thenReturn(Either.left(csar_data));
111 CsarInfo csarInfo = test.getCsarInfo(resource, null, user, null, CSAR_UUID);
114 assertNotNull(csarInfo);
116 assertEquals(CSAR_UUID, csarInfo.getCsarUUID());
117 assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
118 assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());
120 assertEquals(CSAR_ENTRY_CONTENT, csarInfo.getMainTemplateContent());
121 assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
125 public void testGetCsarInfoWithPayload() throws IOException, URISyntaxException {
127 Resource resource = new Resource();
128 resource.setName(RESOURCE_NAME);
130 Map<String, byte[]> payload = loadPayload(PAYLOAD_NAME);
133 CsarInfo csarInfo = test.getCsarInfo(resource, null, user, payload, CSAR_UUID);
136 assertNotNull(csarInfo);
138 assertEquals(CSAR_UUID, csarInfo.getCsarUUID());
139 assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
140 assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());
142 assertTrue(csarInfo.getMainTemplateContent().startsWith(CSAR_ENTRY_CONTENT));
143 assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
146 @Test(expected = ComponentException.class)
147 public void testGetCsarInfoWithBadData(){
149 Resource resource = new Resource();
150 resource.setName(RESOURCE_NAME);
152 Map<String, byte[]> csar_data = new HashMap<>();
153 when(csarOperation.getCsar(anyString(), any(User.class))).thenReturn(Either.left(csar_data));
156 test.getCsarInfo(resource, null, user, null, CSAR_UUID);
160 public void testValidateCsarBeforeCreate() {
161 Resource resource = new Resource();
162 StorageOperationStatus status = StorageOperationStatus.OK;
163 when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
164 test.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, CSAR_UUID);
167 @Test(expected = ComponentException.class)
168 public void testValidateCsarBeforeCreate_Exists() {
169 Resource resource = new Resource();
170 ResponseFormat responseFormat = new ResponseFormat();
171 StorageOperationStatus status = StorageOperationStatus.ENTITY_ALREADY_EXISTS;
172 when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
173 when(componentsUtils.getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, CSAR_UUID)).thenReturn(responseFormat);
174 test.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID");
177 @Test(expected = ComponentException.class)
178 public void testValidateCsarBeforeCreate_Fail() {
179 Resource resource = new Resource();
181 when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(StorageOperationStatus.EXEUCTION_FAILED);
182 test.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID");
185 public Map<String, byte[]> loadPayload(String payloadName) throws IOException, URISyntaxException {
187 Path path = Paths.get(getClass().getResource("/" + payloadName).toURI());
188 byte[] data = Files.readAllBytes(path);
190 return ZipUtil.readZip(data);