-
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
base: master
Are you sure you want to change the base?
Changes from all commits
ea3841b
0cd351f
b66306c
d5bb116
5333295
0a530cb
5a37480
554fa2a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| 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; | ||
|
|
||
| import io.vertx.core.json.JsonArray; | ||
|
Check warning on line 24 in src/main/java/org/folio/circulation/infrastructure/storage/requests/AnonymizeStorageRequestsRepository.java
|
||
| import io.vertx.core.json.JsonObject; | ||
|
Check warning on line 25 in src/main/java/org/folio/circulation/infrastructure/storage/requests/AnonymizeStorageRequestsRepository.java
|
||
|
|
||
| public class AnonymizeStorageRequestsRepository { | ||
|
longvo-cv marked this conversation as resolved.
|
||
| 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( | ||
|
Check warning on line 39 in src/main/java/org/folio/circulation/infrastructure/storage/requests/AnonymizeStorageRequestsRepository.java
|
||
| response -> { | ||
| var anonymized = toStream(response, "anonymizedRequests").toList(); | ||
| log.info("createStorageRequestResponseInterpreter:: successfully anonymized {} requests", | ||
| anonymized.size()); | ||
| return records.withAnonymizedRequests(anonymized); | ||
| }); | ||
|
Check warning on line 45 in src/main/java/org/folio/circulation/infrastructure/storage/requests/AnonymizeStorageRequestsRepository.java
|
||
|
|
||
| return new ResponseInterpreter<RequestAnonymizationRecords>() | ||
| .on(HTTP_CREATED.toInt(), Result.of(() -> records)) | ||
| .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); | ||
| } | ||
| } | ||
| 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")); | ||
| } | ||
| } |
| 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; | ||
|
Check warning on line 23 in src/test/java/org/folio/circulation/infrastructure/storage/requests/AnonymizeStorageRequestsRepositoryTest.java
|
||
| 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)); | ||
| } | ||
| } | ||
| 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; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.