2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018, 2020-2021 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.core;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNull;
27 import static org.mockito.Mockito.doAnswer;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.concurrent.Semaphore;
33 import org.junit.jupiter.api.BeforeEach;
34 import org.junit.jupiter.api.Test;
35 import org.kie.api.event.rule.AgendaEventListener;
36 import org.kie.api.event.rule.RuleRuntimeEventListener;
37 import org.kie.api.runtime.KieSession;
38 import org.onap.policy.drools.core.PolicySession.ThreadModel;
40 class PolicySessionTest {
42 private static final String MY_NAME = "my-name";
43 private static final String CONTAINER = "my-container";
44 private static final String EXPECTED = null;
46 private PolicySession session;
47 private PolicyContainer container;
48 private KieSession kie;
51 * Initialize test objects.
55 container = mock(PolicyContainer.class);
56 kie = mock(KieSession.class);
58 when(container.getName()).thenReturn(CONTAINER);
60 session = new PolicySession(MY_NAME, container, kie);
65 // verify constructor operations
66 AgendaEventListener agenda = session;
67 verify(kie).addEventListener(agenda);
69 RuleRuntimeEventListener rule = session;
70 verify(kie).addEventListener(rule);
72 // test other simple methods
73 assertEquals(container, session.getContainer());
74 assertEquals(kie, session.getKieSession());
75 assertEquals(MY_NAME, session.getName());
76 assertEquals(CONTAINER + ":" + MY_NAME, session.getFullName());
81 session.afterRuleFlowGroupActivated(null);
82 session.afterRuleFlowGroupDeactivated(null);
83 session.agendaGroupPopped(null);
84 session.agendaGroupPushed(null);
85 session.beforeMatchFired(null);
86 session.beforeRuleFlowGroupActivated(null);
87 session.beforeRuleFlowGroupDeactivated(null);
88 session.matchCancelled(null);
89 session.matchCreated(null);
90 session.objectDeleted(null);
91 session.objectInserted(null);
92 session.objectUpdated(null);
96 void testStartThread() {
97 session.startThread();
100 session.startThread();
102 assertThatCode(() -> session.stopThread()).doesNotThrowAnyException();
106 void testSetPolicySession_testGetCurrentSession_testRemovePolicySession() {
107 PolicySession sess2 = new PolicySession(MY_NAME + "-b", container, kie);
109 session.setPolicySession();
110 assertEquals(session, PolicySession.getCurrentSession());
112 sess2.setPolicySession();
113 assertEquals(sess2, PolicySession.getCurrentSession());
115 // remove a different session - should be unchanged
116 session.removePolicySession();
117 assertEquals(sess2, PolicySession.getCurrentSession());
119 // remove the session
120 sess2.removePolicySession();
121 assertNull(PolicySession.getCurrentSession());
125 void testGetAdjunct_testSetAdjunct() {
126 Object adjnm1 = "adjunct-a";
127 Object adjval1 = "value-a";
128 session.setAdjunct(adjnm1, adjval1);
130 Object adjnm2 = "adjunct-b";
131 Object adjval2 = "value-b";
132 session.setAdjunct(adjnm2, adjval2);
134 assertEquals(adjval1, session.getAdjunct(adjnm1));
135 assertEquals(adjval2, session.getAdjunct(adjnm2));
136 assertNull(session.getAdjunct("unknown-adjunct"));
140 void testThreadModel() {
141 ThreadModel model = new PolicySession.ThreadModel() {
148 public void start() {
153 assertThatCode(model::updated).doesNotThrowAnyException();
157 void testDefaultThreadModelRun() throws Exception {
158 testDefaultThreadModelRun_Ex(() -> {
159 throw new RuntimeException(EXPECTED);
161 testDefaultThreadModelRun_Ex(() -> {
162 throw new LinkageError(EXPECTED);
167 * Starts a thread and then invokes a function to generate an exception within the
168 * fireUntilHalt() method.
170 * @param genEx function to generate an exception
171 * @throws Exception if an error occurs
173 private void testDefaultThreadModelRun_Ex(Runnable genEx) throws Exception {
174 Semaphore me = new Semaphore(0);
175 Semaphore thread = new Semaphore(0);
178 // let me know the thread has started
181 // wait until I tell it to continue
184 // generate the exception
187 // never reaches here
190 }).when(kie).fireUntilHalt();
192 session.startThread();
197 session.stopThread();