18ab15b5aff25e36259a18f4e2cf185a26e30b22
[policy/drools-applications.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.controlloop.eventmanager;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertSame;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Mockito.verify;
27 import static org.mockito.Mockito.when;
28
29 import org.drools.core.WorkingMemory;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.kie.api.runtime.rule.FactHandle;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.drools.core.lock.Lock;
36
37 public class LockCallbackWorkingMemoryTest {
38     private static final String MY_NAME = "my-name";
39
40     @Mock
41     private WorkingMemory workingMemory;
42
43     @Mock
44     private Lock lock;
45
46     @Mock
47     private FactHandle fact;
48
49     private LockCallbackWorkingMemory callback;
50
51
52     /**
53      * Initializes mocks and creates a call-back.
54      */
55     @Before
56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58
59         when(workingMemory.getFactHandle(lock)).thenReturn(fact);
60
61         callback = new LockCallbackWorkingMemory(MY_NAME, workingMemory);
62     }
63
64     @Test
65     public void testLockCallbackWorkingMemory() {
66         assertEquals(MY_NAME, callback.getName());
67         assertSame(workingMemory, callback.getWorkingMemory());
68     }
69
70     @Test
71     public void testLockAvailable() {
72         callback.lockAvailable(lock);
73         verify(workingMemory).update(fact, lock);
74
75         // "remove" from working memory
76         when(workingMemory.getFactHandle(lock)).thenReturn(null);
77         callback.lockAvailable(lock);
78
79         // should be no additional calls
80         verify(workingMemory).update(any(), any());
81     }
82
83     @Test
84     public void testLockUnavailable() {
85         callback.lockUnavailable(lock);
86         verify(workingMemory).update(fact, lock);
87
88         // "remove" from working memory
89         when(workingMemory.getFactHandle(lock)).thenReturn(null);
90         callback.lockUnavailable(lock);
91
92         // should be no additional calls
93         verify(workingMemory).update(any(), any());
94     }
95
96 }