Fix Sonar vulnerabilities
[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[] { new X509TrustManager() {
73
74         @Override
75         public java.security.cert.X509Certificate[] getAcceptedIssuers() {
76             return null;
77         }
78
79         @Override
80         public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
81         }
82
83         @Override
84         public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
85         }
86     } };
87
88     private void enableSslNoCheck() throws NoSuchAlgorithmException, KeyManagementException {
89         SSLContext sc = SSLContext.getInstance("SSL");
90         sc.init(null, trustAllCerts, new java.security.SecureRandom());
91         HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
92         HostnameVerifier allHostsValid = new HostnameVerifier() {
93
94             @Override
95             public boolean verify(String hostname, SSLSession session) {
96                 return true;
97             }
98         };
99         // set the allTrusting verifier
100         HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
101     }
102
103     @Before
104     public void setupEnvBeforeTest() throws KeyManagementException, NoSuchAlgorithmException {
105         enableSslNoCheck();
106     }
107
108     @Test
109     public void testHttpGet() throws Exception {
110         String response = httpConnectionManager.doHttpRequest("http://localhost:" + this.httpPort + "/swagger.html",
111                 "GET", null, null, "DCAE", null, null);
112         assertNotNull(response);
113         // Should be a redirection so 302, so empty
114         assertTrue(response.isEmpty());
115     }
116
117     @Test
118     public void testHttpsGet() throws Exception {
119         String response = httpConnectionManager.doHttpRequest("https://localhost:" + this.httpsPort + "/swagger.html",
120                 "GET", null, null, "DCAE", null, null);
121         assertNotNull(response);
122         // Should contain something
123         assertTrue(!response.isEmpty());
124     }
125
126     @Test(expected = BadRequestException.class)
127     public void testHttpsGet404() throws IOException {
128         httpConnectionManager.doHttpRequest("https://localhost:" + this.httpsPort + "/swaggerx.html", "GET", null, null,
129                 "DCAE", null, null);
130         fail("Should have raised an BadRequestException");
131     }
132
133     @Test(expected = BadRequestException.class)
134     public void testHttpsPost404() throws IOException {
135         httpConnectionManager.doHttpRequest("https://localhost:" + this.httpsPort + "/swaggerx.html", "POST", "",
136                 "application/json", "DCAE", null, null);
137         fail("Should have raised an BadRequestException");
138     }
139
140     @Test(expected = BadRequestException.class)
141     public void testHttpException() throws IOException {
142         httpConnectionManager.doHttpRequest("http://localhost:" + this.httpsPort + "/swagger.html", "GET", null, null,
143                 "DCAE", null, null);
144         fail("Should have raised an BadRequestException");
145     }
146 }