aboutsummaryrefslogtreecommitdiffstats
path: root/policy-core/src/test/java/org/onap/policy/drools/core/PolicySessionTest.java
blob: 6f07a34fb65c3fc01f8943c3252ac43a44d3b8e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
 * ============LICENSE_START=======================================================
 * ONAP
 * ================================================================================
 * Copyright (C) 2018, 2020-2021 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2024 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.drools.core;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.concurrent.Semaphore;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.kie.api.event.rule.AgendaEventListener;
import org.kie.api.event.rule.RuleRuntimeEventListener;
import org.kie.api.runtime.KieSession;
import org.onap.policy.drools.core.PolicySession.ThreadModel;

class PolicySessionTest {

    private static final String MY_NAME = "my-name";
    private static final String CONTAINER = "my-container";
    private static final String EXPECTED = null;

    private PolicySession session;
    private PolicyContainer container;
    private KieSession kie;

    /**
     * Initialize test objects.
     */
    @BeforeEach
    public void setUp() {
        container = mock(PolicyContainer.class);
        kie = mock(KieSession.class);

        when(container.getName()).thenReturn(CONTAINER);

        session = new PolicySession(MY_NAME, container, kie);
    }

    @Test
    void test_Simple() {
        // verify constructor operations
        AgendaEventListener agenda = session;
        verify(kie).addEventListener(agenda);

        RuleRuntimeEventListener rule = session;
        verify(kie).addEventListener(rule);

        // test other simple methods
        assertEquals(container, session.getContainer());
        assertEquals(kie, session.getKieSession());
        assertEquals(MY_NAME, session.getName());
        assertEquals(CONTAINER + ":" + MY_NAME, session.getFullName());

        session.stopThread();
        session.updated();

        session.afterRuleFlowGroupActivated(null);
        session.afterRuleFlowGroupDeactivated(null);
        session.agendaGroupPopped(null);
        session.agendaGroupPushed(null);
        session.beforeMatchFired(null);
        session.beforeRuleFlowGroupActivated(null);
        session.beforeRuleFlowGroupDeactivated(null);
        session.matchCancelled(null);
        session.matchCreated(null);
        session.objectDeleted(null);
        session.objectInserted(null);
        session.objectUpdated(null);
    }

    @Test
    void testStartThread() {
        session.startThread();

        // re-start
        session.startThread();

        assertThatCode(() -> session.stopThread()).doesNotThrowAnyException();
    }

    @Test
    void testSetPolicySession_testGetCurrentSession_testRemovePolicySession() {
        PolicySession sess2 = new PolicySession(MY_NAME + "-b", container, kie);

        session.setPolicySession();
        assertEquals(session, PolicySession.getCurrentSession());

        sess2.setPolicySession();
        assertEquals(sess2, PolicySession.getCurrentSession());

        // remove a different session - should be unchanged
        session.removePolicySession();
        assertEquals(sess2, PolicySession.getCurrentSession());

        // remove the session
        sess2.removePolicySession();
        assertNull(PolicySession.getCurrentSession());
    }

    @Test
    void testGetAdjunct_testSetAdjunct() {
        Object adjnm1 = "adjunct-a";
        Object adjval1 = "value-a";
        session.setAdjunct(adjnm1, adjval1);

        Object adjnm2 = "adjunct-b";
        Object adjval2 = "value-b";
        session.setAdjunct(adjnm2, adjval2);

        assertEquals(adjval1, session.getAdjunct(adjnm1));
        assertEquals(adjval2, session.getAdjunct(adjnm2));
        assertNull(session.getAdjunct("unknown-adjunct"));
    }

    @Test
    void testThreadModel() {
        ThreadModel model = new PolicySession.ThreadModel() {
            @Override
            public void stop() {
                // do nothing
            }

            @Override
            public void start() {
                // do nothing
            }
        };

        assertThatCode(model::updated).doesNotThrowAnyException();
    }

    @Test
    void testDefaultThreadModelRun() throws Exception {
        testDefaultThreadModelRun_Ex(() -> {
            throw new RuntimeException(EXPECTED);
        });
        testDefaultThreadModelRun_Ex(() -> {
            throw new LinkageError(EXPECTED);
        });
    }

    /**
     * Starts a thread and then invokes a function to generate an exception within the
     * fireUntilHalt() method.
     *
     * @param genEx function to generate an exception
     * @throws Exception if an error occurs
     */
    private void testDefaultThreadModelRun_Ex(Runnable genEx) throws Exception {
        Semaphore me = new Semaphore(0);
        Semaphore thread = new Semaphore(0);

        doAnswer(args -> {
            // let me know the thread has started
            me.release(1);

            // wait until I tell it to continue
            thread.acquire();

            // generate the exception
            genEx.run();

            // never reaches here
            return null;

        }).when(kie).fireUntilHalt();

        session.startThread();

        me.acquire();
        thread.release();

        session.stopThread();
    }

}