2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
20 package org.onap.policy.pdp.rest.api.services;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertNull;
25 import static org.junit.Assert.assertTrue;
26 import java.io.FileInputStream;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.LinkedList;
30 import java.util.List;
32 import java.util.Properties;
33 import java.util.UUID;
34 import org.junit.After;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.onap.policy.api.AttributeType;
38 import org.onap.policy.api.PolicyClass;
39 import org.onap.policy.api.PolicyException;
40 import org.onap.policy.api.PolicyParameters;
42 public class DecisionPolicyServiceTest {
44 DecisionPolicyService service = null;
47 public void setUp() throws Exception {
48 Properties prop = new Properties();
49 prop.load(new FileInputStream("src/test/resources/pass.xacml.pdp.properties"));
50 String succeeded = prop.getProperty("xacml.rest.pap.url");
51 List<String> paps = Arrays.asList(succeeded.split(","));
52 PAPServices.setPaps(paps);
53 PAPServices.setJunit(true);
55 PolicyParameters policyParameters = new PolicyParameters();
56 policyParameters.setPolicyClass(PolicyClass.Decision);
57 policyParameters.setPolicyName("Test.testDecisionPolicy");
58 policyParameters.setOnapName("MSO");
60 .setPolicyDescription("This is a sample Decision policy UPDATE example with Settings");
62 Map<String, String> configAttributes = new HashMap<>();
63 configAttributes.put("Template", "UpdateTemplate");
64 configAttributes.put("controller", "default");
65 configAttributes.put("SamPoll", "30");
66 configAttributes.put("value", "abcd");
67 Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
68 attributes.put(AttributeType.MATCHING, configAttributes);
69 Map<String, String> settingsMap = new HashMap<>();
70 settingsMap.put("server", "5");
71 attributes.put(AttributeType.SETTINGS, settingsMap);
72 policyParameters.setAttributes(attributes);
74 List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
75 List<String> dynamicRuleAlgorithmFunctions = new LinkedList<>();
76 List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
77 List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
78 dynamicRuleAlgorithmLabels = Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7");
79 dynamicRuleAlgorithmField1 =
80 Arrays.asList("S_server", "cap", "cobal", "A2", "Config", "A4", "A1");
81 dynamicRuleAlgorithmFunctions = Arrays.asList("integer-equal", "string-contains",
82 "integer-equal", "and", "integer-greater-than", "or", "and");
83 dynamicRuleAlgorithmField2 = Arrays.asList("90", "ca", "90", "A3", "45", "A5", "A6");
84 policyParameters.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
85 policyParameters.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
86 policyParameters.setDynamicRuleAlgorithmFunctions(dynamicRuleAlgorithmFunctions);
87 policyParameters.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
89 policyParameters.setRequestID(UUID.randomUUID());
90 policyParameters.setGuard(true);
91 policyParameters.setRiskLevel("5");
92 policyParameters.setRiskType("TEST");
93 String policyName = "testDecisionPolicy";
94 String policyScope = "Test";
95 service = new DecisionPolicyService(policyName, policyScope, policyParameters);
99 public void tearDown() throws Exception {
100 PAPServices.setPaps(null);
101 PAPServices.setJunit(false);
105 public final void testDecisionPolicyService() {
106 assertNotNull(service);
110 public final void testGetValidation() {
111 assertTrue(service.getValidation());
115 public final void testGetMessage() {
116 String message = service.getMessage();
121 public final void testGetResult() throws PolicyException {
122 service.getValidation();
123 String result = service.getResult(false);
124 assertEquals("success", result);
128 public void testExtraPaths() throws PolicyException {
130 String value = "testVal";
131 PolicyParameters params = new PolicyParameters();
132 Map<AttributeType, Map<String, String>> attributes =
133 new HashMap<AttributeType, Map<String, String>>();
134 // attributes.put(AttributeType.MATCHING, null);
135 attributes.put(AttributeType.SETTINGS, null);
136 params.setAttributes(attributes);
137 DecisionPolicyService service = new DecisionPolicyService(value, value, params);
139 // Negative test methods
140 assertEquals(false, service.getValidation());
141 assertEquals("success", service.getResult(true));
142 attributes.remove(AttributeType.SETTINGS);
143 attributes.put(AttributeType.MATCHING, null);
144 assertEquals("success", service.getResult(true));