uplift oparent version 1.2.1->1.2.3
[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     @Override
66     public void setUp() throws Exception {
67         try {
68             policyEngine = new PolicyEngine("Test/config_pass.properties");
69         } catch (PolicyEngineException e) {
70             logger.error(e.getMessage());
71             fail("PolicyEngine Instantiation Error" + e);
72         }
73         logger.info("Loaded.. PolicyEngine");
74
75         mockPolicyEngine = Mockito.mock(PolicyEngine.class);
76
77         policyParameters.setPolicyClass(PolicyClass.Decision); // required
78         policyParameters.setPolicyName("test.junitTest"); // required
79         policyParameters.setOnapName("test");
80         policyParameters.setPolicyDescription("testing"); // optional
81
82         // Set the Component Attributes... These are Optional
83         Map<String, String> configAttributes = new HashMap<String, String>();
84         configAttributes.put("test", "testing");
85
86         Map<AttributeType, Map<String, String>> attributes =
87                 new HashMap<AttributeType, Map<String, String>>();
88         attributes.put(AttributeType.MATCHING, configAttributes);
89         policyParameters.setAttributes(attributes);
90
91         policyParameters.setRequestID(UUID.randomUUID());
92     }
93
94     /**
95      * Perform post-test clean up
96      *
97      * @throws Exception
98      *
99      * @see TestCase#tearDown()
100      */
101     @Override
102     public void tearDown() throws Exception {
103         super.tearDown();
104         // Add additional tear down code here
105     }
106
107     /**
108      * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
109      */
110     public void testCreatePolicy() {
111         response.setResponseMessage("success");
112         PolicyChangeResponse result = null;
113         try {
114
115             Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
116             result = mockPolicyEngine.createPolicy(policyParameters);
117
118         } catch (Exception e) {
119             logger.warn(e.getMessage());
120         }
121         assertEquals(result, response);
122     }
123
124     /**
125      * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
126      */
127     public void testUpdatePolicy() {
128         response.setResponseMessage("success");
129         PolicyChangeResponse result = null;
130         try {
131
132             Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
133             result = mockPolicyEngine.updatePolicy(policyParameters);
134
135         } catch (Exception e) {
136             logger.warn(e.getMessage());
137         }
138         assertEquals(result, response);
139     }
140
141     @Test
142     public final void testCreatePolicyNullPolicyName() {
143         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
144         policyParameters.setPolicyName(null);
145         try {
146             result = policyEngine.createPolicy(policyParameters);
147         } catch (Exception e) {
148             logger.warn(e.getMessage());
149         }
150         assertEquals(result.getResponseMessage(), response.getResponseMessage());
151     }
152
153     @Test
154     public final void testCreatePolicyNullPolicyScope() {
155         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
156         policyParameters.setPolicyName("test");
157         try {
158             result = policyEngine.createPolicy(policyParameters);
159         } catch (Exception e) {
160             logger.warn(e.getMessage());
161         }
162         assertEquals(result.getResponseMessage(), response.getResponseMessage());
163     }
164
165     @Test
166     public final void testCreatePolicyNullOnapName() {
167         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
168         policyParameters.setOnapName(null);
169         try {
170             result = policyEngine.createPolicy(policyParameters);
171         } catch (Exception e) {
172             logger.warn(e.getMessage());
173         }
174         assertEquals(result.getResponseMessage(), response.getResponseMessage());
175     }
176
177     @Test
178     public final void testUpdatePolicyNullPolicyName() {
179         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
180         policyParameters.setPolicyName(null);
181         try {
182             result = policyEngine.updatePolicy(policyParameters);
183         } catch (Exception e) {
184             logger.warn(e.getMessage());
185         }
186         assertEquals(result.getResponseMessage(), response.getResponseMessage());
187     }
188
189     @Test
190     public final void testUpdatePolicyNullPolicyScope() {
191         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
192         policyParameters.setPolicyName("test");
193         try {
194             result = policyEngine.updatePolicy(policyParameters);
195         } catch (Exception e) {
196             logger.warn(e.getMessage());
197         }
198         assertEquals(result.getResponseMessage(), response.getResponseMessage());
199     }
200
201     @Test
202     public final void testUpdatePolicyNullOnapName() {
203         response.setResponseMessage("PE500 - Process Flow Issue: :500 INTERNAL_SERVER_ERROR:");
204         policyParameters.setOnapName(null);
205         try {
206             result = policyEngine.updatePolicy(policyParameters);
207         } catch (Exception e) {
208             logger.warn(e.getMessage());
209         }
210         assertEquals(result.getResponseMessage(), response.getResponseMessage());
211     }
212 }