aboutsummaryrefslogtreecommitdiffstats
path: root/applications/optimization/src/test
diff options
context:
space:
mode:
Diffstat (limited to 'applications/optimization/src/test')
-rw-r--r--applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java60
-rw-r--r--applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationSubscriberRequestTest.java84
-rw-r--r--applications/optimization/src/test/resources/bad-subscriber-policies.yaml34
-rw-r--r--applications/optimization/src/test/resources/test-optimization-policies.yaml (renamed from applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml)1
4 files changed, 173 insertions, 6 deletions
diff --git a/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java
index e457e2f2..85541f18 100644
--- a/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java
+++ b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
Modifications Copyright (C) 2019 Nordix Foundation.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -24,6 +24,7 @@
package org.onap.policy.xacml.pdp.application.optimization;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -50,11 +51,15 @@ import org.junit.runners.MethodSorters;
import org.onap.policy.common.endpoints.parameters.RestServerParameters;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.coder.StandardYamlCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.common.utils.resources.TextFileUtils;
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.ToscaPolicyTypeIdentifier;
+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.XacmlApplicationException;
import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
import org.onap.policy.pdp.xacml.application.common.XacmlPolicyUtils;
@@ -214,7 +219,10 @@ public class OptimizationPdpApplicationTest {
//
// Now load all the optimization policies
//
- TestUtils.loadPolicies("src/test/resources/vCPE.policies.optimization.input.tosca.yaml", service);
+ List<ToscaPolicy> loadedPolicies = TestUtils.loadPolicies("src/test/resources/test-optimization-policies.yaml",
+ service);
+ assertThat(loadedPolicies).isNotNull();
+ assertThat(loadedPolicies).hasSize(14);
//
// Ask for a decision for available default policies
//
@@ -337,7 +345,6 @@ public class OptimizationPdpApplicationTest {
//
// Add gold as a scope
//
- //((List<String>)baseRequest.getResource().get("scope")).add("gold");
((List<String>)baseRequest.getContext().get("subscriberName")).add("subscriber_a");
//
// Ask for a decision for specific US vCPE vG gold
@@ -345,7 +352,8 @@ public class OptimizationPdpApplicationTest {
DecisionResponse response = makeDecision();
assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(6);
+ assertThat(response.getPolicies()).hasSize(6);
+ assertThat(response.getAdvice()).hasSize(2);
//
// Validate it
//
@@ -369,7 +377,8 @@ public class OptimizationPdpApplicationTest {
DecisionResponse response = makeDecision();
assertThat(response).isNotNull();
- assertThat(response.getPolicies().size()).isEqualTo(8);
+ assertThat(response.getPolicies()).hasSize(8);
+ assertThat(response.getAdvice()).hasSize(2);
//
// Validate it
//
@@ -448,6 +457,47 @@ public class OptimizationPdpApplicationTest {
validateDecision(response, baseRequest);
}
+ @Test
+ public void test999BadSubscriberPolicies() throws Exception {
+ final StandardYamlCoder yamlCoder = new StandardYamlCoder();
+ //
+ // Decode it
+ //
+ String policyYaml = ResourceUtils.getResourceAsString("src/test/resources/bad-subscriber-policies.yaml");
+ //
+ // Serialize it into a class
+ //
+ ToscaServiceTemplate serviceTemplate;
+ try {
+ serviceTemplate = yamlCoder.decode(policyYaml, ToscaServiceTemplate.class);
+ } catch (CoderException e) {
+ throw new XacmlApplicationException("Failed to decode policy from resource file", e);
+ }
+ //
+ // Make sure all the fields are setup properly
+ //
+ JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+ jtst.fromAuthorative(serviceTemplate);
+ ToscaServiceTemplate completedJtst = jtst.toAuthorative();
+ //
+ // Get the policies
+ //
+ for (Map<String, ToscaPolicy> policies : completedJtst.getToscaTopologyTemplate().getPolicies()) {
+ for (ToscaPolicy policy : policies.values()) {
+ if ("missing-subscriberProperties".equals(policy.getName())) {
+ assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() ->
+ service.loadPolicy(policy));
+ } else if ("missing-subscriberName".equals(policy.getName())) {
+ assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() ->
+ service.loadPolicy(policy));
+ } else if ("missing-subscriberRole".equals(policy.getName())) {
+ assertThatExceptionOfType(XacmlApplicationException.class).isThrownBy(() ->
+ service.loadPolicy(policy));
+ }
+ }
+ }
+ }
+
private DecisionResponse makeDecision() {
Pair<DecisionResponse, Response> decision = service.makeDecision(baseRequest, null);
LOGGER.info("Request Resources {}", baseRequest.getResource());
diff --git a/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationSubscriberRequestTest.java b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationSubscriberRequestTest.java
new file mode 100644
index 00000000..3fd9e44b
--- /dev/null
+++ b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationSubscriberRequestTest.java
@@ -0,0 +1,84 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2020 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.xacml.pdp.application.optimization;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import com.att.research.xacml.api.Request;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.Test;
+import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.resources.TextFileUtils;
+import org.onap.policy.models.decisions.concepts.DecisionRequest;
+
+public class OptimizationSubscriberRequestTest {
+ private static StandardCoder gson = new StandardCoder();
+ private DecisionRequest request;
+
+ /**
+ * setupLoadDecision loads the decision request object for testing.
+ *
+ * @throws Exception Exception if unable to load
+ */
+ @Before
+ public void setupLoadDecision() throws Exception {
+ request = gson.decode(
+ TextFileUtils
+ .getTextFileAsString(
+ "src/test/resources/decision.optimization.input.json"),
+ DecisionRequest.class);
+
+ assertThat(request).isNotNull();
+ }
+
+ @Test
+ public void testDecisionRequest() throws Exception {
+ //
+ // Add context
+ //
+ Map<String, Object> context = new HashMap<>();
+ context.put("subscriberRole", "role1");
+ request.setContext(context);
+ //
+ // Check the return object
+ //
+ Request xacml = OptimizationSubscriberRequest.createInstance(request);
+
+ assertThat(xacml).isNotNull();
+ assertThat(xacml.getRequestAttributes()).hasSize(4);
+
+ List<String> roles = new ArrayList<>();
+ roles.add("role-A");
+ roles.add("role-B");
+ context.put("subscriberRole", "role1");
+ request.setContext(context);
+
+ xacml = OptimizationSubscriberRequest.createInstance(request);
+ assertThat(xacml).isNotNull();
+ assertThat(xacml.getRequestAttributes()).hasSize(4);
+ }
+}
diff --git a/applications/optimization/src/test/resources/bad-subscriber-policies.yaml b/applications/optimization/src/test/resources/bad-subscriber-policies.yaml
new file mode 100644
index 00000000..db638fb8
--- /dev/null
+++ b/applications/optimization/src/test/resources/bad-subscriber-policies.yaml
@@ -0,0 +1,34 @@
+tosca_definitions_version: tosca_simple_yaml_1_0_0
+topology_template:
+ policies:
+ -
+ missing-subscriberProperties:
+ type: onap.policies.optimization.service.SubscriberPolicy
+ version: 1.0.0
+ type_version: 1.0.0
+ properties:
+ scope: []
+ services: [vCPE]
+ identity: subscriber_vCPE
+ -
+ missing-subscriberName:
+ type: onap.policies.optimization.service.SubscriberPolicy
+ version: 1.0.0
+ type_version: 1.0.0
+ properties:
+ scope: []
+ services: [vCPE]
+ identity: subscriber_vCPE
+ subscriberProperties:
+ missingName: foo
+ -
+ missing-subscriberRole:
+ type: onap.policies.optimization.service.SubscriberPolicy
+ version: 1.0.0
+ type_version: 1.0.0
+ properties:
+ scope: []
+ services: [vCPE]
+ identity: subscriber_vCPE
+ subscriberProperties:
+ subscriberName: foo \ No newline at end of file
diff --git a/applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml b/applications/optimization/src/test/resources/test-optimization-policies.yaml
index fb848637..f893158b 100644
--- a/applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml
+++ b/applications/optimization/src/test/resources/test-optimization-policies.yaml
@@ -255,7 +255,6 @@ topology_template:
properties:
scope: []
services: [vCPE]
-# resources: [vGMuxInfra, vG]
geography: [US, INTERNATIONAL]
identity: vCPE_Query_Policy
queryProperties: