2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 - 2019 Nokia. 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.client;
23 import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp;
24 import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp;
25 import static com.xebialabs.restito.semantics.Action.contentType;
26 import static com.xebialabs.restito.semantics.Action.ok;
27 import static com.xebialabs.restito.semantics.Action.stringContent;
28 import static org.apache.http.client.config.RequestConfig.custom;
29 import static org.mockito.Mockito.mock;
30 import static org.testng.Assert.assertEquals;
32 import com.fasterxml.jackson.core.JsonProcessingException;
33 import com.fasterxml.jackson.databind.ObjectMapper;
34 import com.xebialabs.restito.semantics.Action;
35 import com.xebialabs.restito.semantics.Condition;
36 import com.xebialabs.restito.server.StubServer;
37 import io.joshworks.restclient.http.HttpResponse;
38 import io.joshworks.restclient.http.JsonNode;
39 import java.security.GeneralSecurityException;
40 import java.util.Collections;
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.glassfish.grizzly.http.Method;
52 import org.onap.vid.utils.Logging;
53 import org.testng.annotations.AfterMethod;
54 import org.testng.annotations.BeforeMethod;
55 import org.testng.annotations.Test;
57 public class SyncRestClientForHttpsServerTest {
59 private static final SyncRestClientModel.TestModel testObject = new SyncRestClientModel.TestModel(1, "test");
60 private static final String[] SUPPORTED_PROTOCOLS = {"TLSv1", "TLSv1.2"};
61 private StubServer stubServer;
62 private ObjectMapper objectMapper = new ObjectMapper();
64 private SyncRestClient syncRestClient;
65 private Logging mockLoggingService;
68 public void setUp() throws GeneralSecurityException {
69 stubServer = new StubServer();
70 stubServer.secured().run();
71 mockLoggingService = mock(Logging.class);
72 syncRestClient = new SyncRestClient(createNaiveHttpClient(), mockLoggingService);
76 public void tearDown() {
81 public void testJsonResponseFromGet() throws JsonProcessingException {
84 String securedUrl = "https://0.0.0.0:" + stubServer.getPort() + "/test";
85 String notSecuredUrl = "http://0.0.0.0:" + stubServer.getPort() + "/test";
87 HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient
88 .get(securedUrl, Collections.emptyMap(), Collections.emptyMap());
90 verifyHttp(stubServer)
91 .once(Condition.method(Method.GET), Condition.url(securedUrl), Condition.not(Condition.url(notSecuredUrl)));
92 assertEquals(jsonNodeHttpResponse.getStatus(), 200);
93 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1);
94 assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test");
98 public void testObjectResponseFromGet() throws JsonProcessingException {
102 String securedUrl = "https://0.0.0.0:" + stubServer.getPort() + "/test";
103 String notSecuredUrl = "http://0.0.0.0:" + stubServer.getPort() + "/test";
105 HttpResponse<SyncRestClientModel.TestModel> testModelHttpResponse = syncRestClient
106 .get(securedUrl, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class);
108 verifyHttp(stubServer)
109 .once(Condition.method(Method.GET), Condition.url(securedUrl), Condition.not(Condition.url(notSecuredUrl)));
110 assertEquals(testModelHttpResponse.getStatus(), 200);
111 assertEquals(testModelHttpResponse.getBody().getKey(), 1);
112 assertEquals(testModelHttpResponse.getBody().getValue(), "test");
115 private Action jsonContent() throws JsonProcessingException {
116 return stringContent(objectMapper.writeValueAsString(testObject));
119 private void stubGetCall() throws JsonProcessingException {
121 .match(Condition.get("/test"))
122 .then(ok(), jsonContent(), contentType("application/json"));
125 private CloseableHttpClient createNaiveHttpClient() throws GeneralSecurityException {
126 final SSLContext context = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy())
129 final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context, SUPPORTED_PROTOCOLS,
130 null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
131 Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
132 .register("https", socketFactory)
135 return HttpClientBuilder.create()
136 .setDefaultRequestConfig(custom().setConnectionRequestTimeout(10000).build())
137 .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
138 .setSSLSocketFactory(socketFactory).build();