2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.policy.test;
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;
28 import java.util.Collection;
29 import java.util.HashMap;
32 import org.junit.Before;
33 import org.openecomp.policy.api.PolicyEngine;
34 import org.openecomp.policy.api.PolicyEngineException;
35 import org.openecomp.policy.api.PolicyEventException;
36 import org.openecomp.policy.api.PolicyResponse;
37 import org.openecomp.policy.api.PolicyResponseStatus;
38 import org.openecomp.policy.common.logging.flexlogger.FlexLogger;
39 import org.openecomp.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);
50 policyEngine = new PolicyEngine("Test/config_pass.properties");
51 } catch (PolicyEngineException e) {
52 logger.error(e.getMessage());
53 fail("PolicyEngine Instantiation Error" + e);
55 logger.info("Loaded.. PolicyEngine");
59 @SuppressWarnings("deprecation")
60 public void testSendEventFail() {
61 eventAttributes = null;
63 policyResponse = policyEngine.sendEvent(eventAttributes);
64 } catch (PolicyEventException e) {
65 logger.warn(e.getMessage());
67 assertNull(policyResponse);
71 @SuppressWarnings("deprecation")
72 public void testSendEventFailNull() {
73 eventAttributes.put("", "");
75 policyResponse = policyEngine.sendEvent(eventAttributes);
76 } catch (PolicyEventException e) {
77 logger.warn(e.getMessage());
79 assertNull(policyResponse);
84 public void testSendEventFailAttribute() {
85 eventAttributes.put("Fail.key", "Value");
87 policyResponse = policyEngine.sendEvent(eventAttributes);
88 } catch (PolicyEventException e) {
89 logger.warn(e.getMessage());
91 assertNull(policyResponse.getPolicyResponseMessage());
95 @SuppressWarnings("deprecation")
96 public void testSendEventNotValid() {
97 eventAttributes.put("Action.fail", "Value");
99 policyResponse = policyEngine.sendEvent(eventAttributes);
100 } catch (PolicyEventException e) {
101 logger.warn(e.getMessage());
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());
115 @SuppressWarnings("deprecation")
116 public void testSendEventActionAdvised() {
117 eventAttributes.put("Key", "Value");
118 eventAttributes.put("cpu", "80");
120 policyResponse = policyEngine.sendEvent(eventAttributes);
121 } catch (PolicyEventException e) {
122 logger.warn(e.getMessage());
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());
136 @SuppressWarnings("deprecation")
137 public void testSendEventActionTaken() {
138 eventAttributes.put("Key", "Value");
139 eventAttributes.put("cpu", "91");
141 policyResponse = policyEngine.sendEvent(eventAttributes);
142 } catch (PolicyEventException e) {
143 logger.warn(e.getMessage());
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());