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.Test;
28 import org.mockito.Mockito;
29 import org.openecomp.policy.api.AttributeType;
30 import org.openecomp.policy.api.PolicyChangeResponse;
31 import org.openecomp.policy.api.PolicyClass;
32 import org.openecomp.policy.api.PolicyEngine;
33 import org.openecomp.policy.api.PolicyEngineException;
34 import org.openecomp.policy.api.PolicyParameters;
35 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
36 import org.openecomp.policy.common.logging.flexlogger.Logger;
37 import org.openecomp.policy.std.StdPolicyChangeResponse;
39 import junit.framework.TestCase;
42 * The class <code>DecisionPolicyApiTest</code> contains tests for the class
43 * {@link <code>PolicyEngine</code>}
45 * @pattern JUnit Test Case
48 public class DecisionPolicyApiTest extends TestCase {
50 private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
52 private PolicyEngine policyEngine = null;
53 private PolicyEngine mockPolicyEngine = null;
55 PolicyChangeResponse result = null;
56 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
57 PolicyParameters policyParameters = new PolicyParameters();
60 * Perform pre-test initialization
64 * @see TestCase#setUp()
66 protected void setUp() throws Exception {
68 policyEngine = new PolicyEngine("Test/config_pass.properties");
69 } catch (PolicyEngineException e) {
70 logger.error(e.getMessage());
71 fail("PolicyEngine Instantiation Error" + e);
73 logger.info("Loaded.. PolicyEngine");
75 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
77 policyParameters.setPolicyClass(PolicyClass.Decision); //required
78 policyParameters.setPolicyName("test.junitTest"); //required
79 policyParameters.setEcompName("test");
80 policyParameters.setPolicyDescription("testing"); //optional
81 //policyParameters.setPolicyScope("test"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
83 //Set the Component Attributes... These are Optional
84 Map<String, String> configAttributes = new HashMap<String, String>();
85 configAttributes.put("test", "testing");
87 Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
88 attributes.put(AttributeType.MATCHING, configAttributes);
89 policyParameters.setAttributes(attributes);
91 //Set the settings... These are Optional
92 /* Map<String, String> settingsMap = new HashMap<String, String>();
93 settingsMap.put("server", "5");
95 Map<AttributeType, Map<String,String>> settings = new HashMap<AttributeType, Map<String, String>>();
96 settings.put(AttributeType.SETTINGS, settingsMap);
97 policyParameters.setSettings(settings);*/
99 policyParameters.setRequestID(UUID.randomUUID());
103 * Perform post-test clean up
107 * @see TestCase#tearDown()
109 protected void tearDown() throws Exception {
111 // Add additional tear down code here
115 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
117 public void testCreatePolicy() {
118 response.setResponseMessage("success");
119 PolicyChangeResponse result = null;
122 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
123 result = mockPolicyEngine.createPolicy(policyParameters);
125 } catch (Exception e) {
126 logger.warn(e.getMessage());
128 assertEquals(result, response);
132 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
134 public void testUpdatePolicy() {
135 response.setResponseMessage("success");
136 PolicyChangeResponse result = null;
139 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
140 result = mockPolicyEngine.updatePolicy(policyParameters);
142 } catch (Exception e) {
143 logger.warn(e.getMessage());
145 assertEquals(result, response);
149 public final void testCreatePolicyNullPolicyName() {
150 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
151 policyParameters.setPolicyName(null);
153 result = policyEngine.createPolicy(policyParameters);
154 } catch (Exception e){
155 logger.warn(e.getMessage());
157 assertEquals(result.getResponseMessage(), response.getResponseMessage());
161 public final void testCreatePolicyNullPolicyScope() {
162 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
163 policyParameters.setPolicyName("test");
165 result = policyEngine.createPolicy(policyParameters);
166 } catch (Exception e){
167 logger.warn(e.getMessage());
169 assertEquals(result.getResponseMessage(), response.getResponseMessage());
173 public final void testCreatePolicyNullEcompName() {
174 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
175 policyParameters.setEcompName(null);
177 result = policyEngine.createPolicy(policyParameters);
178 } catch (Exception e){
179 logger.warn(e.getMessage());
181 assertEquals(result.getResponseMessage(), response.getResponseMessage());
185 public final void testUpdatePolicyNullPolicyName() {
186 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
187 policyParameters.setPolicyName(null);
189 result = policyEngine.updatePolicy(policyParameters);
190 } catch (Exception e){
191 logger.warn(e.getMessage());
193 assertEquals(result.getResponseMessage(), response.getResponseMessage());
197 public final void testUpdatePolicyNullPolicyScope() {
198 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
199 policyParameters.setPolicyName("test");
201 result = policyEngine.updatePolicy(policyParameters);
202 } catch (Exception e){
203 logger.warn(e.getMessage());
205 assertEquals(result.getResponseMessage(), response.getResponseMessage());
209 public final void testUpdatePolicyNullEcompName() {
210 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
211 policyParameters.setEcompName(null);
213 result = policyEngine.updatePolicy(policyParameters);
214 } catch (Exception e){
215 logger.warn(e.getMessage());
217 assertEquals(result.getResponseMessage(), response.getResponseMessage());