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.mockito.Mockito.mock;
33 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID;
34 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.xebialabs.restito.semantics.Call;
38 import java.io.IOException;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.security.GeneralSecurityException;
42 import java.util.Collections;
43 import java.util.Optional;
44 import java.util.UUID;
45 import javax.net.ssl.SSLContext;
46 import org.apache.http.config.Registry;
47 import org.apache.http.config.RegistryBuilder;
48 import org.apache.http.conn.socket.ConnectionSocketFactory;
49 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
50 import org.apache.http.conn.ssl.SSLContextBuilder;
51 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
52 import org.apache.http.impl.client.CloseableHttpClient;
53 import org.apache.http.impl.client.HttpClientBuilder;
54 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
55 import org.junit.AfterClass;
56 import org.junit.BeforeClass;
57 import org.junit.Test;
58 import org.onap.vid.asdc.AsdcCatalogException;
59 import org.onap.vid.asdc.beans.Service;
60 import org.onap.vid.client.SyncRestClient;
61 import org.onap.vid.testUtils.StubServerUtil;
62 import org.onap.vid.utils.Logging;
65 public class SdcRestClientITTest {
66 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}";
67 private static final String[] SUPPORTED_SSL_VERSIONS = {"TLSv1", "TLSv1.2"};
68 private static StubServerUtil stubServer;
69 private static SdcRestClient sdcRestClient;
72 public static void setUpClass() throws GeneralSecurityException {
73 stubServer = new StubServerUtil();
74 stubServer.runSecuredServer();
75 SyncRestClient syncRestClient = new SyncRestClient(createNaiveHttpClient(), mock(Logging.class));
76 String serverUrl = stubServer.constructTargetUrl("https", "");
77 sdcRestClient = new SdcRestClient(serverUrl, "", syncRestClient, mock(Logging.class));
81 public static void tearDown() {
82 stubServer.stopServer();
86 public void shouldDownloadToscaArtifactUsingSecuredEndpoint() throws AsdcCatalogException, IOException {
87 UUID uuid = UUID.randomUUID();
88 String expectedEndpoint = String.format("/sdc/v1/catalog/services/%s/toscaModel", uuid);
90 stubServer.prepareGetCall(
91 expectedEndpoint, stringContent("sampleFileContent"), ok(), "application/octet-stream");
94 Path serviceToscaModel = sdcRestClient.getServiceToscaModel(uuid);
95 serviceToscaModel.toFile().deleteOnExit();
98 assertThat(Files.readAllLines(serviceToscaModel), contains("sampleFileContent"));
99 assertThatRequestHasRequiredHeaders(expectedEndpoint);
103 public void shouldGetServiceDetailsUsingSecuredEndpoint() throws AsdcCatalogException, JsonProcessingException {
104 UUID uuid = UUID.randomUUID();
105 String expectedEndpoint = String.format("/sdc/v1/catalog/services/%s/metadata", uuid);
106 Service expectedService = getExpectedService(uuid.toString());
109 stubServer.prepareGetCall(expectedEndpoint, expectedService, ok());
112 Service actualService = sdcRestClient.getService(uuid);
115 assertThat(actualService, is(expectedService));
116 assertThatRequestHasRequiredHeaders(expectedEndpoint);
119 private void assertThatRequestHasRequiredHeaders(String expectedEndpoint) {
120 Optional<Call> first = stubServer
123 .filter(x -> x.getUri().contains(expectedEndpoint))
126 assertTrue(first.isPresent());
128 assertThat(first.get().getHeaders().keySet(),
129 hasItems(X_ECOMP_INSTANCE_ID.toLowerCase(), REQUEST_ID_HEADER_KEY.toLowerCase()));
130 assertThat(first.get().getHeaders().get(REQUEST_ID_HEADER_KEY.toLowerCase()).get(0),
131 matchesPattern(UUID_REGEX));
134 private Service getExpectedService(String stringId) {
135 return new Service.ServiceBuilder().setUuid(stringId)
136 .setInvariantUUID(stringId)
137 .setCategory("sampleCategory")
138 .setVersion("sampleVersion")
139 .setName( "sampleName")
140 .setDistributionStatus("sampleDistStatus")
141 .setToscaModelURL("sampleToscaUrl")
142 .setLifecycleState(Service.LifecycleState.CERTIFIED)
143 .setArtifacts(Collections.emptyList())
144 .setResources(Collections.emptyList()).build();
148 private static CloseableHttpClient createNaiveHttpClient() throws GeneralSecurityException {
149 final SSLContext context = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
152 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context, SUPPORTED_SSL_VERSIONS,
153 null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
154 Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
155 .register("https", socketFactory)
158 return HttpClientBuilder.create()
159 .setDefaultRequestConfig(custom().setConnectionRequestTimeout(10000).build())
160 .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
161 .setSSLSocketFactory(socketFactory).build();