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