2830cb547a01259d55c3737834481caefbddc200
[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(expected = NoSuchElementException::class)
52     fun notFoundEntryReturnsExceptionTest() {
53         val tr = ResourceConfigSnapshot()
54         runBlocking {
55             every {
56                 cfgRepository.findByResourceIdAndResourceTypeAndStatus(any(), any(), any())
57             } returns tr
58             val snap = cfgService.findByResourceIdAndResourceTypeAndStatus("MISSING_ID", "UNKNOWN_TYPE")
59             assertTrue ( snap.isBlank(), "Not found but returned a non empty string" )
60         }
61     }
62
63     @Test
64     fun createNewResourceConfigSnapshotTest() {
65         val tr = ResourceConfigSnapshot()
66         runBlocking {
67             every { cfgRepository.saveAndFlush(any<ResourceConfigSnapshot>()) } returns tr
68             every {
69                 cfgRepository.findByResourceIdAndResourceTypeAndStatus(any(), any(), any())
70             } returns null
71             val res = cfgService.write( configSnapshot, resourceId, resourceType, resourceStatus)
72             assertEquals(tr, res)
73         }
74     }
75
76     @Test
77     fun updateExistingResourceConfigSnapshotTest() {
78         val tr = ResourceConfigSnapshot()
79         runBlocking {
80             every { cfgRepository.saveAndFlush(any<ResourceConfigSnapshot>()) } returns tr
81             every {
82                 cfgRepository.findByResourceIdAndResourceTypeAndStatus(any(), any(), any())
83             } returns tr
84             every {
85                 cfgRepository.deleteByResourceIdAndResourceTypeAndStatus(any(), any(), any())
86             } returns Unit
87             val res = cfgService.write( configSnapshot, resourceId, resourceType)
88             verify {
89                 cfgRepository.deleteByResourceIdAndResourceTypeAndStatus(eq(resourceId), eq(resourceType), eq(resourceStatus))
90             }
91             assertEquals(tr, res)
92         }
93     }
94 }