Added oparent to sdc main
[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
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 listVnfsReturnsInternalServerErrorWhenRemoteClientError() {
85         stubFor(get(GET_PATH).willReturn(aResponse().withStatus(403).withBody("Forbidden")));
86         VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
87         Response response = repository.getVnfPackages(VSP, VERSION, USER);
88         assertEquals(500, response.getStatus());
89         verify(getRequestedFor(urlEqualTo(GET_PATH)));
90     }
91
92     @Test
93     public void listVnfsReturnsInternalServerErrorWhenRemoteReturnsNotOk() {
94         stubFor(get(GET_PATH).willReturn(aResponse().withStatus(201).withBody("Created")));
95         VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
96         Response response = repository.getVnfPackages(VSP, VERSION, USER);
97         assertEquals(500, response.getStatus());
98         verify(getRequestedFor(urlEqualTo(GET_PATH)));
99     }
100
101     @Test
102     public void listVnfsReturnsUnchangedResponse() {
103         final String vnfList = "this is a response body for list of VNFs";
104         stubFor(get(GET_PATH).willReturn(aResponse().withStatus(200).withBody(vnfList)));
105         VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
106         Response response = repository.getVnfPackages(VSP, VERSION, USER);
107         assertEquals(200, response.getStatus());
108         assertEquals(vnfList, response.getEntity());
109         verify(getRequestedFor(urlEqualTo(GET_PATH)));
110     }
111
112     @Test
113     public void downloadVnfsReturnsInternalServerErrorWhenRemoteClientError() {
114         stubFor(get(DOWNLOAD_PATH).willReturn(aResponse().withStatus(403).withBody("{\"error\": \"Permissions\"}")));
115         VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
116         Response response = repository.downloadVnfPackage(VSP, VERSION, CSAR, USER);
117         assertEquals(500, response.getStatus());
118         verify(getRequestedFor(urlEqualTo(DOWNLOAD_PATH)));
119     }
120
121     @Test
122     public void downloadVnfsReturnsInternalServerErrorWhenRemoteReturnsNotOk() {
123         stubFor(get(DOWNLOAD_PATH).willReturn(aResponse().withStatus(201).withBody(new byte[0])));
124         VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
125         Response response = repository.downloadVnfPackage(VSP, VERSION, CSAR, USER);
126         assertEquals(500, response.getStatus());
127         verify(getRequestedFor(urlEqualTo(DOWNLOAD_PATH)));
128     }
129
130     @Test
131     public void downloadVnfsReturnsUnchangedBytes() {
132         final byte[] body = "this is the content of a VNF archive (.csar) file".getBytes(StandardCharsets.ISO_8859_1);
133         stubFor(get(DOWNLOAD_PATH).willReturn(aResponse().withStatus(200).withBody(body)));
134         VnfPackageRepositoryImpl repository = new VnfPackageRepositoryImpl(config);
135         Response response = repository.downloadVnfPackage(VSP, VERSION, CSAR, USER);
136         assertEquals(200, response.getStatus());
137         assertTrue(Arrays.equals(body, response.readEntity(byte[].class)));
138         assertNotNull(response.getHeaderString("Content-Disposition"));
139         verify(getRequestedFor(urlEqualTo(DOWNLOAD_PATH)));
140     }
141
142     private static class DynamicConfiguration implements VnfPackageRepositoryImpl.Configuration {
143
144         private final int port;
145
146         private DynamicConfiguration(int port) {
147             this.port = port;
148         }
149
150         @Override
151         public String getGetUri() {
152             return toUri(GET_PATH);
153         }
154
155         @Override
156         public String getDownloadUri() {
157             return toUri(DOWNLOAD_PATH);
158         }
159
160         private String toUri(String path) {
161             return "http://localhost:" + port + path;
162         }
163     }
164
165 }