2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018-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
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.Files;
27 import java.nio.file.Paths;
28 import java.util.List;
29 import java.util.Properties;
30 import org.junit.Assert;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.kie.api.builder.ReleaseId;
34 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
35 import org.onap.policy.common.endpoints.event.comm.TopicSink;
36 import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
37 import org.onap.policy.drools.controller.DroolsController;
38 import org.onap.policy.drools.controller.internal.MavenDroolsControllerTest;
39 import org.onap.policy.drools.properties.DroolsProperties;
40 import org.onap.policy.drools.protocol.coders.EventProtocolCoder.CoderFilters;
41 import org.onap.policy.drools.protocol.coders.TopicCoderFilterConfiguration.CustomGsonCoder;
42 import org.onap.policy.drools.util.KieUtils;
43 import org.onap.policy.drools.utils.Triple;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * ProtocolCoder Toolset JUNITs.
50 public class ProtocolCoderToolsetTest {
51 public static final String JUNIT_PROTOCOL_CODER_ARTIFACT_ID = "protocolcoder";
52 public static final String JUNIT_PROTOCOL_CODER_TOPIC = JUNIT_PROTOCOL_CODER_ARTIFACT_ID;
53 public static final String CONTROLLER_ID = "blah";
54 public static final String ARTIFACT_ID_ECHO = "echo";
55 public static final String ARTIFACT_ID_POM_LINE = "<artifactId>" + ARTIFACT_ID_ECHO + "</artifactId>";
57 private static Logger logger = LoggerFactory.getLogger(ProtocolCoderToolset.class);
59 private volatile ReleaseId releaseId;
61 public static final Gson customCoder = new GsonBuilder().create();
66 * @throws IOException throws IO Exception
69 public void setUp() throws IOException {
70 if (releaseId != null) {
74 String pom = new String(Files.readAllBytes(Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_POM_PATH)));
76 if (!pom.contains(ARTIFACT_ID_POM_LINE)) {
77 throw new IllegalArgumentException("unexpected junit test pom");
80 String newPom = pom.replace(ARTIFACT_ID_ECHO, JUNIT_PROTOCOL_CODER_ARTIFACT_ID);
82 String kmodule = new String(Files.readAllBytes(Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_PATH)));
84 String drl = new String(Files.readAllBytes(Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_DRL_PATH)));
86 releaseId = KieUtils.installArtifact(kmodule, newPom, MavenDroolsControllerTest.JUNIT_ECHO_KJAR_DRL_PATH, drl);
90 public void testToolsets() {
92 testGsonToolset(createFilterSet());
96 * Test the Gson toolset.
98 * @param protocolFilter protocol filter
100 public void testGsonToolset(JsonProtocolFilter protocolFilter) {
101 GsonProtocolCoderToolset gsonToolset = new GsonProtocolCoderToolset(
102 EventProtocolParams.builder().topic(JUNIT_PROTOCOL_CODER_TOPIC)
103 .groupId(this.releaseId.getGroupId())
104 .artifactId(this.releaseId.getArtifactId())
105 .eventClass(Triple.class.getName())
106 .protocolFilter(protocolFilter)
107 .customGsonCoder(null)
108 .modelClassLoaderHash(12345678), CONTROLLER_ID);
110 Assert.assertNotNull(gsonToolset.getEncoder());
111 Assert.assertNotNull(gsonToolset.getDecoder());
113 testToolset(protocolFilter, gsonToolset);
115 Triple<String, String, String> triple = createTriple();
116 gsonToolset.setCustomCoder(new CustomGsonCoder(this.getClass().getName(), "customCoder"));
117 String tripleEncoded = encode(gsonToolset, triple);
118 decode(protocolFilter, gsonToolset, triple, tripleEncoded);
121 private Triple<String, String, String> createTriple() {
122 return new Triple<>("v1", "v2", "v3");
125 private void testToolset(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset) {
127 validateInitialization(protocolFilter, coderToolset);
129 updateCoderFilterRule(coderToolset);
131 addRemoveCoder(coderToolset);
133 /* restore original filters */
134 coderToolset.addCoder(Triple.class.getName(), protocolFilter, 654321);
136 Triple<String, String, String> triple = createTriple();
138 String tripleEncoded = encode(coderToolset, triple);
140 decode(protocolFilter, coderToolset, triple, tripleEncoded);
143 @SuppressWarnings("unchecked")
144 private void decode(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset,
145 Triple<String, String, String> triple, String tripleEncoded) {
147 Triple<String, String, String> tripleDecoded = null;
149 tripleDecoded = (Triple<String, String, String>) coderToolset.decode(tripleEncoded);
150 } catch (UnsupportedOperationException e) {
152 logger.trace("Junit expected exception - decode does not pass filtering", e);
155 CoderFilters coderFilters = coderToolset.getCoder(Triple.class.getName());
156 Assert.assertTrue(coderFilters.getCodedClass() == Triple.class.getName());
157 Assert.assertTrue(coderFilters.getFilter() == protocolFilter);
158 Assert.assertTrue(coderFilters.getFilter().getRule() != null);
160 coderFilters.getFilter().setRule("[?($.second =~ /^v2$/ && $.third =~ /.*v3.*/)]");
162 tripleDecoded = (Triple<String, String, String>) coderToolset.decode(tripleEncoded);
164 Assert.assertTrue(tripleDecoded.first().equals(triple.first()));
165 Assert.assertTrue(tripleDecoded.second().equals(triple.second()));
166 Assert.assertTrue(tripleDecoded.third().equals(triple.third()));
168 coderFilters.getFilter().setRule(null);
169 Assert.assertEquals("[?($ =~ /.*/)]", coderFilters.getFilter().getRule());
171 tripleDecoded = (Triple<String, String, String>) coderToolset.decode(tripleEncoded);
173 Assert.assertTrue(tripleDecoded.first().equals(triple.first()));
174 Assert.assertTrue(tripleDecoded.second().equals(triple.second()));
175 Assert.assertTrue(tripleDecoded.third().equals(triple.third()));
177 coderFilters.getFilter().setRule("[?($.third =~ /.*v3.*/)]");
180 private String encode(ProtocolCoderToolset coderToolset, Triple<String, String, String> triple) {
181 String tripleEncoded = coderToolset.encode(triple);
182 Assert.assertTrue(!tripleEncoded.isEmpty());
183 return tripleEncoded;
186 private void addRemoveCoder(ProtocolCoderToolset coderToolset) {
187 coderToolset.addCoder(this.getClass().getName(),
188 new JsonProtocolFilter("[?($.second =~ /.*/)]"), 654321);
189 Assert.assertTrue(coderToolset.getCoders().size() == 2);
191 coderToolset.removeCoders(this.getClass().getName());
192 Assert.assertTrue(coderToolset.getCoders().size() == 1);
195 private void updateCoderFilterRule(ProtocolCoderToolset coderToolset) {
196 coderToolset.addCoder(Triple.class.getName(), new JsonProtocolFilter("[?($.third =~ /.*/)]"), 654321);
198 Assert.assertTrue(coderToolset.getCoders().size() == 1);
200 Assert.assertTrue(coderToolset.getCoder(Triple.class.getName()).getModelClassLoaderHash() == 654321);
203 coderToolset.getCoder(
204 Triple.class.getName()).getFilter().getRule() != null);
206 Assert.assertTrue("[?($.third =~ /.*/)]".equals(coderToolset.getCoder(Triple.class.getName())
207 .getFilter().getRule()));
210 private void validateInitialization(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset) {
211 Assert.assertTrue(CONTROLLER_ID.equals(coderToolset.getControllerId()));
212 Assert.assertTrue(this.releaseId.getGroupId().equals(coderToolset.getGroupId()));
213 Assert.assertTrue(this.releaseId.getArtifactId().equals(coderToolset.getArtifactId()));
214 Assert.assertNull(coderToolset.getCustomCoder());
216 Assert.assertTrue(coderToolset.getCoders().size() == 1);
218 CoderFilters coderFilters = coderToolset.getCoder(CONTROLLER_ID);
219 Assert.assertTrue(coderFilters == null);
221 coderFilters = coderToolset.getCoder(Triple.class.getName());
222 Assert.assertNotNull(coderFilters);
224 Assert.assertEquals(coderFilters.getFilter(), protocolFilter);
227 private void createController() {
228 if (releaseId == null) {
229 throw new IllegalStateException("no prereq artifact installed in maven repository");
232 Properties sinkConfig = new Properties();
233 sinkConfig.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS, JUNIT_PROTOCOL_CODER_TOPIC);
234 final List<? extends TopicSink> noopTopics = TopicEndpointManager.getManager().addTopicSinks(sinkConfig);
236 Properties droolsControllerConfig = new Properties();
237 droolsControllerConfig.put(DroolsProperties.RULES_GROUPID, releaseId.getGroupId());
238 droolsControllerConfig.put(DroolsProperties.RULES_ARTIFACTID, releaseId.getArtifactId());
239 droolsControllerConfig.put(DroolsProperties.RULES_VERSION, releaseId.getVersion());
240 droolsControllerConfig.put(PolicyEndPointProperties.PROPERTY_NOOP_SINK_TOPICS + "." + JUNIT_PROTOCOL_CODER_TOPIC
241 + PolicyEndPointProperties.PROPERTY_TOPIC_EVENTS_SUFFIX, Triple.class.getName());
243 DroolsController.factory.build(droolsControllerConfig, null, noopTopics);
246 private JsonProtocolFilter createFilterSet() {
247 return new JsonProtocolFilter("[?($.first =~ /.*/ && $.second =~ /^blah.*/ && $.third =~ /^hello$/)]");