2 * Copyright © 2016-2018 European Support Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.openecomp.sdcrests.vsp.rest.services;
19 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
20 import static com.github.tomakehurst.wiremock.client.WireMock.get;
21 import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
22 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
23 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
24 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
25 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import com.github.tomakehurst.wiremock.junit.WireMockRule;
32 import java.nio.charset.StandardCharsets;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import javax.ws.rs.core.Response;
37 import org.junit.BeforeClass;
38 import org.junit.ClassRule;
39 import org.junit.Test;
40 import org.openecomp.sdc.versioning.dao.types.Version;
43 * Configuration testing.
44 * WireMock testing of remote calls.
49 public class VnfPackageRepositoryImplTest {
51 private static final String GET_PATH = "/get";
52 private static final String DOWNLOAD_PATH = "/download";
55 public static final WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());
57 private static final String VSP = "anyVsp";
58 private static final String VERSION = "anyVersion";
59 private static final String USER = "anyUser";
60 private static final String CSAR = "anyCsar";
62 private static VnfPackageRepositoryImpl.Configuration config;
65 public static void initConfiguration() {
66 config = new DynamicConfiguration(wireMockRule.port());
70 public void versionFoundWhenInList() {
71 VnfPackageRepositoryImpl vnfRepository = new VnfPackageRepositoryImpl();
72 List<Version> versions = Arrays.asList(new Version("1243"), new Version("3434"), new Version("398"));
73 assertTrue("Expected to find the version", vnfRepository.findVersion(versions, "3434").isPresent());
77 public void versionNotFoundWhenInList() {
78 VnfPackageRepositoryImpl vnfRepository = new VnfPackageRepositoryImpl();
79 List<Version> versions = Collections.singletonList(new Version("1243"));
80 assertFalse("Did not expect to find the version", vnfRepository.findVersion(versions, "3434").isPresent());
84 public void configurationLoadedFromFile() {
85 final String prefix = "http://10.57.30.20:1111/";
86 assertEquals(prefix + "download-vnf-31", new VnfPackageRepositoryImpl.FileConfiguration().getDownloadUri());
87 assertEquals(prefix + "get-vnf-13", new VnfPackageRepositoryImpl.FileConfiguration().getGetUri());
91 public void listVnfsReturnsInternalServerErrorWhenRemoteClientError() {
92 stubFor(get(GET_PATH).willReturn(aResponse().withStatus(403).withBody("Forbidden")));
93 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
94 Response response = repository.getVnfPackages(VSP, VERSION, USER);
95 assertEquals(500, response.getStatus());
96 verify(getRequestedFor(urlEqualTo(GET_PATH)));
100 public void listVnfsReturnsInternalServerErrorWhenRemoteReturnsNotOk() {
101 stubFor(get(GET_PATH).willReturn(aResponse().withStatus(201).withBody("Created")));
102 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
103 Response response = repository.getVnfPackages(VSP, VERSION, USER);
104 assertEquals(500, response.getStatus());
105 verify(getRequestedFor(urlEqualTo(GET_PATH)));
109 public void listVnfsReturnsUnchangedResponse() {
110 final String vnfList = "this is a response body for list of VNFs";
111 stubFor(get(GET_PATH).willReturn(aResponse().withStatus(200).withBody(vnfList)));
112 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
113 Response response = repository.getVnfPackages(VSP, VERSION, USER);
114 assertEquals(200, response.getStatus());
115 assertEquals(vnfList, response.getEntity());
116 verify(getRequestedFor(urlEqualTo(GET_PATH)));
120 public void downloadVnfsReturnsInternalServerErrorWhenRemoteClientError() {
121 stubFor(get(DOWNLOAD_PATH).willReturn(aResponse().withStatus(403).withBody("{\"error\": \"Permissions\"}")));
122 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
123 Response response = repository.downloadVnfPackage(VSP, VERSION, CSAR, USER);
124 assertEquals(500, response.getStatus());
125 verify(getRequestedFor(urlEqualTo(DOWNLOAD_PATH)));
129 public void downloadVnfsReturnsInternalServerErrorWhenRemoteReturnsNotOk() {
130 stubFor(get(DOWNLOAD_PATH).willReturn(aResponse().withStatus(201).withBody(new byte[0])));
131 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
132 Response response = repository.downloadVnfPackage(VSP, VERSION, CSAR, USER);
133 assertEquals(500, response.getStatus());
134 verify(getRequestedFor(urlEqualTo(DOWNLOAD_PATH)));
138 public void downloadVnfsReturnsUnchangedBytes() {
139 final byte[] body = "this is the content of a VNF archive (.csar) file".getBytes(StandardCharsets.ISO_8859_1);
140 stubFor(get(DOWNLOAD_PATH).willReturn(aResponse().withStatus(200).withBody(body)));
141 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
142 Response response = repository.downloadVnfPackage(VSP, VERSION, CSAR, USER);
143 assertEquals(200, response.getStatus());
144 assertTrue(Arrays.equals(body, response.readEntity(byte[].class)));
145 assertNotNull(response.getHeaderString("Content-Disposition"));
146 verify(getRequestedFor(urlEqualTo(DOWNLOAD_PATH)));
149 private static class DynamicConfiguration implements VnfPackageRepositoryImpl.Configuration {
151 private final int port;
153 private DynamicConfiguration(int port) {
158 public String getGetUri() {
159 return toUri(GET_PATH);
163 public String getDownloadUri() {
164 return toUri(DOWNLOAD_PATH);
167 private String toUri(String path) {
168 return "http://localhost:" + port + path;