Fix the Checkstyle issues
[clamp.git] / src / test / java / org / onap / clamp / clds / it / HttpsItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  * 
12  * http://www.apache.org/licenses/LICENSE-2.0
13  * 
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.
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import java.io.IOException;
29 import java.net.HttpURLConnection;
30
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;
37
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.onap.clamp.clds.AbstractItCase;
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.context.TestPropertySource;
49 import org.springframework.test.context.junit4.SpringRunner;
50 import org.springframework.web.client.RestTemplate;
51
52 /**
53  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
54  */
55 @RunWith(SpringRunner.class)
56 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
57 @TestPropertySource(locations = "classpath:https/https-test.properties")
58 public class HttpsItCase extends AbstractItCase {
59
60     @Value("${server.port}")
61     private String httpsPort;
62
63     @Value("${server.http-to-https-redirection.port}")
64     private String httpPort;
65
66     /**
67      * Setup the variable before tests execution.
68      */
69     @BeforeClass
70     public static void setUp() {
71
72         try {
73             // setup ssl context to ignore certificate errors
74             SSLContext ctx = SSLContext.getInstance("TLS");
75             X509TrustManager tm = new X509TrustManager() {
76
77                 @Override
78                 public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
79                         throws java.security.cert.CertificateException {
80                 }
81
82                 @Override
83                 public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
84                         throws java.security.cert.CertificateException {
85                 }
86
87                 @Override
88                 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
89                     return null;
90                 }
91             };
92             ctx.init(null, new TrustManager[] { tm }, null);
93             SSLContext.setDefault(ctx);
94         } catch (Exception ex) {
95             ex.printStackTrace();
96         }
97
98     }
99
100     @Test
101     public void testDesignerIndex() throws Exception {
102         RestTemplate template = new RestTemplate();
103         final MySimpleClientHttpRequestFactory factory = new MySimpleClientHttpRequestFactory(new HostnameVerifier() {
104
105             @Override
106             public boolean verify(final String hostname, final SSLSession session) {
107                 return true;
108             }
109         });
110         template.setRequestFactory(factory);
111
112         ResponseEntity<String> entity = template
113                 .getForEntity("http://localhost:" + this.httpPort + "/designer/index.html", String.class);
114         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
115
116         ResponseEntity<String> httpsEntity = template
117                 .getForEntity("https://localhost:" + this.httpsPort + "/designer/index.html", String.class);
118         assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
119         assertThat(httpsEntity.getBody()).contains("CLDS");
120
121     }
122
123     /**
124      * Http Request Factory for ignoring SSL hostname errors. Not for production
125      * use!
126      */
127     class MySimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
128
129         private final HostnameVerifier verifier;
130
131         public MySimpleClientHttpRequestFactory(final HostnameVerifier verifier) {
132             this.verifier = verifier;
133         }
134
135         @Override
136         protected void prepareConnection(final HttpURLConnection connection, final String httpMethod)
137                 throws IOException {
138             if (connection instanceof HttpsURLConnection) {
139                 ((HttpsURLConnection) connection).setHostnameVerifier(this.verifier);
140             }
141             super.prepareConnection(connection, httpMethod);
142         }
143     }
144
145 }