From 0a9216734694a68e609a01d072273bbfe4233ef8 Mon Sep 17 00:00:00 2001 From: memojja Date: Tue, 2 Nov 2021 23:18:49 +0300 Subject: [PATCH] string support for idempotency check --- Jdempotent-core/pom.xml | 2 +- .../core/aspect/IdempotentAspect.java | 24 +++++++++++-------- .../aspect/core/IdempotentTestPayload.java | 4 ---- .../aspect/withaspect/IdempotentAspectIT.java | 16 +++++++++++++ .../pom.xml | 4 ++-- Jdempotent-spring-boot-redis-starter/pom.xml | 4 ++-- examples/jdempotent-couchbase-example/pom.xml | 2 +- examples/jdempotent-redis-example/pom.xml | 2 +- .../demo/controller/MailController.java | 21 +++++++++++++++- pom.xml | 2 +- 10 files changed, 58 insertions(+), 23 deletions(-) diff --git a/Jdempotent-core/pom.xml b/Jdempotent-core/pom.xml index 664de06..d2b314d 100644 --- a/Jdempotent-core/pom.xml +++ b/Jdempotent-core/pom.xml @@ -6,7 +6,7 @@ 4.0.0 com.trendyol Jdempotent-core - 1.0.8 + 1.0.9 Jdempotent-core jar https://github.com/Trendyol/Jdempotent/tree/master/Jdempotent-core diff --git a/Jdempotent-core/src/main/java/com/trendyol/jdempotent/core/aspect/IdempotentAspect.java b/Jdempotent-core/src/main/java/com/trendyol/jdempotent/core/aspect/IdempotentAspect.java index bdcaadb..6bc6006 100644 --- a/Jdempotent-core/src/main/java/com/trendyol/jdempotent/core/aspect/IdempotentAspect.java +++ b/Jdempotent-core/src/main/java/com/trendyol/jdempotent/core/aspect/IdempotentAspect.java @@ -199,7 +199,7 @@ public IdempotentRequestWrapper findIdempotentRequestArg(ProceedingJoinPoint pjp if (args.length == 0) { throw new IllegalStateException("Idempotent method not found"); } else if (args.length == 1) { - return new IdempotentRequestWrapper(getIdempotentNonIgnorableWrapper(args)); + return new IdempotentRequestWrapper(getIdempotentNonIgnorableWrapper(args[0])); } else { try { MethodSignature signature = (MethodSignature) pjp.getSignature(); @@ -210,7 +210,7 @@ public IdempotentRequestWrapper findIdempotentRequestArg(ProceedingJoinPoint pjp for (int i = 0; i < args.length; i++) { for (Annotation annotation : annotations[i]) { if (annotation instanceof IdempotentRequestPayload) { - return new IdempotentRequestWrapper(getIdempotentNonIgnorableWrapper(args)); + return new IdempotentRequestWrapper(getIdempotentNonIgnorableWrapper(args[i])); } } } @@ -246,17 +246,21 @@ public void setJdempotentId(Object[] args, String idempotencyKey) throws Illegal } } - public IdempotentIgnorableWrapper getIdempotentNonIgnorableWrapper(Object[] args) throws IllegalAccessException { + public IdempotentIgnorableWrapper getIdempotentNonIgnorableWrapper(Object args) throws IllegalAccessException { var wrapper = new IdempotentIgnorableWrapper(); - Field[] declaredFields = args[0].getClass().getDeclaredFields(); - for (int i = 0; i < declaredFields.length; i++) { - declaredFields[i].setAccessible(true); - if (declaredFields[i].getDeclaredAnnotations().length == 0) { - wrapper.getNonIgnoredFields().put(declaredFields[i].getName(), declaredFields[i].get(args[0])); + Field[] declaredFields = args.getClass().getDeclaredFields(); + if(args instanceof String){ + wrapper.getNonIgnoredFields().put(args.toString(), args); + return wrapper; + } + for (Field declaredField : declaredFields) { + declaredField.setAccessible(true); + if (declaredField.getDeclaredAnnotations().length == 0) { + wrapper.getNonIgnoredFields().put(declaredField.getName(), declaredField.get(args)); } else { - for (Annotation annotation : declaredFields[i].getDeclaredAnnotations()) { + for (Annotation annotation : declaredField.getDeclaredAnnotations()) { if (!(annotation instanceof IdempotentIgnore)) { - wrapper.getNonIgnoredFields().put(declaredFields[i].getName(), declaredFields[i].get(args[0])); + wrapper.getNonIgnoredFields().put(declaredField.getName(), declaredField.get(args)); } } } diff --git a/Jdempotent-core/src/test/java/aspect/core/IdempotentTestPayload.java b/Jdempotent-core/src/test/java/aspect/core/IdempotentTestPayload.java index 30b2a6a..e5af480 100644 --- a/Jdempotent-core/src/test/java/aspect/core/IdempotentTestPayload.java +++ b/Jdempotent-core/src/test/java/aspect/core/IdempotentTestPayload.java @@ -1,16 +1,12 @@ package aspect.core; import com.trendyol.jdempotent.core.annotation.IdempotentIgnore; -import com.trendyol.jdempotent.core.annotation.JdempotentId; public class IdempotentTestPayload { private String name; @IdempotentIgnore private Long age; - @JdempotentId - private String jdempotentId; - public IdempotentTestPayload() { } diff --git a/Jdempotent-core/src/test/java/aspect/withaspect/IdempotentAspectIT.java b/Jdempotent-core/src/test/java/aspect/withaspect/IdempotentAspectIT.java index 6272c4e..145f285 100644 --- a/Jdempotent-core/src/test/java/aspect/withaspect/IdempotentAspectIT.java +++ b/Jdempotent-core/src/test/java/aspect/withaspect/IdempotentAspectIT.java @@ -155,4 +155,20 @@ public void given_jdempotent_id_then_args_should_have_idempotency_id() throws No assertTrue(idempotentRepository.contains(idempotencyKey)); } + @Test + public void given_new_payload_as_string_when_trigger_aspect_then_that_will_be_aviable_in_repository() throws NoSuchAlgorithmException { + //given + String idempotencyKey = "key"; + IdempotentTestPayload test = new IdempotentTestPayload(); + IdempotentIgnorableWrapper wrapper = new IdempotentIgnorableWrapper(); + wrapper.getNonIgnoredFields().put(idempotencyKey, idempotencyKey); + IdempotencyKey key = defaultKeyGenerator.generateIdempotentKey(new IdempotentRequestWrapper(wrapper), "", new StringBuilder(), MessageDigest.getInstance(CryptographyAlgorithm.MD5.value())); + + //when + testIdempotentResource.idempotencyKeyAsString(idempotencyKey); + + //then + assertTrue(idempotentRepository.contains(key)); + } + } \ No newline at end of file diff --git a/Jdempotent-spring-boot-couchbase-starter/pom.xml b/Jdempotent-spring-boot-couchbase-starter/pom.xml index 7dd671c..52615f9 100644 --- a/Jdempotent-spring-boot-couchbase-starter/pom.xml +++ b/Jdempotent-spring-boot-couchbase-starter/pom.xml @@ -5,7 +5,7 @@ 4.0.0 com.trendyol Jdempotent-spring-boot-couchbase-starter - 1.0.8 + 1.0.9 Jdempotent-spring-boot-couchbase-starter jar https://github.com/Trendyol/Jdempotent/tree/master/Jdempotent-spring-boot-couchbase-starter @@ -54,7 +54,7 @@ com.trendyol Jdempotent-core - 1.0.8 + 1.0.9 org.aspectj diff --git a/Jdempotent-spring-boot-redis-starter/pom.xml b/Jdempotent-spring-boot-redis-starter/pom.xml index 3e9edeb..38bcd6c 100644 --- a/Jdempotent-spring-boot-redis-starter/pom.xml +++ b/Jdempotent-spring-boot-redis-starter/pom.xml @@ -6,7 +6,7 @@ 4.0.0 com.trendyol Jdempotent-spring-boot-redis-starter - 1.0.8 + 1.0.9 Jdempotent-spring-boot-redis-starter jar https://github.com/Trendyol/Jdempotent/tree/master/Jdempotent-spring-boot-redis-starter @@ -54,7 +54,7 @@ com.trendyol Jdempotent-core - 1.0.8 + 1.0.9 redis.clients diff --git a/examples/jdempotent-couchbase-example/pom.xml b/examples/jdempotent-couchbase-example/pom.xml index 0499aa0..fa37b6d 100644 --- a/examples/jdempotent-couchbase-example/pom.xml +++ b/examples/jdempotent-couchbase-example/pom.xml @@ -59,7 +59,7 @@ com.trendyol Jdempotent-spring-boot-couchbase-starter - 1.0.8 + 1.0.9 diff --git a/examples/jdempotent-redis-example/pom.xml b/examples/jdempotent-redis-example/pom.xml index c9900d3..3cd4dfa 100644 --- a/examples/jdempotent-redis-example/pom.xml +++ b/examples/jdempotent-redis-example/pom.xml @@ -59,7 +59,7 @@ com.trendyol Jdempotent-spring-boot-redis-starter - 1.0.8 + 1.0.9 diff --git a/examples/jdempotent-redis-example/src/main/java/com/jdempotent/example/demo/controller/MailController.java b/examples/jdempotent-redis-example/src/main/java/com/jdempotent/example/demo/controller/MailController.java index 5d9ac8b..0742fe1 100644 --- a/examples/jdempotent-redis-example/src/main/java/com/jdempotent/example/demo/controller/MailController.java +++ b/examples/jdempotent-redis-example/src/main/java/com/jdempotent/example/demo/controller/MailController.java @@ -14,9 +14,10 @@ import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; - +import com.trendyol.jdempotent.core.annotation.IdempotentRequestPayload; import javax.mail.MessagingException; import java.util.concurrent.TimeUnit; +import org.springframework.web.bind.annotation.RequestHeader; @RestController public class MailController { @@ -42,6 +43,24 @@ public ResponseEntity sendEmail(@RequestBody SendEmailRequest return new ResponseEntity(new SendEmailResponse("We will send your message"), HttpStatus.ACCEPTED); } + + + @PostMapping("/send-email-header") + @IdempotentResource(cachePrefix = "MailController.sendEmail") + public ResponseEntity sendEmail(@IdempotentRequestPayload @RequestHeader("x-idempotency-key") String idempotencyKey, @RequestBody SendEmailRequest request) { + if (StringUtils.isEmpty(request.getEmail())) { + throw new InvalidEmailAddressException(); + } + + try { + mailSenderService.sendMail(request); + } catch (MessagingException e) { + logger.debug("MailSenderService.sendEmail() throw exception: {} request: {} ", e, request); + } + + return new ResponseEntity(new SendEmailResponse("We will send your message"), HttpStatus.ACCEPTED); + } + @PostMapping("v2/send-email") @IdempotentResource( cachePrefix = "MailController.sendEmailV2", diff --git a/pom.xml b/pom.xml index 82e0ddb..f9d4263 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.trendyol jdempotent pom - 1.0.8 + 1.0.9 Jdempotent https://github.com/Trendyol/Jdempotent Jdempotent