Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / db-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / db / BlueprintProcessorCatalogServiceImplTest.kt
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
4  * Copyright (C) 2019 IBM, Bell Canada.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 package org.onap.ccsdk.cds.blueprintsprocessor.db
19
20 import kotlinx.coroutines.runBlocking
21 import org.junit.Test
22 import org.junit.runner.RunWith
23 import org.onap.ccsdk.cds.blueprintsprocessor.core.BluePrintCoreConfiguration
24 import org.onap.ccsdk.cds.blueprintsprocessor.db.mock.MockBlueprintProcessorCatalogServiceImpl
25 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.service.BlueprintCatalogServiceImpl
26 import org.onap.ccsdk.cds.blueprintsprocessor.db.primary.service.BlueprintProcessorCatalogServiceImpl
27 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintConstants
28 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
29 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
30 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
31 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
32 import org.springframework.beans.factory.annotation.Autowired
33 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
34 import org.springframework.context.annotation.ComponentScan
35 import org.springframework.test.context.ContextConfiguration
36 import org.springframework.test.context.TestPropertySource
37 import org.springframework.test.context.junit4.SpringRunner
38 import java.io.File
39 import kotlin.test.AfterTest
40 import kotlin.test.BeforeTest
41 import kotlin.test.assertTrue
42
43 @RunWith(SpringRunner::class)
44 @EnableAutoConfiguration
45 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor"])
46 @ContextConfiguration(
47     classes = [BlueprintProcessorCatalogServiceImpl::class, BluePrintCoreConfiguration::class,
48         MockBlueprintProcessorCatalogServiceImpl::class]
49 )
50 @TestPropertySource(locations = ["classpath:application-test.properties"])
51 class BlueprintProcessorCatalogServiceImplTest {
52
53     @Autowired
54     lateinit var blueprintsProcessorCatalogService: BlueprintCatalogServiceImpl
55
56     @Autowired
57     lateinit var blueprintCoreConfiguration: BluePrintCoreConfiguration
58
59     private lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
60
61     private val blueprintId = "1234"
62
63     @BeforeTest
64     fun setup() {
65         deleteDir("target", "blueprints")
66         bluePrintRuntimeService = BluePrintMetadataUtils.getBluePrintRuntime(
67             blueprintId,
68             "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
69         )
70     }
71
72     @AfterTest
73     fun cleanDir() {
74         deleteDir("target", "blueprints")
75     }
76
77     @Test
78     fun `test catalog service`() {
79         // TODO: I thing this test function should be remve and replace by the other one.
80         runBlocking {
81             // FIXME("Create ZIP from test blueprints")
82
83             val file = normalizedFile("./src/test/resources/test-cba.zip")
84             assertTrue(file.exists(), "couldn't get file ${file.absolutePath}")
85
86             blueprintsProcessorCatalogService.saveToDatabase("1234", file)
87             blueprintsProcessorCatalogService.getFromDatabase("baseconfiguration", "1.0.0")
88
89             blueprintsProcessorCatalogService.deleteFromDatabase("baseconfiguration", "1.0.0")
90         }
91     }
92
93     @Test
94     fun `test save function`() {
95         runBlocking {
96             val file = normalizedFile("./src/test/resources/test-cba.zip")
97             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
98             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
99             metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
100
101             blueprintsProcessorCatalogService.save(metadata, file)
102         }
103     }
104
105     @Test
106     fun `test get function`() {
107         runBlocking {
108             val file = normalizedFile("./src/test/resources/test-cba.zip")
109             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
110             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
111             metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
112
113             blueprintsProcessorCatalogService.save(metadata, file)
114             blueprintsProcessorCatalogService.get("baseconfiguration", "1.0.0", true)
115         }
116
117         assertTrue(
118             File(
119                 blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath +
120                         "/baseconfiguration"
121             ).deleteRecursively(), "Couldn't get blueprint archive " +
122                     "${blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath}/baseconfiguration " +
123                     "from data base."
124         )
125     }
126
127     @Test
128     fun `test delete function`() {
129         runBlocking {
130             blueprintsProcessorCatalogService.delete("baseconfiguration", "1.0.0")
131         }
132     }
133 }