re base code
[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 fj.data.Either;
24 import org.apache.commons.codec.binary.Base64;
25 import org.junit.Test;
26 import org.openecomp.sdc.be.model.UploadResourceInfo;
27 import org.openecomp.sdc.be.model.User;
28 import org.openecomp.sdc.common.datastructure.Wrapper;
29 import org.openecomp.sdc.exception.ResponseFormat;
30
31 import javax.ws.rs.core.Response;
32 import java.io.IOException;
33 import java.lang.reflect.InvocationTargetException;
34 import java.lang.reflect.Method;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.Map;
39 import java.util.stream.Stream;
40
41 import static org.junit.Assert.assertNotNull;
42 import static org.junit.Assert.assertTrue;
43
44 public class AbstractValidationsServletTest {
45     private static AbstractValidationsServlet servlet = new AbstractValidationsServlet() {};
46
47     private static final String BASIC_TOSCA_TEMPLATE = "tosca_definitions_version: tosca_simple_yaml_%s";
48
49     @SuppressWarnings("unchecked")
50     @Test
51     public void testGetCsarFromPayload() {
52
53         String payloadName = "valid_vf.csar";
54         String rootPath = System.getProperty("user.dir");
55         Path path = null;
56         byte[] data = null;
57         String payloadData = null;
58         Either<Map<String, byte[]>, ResponseFormat> returnValue = null;
59         try {
60             path = Paths.get(rootPath + "/src/test/resources/valid_vf.csar");
61             data = Files.readAllBytes(path);
62             payloadData = Base64.encodeBase64String(data);
63             UploadResourceInfo resourceInfo = new UploadResourceInfo();
64             resourceInfo.setPayloadName(payloadName);
65             resourceInfo.setPayloadData(payloadData);
66             Method privateMethod = null;
67             privateMethod = AbstractValidationsServlet.class.getDeclaredMethod("getCsarFromPayload", UploadResourceInfo.class);
68             privateMethod.setAccessible(true);
69             returnValue = (Either<Map<String, byte[]>, ResponseFormat>) privateMethod.invoke(servlet, resourceInfo);
70         } catch (IOException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
71             e.printStackTrace();
72         }
73         assertTrue(returnValue.isLeft());
74         Map<String, byte[]> csar = returnValue.left().value();
75         assertNotNull(csar);
76     }
77
78     @Test
79     public void testValidToscaVersion() throws Exception {
80         Stream.of("1_0", "1_0_0", "1_1", "1_1_0").forEach(this::testValidToscaVersion);
81     }
82
83
84     private void testValidToscaVersion(String version)  {
85         Wrapper<Response> responseWrapper = new Wrapper<>();
86         servlet.validatePayloadIsTosca(responseWrapper, new UploadResourceInfo(), new User(), String.format(BASIC_TOSCA_TEMPLATE, version));
87         assertTrue(responseWrapper.isEmpty());
88     }
89
90
91
92 }