Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-core / src / test / java / org / onap / policy / drools / util / KieUtilsTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.util;
23
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;
27
28 import java.io.IOException;
29 import java.net.URL;
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;
43
44 /**
45  * Kie Utils Tests.
46  */
47 public class KieUtilsTest {
48
49     private static KieContainer container;
50     private static KieSession session;
51
52     /**
53      * Test class initialization.
54      */
55     @BeforeAll
56     public static void createArtifact() throws Exception {
57         ReleaseId releaseId =
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());
63
64         container = KieUtils.createContainer(releaseId);
65         session = container.getKieBase("rules").newKieSession();
66     }
67
68     @Test
69     void testInstallArtifact() throws IOException {
70         ReleaseId releaseId =
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());
76
77         assertNotNull(releaseId);
78     }
79
80     @Test
81     void testInstallArtifactList() throws IOException {
82         ReleaseId releaseId =
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()));
89
90         assertNotNull(releaseId);
91     }
92
93     @Test
94     void getBases() {
95         assertEquals(Collections.singletonList("rules"), KieUtils.getBases(container));
96     }
97
98     @Test
99     void getPackages() {
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()));
102     }
103
104     @Test
105     void getPackageNames() {
106         assertEquals(Arrays.asList("java.util", "java.util.concurrent", "org.onap.policy.drools.core.test"),
107             new ArrayList<>(KieUtils.getPackageNames(container)));
108     }
109
110     @Test
111     void getRules() {
112         assertEquals(Arrays.asList("Initialization", "Add elements of an int list"),
113             KieUtils.getRules(container).stream().map(Rule::getName).collect(Collectors.toList()));
114     }
115
116     @Test
117     void getRuleNames() {
118         assertEquals(Arrays.asList("Initialization", "Add elements of an int list"),
119             new ArrayList<>(KieUtils.getRuleNames(container)));
120     }
121
122     @Test
123     void getFacts() {
124         assertEquals(0, KieUtils.getFacts(session).size());
125     }
126
127     @Test
128     void testResourceToPackages() {
129         // Some minimal logging -- it would be nice to verify the 'KieUtils' logger messages
130         StringBuffer log;
131
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());
136
137         // test 'null' return when no resources are found
138         assertNull(KieUtils.resourceToPackages(ClassLoader.getSystemClassLoader(), "no/such/url").orElse(null));
139
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());
144
145         // don't know how to test 'KieBuilder' errors at this point
146
147         // success legs are tested in 'DroolsContainerTest'
148     }
149
150     static class BogusClassLoader extends ClassLoader {
151         StringBuffer log;
152
153         BogusClassLoader(StringBuffer log) {
154             this.log = log;
155         }
156
157         @Override
158         public Enumeration<URL> getResources(String name) throws IOException {
159             if ("BogusUrl".equals(name)) {
160                 return new Enumeration<>() {
161                     @Override
162                     public boolean hasMoreElements() {
163                         return true;
164                     }
165
166                     @Override
167                     public URL nextElement() {
168                         try {
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
173                             return null;
174                         }
175                     }
176                 };
177             } else {
178                 log.append("IOException(BogusClassLoader)");
179                 throw new IOException("BogusClassLoader");
180             }
181         }
182     }
183 }