[POLICY-73] replace openecomp for policy-engine
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / test / SendEventTest.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 static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.fail;
27
28 import java.util.Collection;
29 import java.util.HashMap;
30 import java.util.Map;
31
32 import org.junit.Before;
33 import org.onap.policy.api.PolicyEngine;
34 import org.onap.policy.api.PolicyEngineException;
35 import org.onap.policy.api.PolicyEventException;
36 import org.onap.policy.api.PolicyResponse;
37 import org.onap.policy.api.PolicyResponseStatus;
38 import org.onap.policy.common.logging.flexlogger.FlexLogger;
39 import org.onap.policy.common.logging.flexlogger.Logger; 
40
41 public class SendEventTest {
42         
43         private PolicyEngine policyEngine = null;
44         private Map<String,String> eventAttributes = new HashMap<String,String>();
45         private Collection<PolicyResponse> policyResponse = null;
46         private static final Logger logger = FlexLogger.getLogger(SendEventTest.class);
47         @Before
48         public void setUp() {
49                 try {
50                         policyEngine = new PolicyEngine("Test/config_pass.properties");
51                 } catch (PolicyEngineException e) {
52                         logger.error(e.getMessage());
53                         fail("PolicyEngine Instantiation Error" + e);
54                 }
55                 logger.info("Loaded.. PolicyEngine");
56         }
57
58         //@Test
59         @SuppressWarnings("deprecation")
60         public void testSendEventFail() {
61                 eventAttributes = null;
62                 try {
63                         policyResponse = policyEngine.sendEvent(eventAttributes);
64                 } catch (PolicyEventException e) {
65                         logger.warn(e.getMessage());
66                 }
67                 assertNull(policyResponse);
68         }
69         
70         //@Test
71         @SuppressWarnings("deprecation")
72         public void testSendEventFailNull() {
73                 eventAttributes.put("", "");
74                 try {
75                         policyResponse = policyEngine.sendEvent(eventAttributes);
76                 } catch (PolicyEventException e) {
77                         logger.warn(e.getMessage());
78                 }
79                 assertNull(policyResponse);
80         }
81         
82         // deprecated Test. 
83         /*@Test
84         public void testSendEventFailAttribute() {
85                 eventAttributes.put("Fail.key", "Value");
86                 try {
87                         policyResponse = policyEngine.sendEvent(eventAttributes);
88                 } catch (PolicyEventException e) {
89                         logger.warn(e.getMessage());
90                 }
91                 assertNull(policyResponse.getPolicyResponseMessage());
92         }*/
93         
94         //@Test
95         @SuppressWarnings("deprecation")
96         public void testSendEventNotValid() {
97                 eventAttributes.put("Action.fail", "Value");
98                 try {
99                         policyResponse = policyEngine.sendEvent(eventAttributes);
100                 } catch (PolicyEventException e) {
101                         logger.warn(e.getMessage());
102                 }
103                 for(PolicyResponse policyResponse: this.policyResponse){
104                         logger.info(policyResponse.getPolicyResponseMessage() + " , " + policyResponse.getPolicyResponseStatus());
105                         assertNotNull(policyResponse);
106                         assertEquals(PolicyResponseStatus.NO_ACTION_REQUIRED, policyResponse.getPolicyResponseStatus());
107                         assertNotNull(policyResponse.getPolicyResponseMessage());
108                         assertNotNull(policyResponse.getRequestAttributes());
109                         assertNull(policyResponse.getActionTaken());
110                         assertNull(policyResponse.getActionAdvised());
111                 }
112         }
113         
114         //@Test
115         @SuppressWarnings("deprecation")
116         public void testSendEventActionAdvised() {
117                 eventAttributes.put("Key", "Value");
118                 eventAttributes.put("cpu", "80");
119                 try {
120                         policyResponse = policyEngine.sendEvent(eventAttributes);
121                 } catch (PolicyEventException e) {
122                         logger.warn(e.getMessage());
123                 }
124                 for(PolicyResponse policyResponse: this.policyResponse){
125                         logger.info(policyResponse.getPolicyResponseMessage() + " , " + policyResponse.getPolicyResponseStatus());
126                         assertNotNull(policyResponse);
127                         assertEquals(PolicyResponseStatus.ACTION_ADVISED, policyResponse.getPolicyResponseStatus());
128                         assertNotNull(policyResponse.getPolicyResponseMessage());
129                         assertNotNull(policyResponse.getRequestAttributes());
130                         assertNull(policyResponse.getActionTaken());
131                         assertNotNull(policyResponse.getActionAdvised());
132                 }
133         }
134         
135         //@Test
136         @SuppressWarnings("deprecation")
137         public void testSendEventActionTaken() {
138                 eventAttributes.put("Key", "Value");
139                 eventAttributes.put("cpu", "91");
140                 try {
141                         policyResponse = policyEngine.sendEvent(eventAttributes);
142                 } catch (PolicyEventException e) {
143                         logger.warn(e.getMessage());
144                 }
145                 for(PolicyResponse policyResponse: this.policyResponse){
146                         logger.info(policyResponse.getPolicyResponseMessage() + " , " + policyResponse.getPolicyResponseStatus());
147                         assertNotNull(policyResponse);
148                         assertEquals(PolicyResponseStatus.ACTION_TAKEN, policyResponse.getPolicyResponseStatus());
149                         assertNotNull(policyResponse.getPolicyResponseMessage());
150                         assertNotNull(policyResponse.getRequestAttributes());
151                         assertNotNull(policyResponse.getActionTaken());
152                         assertNull(policyResponse.getActionAdvised());
153                 }
154         }
155 }