5f1d0957813f7b80c0051b8ea74b2e76596daf90
[ccsdk/cds.git] /
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 ctx = bluePrintRuntimeService.bluePrintContext()
106             val metadata = ctx.metadata!!
107             metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
108
109             blueprintsProcessorCatalogService.save(metadata, file, ctx.workflows()!!)
110         }
111     }
112
113     @Test
114     fun `test get function`() {
115         runBlocking {
116             val file = normalizedFile("./target/blueprints/generated-cba.zip")
117             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
118
119             val ctx = bluePrintRuntimeService.bluePrintContext()
120             val metadata = ctx.metadata!!
121             metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
122
123             blueprintsProcessorCatalogService.save(metadata, file, ctx.workflows()!!)
124             blueprintsProcessorCatalogService.get("baseconfiguration", "1.0.0", true)
125         }
126
127         assertTrue(
128             File(
129                 blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath +
130                     "/baseconfiguration"
131             ).deleteRecursively(),
132             "Couldn't get blueprint archive " +
133                 "${blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath}/baseconfiguration " +
134                 "from data base."
135         )
136     }
137
138     @Test
139     fun `test delete function`() {
140         runBlocking {
141             blueprintsProcessorCatalogService.delete("baseconfiguration", "1.0.0")
142         }
143     }
144 }