2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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.servlets;
23 import fj.data.Either;
24 import org.apache.commons.codec.binary.Base64;
25 import org.junit.Test;
26 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
28 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
29 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
30 import org.openecomp.sdc.be.externalapi.servlet.ArtifactExternalServlet;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.impl.ServletUtils;
33 import org.openecomp.sdc.be.model.UploadResourceInfo;
34 import org.openecomp.sdc.be.model.User;
35 import org.openecomp.sdc.be.user.UserBusinessLogic;
36 import org.openecomp.sdc.common.datastructure.Wrapper;
37 import org.openecomp.sdc.exception.ResponseFormat;
39 import javax.ws.rs.core.Response;
40 import java.io.IOException;
41 import java.lang.reflect.InvocationTargetException;
42 import java.lang.reflect.Method;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.nio.file.Paths;
47 import java.util.stream.Stream;
49 import static org.junit.Assert.assertNotNull;
50 import static org.junit.Assert.assertTrue;
51 import static org.mockito.Mockito.mock;
53 public class AbstractValidationsServletTest {
54 private static UserBusinessLogic userBusinessLogic = mock(UserBusinessLogic.class);
55 private static ComponentInstanceBusinessLogic componentInstanceBL = mock(ComponentInstanceBusinessLogic.class);
56 private static ComponentsUtils componentsUtils = mock(ComponentsUtils.class);
57 private static ServletUtils servletUtils = mock(ServletUtils.class);
58 private static ResourceImportManager resourceImportManager = mock(ResourceImportManager.class);
59 private static ArtifactsBusinessLogic artifactsBusinessLogic = mock(ArtifactsBusinessLogic.class);
61 private static AbstractValidationsServlet servlet = new ArtifactExternalServlet(userBusinessLogic,
62 componentInstanceBL, componentsUtils, servletUtils, resourceImportManager, artifactsBusinessLogic);
64 private static final String BASIC_TOSCA_TEMPLATE = "tosca_definitions_version: tosca_simple_yaml_%s";
66 @SuppressWarnings("unchecked")
68 public void testGetCsarFromPayload() {
70 String payloadName = "valid_vf.csar";
71 String rootPath = System.getProperty("user.dir");
74 String payloadData = null;
75 Either<Map<String, byte[]>, ResponseFormat> returnValue = null;
77 path = Paths.get(rootPath + "/src/test/resources/valid_vf.csar");
78 data = Files.readAllBytes(path);
79 payloadData = Base64.encodeBase64String(data);
80 UploadResourceInfo resourceInfo = new UploadResourceInfo();
81 resourceInfo.setPayloadName(payloadName);
82 resourceInfo.setPayloadData(payloadData);
83 Method privateMethod = null;
84 privateMethod = AbstractValidationsServlet.class.getDeclaredMethod("getCsarFromPayload", UploadResourceInfo.class);
85 privateMethod.setAccessible(true);
86 returnValue = (Either<Map<String, byte[]>, ResponseFormat>) privateMethod.invoke(servlet, resourceInfo);
87 } catch (IOException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
90 assertTrue(returnValue.isLeft());
91 Map<String, byte[]> csar = returnValue.left().value();
96 public void testValidToscaVersion() throws Exception {
97 Stream.of("1_0", "1_0_0", "1_1", "1_1_0").forEach(this::testValidToscaVersion);
101 private void testValidToscaVersion(String version) {
102 Wrapper<Response> responseWrapper = new Wrapper<>();
103 servlet.validatePayloadIsTosca(responseWrapper, new UploadResourceInfo(), new User(), String.format(BASIC_TOSCA_TEMPLATE, version));
104 assertTrue(responseWrapper.isEmpty());