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