-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OZ-579: Implement session cookie cache.
- Loading branch information
1 parent
08cd414
commit e9fc074
Showing
7 changed files
with
177 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...api/src/main/java/com/ozonehis/camel/frappe/sdk/internal/security/cookie/CookieCache.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.ozonehis.camel.frappe.sdk.internal.security.cookie; | ||
|
||
import java.util.concurrent.ConcurrentHashMap; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* A cache for storing cookies. | ||
*/ | ||
@NoArgsConstructor | ||
public class CookieCache { | ||
|
||
private static CookieCache instance = null; | ||
|
||
private final ConcurrentHashMap<String, WrappedCookie> cookieStore = new ConcurrentHashMap<>(); | ||
|
||
public static CookieCache getInstance() { | ||
if (instance == null) { | ||
instance = new CookieCache(); | ||
} | ||
return instance; | ||
} | ||
|
||
/** | ||
* Put cookies by name. | ||
* | ||
* @param cookieName the name of the cookie | ||
* @param cookies the wrappedCookies | ||
*/ | ||
public void put(String cookieName, WrappedCookie cookies) { | ||
cookieStore.put(cookieName, cookies); | ||
} | ||
|
||
/** | ||
* Get cookies by name. | ||
* | ||
* @param cookieName the name of the cookie | ||
* @return the wrappedCookies | ||
*/ | ||
public WrappedCookie get(String cookieName) { | ||
return cookieStore.get(cookieName); | ||
} | ||
|
||
/** | ||
* Clear all cookies from the cache. | ||
*/ | ||
public void clear() { | ||
cookieStore.clear(); | ||
} | ||
|
||
/** | ||
* Clear expired cookies from the cache. | ||
*/ | ||
public void clearExpired() { | ||
cookieStore.forEach((key, value) -> { | ||
if (value.isExpired()) { | ||
cookieStore.remove(key); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 7 additions & 10 deletions
17
...i/src/main/java/com/ozonehis/camel/frappe/sdk/internal/security/cookie/WrappedCookie.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
package com.ozonehis.camel.frappe.sdk.internal.security.cookie; | ||
|
||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import okhttp3.Cookie; | ||
import okhttp3.HttpUrl; | ||
|
||
@Data | ||
@EqualsAndHashCode | ||
@AllArgsConstructor | ||
public class WrappedCookie { | ||
|
||
private Cookie cookie; | ||
private long expiresAt; | ||
|
||
public boolean isExpired() { | ||
return cookie.expiresAt() < System.currentTimeMillis(); | ||
} | ||
private List<String> cookies; | ||
|
||
public Cookie unwrap() { | ||
return cookie; | ||
public boolean isExpired() { | ||
return expiresAt < System.currentTimeMillis(); | ||
} | ||
|
||
public boolean matches(HttpUrl url) { | ||
return cookie.matches(url); | ||
public List<String> unwrap() { | ||
return cookies; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...src/test/java/com/ozonehis/camel/frappe/sdk/internal/security/cookie/CookieCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.ozonehis.camel.frappe.sdk.internal.security.cookie; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
|
||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class CookieCacheTest { | ||
|
||
@Test | ||
@DisplayName("put should store the cookie in the cache") | ||
void putShouldStoreTheCookieInTheCache() { | ||
CookieCache cookieCache = CookieCache.getInstance(); | ||
WrappedCookie wrappedCookie = | ||
new WrappedCookie(System.currentTimeMillis() + 1000, List.of("cookie1", "cookie2")); | ||
cookieCache.put("testCookie", wrappedCookie); | ||
assertEquals(wrappedCookie, cookieCache.get("testCookie")); | ||
} | ||
|
||
@Test | ||
@DisplayName("get should return the correct cookie from the cache") | ||
void getShouldReturnTheCorrectCookieFromTheCache() { | ||
CookieCache cookieCache = CookieCache.getInstance(); | ||
WrappedCookie wrappedCookie = | ||
new WrappedCookie(System.currentTimeMillis() + 1000, List.of("cookie1", "cookie2")); | ||
cookieCache.put("testCookie", wrappedCookie); | ||
assertEquals(wrappedCookie, cookieCache.get("testCookie")); | ||
} | ||
|
||
@Test | ||
@DisplayName("clear should remove all cookies from the cache") | ||
void clearShouldRemoveAllCookiesFromTheCache() { | ||
CookieCache cookieCache = CookieCache.getInstance(); | ||
WrappedCookie wrappedCookie = | ||
new WrappedCookie(System.currentTimeMillis() + 1000, List.of("cookie1", "cookie2")); | ||
cookieCache.put("testCookie", wrappedCookie); | ||
cookieCache.clear(); | ||
assertNull(cookieCache.get("testCookie")); | ||
} | ||
|
||
@Test | ||
@DisplayName("clearExpired should remove only expired cookies from the cache") | ||
void clearExpiredShouldRemoveOnlyExpiredCookiesFromTheCache() { | ||
CookieCache cookieCache = CookieCache.getInstance(); | ||
WrappedCookie expiredCookie = | ||
new WrappedCookie(System.currentTimeMillis() - 1000, List.of("cookie1", "cookie2")); | ||
WrappedCookie validCookie = new WrappedCookie(System.currentTimeMillis() + 1000, List.of("cookie3", "cookie4")); | ||
cookieCache.put("expiredCookie", expiredCookie); | ||
cookieCache.put("validCookie", validCookie); | ||
cookieCache.clearExpired(); | ||
assertNull(cookieCache.get("expiredCookie")); | ||
assertEquals(validCookie, cookieCache.get("validCookie")); | ||
} | ||
} |
69 changes: 15 additions & 54 deletions
69
...c/test/java/com/ozonehis/camel/frappe/sdk/internal/security/cookie/WrappedCookieTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,34 @@ | ||
package com.ozonehis.camel.frappe.sdk.internal.security.cookie; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.openMocks; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.Date; | ||
import okhttp3.Cookie; | ||
import okhttp3.HttpUrl; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import java.util.List; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
|
||
class WrappedCookieTest { | ||
|
||
@Mock | ||
private Cookie cookie; | ||
|
||
private WrappedCookie wrappedCookie; | ||
|
||
private static AutoCloseable mocksCloser; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
mocksCloser = openMocks(this); | ||
wrappedCookie = new WrappedCookie(cookie); | ||
} | ||
|
||
@AfterAll | ||
static void closeMocks() throws Exception { | ||
mocksCloser.close(); | ||
} | ||
|
||
@Test | ||
@DisplayName("isExpired should return true when cookie is expired") | ||
void isExpiredShouldReturnTrueWhenCookieIsExpired() { | ||
when(cookie.expiresAt()).thenReturn(new Date().getTime() - 1000); | ||
@DisplayName("isExpired should return true when expiresAt is less than current time") | ||
void isExpiredShouldReturnTrueWhenExpiresAtIsLessThanCurrentTime() { | ||
WrappedCookie wrappedCookie = | ||
new WrappedCookie(System.currentTimeMillis() - 1000, List.of("cookie1", "cookie2")); | ||
assertTrue(wrappedCookie.isExpired()); | ||
} | ||
|
||
@Test | ||
@DisplayName("isExpired should return false when cookie is not expired") | ||
void isExpiredShouldReturnFalseWhenCookieIsNotExpired() { | ||
when(cookie.expiresAt()).thenReturn(new Date().getTime() + 1000); | ||
@DisplayName("isExpired should return false when expiresAt is greater than current time") | ||
void isExpiredShouldReturnFalseWhenExpiresAtIsGreaterThanCurrentTime() { | ||
WrappedCookie wrappedCookie = | ||
new WrappedCookie(System.currentTimeMillis() + 1000, List.of("cookie1", "cookie2")); | ||
assertFalse(wrappedCookie.isExpired()); | ||
} | ||
|
||
@Test | ||
@DisplayName("unwrap should return the original cookie") | ||
void unwrapShouldReturnTheOriginalCookie() { | ||
assertEquals(cookie, wrappedCookie.unwrap()); | ||
} | ||
|
||
@Test | ||
@DisplayName("matches should return true when cookie matches the url") | ||
void matchesShouldReturnTrueWhenCookieMatchesTheUrl() { | ||
HttpUrl url = HttpUrl.parse("http://localhost"); | ||
when(cookie.matches(url)).thenReturn(true); | ||
assertTrue(wrappedCookie.matches(url)); | ||
} | ||
|
||
@Test | ||
@DisplayName("matches should return false when cookie does not match the url") | ||
void matchesShouldReturnFalseWhenCookieDoesNotMatchTheUrl() { | ||
HttpUrl url = HttpUrl.parse("http://localhost"); | ||
when(cookie.matches(url)).thenReturn(false); | ||
assertFalse(wrappedCookie.matches(url)); | ||
@DisplayName("unwrap should return the original list of cookies") | ||
void unwrapShouldReturnTheOriginalListOfCookies() { | ||
List<String> originalCookies = List.of("cookie1", "cookie2"); | ||
WrappedCookie wrappedCookie = new WrappedCookie(System.currentTimeMillis() + 1000, originalCookies); | ||
assertEquals(originalCookies, wrappedCookie.unwrap()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters