summaryrefslogtreecommitdiffstats
path: root/applications/common/src/test/java/org
diff options
context:
space:
mode:
authorPamela Dragosh <pdragosh@research.att.com>2019-11-04 16:18:08 -0500
committerPamela Dragosh <pdragosh@research.att.com>2019-11-12 18:48:04 -0500
commitfe3a8ec467beae89bca9d10b8b5b39f98c81ca01 (patch)
tree284c92033a8a504c46615ee26be6e7955938488f /applications/common/src/test/java/org
parent1bedb591cc68c10c7db916fda8a3f02d67c2314d (diff)
Implement closest match algorithm
Needed to add more obligations, which make it easier to scan through obligations and be able to pull information about the policy. Adding weight as an obligation for Optimization policies in order to implement "closest match" algorithm. Moved Obligation to a support class. Added JUnit code coverage on the translator classes. Split some methods up to reduce complexity via sonar. Issue-ID: POLICY-2066 Change-Id: Ibb13d2dc0a63ab2a6d585b0697a0c1d129fa8f7b Signed-off-by: Pamela Dragosh <pdragosh@research.att.com>
Diffstat (limited to 'applications/common/src/test/java/org')
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/OnapObligationTest.java144
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/TestUtilsCommon.java126
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaDictionaryTest.java5
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslatorTest.java198
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslatorTest.java155
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchablePolicyRequestTest.java35
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java84
7 files changed, 701 insertions, 46 deletions
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/OnapObligationTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/OnapObligationTest.java
new file mode 100644
index 00000000..c02d7fb3
--- /dev/null
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/OnapObligationTest.java
@@ -0,0 +1,144 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pdp.xacml.application.common;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.Assert.assertNotNull;
+
+import com.att.research.xacml.api.AttributeAssignment;
+import com.att.research.xacml.api.Obligation;
+import com.att.research.xacml.api.XACML3;
+import java.util.Arrays;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+
+public class OnapObligationTest {
+
+ String policyJson;
+ String policyBadJson;
+
+ AttributeAssignment assignmentPolicyId;
+ AttributeAssignment assignmentPolicy;
+ AttributeAssignment assignmentBadPolicy;
+ AttributeAssignment assignmentWeight;
+ AttributeAssignment assignmentPolicyType;
+ AttributeAssignment assignmentUnknown;
+
+ Obligation obligation;
+
+ /**
+ * setup - create test data.
+ */
+ @Before
+ public void setup() {
+ policyJson = ResourceUtils.getResourceAsString("test.policy.json");
+ policyBadJson = ResourceUtils.getResourceAsString("test.policy.bad.json");
+
+ assignmentPolicyId = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_ID.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY.stringValue(),
+ policyJson
+ );
+
+ assignmentPolicy = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
+ policyJson
+ );
+
+ assignmentBadPolicy = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
+ policyBadJson
+ );
+
+ assignmentWeight = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY.stringValue(),
+ 0
+ );
+
+ assignmentPolicyType = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY.stringValue(),
+ "onap.policies.Test"
+ );
+
+ assignmentUnknown = TestUtilsCommon.createAttributeAssignment(
+ "foo:bar",
+ XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue(),
+ 10.2
+ );
+
+ obligation = TestUtilsCommon.createXacmlObligation(
+ ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
+ Arrays.asList(assignmentPolicyId, assignmentPolicy, assignmentWeight, assignmentPolicyType,
+ assignmentUnknown));
+ }
+
+ @Test
+ public void testObligation() {
+ OnapObligation onapObligation = new OnapObligation(obligation);
+ assertNotNull(onapObligation);
+ assertThat(onapObligation.getPolicyId()).isEqualTo(assignmentPolicyId.getAttributeValue().getValue());
+ assertThat(onapObligation.getPolicyType()).isEqualTo(assignmentPolicyType.getAttributeValue().getValue());
+ assertThat(onapObligation.getPolicyContent()).isEqualTo(assignmentPolicy.getAttributeValue().getValue());
+ assertThat(onapObligation.getWeight()).isEqualTo(assignmentWeight.getAttributeValue().getValue());
+ }
+
+ @Test
+ public void testSimplePolicy() {
+ OnapObligation onapObligation = new OnapObligation("my.policy.id", policyJson);
+ assertNotNull(onapObligation);
+ assertThat(onapObligation.getPolicyId()).isEqualTo("my.policy.id");
+ assertThat(onapObligation.getPolicyContent()).isEqualTo(policyJson);
+ assertThat(onapObligation.getPolicyType()).isNull();
+ assertThat(onapObligation.getWeight()).isNull();
+ //
+ // Create an obligation from it
+ //
+ ObligationExpressionType newObligation = onapObligation.generateObligation();
+ assertNotNull(newObligation);
+ assertThat(newObligation.getAttributeAssignmentExpression()).hasSize(2);
+ }
+
+
+ @Test
+ public void testWeightedPolicy() {
+ OnapObligation onapObligation = new OnapObligation("my.policy.id", policyJson, "onap.policies.Test", 5);
+ assertNotNull(onapObligation);
+ assertThat(onapObligation.getPolicyId()).isEqualTo("my.policy.id");
+ assertThat(onapObligation.getPolicyContent()).isEqualTo(policyJson);
+ assertThat(onapObligation.getPolicyType()).isEqualTo("onap.policies.Test");
+ assertThat(onapObligation.getWeight()).isEqualTo(5);
+ //
+ // Create an obligation from it
+ //
+ ObligationExpressionType newObligation = onapObligation.generateObligation();
+ assertNotNull(newObligation);
+ assertThat(newObligation.getAttributeAssignmentExpression()).hasSize(4);
+ }
+
+}
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/TestUtilsCommon.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/TestUtilsCommon.java
new file mode 100644
index 00000000..ab5dde27
--- /dev/null
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/TestUtilsCommon.java
@@ -0,0 +1,126 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pdp.xacml.application.common;
+
+import com.att.research.xacml.api.AttributeAssignment;
+import com.att.research.xacml.api.Decision;
+import com.att.research.xacml.api.IdReference;
+import com.att.research.xacml.api.Obligation;
+import com.att.research.xacml.api.Response;
+import com.att.research.xacml.api.StatusCode;
+import com.att.research.xacml.api.XACML3;
+import com.att.research.xacml.std.IdentifierImpl;
+import com.att.research.xacml.std.StdAttributeAssignment;
+import com.att.research.xacml.std.StdAttributeValue;
+import com.att.research.xacml.std.StdIdReference;
+import com.att.research.xacml.std.StdMutableObligation;
+import com.att.research.xacml.std.StdMutableResponse;
+import com.att.research.xacml.std.StdMutableResult;
+import com.att.research.xacml.std.StdStatus;
+import com.att.research.xacml.std.StdVersion;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+
+public class TestUtilsCommon {
+
+ /**
+ * createAttributeAssignment.
+ *
+ * @param <T> Object can be String, Integer, Double, Boolean
+ * @param id String attribute id
+ * @param category String for the attribute category
+ * @param value Object containing a value
+ * @return AttributeAssignment object
+ */
+ public static <T> AttributeAssignment createAttributeAssignment(String id, String category, T value) {
+ StdAttributeValue<T> attributeValue = null;
+ if (value instanceof String) {
+ attributeValue = new StdAttributeValue<>(XACML3.ID_DATATYPE_STRING, value);
+ } else if (value instanceof Integer) {
+ attributeValue = new StdAttributeValue<>(XACML3.ID_DATATYPE_INTEGER, value);
+ } else if (value instanceof Double) {
+ attributeValue = new StdAttributeValue<>(XACML3.ID_DATATYPE_DOUBLE, value);
+ } else if (value instanceof Boolean) {
+ attributeValue = new StdAttributeValue<>(XACML3.ID_DATATYPE_BOOLEAN, value);
+ } else {
+ throw new IllegalArgumentException("Unsupported value object " + value.getClass());
+ }
+
+ return new StdAttributeAssignment(new IdentifierImpl(category),
+ new IdentifierImpl(id), "", attributeValue);
+ }
+
+ /**
+ * createXacmlObligation.
+ *
+ * @param id String obligation id
+ * @param attributeAssignments Collection of AttributeAssignment objects
+ * @return Obligation object
+ */
+ public static Obligation createXacmlObligation(String id, Collection<AttributeAssignment> attributeAssignments) {
+ return new StdMutableObligation(new IdentifierImpl(id), attributeAssignments);
+ }
+
+ /**
+ * createPolicyIdList.
+ *
+ * @param ids Map of policy Ids
+ * @return {@code Collection<IdReference>} objects
+ * @throws ParseException ParseException
+ */
+ public static Collection<IdReference> createPolicyIdList(Map<String, String> ids) throws ParseException {
+ List<IdReference> policyIds = new ArrayList<>();
+
+ for (Entry<String, String> entrySet : ids.entrySet()) {
+ policyIds.add(new StdIdReference(new IdentifierImpl(entrySet.getKey()),
+ StdVersion.newInstance(entrySet.getValue())));
+ }
+
+ return policyIds;
+ }
+
+ /**
+ * createXacmlResponse.
+ *
+ * @param code StatusCode
+ * @param decision Decision
+ * @param obligations Collection of Obligation objects
+ * @param policyIds Collection of IdReference objects
+ * @return Response object
+ */
+ public static Response createXacmlResponse(StatusCode code, Decision decision, Collection<Obligation> obligations,
+ Collection<IdReference> policyIds) {
+ StdStatus status = new StdStatus(code);
+
+ StdMutableResult result = new StdMutableResult(decision, status);
+ result.addObligations(obligations);
+ result.addPolicyIdentifiers(policyIds);
+
+ return new StdMutableResponse(result);
+ }
+
+}
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaDictionaryTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaDictionaryTest.java
index b86617c5..b6a7ae20 100644
--- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaDictionaryTest.java
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/ToscaDictionaryTest.java
@@ -23,7 +23,6 @@
package org.onap.policy.pdp.xacml.application.common;
import static org.assertj.core.api.Assertions.assertThatCode;
-import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.lang.reflect.Constructor;
@@ -47,10 +46,6 @@ public class ToscaDictionaryTest {
constructor.setAccessible(true);
constructor.newInstance();
}).doesNotThrowAnyException();
- //
- // Probably don't need these as these ID's are used by other components
- //
- assertNotNull(ToscaDictionary.ID_OBLIGATION_MONITORING_ISSUER);
}
}
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslatorTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslatorTest.java
index 0b3ce6c3..a7391fab 100644
--- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslatorTest.java
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslatorTest.java
@@ -22,30 +22,172 @@
package org.onap.policy.pdp.xacml.application.common.std;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
+import com.att.research.xacml.api.AttributeAssignment;
+import com.att.research.xacml.api.Decision;
+import com.att.research.xacml.api.IdReference;
+import com.att.research.xacml.api.Obligation;
+import com.att.research.xacml.api.Response;
+import com.att.research.xacml.api.XACML3;
+import com.att.research.xacml.std.StdStatusCode;
+import java.text.ParseException;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicySetType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
+import org.junit.Before;
import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.resources.ResourceUtils;
+import org.onap.policy.models.decisions.concepts.DecisionResponse;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.pdp.xacml.application.common.TestUtilsCommon;
+import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
public class StdBaseTranslatorTest {
+ String policyJson;
+ String policyBadJson;
+
+ AttributeAssignment assignmentPolicyId;
+ AttributeAssignment assignmentPolicy;
+ AttributeAssignment assignmentBadPolicy;
+ AttributeAssignment assignmentWeight;
+ AttributeAssignment assignmentPolicyType;
+ AttributeAssignment assignmentUnknown;
+
+ Obligation obligation;
+
+ /**
+ * beforeSetup - loads and creates objects used later by the tests.
+ * @throws CoderException CoderException
+ *
+ */
+ @Before
+ public void beforeSetup() throws CoderException {
+ policyJson = ResourceUtils.getResourceAsString("test.policy.json");
+ policyBadJson = ResourceUtils.getResourceAsString("test.policy.bad.json");
+
+ assignmentPolicyId = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_ID.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY.stringValue(),
+ "policy.id"
+ );
+
+ assignmentPolicy = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
+ policyJson
+ );
+
+ assignmentBadPolicy = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
+ policyBadJson
+ );
+
+ assignmentWeight = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_WEIGHT_CATEGORY.stringValue(),
+ 0
+ );
+
+ assignmentPolicyType = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_TYPE.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_TYPE_CATEGORY.stringValue(),
+ "onap.policies.Test"
+ );
+
+ assignmentUnknown = TestUtilsCommon.createAttributeAssignment(
+ "foo:bar",
+ XACML3.ID_SUBJECT_CATEGORY_ACCESS_SUBJECT.stringValue(),
+ 10.2
+ );
+
+ obligation = TestUtilsCommon.createXacmlObligation(
+ ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
+ Arrays.asList(assignmentPolicyId, assignmentPolicy, assignmentWeight, assignmentPolicyType));
+ }
+
@Test
- public void test() {
- StdBaseTranslator translator = new StdBaseTranslator();
+ public void test() throws ParseException {
+ StdBaseTranslator translator = new MyStdBaseTranslator();
assertNotNull(translator);
assertThatThrownBy(() -> translator.convertPolicy(null)).isInstanceOf(ToscaPolicyConversionException.class);
assertNull(translator.convertRequest(null));
+
+ assertThat(translator.generateAnyOfForPolicyType("foo.bar")).isNotNull();
+ assertThat(translator.generateAnyOfForPolicyType("foo.bar").getAllOf().size()).isEqualTo(1);
+
+ assertThat(translator.generateConditionForPolicyType("foo.bar")).isNotNull();
+ assertThat(translator.generateConditionForPolicyType("foo.bar").getExpression()).isNotNull();
+
+ //
+ // Test the addObligation method
+ //
+ PolicySetType policySet = new PolicySetType();
+
+ translator.addObligation(policySet, "policy.id", policyJson, 0, "foo.bar");
+
+ assertThat(policySet.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
+ assertThat(policySet.getObligationExpressions().getObligationExpression().get(0)
+ .getAttributeAssignmentExpression().size()).isEqualTo(4);
+
+ PolicyType policy = new PolicyType();
+ translator.addObligation(policy, null, policyJson, null, null);
+
+ assertThat(policy.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
+ assertThat(policy.getObligationExpressions().getObligationExpression().get(0)
+ .getAttributeAssignmentExpression().size()).isEqualTo(1);
+
+ RuleType rule = new RuleType();
+ translator.addObligation(rule, "policy.id", null, null, "foo.bar");
+
+ assertThat(rule.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
+ assertThat(rule.getObligationExpressions().getObligationExpression().get(0)
+ .getAttributeAssignmentExpression().size()).isEqualTo(2);
+
+ rule = new RuleType();
+ translator.addObligation(rule, null, null, null, null);
+
+ assertThat(rule.getObligationExpressions().getObligationExpression().size()).isEqualTo(1);
+ assertThat(rule.getObligationExpressions().getObligationExpression().get(0)
+ .getAttributeAssignmentExpression().size()).isEqualTo(0);
+
+ //
+ // Should not throw an exception
+ //
+ translator.addObligation(new String(), "policy.id", policyJson, null, "foo.bar");
+
+ //
+ // Test the response conversion
+ //
+ Map<String, String> ids = new HashMap<>();
+ ids.put("onap.policies.Test", "1.0.0");
+ Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
+
+ Response xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
+ Decision.PERMIT, Arrays.asList(obligation), policyIds);
+
+ DecisionResponse decision = translator.convertResponse(xacmlResponse);
+
+ assertNotNull(decision);
+
+ assertThat(decision.getPolicies()).isNotNull();
+ assertThat(decision.getPolicies().size()).isEqualTo(0);
}
@Test
- public void testBadData() throws ToscaPolicyConversionException {
+ public void testBadData() throws ToscaPolicyConversionException, ParseException {
TestTranslator translator = new TestTranslator();
assertThatThrownBy(() -> translator.convertPolicy(
@@ -63,12 +205,60 @@ public class StdBaseTranslatorTest {
ToscaPolicy policy = new ToscaPolicy();
assertEquals("1.0.0", translator.convertPolicy(policy).getVersion());
+ Map<String, String> ids = new HashMap<>();
+ ids.put("onap.policies.Test", "1.0.0");
+ Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
+
+ Response xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
+ Decision.PERMIT, Arrays.asList(obligation), policyIds);
+
+ DecisionResponse decision = translator.convertResponse(xacmlResponse);
+
+ assertNotNull(decision);
+
+ assertThat(decision.getPolicies()).isNotNull();
+ assertThat(decision.getPolicies().size()).isEqualTo(0);
+
+ //
+ // This will need more work when I fix
+ // the convertResponse
+ //
+
+ Obligation badObligation = TestUtilsCommon.createXacmlObligation(
+ ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
+ Arrays.asList(assignmentBadPolicy, assignmentUnknown));
+
+ xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_MISSING_ATTRIBUTE,
+ Decision.PERMIT, Arrays.asList(badObligation), policyIds);
+
+ decision = translator.convertResponse(xacmlResponse);
+
+ assertNotNull(decision);
+
+ xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
+ Decision.DENY, Arrays.asList(badObligation), policyIds);
+
+ decision = translator.convertResponse(xacmlResponse);
+
+ assertNotNull(decision);
+ }
+
+ private class MyStdBaseTranslator extends StdBaseTranslator {
+
+ @Override
+ protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
+ }
+
}
- public class TestTranslator extends StdBaseTranslator {
+ private class TestTranslator extends StdBaseTranslator {
public Map<String, String> metadata = new HashMap<>();
@Override
+ protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
+ }
+
+ @Override
public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
PolicyType xacmlPolicy = new PolicyType();
this.fillMetadataSection(xacmlPolicy, metadata);
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslatorTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslatorTest.java
new file mode 100644
index 00000000..93124552
--- /dev/null
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdCombinedPolicyResultsTranslatorTest.java
@@ -0,0 +1,155 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.pdp.xacml.application.common.std;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
+import static org.junit.Assert.assertNotNull;
+
+import com.att.research.xacml.api.AttributeAssignment;
+import com.att.research.xacml.api.Decision;
+import com.att.research.xacml.api.IdReference;
+import com.att.research.xacml.api.Obligation;
+import com.att.research.xacml.api.Response;
+import com.att.research.xacml.std.StdStatusCode;
+import java.text.ParseException;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.coder.CoderException;
+import org.onap.policy.common.utils.coder.StandardCoder;
+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.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.onap.policy.pdp.xacml.application.common.TestUtilsCommon;
+import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
+import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
+
+public class StdCombinedPolicyResultsTranslatorTest {
+
+ String policyJson;
+ String policyBadJson;
+
+ AttributeAssignment assignmentPolicyId;
+ AttributeAssignment assignmentPolicy;
+ AttributeAssignment assignmentBadPolicy;
+
+ Obligation obligation;
+
+ /**
+ * setup - preload policies.
+ */
+ @Before
+ public void setup() {
+ policyJson = ResourceUtils.getResourceAsString("test.policy.json");
+ policyBadJson = ResourceUtils.getResourceAsString("test.policy.bad.json");
+
+ assignmentPolicyId = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_ID.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_ID_CATEGORY.stringValue(),
+ policyJson
+ );
+
+ assignmentPolicy = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
+ policyJson
+ );
+
+ assignmentBadPolicy = TestUtilsCommon.createAttributeAssignment(
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT.stringValue(),
+ ToscaDictionary.ID_OBLIGATION_POLICY_CONTENT_CATEGORY.stringValue(),
+ policyBadJson
+ );
+
+
+ obligation = TestUtilsCommon.createXacmlObligation(
+ ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
+ Arrays.asList(assignmentPolicyId, assignmentPolicy));
+
+ }
+
+ @Test
+ public void test() throws ParseException {
+ StdCombinedPolicyResultsTranslator translator = new StdCombinedPolicyResultsTranslator();
+
+ assertNotNull(translator);
+ assertThatThrownBy(() -> translator.convertPolicy(null)).isInstanceOf(ToscaPolicyConversionException.class);
+
+ assertThat(translator.generateTargetType("policy.id", "onap.policy.type", "1.0.0")).isNotNull();
+
+ Map<String, String> ids = new HashMap<>();
+ ids.put("onap.policies.Test", "1.0.0");
+ Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
+
+ Response xacmlResponse = TestUtilsCommon.createXacmlResponse(StdStatusCode.STATUS_CODE_OK,
+ Decision.PERMIT, Arrays.asList(obligation), policyIds);
+
+ DecisionResponse decision = translator.convertResponse(xacmlResponse);
+
+ assertNotNull(decision);
+
+ assertThat(decision.getPolicies()).isNotNull();
+ assertThat(decision.getPolicies().size()).isEqualTo(1);
+ }
+
+ @Test
+ public void testConvert() throws ToscaPolicyConversionException, CoderException {
+ StdCombinedPolicyResultsTranslator translator = new StdCombinedPolicyResultsTranslator();
+
+ assertThatThrownBy(() -> translator.convertPolicy(
+ new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
+ .hasMessageContaining("missing metadata");
+
+ StandardCoder coder = new StandardCoder();
+
+ ToscaServiceTemplate template = coder.decode(policyJson, ToscaServiceTemplate.class);
+ //
+ // Make sure all the fields are setup properly
+ //
+ JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+ jtst.fromAuthorative(template);
+ ToscaServiceTemplate completedJtst = jtst.toAuthorative();
+ for (Map<String, ToscaPolicy> policies : completedJtst.getToscaTopologyTemplate().getPolicies()) {
+ for (ToscaPolicy policy : policies.values()) {
+ translator.convertPolicy(policy);
+ }
+ }
+ }
+
+ @Test
+ public void testDecision() {
+ StdCombinedPolicyResultsTranslator translator = new StdCombinedPolicyResultsTranslator();
+
+ DecisionRequest decision = new DecisionRequest();
+ Map<String, Object> resource = new HashMap<>();
+ decision.setResource(resource);
+ assertNotNull(translator.convertRequest(decision));
+ }
+}
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchablePolicyRequestTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchablePolicyRequestTest.java
index d3e362c1..f259c207 100644
--- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchablePolicyRequestTest.java
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchablePolicyRequestTest.java
@@ -96,39 +96,4 @@ public class StdMatchablePolicyRequestTest {
}
- /*
- @Test
- public void testCreateInstance_StringValues() {
- resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY, POLICY_SCOPE);
- resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY + "-x", "unused value");
- resources.put(StdMatchablePolicyRequest.POLICY_TYPE_KEY, POLICY_TYPE);
-
- stdreq = StdMatchablePolicyRequest.createInstance(decreq);
-
- Collection<String> res = stdreq.getPolicyScopes();
- assertFalse(res.isEmpty());
- assertEquals(POLICY_SCOPE, res.iterator().next());
-
- res = stdreq.getPolicyTypes();
- assertFalse(res.isEmpty());
- assertEquals(POLICY_TYPE, res.iterator().next());
- }
-
- @Test
- public void testCreateInstance_Collections() {
- resources.put(StdMatchablePolicyRequest.POLICY_SCOPE_KEY, Collections.singleton(POLICY_SCOPE));
- resources.put(StdMatchablePolicyRequest.POLICY_TYPE_KEY, Collections.singleton(POLICY_TYPE));
-
- stdreq = StdMatchablePolicyRequest.createInstance(decreq);
-
- Collection<String> res = stdreq.getPolicyScopes();
- assertFalse(res.isEmpty());
- assertEquals(POLICY_SCOPE, res.iterator().next());
-
- res = stdreq.getPolicyTypes();
- assertFalse(res.isEmpty());
- assertEquals(POLICY_TYPE, res.iterator().next());
- }
-*/
-
}
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java
index 07317c0a..e9e8814c 100644
--- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java
@@ -20,13 +20,26 @@
package org.onap.policy.pdp.xacml.application.common.std;
+import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import com.att.research.xacml.api.AttributeAssignment;
+import com.att.research.xacml.api.Decision;
+import com.att.research.xacml.api.IdReference;
+import com.att.research.xacml.api.Obligation;
+import com.att.research.xacml.api.Request;
+import com.att.research.xacml.std.StdStatusCode;
import java.io.IOException;
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;
@@ -37,6 +50,8 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObligationExpressionType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
import org.junit.AfterClass;
import org.junit.BeforeClass;
@@ -52,10 +67,14 @@ import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
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.tosca.authorative.concepts.ToscaPolicy;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.onap.policy.pdp.xacml.application.common.TestUtilsCommon;
+import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
import org.slf4j.Logger;
@@ -141,7 +160,7 @@ public class StdMatchableTranslatorTest {
}
@Test
- public void test() throws CoderException, ToscaPolicyConversionException {
+ public void test() throws CoderException, ToscaPolicyConversionException, ParseException {
//
// Create our translator
//
@@ -168,15 +187,76 @@ public class StdMatchableTranslatorTest {
jtst.fromAuthorative(serviceTemplate);
ToscaServiceTemplate completedJtst = jtst.toAuthorative();
//
- // Get the policies
+ // Convert the policy
//
for (Map<String, ToscaPolicy> policies : completedJtst.getToscaTopologyTemplate().getPolicies()) {
for (ToscaPolicy policy : policies.values()) {
PolicyType translatedPolicy = translator.convertPolicy(policy);
assertNotNull(translatedPolicy);
+ assertThat(translatedPolicy.getObligationExpressions().getObligationExpression()).hasSize(1);
logger.info("Translated policy {} {}", XacmlPolicyUtils.LINE_SEPARATOR, translatedPolicy);
+ //
+ // Shortcut to create an obligation, we are just going to steal
+ // the attributes from the translated policy.
+ //
+ List<AttributeAssignment> listAttributes = new ArrayList<>();
+ ObligationExpressionType xacmlObligation = translatedPolicy.getObligationExpressions()
+ .getObligationExpression().get(0);
+ assertThat(xacmlObligation.getAttributeAssignmentExpression()).hasSize(4);
+ //
+ // Copy into the list
+ //
+ xacmlObligation.getAttributeAssignmentExpression().forEach(assignment -> {
+ Object value = ((AttributeValueType) assignment.getExpression().getValue()).getContent().get(0);
+ listAttributes.add(TestUtilsCommon.createAttributeAssignment(assignment.getAttributeId(),
+ assignment.getCategory(), value));
+ });
+ //
+ // Pretend we got multiple policies to match a fictional request
+ //
+ Obligation obligation1 = TestUtilsCommon.createXacmlObligation(
+ ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
+ listAttributes);
+ Obligation obligation2 = TestUtilsCommon.createXacmlObligation(
+ ToscaDictionary.ID_OBLIGATION_REST_BODY.stringValue(),
+ listAttributes);
+ //
+ // Should ignore this obligation
+ //
+ Obligation obligation3 = TestUtilsCommon.createXacmlObligation(
+ "nobody:cares",
+ listAttributes);
+ //
+ // Create a test XACML Response
+ //
+ Map<String, String> ids = new HashMap<>();
+ ids.put("onap.policies.Test", "1.0.0");
+ Collection<IdReference> policyIds = TestUtilsCommon.createPolicyIdList(ids);
+
+ com.att.research.xacml.api.Response xacmlResponse = TestUtilsCommon.createXacmlResponse(
+ StdStatusCode.STATUS_CODE_OK, Decision.PERMIT,
+ Arrays.asList(obligation1, obligation2, obligation3), policyIds);
+ //
+ // Test the response
+ //
+ DecisionResponse decisionResponse = translator.convertResponse(xacmlResponse);
+ assertNotNull(decisionResponse);
+ assertThat(decisionResponse.getPolicies()).hasSize(1);
}
}
+ //
+ // Test request decisions
+ //
+ DecisionRequest decisionRequest = new DecisionRequest();
+ decisionRequest.setAction("action");
+ decisionRequest.setOnapComponent("onap-component");
+ decisionRequest.setOnapName("onap");
+ Map<String, Object> resource = new HashMap<>();
+ resource.put("matchableString", "I should be matched");
+ decisionRequest.setResource(resource);
+ Request xacmlRequest = translator.convertRequest(decisionRequest);
+ assertNotNull(xacmlRequest);
+ assertThat(xacmlRequest.getRequestAttributes()).hasSize(3);
}
/**