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.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;
40 import junit.framework.TestCase;
41 import org.openecomp.policy.common.logging.flexlogger.*;
43 public class ActionPolicyApiTest extends TestCase {
45 private static final Logger logger = FlexLogger.getLogger(ActionPolicyApiTest.class);
47 private PolicyEngine policyEngine = null;
48 private PolicyEngine mockPolicyEngine = null;
50 PolicyChangeResponse result = null;
51 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
52 PolicyParameters policyParameters = new PolicyParameters();
55 protected void setUp() throws Exception {
57 policyEngine = new PolicyEngine("Test/config_pass.properties");
58 } catch (PolicyEngineException e) {
59 logger.error(e.getMessage());
60 fail("PolicyEngine Instantiation Error" + e);
62 logger.info("Loaded.. PolicyEngine");
64 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
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
71 //Set the Component Attributes... These are Optional
72 Map<String, String> configAttributes = new HashMap<String, String>();
73 configAttributes.put("test", "testing");
75 Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
76 attributes.put(AttributeType.MATCHING, configAttributes);
77 policyParameters.setAttributes(attributes);
79 policyParameters.setActionPerformer("PEP");
80 policyParameters.setActionAttribute("testing");
81 policyParameters.setRequestID(UUID.randomUUID());
84 protected void tearDown() throws Exception {
89 public final void testCreatePolicy() {
90 response.setResponseMessage("success");
91 PolicyChangeResponse result = null;
94 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
95 result = mockPolicyEngine.createPolicy(policyParameters);
97 } catch (Exception e) {
98 logger.warn(e.getMessage());
100 assertEquals(result, response);
104 public final void testUpdatePolicy() {
105 response.setResponseMessage("success");
106 PolicyChangeResponse result = null;
109 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
110 result = mockPolicyEngine.updatePolicy(policyParameters);
112 } catch (Exception e) {
113 logger.warn(e.getMessage());
115 assertEquals(result, response);
120 public final void testCreatePolicyNullPolicyName() {
121 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
122 policyParameters.setPolicyName(null);
124 result = policyEngine.createPolicy(policyParameters);
125 } catch (Exception e){
126 logger.warn(e.getMessage());
128 assertEquals(result.getResponseMessage(), response.getResponseMessage());
132 public final void testCreatePolicyNullPolicyScope() {
133 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
134 policyParameters.setPolicyName("test");
136 result = policyEngine.createPolicy(policyParameters);
137 } catch (Exception e){
138 logger.warn(e.getMessage());
140 assertEquals(result.getResponseMessage(), response.getResponseMessage());
144 public final void testCreatePolicyNullActionAttributes() {
145 response.setResponseMessage("PE300 - Data Issue: No Action Attribute given.");
146 policyParameters.setActionAttribute(null);
148 result = policyEngine.createPolicy(policyParameters);
149 } catch (Exception e){
150 logger.warn(e.getMessage());
152 assertEquals(result.getResponseMessage(), response.getResponseMessage());
156 public final void testCreatePolicyNullActionPerformer() {
157 response.setResponseMessage("PE300 - Data Issue: No Action Performer given.");
158 policyParameters.setActionPerformer(null);
160 result = policyEngine.createPolicy(policyParameters);
161 } catch (Exception e){
162 logger.warn(e.getMessage());
164 assertEquals(result.getResponseMessage(), response.getResponseMessage());
168 public final void testCreatePolicyInvalidActionPerformer() {
169 response.setResponseMessage("PE300 - Data Issue: Invalid Action Performer given.");
170 policyParameters.setActionPerformer("testfail");
172 result = policyEngine.createPolicy(policyParameters);
173 } catch (Exception e){
174 logger.warn(e.getMessage());
176 assertEquals(result.getResponseMessage(), response.getResponseMessage());
180 public final void testUpdatePolicyNullPolicyName() {
181 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
182 policyParameters.setPolicyName(null);
184 result = policyEngine.updatePolicy(policyParameters);
185 } catch (Exception e){
186 logger.warn(e.getMessage());
188 assertEquals(result.getResponseMessage(), response.getResponseMessage());
192 public final void testUpdatePolicyNullPolicyScope() {
193 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
194 policyParameters.setPolicyName("test");
196 result = policyEngine.updatePolicy(policyParameters);
197 } catch (Exception e){
198 logger.warn(e.getMessage());
200 assertEquals(result.getResponseMessage(), response.getResponseMessage());
204 public final void testUpdatePolicyNullActionAttributes() {
205 response.setResponseMessage("PE300 - Data Issue: No Action Attribute given.");
206 policyParameters.setActionAttribute(null);
208 result = policyEngine.updatePolicy(policyParameters);
209 } catch (Exception e){
210 logger.warn(e.getMessage());
212 assertEquals(result.getResponseMessage(), response.getResponseMessage());
216 public final void testUpdatePolicyNullActionPerformer() {
217 response.setResponseMessage("PE300 - Data Issue: No Action Performer given.");
218 policyParameters.setActionPerformer(null);
220 result = policyEngine.updatePolicy(policyParameters);
221 } catch (Exception e){
222 logger.warn(e.getMessage());
224 assertEquals(result.getResponseMessage(), response.getResponseMessage());
228 public final void testUpdatePolicyInvalidActionPerformer() {
229 response.setResponseMessage("PE300 - Data Issue: Invalid Action Performer given.");
230 policyParameters.setActionPerformer("testfail");
232 result = policyEngine.updatePolicy(policyParameters);
233 } catch (Exception e){
234 logger.warn(e.getMessage());
236 assertEquals(result.getResponseMessage(), response.getResponseMessage());