Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix save response in redis. #37

Merged
merged 3 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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