Fixes for sonar critical issues
[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  * 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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.policy.test;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.onap.policy.api.AttributeType;
30 import org.onap.policy.api.PolicyChangeResponse;
31 import org.onap.policy.api.PolicyClass;
32 import org.onap.policy.api.PolicyEngine;
33 import org.onap.policy.api.PolicyEngineException;
34 import org.onap.policy.api.PolicyParameters;
35 import org.onap.policy.common.logging.flexlogger.FlexLogger;
36 import org.onap.policy.common.logging.flexlogger.Logger;
37 import org.onap.policy.std.StdPolicyChangeResponse;
38
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  */
48 public class DecisionPolicyApiTest extends TestCase {
49
50         private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
51
52         private PolicyEngine policyEngine = null;
53         private PolicyEngine mockPolicyEngine = null;
54         
55         PolicyChangeResponse result = null;
56         StdPolicyChangeResponse response = new StdPolicyChangeResponse();
57         PolicyParameters policyParameters = new PolicyParameters();
58
59         /**
60          * Perform pre-test initialization
61          *
62          * @throws Exception
63          *
64          * @see TestCase#setUp()
65          */
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         //policyParameters.setPolicyScope("test"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
82         
83         //Set the Component Attributes... These are Optional
84         Map<String, String> configAttributes = new HashMap<String, String>(); 
85         configAttributes.put("test", "testing");
86         
87         Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
88         attributes.put(AttributeType.MATCHING, configAttributes);
89         policyParameters.setAttributes(attributes);
90
91       //Set the settings... These are Optional
92 /*        Map<String, String> settingsMap = new HashMap<String, String>();
93         settingsMap.put("server", "5");
94          
95         Map<AttributeType, Map<String,String>> settings = new HashMap<AttributeType, Map<String, String>>();
96         settings.put(AttributeType.SETTINGS, settingsMap);
97         policyParameters.setSettings(settings);*/
98         
99         policyParameters.setRequestID(UUID.randomUUID());
100         }
101
102         /**
103          * Perform post-test clean up
104          *
105          * @throws Exception
106          *
107          * @see TestCase#tearDown()
108          */
109         public void tearDown() throws Exception {
110                 super.tearDown();
111                 // Add additional tear down code here
112         }
113
114         /**
115          * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
116          */
117         public void testCreatePolicy() {
118                 response.setResponseMessage("success");
119                 PolicyChangeResponse result = null;
120                 try {
121                 
122                         Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
123                         result = mockPolicyEngine.createPolicy(policyParameters);
124                         
125                 } catch (Exception e) {
126                         logger.warn(e.getMessage());
127                 }
128                 assertEquals(result, response);
129         }
130
131         /**
132          * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
133          */
134         public void testUpdatePolicy() {
135                 response.setResponseMessage("success");
136                 PolicyChangeResponse result = null;
137                 try {
138                 
139                         Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
140                         result = mockPolicyEngine.updatePolicy(policyParameters);
141                         
142                 } catch (Exception e) {
143                         logger.warn(e.getMessage());
144                 }
145                 assertEquals(result, response);
146         }
147         
148         @Test
149         public final void testCreatePolicyNullPolicyName() {
150                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
151                 policyParameters.setPolicyName(null);
152                 try{
153                         result = policyEngine.createPolicy(policyParameters);
154                 } catch (Exception e){
155                         logger.warn(e.getMessage());
156                 }
157                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
158         }
159         
160         @Test
161         public final void testCreatePolicyNullPolicyScope() {
162                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
163                 policyParameters.setPolicyName("test");
164                 try{
165                         result = policyEngine.createPolicy(policyParameters);
166                 } catch (Exception e){
167                         logger.warn(e.getMessage());
168                 }
169                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
170         }
171         
172         @Test
173         public final void testCreatePolicyNullOnapName() {
174                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
175                 policyParameters.setOnapName(null);
176                 try{
177                         result = policyEngine.createPolicy(policyParameters);
178                 } catch (Exception e){
179                         logger.warn(e.getMessage());
180                 }
181                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
182         }
183         
184         @Test
185         public final void testUpdatePolicyNullPolicyName() {
186                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
187                 policyParameters.setPolicyName(null);
188                 try{
189                         result = policyEngine.updatePolicy(policyParameters);
190                 } catch (Exception e){
191                         logger.warn(e.getMessage());
192                 }
193                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
194         }
195         
196         @Test
197         public final void testUpdatePolicyNullPolicyScope() {
198                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
199                 policyParameters.setPolicyName("test");
200                 try{
201                         result = policyEngine.updatePolicy(policyParameters);
202                 } catch (Exception e){
203                         logger.warn(e.getMessage());
204                 }
205                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
206         }
207         
208         @Test
209         public final void testUpdatePolicyNullOnapName() {
210                 response.setResponseMessage("PE500 - Process Flow Issue: :500:");
211                 policyParameters.setOnapName(null);
212                 try{
213                         result = policyEngine.updatePolicy(policyParameters);
214                 } catch (Exception e){
215                         logger.warn(e.getMessage());
216                 }
217                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
218         }
219 }