From 94b3afa1a92d3b142dbf29810cfc9a0d49a362bd Mon Sep 17 00:00:00 2001 From: "aravind.est" Date: Mon, 31 Jul 2023 16:13:12 +0100 Subject: [PATCH] Update response format of get all rapps endpoint Use ConcurrentMapCacheManager as default cachemanager. Update getrApps endpoint to list array of rapps. Issue-ID: NONRTRIC-900 Signed-off-by: aravind.est Change-Id: I82b0be013322abbd835d9313d539919bc954fdf3 --- .../main/java/com/oransc/rappmanager/BeanConfiguration.java | 7 +++++++ .../java/com/oransc/rappmanager/rest/RappController.java | 4 ++-- .../com/oransc/rappmanager/rest/RappControllerTest.java | 10 +++++++++- .../oransc/rappmanager/models/cache/RappCacheService.java | 13 +++++++++++-- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/rapp-manager-application/src/main/java/com/oransc/rappmanager/BeanConfiguration.java b/rapp-manager-application/src/main/java/com/oransc/rappmanager/BeanConfiguration.java index 0fd2dfa..be88202 100755 --- a/rapp-manager-application/src/main/java/com/oransc/rappmanager/BeanConfiguration.java +++ b/rapp-manager-application/src/main/java/com/oransc/rappmanager/BeanConfiguration.java @@ -28,6 +28,8 @@ import com.oransc.rappmanager.sme.provider.rest.DefaultApiClient; import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.cache.CacheManager; +import org.springframework.cache.concurrent.ConcurrentMapCacheManager; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.client.RestTemplate; @@ -107,4 +109,9 @@ public class BeanConfiguration { return new com.oransc.rappmanager.sme.invoker.rest.DefaultApiClient(apiClient); } + @Bean + public CacheManager cacheManager() { + return new ConcurrentMapCacheManager(); + } + } diff --git a/rapp-manager-application/src/main/java/com/oransc/rappmanager/rest/RappController.java b/rapp-manager-application/src/main/java/com/oransc/rappmanager/rest/RappController.java index 7bef1f1..5376c07 100755 --- a/rapp-manager-application/src/main/java/com/oransc/rappmanager/rest/RappController.java +++ b/rapp-manager-application/src/main/java/com/oransc/rappmanager/rest/RappController.java @@ -30,11 +30,11 @@ import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; +import java.util.Collection; import java.util.Optional; import lombok.RequiredArgsConstructor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.cache.Cache; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; @@ -59,7 +59,7 @@ public class RappController { private final RappService rappService; @GetMapping - public ResponseEntity getRapps() { + public ResponseEntity> getRapps() { return ResponseEntity.ok(rappCacheService.getAllRapp()); } diff --git a/rapp-manager-application/src/test/java/com/oransc/rappmanager/rest/RappControllerTest.java b/rapp-manager-application/src/test/java/com/oransc/rappmanager/rest/RappControllerTest.java index 896eef3..c350d98 100755 --- a/rapp-manager-application/src/test/java/com/oransc/rappmanager/rest/RappControllerTest.java +++ b/rapp-manager-application/src/test/java/com/oransc/rappmanager/rest/RappControllerTest.java @@ -1,5 +1,6 @@ package com.oransc.rappmanager.rest; +import static org.hamcrest.Matchers.hasSize; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.when; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; @@ -50,7 +51,14 @@ public class RappControllerTest { @Test void testGetRapps() throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk()); + mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk()) + .andExpect(jsonPath("$", hasSize(0))); + UUID rappId = UUID.randomUUID(); + Rapp rapp = Rapp.builder().rappId(rappId).name(String.valueOf(rappId)).packageName(validRappFile) + .packageLocation(validCsarFileLocation).state(RappState.COMMISSIONED).build(); + rappCacheService.putRapp(rapp); + mockMvc.perform(MockMvcRequestBuilders.get("/rapps")).andExpect(status().isOk()) + .andExpect(jsonPath("$", hasSize(1))); } @Test diff --git a/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/cache/RappCacheService.java b/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/cache/RappCacheService.java index ec7d7b2..49a6dfc 100755 --- a/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/cache/RappCacheService.java +++ b/rapp-manager-models/src/main/java/com/oransc/rappmanager/models/cache/RappCacheService.java @@ -19,6 +19,10 @@ package com.oransc.rappmanager.models.cache; import com.oransc.rappmanager.models.rapp.Rapp; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; import java.util.Optional; import lombok.RequiredArgsConstructor; import org.springframework.cache.Cache; @@ -32,8 +36,13 @@ public class RappCacheService { private final String RAPP_CACHE = "rapp-cache"; private final CacheManager cacheManager; - public Cache getAllRapp() { - return cacheManager.getCache(RAPP_CACHE); + public Collection getAllRapp() { + Cache cache = cacheManager.getCache(RAPP_CACHE); + if (cache != null) { + Map nativeCache = (Map) cache.getNativeCache(); + return nativeCache.values(); + } + return List.of(); } public Optional getRapp(String rappId) { -- 2.16.6