2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 Nokia Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.vid.asdc.rest;
23 import static com.xebialabs.restito.semantics.Action.ok;
24 import static com.xebialabs.restito.semantics.Action.stringContent;
25 import static org.apache.http.client.config.RequestConfig.custom;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.Matchers.equalToIgnoringCase;
28 import static org.hamcrest.Matchers.is;
29 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
30 import static org.hamcrest.collection.IsMapContaining.hasKey;
31 import static org.junit.Assert.assertTrue;
32 import static org.mockito.Mockito.mock;
33 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID;
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.xebialabs.restito.semantics.Call;
37 import java.io.IOException;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.security.GeneralSecurityException;
41 import java.util.Collections;
42 import java.util.Optional;
43 import java.util.UUID;
44 import javax.net.ssl.SSLContext;
45 import org.apache.http.config.Registry;
46 import org.apache.http.config.RegistryBuilder;
47 import org.apache.http.conn.socket.ConnectionSocketFactory;
48 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
49 import org.apache.http.conn.ssl.SSLContextBuilder;
50 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
51 import org.apache.http.impl.client.CloseableHttpClient;
52 import org.apache.http.impl.client.HttpClientBuilder;
53 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
54 import org.junit.AfterClass;
55 import org.junit.BeforeClass;
56 import org.junit.Test;
57 import org.onap.vid.asdc.AsdcCatalogException;
58 import org.onap.vid.asdc.beans.Service;
59 import org.onap.vid.client.SyncRestClient;
60 import org.onap.vid.testUtils.StubServerUtil;
61 import org.onap.vid.utils.Logging;
64 public class SdcRestClientITTest {
65 private static final String UUID_REGEX = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
66 private static final String[] SUPPORTED_SSL_VERSIONS = {"TLSv1", "TLSv1.2"};
67 private static StubServerUtil stubServer;
68 private static SdcRestClient sdcRestClient;
71 public static void setUpClass() throws GeneralSecurityException {
72 stubServer = new StubServerUtil();
73 stubServer.runSecuredServer();
74 SyncRestClient syncRestClient = new SyncRestClient(createNaiveHttpClient(), mock(Logging.class));
75 String serverUrl = stubServer.constructTargetUrl("https", "");
76 sdcRestClient = new SdcRestClient(serverUrl, "", syncRestClient, mock(Logging.class));
80 public static void tearDown() {
81 stubServer.stopServer();
85 public void shouldDownloadToscaArtifactUsingSecuredEndpoint() throws AsdcCatalogException, IOException {
86 UUID uuid = UUID.randomUUID();
87 String expectedEndpoint = String.format("/sdc/v1/catalog/services/%s/toscaModel", uuid);
89 stubServer.prepareGetCall(
90 expectedEndpoint, stringContent("sampleFileContent"), ok(), "application/octet-stream");
93 Path serviceToscaModel = sdcRestClient.getServiceToscaModel(uuid);
94 serviceToscaModel.toFile().deleteOnExit();
97 assertThat(Files.readAllLines(serviceToscaModel), contains("sampleFileContent"));
98 assertThatRequestHasRequiredHeaders(expectedEndpoint);
102 public void shouldGetServiceDetailsUsingSecuredEndpoint() throws AsdcCatalogException, JsonProcessingException {
103 UUID uuid = UUID.randomUUID();
104 String expectedEndpoint = String.format("/sdc/v1/catalog/services/%s/metadata", uuid);
105 Service expectedService = getExpectedService(uuid.toString());
108 stubServer.prepareGetCall(expectedEndpoint, expectedService, ok());
111 Service actualService = sdcRestClient.getService(uuid);
114 assertThat(actualService, is(expectedService));
115 assertThatRequestHasRequiredHeaders(expectedEndpoint);
118 private void assertThatRequestHasRequiredHeaders(String expectedEndpoint) {
119 Optional<Call> first = stubServer
122 .filter(x -> x.getUri().contains(expectedEndpoint))
125 assertTrue(first.isPresent());
127 assertThat(first.get().getHeaders(), hasKey(equalToIgnoringCase(X_ECOMP_INSTANCE_ID)));
130 private Service getExpectedService(String stringId) {
131 return new Service.ServiceBuilder().setUuid(stringId)
132 .setInvariantUUID(stringId)
133 .setCategory("sampleCategory")
134 .setVersion("sampleVersion")
135 .setName( "sampleName")
136 .setDistributionStatus("sampleDistStatus")
137 .setToscaModelURL("sampleToscaUrl")
138 .setLifecycleState(Service.LifecycleState.CERTIFIED)
139 .setArtifacts(Collections.emptyList())
140 .setResources(Collections.emptyList()).build();
144 private static CloseableHttpClient createNaiveHttpClient() throws GeneralSecurityException {
145 final SSLContext context = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
148 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context, SUPPORTED_SSL_VERSIONS,
149 null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
150 Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
151 .register("https", socketFactory)
154 return HttpClientBuilder.create()
155 .setDefaultRequestConfig(custom().setConnectionRequestTimeout(10000).build())
156 .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
157 .setSSLSocketFactory(socketFactory).build();