Remove ECOMP in headers
[clamp.git] / src / test / java / org / onap / clamp / clds / it / DcaeHttpConnectionManagerItCase.java
index 7714270..264853c 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP CLAMP
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights
+ * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
  *                             reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License"); 
@@ -18,7 +18,7 @@
  * limitations under the License.
  * ============LICENSE_END============================================
  * ===================================================================
- * ECOMP is a trademark and service mark of AT&T Intellectual Property.
+ * 
  */
 
 package org.onap.clamp.clds.it;
@@ -28,12 +28,22 @@ import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
 import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.CertificateException;
+import java.security.cert.X509Certificate;
 
+import javax.net.ssl.HostnameVerifier;
+import javax.net.ssl.HttpsURLConnection;
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.SSLSession;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
 import javax.ws.rs.BadRequestException;
 
+import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
-import org.onap.clamp.clds.AbstractItCase;
 import org.onap.clamp.clds.client.DcaeHttpConnectionManager;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.boot.test.context.SpringBootTest;
@@ -47,16 +57,54 @@ import org.springframework.test.context.junit4.SpringRunner;
 @RunWith(SpringRunner.class)
 @SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
 @TestPropertySource(locations = "classpath:https/https-test.properties")
-public class DcaeHttpConnectionManagerItCase extends AbstractItCase {
+public class DcaeHttpConnectionManagerItCase {
+
     @Value("${server.port}")
     private String httpsPort;
     @Value("${server.http-to-https-redirection.port}")
     private String httpPort;
+    private static TrustManager[] trustAllCerts = new TrustManager[] {
+            new X509TrustManager() {
+
+                @Override
+                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
+                    return null;
+                }
+
+                @Override
+                public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
+                }
+
+                @Override
+                public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
+                }
+            }
+    };
+
+    private void enableSslNoCheck() throws NoSuchAlgorithmException, KeyManagementException {
+        SSLContext sc = SSLContext.getInstance("SSL");
+        sc.init(null, trustAllCerts, new java.security.SecureRandom());
+        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
+        HostnameVerifier allHostsValid = new HostnameVerifier() {
+
+            @Override
+            public boolean verify(String hostname, SSLSession session) {
+                return true;
+            }
+        };
+        // set the allTrusting verifier
+        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
+    }
+
+    @Before
+    public void setupEnvBeforeTest() throws KeyManagementException, NoSuchAlgorithmException {
+        enableSslNoCheck();
+    }
 
     @Test
     public void testHttpGet() throws Exception {
         String response = DcaeHttpConnectionManager
-                .doDcaeHttpQuery("http://localhost:" + this.httpPort + "/designer/index.html", "GET", null, null, true);
+                .doDcaeHttpQuery("http://localhost:" + this.httpPort + "/designer/index.html", "GET", null, null);
         assertNotNull(response);
         // Should be a redirection so 302, so empty
         assertTrue(response.isEmpty());
@@ -64,8 +112,8 @@ public class DcaeHttpConnectionManagerItCase extends AbstractItCase {
 
     @Test
     public void testHttpsGet() throws Exception {
-        String response = DcaeHttpConnectionManager.doDcaeHttpQuery(
-                "https://localhost:" + this.httpsPort + "/designer/index.html", "GET", null, null, true);
+        String response = DcaeHttpConnectionManager
+                .doDcaeHttpQuery("https://localhost:" + this.httpsPort + "/designer/index.html", "GET", null, null);
         assertNotNull(response);
         // Should contain something
         assertTrue(!response.isEmpty());
@@ -74,21 +122,21 @@ public class DcaeHttpConnectionManagerItCase extends AbstractItCase {
     @Test(expected = BadRequestException.class)
     public void testHttpsGet404() throws IOException {
         DcaeHttpConnectionManager.doDcaeHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html",
-                "GET", null, null, true);
-        fail("Should have raised an BadRequestException exception");
+                "GET", null, null);
+        fail("Should have raised an BadRequestException");
     }
 
     @Test(expected = BadRequestException.class)
     public void testHttpsPost404() throws IOException {
         DcaeHttpConnectionManager.doDcaeHttpQuery("https://localhost:" + this.httpsPort + "/designer/index1.html",
-                "POST", "", "application/json", true);
-        fail("Should have raised an BadRequestException exception");
+                "POST", "", "application/json");
+        fail("Should have raised an BadRequestException");
     }
 
-    @Test(expected = IOException.class)
+    @Test(expected = BadRequestException.class)
     public void testHttpException() throws IOException {
         DcaeHttpConnectionManager.doDcaeHttpQuery("http://localhost:" + this.httpsPort + "/designer/index.html", "GET",
-                null, null, true);
-        fail("Should have raised an IOException exception");
+                null, null);
+        fail("Should have raised an BadRequestException");
     }
 }