Merge "Additinal fixes for resources not being released"
[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 static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.mock;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.lang.reflect.InvocationTargetException;
29 import java.lang.reflect.Method;
30 import java.nio.file.Files;
31 import java.nio.file.Path;
32 import java.nio.file.Paths;
33 import java.util.List;
34 import java.util.Map;
35 import java.util.stream.Stream;
36
37 import javax.servlet.http.HttpServletRequest;
38 import javax.ws.rs.core.Response;
39
40 import org.apache.commons.codec.binary.Base64;
41 import org.apache.commons.lang3.tuple.ImmutablePair;
42 import org.apache.tinkerpop.gremlin.structure.T;
43 import org.glassfish.grizzly.servlet.ServletUtils;
44 import org.junit.Assert;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
48 import org.openecomp.sdc.be.dao.api.ActionStatus;
49 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
50 import org.openecomp.sdc.be.impl.ComponentsUtils;
51 import org.openecomp.sdc.be.model.Resource;
52 import org.openecomp.sdc.be.model.UploadResourceInfo;
53 import org.openecomp.sdc.be.model.User;
54 import org.openecomp.sdc.be.servlets.ResourceUploadServlet.ResourceAuthorityTypeEnum;
55 import org.openecomp.sdc.common.datastructure.Wrapper;
56 import org.openecomp.sdc.exception.ResponseFormat;
57 import org.slf4j.Logger;
58
59 import com.google.common.base.Supplier;
60 import com.google.gson.Gson;
61
62 import aj.org.objectweb.asm.Type;
63 import fj.data.Either;
64
65 public class AbstractValidationsServletTest {
66         private static AbstractValidationsServlet servlet = new AbstractValidationsServlet() {
67         };
68
69         private static final String BASIC_TOSCA_TEMPLATE = "tosca_definitions_version: tosca_simple_yaml_%s";
70
71         @Before
72         public void setUp() throws Exception {
73                 servlet.initLog(mock(Logger.class));
74         }
75
76         
77         @SuppressWarnings("unchecked")
78         @Test
79         public void testGetScarFromPayload() {
80
81                 String payloadName = "valid_vf.csar";
82                 String rootPath = System.getProperty("user.dir");
83                 Path path = null;
84                 byte[] data = null;
85                 String payloadData = null;
86                 Either<Map<String, byte[]>, ResponseFormat> returnValue = null;
87                 try {
88                         path = Paths.get(rootPath + "/src/test/resources/valid_vf.csar");
89                         data = Files.readAllBytes(path);
90                         payloadData = Base64.encodeBase64String(data);
91                         UploadResourceInfo resourceInfo = new UploadResourceInfo();
92                         resourceInfo.setPayloadName(payloadName);
93                         resourceInfo.setPayloadData(payloadData);
94                         Method privateMethod = null;
95                         privateMethod = AbstractValidationsServlet.class.getDeclaredMethod("getScarFromPayload",
96                                         UploadResourceInfo.class);
97                         privateMethod.setAccessible(true);
98                         returnValue = (Either<Map<String, byte[]>, ResponseFormat>) privateMethod.invoke(servlet, resourceInfo);
99                 } catch (IOException | NoSuchMethodException | SecurityException | IllegalAccessException
100                                 | IllegalArgumentException | InvocationTargetException e) {
101                         e.printStackTrace();
102                 }
103                 assertTrue(returnValue.isLeft());
104                 Map<String, byte[]> csar = returnValue.left().value();
105                 assertTrue(csar != null);
106         }
107
108         @Test
109         public void testValidToscaVersion() throws Exception {
110                 Stream.of("1_0", "1_0_0", "1_1", "1_1_0").forEach(this::testValidToscaVersion);
111         }
112
113         private void testValidToscaVersion(String version) {
114                 Wrapper<Response> responseWrapper = new Wrapper<>();
115                 servlet.validatePayloadIsTosca(responseWrapper, new UploadResourceInfo(), new User(),
116                                 String.format(BASIC_TOSCA_TEMPLATE, version));
117                 assertTrue(responseWrapper.isEmpty());
118         }
119
120         
121 }