Reformat ONAP-PDP-REST test cases
[policy/engine.git] / ONAP-PDP-REST / src / test / java / org / onap / policy / pdp / rest / api / services / DecisionPolicyServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PDP-REST
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.policy.pdp.rest.api.services;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import java.io.FileInputStream;
29 import java.util.Arrays;
30 import java.util.HashMap;
31 import java.util.LinkedList;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Properties;
35 import java.util.UUID;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.api.AttributeType;
40 import org.onap.policy.api.PolicyClass;
41 import org.onap.policy.api.PolicyException;
42 import org.onap.policy.api.PolicyParameters;
43
44 public class DecisionPolicyServiceTest {
45
46     DecisionPolicyService service = null;
47
48     @Before
49     public void setUp() throws Exception {
50         Properties prop = new Properties();
51         prop.load(new FileInputStream("src/test/resources/pass.xacml.pdp.properties"));
52         String succeeded = prop.getProperty("xacml.rest.pap.url");
53         List<String> paps = Arrays.asList(succeeded.split(","));
54         PAPServices.setPaps(paps);
55         PAPServices.setJunit(true);
56
57         PolicyParameters policyParameters = new PolicyParameters();
58         policyParameters.setPolicyClass(PolicyClass.Decision);
59         policyParameters.setPolicyName("Test.testDecisionPolicy");
60         policyParameters.setOnapName("MSO");
61         policyParameters.setPolicyDescription("This is a sample Decision policy UPDATE example with Settings");
62
63         Map<String, String> configAttributes = new HashMap<>();
64         configAttributes.put("Template", "UpdateTemplate");
65         configAttributes.put("controller", "default");
66         configAttributes.put("SamPoll", "30");
67         configAttributes.put("value", "abcd");
68         Map<AttributeType, Map<String, String>> attributes = new HashMap<>();
69         attributes.put(AttributeType.MATCHING, configAttributes);
70         Map<String, String> settingsMap = new HashMap<>();
71         settingsMap.put("server", "5");
72         attributes.put(AttributeType.SETTINGS, settingsMap);
73         policyParameters.setAttributes(attributes);
74
75         List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
76         List<String> dynamicRuleAlgorithmFunctions = new LinkedList<>();
77         List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
78         List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
79         dynamicRuleAlgorithmLabels = Arrays.asList("A1", "A2", "A3", "A4", "A5", "A6", "A7");
80         dynamicRuleAlgorithmField1 = Arrays.asList("S_server", "cap", "cobal", "A2", "Config", "A4", "A1");
81         dynamicRuleAlgorithmFunctions = Arrays.asList("integer-equal", "string-contains", "integer-equal", "and",
82                 "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 = new HashMap<AttributeType, Map<String, String>>();
133         // attributes.put(AttributeType.MATCHING, null);
134         attributes.put(AttributeType.SETTINGS, null);
135         params.setAttributes(attributes);
136         DecisionPolicyService service = new DecisionPolicyService(value, value, params);
137
138         // Negative test methods
139         assertEquals(false, service.getValidation());
140         assertEquals("success", service.getResult(true));
141         attributes.remove(AttributeType.SETTINGS);
142         attributes.put(AttributeType.MATCHING, null);
143         assertEquals("success", service.getResult(true));
144     }
145 }