82cfbd9168313bc257d876327eba57ab1639c005
[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/test.drl";
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     /**
79      * These are used for sending PDPD configuration notifications to a policy controller.
80      */
81     private static Properties controllerProps = null;
82     private static String message = null;
83     private static PdpdConfiguration pdpdNotification = null;
84     private static PolicyController policyController = null;
85
86     /**
87      * This is a list of events that are appended to the controller-test logger.
88      */
89     private static List<LoggingEvent> events = new ArrayList<>();
90
91     /**
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.
94      */
95     public static class NetworkAppender extends AppenderBase<LoggingEvent> {
96
97         @Override
98         protected void append(LoggingEvent event) {
99             events.add(event);
100         }
101
102     }
103
104     /**
105      * Runs before all the test cases to install the drools artifact, create a policy
106      * controller, and create a PDPD configuration notification.
107      */
108     @BeforeClass
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());
112
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);
118
119         policyController = PolicyEngineConstants.getManager().createPolicyController(TEST_CONTROLLER_NAME,
120                         controllerProps);
121
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\"}]}";
125
126         Gson decoder = new GsonBuilder().disableHtmlEscaping().create();
127         pdpdNotification = decoder.fromJson(message, PdpdConfiguration.class);
128     }
129
130     /**
131      * Runs after every test case to clean up the events added to the event list during
132      * unit test.
133      */
134     @After
135     public void cleanUpLogs() {
136         events.clear();
137     }
138
139     /**
140      * Obtains the sequence number of the controller logging feature. This should return
141      * 1000.
142      */
143     @Test
144     public void getSequenceNumberTest() {
145         ControllerLoggingFeature nlf = new ControllerLoggingFeature();
146         assertEquals(1000, nlf.getSequenceNumber());
147     }
148
149     /**
150      * Asserts that the controller-test logger appends the incoming message to the event
151      * list.
152      */
153     @Test
154     public void beforeOffer() {
155         ControllerLoggingFeature nlf = new ControllerLoggingFeature();
156
157         nlf.beforeOffer(policyController, Topic.CommInfrastructure.UEB, TEST_TOPIC, "{\"test\":\"test\"}");
158
159         assertEquals(1, events.size());
160     }
161
162     /**
163      * Asserts that the controller-test logger appends the outgoing message to the event
164      * list.
165      */
166     @Test
167     public void afterDeliverSuccess() {
168
169         final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
170
171         DroolsController droolsController =
172                         DroolsControllerConstants.getFactory().get(TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION);
173
174         NoopTopicSink sinkTopic = new NoopTopicSink(Arrays.asList(TEST_SERVER), TEST_TOPIC);
175
176         nlf.afterDeliver(droolsController, sinkTopic, null, "{\"test\":\"test\"}", true);
177
178         assertEquals(1, events.size());
179
180     }
181
182     /**
183      * Asserts that the controller-test logger does not append the outgoing message to the
184      * event list if there was a failure.
185      */
186     @Test
187     public void afterDeliverFailure() {
188
189         final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
190
191         DroolsController droolsController =
192                         DroolsControllerConstants.getFactory().get(TEST_GROUP_ID, TEST_ARTIFACT_ID, TEST_VERSION);
193
194         NoopTopicSink sinkTopic = new NoopTopicSink(Arrays.asList(TEST_SERVER), TEST_TOPIC);
195
196         nlf.afterDeliver(droolsController, sinkTopic, null, "{\"test\":\"test\"}", false);
197
198         assertEquals(0, events.size());
199     }
200
201     /**
202      * Asserts that the controller logging feature can log the messages to the proper
203      * controller based on the message containing the controller name.
204      */
205     @Test
206     public void afterOnTopicEventSuccess() {
207         final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
208
209         nlf.afterOnTopicEvent(PolicyEngineConstants.getManager(), pdpdNotification, CommInfrastructure.UEB, TEST_TOPIC,
210                         message);
211
212         assertEquals(1, events.size());
213     }
214
215     /**
216      * Asserts that the controller logging feature can skip logging messages that don't
217      * contain the controller names in it.
218      */
219     @Test
220     public void afterOnTopicEventFailure() {
221         final ControllerLoggingFeature nlf = new ControllerLoggingFeature();
222
223         PdpdConfiguration notification = new PdpdConfiguration();
224         ControllerConfiguration config = new ControllerConfiguration();
225         config.setName("test-controller-2");
226         notification.setControllers(Arrays.asList(config));
227
228         nlf.afterOnTopicEvent(PolicyEngineConstants.getManager(), notification, CommInfrastructure.UEB, TEST_TOPIC,
229                         message);
230
231         assertEquals(0, events.size());
232     }
233 }