69277baf9c56d2439c93f3b6bef23d3fc13e934f
[clamp.git] / src / test / java / org / onap / clamp / clds / it / HttpConnectionManagerItCase.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  * Modifications copyright (c) 2018 Nokia
21  * ===================================================================
22  * 
23  */
24
25 package org.onap.clamp.clds.it;
26
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30
31 import java.io.IOException;
32 import java.security.KeyManagementException;
33 import java.security.NoSuchAlgorithmException;
34 import java.security.cert.CertificateException;
35 import java.security.cert.X509Certificate;
36
37 import javax.net.ssl.HostnameVerifier;
38 import javax.net.ssl.HttpsURLConnection;
39 import javax.net.ssl.SSLContext;
40 import javax.net.ssl.SSLSession;
41 import javax.net.ssl.TrustManager;
42 import javax.net.ssl.X509TrustManager;
43 import javax.ws.rs.BadRequestException;
44
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.onap.clamp.util.HttpConnectionManager;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.beans.factory.annotation.Value;
51 import org.springframework.boot.test.context.SpringBootTest;
52 import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
53 import org.springframework.test.context.TestPropertySource;
54 import org.springframework.test.context.junit4.SpringRunner;
55
56 /**
57  * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
58  */
59 @RunWith(SpringRunner.class)
60 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
61 @TestPropertySource(locations = "classpath:https/https-test.properties")
62 public class HttpConnectionManagerItCase {
63
64     @Value("${server.port}")
65     private String httpsPort;
66     @Value("${server.http-to-https-redirection.port}")
67     private String httpPort;
68
69     @Autowired
70     HttpConnectionManager httpConnectionManager;
71
72     private static TrustManager[] trustAllCerts = new TrustManager[]{
73         new X509TrustManager() {
74
75             @Override
76             public java.security.cert.X509Certificate[] getAcceptedIssuers() {
77                 return null;
78             }
79
80             @Override
81             public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
82             }
83
84             @Override
85             public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
86             }
87         }
88     };
89
90     private void enableSslNoCheck() throws NoSuchAlgorithmException, KeyManagementException {
91         SSLContext sc = SSLContext.getInstance("SSL");
92         sc.init(null, trustAllCerts, new java.security.SecureRandom());
93         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
94         HostnameVerifier allHostsValid = new HostnameVerifier() {
95
96             @Override
97             public boolean verify(String hostname, SSLSession session) {
98                 return true;
99             }
100         };
101         // set the allTrusting verifier
102         HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
103     }
104
105     @Before
106     public void setupEnvBeforeTest() throws KeyManagementException, NoSuchAlgorithmException {
107         enableSslNoCheck();
108     }
109
110     @Test
111     public void testHttpGet() throws Exception {
112         String response = httpConnectionManager
113                 .doHttpRequest("http://localhost:" + this.httpPort + "/designer/index.html", "GET", null, null, "DCAE", null, null);
114         assertNotNull(response);
115         // Should be a redirection so 302, so empty
116         assertTrue(response.isEmpty());
117     }
118
119     @Test
120     public void testHttpsGet() throws Exception {
121         String response = httpConnectionManager
122                 .doHttpRequest("https://localhost:" + this.httpsPort + "/designer/index.html", "GET", null, null, "DCAE", null, null);
123         assertNotNull(response);
124         // Should contain something
125         assertTrue(!response.isEmpty());
126     }
127
128     @Test(expected = BadRequestException.class)
129     public void testHttpsGet404() throws IOException {
130         httpConnectionManager.doHttpRequest("https://localhost:" + this.httpsPort + "/designer/index1.html", "GET",
131                 null, null, "DCAE", null, null);
132         fail("Should have raised an BadRequestException");
133     }
134
135     @Test(expected = BadRequestException.class)
136     public void testHttpsPost404() throws IOException {
137         httpConnectionManager.doHttpRequest("https://localhost:" + this.httpsPort + "/designer/index1.html", "POST",
138                 "", "application/json", "DCAE", null, null);
139         fail("Should have raised an BadRequestException");
140     }
141
142     @Test(expected = BadRequestException.class)
143     public void testHttpException() throws IOException {
144         httpConnectionManager.doHttpRequest("http://localhost:" + this.httpsPort + "/designer/index.html", "GET",
145                 null, null, "DCAE", null, null);
146         fail("Should have raised an BadRequestException");
147     }
148 }