Renaming Files having BluePrint to have Blueprint
[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 = [
49         BlueprintProcessorCatalogServiceImpl::class, BlueprintCoreConfiguration::class,
50         MockBlueprintProcessorCatalogServiceImpl::class
51     ]
52 )
53 @TestPropertySource(locations = ["classpath:application-test.properties"])
54 class BlueprintProcessorCatalogServiceImplTest {
55
56     @Autowired
57     lateinit var blueprintsProcessorCatalogService: BlueprintCatalogServiceImpl
58
59     @Autowired
60     lateinit var blueprintCoreConfiguration: BlueprintCoreConfiguration
61
62     private lateinit var bluePrintRuntimeService: BlueprintRuntimeService<*>
63
64     private val blueprintId = "1234"
65
66     @BeforeTest
67     fun setup() {
68
69         deleteDir("target", "blueprints")
70
71         // Create sample CBA zip
72         normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration")
73             .compress(normalizedFile("./target/blueprints/generated-cba.zip"))
74
75         bluePrintRuntimeService = BlueprintMetadataUtils.bluePrintRuntime(
76             blueprintId,
77             "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
78         )
79     }
80
81     @AfterTest
82     fun cleanDir() {
83         deleteDir("target", "blueprints")
84     }
85
86     @Test
87     fun `test catalog service`() {
88
89         runBlocking {
90             val file = normalizedFile("./target/blueprints/generated-cba.zip")
91             assertTrue(file.exists(), "couldn't get file ${file.absolutePath}")
92
93             blueprintsProcessorCatalogService.saveToDatabase("1234", file)
94             blueprintsProcessorCatalogService.getFromDatabase("baseconfiguration", "1.0.0")
95
96             blueprintsProcessorCatalogService.deleteFromDatabase("baseconfiguration", "1.0.0")
97         }
98     }
99
100     @Test
101     fun `test save function`() {
102         runBlocking {
103             val file = normalizedFile("./target/blueprints/generated-cba.zip")
104             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
105             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
106             metadata[BlueprintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
107
108             blueprintsProcessorCatalogService.save(metadata, file)
109         }
110     }
111
112     @Test
113     fun `test get function`() {
114         runBlocking {
115             val file = normalizedFile("./target/blueprints/generated-cba.zip")
116             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
117             val metadata = bluePrintRuntimeService.bluePrintContext().metadata!!
118             metadata[BlueprintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
119
120             blueprintsProcessorCatalogService.save(metadata, file)
121             blueprintsProcessorCatalogService.get("baseconfiguration", "1.0.0", true)
122         }
123
124         assertTrue(
125             File(
126                 blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath +
127                     "/baseconfiguration"
128             ).deleteRecursively(),
129             "Couldn't get blueprint archive " +
130                 "${blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath}/baseconfiguration " +
131                 "from data base."
132         )
133     }
134
135     @Test
136     fun `test delete function`() {
137         runBlocking {
138             blueprintsProcessorCatalogService.delete("baseconfiguration", "1.0.0")
139         }
140     }
141 }