2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
24 package org.onap.clamp.clds.it;
26 import static org.assertj.core.api.Assertions.assertThat;
29 import java.io.IOException;
30 import java.net.HttpURLConnection;
31 import java.nio.charset.Charset;
32 import javax.net.ssl.HostnameVerifier;
33 import javax.net.ssl.HttpsURLConnection;
34 import javax.net.ssl.SSLContext;
35 import javax.net.ssl.SSLSession;
36 import javax.net.ssl.TrustManager;
37 import javax.net.ssl.X509TrustManager;
38 import org.apache.commons.io.FileUtils;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.springframework.beans.factory.annotation.Value;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
45 import org.springframework.http.HttpStatus;
46 import org.springframework.http.ResponseEntity;
47 import org.springframework.http.client.SimpleClientHttpRequestFactory;
48 import org.springframework.test.annotation.DirtiesContext;
49 import org.springframework.test.context.TestPropertySource;
50 import org.springframework.test.context.junit4.SpringRunner;
51 import org.springframework.web.client.RestTemplate;
54 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
56 @RunWith(SpringRunner.class)
57 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
58 @TestPropertySource(locations = "classpath:https/https-test.properties")
60 public class HttpsItCase {
62 @Value("${server.port}")
63 private String httpsPort;
64 @Value("${server.http-to-https-redirection.port}")
65 private String httpPort;
68 * Setup the variable before tests execution.
71 public static void setUp() {
73 // setup ssl context to ignore certificate errors
74 SSLContext ctx = SSLContext.getInstance("TLS");
75 X509TrustManager tm = new X509TrustManager() {
78 public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
79 throws java.security.cert.CertificateException {
83 public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
84 throws java.security.cert.CertificateException {
88 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
92 ctx.init(null, new TrustManager[] { tm }, null);
93 SSLContext.setDefault(ctx);
94 } catch (Exception ex) {
100 public void testDesignerIndex() throws Exception {
101 RestTemplate template = new RestTemplate();
102 final MySimpleClientHttpRequestFactory factory = new MySimpleClientHttpRequestFactory(new HostnameVerifier() {
105 public boolean verify(final String hostname, final SSLSession session) {
109 template.setRequestFactory(factory);
110 ResponseEntity<String> entity = template.getForEntity("http://localhost:" + this.httpPort + "/swagger.html",
112 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
113 ResponseEntity<String> httpsEntity = template
114 .getForEntity("https://localhost:" + this.httpsPort + "/swagger.html", String.class);
115 assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
116 assertThat(httpsEntity.getBody()).contains("Clamp Rest API");
120 public void testSwaggerJson() throws Exception {
121 RestTemplate template = new RestTemplate();
122 final MySimpleClientHttpRequestFactory factory = new MySimpleClientHttpRequestFactory(new HostnameVerifier() {
125 public boolean verify(final String hostname, final SSLSession session) {
129 template.setRequestFactory(factory);
130 ResponseEntity<String> httpsEntity = template
131 .getForEntity("https://localhost:" + this.httpsPort + "/restservices/clds/api-doc", String.class);
132 assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
133 assertThat(httpsEntity.getBody()).contains("swagger");
134 FileUtils.writeStringToFile(new File("docs/swagger/swagger.json"), httpsEntity.getBody(),
135 Charset.defaultCharset());
139 * Http Request Factory for ignoring SSL hostname errors. Not for production
142 class MySimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
144 private final HostnameVerifier verifier;
146 public MySimpleClientHttpRequestFactory(final HostnameVerifier verifier) {
147 this.verifier = verifier;
151 protected void prepareConnection(final HttpURLConnection connection, final String httpMethod)
153 if (connection instanceof HttpsURLConnection) {
154 ((HttpsURLConnection) connection).setHostnameVerifier(this.verifier);
156 super.prepareConnection(connection, httpMethod);