aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogicTest.java
blob: fec288a953ceeb2bf22010202d05eb68768f63d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * -
 *  ============LICENSE_START=======================================================
 *  Copyright (c) 2018 Huawei Intellectual Property.
 *  Modifications Copyright (c) 2019 Samsung.
 *  Modifications Copyright (c) 2021 Nordix Foundation.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.be.components.csar;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import fj.data.Either;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.openecomp.sdc.be.components.impl.BaseBusinessLogicMock;
import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.VendorSoftwareProduct;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.CsarOperation;
import org.openecomp.sdc.be.model.operations.impl.ModelOperation;
import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
import org.openecomp.sdc.common.zip.ZipUtils;
import org.openecomp.sdc.common.zip.exception.ZipException;
import org.openecomp.sdc.exception.ResponseFormat;


class CsarBusinessLogicTest extends BaseBusinessLogicMock {

    private final CsarOperation csarOperation = Mockito.mock(CsarOperation.class);
    private final ToscaOperationFacade toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
    private final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
    private final User user = Mockito.mock(User.class);
    private final YamlTemplateParsingHandler yamlHandler = Mockito.mock(YamlTemplateParsingHandler.class);
    private final ModelOperation modelOperation = Mockito.mock(ModelOperation.class);

    private final CsarBusinessLogic csarBusinessLogic = new CsarBusinessLogic(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation,
        interfaceOperation, interfaceLifecycleTypeOperation, yamlHandler, artifactToscaOperation, modelOperation);

    private static final String CSAR_UUID = "csarUUID";
    private static final String CSAR_ENTRY = "Definitions/tosca_mock_vf.yaml";
    private static final String CSAR_METADATA = "TOSCA-Metadata/TOSCA.meta";
    private static final String CSAR_METADATA_CONTENT = "TOSCA-Meta-File-Version: 1.0\n" +
            "CSAR-Version: 1.1\n" +
            "Created-By: OASIS TOSCA TC\n" +
            "Entry-Definitions:" + CSAR_ENTRY;
    private static final String CSAR_ENTRY_CONTENT = "tosca_definitions_version: tosca_simple_yaml_1_0\n";

    private static final String RESOURCE_NAME = "resourceName";
    private static final String PAYLOAD_NAME = "mock_vf.csar";

    @BeforeEach
    void setUp() throws Exception {
        csarBusinessLogic.setCsarOperation(csarOperation);
        csarBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
        csarBusinessLogic.setComponentsUtils(componentsUtils);
    }

    @Test
    void testGetCsarInfo() {
        // given
        Resource resource = new Resource();
        resource.setName(RESOURCE_NAME);
        resource.setCsarUUID(CSAR_UUID);
        resource.setCsarVersionId("csarVersionId");

        Map<String, byte[]> csar_data = new HashMap<>();
        csar_data.put(CSAR_METADATA, CSAR_METADATA_CONTENT.getBytes());
        csar_data.put(CSAR_ENTRY, CSAR_ENTRY_CONTENT.getBytes());
        final var vendorSoftwareProduct = new VendorSoftwareProduct();
        vendorSoftwareProduct.setFileMap(csar_data);
        vendorSoftwareProduct.setModelList(Collections.emptyList());
        when(csarOperation.findVsp(eq(resource.getCsarUUID()), eq(resource.getCsarVersionId()), any(User.class)))
            .thenReturn(Optional.of(vendorSoftwareProduct));

        // when
        final CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID);

        // then
        assertNotNull(csarInfo);

        assertEquals(resource.getCsarUUID(), csarInfo.getCsarUUID());
        assertEquals(resource.getCsarVersionId(), csarInfo.getCsarVersionId());
        assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
        assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());

        assertEquals(CSAR_ENTRY_CONTENT, csarInfo.getMainTemplateContent());
        assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
    }

    @Test
    void testGetCsarInfo_vspWithModelAndResourceWithInvalidModel() {
        final var resource = new Resource();
        resource.setCsarUUID(CSAR_UUID);
        final String csarVersionId = "csarVersionId";
        resource.setCsarVersionId(csarVersionId);
        resource.setModel("model1");
        var vendorSoftwareProduct = new VendorSoftwareProduct();
        final List<String> modelList = List.of("model2", "model3");
        vendorSoftwareProduct.setModelList(modelList);

        when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct));

        final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
            () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
        assertEquals(ActionStatus.VSP_MODEL_NOT_ALLOWED, actualException.getActionStatus());
        assertEquals(2, actualException.getParams().length);
        assertEquals(resource.getModel(), actualException.getParams()[0]);
        assertEquals(String.join(", ", modelList), actualException.getParams()[1]);
    }


    @Test
    void testGetCsarInfo_vspWithNoModelAndResourceWithInvalidModel() {
        final var resource = new Resource();
        resource.setCsarUUID(CSAR_UUID);
        final String csarVersionId = "csarVersionId";
        resource.setCsarVersionId(csarVersionId);
        resource.setModel("model1");
        var vendorSoftwareProduct = new VendorSoftwareProduct();
        final List<String> modelList = new ArrayList<>();
        vendorSoftwareProduct.setModelList(modelList);

        when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct));

        final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
            () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
        assertEquals(ActionStatus.VSP_MODEL_NOT_ALLOWED, actualException.getActionStatus());
        assertEquals(2, actualException.getParams().length);
        assertEquals(resource.getModel(), actualException.getParams()[0]);
        assertEquals("SDC AID", actualException.getParams()[1]);
    }

    @Test
    void testGetCsarInfo_vspWithModelAndResourceWithNoModel() {
        final var resource = new Resource();
        resource.setCsarUUID(CSAR_UUID);
        final String csarVersionId = "csarVersionId";
        resource.setCsarVersionId(csarVersionId);
        resource.setModel(null);
        var vendorSoftwareProduct = new VendorSoftwareProduct();
        final List<String> modelList = List.of("model2", "model3");
        vendorSoftwareProduct.setModelList(modelList);

        when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenReturn(Optional.of(vendorSoftwareProduct));

        final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
            () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
        assertEquals(ActionStatus.VSP_MODEL_NOT_ALLOWED, actualException.getActionStatus());
        assertEquals(2, actualException.getParams().length);
        assertEquals("SDC AID", actualException.getParams()[0]);
        assertEquals(String.join(", ", modelList), actualException.getParams()[1]);
    }

    @Test
    void testGetCsarInfo_vspWithNoModelAndResourceWithNoModel() {
        final var resource = new Resource();
        resource.setCsarUUID(CSAR_UUID);
        final String csarVersionId = "csarVersionId";
        resource.setCsarVersionId(csarVersionId);
        resource.setModel(null);
        var vendorSoftwareProduct = new VendorSoftwareProduct();
        final List<String> modelList = new ArrayList<>();
        vendorSoftwareProduct.setModelList(modelList);
        when(csarOperation.findVsp(resource.getCsarUUID(), resource.getCsarVersionId(), user)).thenThrow(new RuntimeException());

        final ByActionStatusComponentException actualException = assertThrows(ByActionStatusComponentException.class,
            () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
        assertEquals(ActionStatus.VSP_FIND_ERROR, actualException.getActionStatus());
        assertEquals(2, actualException.getParams().length);
        assertEquals(resource.getCsarUUID(), actualException.getParams()[0]);
        assertEquals(resource.getCsarVersionId(), actualException.getParams()[1]);
    }

    @Test
    void testGetCsarInfoWithPayload() throws IOException, URISyntaxException, ZipException {
        // given
        Resource resource = new Resource();
        resource.setName(RESOURCE_NAME);

        Map<String, byte[]> payload = loadPayload(PAYLOAD_NAME);

        // when
        CsarInfo csarInfo = csarBusinessLogic.getCsarInfo(resource, null, user, payload, CSAR_UUID);

        // then
        assertNotNull(csarInfo);

        assertEquals(CSAR_UUID, csarInfo.getCsarUUID());
        assertEquals(CSAR_ENTRY, csarInfo.getMainTemplateName());
        assertEquals(RESOURCE_NAME, csarInfo.getVfResourceName());

        assertTrue(csarInfo.getMainTemplateContent().startsWith(CSAR_ENTRY_CONTENT));
        assertTrue(csarInfo.getCsar().keySet().containsAll(Arrays.asList(CSAR_ENTRY, CSAR_METADATA)));
    }

    @Test
    void testGetCsarInfoWithBadData(){
        // given
        Resource resource = new Resource();
        resource.setName(RESOURCE_NAME);

        Map<String, byte[]> csar_data = new HashMap<>();
        when(csarOperation.findVspLatestPackage(anyString(), any(User.class))).thenReturn(Either.left(csar_data));

        // when/then
        assertThrows(ComponentException.class, () -> csarBusinessLogic.getCsarInfo(resource, null, user, null, CSAR_UUID));
    }

    @Test
    void testValidateCsarBeforeCreate() {
        Resource resource = new Resource();
        StorageOperationStatus status = StorageOperationStatus.OK;
        when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
        csarBusinessLogic.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, CSAR_UUID);
    }

    @Test
    void testValidateCsarBeforeCreate_ResourceExists() {
        Resource resource = new Resource();
        ResponseFormat responseFormat = new ResponseFormat();
        StorageOperationStatus status = StorageOperationStatus.ENTITY_ALREADY_EXISTS;
        when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
        when(componentsUtils.getResponseFormat(ActionStatus.VSP_ALREADY_EXISTS, CSAR_UUID)).thenReturn(responseFormat);
        assertThrows(ComponentException.class, () -> csarBusinessLogic
            .validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, CSAR_UUID));
    }

    @Test
    void testValidateCsarBeforeCreate_ServiceExists() {
        final var  service = new Service();
        final var status = StorageOperationStatus.ENTITY_ALREADY_EXISTS;
        when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
        csarBusinessLogic.validateCsarBeforeCreate(service, CSAR_UUID);
        verify(toscaOperationFacade).validateCsarUuidUniqueness(CSAR_UUID);
    }

    @Test
    void testValidateCsarBeforeCreate_ServiceValidateError() {
        final var service = new Service();
        final var status = StorageOperationStatus.GENERAL_ERROR;
        when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(status);
        when(componentsUtils.convertFromStorageResponse(status)).thenReturn(ActionStatus.GENERAL_ERROR);
        assertThrows(ComponentException.class, () -> csarBusinessLogic.validateCsarBeforeCreate(service, CSAR_UUID));
        verify(toscaOperationFacade).validateCsarUuidUniqueness(CSAR_UUID);
    }

    @Test
    void testValidateCsarBeforeCreate_Fail() {
        Resource resource = new Resource();
        String csarUUID = "csarUUID";
        when(toscaOperationFacade.validateCsarUuidUniqueness(csarUUID)).thenReturn(StorageOperationStatus.EXEUCTION_FAILED);
        when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.EXEUCTION_FAILED)).thenReturn(ActionStatus.GENERAL_ERROR);
        assertThrows(ComponentException.class, () -> csarBusinessLogic
            .validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID"));
    }

    private Map<String, byte[]> loadPayload(String payloadName) throws IOException, URISyntaxException, ZipException {
        var path = Paths.get(getClass().getResource("/" + payloadName).toURI());
        byte[] data = Files.readAllBytes(path);

        return ZipUtils.readZip(data, false);
    }
}