Skip to content

Commit

Permalink
[MODORDERS-1174] Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Saba-Zedginidze-EPAM committed Sep 13, 2024
1 parent fabdc67 commit fd36c89
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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;
import static org.folio.TestConfig.getFirstContextFromVertx;
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;
Expand All @@ -21,11 +24,13 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
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;
Expand Down Expand Up @@ -54,9 +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;

Expand Down Expand Up @@ -106,6 +123,7 @@ void testPiecesShouldBeReturnedByQuery(VertxTestContext vertxTestContext) {
.withTotalRecords(1);

when(restClientMock.get(any(RequestEntry.class), any(), any())).thenReturn(Future.succeededFuture(pieceCollection));
when(consortiumConfigurationService.getConsortiumConfiguration(any(RequestContext.class))).thenReturn(Future.succeededFuture(Optional.empty()));

String expectedQuery = String.format("id==%s", pieceId);
var future = pieceStorageService.getPieces(Integer.MAX_VALUE, 0, expectedQuery, requestContext);
Expand All @@ -128,31 +146,84 @@ 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 RestClient restClient() {
@Bean
RestClient restClient() {
return mock(RestClient.class);
}

@Bean ProtectionService protectionService() {
@Bean
ProtectionService protectionService() {
return mock(ProtectionService.class);
}

@Bean PieceStorageService pieceStorageService(ConsortiumConfigurationService consortiumConfigurationService,
@Bean
PieceStorageService pieceStorageService(ConsortiumConfigurationService consortiumConfigurationService,
ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever,
RestClient restClient) {
return spy(new PieceStorageService(consortiumConfigurationService, consortiumUserTenantsRetriever, restClient));
}

@Bean
ConsortiumConfigurationService consortiumConfigurationService(RestClient restClient) {
return new ConsortiumConfigurationService(restClient);
return spy(new ConsortiumConfigurationService(restClient));
}

@Bean
ConsortiumUserTenantsRetriever consortiumUserTenantsRetriever(RestClient restClient) {
return new ConsortiumUserTenantsRetriever(restClient);
return spy(new ConsortiumUserTenantsRetriever(restClient));
}

}

}
16 changes: 16 additions & 0 deletions src/test/resources/mockdata/pieces/pieces-for-user-tenants.json
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions src/test/resources/mockdata/userTenants/userTenants.json
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit fd36c89

Please sign in to comment.