Fixing Blueprint Typo's and docs
[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 kotlin.test.assertEquals
28
29 class BluePrintClusterExtensionsTest {
30
31     private lateinit var clusterLockMock: ClusterLock
32
33     @Before
34     fun setup() {
35         clusterLockMock = mockk()
36         every { clusterLockMock.name() } returns "mock-lock"
37     }
38
39     @Test
40     fun `executeWithLock - should call unlock and return block result`() {
41         runBlocking {
42             every { runBlocking { clusterLockMock.tryLock(more(0L)) } } returns true
43             every { runBlocking { clusterLockMock.unLock() } } returns Unit
44
45             val result = clusterLockMock.executeWithLock(1_000) { "result" }
46
47             verify { runBlocking { clusterLockMock.unLock() } }
48             assertEquals("result", result)
49         }
50     }
51
52     @Test
53     fun `executeWithLock - should call unlock even when block throws exception`() {
54         runBlocking {
55             every { runBlocking { clusterLockMock.tryLock(more(0L)) } } returns true
56             every { runBlocking { clusterLockMock.unLock() } } returns Unit
57
58             try {
59                 clusterLockMock.executeWithLock(1_000) { throw RuntimeException("It crashed") }
60             } catch (e: Exception) {
61             }
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 }