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.Test;
30 import org.mockito.Mockito;
31 import org.openecomp.policy.api.AttributeType;
32 import org.openecomp.policy.api.PolicyChangeResponse;
33 import org.openecomp.policy.api.PolicyClass;
34 import org.openecomp.policy.api.PolicyEngine;
35 import org.openecomp.policy.api.PolicyEngineException;
36 import org.openecomp.policy.api.PolicyParameters;
37 import org.openecomp.policy.std.StdPolicyChangeResponse;
39 import org.openecomp.policy.common.logging.flexlogger.*;
41 import junit.framework.TestCase;
44 * The class <code>DecisionPolicyApiTest</code> contains tests for the class
45 * {@link <code>PolicyEngine</code>}
47 * @pattern JUnit Test Case
50 public class DecisionPolicyApiTest extends TestCase {
52 private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
54 private PolicyEngine policyEngine = null;
55 private PolicyEngine mockPolicyEngine = null;
57 PolicyChangeResponse result = null;
58 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
59 PolicyParameters policyParameters = new PolicyParameters();
62 * Perform pre-test initialization
66 * @see TestCase#setUp()
68 protected void setUp() throws Exception {
70 policyEngine = new PolicyEngine("Test/config_pass.properties");
71 } catch (PolicyEngineException e) {
72 logger.error(e.getMessage());
73 fail("PolicyEngine Instantiation Error" + e);
75 logger.info("Loaded.. PolicyEngine");
77 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
79 policyParameters.setPolicyClass(PolicyClass.Decision); //required
80 policyParameters.setPolicyName("test.junitTest"); //required
81 policyParameters.setEcompName("test");
82 policyParameters.setPolicyDescription("testing"); //optional
83 //policyParameters.setPolicyScope("test"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
85 //Set the Component Attributes... These are Optional
86 Map<String, String> configAttributes = new HashMap<String, String>();
87 configAttributes.put("test", "testing");
89 Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
90 attributes.put(AttributeType.MATCHING, configAttributes);
91 policyParameters.setAttributes(attributes);
93 //Set the settings... These are Optional
94 /* Map<String, String> settingsMap = new HashMap<String, String>();
95 settingsMap.put("server", "5");
97 Map<AttributeType, Map<String,String>> settings = new HashMap<AttributeType, Map<String, String>>();
98 settings.put(AttributeType.SETTINGS, settingsMap);
99 policyParameters.setSettings(settings);*/
101 policyParameters.setRequestID(UUID.randomUUID());
105 * Perform post-test clean up
109 * @see TestCase#tearDown()
111 protected void tearDown() throws Exception {
113 // Add additional tear down code here
117 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
119 public void testCreatePolicy() {
120 response.setResponseMessage("success");
121 PolicyChangeResponse result = null;
124 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
125 result = mockPolicyEngine.createPolicy(policyParameters);
127 } catch (Exception e) {
128 logger.warn(e.getMessage());
130 assertEquals(result, response);
134 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
136 public void testUpdatePolicy() {
137 response.setResponseMessage("success");
138 PolicyChangeResponse result = null;
141 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
142 result = mockPolicyEngine.updatePolicy(policyParameters);
144 } catch (Exception e) {
145 logger.warn(e.getMessage());
147 assertEquals(result, response);
151 public final void testCreatePolicyNullPolicyName() {
152 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
153 policyParameters.setPolicyName(null);
155 result = policyEngine.createPolicy(policyParameters);
156 } catch (Exception e){
157 logger.warn(e.getMessage());
159 assertEquals(result.getResponseMessage(), response.getResponseMessage());
163 public final void testCreatePolicyNullPolicyScope() {
164 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
165 policyParameters.setPolicyName("test");
167 result = policyEngine.createPolicy(policyParameters);
168 } catch (Exception e){
169 logger.warn(e.getMessage());
171 assertEquals(result.getResponseMessage(), response.getResponseMessage());
175 public final void testCreatePolicyNullEcompName() {
176 response.setResponseMessage("PE300 - Data Issue: No ECOMP Name given.");
177 policyParameters.setEcompName(null);
179 result = policyEngine.createPolicy(policyParameters);
180 } catch (Exception e){
181 logger.warn(e.getMessage());
183 assertEquals(result.getResponseMessage(), response.getResponseMessage());
187 public final void testUpdatePolicyNullPolicyName() {
188 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
189 policyParameters.setPolicyName(null);
191 result = policyEngine.updatePolicy(policyParameters);
192 } catch (Exception e){
193 logger.warn(e.getMessage());
195 assertEquals(result.getResponseMessage(), response.getResponseMessage());
199 public final void testUpdatePolicyNullPolicyScope() {
200 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
201 policyParameters.setPolicyName("test");
203 result = policyEngine.updatePolicy(policyParameters);
204 } catch (Exception e){
205 logger.warn(e.getMessage());
207 assertEquals(result.getResponseMessage(), response.getResponseMessage());
211 public final void testUpdatePolicyNullEcompName() {
212 response.setResponseMessage("PE300 - Data Issue: No ECOMP Name given.");
213 policyParameters.setEcompName(null);
215 result = policyEngine.updatePolicy(policyParameters);
216 } catch (Exception e){
217 logger.warn(e.getMessage());
219 assertEquals(result.getResponseMessage(), response.getResponseMessage());