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.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;
42 import org.openecomp.policy.common.logging.flexlogger.*;
44 public class SendEventTest {
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);
53 policyEngine = new PolicyEngine("Test/config_pass.properties");
54 } catch (PolicyEngineException e) {
55 logger.error(e.getMessage());
56 fail("PolicyEngine Instantiation Error" + e);
58 logger.info("Loaded.. PolicyEngine");
62 public void testSendEventFail() {
63 eventAttributes = null;
65 policyResponse = policyEngine.sendEvent(eventAttributes);
66 } catch (PolicyEventException e) {
67 logger.warn(e.getMessage());
69 assertNull(policyResponse);
73 public void testSendEventFailNull() {
74 eventAttributes.put("", "");
76 policyResponse = policyEngine.sendEvent(eventAttributes);
77 } catch (PolicyEventException e) {
78 logger.warn(e.getMessage());
80 assertNull(policyResponse);
85 public void testSendEventFailAttribute() {
86 eventAttributes.put("Fail.key", "Value");
88 policyResponse = policyEngine.sendEvent(eventAttributes);
89 } catch (PolicyEventException e) {
90 logger.warn(e.getMessage());
92 assertNull(policyResponse.getPolicyResponseMessage());
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 public void testSendEventActionAdvised() {
116 eventAttributes.put("Key", "Value");
117 eventAttributes.put("cpu", "80");
119 policyResponse = policyEngine.sendEvent(eventAttributes);
120 } catch (PolicyEventException e) {
121 logger.warn(e.getMessage());
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());
135 public void testSendEventActionTaken() {
136 eventAttributes.put("Key", "Value");
137 eventAttributes.put("cpu", "91");
139 policyResponse = policyEngine.sendEvent(eventAttributes);
140 } catch (PolicyEventException e) {
141 logger.warn(e.getMessage());
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());