diff options
author | Ofir Sonsino <os0695@intl.att.com> | 2018-08-23 11:31:48 +0000 |
---|---|---|
committer | Gerrit Code Review <gerrit@onap.org> | 2018-08-23 11:31:48 +0000 |
commit | 5adc0b0ee92d1e39ddaacd27e67a327dd5988f11 (patch) | |
tree | bbb51070fd9996ca9e3e5ef85c029aa2da693782 /vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java | |
parent | eea1c99fcc1ec5a0587f60fe7ac5ba96e186ca81 (diff) | |
parent | 985bc34c7519ae45adf464d68315f06972152d0e (diff) |
Merge "Generic REST client for external services"
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java')
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java b/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java new file mode 100644 index 00000000..ba6da435 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/client/SyncRestClientForHttpsServerTest.java @@ -0,0 +1,139 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 Nokia. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.vid.client; + +import io.joshworks.restclient.http.HttpResponse; +import io.joshworks.restclient.http.JsonNode; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import com.fasterxml.jackson.core.JsonProcessingException; +import org.apache.http.conn.ssl.TrustSelfSignedStrategy; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.conn.ssl.SSLContextBuilder; +import com.xebialabs.restito.semantics.Condition; +import com.xebialabs.restito.server.StubServer; +import com.xebialabs.restito.semantics.Action; +import org.apache.http.config.RegistryBuilder; +import org.testng.annotations.BeforeMethod; +import org.testng.annotations.AfterMethod; +import org.glassfish.grizzly.http.Method; +import org.apache.http.client.HttpClient; +import org.apache.http.config.Registry; +import org.testng.annotations.Test; + +import java.security.GeneralSecurityException; +import javax.net.ssl.SSLContext; +import java.util.Collections; + +import static com.xebialabs.restito.builder.verify.VerifyHttp.verifyHttp; +import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp; +import static com.xebialabs.restito.semantics.Action.stringContent; +import static com.xebialabs.restito.semantics.Action.contentType; +import static org.apache.http.client.config.RequestConfig.custom; +import static com.xebialabs.restito.semantics.Action.ok; +import static org.testng.Assert.assertEquals; + +public class SyncRestClientForHttpsServerTest { + + private static final SyncRestClientModel.TestModel testObject = new SyncRestClientModel.TestModel(1, "test"); + private static final String[] SUPPORTED_PROTOCOLS = {"TLSv1", "TLSv1.2"}; + private StubServer stubServer; + private ObjectMapper objectMapper = new ObjectMapper(); + + private SyncRestClient syncRestClient; + + @BeforeMethod + public void setUp() throws GeneralSecurityException { + stubServer = new StubServer(); + stubServer.secured().run(); + syncRestClient = new SyncRestClient(createNaiveHttpClient()); + } + + @AfterMethod + public void tearDown() { + stubServer.stop(); + } + + @Test + public void testJsonResponseFromGet() throws JsonProcessingException { + // given + stubGetCall(); + String securedUrl = "https://0.0.0.0:" + stubServer.getPort() + "/test"; + String notSecuredUrl = "http://0.0.0.0:" + stubServer.getPort() + "/test"; + // when + HttpResponse<JsonNode> jsonNodeHttpResponse = syncRestClient + .get(securedUrl, Collections.emptyMap(), Collections.emptyMap()); + // then + verifyHttp(stubServer) + .once(Condition.method(Method.GET), Condition.url(securedUrl), Condition.not(Condition.url(notSecuredUrl))); + assertEquals(jsonNodeHttpResponse.getStatus(), 200); + assertEquals(jsonNodeHttpResponse.getBody().getObject().get("key"), 1); + assertEquals(jsonNodeHttpResponse.getBody().getObject().get("value"), "test"); + } + + @Test + public void testObjectResponseFromGet() throws JsonProcessingException { + // given + stubServer.run(); + stubGetCall(); + String securedUrl = "https://0.0.0.0:" + stubServer.getPort() + "/test"; + String notSecuredUrl = "http://0.0.0.0:" + stubServer.getPort() + "/test"; + // when + HttpResponse<SyncRestClientModel.TestModel> testModelHttpResponse = syncRestClient + .get(securedUrl, Collections.emptyMap(), Collections.emptyMap(), SyncRestClientModel.TestModel.class); + // then + verifyHttp(stubServer) + .once(Condition.method(Method.GET), Condition.url(securedUrl), Condition.not(Condition.url(notSecuredUrl))); + assertEquals(testModelHttpResponse.getStatus(), 200); + assertEquals(testModelHttpResponse.getBody().getKey(), 1); + assertEquals(testModelHttpResponse.getBody().getValue(), "test"); + } + + private Action jsonContent() throws JsonProcessingException { + return stringContent(objectMapper.writeValueAsString(testObject)); + } + + private void stubGetCall() throws JsonProcessingException { + whenHttp(stubServer) + .match(Condition.get("/test")) + .then(ok(), jsonContent(), contentType("application/json")); + } + + private CloseableHttpClient createNaiveHttpClient() throws GeneralSecurityException { + final SSLContext context = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()) + .build(); + + final SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context, SUPPORTED_PROTOCOLS, + null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); + Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create() + .register("https", socketFactory) + .build(); + + return HttpClientBuilder.create() + .setDefaultRequestConfig(custom().setConnectionRequestTimeout(10000).build()) + .setConnectionManager(new PoolingHttpClientConnectionManager(registry)) + .setSSLSocketFactory(socketFactory).build(); + } + +}
\ No newline at end of file |