Reformat PolicyEngineAPI test cases
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / test / ActionPolicyApiTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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
23 package org.onap.policy.test;
24
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.UUID;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.onap.policy.api.AttributeType;
32 import org.onap.policy.api.PolicyChangeResponse;
33 import org.onap.policy.api.PolicyClass;
34 import org.onap.policy.api.PolicyEngine;
35 import org.onap.policy.api.PolicyEngineException;
36 import org.onap.policy.api.PolicyParameters;
37 import org.onap.policy.common.logging.flexlogger.FlexLogger;
38 import org.onap.policy.common.logging.flexlogger.Logger;
39 import org.onap.policy.std.StdPolicyChangeResponse;
40 import junit.framework.TestCase;
41
42 public class ActionPolicyApiTest extends TestCase {
43
44     private static final Logger logger = FlexLogger.getLogger(ActionPolicyApiTest.class);
45
46     private PolicyEngine policyEngine = null;
47     private PolicyEngine mockPolicyEngine = null;
48
49     PolicyChangeResponse result = null;
50     StdPolicyChangeResponse response = new StdPolicyChangeResponse();
51     PolicyParameters policyParameters = new PolicyParameters();
52
53     @Before
54     public void setUp() throws Exception {
55         try {
56             policyEngine = new PolicyEngine("Test/config_pass.properties");
57         } catch (PolicyEngineException e) {
58             logger.error(e.getMessage());
59             fail("PolicyEngine Instantiation Error" + e);
60         }
61         logger.info("Loaded.. PolicyEngine");
62
63         mockPolicyEngine = Mockito.mock(PolicyEngine.class);
64
65         policyParameters.setPolicyClass(PolicyClass.Action); // required
66         policyParameters.setPolicyName("test.junitTest"); // required
67         policyParameters.setPolicyDescription("testing"); // optional
68         // policyParameters.setPolicyScope("test"); //Directory will be created where the Policies
69         // are saved... this displays a a subscope on the GUI
70
71         // Set the Component Attributes... These are Optional
72         Map<String, String> configAttributes = new HashMap<String, String>();
73         configAttributes.put("test", "testing");
74
75         Map<AttributeType, Map<String, String>> attributes =
76                 new HashMap<AttributeType, Map<String, String>>();
77         attributes.put(AttributeType.MATCHING, configAttributes);
78         policyParameters.setAttributes(attributes);
79
80         policyParameters.setActionPerformer("PEP");
81         policyParameters.setActionAttribute("testing");
82         policyParameters.setRequestID(UUID.randomUUID());
83     }
84
85     public void tearDown() throws Exception {
86         super.tearDown();
87     }
88
89     @Test
90     public final void testCreatePolicy() {
91         response.setResponseMessage("success");
92         PolicyChangeResponse result = null;
93         try {
94
95             Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
96             result = mockPolicyEngine.createPolicy(policyParameters);
97
98         } catch (Exception e) {
99             logger.warn(e.getMessage());
100         }
101         assertEquals(result, response);
102     }
103
104     @Test
105     public final void testUpdatePolicy() {
106         response.setResponseMessage("success");
107         PolicyChangeResponse result = null;
108         try {
109
110             Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
111             result = mockPolicyEngine.updatePolicy(policyParameters);
112
113         } catch (Exception e) {
114             logger.warn(e.getMessage());
115         }
116         assertEquals(result, response);
117
118     }
119
120     @Test
121     public final void testCreatePolicyNullPolicyName() {
122         response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
123         policyParameters.setPolicyName(null);
124         try {
125             result = policyEngine.createPolicy(policyParameters);
126         } catch (Exception e) {
127             logger.warn(e.getMessage());
128         }
129         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
130     }
131
132     @Test
133     public final void testCreatePolicyNullPolicyScope() {
134         response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
135         policyParameters.setPolicyName("test");
136         try {
137             result = policyEngine.createPolicy(policyParameters);
138         } catch (Exception e) {
139             logger.warn(e.getMessage());
140         }
141         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
142     }
143
144     @Test
145     public final void testCreatePolicyNullActionAttributes() {
146         response.setResponseMessage("PE300 - Data Issue: No Action Attribute given.");
147         policyParameters.setActionAttribute(null);
148         try {
149             result = policyEngine.createPolicy(policyParameters);
150         } catch (Exception e) {
151             logger.warn(e.getMessage());
152         }
153         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
154     }
155
156     @Test
157     public final void testCreatePolicyNullActionPerformer() {
158         response.setResponseMessage("PE300 - Data Issue: No Action Performer given.");
159         policyParameters.setActionPerformer(null);
160         try {
161             result = policyEngine.createPolicy(policyParameters);
162         } catch (Exception e) {
163             logger.warn(e.getMessage());
164         }
165         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
166     }
167
168     @Test
169     public final void testCreatePolicyInvalidActionPerformer() {
170         response.setResponseMessage("PE300 - Data Issue: Invalid Action Performer given.");
171         policyParameters.setActionPerformer("testfail");
172         try {
173             result = policyEngine.createPolicy(policyParameters);
174         } catch (Exception e) {
175             logger.warn(e.getMessage());
176         }
177         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
178     }
179
180     @Test
181     public final void testUpdatePolicyNullPolicyName() {
182         response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
183         policyParameters.setPolicyName(null);
184         try {
185             result = policyEngine.updatePolicy(policyParameters);
186         } catch (Exception e) {
187             logger.warn(e.getMessage());
188         }
189         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
190     }
191
192     @Test
193     public final void testUpdatePolicyNullPolicyScope() {
194         response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
195         policyParameters.setPolicyName("test");
196         try {
197             result = policyEngine.updatePolicy(policyParameters);
198         } catch (Exception e) {
199             logger.warn(e.getMessage());
200         }
201         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
202     }
203
204     @Test
205     public final void testUpdatePolicyNullActionAttributes() {
206         response.setResponseMessage("PE300 - Data Issue: No Action Attribute given.");
207         policyParameters.setActionAttribute(null);
208         try {
209             result = policyEngine.updatePolicy(policyParameters);
210         } catch (Exception e) {
211             logger.warn(e.getMessage());
212         }
213         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
214     }
215
216     @Test
217     public final void testUpdatePolicyNullActionPerformer() {
218         response.setResponseMessage("PE300 - Data Issue: No Action Performer given.");
219         policyParameters.setActionPerformer(null);
220         try {
221             result = policyEngine.updatePolicy(policyParameters);
222         } catch (Exception e) {
223             logger.warn(e.getMessage());
224         }
225         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
226     }
227
228     @Test
229     public final void testUpdatePolicyInvalidActionPerformer() {
230         response.setResponseMessage("PE300 - Data Issue: Invalid Action Performer given.");
231         policyParameters.setActionPerformer("testfail");
232         try {
233             result = policyEngine.updatePolicy(policyParameters);
234         } catch (Exception e) {
235             logger.warn(e.getMessage());
236         }
237         // assertEquals(result.getResponseMessage(), response.getResponseMessage());
238     }
239
240
241 }