Fix xacml-pdp due to sonar changes in common
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestDecision.java
index b81336a..0edfc6f 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2019 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,7 +26,6 @@ import static org.junit.Assert.assertEquals;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
-
 import java.io.File;
 import java.io.IOException;
 import java.nio.file.Files;
@@ -37,18 +37,10 @@ import java.security.NoSuchAlgorithmException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
-
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
 import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.Invocation;
-import javax.ws.rs.client.WebTarget;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
-
-import org.glassfish.jersey.client.ClientConfig;
-import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.ClassRule;
@@ -56,14 +48,17 @@ import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
 import org.onap.policy.common.endpoints.http.client.HttpClient;
+import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
+import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
+import org.onap.policy.common.endpoints.parameters.RestServerParameters;
+import org.onap.policy.common.endpoints.parameters.TopicParameterGroup;
 import org.onap.policy.common.gson.GsonMessageBodyHandler;
 import org.onap.policy.common.utils.network.NetworkUtil;
 import org.onap.policy.models.decisions.concepts.DecisionRequest;
 import org.onap.policy.models.decisions.concepts.DecisionResponse;
 import org.onap.policy.models.errors.concepts.ErrorResponse;
 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
-import org.onap.policy.pdpx.main.parameters.RestServerBuilder;
-import org.onap.policy.pdpx.main.parameters.RestServerParameters;
+import org.onap.policy.pdpx.main.parameters.CommonTestData;
 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
 import org.onap.policy.pdpx.main.startstop.Main;
 import org.slf4j.Logger;
@@ -73,7 +68,10 @@ public class TestDecision {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(TestDecision.class);
 
+    private static int port;
     private static Main main;
+    private static HttpClient client;
+    private static CommonTestData testData = new CommonTestData();
 
     @ClassRule
     public static final TemporaryFolder appsFolder = new TemporaryFolder();
@@ -81,15 +79,19 @@ public class TestDecision {
     /**
      * BeforeClass setup environment.
      * @throws IOException Cannot create temp apps folder
+     * @throws Exception exception if service does not start
      */
     @BeforeClass
-    public static void beforeClass() throws IOException {
+    public static void beforeClass() throws Exception {
         System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
         System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
+
+        port = NetworkUtil.allocPort();
+
         //
         // Copy test directory over of the application directories
         //
-        Path src = Paths.get("../packages/policy-xacmlpdp-tarball/src/main/resources/apps");
+        Path src = Paths.get("src/test/resources/apps");
         File apps = appsFolder.newFolder("apps");
         Files.walk(src).forEach(source -> {
             copy(source, apps.toPath().resolve(src.relativize(source)));
@@ -97,9 +99,12 @@ public class TestDecision {
         //
         // Get the parameters file correct.
         //
-        RestServerParameters rest = new RestServerParameters(new RestServerBuilder()
-                .setHost("0.0.0.0").setPort(6969).setUserName("healthcheck").setPassword("zb!XztG34"));
-        XacmlPdpParameterGroup params = new XacmlPdpParameterGroup("XacmlPdpGroup", rest, apps.getAbsolutePath());
+        RestServerParameters rest =
+            testData.toObject(testData.getRestServerParametersMap(port), RestServerParameters.class);
+        TopicParameterGroup topicParameterGroup =
+            testData.toObject(testData.getTopicParametersMap(false), TopicParameterGroup.class);
+        XacmlPdpParameterGroup params =
+            new XacmlPdpParameterGroup("XacmlPdpGroup", rest, topicParameterGroup, apps.getAbsolutePath());
         final Gson gson = new GsonBuilder().create();
         File fileParams = appsFolder.newFile("params.json");
         String jsonParams = gson.toJson(params);
@@ -109,6 +114,16 @@ public class TestDecision {
         // Start the service
         //
         main = startXacmlPdpService(fileParams);
+        //
+        // Make sure it is running
+        //
+        if (!NetworkUtil.isTcpPortOpen("localhost", port, 20, 1000L)) {
+            throw new IllegalStateException("Cannot connect to port " + port);
+        }
+        //
+        // Create a client
+        //
+        client = getNoAuthHttpClient();
     }
 
     @AfterClass
@@ -117,8 +132,7 @@ public class TestDecision {
     }
 
     @Test
-    public void testDecision_UnsupportedAction() throws KeyManagementException, NoSuchAlgorithmException,
-        ClassNotFoundException {
+    public void testDecision_UnsupportedAction() throws Exception {
 
         LOGGER.info("Running test testDecision_UnsupportedAction");
 
@@ -139,7 +153,9 @@ public class TestDecision {
     }
 
     @Test
-    public void testDecision_Guard() throws InterruptedException, IOException {
+    public void testDecision_Guard() throws KeyManagementException, NoSuchAlgorithmException,
+        ClassNotFoundException {
+
         LOGGER.info("Running test testDecision_Guard");
 
         DecisionRequest request = new DecisionRequest();
@@ -157,9 +173,8 @@ public class TestDecision {
         assertThat(response.getStatus()).isEqualTo("Permit");
     }
 
-    private static Main startXacmlPdpService(File params) {
-        final String[] XacmlPdpConfigParameters = {"-c", params.getAbsolutePath(), "-p",
-            "parameters/topic.properties"};
+    private static Main startXacmlPdpService(File params) throws PolicyXacmlPdpException {
+        final String[] XacmlPdpConfigParameters = {"-c", params.getAbsolutePath()};
         return new Main(XacmlPdpConfigParameters);
     }
 
@@ -167,28 +182,17 @@ public class TestDecision {
         main.shutdown();
     }
 
-    private DecisionResponse getDecision(DecisionRequest request) throws InterruptedException, IOException {
-        final ClientConfig clientConfig = new ClientConfig();
-
-        final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
-        clientConfig.register(feature);
-
-        final Client client = ClientBuilder.newClient(clientConfig);
-        final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/decision");
+    private DecisionResponse getDecision(DecisionRequest request) {
 
-        final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
+        Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
+        Response response = client.post("", entityRequest, Collections.emptyMap());
 
-        if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
-            throw new IllegalStateException("Cannot connect to port 6969");
-        }
+        assertEquals(200, response.getStatus());
 
-        return invocationBuilder.post(Entity.json(request), DecisionResponse.class);
+        return HttpClient.getBody(response, DecisionResponse.class);
     }
 
-    private ErrorResponse getErrorDecision(DecisionRequest request) throws KeyManagementException,
-        NoSuchAlgorithmException, ClassNotFoundException {
-
-        HttpClient client = getNoAuthHttpClient();
+    private ErrorResponse getErrorDecision(DecisionRequest request) {
 
         Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
         Response response = client.post("", entityRequest, Collections.emptyMap());
@@ -198,12 +202,11 @@ public class TestDecision {
         return HttpClient.getBody(response, ErrorResponse.class);
     }
 
-    private HttpClient getNoAuthHttpClient()
-            throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException {
-        return HttpClient.factory.build(BusTopicParams.builder()
+    private static HttpClient getNoAuthHttpClient() throws HttpClientConfigException {
+        return HttpClientFactoryInstance.getClientFactory().build(BusTopicParams.builder()
                 .clientName("testDecisionClient")
                 .serializationProvider(GsonMessageBodyHandler.class.getName())
-                .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(6969)
+                .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(port)
                 .basePath("policy/pdpx/v1/decision")
                 .userName("healthcheck").password("zb!XztG34").managed(true).build());
     }