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