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