Fix Raw XACML Decision API Serialization
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestDecision.java
index 6c6e35b..b369ad3 100644 (file)
@@ -1,6 +1,6 @@
 /*-
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
  * Modifications Copyright (C) 2019 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -46,19 +46,20 @@ import org.junit.BeforeClass;
 import org.junit.ClassRule;
 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.RestClientParameters;
 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.common.utils.resources.ResourceUtils;
 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.CommonTestData;
+import org.onap.policy.pdpx.main.parameters.XacmlApplicationParameters;
 import org.onap.policy.pdpx.main.parameters.XacmlPdpParameterGroup;
 import org.onap.policy.pdpx.main.startstop.Main;
 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
@@ -73,6 +74,8 @@ public class TestDecision {
     private static Main main;
     private static HttpClient client;
     private static CommonTestData testData = new CommonTestData();
+    private static final String APPLICATION_XACML_XML = "application/xacml+xml";
+    private static final String APPLICATION_XACML_JSON = "application/xacml+json";
 
     @ClassRule
     public static final TemporaryFolder appsFolder = new TemporaryFolder();
@@ -102,12 +105,16 @@ public class TestDecision {
         //
         RestServerParameters rest =
             testData.toObject(testData.getRestServerParametersMap(port), RestServerParameters.class);
-        RestServerParameters policyApiParameters =
-                        testData.toObject(testData.getPolicyApiParametersMap(false), RestServerParameters.class);
+        RestClientParameters policyApiParameters =
+                        testData.toObject(testData.getPolicyApiParametersMap(false), RestClientParameters.class);
         TopicParameterGroup topicParameterGroup =
                         testData.toObject(testData.getTopicParametersMap(false), TopicParameterGroup.class);
-        XacmlPdpParameterGroup params = new XacmlPdpParameterGroup("XacmlPdpGroup", rest, policyApiParameters,
-                        topicParameterGroup, apps.getAbsolutePath());
+        final XacmlApplicationParameters xacmlApplicationParameters =
+                testData.toObject(testData.getXacmlapplicationParametersMap(false,
+                        apps.getAbsolutePath().toString()), XacmlApplicationParameters.class);
+        XacmlPdpParameterGroup params =
+                new XacmlPdpParameterGroup("XacmlPdpParameters", "XacmlPdpGroup", "xacml", rest, policyApiParameters,
+                        topicParameterGroup, xacmlApplicationParameters);
         final Gson gson = new GsonBuilder().create();
         File fileParams = appsFolder.newFile("params.json");
         String jsonParams = gson.toJson(params);
@@ -117,7 +124,7 @@ public class TestDecision {
         // Start the service
         //
         main = startXacmlPdpService(fileParams);
-        XacmlPdpActivator.getCurrent().startXacmlRestController();
+        XacmlPdpActivator.getCurrent().enableApi();
         //
         // Make sure it is running
         //
@@ -138,7 +145,6 @@ public class TestDecision {
 
     @Test
     public void testDecision_UnsupportedAction() throws Exception {
-
         LOGGER.info("Running test testDecision_UnsupportedAction");
 
         DecisionRequest request = new DecisionRequest();
@@ -160,7 +166,6 @@ public class TestDecision {
     @Test
     public void testDecision_Guard() throws KeyManagementException, NoSuchAlgorithmException,
         ClassNotFoundException {
-
         LOGGER.info("Running test testDecision_Guard");
 
         DecisionRequest request = new DecisionRequest();
@@ -178,6 +183,31 @@ public class TestDecision {
         assertThat(response.getStatus()).isEqualTo("Permit");
     }
 
+    @Test
+    public void testDecision_Native() throws IOException {
+        LOGGER.info("Running test testDecision_Native");
+
+        String xmlRequestAsString = ResourceUtils.getResourceAsString(
+                "src/test/resources/decisions/decision.native.request.xml");
+        if (xmlRequestAsString == null) {
+            throw new IOException("failed to read the xml request");
+        }
+
+        String jsonRequestAsString = ResourceUtils.getResourceAsString(
+                "src/test/resources/decisions/decision.native.request.json");
+        if (jsonRequestAsString == null) {
+            throw new IOException("failed to read the json request");
+        }
+
+        String responseFromXmlRequest = getNativeDecision(xmlRequestAsString, APPLICATION_XACML_XML);
+        LOGGER.info("Response from xml request {}", responseFromXmlRequest);
+        assertThat(responseFromXmlRequest).contains("<Response xmlns=").contains("NotApplicable");
+
+        String responseFromJsonRequest = getNativeDecision(jsonRequestAsString, APPLICATION_XACML_JSON);
+        LOGGER.info("Response from json request {}", responseFromJsonRequest);
+        assertThat(responseFromJsonRequest).contains("\"Response\":").contains("NotApplicable");
+    }
+
     private static Main startXacmlPdpService(File params) throws PolicyXacmlPdpException {
         final String[] XacmlPdpConfigParameters = {"-c", params.getAbsolutePath()};
         return new Main(XacmlPdpConfigParameters);
@@ -188,19 +218,26 @@ public class TestDecision {
     }
 
     private DecisionResponse getDecision(DecisionRequest request) {
-
         Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
-        Response response = client.post("", entityRequest, Collections.emptyMap());
+        Response response = client.post("/decision", entityRequest, Collections.emptyMap());
 
         assertEquals(200, response.getStatus());
 
         return HttpClient.getBody(response, DecisionResponse.class);
     }
 
-    private ErrorResponse getErrorDecision(DecisionRequest request) {
+    private String getNativeDecision(String request, String mediaType) {
+        Entity<String> entityRequest = Entity.entity(request, mediaType);
+        Response response = client.post("/xacml", entityRequest, Map.of("Accept", mediaType));
+
+        assertEquals(200, response.getStatus());
 
+        return HttpClient.getBody(response, String.class);
+    }
+
+    private ErrorResponse getErrorDecision(DecisionRequest request) {
         Entity<DecisionRequest> entityRequest = Entity.entity(request, MediaType.APPLICATION_JSON);
-        Response response = client.post("", entityRequest, Collections.emptyMap());
+        Response response = client.post("/decision", entityRequest, Collections.emptyMap());
 
         assertEquals(400, response.getStatus());
 
@@ -208,11 +245,10 @@ public class TestDecision {
     }
 
     private static HttpClient getNoAuthHttpClient() throws HttpClientConfigException {
-        return HttpClientFactoryInstance.getClientFactory().build(BusTopicParams.builder()
+        return HttpClientFactoryInstance.getClientFactory().build(RestClientParameters.builder()
                 .clientName("testDecisionClient")
-                .serializationProvider(GsonMessageBodyHandler.class.getName())
                 .useHttps(false).allowSelfSignedCerts(false).hostname("localhost").port(port)
-                .basePath("policy/pdpx/v1/decision")
+                .basePath("policy/pdpx/v1")
                 .userName("healthcheck").password("zb!XztG34").managed(true).build());
     }
 
@@ -224,5 +260,4 @@ public class TestDecision {
             LOGGER.error("Failed to copy {} to {}", source, dest);
         }
     }
-
-}
\ No newline at end of file
+}