Update response format of get all rapps endpoint 35/11535/1
authoraravind.est <aravindhan.a@est.tech>
Mon, 31 Jul 2023 15:13:12 +0000 (16:13 +0100)
committeraravind.est <aravindhan.a@est.tech>
Mon, 31 Jul 2023 15:13:12 +0000 (16:13 +0100)
Use ConcurrentMapCacheManager as default cachemanager.
Update getrApps endpoint to list array of rapps.

Issue-ID: NONRTRIC-900
Signed-off-by: aravind.est <aravindhan.a@est.tech>
Change-Id: I82b0be013322abbd835d9313d539919bc954fdf3

rapp-manager-application/src/main/java/com/oransc/rappmanager/BeanConfiguration.java
rapp-manager-application/src/main/java/com/oransc/rappmanager/rest/RappController.java
rapp-manager-application/src/test/java/com/oransc/rappmanager/rest/RappControllerTest.java
rapp-manager-models/src/main/java/com/oransc/rappmanager/models/cache/RappCacheService.java

index 0fd2dfa..be88202 100755 (executable)
@@ -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();
+    }
+
 }
index 7bef1f1..5376c07 100755 (executable)
@@ -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<Cache> getRapps() {
+    public ResponseEntity<Collection<Rapp>> getRapps() {
         return ResponseEntity.ok(rappCacheService.getAllRapp());
     }
 
index 896eef3..c350d98 100755 (executable)
@@ -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
index ec7d7b2..49a6dfc 100755 (executable)
 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<Rapp> getAllRapp() {
+        Cache cache = cacheManager.getCache(RAPP_CACHE);
+        if (cache != null) {
+            Map<String, Rapp> nativeCache = (Map<String, Rapp>) cache.getNativeCache();
+            return nativeCache.values();
+        }
+        return List.of();
     }
 
     public Optional<Rapp> getRapp(String rappId) {