7ef4eb49b4b632c50a1aeeceb5a307bc71e6b24f
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / processor-core / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / core / cluster / BluePrintClusterExtensionsTest.kt
1 /*
2  * Copyright © 2019 Bell Canada.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.core.cluster
18
19 import io.mockk.every
20 import io.mockk.mockk
21 import io.mockk.verify
22 import kotlinx.coroutines.runBlocking
23 import org.junit.Before
24 import org.junit.Test
25 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.ClusterLock
26 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
27 import java.lang.RuntimeException
28 import kotlin.test.assertEquals
29
30 class BluePrintClusterExtensionsTest {
31
32     private lateinit var clusterLockMock: ClusterLock
33
34     @Before
35     fun setup() {
36         clusterLockMock = mockk()
37         every { clusterLockMock.name() } returns "mock-lock"
38     }
39
40     @Test
41     fun `executeWithLock - should call unlock and return block result`() {
42         runBlocking {
43             every { runBlocking { clusterLockMock.tryLock(more(0L)) } } returns true
44             every { runBlocking { clusterLockMock.unLock() } } returns Unit
45
46             val result = clusterLockMock.executeWithLock(1_000) { "result" }
47
48             verify { runBlocking { clusterLockMock.unLock() } }
49             assertEquals("result", result)
50         }
51     }
52
53     @Test
54     fun `executeWithLock - should call unlock even when block throws exception`() {
55         runBlocking {
56             every { runBlocking { clusterLockMock.tryLock(more(0L)) } } returns true
57             every { runBlocking { clusterLockMock.unLock() } } returns Unit
58
59             try {
60                 clusterLockMock.executeWithLock(1_000) { throw RuntimeException("It crashed") }
61             } catch (e: Exception) { }
62
63             verify { runBlocking { clusterLockMock.unLock() } }
64         }
65     }
66
67     @Test(expected = BluePrintException::class)
68     fun `executeWithLock - should throw exception when lock was not acquired within timeout`() {
69         runBlocking {
70             every { runBlocking { clusterLockMock.tryLock(eq(0L)) } } returns false
71             clusterLockMock.executeWithLock(0) { "Will not run" }
72         }
73     }
74 }