Merge "Add missing INFO.yaml blocks"
[clamp.git] / src / test / java / org / onap / clamp / clds / it / HttpsItCase.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 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  *
22  */
23
24 package org.onap.clamp.clds.it;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import java.io.File;
29 import java.io.IOException;
30 import java.net.HttpURLConnection;
31 import java.nio.charset.Charset;
32
33 import javax.net.ssl.HostnameVerifier;
34 import javax.net.ssl.HttpsURLConnection;
35 import javax.net.ssl.SSLContext;
36 import javax.net.ssl.SSLSession;
37 import javax.net.ssl.TrustManager;
38 import javax.net.ssl.X509TrustManager;
39
40 import org.apache.commons.io.FileUtils;
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.springframework.beans.factory.annotation.Value;
45 import org.springframework.boot.test.context.SpringBootTest;
46 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
47 import org.springframework.http.HttpStatus;
48 import org.springframework.http.ResponseEntity;
49 import org.springframework.http.client.SimpleClientHttpRequestFactory;
50 import org.springframework.test.context.TestPropertySource;
51 import org.springframework.test.context.junit4.SpringRunner;
52 import org.springframework.web.client.RestTemplate;
53
54 /**
55  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
56  */
57 @RunWith(SpringRunner.class)
58 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
59 @TestPropertySource(locations = "classpath:https/https-test.properties")
60 public class HttpsItCase {
61
62     @Value("${server.port}")
63     private String httpsPort;
64     @Value("${server.http-to-https-redirection.port}")
65     private String httpPort;
66
67     /**
68      * Setup the variable before tests execution.
69      */
70     @BeforeClass
71     public static void setUp() {
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[] {
93                 tm
94             }, null);
95             SSLContext.setDefault(ctx);
96         } catch (Exception ex) {
97             ex.printStackTrace();
98         }
99     }
100
101     @Test
102     public void testDesignerIndex() throws Exception {
103         RestTemplate template = new RestTemplate();
104         final MySimpleClientHttpRequestFactory factory = new MySimpleClientHttpRequestFactory(new HostnameVerifier() {
105
106             @Override
107             public boolean verify(final String hostname, final SSLSession session) {
108                 return true;
109             }
110         });
111         template.setRequestFactory(factory);
112         ResponseEntity<String> entity = template
113             .getForEntity("http://localhost:" + this.httpPort + "/designer/index.html", String.class);
114         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
115         ResponseEntity<String> httpsEntity = template
116             .getForEntity("https://localhost:" + this.httpsPort + "/designer/index.html", String.class);
117         assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
118         assertThat(httpsEntity.getBody()).contains("CLDS");
119     }
120
121     @Test
122     public void testSwaggerJson() throws Exception {
123         RestTemplate template = new RestTemplate();
124         final MySimpleClientHttpRequestFactory factory = new MySimpleClientHttpRequestFactory(new HostnameVerifier() {
125
126             @Override
127             public boolean verify(final String hostname, final SSLSession session) {
128                 return true;
129             }
130         });
131         template.setRequestFactory(factory);
132         ResponseEntity<String> httpsEntity = template
133             .getForEntity("https://localhost:" + this.httpsPort + "/restservices/clds/api-doc", String.class);
134         assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
135         assertThat(httpsEntity.getBody()).contains("swagger");
136         FileUtils.writeStringToFile(
137                 new File("docs/swagger/swagger.json"), httpsEntity.getBody(), Charset.defaultCharset());
138     }
139
140     /**
141      * Http Request Factory for ignoring SSL hostname errors. Not for production
142      * use!
143      */
144     class MySimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
145
146         private final HostnameVerifier verifier;
147
148         public MySimpleClientHttpRequestFactory(final HostnameVerifier verifier) {
149             this.verifier = verifier;
150         }
151
152         @Override
153         protected void prepareConnection(final HttpURLConnection connection, final String httpMethod)
154             throws IOException {
155             if (connection instanceof HttpsURLConnection) {
156                 ((HttpsURLConnection) connection).setHostnameVerifier(this.verifier);
157             }
158             super.prepareConnection(connection, httpMethod);
159         }
160     }
161 }