Updating licenses in all files
[appc.git] / appc-dispatcher / appc-dispatcher-common / lock-manager-lib / lock-manager-impl / src / test / java / org / openecomp / appc / lockmanager / api / LockManagerBaseTests.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * APPC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright (C) 2017 Amdocs
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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22
23 package org.openecomp.appc.lockmanager.api;
24
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.openecomp.appc.lockmanager.api.LockException;
29 import org.openecomp.appc.lockmanager.api.LockManager;
30
31 public abstract class LockManagerBaseTests {
32
33         protected enum Resource {Resource1, Resource2};
34         protected enum Owner {A, B};
35
36         protected LockManager lockManager;
37
38         @Before
39         public void beforeTest() {
40                 lockManager = createLockManager();
41         }
42
43         protected abstract LockManager createLockManager();
44
45         @Test
46         public void testAcquireLock() throws LockException {
47                 boolean lockRes = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
48                 try {
49                         Assert.assertTrue(lockRes);
50                 } finally {
51                         lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
52                 }
53         }
54
55         @Test
56         public void testAcquireLock_AlreadyLockedBySameOwner() throws LockException {
57                 boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
58                 try {
59                         Assert.assertTrue(lockRes1);
60                         boolean lockRes2 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
61                         Assert.assertFalse(lockRes2);
62                 } finally {
63                         lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
64                 }
65         }
66
67         @Test(expected = LockException.class)
68         public void testAcquireLock_AlreadyLockedByOtherOwner() throws LockException {
69                 String owner2 = "B";
70                 boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
71                 try {
72                         Assert.assertTrue(lockRes1);
73                         boolean lockRes2 = lockManager.acquireLock(Resource.Resource1.name(), owner2);
74                         Assert.assertFalse(lockRes2);
75                 } finally {
76                         lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
77                 }
78         }
79
80         @Test
81         public void testAcquireLock_LockDifferentResources() throws LockException {
82                 boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
83                 try {
84                         Assert.assertTrue(lockRes1);
85                         boolean lockRes2 = lockManager.acquireLock(Resource.Resource2.name(), Owner.B.name());
86                         try {
87                                 Assert.assertTrue(lockRes2);
88                         } finally {
89                                 lockManager.releaseLock(Resource.Resource2.name(), Owner.B.name());
90                         }
91                 } finally {
92                         lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
93                 }
94         }
95
96         @Test(expected = LockException.class)
97         public void testReleaseLock_NotLockedResource() throws LockException {
98                 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
99         }
100
101         @Test(expected = LockException.class)
102         public void testReleaseLock_LockedByOtherOwnerResource() throws LockException {
103                 boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name());
104                 try {
105                         Assert.assertTrue(lockRes1);
106                         lockManager.releaseLock(Resource.Resource1.name(), Owner.B.name());
107                 } finally {
108                         lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
109                 }
110         }
111
112         @Test(expected = LockException.class)
113         public void testAcquireLock_LockExpired() throws LockException, InterruptedException {
114                 boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
115                 Assert.assertTrue(lockRes1);
116                 Thread.sleep(1000);
117                 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
118         }
119
120         @Test
121         public void testAcquireLock_OtherLockExpired() throws LockException, InterruptedException {
122                 boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
123                 Assert.assertTrue(lockRes1);
124                 Thread.sleep(1000);
125                 boolean lockRes2 = lockManager.acquireLock(Resource.Resource1.name(), Owner.B.name());
126                 try {
127                         Assert.assertTrue(lockRes2);
128                 }finally {
129                         lockManager.releaseLock(Resource.Resource1.name(), Owner.B.name());
130                 }
131         }
132
133         @Test
134         public void testIsLocked_WhenLocked() throws LockException, InterruptedException {
135                 boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
136                 try {
137                 Assert.assertTrue(lockManager.isLocked(Resource.Resource1.name()));
138                 }finally {
139                         lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
140                 }
141         }
142
143
144     @Test(expected = LockException.class)
145         public void testIsLocked_LockExpired() throws LockException, InterruptedException {
146                 boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
147                 Assert.assertTrue(lockRes1);
148                 Assert.assertTrue(lockManager.isLocked(Resource.Resource1.name()));
149                 Thread.sleep(1000);
150                 try {
151                         Assert.assertFalse(lockManager.isLocked(Resource.Resource1.name()));
152                 }finally {
153                         lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
154                 }
155         }
156
157         @Test
158         public void testIsLocked_LockReleased() throws LockException, InterruptedException {
159                 boolean lockRes1 = lockManager.acquireLock(Resource.Resource1.name(), Owner.A.name(), 50);
160                 lockManager.releaseLock(Resource.Resource1.name(), Owner.A.name());
161                 Assert.assertFalse(lockManager.isLocked(Resource.Resource1.name()));
162         }
163
164         @Test
165         public void testIsLocked_NoLock() throws LockException, InterruptedException {
166                 Assert.assertFalse(lockManager.isLocked(Resource.Resource1.name()));
167         }
168 }