aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/csar/CsarBusinessLogicTest.java
blob: c4b76a2a37d9d268886a67b65dbc851def46c5ff (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
/*

 * Copyright (c) 2018 Huawei Intellectual Property.

 * Modifications Copyright (c) 2019 Samsung

 * 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.

 */

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

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
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.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

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.User;
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.resources.data.auditing.AuditingActionEnum;
import org.openecomp.sdc.common.util.ZipUtil;
import org.openecomp.sdc.exception.ResponseFormat;

@RunWith(MockitoJUnitRunner.class)
public class CsarBusinessLogicTest {

    @InjectMocks
    private CsarBusinessLogic test;

    @Mock
    private CsarOperation csarOperation;

    @Mock
    private ToscaOperationFacade toscaOperationFacade;

    @Mock
    private ComponentsUtils componentsUtils;

    @Mock
    private User user;

    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";

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

        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());
        when(csarOperation.getCsar(anyString(), any(User.class))).thenReturn(Either.left(csar_data));

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

        // then
        assertNotNull(csarInfo);

        assertEquals(CSAR_UUID, csarInfo.getCsarUUID());
        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()
    public void testGetCsarInfoWithPayload() throws IOException, URISyntaxException {
        // given
        Resource resource = new Resource();
        resource.setName(RESOURCE_NAME);

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

        // when
        CsarInfo csarInfo = test.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(expected = ComponentException.class)
    public void testGetCsarInfoWithBadData(){
        // given
        Resource resource = new Resource();
        resource.setName(RESOURCE_NAME);

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

        // when
        test.getCsarInfo(resource, null, user, null, CSAR_UUID);
    }

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

    @Test(expected = ComponentException.class)
    public void testValidateCsarBeforeCreate_Exists() {
        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);
        test.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID");
    }

    @Test(expected = ComponentException.class)
    public void testValidateCsarBeforeCreate_Fail() {
        Resource resource = new Resource();

        when(toscaOperationFacade.validateCsarUuidUniqueness(CSAR_UUID)).thenReturn(StorageOperationStatus.EXEUCTION_FAILED);
        test.validateCsarBeforeCreate(resource, AuditingActionEnum.ARTIFACT_DOWNLOAD, user, "csarUUID");
    }

    public Map<String, byte[]> loadPayload(String payloadName) throws IOException, URISyntaxException {

        Path path = Paths.get(getClass().getResource("/" + payloadName).toURI());
        byte[] data = Files.readAllBytes(path);

        return ZipUtil.readZip(data);
    }
}