b91ad4a0f5992f49aedee762386b1b2797f1f5b9
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 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.fasterxml.jackson.annotation.JsonAutoDetect.Visibility;
24 import com.fasterxml.jackson.annotation.PropertyAccessor;
25 import java.io.IOException;
26 import java.nio.file.Files;
27 import java.nio.file.Paths;
28 import java.util.ArrayList;
29 import java.util.List;
30 import java.util.Properties;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.kie.api.builder.ReleaseId;
35 import org.onap.policy.drools.controller.DroolsController;
36 import org.onap.policy.drools.controller.internal.MavenDroolsControllerTest;
37 import org.onap.policy.drools.event.comm.TopicEndpoint;
38 import org.onap.policy.drools.event.comm.TopicSink;
39 import org.onap.policy.drools.properties.PolicyProperties;
40 import org.onap.policy.drools.protocol.coders.EventProtocolCoder.CoderFilters;
41 import org.onap.policy.drools.protocol.coders.JsonProtocolFilter.FilterRule;
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;
46
47 /**
48  * ProtocolCoder Toolset JUNITs
49  */
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
54     private static Logger logger = LoggerFactory.getLogger(ProtocolCoderToolset.class);
55
56     private volatile ReleaseId releaseId;
57
58     @Before
59     public void setUp() throws IOException {
60         if (releaseId != null)
61             return;
62
63         String pom = new String(Files.readAllBytes
64             (Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_POM_PATH)));
65
66         if (!pom.contains("<artifactId>echo</artifactId>"))
67             throw new IllegalArgumentException("unexpected junit test pom");
68
69         String newPom = pom.replace("echo",  JUNIT_PROTOCOL_CODER_ARTIFACT_ID);
70
71         String kmodule = new String(Files.readAllBytes
72             (Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_PATH)));
73
74         String drl = new String(Files.readAllBytes
75             (Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_DRL_PATH)));
76
77         releaseId =
78             KieUtils.installArtifact(kmodule, newPom,
79                     MavenDroolsControllerTest.JUNIT_ECHO_KJAR_DRL_PATH, drl);
80     }
81
82     @Test
83     public void testGsonToolset() {
84         if (releaseId == null)
85             throw new IllegalStateException("no prereq artifact installed in maven repository");
86
87         JsonProtocolFilter protocolFilter = createFilterSet();
88
89         GsonProtocolCoderToolset gsonToolset =
90             new GsonProtocolCoderToolset(JUNIT_PROTOCOL_CODER_TOPIC,
91                 "blah",
92                 this.releaseId.getGroupId(),
93                 this.releaseId.getArtifactId(),
94                 Triple.class.getCanonicalName(),
95                 protocolFilter,
96                 null,
97                 12345678);
98
99         Assert.assertNotNull(gsonToolset.getEncoder());
100         Assert.assertNotNull(gsonToolset.getDecoder());
101
102         testToolset(protocolFilter, gsonToolset);
103     }
104
105     @Test
106     public void testJacksonToolset() {
107         if (releaseId == null)
108             throw new IllegalStateException("no prereq artifact installed in maven repository");
109
110         JsonProtocolFilter protocolFilter = createFilterSet();
111
112         JacksonProtocolCoderToolset jacksonToolset =
113             new JacksonProtocolCoderToolset(JUNIT_PROTOCOL_CODER_TOPIC,
114                 "blah",
115                 this.releaseId.getGroupId(),
116                 this.releaseId.getArtifactId(),
117                 Triple.class.getCanonicalName(),
118                 protocolFilter,
119                 null,
120                 12345678);
121
122         jacksonToolset.getEncoder().setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
123         jacksonToolset.getDecoder().setVisibility(PropertyAccessor.FIELD, Visibility.ANY);
124
125         testToolset(protocolFilter, jacksonToolset);
126     }
127
128     private JsonProtocolFilter createFilterSet() {
129         List<FilterRule> filters = new ArrayList<>();
130         filters.add(new FilterRule("first", ".*"));
131         filters.add(new FilterRule("second", "^blah.*"));
132         filters.add(new FilterRule("third", "^hello$"));
133
134         return new JsonProtocolFilter(filters);
135     }
136
137     private void testToolset(JsonProtocolFilter protocolFilter, ProtocolCoderToolset coderToolset) {
138
139         Assert.assertTrue("blah".equals(coderToolset.getControllerId()));
140         Assert.assertTrue(this.releaseId.getGroupId().equals(coderToolset.getGroupId()));
141         Assert.assertTrue(this.releaseId.getArtifactId().equals(coderToolset.getArtifactId()));
142         Assert.assertNull(coderToolset.getCustomCoder());
143
144         Assert.assertTrue(coderToolset.getCoders().size() == 1);
145
146         CoderFilters coderFilters = coderToolset.getCoder("blah");
147         Assert.assertTrue(coderFilters == null);
148
149         coderFilters = coderToolset.getCoder(Triple.class.getCanonicalName());
150         Assert.assertNotNull(coderFilters);
151
152         Assert.assertEquals(coderFilters.getFilter(), protocolFilter);
153
154         List<FilterRule> filters = new ArrayList<>();
155         filters.add(new FilterRule("third", ".*"));
156         coderToolset.addCoder(Triple.class.getCanonicalName(),
157             new JsonProtocolFilter(filters), 654321);
158
159         Assert.assertTrue(coderToolset.getCoders().size() == 1);
160
161         Assert.assertTrue
162             (coderToolset.getCoder(Triple.class.getCanonicalName()).
163                 getModelClassLoaderHash() == 654321);
164
165         Assert.assertTrue
166             (coderToolset.getCoder(Triple.class.getCanonicalName()).
167                 getFilter().getRules("third").size() == 1);
168
169         Assert.assertTrue
170             (coderToolset.getCoder(Triple.class.getCanonicalName()).
171                 getFilter().getRules("third").size() == 1);
172
173         Assert.assertTrue
174             (".*".equals(coderToolset.getCoder(Triple.class.getCanonicalName()).
175                 getFilter().getRules("third").get(0).getRegex()));
176
177         coderToolset.addCoder("blah", new JsonProtocolFilter(filters),654321);
178         Assert.assertTrue(coderToolset.getCoders().size() == 2);
179
180         coderToolset.removeCoders("blah");
181         Assert.assertTrue(coderToolset.getCoders().size() == 1);
182
183         /* restore original filters */
184         coderToolset.addCoder(Triple.class.getCanonicalName(), protocolFilter, 654321);
185
186         Triple<String, String, String> triple =
187             new Triple<>("v1", "v2", "v3");
188
189         String tripleEncoded = coderToolset.encode(triple);
190         Assert.assertTrue(!tripleEncoded.isEmpty());
191
192         Properties sinkConfig = new Properties();
193         sinkConfig.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS, JUNIT_PROTOCOL_CODER_TOPIC);
194         List<? extends TopicSink> noopTopics =
195             TopicEndpoint.manager.addTopicSinks(sinkConfig);
196
197         Properties droolsControllerConfig = new Properties();
198         droolsControllerConfig.put(PolicyProperties.RULES_GROUPID, releaseId.getGroupId());
199         droolsControllerConfig.put(PolicyProperties.RULES_ARTIFACTID, releaseId.getArtifactId());
200         droolsControllerConfig.put(PolicyProperties.RULES_VERSION, releaseId.getVersion());
201         droolsControllerConfig.put(PolicyProperties.PROPERTY_NOOP_SINK_TOPICS + "." +
202                 JUNIT_PROTOCOL_CODER_TOPIC + PolicyProperties.PROPERTY_TOPIC_EVENTS_SUFFIX,
203                  Triple.class.getCanonicalName());
204
205         DroolsController droolsController =
206             DroolsController.factory.build(droolsControllerConfig, null, noopTopics);
207
208         Triple<String, String, String> tripleDecoded = null;
209         try {
210             tripleDecoded =
211                 (Triple<String, String, String>) coderToolset.decode(tripleEncoded);
212         } catch(UnsupportedOperationException e){
213             logger.trace("Junit expected exception - decode does not pass filtering", e);
214         }
215
216         coderFilters = coderToolset.getCoder(Triple.class.getCanonicalName());
217         Assert.assertTrue(coderFilters.getCodedClass() == Triple.class.getCanonicalName());
218         Assert.assertTrue(coderFilters.getFilter() == protocolFilter);
219         Assert.assertTrue(coderFilters.getFilter().getRules("second").size() == 1);
220         Assert.assertTrue(coderFilters.getFilter().getRules("third").size() == 1);
221
222         coderFilters.getFilter().getRules("second").get(0).setRegex("^v2$");
223         coderFilters.getFilter().getRules("third").get(0).setRegex(".*v3.*");
224
225         tripleDecoded =
226             (Triple<String, String, String>) coderToolset.decode(tripleEncoded);
227
228         Assert.assertTrue(tripleDecoded.first().equals(triple.first()));
229         Assert.assertTrue(tripleDecoded.second().equals(triple.second()));
230         Assert.assertTrue(tripleDecoded.third().equals(triple.third()));
231
232         coderFilters.getFilter().deleteRules("third");
233         Assert.assertTrue(coderFilters.getFilter().getRules("third").isEmpty());
234
235         tripleDecoded =
236             (Triple<String, String, String>) coderToolset.decode(tripleEncoded);
237
238         Assert.assertTrue(tripleDecoded.first().equals(triple.first()));
239         Assert.assertTrue(tripleDecoded.second().equals(triple.second()));
240         Assert.assertTrue(tripleDecoded.third().equals(triple.third()));
241     }
242 }