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.hasItems;
28 import static org.hamcrest.Matchers.is;
29 import static org.hamcrest.Matchers.matchesPattern;
30 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
31 import static org.junit.Assert.assertTrue;
32 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID;
33 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
34 import java.io.IOException;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.security.GeneralSecurityException;
38 import java.util.Collections;
39 import java.util.Optional;
40 import java.util.UUID;
41 import javax.net.ssl.SSLContext;
42 import org.apache.http.config.Registry;
43 import org.apache.http.config.RegistryBuilder;
44 import org.apache.http.conn.socket.ConnectionSocketFactory;
45 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
46 import org.apache.http.conn.ssl.SSLContextBuilder;
47 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
48 import org.apache.http.impl.client.CloseableHttpClient;
49 import org.apache.http.impl.client.HttpClientBuilder;
50 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
51 import org.junit.AfterClass;
52 import org.junit.BeforeClass;
53 import org.junit.Test;
54 import org.onap.vid.asdc.AsdcCatalogException;
55 import org.onap.vid.asdc.beans.Service;
56 import org.onap.vid.client.SyncRestClient;
57 import org.onap.vid.testUtils.StubServerUtil;
58 import com.fasterxml.jackson.core.JsonProcessingException;
59 import com.xebialabs.restito.semantics.Call;
62 public class SdcRestClientITTest {
63 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}";
64 private static final String[] SUPPORTED_SSL_VERSIONS = {"TLSv1", "TLSv1.2"};
65 private static StubServerUtil stubServer;
66 private static SdcRestClient sdcRestClient;
69 public static void setUpClass() throws GeneralSecurityException {
70 stubServer = new StubServerUtil();
71 stubServer.runSecuredServer();
72 SyncRestClient syncRestClient = new SyncRestClient(createNaiveHttpClient());
73 String serverUrl = stubServer.constructTargetUrl("https", "");
74 sdcRestClient = new SdcRestClient(serverUrl, "", syncRestClient);
78 public static void tearDown() {
79 stubServer.stopServer();
83 public void shouldDownloadToscaArtifactUsingSecuredEndpoint() throws AsdcCatalogException, IOException {
84 UUID uuid = UUID.randomUUID();
85 String expectedEndpoint = String.format("/sdc/v1/catalog/services/%s/toscaModel", uuid);
87 stubServer.prepareGetCall(
88 expectedEndpoint, stringContent("sampleFileContent"), "sampleFileContent", ok(), "application/octet-stream");
91 Path serviceToscaModel = sdcRestClient.getServiceToscaModel(uuid);
92 serviceToscaModel.toFile().deleteOnExit();
95 assertThat(Files.readAllLines(serviceToscaModel), contains("sampleFileContent"));
96 assertThatRequestHasRequiredHeaders(expectedEndpoint);
100 public void shouldGetServiceDetailsUsingSecuredEndpoint() throws AsdcCatalogException, JsonProcessingException {
101 UUID uuid = UUID.randomUUID();
102 String expectedEndpoint = String.format("/sdc/v1/catalog/services/%s/metadata", uuid);
103 Service expectedService = getExpectedService(uuid.toString());
106 stubServer.prepareGetCall(expectedEndpoint, expectedService, ok());
109 Service actualService = sdcRestClient.getService(uuid);
112 assertThat(actualService, is(expectedService));
113 assertThatRequestHasRequiredHeaders(expectedEndpoint);
116 private void assertThatRequestHasRequiredHeaders(String expectedEndpoint) {
117 Optional<Call> first = stubServer
120 .filter(x -> x.getUri().contains(expectedEndpoint))
123 assertTrue(first.isPresent());
125 assertThat(first.get().getHeaders().keySet(),
126 hasItems(X_ECOMP_INSTANCE_ID.toLowerCase(), REQUEST_ID_HEADER_KEY.toLowerCase()));
127 assertThat(first.get().getHeaders().get(REQUEST_ID_HEADER_KEY.toLowerCase()).get(0),
128 matchesPattern(UUID_REGEX));
131 private Service getExpectedService(String stringId) {
132 return new Service.ServiceBuilder().setUuid(stringId)
133 .setInvariantUUID(stringId)
134 .setCategory("sampleCategory")
135 .setVersion("sampleVersion")
136 .setName( "sampleName")
137 .setDistributionStatus("sampleDistStatus")
138 .setToscaModelURL("sampleToscaUrl")
139 .setLifecycleState(Service.LifecycleState.CERTIFIED)
140 .setArtifacts(Collections.emptyList())
141 .setResources(Collections.emptyList()).build();
145 private static CloseableHttpClient createNaiveHttpClient() throws GeneralSecurityException {
146 final SSLContext context = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
149 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context, SUPPORTED_SSL_VERSIONS,
150 null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
151 Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
152 .register("https", socketFactory)
155 return HttpClientBuilder.create()
156 .setDefaultRequestConfig(custom().setConnectionRequestTimeout(10000).build())
157 .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
158 .setSSLSocketFactory(socketFactory).build();