2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright (C) 2019 Samsung
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.test;
25 import java.util.HashMap;
27 import java.util.UUID;
28 import org.junit.Test;
29 import org.mockito.Mockito;
30 import org.onap.policy.api.AttributeType;
31 import org.onap.policy.api.PolicyChangeResponse;
32 import org.onap.policy.api.PolicyClass;
33 import org.onap.policy.api.PolicyEngine;
34 import org.onap.policy.api.PolicyEngineException;
35 import org.onap.policy.api.PolicyParameters;
36 import org.onap.policy.common.logging.flexlogger.FlexLogger;
37 import org.onap.policy.common.logging.flexlogger.Logger;
38 import org.onap.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 *
47 public class DecisionPolicyApiTest extends TestCase {
49 private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
51 private PolicyEngine policyEngine = null;
52 private PolicyEngine mockPolicyEngine = null;
54 PolicyChangeResponse result = null;
55 StdPolicyChangeResponse response = new StdPolicyChangeResponse();
56 PolicyParameters policyParameters = new PolicyParameters();
59 * Perform pre-test initialization
63 * @see TestCase#setUp()
66 public 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.setOnapName("test");
80 policyParameters.setPolicyDescription("testing"); // optional
82 // Set the Component Attributes... These are Optional
83 Map<String, String> configAttributes = new HashMap<String, String>();
84 configAttributes.put("test", "testing");
86 Map<AttributeType, Map<String, String>> attributes =
87 new HashMap<AttributeType, Map<String, String>>();
88 attributes.put(AttributeType.MATCHING, configAttributes);
89 policyParameters.setAttributes(attributes);
91 policyParameters.setRequestID(UUID.randomUUID());
95 * Perform post-test clean up
99 * @see TestCase#tearDown()
102 public void tearDown() throws Exception {
104 // Add additional tear down code here
108 * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
110 public void testCreatePolicy() {
111 response.setResponseMessage("success");
112 PolicyChangeResponse result = null;
115 Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
116 result = mockPolicyEngine.createPolicy(policyParameters);
118 } catch (Exception e) {
119 logger.warn(e.getMessage());
121 assertEquals(result, response);
125 * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
127 public void testUpdatePolicy() {
128 response.setResponseMessage("success");
129 PolicyChangeResponse result = null;
132 Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
133 result = mockPolicyEngine.updatePolicy(policyParameters);
135 } catch (Exception e) {
136 logger.warn(e.getMessage());
138 assertEquals(result, response);
142 public final void testCreatePolicyNullPolicyName() {
143 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
144 policyParameters.setPolicyName(null);
146 result = policyEngine.createPolicy(policyParameters);
147 } catch (Exception e) {
148 logger.warn(e.getMessage());
150 assertEquals(result.getResponseMessage(), response.getResponseMessage());
154 public final void testCreatePolicyNullPolicyScope() {
155 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
156 policyParameters.setPolicyName("test");
158 result = policyEngine.createPolicy(policyParameters);
159 } catch (Exception e) {
160 logger.warn(e.getMessage());
162 assertEquals(result.getResponseMessage(), response.getResponseMessage());
166 public final void testCreatePolicyNullOnapName() {
167 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
168 policyParameters.setOnapName(null);
170 result = policyEngine.createPolicy(policyParameters);
171 } catch (Exception e) {
172 logger.warn(e.getMessage());
174 assertEquals(result.getResponseMessage(), response.getResponseMessage());
178 public final void testUpdatePolicyNullPolicyName() {
179 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
180 policyParameters.setPolicyName(null);
182 result = policyEngine.updatePolicy(policyParameters);
183 } catch (Exception e) {
184 logger.warn(e.getMessage());
186 assertEquals(result.getResponseMessage(), response.getResponseMessage());
190 public final void testUpdatePolicyNullPolicyScope() {
191 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
192 policyParameters.setPolicyName("test");
194 result = policyEngine.updatePolicy(policyParameters);
195 } catch (Exception e) {
196 logger.warn(e.getMessage());
198 assertEquals(result.getResponseMessage(), response.getResponseMessage());
202 public final void testUpdatePolicyNullOnapName() {
203 response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
204 policyParameters.setOnapName(null);
206 result = policyEngine.updatePolicy(policyParameters);
207 } catch (Exception e) {
208 logger.warn(e.getMessage());
210 assertEquals(result.getResponseMessage(), response.getResponseMessage());