Removing deprecated DMAAP library
[policy/drools-pdp.git] / policy-core / src / test / java / org / onap / policy / drools / core / PolicySessionTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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
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.core;
23
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;
31
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;
39
40 class PolicySessionTest {
41
42     private static final String MY_NAME = "my-name";
43     private static final String CONTAINER = "my-container";
44     private static final String EXPECTED = null;
45
46     private PolicySession session;
47     private PolicyContainer container;
48     private KieSession kie;
49
50     /**
51      * Initialize test objects.
52      */
53     @BeforeEach
54     public void setUp() {
55         container = mock(PolicyContainer.class);
56         kie = mock(KieSession.class);
57
58         when(container.getName()).thenReturn(CONTAINER);
59
60         session = new PolicySession(MY_NAME, container, kie);
61     }
62
63     @Test
64     void test_Simple() {
65         // verify constructor operations
66         AgendaEventListener agenda = session;
67         verify(kie).addEventListener(agenda);
68
69         RuleRuntimeEventListener rule = session;
70         verify(kie).addEventListener(rule);
71
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());
77
78         session.stopThread();
79         session.updated();
80
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);
93     }
94
95     @Test
96     void testStartThread() {
97         session.startThread();
98
99         // re-start
100         session.startThread();
101
102         assertThatCode(() -> session.stopThread()).doesNotThrowAnyException();
103     }
104
105     @Test
106     void testSetPolicySession_testGetCurrentSession_testRemovePolicySession() {
107         PolicySession sess2 = new PolicySession(MY_NAME + "-b", container, kie);
108
109         session.setPolicySession();
110         assertEquals(session, PolicySession.getCurrentSession());
111
112         sess2.setPolicySession();
113         assertEquals(sess2, PolicySession.getCurrentSession());
114
115         // remove a different session - should be unchanged
116         session.removePolicySession();
117         assertEquals(sess2, PolicySession.getCurrentSession());
118
119         // remove the session
120         sess2.removePolicySession();
121         assertNull(PolicySession.getCurrentSession());
122     }
123
124     @Test
125     void testGetAdjunct_testSetAdjunct() {
126         Object adjnm1 = "adjunct-a";
127         Object adjval1 = "value-a";
128         session.setAdjunct(adjnm1, adjval1);
129
130         Object adjnm2 = "adjunct-b";
131         Object adjval2 = "value-b";
132         session.setAdjunct(adjnm2, adjval2);
133
134         assertEquals(adjval1, session.getAdjunct(adjnm1));
135         assertEquals(adjval2, session.getAdjunct(adjnm2));
136         assertNull(session.getAdjunct("unknown-adjunct"));
137     }
138
139     @Test
140     void testThreadModel() {
141         ThreadModel model = new PolicySession.ThreadModel() {
142             @Override
143             public void stop() {
144                 // do nothing
145             }
146
147             @Override
148             public void start() {
149                 // do nothing
150             }
151         };
152
153         assertThatCode(model::updated).doesNotThrowAnyException();
154     }
155
156     @Test
157     void testDefaultThreadModelRun() throws Exception {
158         testDefaultThreadModelRun_Ex(() -> {
159             throw new RuntimeException(EXPECTED);
160         });
161         testDefaultThreadModelRun_Ex(() -> {
162             throw new LinkageError(EXPECTED);
163         });
164     }
165
166     /**
167      * Starts a thread and then invokes a function to generate an exception within the
168      * fireUntilHalt() method.
169      *
170      * @param genEx function to generate an exception
171      * @throws Exception if an error occurs
172      */
173     private void testDefaultThreadModelRun_Ex(Runnable genEx) throws Exception {
174         Semaphore me = new Semaphore(0);
175         Semaphore thread = new Semaphore(0);
176
177         doAnswer(args -> {
178             // let me know the thread has started
179             me.release(1);
180
181             // wait until I tell it to continue
182             thread.acquire();
183
184             // generate the exception
185             genEx.run();
186
187             // never reaches here
188             return null;
189
190         }).when(kie).fireUntilHalt();
191
192         session.startThread();
193
194         me.acquire();
195         thread.release();
196
197         session.stopThread();
198     }
199
200 }