-
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.
Additional, implement shared container for integration tests
- Loading branch information
1 parent
08cd414
commit b1678b4
Showing
10 changed files
with
242 additions
and
77 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
63 changes: 63 additions & 0 deletions
63
...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,63 @@ | ||
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 synchronized 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) { | ||
if (get(cookieName) != null) { | ||
cookieStore.remove(cookieName); | ||
} | ||
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
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
92 changes: 92 additions & 0 deletions
92
...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,92 @@ | ||
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 static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.openMocks; | ||
|
||
import java.util.Objects; | ||
import okhttp3.Cookie; | ||
import okhttp3.HttpUrl; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mockito; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
class CookieCacheTest { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(CookieCacheTest.class); | ||
|
||
private static AutoCloseable mocksCloser; | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
mocksCloser = openMocks(CookieCacheTest.class); | ||
} | ||
|
||
@AfterAll | ||
static void closeMocks() throws Exception { | ||
mocksCloser.close(); | ||
} | ||
|
||
@Test | ||
@DisplayName("put should store the cookie in the cache") | ||
void putShouldStoreTheCookieInTheCache() { | ||
CookieCache cookieCache = CookieCache.getInstance(); | ||
WrappedCookie wrappedCookie = new WrappedCookie(Cookie.parse( | ||
Objects.requireNonNull(HttpUrl.parse("https://example.com")), | ||
"testCookie=cookie1; testCookie2=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(Cookie.parse( | ||
Objects.requireNonNull(HttpUrl.parse("https://example.com")), | ||
"testCookie=cookie1; testCookie2=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(Cookie.parse( | ||
Objects.requireNonNull(HttpUrl.parse("https://example.com")), | ||
"testCookie=cookie1; testCookie2=cookie2")); | ||
cookieCache.put("testCookie", wrappedCookie); | ||
cookieCache.clear(); | ||
assertNull(cookieCache.get("testCookie")); | ||
assertNull(cookieCache.get("testCookie2")); | ||
} | ||
|
||
@Test | ||
@DisplayName("clearExpired should remove only expired cookies from the cache") | ||
void clearExpiredShouldRemoveOnlyExpiredCookiesFromTheCache() { | ||
CookieCache cookieCache = CookieCache.getInstance(); | ||
|
||
var expiredCookie = Mockito.mock(Cookie.class); | ||
WrappedCookie expiredWrappedCookie = new WrappedCookie(expiredCookie); | ||
|
||
var validCookie = Mockito.mock(Cookie.class); | ||
WrappedCookie validWrappedCookie = new WrappedCookie(validCookie); | ||
|
||
cookieCache.put("expiredCookie", expiredWrappedCookie); | ||
cookieCache.put("validCookie", validWrappedCookie); | ||
|
||
when(expiredCookie.expiresAt()).thenReturn(System.currentTimeMillis() - 1000); | ||
when(validCookie.expiresAt()).thenReturn(System.currentTimeMillis() + 1000); | ||
|
||
cookieCache.clearExpired(); | ||
|
||
assertNull(cookieCache.get("expiredCookie")); | ||
assertEquals(validWrappedCookie, cookieCache.get("validCookie")); | ||
} | ||
} |
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
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
Oops, something went wrong.