b80eaed89c565f2c313ec044ac4bf5e88c2a8de2
[policy/engine.git] / ONAP-PAP-REST / src / test / java / org / onap / policy / pap / xacml / rest / components / ActionPolicyTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP-PAP-REST
4  * ================================================================================
5  * Copyright (C) 2017 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.pap.xacml.rest.components;
23
24 import static org.junit.Assert.*;
25 import static org.mockito.Mockito.when;
26 import java.util.HashMap;
27 import java.util.LinkedList;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.UUID;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.rest.adapter.PolicyRestAdapter;
38 import com.att.research.xacml.util.XACMLProperties;
39
40
41 public class ActionPolicyTest {
42
43     private static Logger logger = FlexLogger.getLogger(ActionPolicyTest.class);
44     PolicyRestAdapter policyAdapter = new PolicyRestAdapter();
45     List<String> dynamicRuleAlgorithmLabels = new LinkedList<>();
46     List<String> dynamicRuleAlgorithmCombo = new LinkedList<>();
47     List<String> dynamicRuleAlgorithmField1 = new LinkedList<>();
48     List<String> dynamicRuleAlgorithmField2 = new LinkedList<>();
49     Map<String, String> attributeMap = new HashMap<>();
50     ActionPolicy component = null;
51
52     /**
53      * @throws java.lang.Exception
54      */
55     @Before
56     public void setUp() throws Exception {
57         logger.info("setUp: Entering");
58         System.setProperty(XACMLProperties.XACML_PROPERTIES_NAME,
59                 "src/test/resources/xacml.pap.properties");
60
61         dynamicRuleAlgorithmLabels.add("test");
62         dynamicRuleAlgorithmField1.add("testField1");
63         dynamicRuleAlgorithmCombo.add("testCombo");
64         dynamicRuleAlgorithmField2.add("testField2");
65
66         policyAdapter.setPolicyName("Test.Action_junitTest");
67         policyAdapter.setPolicyDescription("test");
68         policyAdapter.setRuleCombiningAlgId(
69                 "urn:oasis:names:tc:xacml:3.0:rule-combining-algorithm:permit-overrides");
70         policyAdapter.setPolicyType("Action");
71         policyAdapter.setEditPolicy(false);
72         policyAdapter.setDomainDir("Test");
73         policyAdapter.setNewFileName("Test.Action_junitTest.1.xml");
74         policyAdapter.setHighestVersion(1);
75         policyAdapter.setPolicyID("urn:xacml:policy:id:" + UUID.randomUUID());
76
77         policyAdapter.setActionDictHeader("");
78         policyAdapter.setActionDictType("REST");
79         policyAdapter.setActionDictUrl("onap.org");
80         policyAdapter.setActionDictMethod("GET");
81         policyAdapter.setActionAttribute("test");
82         policyAdapter.setActionBody("test");
83
84         policyAdapter.setDynamicRuleAlgorithmLabels(dynamicRuleAlgorithmLabels);
85         policyAdapter.setDynamicRuleAlgorithmCombo(dynamicRuleAlgorithmCombo);
86         policyAdapter.setDynamicRuleAlgorithmField1(dynamicRuleAlgorithmField1);
87         policyAdapter.setDynamicRuleAlgorithmField2(dynamicRuleAlgorithmField2);
88
89         attributeMap.put("java", "test");
90         policyAdapter.setDynamicFieldConfigAttributes(attributeMap);
91
92         component = new ActionPolicy(policyAdapter, null);
93
94         logger.info("setUp: exit");
95     }
96
97     /**
98      * @throws java.lang.Exception
99      */
100     @After
101     public void tearDown() throws Exception {}
102
103     /**
104      * Test method for
105      * {@link org.openecomp.policy.pap.xacml.rest.components.ActionPolicy#savePolicies()}.
106      */
107     @Test
108     public void testSavePolicies() {
109         ActionPolicy mockAction = Mockito.mock(component.getClass());
110
111         Map<String, String> successMap = new HashMap<>();
112
113         successMap.put("success", "success");
114
115         try {
116             when(mockAction.savePolicies()).thenReturn(successMap);
117             successMap = mockAction.savePolicies();
118         } catch (Exception e) {
119             logger.error("Exception Occured" + e);
120         }
121         assertEquals(successMap.get("success"), "success");
122     }
123
124     /**
125      * Test method for
126      * {@link org.openecomp.policy.pap.xacml.rest.components.ActionPolicy#prepareToSave()}.
127      */
128     @Test
129     public void testPrepareToSave() {
130         logger.debug("test PrepareToSave Policy: enter");
131         boolean response = true;
132
133         try {
134             response = component.prepareToSave();
135         } catch (Exception e) {
136             logger.error("Exception Occured" + e);
137         }
138         assertTrue(response);
139     }
140 }