2 * ============LICENSE_START=======================================================
3 * feature-controller-logging
4 * ================================================================================
5 * Copyright (C) 2019 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.onap.policy.drools.controller.logging;
23 import static org.junit.Assert.assertEquals;
25 import ch.qos.logback.classic.spi.LoggingEvent;
26 import ch.qos.logback.core.AppenderBase;
27 import com.google.gson.Gson;
28 import com.google.gson.GsonBuilder;
29 import java.io.IOException;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 import java.util.Properties;
35 import org.junit.After;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.common.endpoints.event.comm.Topic;
39 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
40 import org.onap.policy.common.endpoints.event.comm.bus.NoopTopicSink;
41 import org.onap.policy.drools.controller.DroolsController;
42 import org.onap.policy.drools.controller.DroolsControllerConstants;
43 import org.onap.policy.drools.properties.DroolsPropertyConstants;
44 import org.onap.policy.drools.protocol.configuration.ControllerConfiguration;
45 import org.onap.policy.drools.protocol.configuration.PdpdConfiguration;
46 import org.onap.policy.drools.system.PolicyController;
47 import org.onap.policy.drools.system.PolicyEngineConstants;
48 import org.onap.policy.drools.util.KieUtils;
51 * Controller Logger Tests.
53 public class ControllerLoggingTest {
56 * These properties are for installing a test artifact that the drools controller can
57 * fetch while testing.
59 private static final String JUNIT_KMODULE_DRL_PATH = "src/test/resources/test.drl";
60 private static final String JUNIT_KMODULE_POM_PATH = "src/test/resources/test.pom";
61 private static final String JUNIT_KMODULE_PATH = "src/test/resources/kmodule.xml";
62 private static final String JUNIT_KJAR_DRL_PATH = "src/main/resources/org/onap/policy/drools/test/";
65 * These properties are used for the Policy Controller to point to the test artifact.
67 private static final String TEST_CONTROLLER_NAME = "test-controller";
68 private static final String TEST_GROUP_ID = "org.onap.policy.drools.test";
69 private static final String TEST_ARTIFACT_ID = "test";
70 private static final String TEST_VERSION = "1.0.0";
73 * A test topic used for delivery and network logging.
75 private static final String TEST_TOPIC = "test-topic";
76 private static final String TEST_SERVER = "http://test.com";
79 * These are used for sending PDPD configuration notifications to a policy controller.
81 private static Properties controllerProps = null;
82 private static String message = null;
83 private static PdpdConfiguration pdpdNotification = null;
84 private static PolicyController policyController = null;
87 * This is a list of events that are appended to the controller-test logger.
89 private static List<LoggingEvent> events = new ArrayList<>();
92 * A custom appender used to intercept events and add them to a list of events that
93 * the junits can use to determine logging was successful.
95 public static class NetworkAppender extends AppenderBase<LoggingEvent> {
98 protected void append(LoggingEvent event) {
105 * Runs before all the test cases to install the drools artifact, create a policy
106 * controller, and create a PDPD configuration notification.
109 public static void setUp() throws IOException {
110 KieUtils.installArtifact(Paths.get(JUNIT_KMODULE_PATH).toFile(), Paths.get(JUNIT_KMODULE_POM_PATH).toFile(),
111 JUNIT_KJAR_DRL_PATH, Paths.get(JUNIT_KMODULE_DRL_PATH).toFile());
113 controllerProps = new Properties();
114 controllerProps.put(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
115 controllerProps.put(DroolsPropertyConstants.RULES_GROUPID, TEST_GROUP_ID);
116 controllerProps.put(DroolsPropertyConstants.RULES_ARTIFACTID, TEST_ARTIFACT_ID);
117 controllerProps.put(DroolsPropertyConstants.RULES_VERSION, TEST_VERSION);
119 policyController = PolicyEngineConstants.getManager().createPolicyController(TEST_CONTROLLER_NAME,
122 message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\",\"controllers\":"
123 + "[{\"name\":\"test-controller\",\"drools\":{\"groupId\":\"org.onap.policy.drools.test\","
124 + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}";
126 Gson decoder = new GsonBuilder().disableHtmlEscaping().create();
127 pdpdNotification = decoder.fromJson(message, PdpdConfiguration.class);
131 * Runs after every test case to clean up the events added to the event list during
135 public void cleanUpLogs() {
140 * Obtains the sequence number of the controller logging feature. This should return
144 public void getSequenceNumberTest() {
145 ControllerLoggingFeature nlf = new ControllerLoggingFeature();
146 assertEquals(1000, nlf.getSequenceNumber());
150 * Asserts that the controller-test logger appends the incoming message to the event
154 public void beforeOffer() {
155 ControllerLoggingFeature nlf = new ControllerLoggingFeature();
157 nlf.beforeOffer(policyController, Topic.CommInfrastructure.UEB, TEST_TOPIC, "{\"test\":\"test\"}");
159 assertEquals(1, events.size());
163 * Asserts that the controller-test logger appends the outgoing message to the event
167 public void afterDeliverSuccess() {
169 final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
171 DroolsController droolsController =
172 DroolsControllerConstants.getFactory().get(TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION);
174 NoopTopicSink sinkTopic = new NoopTopicSink(Arrays.asList(TEST_SERVER), TEST_TOPIC);
176 nlf.afterDeliver(droolsController, sinkTopic, null, "{\"test\":\"test\"}", true);
178 assertEquals(1, events.size());
183 * Asserts that the controller-test logger does not append the outgoing message to the
184 * event list if there was a failure.
187 public void afterDeliverFailure() {
189 final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
191 DroolsController droolsController =
192 DroolsControllerConstants.getFactory().get(TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION);
194 NoopTopicSink sinkTopic = new NoopTopicSink(Arrays.asList(TEST_SERVER), TEST_TOPIC);
196 nlf.afterDeliver(droolsController, sinkTopic, null, "{\"test\":\"test\"}", false);
198 assertEquals(0, events.size());
202 * Asserts that the controller logging feature can log the messages to the proper
203 * controller based on the message containing the controller name.
206 public void afterOnTopicEventSuccess() {
207 final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
209 nlf.afterOnTopicEvent(PolicyEngineConstants.getManager(), pdpdNotification, CommInfrastructure.UEB, TEST_TOPIC,
212 assertEquals(1, events.size());
216 * Asserts that the controller logging feature can skip logging messages that don't
217 * contain the controller names in it.
220 public void afterOnTopicEventFailure() {
221 final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
223 PdpdConfiguration notification = new PdpdConfiguration();
224 ControllerConfiguration config = new ControllerConfiguration();
225 config.setName("test-controller-2");
226 notification.setControllers(Arrays.asList(config));
228 nlf.afterOnTopicEvent(PolicyEngineConstants.getManager(), notification, CommInfrastructure.UEB, TEST_TOPIC,
231 assertEquals(0, events.size());