ecbb320d4c90767493dbddc0738a0ccc66ecf637
[policy/drools-pdp.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.controller.logging;
22
23 import static org.junit.Assert.assertEquals;
24
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;
49
50 /**
51  * Controller Logger Tests.
52  */
53 public class ControllerLoggingTest {
54
55     /**
56      * These properties are for installing a test artifact that the drools controller can
57      * fetch while testing.
58      */
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/";
63
64     /**
65      * These properties are used for the Policy Controller to point to the test artifact.
66      */
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";
71
72     /**
73      * A test topic used for delivery and network logging.
74      */
75     private static final String TEST_TOPIC = "test-topic";
76     private static final String TEST_SERVER = "http://test.com";
77
78     private static String message = null;
79     private static PdpdConfiguration pdpdNotification = null;
80     private static PolicyController policyController = null;
81
82     /**
83      * This is a list of events that are appended to the controller-test logger.
84      */
85     private static List<LoggingEvent> events = new ArrayList<>();
86
87     /**
88      * A custom appender used to intercept events and add them to a list of events that
89      * the junits can use to determine logging was successful.
90      */
91     public static class NetworkAppender extends AppenderBase<LoggingEvent> {
92
93         @Override
94         protected void append(LoggingEvent event) {
95             events.add(event);
96         }
97
98     }
99
100     /**
101      * Runs before all the test cases to install the drools artifact, create a policy
102      * controller, and create a PDPD configuration notification.
103      */
104     @BeforeClass
105     public static void setUp() throws IOException {
106         KieUtils.installArtifact(Paths.get(JUNIT_KMODULE_PATH).toFile(), Paths.get(JUNIT_KMODULE_POM_PATH).toFile(),
107                         JUNIT_KJAR_DRL_PATH, Paths.get(JUNIT_KMODULE_DRL_PATH).toFile());
108
109         // These properties are used for sending PDPD configuration notifications to a policy controller.
110         Properties controllerProps = new Properties();
111         controllerProps.put(DroolsPropertyConstants.PROPERTY_CONTROLLER_NAME, TEST_CONTROLLER_NAME);
112         controllerProps.put(DroolsPropertyConstants.RULES_GROUPID, TEST_GROUP_ID);
113         controllerProps.put(DroolsPropertyConstants.RULES_ARTIFACTID, TEST_ARTIFACT_ID);
114         controllerProps.put(DroolsPropertyConstants.RULES_VERSION, TEST_VERSION);
115
116         policyController =
117             PolicyEngineConstants.getManager().createPolicyController(TEST_CONTROLLER_NAME, controllerProps);
118
119         message = "{\"requestID\":\"38adde30-cc22-11e8-a8d5-f2801f1b9fd1\",\"entity\":\"controller\",\"controllers\":"
120                         + "[{\"name\":\"test-controller\",\"drools\":{\"groupId\":\"org.onap.policy.drools.test\","
121                         + "\"artifactId\":\"test\",\"version\":\"0.0.1\"},\"operation\":\"update\"}]}";
122
123         Gson decoder = new GsonBuilder().disableHtmlEscaping().create();
124         pdpdNotification = decoder.fromJson(message, PdpdConfiguration.class);
125     }
126
127     /**
128      * Runs after every test case to clean up the events added to the event list during
129      * unit test.
130      */
131     @After
132     public void cleanUpLogs() {
133         events.clear();
134     }
135
136     /**
137      * Obtains the sequence number of the controller logging feature. This should return
138      * 1000.
139      */
140     @Test
141     public void getSequenceNumberTest() {
142         ControllerLoggingFeature nlf = new ControllerLoggingFeature();
143         assertEquals(1000, nlf.getSequenceNumber());
144     }
145
146     /**
147      * Asserts that the controller-test logger appends the incoming message to the event
148      * list.
149      */
150     @Test
151     public void beforeOffer() {
152         ControllerLoggingFeature nlf = new ControllerLoggingFeature();
153
154         nlf.beforeOffer(policyController, Topic.CommInfrastructure.UEB, TEST_TOPIC, "{\"test\":\"test\"}");
155
156         assertEquals(1, events.size());
157     }
158
159     /**
160      * Asserts that the controller-test logger appends the outgoing message to the event
161      * list.
162      */
163     @Test
164     public void afterDeliverSuccess() {
165
166         final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
167
168         DroolsController droolsController =
169                         DroolsControllerConstants.getFactory().get(TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION);
170
171         NoopTopicSink sinkTopic = new NoopTopicSink(Arrays.asList(TEST_SERVER), TEST_TOPIC);
172
173         nlf.afterDeliver(droolsController, sinkTopic, null, "{\"test\":\"test\"}", true);
174
175         assertEquals(1, events.size());
176
177     }
178
179     /**
180      * Asserts that the controller-test logger does not append the outgoing message to the
181      * event list if there was a failure.
182      */
183     @Test
184     public void afterDeliverFailure() {
185
186         final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
187
188         DroolsController droolsController =
189                         DroolsControllerConstants.getFactory().get(TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION);
190
191         NoopTopicSink sinkTopic = new NoopTopicSink(Arrays.asList(TEST_SERVER), TEST_TOPIC);
192
193         nlf.afterDeliver(droolsController, sinkTopic, null, "{\"test\":\"test\"}", false);
194
195         assertEquals(0, events.size());
196     }
197
198     /**
199      * Asserts that the controller logging feature can log the messages to the proper
200      * controller based on the message containing the controller name.
201      */
202     @Test
203     public void afterOnTopicEventSuccess() {
204         final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
205
206         nlf.afterOnTopicEvent(PolicyEngineConstants.getManager(), pdpdNotification, CommInfrastructure.UEB, TEST_TOPIC,
207                         message);
208
209         assertEquals(1, events.size());
210     }
211
212     /**
213      * Asserts that the controller logging feature can skip logging messages that don't
214      * contain the controller names in it.
215      */
216     @Test
217     public void afterOnTopicEventFailure() {
218         final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
219
220         PdpdConfiguration notification = new PdpdConfiguration();
221         ControllerConfiguration config = new ControllerConfiguration();
222         config.setName("test-controller-2");
223         notification.setControllers(Arrays.asList(config));
224
225         nlf.afterOnTopicEvent(PolicyEngineConstants.getManager(), notification, CommInfrastructure.UEB, TEST_TOPIC,
226                         message);
227
228         assertEquals(0, events.size());
229     }
230 }