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