Skip to content

Commit

Permalink
Merge pull request #29 from Trendyol/string-support
Browse files Browse the repository at this point in the history
string support for idempotency check
  • Loading branch information
memojja authored Nov 2, 2021
2 parents b6376cc + 854efaa commit 50b80d6
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Jdempotent-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.trendyol</groupId>
<artifactId>Jdempotent-core</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<name>Jdempotent-core</name>
<packaging>jar</packaging>
<url>https://github.com/Trendyol/Jdempotent/tree/master/Jdempotent-core</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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]));
}
}
}
Expand Down Expand Up @@ -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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
4 changes: 2 additions & 2 deletions Jdempotent-spring-boot-couchbase-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.trendyol</groupId>
<artifactId>Jdempotent-spring-boot-couchbase-starter</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<name>Jdempotent-spring-boot-couchbase-starter</name>
<packaging>jar</packaging>
<url>https://github.com/Trendyol/Jdempotent/tree/master/Jdempotent-spring-boot-couchbase-starter</url>
Expand Down Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>com.trendyol</groupId>
<artifactId>Jdempotent-core</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
Expand Down
4 changes: 2 additions & 2 deletions Jdempotent-spring-boot-redis-starter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.trendyol</groupId>
<artifactId>Jdempotent-spring-boot-redis-starter</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
<name>Jdempotent-spring-boot-redis-starter</name>
<packaging>jar</packaging>
<url>https://github.com/Trendyol/Jdempotent/tree/master/Jdempotent-spring-boot-redis-starter</url>
Expand Down Expand Up @@ -54,7 +54,7 @@
<dependency>
<groupId>com.trendyol</groupId>
<artifactId>Jdempotent-core</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
Expand Down
2 changes: 1 addition & 1 deletion examples/jdempotent-couchbase-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>com.trendyol</groupId>
<artifactId>Jdempotent-spring-boot-couchbase-starter</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</dependency>
</dependencies>

Expand Down
2 changes: 1 addition & 1 deletion examples/jdempotent-redis-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
<dependency>
<groupId>com.trendyol</groupId>
<artifactId>Jdempotent-spring-boot-redis-starter</artifactId>
<version>1.0.8</version>
<version>1.0.9</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -42,6 +43,24 @@ public ResponseEntity<SendEmailResponse> 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<SendEmailResponse> 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",
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.trendyol</groupId>
<artifactId>jdempotent</artifactId>
<packaging>pom</packaging>
<version>1.0.8</version>
<version>1.0.9</version>
<name>Jdempotent</name>
<url>https://github.com/Trendyol/Jdempotent</url>
<description>Jdempotent</description>
Expand Down

0 comments on commit 50b80d6

Please sign in to comment.