0f790036dac611b0c092f1a5f4cd83150452bf64
[sdc.git] /
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
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";
61
62     private static VnfPackageRepositoryImpl.Configuration config;
63
64     @BeforeClass
65     public static void initConfiguration() {
66         config = new DynamicConfiguration(wireMockRule.port());
67     }
68
69     @Test
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());
74     }
75
76     @Test
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());
81     }
82
83     @Test
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());
88     }
89
90     @Test
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)));
97     }
98
99     @Test
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)));
106     }
107
108     @Test
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)));
117     }
118
119     @Test
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)));
126     }
127
128     @Test
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)));
135     }
136
137     @Test
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)));
147     }
148
149     private static class DynamicConfiguration implements VnfPackageRepositoryImpl.Configuration {
150
151         private final int port;
152
153         private DynamicConfiguration(int port) {
154             this.port = port;
155         }
156
157         @Override
158         public String getGetUri() {
159             return toUri(GET_PATH);
160         }
161
162         @Override
163         public String getDownloadUri() {
164             return toUri(DOWNLOAD_PATH);
165         }
166
167         private String toUri(String path) {
168             return "http://localhost:" + port + path;
169         }
170     }
171
172 }