Metadata for name, version, tags and type
[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.compress
29 import org.onap.ccsdk.cds.controllerblueprints.core.deleteDir
30 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
31 import org.onap.ccsdk.cds.controllerblueprints.core.service.BluePrintRuntimeService
32 import org.onap.ccsdk.cds.controllerblueprints.core.utils.BluePrintMetadataUtils
33 import org.springframework.beans.factory.annotation.Autowired
34 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
35 import org.springframework.context.annotation.ComponentScan
36 import org.springframework.test.context.ContextConfiguration
37 import org.springframework.test.context.TestPropertySource
38 import org.springframework.test.context.junit4.SpringRunner
39 import java.io.File
40 import kotlin.test.AfterTest
41 import kotlin.test.BeforeTest
42 import kotlin.test.assertTrue
43
44 @RunWith(SpringRunner::class)
45 @EnableAutoConfiguration
46 @ComponentScan(basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor"])
47 @ContextConfiguration(
48     classes = [BlueprintProcessorCatalogServiceImpl::class, BluePrintCoreConfiguration::class,
49         MockBlueprintProcessorCatalogServiceImpl::class]
50 )
51 @TestPropertySource(locations = ["classpath:application-test.properties"])
52 class BlueprintProcessorCatalogServiceImplTest {
53
54     @Autowired
55     lateinit var blueprintsProcessorCatalogService: BlueprintCatalogServiceImpl
56
57     @Autowired
58     lateinit var blueprintCoreConfiguration: BluePrintCoreConfiguration
59
60     private lateinit var bluePrintRuntimeService: BluePrintRuntimeService<*>
61
62     private val blueprintId = "1234"
63
64     @BeforeTest
65     fun setup() {
66
67         deleteDir("target", "blueprints")
68
69         // Create sample CBA zip
70         normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
71             .compress(normalizedFile("./target/blueprints/generated-cba.zip"))
72
73         bluePrintRuntimeService = BluePrintMetadataUtils.bluePrintRuntime(
74             blueprintId,
75             "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
76         )
77     }
78
79     @AfterTest
80     fun cleanDir() {
81         deleteDir("target", "blueprints")
82     }
83
84     @Test
85     fun `test catalog service`() {
86
87         runBlocking {
88             val file = normalizedFile("./target/blueprints/generated-cba.zip")
89             assertTrue(file.exists(), "couldn't get file ${file.absolutePath}")
90
91             blueprintsProcessorCatalogService.saveToDatabase("1234", file)
92             blueprintsProcessorCatalogService.getFromDatabase("baseconfiguration", "1.0.0")
93
94             blueprintsProcessorCatalogService.deleteFromDatabase("baseconfiguration", "1.0.0")
95         }
96     }
97
98     @Test
99     fun `test save function`() {
100         runBlocking {
101             val file = normalizedFile("./target/blueprints/generated-cba.zip")
102             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
103             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
104             metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
105
106             blueprintsProcessorCatalogService.save(metadata, file)
107         }
108     }
109
110     @Test
111     fun `test get function`() {
112         runBlocking {
113             val file = normalizedFile("./target/blueprints/generated-cba.zip")
114             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
115             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
116             metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
117
118             blueprintsProcessorCatalogService.save(metadata, file)
119             blueprintsProcessorCatalogService.get("baseconfiguration", "1.0.0", true)
120         }
121
122         assertTrue(
123             File(
124                 blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath +
125                     "/baseconfiguration"
126             ).deleteRecursively(), "Couldn't get blueprint archive " +
127                 "${blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath}/baseconfiguration " +
128                 "from data base."
129         )
130     }
131
132     @Test
133     fun `test delete function`() {
134         runBlocking {
135             blueprintsProcessorCatalogService.delete("baseconfiguration", "1.0.0")
136         }
137     }
138 }