Reformat PolicyEngineAPI test cases
[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  * 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 static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.fail;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.Map;
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
48     @Before
49     public void setUp() {
50         try {
51             policyEngine = new PolicyEngine("Test/config_pass.properties");
52         } catch (PolicyEngineException e) {
53             logger.error(e.getMessage());
54             fail("PolicyEngine Instantiation Error" + e);
55         }
56         logger.info("Loaded.. PolicyEngine");
57     }
58
59     // @Test
60     @SuppressWarnings("deprecation")
61     public void testSendEventFail() {
62         eventAttributes = null;
63         try {
64             policyResponse = policyEngine.sendEvent(eventAttributes);
65         } catch (PolicyEventException e) {
66             logger.warn(e.getMessage());
67         }
68         assertNull(policyResponse);
69     }
70
71     // @Test
72     @SuppressWarnings("deprecation")
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     // @Test
84     @SuppressWarnings("deprecation")
85     public void testSendEventNotValid() {
86         eventAttributes.put("Action.fail", "Value");
87         try {
88             policyResponse = policyEngine.sendEvent(eventAttributes);
89         } catch (PolicyEventException e) {
90             logger.warn(e.getMessage());
91         }
92         for (PolicyResponse policyResponse : this.policyResponse) {
93             logger.info(policyResponse.getPolicyResponseMessage() + " , "
94                     + policyResponse.getPolicyResponseStatus());
95             assertNotNull(policyResponse);
96             assertEquals(PolicyResponseStatus.NO_ACTION_REQUIRED,
97                     policyResponse.getPolicyResponseStatus());
98             assertNotNull(policyResponse.getPolicyResponseMessage());
99             assertNotNull(policyResponse.getRequestAttributes());
100             assertNull(policyResponse.getActionTaken());
101             assertNull(policyResponse.getActionAdvised());
102         }
103     }
104
105     // @Test
106     @SuppressWarnings("deprecation")
107     public void testSendEventActionAdvised() {
108         eventAttributes.put("Key", "Value");
109         eventAttributes.put("cpu", "80");
110         try {
111             policyResponse = policyEngine.sendEvent(eventAttributes);
112         } catch (PolicyEventException e) {
113             logger.warn(e.getMessage());
114         }
115         for (PolicyResponse policyResponse : this.policyResponse) {
116             logger.info(policyResponse.getPolicyResponseMessage() + " , "
117                     + policyResponse.getPolicyResponseStatus());
118             assertNotNull(policyResponse);
119             assertEquals(PolicyResponseStatus.ACTION_ADVISED,
120                     policyResponse.getPolicyResponseStatus());
121             assertNotNull(policyResponse.getPolicyResponseMessage());
122             assertNotNull(policyResponse.getRequestAttributes());
123             assertNull(policyResponse.getActionTaken());
124             assertNotNull(policyResponse.getActionAdvised());
125         }
126     }
127
128     // @Test
129     @SuppressWarnings("deprecation")
130     public void testSendEventActionTaken() {
131         eventAttributes.put("Key", "Value");
132         eventAttributes.put("cpu", "91");
133         try {
134             policyResponse = policyEngine.sendEvent(eventAttributes);
135         } catch (PolicyEventException e) {
136             logger.warn(e.getMessage());
137         }
138         for (PolicyResponse policyResponse : this.policyResponse) {
139             logger.info(policyResponse.getPolicyResponseMessage() + " , "
140                     + policyResponse.getPolicyResponseStatus());
141             assertNotNull(policyResponse);
142             assertEquals(PolicyResponseStatus.ACTION_TAKEN,
143                     policyResponse.getPolicyResponseStatus());
144             assertNotNull(policyResponse.getPolicyResponseMessage());
145             assertNotNull(policyResponse.getRequestAttributes());
146             assertNotNull(policyResponse.getActionTaken());
147             assertNull(policyResponse.getActionAdvised());
148         }
149     }
150 }