2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.test;
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;
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;
41 public class SendEventTest {
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);
51 policyEngine = new PolicyEngine("Test/config_pass.properties");
52 } catch (PolicyEngineException e) {
53 logger.error(e.getMessage());
54 fail("PolicyEngine Instantiation Error" + e);
56 logger.info("Loaded.. PolicyEngine");
60 @SuppressWarnings("deprecation")
61 public void testSendEventFail() {
62 eventAttributes = null;
64 policyResponse = policyEngine.sendEvent(eventAttributes);
65 } catch (PolicyEventException e) {
66 logger.warn(e.getMessage());
68 assertNull(policyResponse);
72 @SuppressWarnings("deprecation")
73 public void testSendEventFailNull() {
74 eventAttributes.put("", "");
76 policyResponse = policyEngine.sendEvent(eventAttributes);
77 } catch (PolicyEventException e) {
78 logger.warn(e.getMessage());
80 assertNull(policyResponse);
84 @SuppressWarnings("deprecation")
85 public void testSendEventNotValid() {
86 eventAttributes.put("Action.fail", "Value");
88 policyResponse = policyEngine.sendEvent(eventAttributes);
89 } catch (PolicyEventException e) {
90 logger.warn(e.getMessage());
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());
106 @SuppressWarnings("deprecation")
107 public void testSendEventActionAdvised() {
108 eventAttributes.put("Key", "Value");
109 eventAttributes.put("cpu", "80");
111 policyResponse = policyEngine.sendEvent(eventAttributes);
112 } catch (PolicyEventException e) {
113 logger.warn(e.getMessage());
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());
129 @SuppressWarnings("deprecation")
130 public void testSendEventActionTaken() {
131 eventAttributes.put("Key", "Value");
132 eventAttributes.put("cpu", "91");
134 policyResponse = policyEngine.sendEvent(eventAttributes);
135 } catch (PolicyEventException e) {
136 logger.warn(e.getMessage());
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());