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[]{
73 new X509TrustManager() {
76 public java.security.cert.X509Certificate[] getAcceptedIssuers() {
81 public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
85 public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
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() {
97 public boolean verify(String hostname, SSLSession session) {
101 // set the allTrusting verifier
102 HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
106 public void setupEnvBeforeTest() throws KeyManagementException, NoSuchAlgorithmException {
111 public void testHttpGet() throws Exception {
112 String response = httpConnectionManager
113 .doGeneralHttpQuery("http://localhost:" + this.httpPort + "/designer/index.html", "GET", null, null, "DCAE");
114 assertNotNull(response);
115 // Should be a redirection so 302, so empty
116 assertTrue(response.isEmpty());
120 public void testHttpsGet() throws Exception {
121 String response = httpConnectionManager
122 .doGeneralHttpQuery("https://localhost:" + this.httpsPort + "/designer/index.html", "GET", null, null, "DCAE");
123 assertNotNull(response);
124 // Should contain something
125 assertTrue(!response.isEmpty());
128 @Test(expected = BadRequestException.class)
129 public void testHttpsGet404() throws IOException {
130 httpConnectionManager.doGeneralHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html",
131 "GET", null, null, "DCAE");
132 fail("Should have raised an BadRequestException");
135 @Test(expected = BadRequestException.class)
136 public void testHttpsPost404() throws IOException {
137 httpConnectionManager.doGeneralHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html",
138 "POST", "", "application/json", "DCAE");
139 fail("Should have raised an BadRequestException");
142 @Test(expected = BadRequestException.class)
143 public void testHttpException() throws IOException {
144 httpConnectionManager.doGeneralHttpQuery("http://localhost:" + this.httpsPort + "/designer/index.html", "GET",
146 fail("Should have raised an BadRequestException");