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