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