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