2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.it;
26 import static org.assertj.core.api.Assertions.assertThat;
28 import java.io.IOException;
29 import java.net.HttpURLConnection;
31 import javax.net.ssl.HostnameVerifier;
32 import javax.net.ssl.HttpsURLConnection;
33 import javax.net.ssl.SSLContext;
34 import javax.net.ssl.SSLSession;
35 import javax.net.ssl.TrustManager;
36 import javax.net.ssl.X509TrustManager;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.springframework.beans.factory.annotation.Value;
42 import org.springframework.boot.test.context.SpringBootTest;
43 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
44 import org.springframework.http.HttpStatus;
45 import org.springframework.http.ResponseEntity;
46 import org.springframework.http.client.SimpleClientHttpRequestFactory;
47 import org.springframework.test.context.TestPropertySource;
48 import org.springframework.test.context.junit4.SpringRunner;
49 import org.springframework.web.client.RestTemplate;
52 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
54 @RunWith(SpringRunner.class)
55 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
56 @TestPropertySource(locations = "classpath:https/https-test.properties")
57 public class HttpsItCase {
59 @Value("${server.port}")
60 private String httpsPort;
61 @Value("${server.http-to-https-redirection.port}")
62 private String httpPort;
65 * Setup the variable before tests execution.
68 public static void setUp() {
70 // setup ssl context to ignore certificate errors
71 SSLContext ctx = SSLContext.getInstance("TLS");
72 X509TrustManager tm = new X509TrustManager() {
75 public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
76 throws java.security.cert.CertificateException {
80 public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
81 throws java.security.cert.CertificateException {
85 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
89 ctx.init(null, new TrustManager[] {
92 SSLContext.setDefault(ctx);
93 } catch (Exception ex) {
99 public void testDesignerIndex() throws Exception {
100 RestTemplate template = new RestTemplate();
101 final MySimpleClientHttpRequestFactory factory = new MySimpleClientHttpRequestFactory(new HostnameVerifier() {
104 public boolean verify(final String hostname, final SSLSession session) {
108 template.setRequestFactory(factory);
109 ResponseEntity<String> entity = template
110 .getForEntity("http://localhost:" + this.httpPort + "/designer/index.html", String.class);
111 assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
112 ResponseEntity<String> httpsEntity = template
113 .getForEntity("https://localhost:" + this.httpsPort + "/designer/index.html", String.class);
114 assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
115 assertThat(httpsEntity.getBody()).contains("CLDS");
119 * Http Request Factory for ignoring SSL hostname errors. Not for production
122 class MySimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
124 private final HostnameVerifier verifier;
126 public MySimpleClientHttpRequestFactory(final HostnameVerifier verifier) {
127 this.verifier = verifier;
131 protected void prepareConnection(final HttpURLConnection connection, final String httpMethod)
133 if (connection instanceof HttpsURLConnection) {
134 ((HttpsURLConnection) connection).setHostnameVerifier(this.verifier);
136 super.prepareConnection(connection, httpMethod);