summaryrefslogtreecommitdiffstats
path: root/applications/common/src
diff options
context:
space:
mode:
Diffstat (limited to 'applications/common/src')
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java66
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslatorTest.java79
2 files changed, 115 insertions, 30 deletions
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java
index e3d87577..48da9969 100644
--- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java
+++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslator.java
@@ -55,6 +55,7 @@ public class StdBaseTranslator implements ToscaPolicyTranslator {
private static Gson gson = new Gson();
public static final String POLICY_ID = "policy-id";
+ public static final String POLICY_VERSION = "policy-version";
@Override
public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
@@ -98,37 +99,41 @@ public class StdBaseTranslator implements ToscaPolicyTranslator {
return decisionResponse;
}
- @SuppressWarnings("unchecked")
protected void scanObligations(Collection<Obligation> obligations, DecisionResponse decisionResponse) {
for (Obligation obligation : obligations) {
LOGGER.info("Obligation: {}", obligation);
for (AttributeAssignment assignment : obligation.getAttributeAssignments()) {
LOGGER.info("Attribute Assignment: {}", assignment);
- //
- // We care about the content attribute
- //
- if (ToscaDictionary.ID_OBLIGATION_POLICY_MONITORING_CONTENTS
- .equals(assignment.getAttributeId())) {
- //
- // The contents are in Json form
- //
- Object stringContents = assignment.getAttributeValue().getValue();
- LOGGER.info("DCAE contents: {}{}", XacmlPolicyUtils.LINE_SEPARATOR, stringContents);
- //
- // Let's parse it into a map using Gson
- //
- Map<String, Object> result;
- result = gson.fromJson(stringContents.toString(), Map.class);
- //
- // Find the metadata section
- //
- Map<String, Object> metadata = (Map<String, Object>) result.get("metadata");
- if (metadata != null) {
- decisionResponse.getPolicies().put(metadata.get(POLICY_ID).toString(), result);
- } else {
- LOGGER.error("Missing metadata section in policy contained in obligation.");
- }
- }
+ processObligationAttribute(assignment, decisionResponse);
+ }
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ protected void processObligationAttribute(AttributeAssignment assignment, DecisionResponse decisionResponse) {
+ //
+ // We care about the content attribute
+ //
+ if (ToscaDictionary.ID_OBLIGATION_POLICY_MONITORING_CONTENTS
+ .equals(assignment.getAttributeId())) {
+ //
+ // The contents are in Json form
+ //
+ Object stringContents = assignment.getAttributeValue().getValue();
+ LOGGER.info("DCAE contents: {}{}", XacmlPolicyUtils.LINE_SEPARATOR, stringContents);
+ //
+ // Let's parse it into a map using Gson
+ //
+ Map<String, Object> result;
+ result = gson.fromJson(stringContents.toString(), Map.class);
+ //
+ // Find the metadata section
+ //
+ Map<String, Object> metadata = (Map<String, Object>) result.get("metadata");
+ if (metadata != null) {
+ decisionResponse.getPolicies().put(metadata.get(POLICY_ID).toString(), result);
+ } else {
+ LOGGER.error("Missing metadata section in policy contained in obligation.");
}
}
}
@@ -148,18 +153,19 @@ public class StdBaseTranslator implements ToscaPolicyTranslator {
// is saved in the TOSCA Policy Name field.
//
if (! map.containsKey(POLICY_ID)) {
- throw new ToscaPolicyConversionException(policy.getPolicyId() + " missing metadata policy-id");
+ throw new ToscaPolicyConversionException(policy.getPolicyId() + " missing metadata " + POLICY_ID);
}
//
// Ensure the policy-version exists
//
- if (! map.containsKey("policy-version")) {
- throw new ToscaPolicyConversionException(policy.getPolicyId() + " missing metadata policy-version");
+ if (! map.containsKey(POLICY_VERSION)) {
+ throw new ToscaPolicyConversionException(policy.getPolicyId() + " missing metadata "
+ + POLICY_VERSION);
}
//
// Add in the Policy Version
//
- policy.setVersion(map.get("policy-version"));
+ policy.setVersion(map.get(POLICY_VERSION));
return policy;
}
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
new file mode 100644
index 00000000..0b3ce6c3
--- /dev/null
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdBaseTranslatorTest.java
@@ -0,0 +1,79 @@
+/*-
+ * ============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.assertThatThrownBy;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.util.HashMap;
+import java.util.Map;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
+import org.junit.Test;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.pdp.xacml.application.common.ToscaPolicyConversionException;
+
+public class StdBaseTranslatorTest {
+
+ @Test
+ public void test() {
+ StdBaseTranslator translator = new StdBaseTranslator();
+ assertNotNull(translator);
+ assertThatThrownBy(() -> translator.convertPolicy(null)).isInstanceOf(ToscaPolicyConversionException.class);
+ assertNull(translator.convertRequest(null));
+ }
+
+ @Test
+ public void testBadData() throws ToscaPolicyConversionException {
+ TestTranslator translator = new TestTranslator();
+
+ assertThatThrownBy(() -> translator.convertPolicy(
+ new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
+ .hasMessageContaining("missing metadata");
+
+ translator.metadata.put(StdBaseTranslator.POLICY_ID, "random.policy.id");
+
+ assertThatThrownBy(() -> translator.convertPolicy(
+ new ToscaPolicy())).isInstanceOf(ToscaPolicyConversionException.class)
+ .hasMessageContaining("missing metadata");
+
+ translator.metadata.put(StdBaseTranslator.POLICY_VERSION, "1.0.0");
+
+ ToscaPolicy policy = new ToscaPolicy();
+ assertEquals("1.0.0", translator.convertPolicy(policy).getVersion());
+
+ }
+
+ public class TestTranslator extends StdBaseTranslator {
+ public Map<String, String> metadata = new HashMap<>();
+
+ @Override
+ public PolicyType convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
+ PolicyType xacmlPolicy = new PolicyType();
+ this.fillMetadataSection(xacmlPolicy, metadata);
+ return xacmlPolicy;
+ }
+ }
+
+}