Resource endpoint should support occurrences 29/129329/1
authorKuldip Rai <kuldip.rai@bell.ca>
Wed, 16 Mar 2022 14:26:30 +0000 (14:26 +0000)
committerkuldipr <kuldip.rai@amdocs.com>
Thu, 19 May 2022 15:52:25 +0000 (11:52 -0400)
The getOneFromResolutionKey endpoint would fail if there are multiple
occurrences for a resource. Instead it should return the last occurrence.

Issue-ID: CCSDK-3664
Signed-off-by: kuldipr <kuldip.rai@amdocs.com>
Change-Id: I1468c41c164f64931ce719f9908b935baae6e1a4

ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBService.kt
ms/blueprintsprocessor/functions/resource-resolution/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionRepository.kt
ms/blueprintsprocessor/functions/resource-resolution/src/test/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/functions/resource/resolution/db/ResourceResolutionDBServiceTest.kt
ms/blueprintsprocessor/modules/inbounds/resource-api/src/main/kotlin/org/onap/ccsdk/cds/blueprintsprocessor/resource/api/ResourceController.kt

index 5958c78..bfe9da3 100644 (file)
@@ -90,7 +90,7 @@ class ResourceResolutionDBService(private val resourceResolutionRepository: Reso
         artifactPrefix: String,
         resolutionKey: String,
         name: String
-    ): ResourceResolution = withContext(Dispatchers.IO) {
+    ): ResourceResolution? = withContext(Dispatchers.IO) {
 
         resourceResolutionRepository.findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
             resolutionKey,
index c2d630e..6e0ed3a 100644 (file)
 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
 
 import org.springframework.data.jpa.repository.JpaRepository
+import org.springframework.data.jpa.repository.Query
+import org.springframework.data.repository.query.Param
 import org.springframework.stereotype.Repository
 import javax.transaction.Transactional
 
 @Repository
 interface ResourceResolutionRepository : JpaRepository<ResourceResolution, String> {
 
+    @Query(
+        value = "SELECT * FROM RESOURCE_RESOLUTION WHERE resolution_key = :key AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion AND artifact_name = :artifactName AND name = :name ORDER BY occurrence DESC, creation_date DESC LIMIT 1",
+        nativeQuery = true
+    )
     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndName(
-        key: String,
-        blueprintName: String?,
-        blueprintVersion: String?,
-        artifactName: String,
-        name: String
-    ): ResourceResolution
+        @Param("key")key: String,
+        @Param("blueprintName")blueprintName: String,
+        @Param("blueprintVersion")blueprintVersion: String,
+        @Param("artifactName")artifactName: String,
+        @Param("name")name: String
+    ): ResourceResolution?
 
     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
         resolutionKey: String,
index fa59876..635ce0e 100644 (file)
@@ -30,6 +30,7 @@ import org.onap.ccsdk.cds.controllerblueprints.core.service.DefaultBluePrintRunt
 import org.onap.ccsdk.cds.controllerblueprints.resource.dict.ResourceAssignment
 import org.springframework.dao.EmptyResultDataAccessException
 import kotlin.test.assertEquals
+import kotlin.test.assertNotEquals
 
 open class ResourceResolutionDBServiceTest {
 
@@ -158,9 +159,11 @@ open class ResourceResolutionDBServiceTest {
                 resourceResolutionDBService.readValue(
                     blueprintName, blueprintVersion, artifactPrefix, resolutionKey, "bob"
                 )
-
-            assertEquals(rr.name, res.name)
-            assertEquals(rr.value, res.value)
+            assertNotEquals(res, null, "resource resolution failed")
+            if (res != null) {
+                assertEquals(rr.name, res.name)
+                assertEquals(rr.value, res.value)
+            }
         }
     }
 
index 15c27a4..4a2a559 100644 (file)
@@ -26,6 +26,8 @@ import org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db.R
 import org.onap.ccsdk.cds.controllerblueprints.core.httpProcessorException
 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
 import org.onap.ccsdk.cds.error.catalog.core.ErrorCatalogCodes
+import org.onap.ccsdk.cds.error.catalog.core.ErrorPayload
+import org.springframework.http.HttpStatus
 import org.springframework.http.MediaType
 import org.springframework.http.ResponseEntity
 import org.springframework.security.access.prepost.PreAuthorize
@@ -161,9 +163,14 @@ open class ResourceController(private var resourceResolutionDBService: ResourceR
         @ApiParam(value = "Name of the resource to retrieve", required = true)
         @RequestParam(value = "name", required = true) name: String
     ):
-        ResponseEntity<ResourceResolution> = runBlocking {
+        ResponseEntity<out Any>? = runBlocking {
 
-            ResponseEntity.ok()
-                .body(resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name))
+            var result: ResourceResolution? = resourceResolutionDBService.readValue(bpName, bpVersion, artifactName, resolutionKey, name)
+            if (result != null) {
+                ResponseEntity.ok().body(result)
+            } else {
+                var errorPayload = ErrorPayload(HttpStatus.NOT_FOUND.value(), ErrorCatalogCodes.GENERIC_FAILURE, "No records found")
+                ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorPayload)
+            }
         }
 }