2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.protocol.coders;
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;
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;
51 * ProtocolCoder Toolset Junits.
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";
58 private static Logger logger = LoggerFactory.getLogger(ProtocolCoderToolset.class);
60 private static volatile ReleaseId releaseId;
62 // customCoder has to be public to be accessed in tests below
63 public static final Gson customCoder = new GsonBuilder().create();
65 private DroolsController controller;
68 * Test Class Initialization.
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());
83 controller = createController();
90 public void tearDown() {
91 if (controller != null) {
92 DroolsControllerConstants.getFactory().destroy(controller);
97 public void testToolsets() {
98 testGsonToolset(createFilterSet());
102 * Test the Gson toolset.
104 * @param protocolFilter protocol filter
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);
113 Assert.assertNotNull(gsonToolset.getEncoder());
114 Assert.assertNotNull(gsonToolset.getDecoder());
116 testToolset(protocolFilter, gsonToolset);
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);
124 private ThreeStrings createTriple() {
125 return new ThreeStrings("v1", "v2", "v3");
128 private void testToolset(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset) {
130 validateInitialization(protocolFilter, coderToolset);
132 updateCoderFilterRule(coderToolset);
134 addRemoveCoder(coderToolset);
136 /* restore original filters */
137 coderToolset.addCoder(ThreeStrings.class.getName(), protocolFilter, 654321);
139 ThreeStrings triple = createTriple();
141 String tripleEncoded = encode(coderToolset, triple);
143 decode(protocolFilter, coderToolset, triple, tripleEncoded);
146 private void decode(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset,
147 ThreeStrings triple, String tripleEncoded) {
149 ThreeStrings tripleDecoded = null;
151 tripleDecoded = (ThreeStrings) coderToolset.decode(tripleEncoded);
152 } catch (UnsupportedOperationException e) {
154 logger.trace("Junit expected exception - decode does not pass filtering", e);
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());
162 coderFilters.getFilter().setRule("[?($.second =~ /^v2$/ && $.third =~ /.*v3.*/)]");
164 tripleDecoded = (ThreeStrings) coderToolset.decode(tripleEncoded);
166 Assert.assertEquals(triple.getFirst(), tripleDecoded.getFirst());
167 Assert.assertEquals(triple.getSecond(), tripleDecoded.getSecond());
168 Assert.assertEquals(triple.getThird(), tripleDecoded.getThird());
170 coderFilters.getFilter().setRule(null);
171 Assert.assertEquals("[?($ =~ /.*/)]", coderFilters.getFilter().getRule());
173 tripleDecoded = (ThreeStrings) coderToolset.decode(tripleEncoded);
175 Assert.assertEquals(tripleDecoded.getFirst(), triple.getFirst());
176 Assert.assertEquals(tripleDecoded.getSecond(), triple.getSecond());
177 Assert.assertEquals(tripleDecoded.getThird(), triple.getThird());
179 coderFilters.getFilter().setRule("[?($.third =~ /.*v3.*/)]");
182 private String encode(ProtocolCoderToolset coderToolset, ThreeStrings triple) {
183 String tripleEncoded = coderToolset.encode(triple);
184 Assert.assertTrue(!tripleEncoded.isEmpty());
185 return tripleEncoded;
188 private void addRemoveCoder(ProtocolCoderToolset coderToolset) {
189 coderToolset.addCoder(this.getClass().getName(), new JsonProtocolFilter("[?($.second =~ /.*/)]"), 654321);
190 Assert.assertEquals(2, coderToolset.getCoders().size());
192 coderToolset.removeCoders(this.getClass().getName());
193 Assert.assertEquals(1, coderToolset.getCoders().size());
196 private void updateCoderFilterRule(ProtocolCoderToolset coderToolset) {
197 coderToolset.addCoder(ThreeStrings.class.getName(), new JsonProtocolFilter("[?($.third =~ /.*/)]"), 654321);
199 Assert.assertEquals(1, coderToolset.getCoders().size());
201 Assert.assertEquals(654321, coderToolset.getCoder(ThreeStrings.class.getName()).getModelClassLoaderHash());
203 Assert.assertNotNull(coderToolset.getCoder(ThreeStrings.class.getName()).getFilter().getRule());
205 Assert.assertEquals("[?($.third =~ /.*/)]",
206 coderToolset.getCoder(ThreeStrings.class.getName()).getFilter().getRule());
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());
215 Assert.assertEquals(1, coderToolset.getCoders().size());
217 CoderFilters coderFilters = coderToolset.getCoder(CONTROLLER_ID);
218 Assert.assertNull(coderFilters);
220 coderFilters = coderToolset.getCoder(ThreeStrings.class.getName());
221 Assert.assertNotNull(coderFilters);
223 Assert.assertEquals(coderFilters.getFilter(), protocolFilter);
226 private DroolsController createController() {
227 if (releaseId == null) {
228 throw new IllegalStateException("no prereq artifact installed in maven repository");
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);
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());
244 return DroolsControllerConstants.getFactory().build(droolsControllerConfig, null, noopTopics);
247 private JsonProtocolFilter createFilterSet() {
248 return new JsonProtocolFilter("[?($.first =~ /.*/ && $.second =~ /^blah.*/ && $.third =~ /^hello$/)]");
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.
257 public static class ThreeStrings {
258 private String first;
259 private String second;
260 private String third;