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