2b4a29b34146cff0b53dcdb60b76a356148c8ada
[policy/drools-applications.git] / controlloop / m2 / lock / src / test / java / org / onap / policy / drools / m2 / lock / LockAdjunctTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * m2/lock
4  * ================================================================================
5  * Copyright (C) 2020 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.drools.m2.lock;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.Properties;
28 import java.util.UUID;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.onap.policy.drools.core.lock.Lock;
33 import org.onap.policy.drools.core.lock.LockCallback;
34 import org.onap.policy.drools.system.PolicyEngineConstants;
35 import org.onap.policy.m2.base.Transaction;
36
37 public class LockAdjunctTest {
38
39     public class TestOwner implements LockCallback {
40
41         @Override
42         public void lockAvailable(Lock arg0) {
43             return;
44         }
45
46         @Override
47         public void lockUnavailable(Lock arg0) {
48             return;
49         }
50     }
51
52     private LockCallback owner;
53     private Lock lock;
54
55     @BeforeClass
56     public static void start() {
57         PolicyEngineConstants.getManager().configure(new Properties());
58         PolicyEngineConstants.getManager().start();
59     }
60
61     @AfterClass
62     public static void stop() {
63         PolicyEngineConstants.getManager().stop();
64     }
65
66     @Test
67     public void testLockAdjunct() {
68         owner = new TestOwner();
69         lock = PolicyEngineConstants.getManager().createLock("key", "ownerKey", 60, owner, false);
70         LockAdjunct lockA = new LockAdjunct();
71
72         assertNotNull(lockA);
73
74         lockA.lockUnavailable(lock);
75         assertTrue(lock.isActive());
76         assertTrue(lockA.getLock(null, "key", "ownerKey", false));
77         LockAdjunct lockB = new LockAdjunct();
78         assertFalse(lockB.getLock(null, "key", "ownerKey", false));
79         assertTrue(lock.free());
80
81         lockB.lockAvailable(lock);
82         assertFalse(lock.isActive());
83         assertTrue(lockB.getLock(null, "key", "ownerKey", false));
84         assertFalse(lock.free());
85
86         UUID uuid = UUID.randomUUID();
87         Transaction transaction = new Transaction(null, "1111", uuid, null);
88         lockA.cleanup(transaction);
89     }
90 }