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());
56 private static final String VSP = "anyVsp";
57 private static final String VERSION = "anyVersion";
58 private static final String USER = "anyUser";
59 private static final String CSAR = "anyCsar";
61 private static VnfPackageRepositoryImpl.Configuration config;
64 public static void initConfiguration() {
65 config = new DynamicConfiguration(wireMockRule.port());
69 public void versionFoundWhenInList() {
70 VnfPackageRepositoryImpl vnfRepository = new VnfPackageRepositoryImpl();
71 List<Version> versions = Arrays.asList(new Version("1243"), new Version("3434"), new Version("398"));
72 assertTrue("Expected to find the version", vnfRepository.findVersion(versions, "3434").isPresent());
76 public void versionNotFoundWhenInList() {
77 VnfPackageRepositoryImpl vnfRepository = new VnfPackageRepositoryImpl();
78 List<Version> versions = Collections.singletonList(new Version("1243"));
79 assertFalse("Did not expect to find the version", vnfRepository.findVersion(versions, "3434").isPresent());
83 public void configurationLoadedFromFile() {
84 final String prefix = "http://10.57.30.20:1111/";
85 assertEquals(prefix + "download-vnf-31", new VnfPackageRepositoryImpl.FileConfiguration().getDownloadUri());
86 assertEquals(prefix + "get-vnf-13", new VnfPackageRepositoryImpl.FileConfiguration().getGetUri());
90 public void listVnfsReturnsInternalServerErrorWhenRemoteClientError() {
91 stubFor(get(GET_PATH).willReturn(aResponse().withStatus(403)));
92 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
93 Response response = repository.getVnfPackages(VSP, VERSION, USER);
94 assertEquals(500, response.getStatus());
95 verify(getRequestedFor(urlEqualTo(GET_PATH)));
99 public void listVnfsReturnsInternalServerErrorWhenRemoteReturnsNotOk() {
100 stubFor(get(GET_PATH).willReturn(aResponse().withStatus(204)));
101 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
102 Response response = repository.getVnfPackages(VSP, VERSION, USER);
103 assertEquals(500, response.getStatus());
104 verify(getRequestedFor(urlEqualTo(GET_PATH)));
108 public void listVnfsReturnsUnchangedResponse() {
109 final String vnfList = "this is a response body for list of VNFs";
110 stubFor(get(GET_PATH).willReturn(aResponse().withStatus(200).withBody(vnfList)));
111 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
112 Response response = repository.getVnfPackages(VSP, VERSION, USER);
113 assertEquals(200, response.getStatus());
114 assertEquals(vnfList, response.getEntity());
115 verify(getRequestedFor(urlEqualTo(GET_PATH)));
119 public void downloadVnfsReturnsInternalServerErrorWhenRemoteClientError() {
120 stubFor(get(DOWNLOAD_PATH).willReturn(aResponse().withStatus(403)));
121 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
122 Response response = repository.downloadVnfPackage(VSP, VERSION, CSAR, USER);
123 assertEquals(500, response.getStatus());
124 verify(getRequestedFor(urlEqualTo(DOWNLOAD_PATH)));
128 public void downloadVnfsReturnsInternalServerErrorWhenRemoteReturnsNotOk() {
129 stubFor(get(DOWNLOAD_PATH).willReturn(aResponse().withStatus(204)));
130 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
131 Response response = repository.downloadVnfPackage(VSP, VERSION, CSAR, USER);
132 assertEquals(500, response.getStatus());
133 verify(getRequestedFor(urlEqualTo(DOWNLOAD_PATH)));
137 public void downloadVnfsReturnsUnchangedBytes() {
138 final byte[] body = "this is the content of a VNF archive (.csar) file".getBytes(StandardCharsets.ISO_8859_1);
139 stubFor(get(DOWNLOAD_PATH).willReturn(aResponse().withStatus(200).withBody(body)));
140 VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
141 Response response = repository.downloadVnfPackage(VSP, VERSION, CSAR, USER);
142 assertEquals(200, response.getStatus());
143 assertTrue(Arrays.equals(body, response.readEntity(byte[].class)));
144 assertNotNull(response.getHeaderString("Content-Disposition"));
145 verify(getRequestedFor(urlEqualTo(DOWNLOAD_PATH)));
148 private static class DynamicConfiguration implements VnfPackageRepositoryImpl.Configuration {
150 private final int port;
152 private DynamicConfiguration(int port) {
157 public String getGetUri() {
158 return toUri(GET_PATH);
162 public String getDownloadUri() {
163 return toUri(DOWNLOAD_PATH);
166 private String toUri(String path) {
167 return "http://localhost:" + port + path;