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.allOf;
28 import static org.hamcrest.Matchers.equalToIgnoringCase;
29 import static org.hamcrest.Matchers.is;
30 import static org.hamcrest.Matchers.matchesPattern;
31 import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
32 import static org.hamcrest.collection.IsMapContaining.hasEntry;
33 import static org.hamcrest.collection.IsMapContaining.hasKey;
34 import static org.hamcrest.core.IsEqual.equalTo;
35 import static org.junit.Assert.assertTrue;
36 import static org.mockito.Mockito.mock;
37 import static org.onap.vid.client.SyncRestClientInterface.HEADERS.X_ECOMP_INSTANCE_ID;
38 import static org.onap.vid.utils.Logging.REQUEST_ID_HEADER_KEY;
40 import com.fasterxml.jackson.core.JsonProcessingException;
41 import com.xebialabs.restito.semantics.Call;
42 import java.io.IOException;
43 import java.nio.file.Files;
44 import java.nio.file.Path;
45 import java.security.GeneralSecurityException;
46 import java.util.Collections;
47 import java.util.Optional;
48 import java.util.UUID;
49 import javax.net.ssl.SSLContext;
50 import org.apache.http.config.Registry;
51 import org.apache.http.config.RegistryBuilder;
52 import org.apache.http.conn.socket.ConnectionSocketFactory;
53 import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
54 import org.apache.http.conn.ssl.SSLContextBuilder;
55 import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
56 import org.apache.http.impl.client.CloseableHttpClient;
57 import org.apache.http.impl.client.HttpClientBuilder;
58 import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
59 import org.junit.AfterClass;
60 import org.junit.BeforeClass;
61 import org.junit.Test;
62 import org.onap.vid.asdc.AsdcCatalogException;
63 import org.onap.vid.asdc.beans.Service;
64 import org.onap.vid.client.SyncRestClient;
65 import org.onap.vid.testUtils.StubServerUtil;
66 import org.onap.vid.utils.Logging;
69 public class SdcRestClientITTest {
70 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}";
71 private static final String[] SUPPORTED_SSL_VERSIONS = {"TLSv1", "TLSv1.2"};
72 private static StubServerUtil stubServer;
73 private static SdcRestClient sdcRestClient;
76 public static void setUpClass() throws GeneralSecurityException {
77 stubServer = new StubServerUtil();
78 stubServer.runSecuredServer();
79 SyncRestClient syncRestClient = new SyncRestClient(createNaiveHttpClient(), mock(Logging.class));
80 String serverUrl = stubServer.constructTargetUrl("https", "");
81 sdcRestClient = new SdcRestClient(serverUrl, "", syncRestClient, mock(Logging.class));
85 public static void tearDown() {
86 stubServer.stopServer();
90 public void shouldDownloadToscaArtifactUsingSecuredEndpoint() throws AsdcCatalogException, IOException {
91 UUID uuid = UUID.randomUUID();
92 String expectedEndpoint = String.format("/sdc/v1/catalog/services/%s/toscaModel", uuid);
94 stubServer.prepareGetCall(
95 expectedEndpoint, stringContent("sampleFileContent"), ok(), "application/octet-stream");
98 Path serviceToscaModel = sdcRestClient.getServiceToscaModel(uuid);
99 serviceToscaModel.toFile().deleteOnExit();
102 assertThat(Files.readAllLines(serviceToscaModel), contains("sampleFileContent"));
103 assertThatRequestHasRequiredHeaders(expectedEndpoint);
107 public void shouldGetServiceDetailsUsingSecuredEndpoint() throws AsdcCatalogException, JsonProcessingException {
108 UUID uuid = UUID.randomUUID();
109 String expectedEndpoint = String.format("/sdc/v1/catalog/services/%s/metadata", uuid);
110 Service expectedService = getExpectedService(uuid.toString());
113 stubServer.prepareGetCall(expectedEndpoint, expectedService, ok());
116 Service actualService = sdcRestClient.getService(uuid);
119 assertThat(actualService, is(expectedService));
120 assertThatRequestHasRequiredHeaders(expectedEndpoint);
123 private void assertThatRequestHasRequiredHeaders(String expectedEndpoint) {
124 Optional<Call> first = stubServer
127 .filter(x -> x.getUri().contains(expectedEndpoint))
130 assertTrue(first.isPresent());
132 assertThat(first.get().getHeaders(),
134 hasEntry(equalToIgnoringCase(REQUEST_ID_HEADER_KEY), contains(matchesPattern(UUID_REGEX))),
135 hasKey(equalToIgnoringCase(X_ECOMP_INSTANCE_ID)),
136 hasEntry(equalToIgnoringCase("x-onap-partnerName"), contains(equalTo("VID.VID")))
140 private Service getExpectedService(String stringId) {
141 return new Service.ServiceBuilder().setUuid(stringId)
142 .setInvariantUUID(stringId)
143 .setCategory("sampleCategory")
144 .setVersion("sampleVersion")
145 .setName( "sampleName")
146 .setDistributionStatus("sampleDistStatus")
147 .setToscaModelURL("sampleToscaUrl")
148 .setLifecycleState(Service.LifecycleState.CERTIFIED)
149 .setArtifacts(Collections.emptyList())
150 .setResources(Collections.emptyList()).build();
154 private static CloseableHttpClient createNaiveHttpClient() throws GeneralSecurityException {
155 final SSLContext context = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
158 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context, SUPPORTED_SSL_VERSIONS,
159 null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
160 Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
161 .register("https", socketFactory)
164 return HttpClientBuilder.create()
165 .setDefaultRequestConfig(custom().setConnectionRequestTimeout(10000).build())
166 .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
167 .setSSLSocketFactory(socketFactory).build();