2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.util;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNotNull;
26 import static org.junit.jupiter.api.Assertions.assertNull;
28 import java.io.IOException;
30 import java.nio.file.Paths;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.Collections;
34 import java.util.Enumeration;
35 import java.util.stream.Collectors;
36 import org.junit.jupiter.api.BeforeAll;
37 import org.junit.jupiter.api.Test;
38 import org.kie.api.builder.ReleaseId;
39 import org.kie.api.definition.KiePackage;
40 import org.kie.api.definition.rule.Rule;
41 import org.kie.api.runtime.KieContainer;
42 import org.kie.api.runtime.KieSession;
47 public class KieUtilsTest {
49 private static KieContainer container;
50 private static KieSession session;
53 * Test class initialization.
56 public static void createArtifact() throws Exception {
58 KieUtils.installArtifact(
59 Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(),
60 Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(),
61 "src/main/resources/rules/org/onap/policy/drools/core/test/",
62 Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile());
64 container = KieUtils.createContainer(releaseId);
65 session = container.getKieBase("rules").newKieSession();
69 void testInstallArtifact() throws IOException {
71 KieUtils.installArtifact(
72 Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(),
73 Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(),
74 "src/main/resources/rules/org/onap/policy/drools/core/test/",
75 Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile());
77 assertNotNull(releaseId);
81 void testInstallArtifactList() throws IOException {
83 KieUtils.installArtifact(
84 Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/META-INF/kmodule.xml").toFile(),
85 Paths.get("src/test/resources/drools-artifact-1.1/pom.xml").toFile(),
86 "src/main/resources/rules/org/onap/policy/drools/core/test/",
87 Collections.singletonList(
88 Paths.get("src/test/resources/drools-artifact-1.1/src/main/resources/rules.drl").toFile()));
90 assertNotNull(releaseId);
95 assertEquals(Collections.singletonList("rules"), KieUtils.getBases(container));
100 assertEquals(Arrays.asList("java.util", "java.util.concurrent", "org.onap.policy.drools.core.test"),
101 KieUtils.getPackages(container).stream().map(KiePackage::getName).collect(Collectors.toList()));
105 void getPackageNames() {
106 assertEquals(Arrays.asList("java.util", "java.util.concurrent", "org.onap.policy.drools.core.test"),
107 new ArrayList<>(KieUtils.getPackageNames(container)));
112 assertEquals(Arrays.asList("Initialization", "Add elements of an int list"),
113 KieUtils.getRules(container).stream().map(Rule::getName).collect(Collectors.toList()));
117 void getRuleNames() {
118 assertEquals(Arrays.asList("Initialization", "Add elements of an int list"),
119 new ArrayList<>(KieUtils.getRuleNames(container)));
124 assertEquals(0, KieUtils.getFacts(session).size());
128 void testResourceToPackages() {
129 // Some minimal logging -- it would be nice to verify the 'KieUtils' logger messages
132 // test IOException from ClassLoader
133 log = new StringBuffer();
134 assertNull(KieUtils.resourceToPackages(new BogusClassLoader(log), "BogusClassLoader").orElse(null));
135 assertEquals("IOException(BogusClassLoader)", log.toString());
137 // test 'null' return when no resources are found
138 assertNull(KieUtils.resourceToPackages(ClassLoader.getSystemClassLoader(), "no/such/url").orElse(null));
140 // test IOException in 'IOUtils.toByteArray()' -> 'InputStream.read()'
141 log = new StringBuffer();
142 assertNull(KieUtils.resourceToPackages(new BogusClassLoader(log), "BogusUrl").orElse(null));
143 assertEquals("", log.toString());
145 // don't know how to test 'KieBuilder' errors at this point
147 // success legs are tested in 'DroolsContainerTest'
150 static class BogusClassLoader extends ClassLoader {
153 BogusClassLoader(StringBuffer log) {
158 public Enumeration<URL> getResources(String name) throws IOException {
159 if ("BogusUrl".equals(name)) {
160 return new Enumeration<>() {
162 public boolean hasMoreElements() {
167 public URL nextElement() {
169 // when the following URL is used, an IOException will occur
170 return new URL("http://127.0.0.1:1");
171 } catch (IOException e) {
172 // this should never happen, as the URL above is syntactically valid
178 log.append("IOException(BogusClassLoader)");
179 throw new IOException("BogusClassLoader");