1e5dc283ca56a5cbf4f41f9a91437566b54e2fac
[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         // Create sample CBA zip without workflows
76         normalizedFile("./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration-no-workflows")
77             .compress(normalizedFile("./target/blueprints/generated-cba-no-workflows.zip"))
78
79         bluePrintRuntimeService = BluePrintMetadataUtils.bluePrintRuntime(
80             blueprintId,
81             "./../../../../../components/model-catalog/blueprint-model/test-blueprint/baseconfiguration"
82         )
83     }
84
85     @AfterTest
86     fun cleanDir() {
87         deleteDir("target", "blueprints")
88     }
89
90     @Test
91     fun `test catalog service`() {
92
93         runBlocking {
94             val file = normalizedFile("./target/blueprints/generated-cba.zip")
95             assertTrue(file.exists(), "couldn't get file ${file.absolutePath}")
96
97             blueprintsProcessorCatalogService.saveToDatabase("1234", file)
98             blueprintsProcessorCatalogService.getFromDatabase("baseconfiguration", "1.0.0")
99
100             blueprintsProcessorCatalogService.deleteFromDatabase("baseconfiguration", "1.0.0")
101         }
102     }
103
104     @Test
105     fun `test save function`() {
106         runBlocking {
107             val file = normalizedFile("./target/blueprints/generated-cba.zip")
108             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
109             val ctx = bluePrintRuntimeService.bluePrintContext()
110             val metadata = ctx.metadata!!
111             metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
112
113             blueprintsProcessorCatalogService.save(metadata, file, ctx.workflows()!!)
114         }
115     }
116
117     @Test
118     fun `test get function`() {
119         runBlocking {
120             val file = normalizedFile("./target/blueprints/generated-cba.zip")
121             assertTrue(file.exists(), "couldnt get file ${file.absolutePath}")
122
123             val ctx = bluePrintRuntimeService.bluePrintContext()
124             val metadata = ctx.metadata!!
125             metadata[BluePrintConstants.PROPERTY_BLUEPRINT_PROCESS_ID] = blueprintId
126
127             blueprintsProcessorCatalogService.save(metadata, file, ctx.workflows()!!)
128             blueprintsProcessorCatalogService.get("baseconfiguration", "1.0.0", true)
129         }
130
131         assertTrue(
132             File(
133                 blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath +
134                     "/baseconfiguration"
135             ).deleteRecursively(),
136             "Couldn't get blueprint archive " +
137                 "${blueprintCoreConfiguration.bluePrintLoadConfiguration().blueprintArchivePath}/baseconfiguration " +
138                 "from data base."
139         )
140     }
141
142     @Test
143     fun `test save CBA without workflows`() {
144         runBlocking {
145             val file = normalizedFile("./target/blueprints/generated-cba-no-workflows.zip")
146             assertTrue(file.exists(), "couldn't get file ${file.absolutePath}")
147
148             blueprintsProcessorCatalogService.saveToDatabase("5678", file)
149             blueprintsProcessorCatalogService.getFromDatabase("baseconfiguration-no-workflows", "1.0.0")
150
151             blueprintsProcessorCatalogService.deleteFromDatabase("baseconfiguration-no-workflows", "1.0.0")
152         }
153     }
154
155     @Test
156     fun `test delete function`() {
157         runBlocking {
158             blueprintsProcessorCatalogService.delete("baseconfiguration", "1.0.0")
159         }
160     }
161 }