From 874002244769f57f2207f588f992b56c2c982b0a Mon Sep 17 00:00:00 2001 From: saba_zedginidze Date: Thu, 12 Sep 2024 22:42:20 +0400 Subject: [PATCH] [MODORDERS-1174] Update tests --- .../pieces/PieceStorageServiceTest.java | 62 +++++++++++++++++++ .../pieces/pieces-for-user-tenants.json | 16 +++++ .../mockdata/userTenants/userTenants.json | 13 ++++ 3 files changed, 91 insertions(+) create mode 100644 src/test/resources/mockdata/pieces/pieces-for-user-tenants.json create mode 100644 src/test/resources/mockdata/userTenants/userTenants.json diff --git a/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java b/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java index 4f99262af..233710a5f 100644 --- a/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java +++ b/src/test/java/org/folio/service/pieces/PieceStorageServiceTest.java @@ -1,6 +1,7 @@ package org.folio.service.pieces; import static io.vertx.core.Future.succeededFuture; +import static org.assertj.core.api.Assertions.assertThat; import static org.folio.TestConfig.autowireDependencies; import static org.folio.TestConfig.clearServiceInteractions; import static org.folio.TestConfig.clearVertxContext; @@ -8,6 +9,8 @@ import static org.folio.TestConfig.getVertx; import static org.folio.TestConfig.initSpringContext; import static org.folio.TestConfig.isVerticleNotDeployed; +import static org.folio.TestUtils.getMockAsJson; +import static org.folio.rest.impl.MockServer.BASE_MOCK_DATA_PATH; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.eq; @@ -27,6 +30,7 @@ import java.util.concurrent.TimeoutException; import org.folio.ApiTestSuite; +import org.folio.models.consortium.ConsortiumConfiguration; import org.folio.rest.core.RestClient; import org.folio.rest.core.models.RequestContext; import org.folio.rest.core.models.RequestEntry; @@ -55,12 +59,21 @@ @ExtendWith(VertxExtension.class) public class PieceStorageServiceTest { + private static final String USER_TENANTS_PATH = BASE_MOCK_DATA_PATH + "userTenants/"; + private static final String USER_TENANTS_MOCK = "userTenants"; + + private static final String PIECES_PATH = BASE_MOCK_DATA_PATH + "pieces/"; + private static final String PIECES_MOCK = "pieces-for-user-tenants"; + @Autowired PieceStorageService pieceStorageService; @Autowired ConsortiumConfigurationService consortiumConfigurationService; + @Autowired + ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever; + @Autowired private RestClient restClientMock; @@ -133,6 +146,55 @@ void testShouldDeleteItems() { verify(pieceStorageService, times(1)).deletePiece(any(String.class), eq(requestContext)); } + @Test + void testGetPiecesFilterByUserTenants(VertxTestContext vertxTestContext) { + var userTenantsMockData = getMockAsJson(USER_TENANTS_PATH, USER_TENANTS_MOCK); + var piecesMockData = getMockAsJson(PIECES_PATH, PIECES_MOCK).mapTo(PieceCollection.class); + var consortiumConfiguration = Optional.of(new ConsortiumConfiguration("tenantId0", UUID.randomUUID().toString())); + + doReturn(Future.succeededFuture(consortiumConfiguration)).when(consortiumConfigurationService).getConsortiumConfiguration(any(RequestContext.class)); + doReturn(Future.succeededFuture(piecesMockData)).when(restClientMock).get(any(RequestEntry.class), eq(PieceCollection.class), any(RequestContext.class)); + doReturn(Future.succeededFuture(userTenantsMockData)).when(restClientMock).getAsJsonObject(any(), any(RequestContext.class)); + + var future = pieceStorageService.getPieces(Integer.MAX_VALUE, 0, null, requestContext); + + verify(restClientMock, times(1)).get(any(RequestEntry.class), eq(PieceCollection.class), eq(requestContext)); + verify(restClientMock, times(1)).getAsJsonObject(any(), any(RequestContext.class)); + verify(consortiumConfigurationService, times(1)).getConsortiumConfiguration(eq(requestContext)); + + vertxTestContext.assertComplete(future) + .onComplete(f -> { + var result = f.result(); + assertThat(result).isNotNull(); + assertThat(result.getTotalRecords()).isEqualTo(2); + assertThat(result.getPieces()).hasSize(2); + vertxTestContext.completeNow(); + }); + } + + @Test + void testGetPiecesFilterByUserTenantsNonECS(VertxTestContext vertxTestContext) { + var piecesMockData = getMockAsJson(PIECES_PATH, PIECES_MOCK).mapTo(PieceCollection.class); + + doReturn(Future.succeededFuture(Optional.empty())).when(consortiumConfigurationService).getConsortiumConfiguration(any(RequestContext.class)); + doReturn(Future.succeededFuture(piecesMockData)).when(restClientMock).get(any(RequestEntry.class), eq(PieceCollection.class), any(RequestContext.class)); + + var future = pieceStorageService.getPieces(Integer.MAX_VALUE, 0, null, requestContext); + + verify(restClientMock, times(1)).get(any(RequestEntry.class), eq(PieceCollection.class), eq(requestContext)); + verify(restClientMock, times(0)).getAsJsonObject(any(), any(RequestContext.class)); + verify(consortiumConfigurationService, times(1)).getConsortiumConfiguration(eq(requestContext)); + + vertxTestContext.assertComplete(future) + .onComplete(f -> { + var result = f.result(); + assertThat(result).isNotNull(); + assertThat(result.getTotalRecords()).isEqualTo(3); + assertThat(result.getPieces()).hasSize(3); + vertxTestContext.completeNow(); + }); + } + private static class ContextConfiguration { @Bean diff --git a/src/test/resources/mockdata/pieces/pieces-for-user-tenants.json b/src/test/resources/mockdata/pieces/pieces-for-user-tenants.json new file mode 100644 index 000000000..00ace7c2d --- /dev/null +++ b/src/test/resources/mockdata/pieces/pieces-for-user-tenants.json @@ -0,0 +1,16 @@ +{ + "pieces": [ + { + "id": "05a95f03-eb00-4248-9f2e-2bd05957ff04", + "receivingTenantId": "tenantId1" + }, + { + "id": "05a95f03-eb00-4248-9f2e-2bd05957ff05", + "receivingTenantId": "tenantId2" + }, + { + "id": "05a95f03-eb00-4248-9f2e-2bd05957ff06" + } + ], + "totalRecords": 3 +} diff --git a/src/test/resources/mockdata/userTenants/userTenants.json b/src/test/resources/mockdata/userTenants/userTenants.json new file mode 100644 index 000000000..bcc9aee04 --- /dev/null +++ b/src/test/resources/mockdata/userTenants/userTenants.json @@ -0,0 +1,13 @@ +{ + "userTenants": [ + { + "id": "05c34b3d-33fa-46fc-8906-4ade19a570fd", + "userId": "440c89e3-7f6c-578a-9ea8-310dad23605e", + "username": "testUser", + "tenantId": "tenantId1", + "tenantName": "Tenant1", + "isPrimary": true + } + ], + "totalRecords": 1 +}