Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / csar / CsarBusinessLogicTest.java
1 /*
2
3  * Copyright (c) 2018 Huawei Intellectual Property.
4
5  * Modifications Copyright (c) 2019 Samsung
6
7  * Licensed under the Apache License, Version 2.0 (the "License");
8
9  * you may not use this file except in compliance with the License.
10
11  * You may obtain a copy of the License at
12
13  *
14
15  *     http://www.apache.org/licenses/LICENSE-2.0
16
17  *
18
19  * Unless required by applicable law or agreed to in writing, software
20
21  * distributed under the License is distributed on an "AS IS" BASIS,
22
23  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
25  * See the License for the specific language governing permissions and
26
27  * limitations under the License.
28
29  */
30
31 package org.openecomp.sdc.be.components.csar;
32
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;
39
40 import fj.data.Either;
41
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;
49 import java.util.Map;
50
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;
56
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;
68
69 @RunWith(MockitoJUnitRunner.class)
70 public class CsarBusinessLogicTest {
71
72     @InjectMocks
73     private CsarBusinessLogic test;
74
75     @Mock
76     private CsarOperation csarOperation;
77
78     @Mock
79     private ToscaOperationFacade toscaOperationFacade;
80
81     @Mock
82     private ComponentsUtils componentsUtils;
83
84     @Mock
85     private User user;
86
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";
95
96     private static final String RESOURCE_NAME = "resourceName";
97     private static final String PAYLOAD_NAME = "mock_vf.csar";
98
99     @Test()
100     public void testGetCsarInfo() {
101         // given
102         Resource resource = new Resource();
103         resource.setName(RESOURCE_NAME);
104
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));
109
110         // when
111         CsarInfo csarInfo = test.getCsarInfo(resource, null, user, null, CSAR_UUID);
112
113         // then
114         assertNotNull(csarInfo);
115
116         assertEquals(CSAR_UUID, csarInfo.getCsarUUID());
117         assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
118         assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());
119
120         assertEquals(CSAR_ENTRY_CONTENT, csarInfo.getMainTemplateContent());
121         assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
122     }
123
124     @Test()
125     public void testGetCsarInfoWithPayload() throws IOException, URISyntaxException {
126         // given
127         Resource resource = new Resource();
128         resource.setName(RESOURCE_NAME);
129
130         Map<String, byte[]> payload = loadPayload(PAYLOAD_NAME);
131
132         // when
133         CsarInfo csarInfo = test.getCsarInfo(resource, null, user, payload, CSAR_UUID);
134
135         // then
136         assertNotNull(csarInfo);
137
138         assertEquals(CSAR_UUID, csarInfo.getCsarUUID());
139         assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
140         assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());
141
142         assertTrue(csarInfo.getMainTemplateContent().startsWith(CSAR_ENTRY_CONTENT));
143         assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
144     }
145
146     @Test(expected = ComponentException.class)
147     public void testGetCsarInfoWithBadData(){
148         // given
149         Resource resource = new Resource();
150         resource.setName(RESOURCE_NAME);
151
152         Map<String, byte[]> csar_data = new HashMap<>();
153         when(csarOperation.getCsar(anyString(), any(User.class))).thenReturn(Either.left(csar_data));
154
155         // when
156         test.getCsarInfo(resource, null, user, null, CSAR_UUID);
157     }
158
159     @Test
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);
165     }
166
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");
175     }
176
177     @Test(expected = ComponentException.class)
178     public void testValidateCsarBeforeCreate_Fail() {
179         Resource resource = new Resource();
180
181         when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(StorageOperationStatus.EXEUCTION_FAILED);
182         test.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID");
183     }
184
185     public Map<String, byte[]> loadPayload(String payloadName) throws IOException, URISyntaxException {
186
187         Path path = Paths.get(getClass().getResource("/" + payloadName).toURI());
188         byte[] data = Files.readAllBytes(path);
189
190         return ZipUtil.readZip(data);
191     }
192 }