Catalog alignment
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / AbstractValidationsServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
23 import org.apache.commons.codec.binary.Base64;
24 import org.junit.Test;
25 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
26 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
27 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
28 import org.openecomp.sdc.be.externalapi.servlet.ArtifactExternalServlet;
29 import org.openecomp.sdc.be.impl.ComponentsUtils;
30 import org.openecomp.sdc.be.impl.ServletUtils;
31 import org.openecomp.sdc.be.model.UploadResourceInfo;
32 import org.openecomp.sdc.be.model.User;
33 import org.openecomp.sdc.be.user.UserBusinessLogic;
34 import org.openecomp.sdc.common.datastructure.Wrapper;
35
36 import javax.ws.rs.core.Response;
37 import java.io.IOException;
38 import java.lang.reflect.InvocationTargetException;
39 import java.lang.reflect.Method;
40 import java.nio.file.Files;
41 import java.nio.file.Path;
42 import java.nio.file.Paths;
43 import java.util.Map;
44 import java.util.stream.Stream;
45
46 import static org.junit.Assert.assertNotNull;
47 import static org.junit.Assert.assertTrue;
48 import static org.mockito.Mockito.mock;
49
50 public class AbstractValidationsServletTest {
51     private static UserBusinessLogic userBusinessLogic = mock(UserBusinessLogic.class);
52     private static ComponentInstanceBusinessLogic componentInstanceBL = mock(ComponentInstanceBusinessLogic.class);
53     private static ComponentsUtils componentsUtils = mock(ComponentsUtils.class);
54     private static ServletUtils servletUtils = mock(ServletUtils.class);
55     private static ResourceImportManager resourceImportManager = mock(ResourceImportManager.class);
56     private static ArtifactsBusinessLogic artifactsBusinessLogic = mock(ArtifactsBusinessLogic.class);
57
58     private static AbstractValidationsServlet servlet = new ArtifactExternalServlet(userBusinessLogic,
59             componentInstanceBL, componentsUtils, servletUtils,  resourceImportManager, artifactsBusinessLogic);
60
61     private static final String BASIC_TOSCA_TEMPLATE = "tosca_definitions_version: tosca_simple_yaml_%s";
62
63     @SuppressWarnings("unchecked")
64     @Test
65     public void testGetCsarFromPayload() {
66
67         String payloadName = "valid_vf.csar";
68         String rootPath = System.getProperty("user.dir");
69         Path path;
70         byte[] data;
71         String payloadData;
72         Map<String, byte[]> returnValue = null;
73         try {
74             path = Paths.get(rootPath + "/src/test/resources/valid_vf.csar");
75             data = Files.readAllBytes(path);
76             payloadData = Base64.encodeBase64String(data);
77             UploadResourceInfo resourceInfo = new UploadResourceInfo();
78             resourceInfo.setPayloadName(payloadName);
79             resourceInfo.setPayloadData(payloadData);
80             Method privateMethod = null;
81             privateMethod = AbstractValidationsServlet.class.getDeclaredMethod("getCsarFromPayload", UploadResourceInfo.class);
82             privateMethod.setAccessible(true);
83             returnValue = (Map<String, byte[]>) privateMethod.invoke(servlet, resourceInfo);
84         } catch (IOException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
85             e.printStackTrace();
86         }
87         assertNotNull(returnValue);
88     }
89
90     @Test
91     public void testValidToscaVersion() throws Exception {
92         Stream.of("1_0", "1_0_0", "1_1", "1_1_0").forEach(this::testValidToscaVersion);
93     }
94
95
96     private void testValidToscaVersion(String version)  {
97         Wrapper<Response> responseWrapper = new Wrapper<>();
98         servlet.validatePayloadIsTosca(responseWrapper, new UploadResourceInfo(), new User(), String.format(BASIC_TOSCA_TEMPLATE, version));
99         assertTrue(responseWrapper.isEmpty());
100     }
101
102
103
104 }