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