Migrated VNF to JAX-RS HTTP client
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vnf-repository-rest-services / src / test / java / org / openecomp / sdcrests / vsp / rest / services / VnfPackageRepositoryImplTest.java
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.openecomp.sdcrests.vsp.rest.services;
18
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;
30
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;
41
42 /**
43  * Configuration testing.
44  * WireMock testing of remote calls.
45  *
46  * @author evitaliy
47  * @since 19 Jul 2018
48  */
49 public class VnfPackageRepositoryImplTest {
50
51     private static final String GET_PATH = "/get";
52     private static final String DOWNLOAD_PATH = "/download";
53
54     @ClassRule
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";
60
61     private static VnfPackageRepositoryImpl.Configuration config;
62
63     @BeforeClass
64     public static void initConfiguration() {
65         config = new DynamicConfiguration(wireMockRule.port());
66     }
67
68     @Test
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());
73     }
74
75     @Test
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());
80     }
81
82     @Test
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());
87     }
88
89     @Test
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)));
96     }
97
98     @Test
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)));
105     }
106
107     @Test
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)));
116     }
117
118     @Test
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)));
125     }
126
127     @Test
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)));
134     }
135
136     @Test
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)));
146     }
147
148     private static class DynamicConfiguration implements VnfPackageRepositoryImpl.Configuration {
149
150         private final int port;
151
152         private DynamicConfiguration(int port) {
153             this.port = port;
154         }
155
156         @Override
157         public String getGetUri() {
158             return toUri(GET_PATH);
159         }
160
161         @Override
162         public String getDownloadUri() {
163             return toUri(DOWNLOAD_PATH);
164         }
165
166         private String toUri(String path) {
167             return "http://localhost:" + port + path;
168         }
169     }
170
171 }