-
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
Open
longvo-cv
wants to merge
10
commits into
master
Choose a base branch
from
CIRC-2531-Storage-Layer-For-Request-Anonymization
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+339
−1
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
37cfe77
CIRC-2531: Implement storage layer for request anonymization
longvo-cv cdfc980
CIRC-2531: Fix compilation errors
longvo-cv 6869f2e
Add unit tests
longvo-cv 504340e
Unit test update
longvo-cv ddadd2d
Merge branch 'master' into CIRC-2531-Storage-Layer-For-Request-Anonym…
yuntianhu caeaacc
Use RequestBatch similar to other functions and fixes
longvo-cv 3edd5e6
Merge branch 'CIRC-2531-Storage-Layer-For-Request-Anonymization' of h…
longvo-cv 27e6ca1
Update test file
longvo-cv 5092a14
Merge branch 'master' into CIRC-2531-Storage-Layer-For-Request-Anonym…
yuntianhu 97e64eb
Remove unneeded code
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
53 changes: 53 additions & 0 deletions
53
...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,53 @@ | ||
| 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 java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Function; | ||
|
|
||
| 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; | ||
|
|
||
| import io.vertx.core.json.JsonArray; | ||
| import io.vertx.core.json.JsonObject; | ||
|
|
||
| public class AnonymizeStorageRequestsRepository { | ||
| 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 -> records.withAnonymizedRequests( | ||
| toStream(response, "anonymizedRequests").toList())); | ||
|
|
||
| return new ResponseInterpreter<RequestAnonymizationRecords>() | ||
| .on(201, Result.of(() -> records)) | ||
| .otherwise(forwardOnFailure()); | ||
| } | ||
|
|
||
| public CompletableFuture<Result<RequestAnonymizationRecords>> | ||
| postAnonymizeStorageRequests(RequestAnonymizationRecords records) { | ||
|
|
||
| if (records.getAnonymizedRequestIds().isEmpty()) { | ||
| return completedFuture(succeeded(records)); | ||
| } | ||
| 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")); | ||
| } | ||
| } |
104 changes: 104 additions & 0 deletions
104
...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,104 @@ | ||
| 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)); | ||
| } | ||
| } |
109 changes: 109 additions & 0 deletions
109
...est/java/org/folio/circulation/infrastructure/storage/requests/RequestRepositoryTest.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,109 @@ | ||
| package org.folio.circulation.infrastructure.storage.requests; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
|
|
||
| import org.folio.circulation.domain.MultipleRecords; | ||
| import org.folio.circulation.domain.Request; | ||
| import org.folio.circulation.support.Clients; | ||
| import org.folio.circulation.support.CollectionResourceClient; | ||
| import org.folio.circulation.support.http.client.CqlQuery; | ||
| import org.folio.circulation.support.http.client.PageLimit; | ||
| import org.folio.circulation.support.http.client.Response; | ||
| import org.folio.circulation.support.results.Result; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import io.vertx.core.json.JsonArray; | ||
| import io.vertx.core.json.JsonObject; | ||
|
|
||
| class RequestRepositoryTest { | ||
|
|
||
| private RequestRepository repository; | ||
| private CollectionResourceClient requestsStorageClient; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| Clients clients = mock(Clients.class); | ||
| requestsStorageClient = mock(CollectionResourceClient.class); | ||
|
|
||
| when(clients.requestsStorage()).thenReturn(requestsStorageClient); | ||
| when(clients.requestsBatchStorage()).thenReturn(mock(CollectionResourceClient.class)); | ||
| when(clients.cancellationReasonStorage()).thenReturn(mock(CollectionResourceClient.class)); | ||
|
|
||
| repository = new RequestRepository(clients); | ||
| } | ||
|
|
||
| @Test | ||
| void findRequestsToAnonymizeQueriesClosedRequests() { | ||
| Response response = createMockResponse(); | ||
| when(requestsStorageClient.getMany(any(CqlQuery.class), any(PageLimit.class))) | ||
| .thenReturn(CompletableFuture.completedFuture(Result.succeeded(response))); | ||
|
|
||
| PageLimit pageLimit = PageLimit.limit(100); | ||
| Result<MultipleRecords<Request>> result = | ||
| repository.findRequestsToAnonymize(pageLimit).join(); | ||
|
|
||
| assertTrue(result.succeeded()); | ||
| verify(requestsStorageClient).getMany(any(CqlQuery.class), eq(pageLimit)); | ||
| } | ||
|
|
||
| @Test | ||
| void findClosedRequestsQueriesForSpecificUser() { | ||
| Response response = createMockResponse(); | ||
| when(requestsStorageClient.getMany(any(CqlQuery.class), any(PageLimit.class))) | ||
| .thenReturn(CompletableFuture.completedFuture(Result.succeeded(response))); | ||
|
|
||
| String userId = "user-123"; | ||
| PageLimit pageLimit = PageLimit.limit(50); | ||
|
|
||
| Result<MultipleRecords<Request>> result = | ||
| repository.findClosedRequests(userId, pageLimit).join(); | ||
|
|
||
| assertTrue(result.succeeded()); | ||
| verify(requestsStorageClient).getMany(any(CqlQuery.class), eq(pageLimit)); | ||
| } | ||
|
|
||
| @Test | ||
| void findRequestsToAnonymizeReturnsEmptyWhenNoRequests() { | ||
| Response response = createMockResponse(); | ||
| when(requestsStorageClient.getMany(any(CqlQuery.class), any(PageLimit.class))) | ||
| .thenReturn(CompletableFuture.completedFuture(Result.succeeded(response))); | ||
|
|
||
| Result<MultipleRecords<Request>> result = | ||
| repository.findRequestsToAnonymize(PageLimit.limit(10)).join(); | ||
|
|
||
| assertTrue(result.succeeded()); | ||
| assertEquals(0, result.value().getTotalRecords()); | ||
| } | ||
|
|
||
| @Test | ||
| void findClosedRequestsReturnsEmptyWhenNoRequestsForUser() { | ||
| Response response = createMockResponse(); | ||
| when(requestsStorageClient.getMany(any(CqlQuery.class), any(PageLimit.class))) | ||
| .thenReturn(CompletableFuture.completedFuture(Result.succeeded(response))); | ||
|
|
||
| Result<MultipleRecords<Request>> result = | ||
| repository.findClosedRequests("user-456", PageLimit.limit(10)).join(); | ||
|
|
||
| assertTrue(result.succeeded()); | ||
| assertEquals(0, result.value().getTotalRecords()); | ||
| } | ||
|
|
||
| private Response createMockResponse() { | ||
| Response response = mock(Response.class); | ||
| JsonObject body = new JsonObject() | ||
| .put("requests", new JsonArray()) | ||
| .put("totalRecords", 0); | ||
| when(response.getJson()).thenReturn(body); | ||
| when(response.getStatusCode()).thenReturn(200); | ||
| return response; | ||
| } | ||
| } |
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.