Skip to content

Commit

Permalink
Merge pull request #37 from SawCunha/hotfix/save_response_redis
Browse files Browse the repository at this point in the history
Hotfix save response in redis.
  • Loading branch information
memojja authored Jan 18, 2022
2 parents f48a6bd + 654acd6 commit 5501784
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 14 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.11</version>
<version>1.0.12</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 @@ -18,6 +18,11 @@ public IdempotentRequestResponseWrapper(IdempotentRequestWrapper request) {
this.request = request;
}

public IdempotentRequestResponseWrapper(IdempotentRequestWrapper request, IdempotentResponseWrapper response) {
this.request = request;
this.response = response;
}

public IdempotentResponseWrapper getResponse() {
return response;
}
Expand Down
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.11</version>
<version>1.0.12</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.11</version>
<version>1.0.12</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.11</version>
<version>1.0.12</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.11</version>
<version>1.0.12</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ public void setResponse(IdempotencyKey idempotencyKey, IdempotentRequestWrapper
ttl = ttl == 0 ? redisProperties.getExpirationTimeHour() : ttl;
IdempotentRequestResponseWrapper requestResponseWrapper = valueOperations.get(idempotencyKey.getKeyValue());
requestResponseWrapper.setResponse(response);
valueOperations.set(idempotencyKey.getKeyValue(), prepareValue(request), ttl, timeUnit);
valueOperations.set(idempotencyKey.getKeyValue(), prepareValue(request, response), ttl, timeUnit);
}
}

/**
* Prepares the value stored in redis
*
* if persistReqRes set to false,
* it does not persist related request and response values in redis
* it does not persist related request values in redis
* @param request
* @return
*/
Expand All @@ -100,5 +100,21 @@ private IdempotentRequestResponseWrapper prepareValue(IdempotentRequestWrapper r
}
return new IdempotentRequestResponseWrapper(null);
}

/**
* Prepares the value stored in redis
*
* if persistReqRes set to false,
* it does not persist related request and response values in redis
* @param request
* @param response
* @return
*/
private IdempotentRequestResponseWrapper prepareValue(IdempotentRequestWrapper request, IdempotentResponseWrapper response) {
if (redisProperties.getPersistReqRes()) {
return new IdempotentRequestResponseWrapper(request, response);
}
return new IdempotentRequestResponseWrapper(null);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
Expand Down Expand Up @@ -46,7 +47,6 @@ public class RedisIdempotentRepositoryTest {
@Captor
private ArgumentCaptor<IdempotentRequestResponseWrapper> captor;


@BeforeEach
public void setUp() {
when(redisTemplate.opsForValue()).thenReturn(valueOperations);
Expand Down Expand Up @@ -157,7 +157,7 @@ public void given_idempotency_key_and_request_and_response_objects_when_set_resp
var wrapper = new IdempotentRequestResponseWrapper(
new IdempotentRequestWrapper(new Object()));
when(valueOperations.get(key.getKeyValue())).thenReturn(wrapper);
assertEquals(wrapper.getResponse(), null);
assertNull(wrapper.getResponse());
when(redisConfigProperties.getPersistReqRes()).thenReturn(true);

//When
Expand All @@ -168,6 +168,31 @@ public void given_idempotency_key_and_request_and_response_objects_when_set_resp
verify(valueOperations).set(eq(key.getKeyValue()), argumentCaptor.capture(), eq(1L), eq(TimeUnit.HOURS));
IdempotentRequestResponseWrapper value = argumentCaptor.getValue();
assertEquals(value.getRequest().getRequest(), 123L);
assertEquals(value.getResponse().getResponse(), "response");
assertEquals(wrapper.getResponse().getResponse(), "response");
}

@Test
public void given_the_idempotence_key_and_the_request_and_response_objects_when_defining_the_response_one_must_save_the_key_without_the_request_and_response_object() {
//Given
IdempotencyKey key = new IdempotencyKey("key");
IdempotentRequestWrapper request = new IdempotentRequestWrapper(123L);
IdempotentResponseWrapper response = new IdempotentResponseWrapper("response");
var wrapper = new IdempotentRequestResponseWrapper(
new IdempotentRequestWrapper(new Object()));
when(valueOperations.get(key.getKeyValue())).thenReturn(wrapper);
assertNull(wrapper.getResponse());
when(redisConfigProperties.getPersistReqRes()).thenReturn(false);

//When
redisIdempotentRepository.setResponse(key, request, response, 1L, TimeUnit.HOURS);

//Then
var argumentCaptor = ArgumentCaptor.forClass(IdempotentRequestResponseWrapper.class);
verify(valueOperations).set(eq(key.getKeyValue()), argumentCaptor.capture(), eq(1L), eq(TimeUnit.HOURS));
IdempotentRequestResponseWrapper value = argumentCaptor.getValue();
assertNull(value.getRequest());
assertNull(value.getResponse());
assertEquals(wrapper.getResponse().getResponse(), "response");
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ For Redis:
<dependency>
<groupId>com.trendyol</groupId>
<artifactId>Jdempotent-spring-boot-redis-starter</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
</dependency>
```
For Couchbase:
Expand All @@ -29,7 +29,7 @@ For Couchbase:
<dependency>
<groupId>com.trendyol</groupId>
<artifactId>Jdempotent-spring-boot-couchbase-starter</artifactId>
<version>1.0.11</version>
<version>1.0.12</version>
</dependency>
```

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.11</version>
<version>1.0.12</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.11</version>
<version>1.0.12</version>
</dependency>
</dependencies>

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.11</version>
<version>1.0.12</version>
<name>Jdempotent</name>
<url>https://github.com/Trendyol/Jdempotent</url>
<description>Jdempotent</description>
Expand Down

0 comments on commit 5501784

Please sign in to comment.