abf9b3872d8243de94e7e02d9a77e6e562b98a8d
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018-2020 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.protocol.coders;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import java.io.IOException;
26 import java.nio.file.Paths;
27 import java.util.List;
28 import java.util.Properties;
29 import lombok.AllArgsConstructor;
30 import lombok.Getter;
31 import org.junit.After;
32 import org.junit.Assert;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.kie.api.builder.ReleaseId;
37 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
38 import org.onap.policy.common.endpoints.event.comm.TopicSink;
39 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
40 import org.onap.policy.drools.controller.DroolsController;
41 import org.onap.policy.drools.controller.DroolsControllerConstants;
42 import org.onap.policy.drools.controller.internal.MavenDroolsControllerTest;
43 import org.onap.policy.drools.properties.DroolsPropertyConstants;
44 import org.onap.policy.drools.protocol.coders.EventProtocolCoder.CoderFilters;
45 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder;
46 import org.onap.policy.drools.util.KieUtils;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * ProtocolCoder Toolset Junits.
52  */
53 public class ProtocolCoderToolsetTest {
54     private static final String JUNIT_PROTOCOL_CODER_ARTIFACT_ID = "protocolcoder";
55     private static final String JUNIT_PROTOCOL_CODER_TOPIC = JUNIT_PROTOCOL_CODER_ARTIFACT_ID;
56     private static final String CONTROLLER_ID = "blah";
57
58     private static Logger logger = LoggerFactory.getLogger(ProtocolCoderToolset.class);
59
60     private static volatile ReleaseId releaseId;
61
62     // customCoder has to be public to be accessed in tests below
63     public static final Gson customCoder = new GsonBuilder().create();
64
65     private DroolsController controller;
66
67     /**
68      * Test Class Initialization.
69      */
70     @BeforeClass
71     public static void setUpClass() throws IOException {
72         releaseId = KieUtils.installArtifact(Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_PATH).toFile(),
73                         Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_POM_PATH).toFile(),
74                         MavenDroolsControllerTest.JUNIT_ECHO_KJAR_DRL_PATH,
75                         Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_DRL_PATH).toFile());
76     }
77
78     /**
79      * Test Set Up.
80      */
81     @Before
82     public void setUp() {
83         controller = createController();
84     }
85
86     /**
87      * Test Termination.
88      */
89     @After
90     public void tearDown() {
91         if (controller != null) {
92             DroolsControllerConstants.getFactory().destroy(controller);
93         }
94     }
95
96     @Test
97     public void testToolsets() {
98         testGsonToolset(createFilterSet());
99     }
100
101     /**
102      * Test the Gson toolset.
103      *
104      * @param protocolFilter protocol filter
105      */
106     private void testGsonToolset(JsonProtocolFilter protocolFilter) {
107         GsonProtocolCoderToolset gsonToolset =
108                         new GsonProtocolCoderToolset(EventProtocolParams.builder().topic(JUNIT_PROTOCOL_CODER_TOPIC)
109                                         .groupId(releaseId.getGroupId()).artifactId(releaseId.getArtifactId())
110                                         .eventClass(ThreeStrings.class.getName()).protocolFilter(protocolFilter)
111                                         .customGsonCoder(null).modelClassLoaderHash(12345678), CONTROLLER_ID);
112
113         Assert.assertNotNull(gsonToolset.getEncoder());
114         Assert.assertNotNull(gsonToolset.getDecoder());
115
116         testToolset(protocolFilter, gsonToolset);
117
118         ThreeStrings triple = createTriple();
119         gsonToolset.setCustomCoder(new CustomGsonCoder(this.getClass().getName(), "customCoder"));
120         String tripleEncoded = encode(gsonToolset, triple);
121         decode(protocolFilter, gsonToolset, triple, tripleEncoded);
122     }
123
124     private ThreeStrings createTriple() {
125         return new ThreeStrings("v1", "v2", "v3");
126     }
127
128     private void testToolset(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset) {
129
130         validateInitialization(protocolFilter, coderToolset);
131
132         updateCoderFilterRule(coderToolset);
133
134         addRemoveCoder(coderToolset);
135
136         /* restore original filters */
137         coderToolset.addCoder(ThreeStrings.class.getName(), protocolFilter, 654321);
138
139         ThreeStrings triple = createTriple();
140
141         String tripleEncoded = encode(coderToolset, triple);
142
143         decode(protocolFilter, coderToolset, triple, tripleEncoded);
144     }
145
146     private void decode(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset,
147                     ThreeStrings triple, String tripleEncoded) {
148
149         ThreeStrings tripleDecoded = null;
150         try {
151             tripleDecoded = (ThreeStrings) coderToolset.decode(tripleEncoded);
152         } catch (UnsupportedOperationException e) {
153             /* OK */
154             logger.trace("Junit expected exception - decode does not pass filtering", e);
155         }
156
157         CoderFilters coderFilters = coderToolset.getCoder(ThreeStrings.class.getName());
158         Assert.assertSame(coderFilters.getCodedClass(), ThreeStrings.class.getName());
159         Assert.assertSame(coderFilters.getFilter(), protocolFilter);
160         Assert.assertNotNull(coderFilters.getFilter().getRule());
161
162         coderFilters.getFilter().setRule("[?($.second =~ /^v2$/ && $.third =~ /.*v3.*/)]");
163
164         tripleDecoded = (ThreeStrings) coderToolset.decode(tripleEncoded);
165
166         Assert.assertEquals(triple.getFirst(), tripleDecoded.getFirst());
167         Assert.assertEquals(triple.getSecond(), tripleDecoded.getSecond());
168         Assert.assertEquals(triple.getThird(), tripleDecoded.getThird());
169
170         coderFilters.getFilter().setRule(null);
171         Assert.assertEquals("[?($ =~ /.*/)]", coderFilters.getFilter().getRule());
172
173         tripleDecoded = (ThreeStrings) coderToolset.decode(tripleEncoded);
174
175         Assert.assertEquals(tripleDecoded.getFirst(), triple.getFirst());
176         Assert.assertEquals(tripleDecoded.getSecond(), triple.getSecond());
177         Assert.assertEquals(tripleDecoded.getThird(), triple.getThird());
178
179         coderFilters.getFilter().setRule("[?($.third =~ /.*v3.*/)]");
180     }
181
182     private String encode(ProtocolCoderToolset coderToolset, ThreeStrings triple) {
183         String tripleEncoded = coderToolset.encode(triple);
184         Assert.assertTrue(!tripleEncoded.isEmpty());
185         return tripleEncoded;
186     }
187
188     private void addRemoveCoder(ProtocolCoderToolset coderToolset) {
189         coderToolset.addCoder(this.getClass().getName(), new JsonProtocolFilter("[?($.second =~ /.*/)]"), 654321);
190         Assert.assertEquals(2, coderToolset.getCoders().size());
191
192         coderToolset.removeCoders(this.getClass().getName());
193         Assert.assertEquals(1, coderToolset.getCoders().size());
194     }
195
196     private void updateCoderFilterRule(ProtocolCoderToolset coderToolset) {
197         coderToolset.addCoder(ThreeStrings.class.getName(), new JsonProtocolFilter("[?($.third =~ /.*/)]"), 654321);
198
199         Assert.assertEquals(1, coderToolset.getCoders().size());
200
201         Assert.assertEquals(654321, coderToolset.getCoder(ThreeStrings.class.getName()).getModelClassLoaderHash());
202
203         Assert.assertNotNull(coderToolset.getCoder(ThreeStrings.class.getName()).getFilter().getRule());
204
205         Assert.assertEquals("[?($.third =~ /.*/)]",
206                         coderToolset.getCoder(ThreeStrings.class.getName()).getFilter().getRule());
207     }
208
209     private void validateInitialization(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset) {
210         Assert.assertEquals(CONTROLLER_ID, coderToolset.getControllerId());
211         Assert.assertEquals(releaseId.getGroupId(), coderToolset.getGroupId());
212         Assert.assertEquals(releaseId.getArtifactId(), coderToolset.getArtifactId());
213         Assert.assertNull(coderToolset.getCustomCoder());
214
215         Assert.assertEquals(1, coderToolset.getCoders().size());
216
217         CoderFilters coderFilters = coderToolset.getCoder(CONTROLLER_ID);
218         Assert.assertNull(coderFilters);
219
220         coderFilters = coderToolset.getCoder(ThreeStrings.class.getName());
221         Assert.assertNotNull(coderFilters);
222
223         Assert.assertEquals(coderFilters.getFilter(), protocolFilter);
224     }
225
226     private DroolsController createController() {
227         if (releaseId == null) {
228             throw new IllegalStateException("no prereq artifact installed in maven repository");
229         }
230
231         Properties sinkConfig = new Properties();
232         sinkConfig.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, JUNIT_PROTOCOL_CODER_TOPIC);
233         final List<TopicSink> noopTopics = TopicEndpointManager.getManager().addTopicSinks(sinkConfig);
234
235         Properties droolsControllerConfig = new Properties();
236         droolsControllerConfig.put(DroolsPropertyConstants.RULES_GROUPID, releaseId.getGroupId());
237         droolsControllerConfig.put(DroolsPropertyConstants.RULES_ARTIFACTID, releaseId.getArtifactId());
238         droolsControllerConfig.put(DroolsPropertyConstants.RULES_VERSION, releaseId.getVersion());
239         droolsControllerConfig.put(
240                         PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + "." + JUNIT_PROTOCOL_CODER_TOPIC
241                                         + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_SUFFIX,
242                         ThreeStrings.class.getName());
243
244         return DroolsControllerConstants.getFactory().build(droolsControllerConfig, null, noopTopics);
245     }
246
247     private JsonProtocolFilter createFilterSet() {
248         return new JsonProtocolFilter("[?($.first =~ /.*/ && $.second =~ /^blah.*/ && $.third =~ /^hello$/)]");
249     }
250
251     /**
252      * Note: We need an object that can be constructed, but the apache Triple cannot, thus
253      * we create our own class just for these tests.
254      */
255     @Getter
256     @AllArgsConstructor
257     public static class ThreeStrings {
258         private String first;
259         private String second;
260         private String third;
261     }
262 }