-
Notifications
You must be signed in to change notification settings - Fork 28
CIRC-2531 storage layer for request anonymization #1651
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
Merged
longvo-cv
merged 11 commits into
master
from
CIRC-2531-Storage-Layer-For-Request-Anonymization
May 7, 2026
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ea3841b
CIRC-2531: Implement storage layer for request anonymization
longvo-cv 0cd351f
CIRC-2531: Fix compilation errors
longvo-cv b66306c
Add unit tests
longvo-cv d5bb116
Unit test update
longvo-cv 5333295
Use RequestBatch similar to other functions and fixes
longvo-cv 0a530cb
Update test file
longvo-cv 5a37480
Remove unneeded code
longvo-cv 554fa2a
CIRC-2531 Add logging and use HTTP_CREATED constant
longvo-cv f53a4b2
Merge remote-tracking branch 'origin/master' into CIRC-2531-Storage-L…
longvo-cv cf6ca52
CIRC-2531: add coverage test
longvo-cv b98e7a7
Fix variable usage
longvo-cv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -18,3 +18,4 @@ nbproject/ | |
| .settings/ | ||
| .classpath | ||
| /bin/ | ||
| .DS_Store | ||
This file contains hidden or 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
66 changes: 66 additions & 0 deletions
66
...folio/circulation/infrastructure/storage/requests/AnonymizeStorageRequestsRepository.java
This file contains hidden or 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,66 @@ | ||
| package org.folio.circulation.infrastructure.storage.requests; | ||
|
|
||
| import static java.util.concurrent.CompletableFuture.completedFuture; | ||
| import static org.folio.circulation.support.http.ResponseMapping.forwardOnFailure; | ||
| import static org.folio.circulation.support.http.ResponseMapping.mapUsingJson; | ||
| import static org.folio.circulation.support.json.JsonStringArrayPropertyFetcher.toStream; | ||
| import static org.folio.circulation.support.results.Result.succeeded; | ||
| import static org.folio.HttpStatus.HTTP_CREATED; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Function; | ||
| import java.lang.invoke.MethodHandles; | ||
|
|
||
| import org.apache.logging.log4j.LogManager; | ||
| import org.apache.logging.log4j.Logger; | ||
| import org.folio.circulation.domain.anonymization.RequestAnonymizationRecords; | ||
| import org.folio.circulation.support.Clients; | ||
| import org.folio.circulation.support.CollectionResourceClient; | ||
| import org.folio.circulation.support.http.client.Response; | ||
| import org.folio.circulation.support.http.client.ResponseInterpreter; | ||
| import org.folio.circulation.support.results.Result; | ||
| import org.folio.circulation.storage.RequestBatch; | ||
|
|
||
| public class AnonymizeStorageRequestsRepository { | ||
| private static final Logger log = LogManager.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
|
||
| private final CollectionResourceClient requestStorageClient; | ||
|
|
||
| public AnonymizeStorageRequestsRepository(Clients clients) { | ||
| requestStorageClient = clients.requestsBatchStorage(); | ||
| } | ||
|
|
||
| private static ResponseInterpreter<RequestAnonymizationRecords> | ||
| createStorageRequestResponseInterpreter(RequestAnonymizationRecords records) { | ||
|
|
||
| Function<Response, Result<RequestAnonymizationRecords>> mapper = mapUsingJson( | ||
| response -> { | ||
| var anonymized = toStream(response, "anonymizedRequests").toList(); | ||
| log.info("createStorageRequestResponseInterpreter:: successfully anonymized {} requests", | ||
| anonymized.size()); | ||
| return records.withAnonymizedRequests(anonymized); | ||
| }); | ||
|
|
||
| return new ResponseInterpreter<RequestAnonymizationRecords>() | ||
| .flatMapOn(HTTP_CREATED.toInt(), mapper) | ||
| .otherwise(forwardOnFailure()); | ||
| } | ||
|
|
||
| public CompletableFuture<Result<RequestAnonymizationRecords>> | ||
| postAnonymizeStorageRequests(RequestAnonymizationRecords records) { | ||
|
|
||
| log.debug("postAnonymizeStorageRequests:: parameters records: {}", records); | ||
|
|
||
| if (records.getAnonymizedRequestIds().isEmpty()) { | ||
| log.info("postAnonymizeStorageRequests:: no requests to anonymize, skipping"); | ||
| return completedFuture(succeeded(records)); | ||
| } | ||
|
|
||
| log.info("postAnonymizeStorageRequests:: anonymizing {} requests", | ||
| records.getAnonymizedRequestIds().size()); | ||
| RequestBatch requestBatch = new RequestBatch(records.getAnonymizedRequests()); | ||
|
|
||
| return requestStorageClient.post(requestBatch.toJson()) | ||
| .thenApply(createStorageRequestResponseInterpreter(records)::flatMap); | ||
| } | ||
| } | ||
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
src/test/java/org/folio/circulation/domain/RequestStatusTest.java
This file contains hidden or 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,32 @@ | ||
| package org.folio.circulation.domain; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class RequestStatusTest { | ||
|
|
||
| @Test | ||
| void closedStatesReturnsAllClosedStatuses() { | ||
| List<String> closedStates = RequestStatus.closedStates(); | ||
|
|
||
| assertEquals(4, closedStates.size()); | ||
| assertTrue(closedStates.contains("Closed - Filled")); | ||
| assertTrue(closedStates.contains("Closed - Cancelled")); | ||
| assertTrue(closedStates.contains("Closed - Unfilled")); | ||
| assertTrue(closedStates.contains("Closed - Pickup expired")); | ||
| } | ||
|
|
||
| @Test | ||
| void closedStatesDoesNotContainOpenStatuses() { | ||
| List<String> closedStates = RequestStatus.closedStates(); | ||
|
|
||
| assertTrue(!closedStates.contains("Open - Not yet filled")); | ||
| assertTrue(!closedStates.contains("Open - Awaiting pickup")); | ||
| assertTrue(!closedStates.contains("Open - In transit")); | ||
| assertTrue(!closedStates.contains("Open - Awaiting delivery")); | ||
| } | ||
| } |
131 changes: 131 additions & 0 deletions
131
...o/circulation/infrastructure/storage/requests/AnonymizeStorageRequestsRepositoryTest.java
This file contains hidden or 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,131 @@ | ||
| package org.folio.circulation.infrastructure.storage.requests; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.times; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import org.folio.circulation.domain.anonymization.RequestAnonymizationRecords; | ||
| import org.folio.circulation.support.Clients; | ||
| import org.folio.circulation.support.CollectionResourceClient; | ||
| import org.folio.circulation.support.http.client.Response; | ||
| import org.folio.circulation.support.results.Result; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import io.vertx.core.json.JsonArray; | ||
| import io.vertx.core.json.JsonObject; | ||
|
|
||
| class AnonymizeStorageRequestsRepositoryTest { | ||
|
|
||
| @Test | ||
| void shouldReturnSuccessWhenRequestIdsAreEmpty() { | ||
| Clients clients = mock(Clients.class); | ||
| CollectionResourceClient client = mock(CollectionResourceClient.class); | ||
|
|
||
| when(clients.requestsBatchStorage()).thenReturn(client); | ||
|
|
||
| AnonymizeStorageRequestsRepository repository = | ||
| new AnonymizeStorageRequestsRepository(clients); | ||
|
|
||
| RequestAnonymizationRecords emptyRecords = new RequestAnonymizationRecords(); | ||
|
|
||
| Result<RequestAnonymizationRecords> result = | ||
| repository.postAnonymizeStorageRequests(emptyRecords).join(); | ||
|
|
||
| assertTrue(result.succeeded()); | ||
| assertEquals(0, result.value().getAnonymizedRequestIds().size()); | ||
| verify(client, times(0)).post(any(JsonObject.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldPostToStorageWhenRequestIdsExist() { | ||
| Clients clients = mock(Clients.class); | ||
| CollectionResourceClient client = mock(CollectionResourceClient.class); | ||
|
|
||
| when(clients.requestsBatchStorage()).thenReturn(client); | ||
|
|
||
| Response response = mock(Response.class); | ||
| when(response.getStatusCode()).thenReturn(201); | ||
|
|
||
| JsonObject responseBody = new JsonObject(); | ||
|
|
||
| when(response.getJson()).thenReturn(responseBody); | ||
| when(client.post(any(JsonObject.class))) | ||
| .thenReturn(CompletableFuture.completedFuture(Result.succeeded(response))); | ||
|
|
||
| AnonymizeStorageRequestsRepository repository = | ||
| new AnonymizeStorageRequestsRepository(clients); | ||
|
|
||
| RequestAnonymizationRecords records = new RequestAnonymizationRecords() | ||
| .withAnonymizedRequests(Arrays.asList("request-id-1", "request-id-2")); | ||
|
|
||
| Result<RequestAnonymizationRecords> result = | ||
| repository.postAnonymizeStorageRequests(records).join(); | ||
|
|
||
| assertTrue(result.succeeded()); | ||
| assertNotNull(result.value()); | ||
| verify(client, times(1)).post(any(JsonObject.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldCreateCorrectPayload() { | ||
| Clients clients = mock(Clients.class); | ||
| CollectionResourceClient client = mock(CollectionResourceClient.class); | ||
|
|
||
| when(clients.requestsBatchStorage()).thenReturn(client); | ||
|
|
||
| Response response = mock(Response.class); | ||
| when(response.getStatusCode()).thenReturn(201); | ||
| when(response.getJson()).thenReturn(new JsonObject()); | ||
|
|
||
| when(client.post(any(JsonObject.class))) | ||
| .thenReturn(CompletableFuture.completedFuture(Result.succeeded(response))); | ||
|
|
||
| AnonymizeStorageRequestsRepository repository = | ||
| new AnonymizeStorageRequestsRepository(clients); | ||
|
|
||
| RequestAnonymizationRecords records = new RequestAnonymizationRecords() | ||
| .withAnonymizedRequests(Collections.singletonList("request-1")); | ||
|
|
||
| Result<RequestAnonymizationRecords> result = | ||
| repository.postAnonymizeStorageRequests(records).join(); | ||
|
|
||
| assertTrue(result.succeeded()); | ||
| verify(client).post(any(JsonObject.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void shouldParseAnonymizedRequestsFromResponse() { | ||
| Clients clients = mock(Clients.class); | ||
| CollectionResourceClient client = mock(CollectionResourceClient.class); | ||
| when(clients.requestsBatchStorage()).thenReturn(client); | ||
|
|
||
| JsonObject responseBody = new JsonObject() | ||
| .put("anonymizedRequests", new JsonArray().add("request-1").add("request-2")); | ||
| Response response = mock(Response.class); | ||
| when(response.getStatusCode()).thenReturn(201); | ||
| when(response.getJson()).thenReturn(responseBody); | ||
| when(client.post(any(JsonObject.class))) | ||
| .thenReturn(CompletableFuture.completedFuture(Result.succeeded(response))); | ||
|
|
||
| AnonymizeStorageRequestsRepository repository = | ||
| new AnonymizeStorageRequestsRepository(clients); | ||
|
|
||
| RequestAnonymizationRecords records = new RequestAnonymizationRecords() | ||
| .withAnonymizedRequests(Arrays.asList("request-1", "request-2")); | ||
|
|
||
| Result<RequestAnonymizationRecords> result = | ||
| repository.postAnonymizeStorageRequests(records).join(); | ||
|
|
||
| assertTrue(result.succeeded()); | ||
| assertNotNull(result.value()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.