Reformat PolicyEngineAPI test cases
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / test / DecisionPolicyApiTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.test;
24
25 import java.util.HashMap;
26 import java.util.Map;
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;
40
41 /**
42  * The class <code>DecisionPolicyApiTest</code> contains tests for the class
43  * {@link <code>PolicyEngine</code>}
44  *
45  * @pattern JUnit Test Case *
46  */
47 public class DecisionPolicyApiTest extends TestCase {
48
49     private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
50
51     private PolicyEngine policyEngine = null;
52     private PolicyEngine mockPolicyEngine = null;
53
54     PolicyChangeResponse result = null;
55     StdPolicyChangeResponse response = new StdPolicyChangeResponse();
56     PolicyParameters policyParameters = new PolicyParameters();
57
58     /**
59      * Perform pre-test initialization
60      *
61      * @throws Exception
62      *
63      * @see TestCase#setUp()
64      */
65     public void setUp() throws Exception {
66         try {
67             policyEngine = new PolicyEngine("Test/config_pass.properties");
68         } catch (PolicyEngineException e) {
69             logger.error(e.getMessage());
70             fail("PolicyEngine Instantiation Error" + e);
71         }
72         logger.info("Loaded.. PolicyEngine");
73
74         mockPolicyEngine = Mockito.mock(PolicyEngine.class);
75
76         policyParameters.setPolicyClass(PolicyClass.Decision); // required
77         policyParameters.setPolicyName("test.junitTest"); // required
78         policyParameters.setOnapName("test");
79         policyParameters.setPolicyDescription("testing"); // optional
80
81         // Set the Component Attributes... These are Optional
82         Map<String, String> configAttributes = new HashMap<String, String>();
83         configAttributes.put("test", "testing");
84
85         Map<AttributeType, Map<String, String>> attributes =
86                 new HashMap<AttributeType, Map<String, String>>();
87         attributes.put(AttributeType.MATCHING, configAttributes);
88         policyParameters.setAttributes(attributes);
89
90         policyParameters.setRequestID(UUID.randomUUID());
91     }
92
93     /**
94      * Perform post-test clean up
95      *
96      * @throws Exception
97      *
98      * @see TestCase#tearDown()
99      */
100     public void tearDown() throws Exception {
101         super.tearDown();
102         // Add additional tear down code here
103     }
104
105     /**
106      * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
107      */
108     public void testCreatePolicy() {
109         response.setResponseMessage("success");
110         PolicyChangeResponse result = null;
111         try {
112
113             Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
114             result = mockPolicyEngine.createPolicy(policyParameters);
115
116         } catch (Exception e) {
117             logger.warn(e.getMessage());
118         }
119         assertEquals(result, response);
120     }
121
122     /**
123      * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
124      */
125     public void testUpdatePolicy() {
126         response.setResponseMessage("success");
127         PolicyChangeResponse result = null;
128         try {
129
130             Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
131             result = mockPolicyEngine.updatePolicy(policyParameters);
132
133         } catch (Exception e) {
134             logger.warn(e.getMessage());
135         }
136         assertEquals(result, response);
137     }
138
139     @Test
140     public final void testCreatePolicyNullPolicyName() {
141         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
142         policyParameters.setPolicyName(null);
143         try {
144             result = policyEngine.createPolicy(policyParameters);
145         } catch (Exception e) {
146             logger.warn(e.getMessage());
147         }
148         assertEquals(result.getResponseMessage(), response.getResponseMessage());
149     }
150
151     @Test
152     public final void testCreatePolicyNullPolicyScope() {
153         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
154         policyParameters.setPolicyName("test");
155         try {
156             result = policyEngine.createPolicy(policyParameters);
157         } catch (Exception e) {
158             logger.warn(e.getMessage());
159         }
160         assertEquals(result.getResponseMessage(), response.getResponseMessage());
161     }
162
163     @Test
164     public final void testCreatePolicyNullOnapName() {
165         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
166         policyParameters.setOnapName(null);
167         try {
168             result = policyEngine.createPolicy(policyParameters);
169         } catch (Exception e) {
170             logger.warn(e.getMessage());
171         }
172         assertEquals(result.getResponseMessage(), response.getResponseMessage());
173     }
174
175     @Test
176     public final void testUpdatePolicyNullPolicyName() {
177         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
178         policyParameters.setPolicyName(null);
179         try {
180             result = policyEngine.updatePolicy(policyParameters);
181         } catch (Exception e) {
182             logger.warn(e.getMessage());
183         }
184         assertEquals(result.getResponseMessage(), response.getResponseMessage());
185     }
186
187     @Test
188     public final void testUpdatePolicyNullPolicyScope() {
189         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
190         policyParameters.setPolicyName("test");
191         try {
192             result = policyEngine.updatePolicy(policyParameters);
193         } catch (Exception e) {
194             logger.warn(e.getMessage());
195         }
196         assertEquals(result.getResponseMessage(), response.getResponseMessage());
197     }
198
199     @Test
200     public final void testUpdatePolicyNullOnapName() {
201         response.setResponseMessage("PE500 - Process Flow Issue: :500:");
202         policyParameters.setOnapName(null);
203         try {
204             result = policyEngine.updatePolicy(policyParameters);
205         } catch (Exception e) {
206             logger.warn(e.getMessage());
207         }
208         assertEquals(result.getResponseMessage(), response.getResponseMessage());
209     }
210 }