Merge "Add new endpoint for posting data-dictinary as ResourceDefinition"
authorBrinda Santh Muthuramalingam <brindasanth@in.ibm.com>
Wed, 8 Jan 2020 20:13:47 +0000 (20:13 +0000)
committerGerrit Code Review <gerrit@onap.org>
Wed, 8 Jan 2020 20:13:47 +0000 (20:13 +0000)
ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/ResourceDictionaryController.kt
ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/handler/ResourceDictionaryHandler.kt
ms/blueprintsprocessor/modules/inbounds/designer-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/load/ResourceDictionaryLoadService.kt
ms/blueprintsprocessor/modules/inbounds/designer-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/handler/ResourceDictionaryHandlerTest.kt [new file with mode: 0644]

index 3902079..75403d4 100644 (file)
@@ -20,6 +20,7 @@ import kotlinx.coroutines.runBlocking
 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ResourceDictionary
 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.ResourceDictionaryHandler
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceSourceMapping
 import org.springframework.http.MediaType
 import org.springframework.web.bind.annotation.DeleteMapping
@@ -49,6 +50,13 @@ open class ResourceDictionaryController(private val resourceDictionaryHandler: R
         resourceDictionaryHandler.saveResourceDictionary(dataDictionary)
     }
 
+    @PostMapping(path = ["/definition"], produces = [MediaType.APPLICATION_JSON_VALUE], consumes = [MediaType.APPLICATION_JSON_VALUE])
+    @ResponseBody
+    @Throws(BluePrintException::class)
+    fun saveResourceDictionary(@RequestBody resourceDefinition: ResourceDefinition): ResourceDefinition = runBlocking {
+        resourceDictionaryHandler.saveResourceDefinition(resourceDefinition)
+    }
+
     @DeleteMapping(path = ["/{name}"])
     fun deleteResourceDictionaryByName(@PathVariable(value = "name") name: String) = runBlocking {
         resourceDictionaryHandler.deleteResourceDictionary(name)
index d7563e8..0f0bfef 100644 (file)
@@ -24,6 +24,7 @@ import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ResourceDictio
 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ResourceDictionaryRepository
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
 import org.onap.ccsdk.cds.controllerblueprints.core.checkNotEmpty
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceSourceMapping
 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.factory.ResourceSourceMappingFactory
 import org.springframework.stereotype.Service
@@ -120,6 +121,34 @@ class ResourceDictionaryHandler(private val resourceDictionaryRepository: Resour
         return resourceDictionary
     }
 
+    /**
+     * This is a saveDataDictionary service
+     *
+     * @param resourceDefinition ResourceDefinition
+     * @return ResourceDefinition
+     */
+    @Throws(BluePrintException::class)
+    suspend fun saveResourceDefinition(resourceDefinition: ResourceDefinition): ResourceDefinition {
+        val resourceDictionary = ResourceDictionary()
+        resourceDictionary.name = resourceDefinition.name
+        resourceDictionary.updatedBy = resourceDefinition.updatedBy
+        resourceDictionary.resourceDictionaryGroup = resourceDefinition.group
+        resourceDictionary.entrySchema = resourceDefinition.property.entrySchema?.type
+        if (StringUtils.isBlank(resourceDefinition.tags)) {
+            resourceDictionary.tags = (resourceDefinition.name + ", " + resourceDefinition.updatedBy +
+                    ", " + resourceDefinition.updatedBy)
+        } else {
+            resourceDictionary.tags = resourceDefinition.tags!!
+        }
+        resourceDictionary.description = resourceDefinition.property.description!!
+        resourceDictionary.dataType = resourceDefinition.property.type
+        resourceDictionary.definition = resourceDefinition
+
+        validateResourceDictionary(resourceDictionary)
+
+        return resourceDictionaryRepository.save(resourceDictionary).definition
+    }
+
     /**
      * This is a deleteResourceDictionary service
      *
index 432ef0f..15cf3dc 100644 (file)
@@ -20,9 +20,7 @@ package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.load
 import kotlinx.coroutines.async
 import kotlinx.coroutines.awaitAll
 import kotlinx.coroutines.coroutineScope
-import org.apache.commons.lang3.StringUtils
 import org.apache.commons.lang3.text.StrBuilder
-import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ResourceDictionary
 import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler.ResourceDictionaryHandler
 import org.onap.ccsdk.cds.controllerblueprints.core.BluePrintException
 import org.onap.ccsdk.cds.controllerblueprints.core.normalizedFile
@@ -74,29 +72,7 @@ open class ResourceDictionaryLoadService(private val resourceDictionaryHandler:
             val definitionContent = file.readNBText()
             val resourceDefinition = JacksonUtils.readValue(definitionContent, ResourceDefinition::class.java)
             if (resourceDefinition != null) {
-
-                checkNotNull(resourceDefinition.property) { "Failed to get Property Definition" }
-                val resourceDictionary = ResourceDictionary()
-                resourceDictionary.name = resourceDefinition.name
-                resourceDictionary.definition = resourceDefinition
-
-                checkNotNull(resourceDefinition.property) { "Property field is missing" }
-                resourceDictionary.description = resourceDefinition.property.description!!
-                resourceDictionary.dataType = resourceDefinition.property.type
-
-                if (resourceDefinition.property.entrySchema != null) {
-                    resourceDictionary.entrySchema = resourceDefinition.property.entrySchema!!.type
-                }
-                resourceDictionary.updatedBy = resourceDefinition.updatedBy
-
-                if (StringUtils.isBlank(resourceDefinition.tags)) {
-                    resourceDictionary.tags = (resourceDefinition.name + ", " + resourceDefinition.updatedBy +
-                            ", " + resourceDefinition.updatedBy)
-                } else {
-                    resourceDictionary.tags = resourceDefinition.tags!!
-                }
-                resourceDictionaryHandler.saveResourceDictionary(resourceDictionary)
-
+                resourceDictionaryHandler.saveResourceDefinition(resourceDefinition)
                 log.trace("Resource dictionary(${file.name}) loaded successfully ")
             } else {
                 throw BluePrintException("couldn't get dictionary from content information")
diff --git a/ms/blueprintsprocessor/modules/inbounds/designer-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/handler/ResourceDictionaryHandlerTest.kt b/ms/blueprintsprocessor/modules/inbounds/designer-api/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/designer/api/handler/ResourceDictionaryHandlerTest.kt
new file mode 100644 (file)
index 0000000..db25b6c
--- /dev/null
@@ -0,0 +1,71 @@
+/*
+ * Copyright © 2019 Bell Canada.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.onap.ccsdk.cds.blueprintsprocessor.designer.api.handler
+
+import kotlinx.coroutines.runBlocking
+import org.hamcrest.Matchers.samePropertyValuesAs
+import org.junit.Assert
+import org.junit.Test
+import org.junit.runner.RunWith
+import org.mockito.ArgumentCaptor
+import org.mockito.ArgumentMatchers.any
+import org.mockito.Mockito
+import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.domain.ResourceDictionary
+import org.onap.ccsdk.cds.blueprintsprocessor.designer.api.repository.ResourceDictionaryRepository
+import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
+import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceDefinition
+import org.springframework.test.context.junit4.SpringRunner
+
+@RunWith(SpringRunner::class)
+class ResourceDictionaryHandlerTest {
+
+    private val mockRepository = Mockito.mock(ResourceDictionaryRepository::class.java)
+    private val resourceDictionaryHandler = ResourceDictionaryHandler(mockRepository)
+
+    @Test
+    fun testSaveResourceDictionary() {
+        val resourceDefinition: ResourceDefinition = JacksonUtils
+                .readValueFromFile(
+                        "./../../../../../components/model-catalog/resource-dictionary/starter-dictionary/sample-db-source.json",
+                        ResourceDefinition::class.java
+                )!!
+
+        val expectedResourceDictionary = ResourceDictionary()
+        expectedResourceDictionary.name = resourceDefinition.name
+        expectedResourceDictionary.updatedBy = resourceDefinition.updatedBy
+        expectedResourceDictionary.resourceDictionaryGroup = resourceDefinition.group
+        expectedResourceDictionary.tags = resourceDefinition.tags!!
+        expectedResourceDictionary.description = resourceDefinition.property.description!!
+        expectedResourceDictionary.dataType = resourceDefinition.property.type
+        expectedResourceDictionary.definition = resourceDefinition
+
+        // Mock save success
+        val mockReturnValue = ResourceDictionary()
+        mockReturnValue.definition = ResourceDefinition()
+        Mockito.`when`(mockRepository.save(any(ResourceDictionary::class.java)))
+                .thenReturn(mockReturnValue)
+
+        runBlocking {
+            resourceDictionaryHandler.saveResourceDefinition(resourceDefinition)
+        }
+
+        val argumentCaptor = ArgumentCaptor.forClass(ResourceDictionary::class.java)
+        Mockito.verify(mockRepository).save(argumentCaptor.capture())
+
+        Assert.assertThat(argumentCaptor.value, samePropertyValuesAs(expectedResourceDictionary))
+    }
+}