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.Before;
26 import org.junit.Test;
27 import org.openecomp.sdc.be.model.UploadResourceInfo;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.common.datastructure.Wrapper;
30 import org.openecomp.sdc.exception.ResponseFormat;
31 import org.slf4j.Logger;
33 import javax.ws.rs.core.Response;
34 import java.io.IOException;
35 import java.lang.reflect.InvocationTargetException;
36 import java.lang.reflect.Method;
37 import java.nio.file.Files;
38 import java.nio.file.Path;
39 import java.nio.file.Paths;
41 import java.util.stream.Stream;
43 import static org.junit.Assert.assertTrue;
44 import static org.mockito.Mockito.mock;
46 public class AbstractValidationsServletTest {
47 private static AbstractValidationsServlet servlet = new AbstractValidationsServlet() {};
49 private static final String BASIC_TOSCA_TEMPLATE = "tosca_definitions_version: tosca_simple_yaml_%s";
52 public void setUp() throws Exception {
53 servlet.initLog(mock(Logger.class));
56 @SuppressWarnings("unchecked")
58 public void testGetScarFromPayload() {
60 String payloadName = "valid_vf.csar";
61 String rootPath = System.getProperty("user.dir");
64 String payloadData = null;
65 Either<Map<String, byte[]>, ResponseFormat> returnValue = null;
67 path = Paths.get(rootPath + "/src/test/resources/valid_vf.csar");
68 data = Files.readAllBytes(path);
69 payloadData = Base64.encodeBase64String(data);
70 UploadResourceInfo resourceInfo = new UploadResourceInfo();
71 resourceInfo.setPayloadName(payloadName);
72 resourceInfo.setPayloadData(payloadData);
73 Method privateMethod = null;
74 privateMethod = AbstractValidationsServlet.class.getDeclaredMethod("getScarFromPayload", UploadResourceInfo.class);
75 privateMethod.setAccessible(true);
76 returnValue = (Either<Map<String, byte[]>, ResponseFormat>) privateMethod.invoke(servlet, resourceInfo);
77 } catch (IOException | NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
80 assertTrue(returnValue.isLeft());
81 Map<String, byte[]> csar = returnValue.left().value();
82 assertTrue(csar != null);
86 public void testValidToscaVersion() throws Exception {
87 Stream.of("1_0", "1_0_0", "1_1", "1_1_0").forEach(this::testValidToscaVersion);
91 private void testValidToscaVersion(String version) {
92 Wrapper<Response> responseWrapper = new Wrapper<>();
93 servlet.validatePayloadIsTosca(responseWrapper, new UploadResourceInfo(), new User(), String.format(BASIC_TOSCA_TEMPLATE, version));
94 assertTrue(responseWrapper.isEmpty());