JUnit additions for PDP,PDP-REST,SDK,XACML
[policy/engine.git] / ONAP-PDP-REST / src / test / java / org / onap / policy / pdp / rest / api / services / DecisionPolicyServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20 package org.onap.policy.pdp.rest.api.services;
21
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;
31 import java.util.Map;
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;
41
42 public class DecisionPolicyServiceTest {
43
44   DecisionPolicyService service = null;
45
46   @Before
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);
54
55     PolicyParameters policyParameters = new PolicyParameters();
56     policyParameters.setPolicyClass(PolicyClass.Decision);
57     policyParameters.setPolicyName("Test.testDecisionPolicy");
58     policyParameters.setOnapName("MSO");
59     policyParameters
60         .setPolicyDescription("This is a sample Decision policy UPDATE example with Settings");
61
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);
73
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);
88
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);
96   }
97
98   @After
99   public void tearDown() throws Exception {
100     PAPServices.setPaps(null);
101     PAPServices.setJunit(false);
102   }
103
104   @Test
105   public final void testDecisionPolicyService() {
106     assertNotNull(service);
107   }
108
109   @Test
110   public final void testGetValidation() {
111     assertTrue(service.getValidation());
112   }
113
114   @Test
115   public final void testGetMessage() {
116     String message = service.getMessage();
117     assertNull(message);
118   }
119
120   @Test
121   public final void testGetResult() throws PolicyException {
122     service.getValidation();
123     String result = service.getResult(false);
124     assertEquals("success", result);
125   }
126
127   @Test
128   public void testExtraPaths() throws PolicyException {
129     // Set up test data
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);
138
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));
145   }
146
147 }