2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
25 package org.onap.clamp.clds.it;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
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;
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;
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;
57 * Test HTTP and HTTPS settings + redirection of HTTP to HTTPS.
59 @RunWith(SpringRunner.class)
60 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
61 @TestPropertySource(locations = "classpath:https/https-test.properties")
62 public class HttpConnectionManagerItCase {
64 @Value("${server.port}")
65 private String httpsPort;
66 @Value("${server.http-to-https-redirection.port}")
67 private String httpPort;
70 HttpConnectionManager httpConnectionManager;
72 private static TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
75 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
80 public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
84 public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
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() {
95 public boolean verify(String hostname, SSLSession session) {
99 // set the allTrusting verifier
100 HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
104 public void setupEnvBeforeTest() throws KeyManagementException, NoSuchAlgorithmException {
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());
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());
126 @Test(expected = BadRequestException.class)
127 public void testHttpsGet404() throws IOException {
128 httpConnectionManager.doHttpRequest("https://localhost:" + this.httpsPort + "/swaggerx.html", "GET", null, null,
130 fail("Should have raised an BadRequestException");
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");
140 @Test(expected = BadRequestException.class)
141 public void testHttpException() throws IOException {
142 httpConnectionManager.doHttpRequest("http://localhost:" + this.httpsPort + "/swagger.html", "GET", null, null,
144 fail("Should have raised an BadRequestException");