18ee8c9dc95b093c4202c6f1ff3ac8818176ee9e
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / config-snapshots / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / config / snapshots / db / ResourceConfigSnapshotServiceTest.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.functions.config.snapshots.db
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.Test
24 import kotlin.test.assertEquals
25
26 class ResourceConfigSnapshotServiceTest {
27
28     private val cfgRepository = mockk<ResourceConfigSnapshotRepository>()
29
30     private val cfgService = ResourceConfigSnapshotService(cfgRepository)
31
32     private val resourceId = "1"
33     private val resourceType = "PNF"
34     private val configSnapshot = "config_snapshot"
35     private val resourceStatus = ResourceConfigSnapshot.Status.RUNNING
36
37     @Test
38     fun findByResourceIdAndResourceTypeTest() {
39         val tr = ResourceConfigSnapshot()
40         tr.config_snapshot = "res"
41         runBlocking {
42             every {
43                 cfgRepository.findByResourceIdAndResourceTypeAndStatus(any(), any(), any())
44             } returns tr
45             val res = cfgService.findByResourceIdAndResourceTypeAndStatus(resourceId, resourceType)
46             assertEquals(tr.config_snapshot, res)
47         }
48     }
49
50     @Test
51     fun createNewResourceConfigSnapshotTest() {
52         val tr = ResourceConfigSnapshot()
53         runBlocking {
54             every { cfgRepository.saveAndFlush(any<ResourceConfigSnapshot>()) } returns tr
55             every {
56                 cfgRepository.findByResourceIdAndResourceTypeAndStatus(any(), any(), any())
57             } returns null
58             val res = cfgService.write( configSnapshot, resourceId, resourceType, resourceStatus)
59             assertEquals(tr, res)
60         }
61     }
62
63     @Test
64     fun updateExistingResourceConfigSnapshotTest() {
65         val tr = ResourceConfigSnapshot()
66         runBlocking {
67             every { cfgRepository.saveAndFlush(any<ResourceConfigSnapshot>()) } returns tr
68             every {
69                 cfgRepository.findByResourceIdAndResourceTypeAndStatus(any(), any(), any())
70             } returns tr
71             every {
72                 cfgRepository.deleteByResourceIdAndResourceTypeAndStatus(any(), any(), any())
73             } returns Unit
74             val res = cfgService.write( configSnapshot, resourceId, resourceType)
75             verify {
76                 cfgRepository.deleteByResourceIdAndResourceTypeAndStatus(eq(resourceId), eq(resourceType), eq(resourceStatus))
77             }
78             assertEquals(tr, res)
79         }
80     }
81 }