From 1701a2a802fc0288b63860a69561be2dee03694d Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Thu, 11 May 2023 17:14:37 +0530 Subject: [PATCH 1/2] Notification testing using cache --- src/main/java/com/uci/utils/BotService.java | 2 ++ .../java/com/uci/utils/cache/service/RedisCacheService.java | 1 + .../java/com/uci/utils/cache/service/RedisCacheServiceTest.java | 2 +- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/uci/utils/BotService.java b/src/main/java/com/uci/utils/BotService.java index acddb83..8e0e92e 100644 --- a/src/main/java/com/uci/utils/BotService.java +++ b/src/main/java/com/uci/utils/BotService.java @@ -315,6 +315,7 @@ public URI apply(UriBuilder builder) { }).retrieve().bodyToMono(String.class).map(new Function() { @Override public JsonNode apply(String response) { + log.info("Call getAdapterByID cache key : "+ cacheKey + " cache data : "+cache.getIfPresent(cacheKey)); if (response != null) { ObjectMapper mapper = new ObjectMapper(); try { @@ -423,6 +424,7 @@ public Mono getVaultCredentials(String secretKey) { httpHeaders.set("admin-token", adminToken); }) .retrieve().bodyToMono(String.class).map(response -> { + log.info("Call getVaultCredentials cache key : "+ cacheKey +" cache data : " + cache.getIfPresent(cacheKey)); if (response != null) { ObjectMapper mapper = new ObjectMapper(); try { diff --git a/src/main/java/com/uci/utils/cache/service/RedisCacheService.java b/src/main/java/com/uci/utils/cache/service/RedisCacheService.java index 9c23a43..faf88b5 100644 --- a/src/main/java/com/uci/utils/cache/service/RedisCacheService.java +++ b/src/main/java/com/uci/utils/cache/service/RedisCacheService.java @@ -22,6 +22,7 @@ @Service @Getter +@AllArgsConstructor @Setter @Slf4j public class RedisCacheService { diff --git a/src/test/java/com/uci/utils/cache/service/RedisCacheServiceTest.java b/src/test/java/com/uci/utils/cache/service/RedisCacheServiceTest.java index 934936e..0050401 100644 --- a/src/test/java/com/uci/utils/cache/service/RedisCacheServiceTest.java +++ b/src/test/java/com/uci/utils/cache/service/RedisCacheServiceTest.java @@ -24,7 +24,7 @@ class RedisCacheServiceTest { @BeforeEach void init(){ RedisTemplate obj = new RedisTemplate<>(); - redisCacheService = Mockito.spy(new RedisCacheService()); + redisCacheService = Mockito.spy(new RedisCacheService(obj)); Mockito.doReturn(obj).when(redisCacheService).getCache(anyString()); Mockito.doNothing().when(redisCacheService).setCache(anyString(), Mockito.any()); Mockito.doNothing().when(redisCacheService).deleteCache(anyString()); From 5b0da0ac89da36262bf916a6c445367d2ff62189 Mon Sep 17 00:00:00 2001 From: pankajjangid05 Date: Mon, 15 May 2023 02:07:07 +0530 Subject: [PATCH 2/2] Added logs in BotService for cache and uci-api response --- src/main/java/com/uci/utils/BotService.java | 95 +++++++++++---------- 1 file changed, 50 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/uci/utils/BotService.java b/src/main/java/com/uci/utils/BotService.java index 8e0e92e..0c35b24 100644 --- a/src/main/java/com/uci/utils/BotService.java +++ b/src/main/java/com/uci/utils/BotService.java @@ -63,7 +63,7 @@ public Mono getBotNodeFromStartingMessage(String startingMessage) { .build()) .retrieve().bodyToMono(String.class).map(response -> { if (response != null) { - log.info(response); + log.info("Call getBotNodeFromStartingMessage : " + response + " cache : " + cache.getIfPresent(cacheKey)); try { ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(response); @@ -106,7 +106,7 @@ public Mono getBotNodeFromName(String botName) { .build()) .retrieve().bodyToMono(String.class).map(response -> { if (response != null) { - log.info(response); + log.info("Call getBotNodeFromName : " + response + " cache : " + cache.getIfPresent(cacheKey)); try { ObjectMapper mapper = new ObjectMapper(); JsonNode root = mapper.readTree(response); @@ -146,6 +146,7 @@ public Mono getBotNodeFromId(String botId) { .bodyToMono(String.class) .map(response -> { if (response != null) { + log.info("Call getBotNodeFromId : " + response + " cache : " + cache.getIfPresent(cacheKey)); ObjectMapper mapper = new ObjectMapper(); try { JsonNode root = mapper.readTree(response); @@ -165,48 +166,49 @@ public Mono getBotNodeFromId(String botId) { .log("cache"); } - /** - * Get Adapter id by bot name - * @param botName - * @return - */ - public Mono getAdapterIdFromBotName(String botName) { - String cacheKey = "valid-adpater-from-bot-name: " + botName; - return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(cacheKey) != null ? cache.getIfPresent(key).toString() : null) - .map(Signal::next), cacheKey) - .onCacheMissResume(() -> webClient.get() - .uri(builder -> builder.path("admin/bot/search/internal") - .queryParam("perPage", 5) - .queryParam("page", 1) - .queryParam("match", true) - .queryParam("name", botName) - .build()) - .retrieve() - .bodyToMono(String.class).map(response -> { - if (response != null) { - ObjectMapper mapper = new ObjectMapper(); - try { - JsonNode root = mapper.readTree(response); - if(root.path("result") != null && root.path("result").get(0) != null - && !root.path("result").get(0).isEmpty()) { - JsonNode botNode = root.path("result").get(0); - return BotUtil.getBotNodeAdapterId(botNode); - } - return null; - } catch (JsonProcessingException jsonMappingException) { - return null; - } - - } else { - } - return null; - }) - .doOnError(throwable -> log.info("Error in getting adpater: " + throwable.getMessage())) - .onErrorReturn("")) - .andWriteWith((key, signal) -> Mono.fromRunnable( - () -> Optional.ofNullable(signal.get()).ifPresent(value -> cache.put(key, value)))) - .log("cache"); - } +// /** +// * Get Adapter id by bot name +// * @param botName +// * @return +// */ +// public Mono getAdapterIdFromBotName(String botName) { +// String cacheKey = "valid-adpater-from-bot-name: " + botName; +// return CacheMono.lookup(key -> Mono.justOrEmpty(cache.getIfPresent(cacheKey) != null ? cache.getIfPresent(key).toString() : null) +// .map(Signal::next), cacheKey) +// .onCacheMissResume(() -> webClient.get() +// .uri(builder -> builder.path("admin/bot/search/internal") +// .queryParam("perPage", 5) +// .queryParam("page", 1) +// .queryParam("match", true) +// .queryParam("name", botName) +// .build()) +// .retrieve() +// .bodyToMono(String.class).map(response -> { +// if (response != null) { +// log.info("Call getAdapterIdFromBotName : " + response); +// ObjectMapper mapper = new ObjectMapper(); +// try { +// JsonNode root = mapper.readTree(response); +// if(root.path("result") != null && root.path("result").get(0) != null +// && !root.path("result").get(0).isEmpty()) { +// JsonNode botNode = root.path("result").get(0); +// return BotUtil.getBotNodeAdapterId(botNode); +// } +// return null; +// } catch (JsonProcessingException jsonMappingException) { +// return null; +// } +// +// } else { +// } +// return null; +// }) +// .doOnError(throwable -> log.info("Error in getting adpater: " + throwable.getMessage())) +// .onErrorReturn("")) +// .andWriteWith((key, signal) -> Mono.fromRunnable( +// () -> Optional.ofNullable(signal.get()).ifPresent(value -> cache.put(key, value)))) +// .log("cache"); +// } /** * Retrieve bot id from bot name (from validated bot) @@ -227,6 +229,7 @@ public Mono getBotIdFromBotName(String botName) { @Override public String apply(String response) { if (response != null) { + log.info("Call getBotIdFromBotName : " + response + " cache : " + cache.getIfPresent(cacheKey)); ObjectMapper mapper = new ObjectMapper(); try { JsonNode root = mapper.readTree(response); @@ -350,7 +353,7 @@ public Mono getAdapterCredentials(String adapterID) { return getAdapterByID(adapterID).map(new Function>() { @Override public Mono apply(JsonNode adapter) { - log.info("adapter: "+adapter); + log.info("getAdapterByID : "+adapter); if(adapter != null) { String vaultKey; try{ @@ -464,6 +467,7 @@ public Mono getFirstFormByBotID(String botId) { @Override public String apply(String response) { if (response != null) { + log.info("Call getFirstFormByBotID : " + response + " cache : " + cache.getIfPresent(cacheKey)); ObjectMapper mapper = new ObjectMapper(); try { JsonNode root = mapper.readTree(response); @@ -505,6 +509,7 @@ public Mono getBotNameByBotID(String botId) { @Override public String apply(String response) { if (response != null) { + log.info("Call getBotNameByBotID : " + response + " cache : " + cache.getIfPresent(cacheKey)); ObjectMapper mapper = new ObjectMapper(); try { JsonNode root = mapper.readTree(response);