Initial OpenECOMP policy/engine commit
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / openecomp / 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.openecomp.policy.test;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.UUID;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.junit.Test;
30 import org.mockito.Mockito;
31 import org.openecomp.policy.api.AttributeType;
32 import org.openecomp.policy.api.PolicyChangeResponse;
33 import org.openecomp.policy.api.PolicyClass;
34 import org.openecomp.policy.api.PolicyEngine;
35 import org.openecomp.policy.api.PolicyEngineException;
36 import org.openecomp.policy.api.PolicyParameters;
37 import org.openecomp.policy.std.StdPolicyChangeResponse;
38
39 import org.openecomp.policy.common.logging.flexlogger.*; 
40
41 import junit.framework.TestCase;
42
43 /**
44  * The class <code>DecisionPolicyApiTest</code> contains tests for the class
45  * {@link <code>PolicyEngine</code>}
46  *
47  * @pattern JUnit Test Case
48  * *
49  */
50 public class DecisionPolicyApiTest extends TestCase {
51
52         private static final Logger logger = FlexLogger.getLogger(DecisionPolicyApiTest.class);
53
54         private PolicyEngine policyEngine = null;
55         private PolicyEngine mockPolicyEngine = null;
56         
57         PolicyChangeResponse result = null;
58         StdPolicyChangeResponse response = new StdPolicyChangeResponse();
59         PolicyParameters policyParameters = new PolicyParameters();
60
61         /**
62          * Perform pre-test initialization
63          *
64          * @throws Exception
65          *
66          * @see TestCase#setUp()
67          */
68         protected void setUp() throws Exception {
69                 try {
70                         policyEngine = new PolicyEngine("Test/config_pass.properties");
71                 } catch (PolicyEngineException e) {
72                         logger.error(e.getMessage());
73                         fail("PolicyEngine Instantiation Error" + e);
74                 }
75                 logger.info("Loaded.. PolicyEngine");
76                 
77                 mockPolicyEngine = Mockito.mock(PolicyEngine.class);
78
79         policyParameters.setPolicyClass(PolicyClass.Decision); //required
80         policyParameters.setPolicyName("test.junitTest"); //required
81         policyParameters.setEcompName("test");
82         policyParameters.setPolicyDescription("testing");  //optional
83         //policyParameters.setPolicyScope("test"); //Directory will be created where the Policies are saved... this displays a a subscope on the GUI
84         
85         //Set the Component Attributes... These are Optional
86         Map<String, String> configAttributes = new HashMap<String, String>(); 
87         configAttributes.put("test", "testing");
88         
89         Map<AttributeType, Map<String,String>> attributes = new HashMap<AttributeType, Map<String,String>>();
90         attributes.put(AttributeType.MATCHING, configAttributes);
91         policyParameters.setAttributes(attributes);
92
93       //Set the settings... These are Optional
94 /*        Map<String, String> settingsMap = new HashMap<String, String>();
95         settingsMap.put("server", "5");
96          
97         Map<AttributeType, Map<String,String>> settings = new HashMap<AttributeType, Map<String, String>>();
98         settings.put(AttributeType.SETTINGS, settingsMap);
99         policyParameters.setSettings(settings);*/
100         
101         policyParameters.setRequestID(UUID.randomUUID());
102         }
103
104         /**
105          * Perform post-test clean up
106          *
107          * @throws Exception
108          *
109          * @see TestCase#tearDown()
110          */
111         protected void tearDown() throws Exception {
112                 super.tearDown();
113                 // Add additional tear down code here
114         }
115
116         /**
117          * Run the PolicyChangeResponse createPolicy(PolicyParameters) method test
118          */
119         public void testCreatePolicy() {
120                 response.setResponseMessage("success");
121                 PolicyChangeResponse result = null;
122                 try {
123                 
124                         Mockito.when(mockPolicyEngine.createPolicy(policyParameters)).thenReturn(response);
125                         result = mockPolicyEngine.createPolicy(policyParameters);
126                         
127                 } catch (Exception e) {
128                         logger.warn(e.getMessage());
129                 }
130                 assertEquals(result, response);
131         }
132
133         /**
134          * Run the PolicyChangeResponse updatePolicy(PolicyParameters) method test
135          */
136         public void testUpdatePolicy() {
137                 response.setResponseMessage("success");
138                 PolicyChangeResponse result = null;
139                 try {
140                 
141                         Mockito.when(mockPolicyEngine.updatePolicy(policyParameters)).thenReturn(response);
142                         result = mockPolicyEngine.updatePolicy(policyParameters);
143                         
144                 } catch (Exception e) {
145                         logger.warn(e.getMessage());
146                 }
147                 assertEquals(result, response);
148         }
149         
150         @Test
151         public final void testCreatePolicyNullPolicyName() {
152                 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
153                 policyParameters.setPolicyName(null);
154                 try{
155                         result = policyEngine.createPolicy(policyParameters);
156                 } catch (Exception e){
157                         logger.warn(e.getMessage());
158                 }
159                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
160         }
161         
162         @Test
163         public final void testCreatePolicyNullPolicyScope() {
164                 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
165                 policyParameters.setPolicyName("test");
166                 try{
167                         result = policyEngine.createPolicy(policyParameters);
168                 } catch (Exception e){
169                         logger.warn(e.getMessage());
170                 }
171                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
172         }
173         
174         @Test
175         public final void testCreatePolicyNullEcompName() {
176                 response.setResponseMessage("PE300 - Data Issue: No ECOMP Name given.");
177                 policyParameters.setEcompName(null);
178                 try{
179                         result = policyEngine.createPolicy(policyParameters);
180                 } catch (Exception e){
181                         logger.warn(e.getMessage());
182                 }
183                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
184         }
185         
186         @Test
187         public final void testUpdatePolicyNullPolicyName() {
188                 response.setResponseMessage("PE300 - Data Issue: No Policy Name given.");
189                 policyParameters.setPolicyName(null);
190                 try{
191                         result = policyEngine.updatePolicy(policyParameters);
192                 } catch (Exception e){
193                         logger.warn(e.getMessage());
194                 }
195                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
196         }
197         
198         @Test
199         public final void testUpdatePolicyNullPolicyScope() {
200                 response.setResponseMessage("PE300 - Data Issue: No Policy Scope given.");
201                 policyParameters.setPolicyName("test");
202                 try{
203                         result = policyEngine.updatePolicy(policyParameters);
204                 } catch (Exception e){
205                         logger.warn(e.getMessage());
206                 }
207                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
208         }
209         
210         @Test
211         public final void testUpdatePolicyNullEcompName() {
212                 response.setResponseMessage("PE300 - Data Issue: No ECOMP Name given.");
213                 policyParameters.setEcompName(null);
214                 try{
215                         result = policyEngine.updatePolicy(policyParameters);
216                 } catch (Exception e){
217                         logger.warn(e.getMessage());
218                 }
219                 assertEquals(result.getResponseMessage(), response.getResponseMessage());
220         }
221 }