Merge "Some rework around DAO classes"
[clamp.git] / src / test / java / org / onap / clamp / clds / it / HttpsIT.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.AbstractIT;
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 DCAE API in org.onap.clamp.ClampDesigner.client package - replicate DCAE
54  * Delegates in test.
55  */
56 @RunWith(SpringRunner.class)
57 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
58 @TestPropertySource(locations = "classpath:https/https-test.properties")
59 public class HttpsIT extends AbstractIT {
60
61     @Value("${server.port}")
62     private String httpsPort;
63
64     @Value("${server.http-to-https-redirection.port}")
65     private String httpPort;
66
67     @BeforeClass
68     public static void setUp() {
69
70         try {
71             // setup ssl context to ignore certificate errors
72             SSLContext ctx = SSLContext.getInstance("TLS");
73             X509TrustManager tm = new X509TrustManager() {
74
75                 @Override
76                 public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType)
77                         throws java.security.cert.CertificateException {
78                 }
79
80                 @Override
81                 public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType)
82                         throws java.security.cert.CertificateException {
83                 }
84
85                 @Override
86                 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
87                     return null;
88                 }
89             };
90             ctx.init(null, new TrustManager[] { tm }, null);
91             SSLContext.setDefault(ctx);
92         } catch (Exception ex) {
93             ex.printStackTrace();
94         }
95
96     }
97
98     @Test
99     public void testDesignerIndex() throws Exception {
100         RestTemplate template = new RestTemplate();
101         final MySimpleClientHttpRequestFactory factory = new MySimpleClientHttpRequestFactory(new HostnameVerifier() {
102
103             @Override
104             public boolean verify(final String hostname, final SSLSession session) {
105                 return true;
106             }
107         });
108         template.setRequestFactory(factory);
109
110         ResponseEntity<String> entity = template
111                 .getForEntity("http://localhost:" + this.httpPort + "/designer/index.html", String.class);
112         assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FOUND);
113
114         ResponseEntity<String> httpsEntity = template
115                 .getForEntity("https://localhost:" + this.httpsPort + "/designer/index.html", String.class);
116         assertThat(httpsEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
117         assertThat(httpsEntity.getBody()).contains("CLDS");
118
119     }
120
121     /**
122      * Http Request Factory for ignoring SSL hostname errors. Not for production
123      * use!
124      */
125     class MySimpleClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
126
127         private final HostnameVerifier verifier;
128
129         public MySimpleClientHttpRequestFactory(final HostnameVerifier verifier) {
130             this.verifier = verifier;
131         }
132
133         @Override
134         protected void prepareConnection(final HttpURLConnection connection, final String httpMethod)
135                 throws IOException {
136             if (connection instanceof HttpsURLConnection) {
137                 ((HttpsURLConnection) connection).setHostnameVerifier(this.verifier);
138             }
139             super.prepareConnection(connection, httpMethod);
140         }
141     }
142
143 }