2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.test;
23 import java.util.HashMap;
25 import java.util.UUID;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.openecomp.policy.api.AttributeType;
31 import org.openecomp.policy.api.PolicyChangeResponse;
32 import org.openecomp.policy.api.PolicyClass;
33 import org.openecomp.policy.api.PolicyEngine;
34 import org.openecomp.policy.api.PolicyEngineException;
35 import org.openecomp.policy.api.PolicyParameters;
36 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
37 import org.openecomp.policy.common.logging.flexlogger.Logger;
38 import org.openecomp.policy.std.StdPolicyChangeResponse;
40 import junit.framework.TestCase;
42 public class ActionPolicyApiTest extends TestCase {
44 private static final Logger logger = FlexLogger.getLogger(ActionPolicyApiTest.class);
46 private PolicyEngine policyEngine = null;
47 private PolicyEngine mockPolicyEngine = null;
49 PolicyChangeResponse result = null;
50 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
51 PolicyParameters policyParameters = new PolicyParameters();
54 protected void setUp() throws Exception {
56 policyEngine = new PolicyEngine("Test/config_pass.properties");
57 } catch (PolicyEngineException e) {
58 logger.error(e.getMessage());
59 fail("PolicyEngine Instantiation Error" + e);
61 logger.info("Loaded.. PolicyEngine");
63 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
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
70 //Set the Component Attributes... These are Optional
71 Map<String, String> configAttributes = new HashMap<String, String>();
72 configAttributes.put("test", "testing");
74 Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
75 attributes.put(AttributeType.MATCHING, configAttributes);
76 policyParameters.setAttributes(attributes);
78 policyParameters.setActionPerformer("PEP");
79 policyParameters.setActionAttribute("testing");
80 policyParameters.setRequestID(UUID.randomUUID());
83 protected void tearDown() throws Exception {
88 public final void testCreatePolicy() {
89 response.setResponseMessage("success");
90 PolicyChangeResponse result = null;
93 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
94 result = mockPolicyEngine.createPolicy(policyParameters);
96 } catch (Exception e) {
97 logger.warn(e.getMessage());
99 assertEquals(result, response);
103 public final void testUpdatePolicy() {
104 response.setResponseMessage("success");
105 PolicyChangeResponse result = null;
108 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
109 result = mockPolicyEngine.updatePolicy(policyParameters);
111 } catch (Exception e) {
112 logger.warn(e.getMessage());
114 assertEquals(result, response);
119 public final void testCreatePolicyNullPolicyName() {
120 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
121 policyParameters.setPolicyName(null);
123 result = policyEngine.createPolicy(policyParameters);
124 } catch (Exception e){
125 logger.warn(e.getMessage());
127 //assertEquals(result.getResponseMessage(), response.getResponseMessage());
131 public final void testCreatePolicyNullPolicyScope() {
132 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
133 policyParameters.setPolicyName("test");
135 result = policyEngine.createPolicy(policyParameters);
136 } catch (Exception e){
137 logger.warn(e.getMessage());
139 //assertEquals(result.getResponseMessage(), response.getResponseMessage());
143 public final void testCreatePolicyNullActionAttributes() {
144 response.setResponseMessage("PE300 - Data Issue: No Action Attribute given.");
145 policyParameters.setActionAttribute(null);
147 result = policyEngine.createPolicy(policyParameters);
148 } catch (Exception e){
149 logger.warn(e.getMessage());
151 //assertEquals(result.getResponseMessage(), response.getResponseMessage());
155 public final void testCreatePolicyNullActionPerformer() {
156 response.setResponseMessage("PE300 - Data Issue: No Action Performer given.");
157 policyParameters.setActionPerformer(null);
159 result = policyEngine.createPolicy(policyParameters);
160 } catch (Exception e){
161 logger.warn(e.getMessage());
163 //assertEquals(result.getResponseMessage(), response.getResponseMessage());
167 public final void testCreatePolicyInvalidActionPerformer() {
168 response.setResponseMessage("PE300 - Data Issue: Invalid Action Performer given.");
169 policyParameters.setActionPerformer("testfail");
171 result = policyEngine.createPolicy(policyParameters);
172 } catch (Exception e){
173 logger.warn(e.getMessage());
175 //assertEquals(result.getResponseMessage(), response.getResponseMessage());
179 public final void testUpdatePolicyNullPolicyName() {
180 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
181 policyParameters.setPolicyName(null);
183 result = policyEngine.updatePolicy(policyParameters);
184 } catch (Exception e){
185 logger.warn(e.getMessage());
187 //assertEquals(result.getResponseMessage(), response.getResponseMessage());
191 public final void testUpdatePolicyNullPolicyScope() {
192 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
193 policyParameters.setPolicyName("test");
195 result = policyEngine.updatePolicy(policyParameters);
196 } catch (Exception e){
197 logger.warn(e.getMessage());
199 //assertEquals(result.getResponseMessage(), response.getResponseMessage());
203 public final void testUpdatePolicyNullActionAttributes() {
204 response.setResponseMessage("PE300 - Data Issue: No Action Attribute given.");
205 policyParameters.setActionAttribute(null);
207 result = policyEngine.updatePolicy(policyParameters);
208 } catch (Exception e){
209 logger.warn(e.getMessage());
211 //assertEquals(result.getResponseMessage(), response.getResponseMessage());
215 public final void testUpdatePolicyNullActionPerformer() {
216 response.setResponseMessage("PE300 - Data Issue: No Action Performer given.");
217 policyParameters.setActionPerformer(null);
219 result = policyEngine.updatePolicy(policyParameters);
220 } catch (Exception e){
221 logger.warn(e.getMessage());
223 //assertEquals(result.getResponseMessage(), response.getResponseMessage());
227 public final void testUpdatePolicyInvalidActionPerformer() {
228 response.setResponseMessage("PE300 - Data Issue: Invalid Action Performer given.");
229 policyParameters.setActionPerformer("testfail");
231 result = policyEngine.updatePolicy(policyParameters);
232 } catch (Exception e){
233 logger.warn(e.getMessage());
235 //assertEquals(result.getResponseMessage(), response.getResponseMessage());