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