FINERACT-2169: Business date api refactor;

This commit is contained in:
viktorpavlenko
2025-03-14 18:10:48 +00:00
committed by Adam Saghy
parent 99f313e02c
commit 35aa6e1f4f
9 changed files with 77 additions and 96 deletions
@@ -23,21 +23,16 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import jakarta.servlet.ServletException;
import jakarta.ws.rs.core.UriInfo;
import java.io.IOException;
import java.util.List;
import org.apache.fineract.commands.service.PortfolioCommandSourceWritePlatformService;
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateData;
import org.apache.fineract.infrastructure.businessdate.data.request.BusinessDateRequest;
import org.apache.fineract.infrastructure.businessdate.service.BusinessDateReadPlatformService;
import org.apache.fineract.infrastructure.core.api.ApiRequestParameterHelper;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.serialization.ApiRequestJsonSerializationSettings;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.security.exception.NoAuthorizationException;
import org.apache.fineract.infrastructure.security.service.PlatformSecurityContext;
import org.apache.fineract.useradministration.domain.AppUser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
@@ -51,15 +46,9 @@ import org.mockito.quality.Strictness;
@MockitoSettings(strictness = Strictness.LENIENT)
class BusinessDateApiTest {
@Mock
private ApiRequestParameterHelper parameterHelper;
@Mock
private BusinessDateReadPlatformService readPlatformService;
@Mock
private UriInfo uriInfo;
@Mock
private PlatformSecurityContext securityContext;
@@ -72,16 +61,8 @@ class BusinessDateApiTest {
@InjectMocks
private BusinessDateApiResource underTest;
private ApiRequestJsonSerializationSettings apiRequestJsonSerializationSettings;
@BeforeEach
void setUp() throws IOException {
apiRequestJsonSerializationSettings = new ApiRequestJsonSerializationSettings(false, null, false, false, false);
given(parameterHelper.process(Mockito.any())).willReturn(apiRequestJsonSerializationSettings);
}
@Test
void getBusinessDatesAPIHasPermission() throws ServletException, IOException {
void getBusinessDatesAPIHasPermission() {
AppUser appUser = Mockito.mock(AppUser.class);
List<BusinessDateData> response = Mockito.mock(List.class);
given(readPlatformService.findAll()).willReturn(response);
@@ -89,26 +70,25 @@ class BusinessDateApiTest {
Mockito.doNothing().when(appUser).validateHasReadPermission("BUSINESS_DATE");
given(securityContext.authenticatedUser()).willReturn(appUser);
// when
underTest.getBusinessDates(uriInfo);
underTest.getBusinessDates();
// then
verify(readPlatformService, Mockito.times(1)).findAll();
verify(jsonSerializer, Mockito.times(1)).serialize(apiRequestJsonSerializationSettings, response);
}
@Test
void getBusinessDatesAPIHasNoPermission() throws ServletException, IOException {
void getBusinessDatesAPIHasNoPermission() {
AppUser appUser = Mockito.mock(AppUser.class);
// given
Mockito.doThrow(NoAuthorizationException.class).when(appUser).validateHasReadPermission("BUSINESS_DATE");
given(securityContext.authenticatedUser()).willReturn(appUser);
// when
assertThatThrownBy(() -> underTest.getBusinessDates(uriInfo)).isInstanceOf(NoAuthorizationException.class);
assertThatThrownBy(() -> underTest.getBusinessDates()).isInstanceOf(NoAuthorizationException.class);
// then
verifyNoInteractions(readPlatformService);
}
@Test
void getBusinessDateByTypeAPIHasPermission() throws ServletException, IOException {
void getBusinessDateByTypeAPIHasPermission() {
AppUser appUser = Mockito.mock(AppUser.class);
BusinessDateData response = Mockito.mock(BusinessDateData.class);
given(readPlatformService.findByType("type")).willReturn(response);
@@ -116,26 +96,25 @@ class BusinessDateApiTest {
Mockito.doNothing().when(appUser).validateHasReadPermission("BUSINESS_DATE");
given(securityContext.authenticatedUser()).willReturn(appUser);
// when
underTest.getBusinessDate("type", uriInfo);
underTest.getBusinessDate("type");
// then
verify(readPlatformService, Mockito.times(1)).findByType("type");
verify(jsonSerializer, Mockito.times(1)).serialize(apiRequestJsonSerializationSettings, response);
}
@Test
void getBusinessDateByTypeAPIHasNoPermission() throws ServletException, IOException {
void getBusinessDateByTypeAPIHasNoPermission() {
AppUser appUser = Mockito.mock(AppUser.class);
// given
Mockito.doThrow(NoAuthorizationException.class).when(appUser).validateHasReadPermission("BUSINESS_DATE");
given(securityContext.authenticatedUser()).willReturn(appUser);
// when
assertThatThrownBy(() -> underTest.getBusinessDate("type", uriInfo)).isInstanceOf(NoAuthorizationException.class);
assertThatThrownBy(() -> underTest.getBusinessDate("type")).isInstanceOf(NoAuthorizationException.class);
// then
verifyNoInteractions(readPlatformService);
}
@Test
void postBusinessDateAPIHasPermission() throws ServletException, IOException {
void postBusinessDateAPIHasPermission() {
AppUser appUser = Mockito.mock(AppUser.class);
CommandProcessingResult response = Mockito.mock(CommandProcessingResult.class);
// given
@@ -143,20 +122,19 @@ class BusinessDateApiTest {
given(securityContext.authenticatedUser()).willReturn(appUser);
given(commandWritePlatformService.logCommandSource(Mockito.any())).willReturn(response);
// when
underTest.updateBusinessDate("{}", uriInfo);
underTest.updateBusinessDate(BusinessDateRequest.ofNull());
// then
verify(commandWritePlatformService, Mockito.times(1)).logCommandSource(Mockito.any());
verify(jsonSerializer, Mockito.times(1)).serialize(response);
}
@Test
void postBusinessDateAPIHasNoPermission() throws ServletException, IOException {
void postBusinessDateAPIHasNoPermission() {
AppUser appUser = Mockito.mock(AppUser.class);
// given
Mockito.doThrow(NoAuthorizationException.class).when(appUser).validateHasUpdatePermission("BUSINESS_DATE");
given(securityContext.authenticatedUser()).willReturn(appUser);
// when
assertThatThrownBy(() -> underTest.updateBusinessDate("{}", uriInfo)).isInstanceOf(NoAuthorizationException.class);
assertThatThrownBy(() -> underTest.updateBusinessDate(BusinessDateRequest.ofNull())).isInstanceOf(NoAuthorizationException.class);
// then
verifyNoInteractions(readPlatformService);
}