Business date part 1 (#2362)

Business date implementation - part 1
This commit is contained in:
Adam Saghy
2022-06-14 19:05:52 +02:00
committed by GitHub
parent a38fc32315
commit 4465e632f1
42 changed files with 1998 additions and 125 deletions
@@ -0,0 +1,163 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.businessdate.api;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.ws.rs.core.UriInfo;
import org.apache.fineract.commands.service.PortfolioCommandSourceWritePlatformService;
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateData;
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;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
class BusinessDateApiTest {
@Mock
private ApiRequestParameterHelper parameterHelper;
@Mock
private BusinessDateReadPlatformService readPlatformService;
@Mock
private UriInfo uriInfo;
@Mock
private PlatformSecurityContext securityContext;
@Mock
private DefaultToApiJsonSerializer<BusinessDateData> jsonSerializer;
@Mock
private PortfolioCommandSourceWritePlatformService commandWritePlatformService;
@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 {
AppUser appUser = Mockito.mock(AppUser.class);
List<BusinessDateData> response = Mockito.mock(List.class);
given(readPlatformService.findAll()).willReturn(response);
// given
Mockito.doNothing().when(appUser).validateHasReadPermission("BUSINESS_DATE");
given(securityContext.authenticatedUser()).willReturn(appUser);
// when
underTest.getBusinessDates(uriInfo);
// then
verify(readPlatformService, Mockito.times(1)).findAll();
verify(jsonSerializer, Mockito.times(1)).serialize(apiRequestJsonSerializationSettings, response);
}
@Test
void getBusinessDatesAPIHasNoPermission() throws ServletException, IOException {
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);
// then
verifyNoInteractions(readPlatformService);
}
@Test
void getBusinessDateByTypeAPIHasPermission() throws ServletException, IOException {
AppUser appUser = Mockito.mock(AppUser.class);
BusinessDateData response = Mockito.mock(BusinessDateData.class);
given(readPlatformService.findByType("type")).willReturn(response);
// given
Mockito.doNothing().when(appUser).validateHasReadPermission("BUSINESS_DATE");
given(securityContext.authenticatedUser()).willReturn(appUser);
// when
underTest.getBusinessDate("type", uriInfo);
// then
verify(readPlatformService, Mockito.times(1)).findByType("type");
verify(jsonSerializer, Mockito.times(1)).serialize(apiRequestJsonSerializationSettings, response);
}
@Test
void getBusinessDateByTypeAPIHasNoPermission() throws ServletException, IOException {
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);
// then
verifyNoInteractions(readPlatformService);
}
@Test
void postBusinessDateAPIHasPermission() throws ServletException, IOException {
AppUser appUser = Mockito.mock(AppUser.class);
CommandProcessingResult response = Mockito.mock(CommandProcessingResult.class);
// given
Mockito.doNothing().when(appUser).validateHasUpdatePermission("BUSINESS_DATE");
given(securityContext.authenticatedUser()).willReturn(appUser);
given(commandWritePlatformService.logCommandSource(Mockito.any())).willReturn(response);
// when
underTest.updateBusinessDate("{}", uriInfo);
// then
verify(commandWritePlatformService, Mockito.times(1)).logCommandSource(Mockito.any());
verify(jsonSerializer, Mockito.times(1)).serialize(response);
}
@Test
void postBusinessDateAPIHasNoPermission() throws ServletException, IOException {
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);
// then
verifyNoInteractions(readPlatformService);
}
}
@@ -0,0 +1,63 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.businessdate.data;
import static org.junit.Assert.assertEquals;
import java.time.LocalDate;
import java.time.ZoneId;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.apache.fineract.infrastructure.core.serialization.CommandProcessingResultJsonSerializer;
import org.apache.fineract.infrastructure.core.serialization.DefaultToApiJsonSerializer;
import org.apache.fineract.infrastructure.core.serialization.ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson;
import org.apache.fineract.infrastructure.core.serialization.ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson;
import org.apache.fineract.infrastructure.core.serialization.GoogleGsonSerializerHelper;
import org.junit.Test;
public class BusinessDataSerialized {
@Test
public void serializeBusinessDateData() {
DefaultToApiJsonSerializer<BusinessDateData> jsonSerializer = new DefaultToApiJsonSerializer<>(
new ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson(),
new ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson(), new CommandProcessingResultJsonSerializer(),
new GoogleGsonSerializerHelper());
LocalDate now = LocalDate.now(ZoneId.systemDefault());
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.BUSINESS_DATE, now);
String result = jsonSerializer.serialize(businessDateData);
assertEquals("{\"description\":\"Business Date\",\"type\":\"BUSINESS_DATE\",\"date\":[" + now.getYear() + "," + now.getMonthValue()
+ "," + now.getDayOfMonth() + "]}", result);
}
@Test
public void serializeBusinessDateData_COB() {
DefaultToApiJsonSerializer<BusinessDateData> jsonSerializer = new DefaultToApiJsonSerializer<>(
new ExcludeNothingWithPrettyPrintingOffJsonSerializerGoogleGson(),
new ExcludeNothingWithPrettyPrintingOnJsonSerializerGoogleGson(), new CommandProcessingResultJsonSerializer(),
new GoogleGsonSerializerHelper());
LocalDate now = LocalDate.now(ZoneId.systemDefault());
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.COB_DATE, now);
String result = jsonSerializer.serialize(businessDateData);
assertEquals("{\"description\":\"Close of Business Date\",\"type\":\"COB_DATE\",\"date\":[" + now.getYear() + ","
+ now.getMonthValue() + "," + now.getDayOfMonth() + "]}", result);
}
}
@@ -0,0 +1,37 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.businessdate.data;
import static org.junit.Assert.assertEquals;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.junit.Test;
public class BusinessDataTypeTest {
@Test
public void typoCheck() {
for (BusinessDateType businessDateType : BusinessDateType.values()) {
switch (businessDateType) {
case BUSINESS_DATE -> assertEquals("Business Date", businessDateType.getDescription());
case COB_DATE -> assertEquals("Close of Business Date", businessDateType.getDescription());
}
}
}
}
@@ -0,0 +1,56 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.businessdate.mapper;
import static org.junit.Assert.assertEquals;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Collections;
import java.util.List;
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateData;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDate;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.junit.Test;
import org.mapstruct.factory.Mappers;
public class BusinessDateMapperTest {
private BusinessDateMapper businessDateMapper = Mappers.getMapper(BusinessDateMapper.class);
@Test
public void testMapping() {
LocalDate now = LocalDate.now(ZoneId.systemDefault());
BusinessDate businessDate = BusinessDate.instance(BusinessDateType.BUSINESS_DATE, now);
BusinessDateData businessDateData = businessDateMapper.map(businessDate);
assertEquals(businessDate.getDate(), businessDateData.getDate());
assertEquals(businessDate.getType().getDescription(), businessDateData.getDescription());
assertEquals(businessDate.getType().getName(), businessDateData.getType());
}
@Test
public void testMappingList() {
LocalDate now = LocalDate.now(ZoneId.systemDefault());
BusinessDate businessDate = BusinessDate.instance(BusinessDateType.BUSINESS_DATE, now);
List<BusinessDateData> businessDateData = businessDateMapper.map(Collections.singletonList(businessDate));
assertEquals(businessDate.getDate(), businessDateData.get(0).getDate());
assertEquals(businessDate.getType().getDescription(), businessDateData.get(0).getDescription());
assertEquals(businessDate.getType().getName(), businessDateData.get(0).getType());
}
}
@@ -0,0 +1,96 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.businessdate.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.List;
import java.util.Optional;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDate;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateRepository;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.apache.fineract.infrastructure.businessdate.exception.BusinessDateNotFoundException;
import org.apache.fineract.infrastructure.businessdate.mapper.BusinessDateMapper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class BusinessDateReadPlatformServiceTest {
@InjectMocks
private BusinessDateReadPlatformServiceImpl businessDateReadPlatformService;
@Mock
private BusinessDateRepository repository;
@Mock
private BusinessDateMapper mapper;
@Test
public void notFoundByTypeNonexistentType() {
BusinessDateNotFoundException businessDateNotFoundException = assertThrows(BusinessDateNotFoundException.class,
() -> businessDateReadPlatformService.findByType("invalid"));
assertEquals("Business date with type `invalid` does not exist.", businessDateNotFoundException.getDefaultUserMessage());
}
@Test
public void notFoundByTypeNotStoredInDB() {
BusinessDateNotFoundException businessDateNotFoundException = assertThrows(BusinessDateNotFoundException.class,
() -> businessDateReadPlatformService.findByType("BUSINESS_DATE"));
assertEquals("Business date with type `BUSINESS_DATE` does not found.", businessDateNotFoundException.getDefaultUserMessage());
}
@Test
public void findAll() {
List<BusinessDate> resultList = Mockito.mock(List.class);
given(repository.findAll()).willReturn(resultList);
businessDateReadPlatformService.findAll();
verify(repository, times(1)).findAll();
verify(mapper, times(1)).map(resultList);
}
@Test
public void findByCOBType() {
Optional<BusinessDate> result = Optional.of(Mockito.mock(BusinessDate.class));
given(repository.findByType(BusinessDateType.COB_DATE)).willReturn(result);
businessDateReadPlatformService.findByType("COB_DATE");
verify(repository, times(1)).findByType(BusinessDateType.COB_DATE);
verify(mapper, times(1)).map(result.get());
}
@Test
public void findByBusinessType() {
Optional<BusinessDate> result = Optional.of(Mockito.mock(BusinessDate.class));
given(repository.findByType(BusinessDateType.BUSINESS_DATE)).willReturn(result);
businessDateReadPlatformService.findByType("BUSINESS_DATE");
verify(repository, times(1)).findByType(BusinessDateType.BUSINESS_DATE);
verify(mapper, times(1)).map(result.get());
}
}
@@ -0,0 +1,264 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.businessdate.service;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Optional;
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateData;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDate;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateRepository;
import org.apache.fineract.infrastructure.businessdate.domain.BusinessDateType;
import org.apache.fineract.infrastructure.businessdate.exception.BusinessDateActionException;
import org.apache.fineract.infrastructure.businessdate.validator.BusinessDateDataParserAndValidator;
import org.apache.fineract.infrastructure.configuration.domain.ConfigurationDomainService;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.data.CommandProcessingResult;
import org.apache.fineract.infrastructure.core.service.DateUtils;
import org.apache.fineract.infrastructure.jobs.exception.JobExecutionException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public class BusinessDateWritePlatformServiceTest {
@InjectMocks
private BusinessDateWritePlatformServiceImpl underTest;
@Mock
private BusinessDateDataParserAndValidator businessDateDataParserAndValidator;
@Mock
private BusinessDateRepository businessDateRepository;
@Mock
private ConfigurationDomainService configurationDomainService;
@Captor
private ArgumentCaptor<BusinessDate> businessDateArgumentCaptor;
@Test
public void businessDateIsNotEnabled() {
JsonCommand command = JsonCommand.from("");
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.BUSINESS_DATE,
LocalDate.now(ZoneId.systemDefault()));
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.FALSE);
given(businessDateDataParserAndValidator.validateAndParseUpdate(command)).willReturn(businessDateData);
BusinessDateActionException exception = assertThrows(BusinessDateActionException.class,
() -> underTest.updateBusinessDate(command));
assertEquals("Business date functionality is not enabled", exception.getDefaultUserMessage());
}
@Test
public void businessDateSetNew() {
JsonCommand command = JsonCommand.from("");
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 13));
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.TRUE);
given(configurationDomainService.isCOBDateAdjustmentEnabled()).willReturn(Boolean.FALSE);
given(businessDateDataParserAndValidator.validateAndParseUpdate(command)).willReturn(businessDateData);
Optional<BusinessDate> newEntity = Optional.empty();
given(businessDateRepository.findByType(BusinessDateType.BUSINESS_DATE)).willReturn(newEntity);
CommandProcessingResult result = underTest.updateBusinessDate(command);
LocalDate resultData = (LocalDate) result.getChanges().get("BUSINESS_DATE");
assertEquals(LocalDate.of(2022, 6, 13), resultData);
verify(configurationDomainService, times(1)).isBusinessDateEnabled();
verify(configurationDomainService, times(1)).isCOBDateAdjustmentEnabled();
verify(businessDateRepository, times(1)).findByType(BusinessDateType.BUSINESS_DATE);
verify(businessDateRepository, times(1)).save(businessDateArgumentCaptor.capture());
assertEquals(LocalDate.of(2022, 6, 13), businessDateArgumentCaptor.getValue().getDate());
assertEquals(BusinessDateType.BUSINESS_DATE, businessDateArgumentCaptor.getValue().getType());
}
@Test
public void cobDateSetNew() {
JsonCommand command = JsonCommand.from("");
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.COB_DATE, LocalDate.of(2022, 6, 13));
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.TRUE);
given(configurationDomainService.isCOBDateAdjustmentEnabled()).willReturn(Boolean.FALSE);
given(businessDateDataParserAndValidator.validateAndParseUpdate(command)).willReturn(businessDateData);
Optional<BusinessDate> newEntity = Optional.empty();
given(businessDateRepository.findByType(BusinessDateType.COB_DATE)).willReturn(newEntity);
CommandProcessingResult result = underTest.updateBusinessDate(command);
LocalDate resultData = (LocalDate) result.getChanges().get("COB_DATE");
assertEquals(LocalDate.of(2022, 6, 13), resultData);
verify(configurationDomainService, times(1)).isBusinessDateEnabled();
verify(configurationDomainService, times(1)).isCOBDateAdjustmentEnabled();
verify(businessDateRepository, times(1)).findByType(BusinessDateType.COB_DATE);
verify(businessDateRepository, times(1)).save(businessDateArgumentCaptor.capture());
assertEquals(LocalDate.of(2022, 6, 13), businessDateArgumentCaptor.getValue().getDate());
assertEquals(BusinessDateType.COB_DATE, businessDateArgumentCaptor.getValue().getType());
}
@Test
public void businessDateSetModifyExistingWhenItWasAfter() {
JsonCommand command = JsonCommand.from("");
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 11));
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.TRUE);
given(configurationDomainService.isCOBDateAdjustmentEnabled()).willReturn(Boolean.FALSE);
given(businessDateDataParserAndValidator.validateAndParseUpdate(command)).willReturn(businessDateData);
Optional<BusinessDate> newEntity = Optional.of(BusinessDate.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 12)));
given(businessDateRepository.findByType(BusinessDateType.BUSINESS_DATE)).willReturn(newEntity);
CommandProcessingResult result = underTest.updateBusinessDate(command);
LocalDate resultData = (LocalDate) result.getChanges().get("BUSINESS_DATE");
assertEquals(LocalDate.of(2022, 6, 11), resultData);
verify(configurationDomainService, times(1)).isBusinessDateEnabled();
verify(configurationDomainService, times(1)).isCOBDateAdjustmentEnabled();
verify(businessDateRepository, times(1)).findByType(BusinessDateType.BUSINESS_DATE);
verify(businessDateRepository, times(1)).save(businessDateArgumentCaptor.capture());
assertEquals(LocalDate.of(2022, 6, 11), businessDateArgumentCaptor.getValue().getDate());
assertEquals(BusinessDateType.BUSINESS_DATE, businessDateArgumentCaptor.getValue().getType());
}
@Test
public void businessDateSetModifyExistingWhenItWasBefore() {
JsonCommand command = JsonCommand.from("");
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 13));
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.TRUE);
given(configurationDomainService.isCOBDateAdjustmentEnabled()).willReturn(Boolean.FALSE);
given(businessDateDataParserAndValidator.validateAndParseUpdate(command)).willReturn(businessDateData);
Optional<BusinessDate> newEntity = Optional.of(BusinessDate.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 12)));
given(businessDateRepository.findByType(BusinessDateType.BUSINESS_DATE)).willReturn(newEntity);
CommandProcessingResult result = underTest.updateBusinessDate(command);
LocalDate resultData = (LocalDate) result.getChanges().get("BUSINESS_DATE");
assertEquals(LocalDate.of(2022, 6, 13), resultData);
verify(configurationDomainService, times(1)).isBusinessDateEnabled();
verify(configurationDomainService, times(1)).isCOBDateAdjustmentEnabled();
verify(businessDateRepository, times(1)).findByType(BusinessDateType.BUSINESS_DATE);
verify(businessDateRepository, times(1)).save(businessDateArgumentCaptor.capture());
assertEquals(LocalDate.of(2022, 6, 13), businessDateArgumentCaptor.getValue().getDate());
assertEquals(BusinessDateType.BUSINESS_DATE, businessDateArgumentCaptor.getValue().getType());
}
@Test
public void businessDateSetModifyExistingButNoChanges() {
JsonCommand command = JsonCommand.from("");
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 13));
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.TRUE);
given(configurationDomainService.isCOBDateAdjustmentEnabled()).willReturn(Boolean.FALSE);
given(businessDateDataParserAndValidator.validateAndParseUpdate(command)).willReturn(businessDateData);
Optional<BusinessDate> newEntity = Optional.of(BusinessDate.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 13)));
given(businessDateRepository.findByType(BusinessDateType.BUSINESS_DATE)).willReturn(newEntity);
CommandProcessingResult result = underTest.updateBusinessDate(command);
assertNull(result.getChanges());
verify(configurationDomainService, times(1)).isBusinessDateEnabled();
verify(configurationDomainService, times(1)).isCOBDateAdjustmentEnabled();
verify(businessDateRepository, times(1)).findByType(BusinessDateType.BUSINESS_DATE);
verify(businessDateRepository, times(0)).save(businessDateArgumentCaptor.capture());
}
@Test
public void cobDateSetNewAutomatically() {
JsonCommand command = JsonCommand.from("");
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 13));
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.TRUE);
given(configurationDomainService.isCOBDateAdjustmentEnabled()).willReturn(Boolean.TRUE);
given(businessDateDataParserAndValidator.validateAndParseUpdate(command)).willReturn(businessDateData);
Optional<BusinessDate> newEntity = Optional.empty();
given(businessDateRepository.findByType(BusinessDateType.BUSINESS_DATE)).willReturn(newEntity);
CommandProcessingResult result = underTest.updateBusinessDate(command);
LocalDate businessDate = (LocalDate) result.getChanges().get("BUSINESS_DATE");
assertEquals(LocalDate.of(2022, 6, 13), businessDate);
LocalDate cobDate = (LocalDate) result.getChanges().get("COB_DATE");
assertEquals(LocalDate.of(2022, 6, 12), cobDate);
verify(configurationDomainService, times(1)).isBusinessDateEnabled();
verify(configurationDomainService, times(1)).isCOBDateAdjustmentEnabled();
verify(businessDateRepository, times(1)).findByType(BusinessDateType.BUSINESS_DATE);
verify(businessDateRepository, times(1)).findByType(BusinessDateType.COB_DATE);
verify(businessDateRepository, times(2)).save(businessDateArgumentCaptor.capture());
assertEquals(LocalDate.of(2022, 6, 13), businessDateArgumentCaptor.getAllValues().get(0).getDate());
assertEquals(BusinessDateType.BUSINESS_DATE, businessDateArgumentCaptor.getAllValues().get(0).getType());
assertEquals(LocalDate.of(2022, 6, 12), businessDateArgumentCaptor.getAllValues().get(1).getDate());
assertEquals(BusinessDateType.COB_DATE, businessDateArgumentCaptor.getAllValues().get(1).getType());
}
@Test
public void businessDateAndCobDateSetModifyExistingButNoChanges() {
JsonCommand command = JsonCommand.from("");
BusinessDateData businessDateData = BusinessDateData.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 13));
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.TRUE);
given(configurationDomainService.isCOBDateAdjustmentEnabled()).willReturn(Boolean.TRUE);
given(businessDateDataParserAndValidator.validateAndParseUpdate(command)).willReturn(businessDateData);
Optional<BusinessDate> newBusinessEntity = Optional
.of(BusinessDate.instance(BusinessDateType.BUSINESS_DATE, LocalDate.of(2022, 6, 13)));
Optional<BusinessDate> newCOBEntity = Optional.of(BusinessDate.instance(BusinessDateType.COB_DATE, LocalDate.of(2022, 6, 12)));
given(businessDateRepository.findByType(BusinessDateType.BUSINESS_DATE)).willReturn(newBusinessEntity);
given(businessDateRepository.findByType(BusinessDateType.COB_DATE)).willReturn(newCOBEntity);
CommandProcessingResult result = underTest.updateBusinessDate(command);
assertNull(result.getChanges());
verify(configurationDomainService, times(1)).isBusinessDateEnabled();
verify(configurationDomainService, times(1)).isCOBDateAdjustmentEnabled();
verify(businessDateRepository, times(1)).findByType(BusinessDateType.BUSINESS_DATE);
verify(businessDateRepository, times(1)).findByType(BusinessDateType.COB_DATE);
verify(businessDateRepository, times(0)).save(Mockito.any());
}
@Test
public void businessDateIsNotEnabledTriggeredByJob() {
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.FALSE);
assertThrows(JobExecutionException.class, () -> underTest.increaseBusinessDateByOneDay());
}
@Test
public void businessDateSetNewTriggeredByJob() throws JobExecutionException {
LocalDate localDate = DateUtils.getLocalDateOfTenant();
LocalDate localDatePlus1 = localDate.plusDays(1);
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.TRUE);
given(configurationDomainService.isCOBDateAdjustmentEnabled()).willReturn(Boolean.TRUE);
Optional<BusinessDate> newEntity = Optional.empty();
given(businessDateRepository.findByType(BusinessDateType.BUSINESS_DATE)).willReturn(newEntity);
underTest.increaseBusinessDateByOneDay();
verify(configurationDomainService, times(1)).isBusinessDateEnabled();
verify(configurationDomainService, times(1)).isCOBDateAdjustmentEnabled();
verify(businessDateRepository, times(2)).save(businessDateArgumentCaptor.capture());
assertEquals(localDatePlus1, businessDateArgumentCaptor.getAllValues().get(0).getDate());
assertEquals(BusinessDateType.BUSINESS_DATE, businessDateArgumentCaptor.getAllValues().get(0).getType());
assertEquals(localDate, businessDateArgumentCaptor.getAllValues().get(1).getDate());
assertEquals(BusinessDateType.COB_DATE, businessDateArgumentCaptor.getAllValues().get(1).getType());
}
@Test
public void cobDateModifyExistingTriggeredByJob() throws JobExecutionException {
Optional<BusinessDate> newCOBEntity = Optional.of(BusinessDate.instance(BusinessDateType.COB_DATE, LocalDate.of(2022, 6, 12)));
given(businessDateRepository.findByType(BusinessDateType.COB_DATE)).willReturn(newCOBEntity);
LocalDate localDate = LocalDate.of(2022, 6, 12).plusDays(1);
given(configurationDomainService.isBusinessDateEnabled()).willReturn(Boolean.TRUE);
given(configurationDomainService.isCOBDateAdjustmentEnabled()).willReturn(Boolean.TRUE);
underTest.increaseCOBDateByOneDay();
verify(configurationDomainService, times(1)).isBusinessDateEnabled();
verify(configurationDomainService, times(1)).isCOBDateAdjustmentEnabled();
verify(businessDateRepository, times(1)).save(businessDateArgumentCaptor.capture());
assertEquals(localDate, businessDateArgumentCaptor.getValue().getDate());
}
}
@@ -0,0 +1,120 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.fineract.infrastructure.businessdate.validator;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import java.time.LocalDate;
import org.apache.fineract.infrastructure.businessdate.data.BusinessDateData;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.exception.PlatformApiDataValidationException;
import org.apache.fineract.infrastructure.core.serialization.FromJsonHelper;
import org.junit.Test;
public class BusinessDateValidatorTest {
private BusinessDateDataParserAndValidator businessDateDataParserAndValidator = new BusinessDateDataParserAndValidator(
new FromJsonHelper());
@Test
public void validateAndParseUpdateWithEmptyRequest() {
String json = "{}";
JsonCommand command = JsonCommand.from(json);
PlatformApiDataValidationException exception = assertThrows(PlatformApiDataValidationException.class,
() -> businessDateDataParserAndValidator.validateAndParseUpdate(command));
assertEquals(
"PlatformApiDataValidationException{errors=[The parameter `type` is mandatory., The parameter `locale` is mandatory., The parameter `dateFormat` is mandatory., The parameter `date` is mandatory.]}",
exception.toString());
}
@Test
public void validateAndParseUpdateWithBlankFieldsInRequest() {
String json = "{\"type\":\"\", \"locale\":\"\",\"dateFormat\":\"\",\"date\":\"\"}";
JsonCommand command = JsonCommand.from(json);
PlatformApiDataValidationException exception = assertThrows(PlatformApiDataValidationException.class,
() -> businessDateDataParserAndValidator.validateAndParseUpdate(command));
assertEquals(
"PlatformApiDataValidationException{errors=[The parameter `type` is mandatory., The parameter `locale` is mandatory., The parameter `dateFormat` is mandatory., The parameter `date` is mandatory.]}",
exception.toString());
}
@Test
public void validateAndParseUpdateWithInvalidLocale() {
String json = "{\"type\":\"BUSINESS_DATE\", \"locale\":\"invalid\",\"dateFormat\":\"yyyy-MM-dd\",\"date\":\"2022-06-11\"}";
JsonCommand command = JsonCommand.from(json);
PlatformApiDataValidationException exception = assertThrows(PlatformApiDataValidationException.class,
() -> businessDateDataParserAndValidator.validateAndParseUpdate(command));
assertEquals("PlatformApiDataValidationException{errors=[The parameter `locale` has an invalid language value: `invalid`.]}",
exception.toString());
}
@Test
public void validateAndParseUpdateWithInvalidBusinessType() {
String json = "{\"type\":\"invalid\", \"locale\":\"hu\",\"dateFormat\":\"yyyy-MM-dd\",\"date\":\"2022-06-11\"}";
JsonCommand command = JsonCommand.from(json);
PlatformApiDataValidationException exception = assertThrows(PlatformApiDataValidationException.class,
() -> businessDateDataParserAndValidator.validateAndParseUpdate(command));
assertEquals("PlatformApiDataValidationException{errors=[Failed data validation due to: Invalid Business Type value: `invalid`.]}",
exception.toString());
}
@Test
public void validateAndParseUpdateWithInvalidDateFormat() {
String json = "{\"type\":\"BUSINESS_DATE\", \"locale\":\"hu\",\"dateFormat\":\"y2yyy-MM-dd\",\"date\":\"2022-06-11\"}";
JsonCommand command = JsonCommand.from(json);
PlatformApiDataValidationException exception = assertThrows(PlatformApiDataValidationException.class,
() -> businessDateDataParserAndValidator.validateAndParseUpdate(command));
assertEquals(
"PlatformApiDataValidationException{errors=[The parameter `date` (value=2022-06-11) is invalid based on the dateFormat: `y2yyy-MM-dd` and locale: `hu` provided.]}",
exception.toString());
}
@Test
public void validateAndParseUpdateWithInvalidDate() {
String json = "{\"type\":\"BUSINESS_DATE\", \"locale\":\"hu\",\"dateFormat\":\"yyyy-MM-dd\",\"date\":\"2y22-06-11\"}";
JsonCommand command = JsonCommand.from(json);
PlatformApiDataValidationException exception = assertThrows(PlatformApiDataValidationException.class,
() -> businessDateDataParserAndValidator.validateAndParseUpdate(command));
assertEquals(
"PlatformApiDataValidationException{errors=[The parameter `date` (value=2y22-06-11) is invalid based on the dateFormat: `yyyy-MM-dd` and locale: `hu` provided.]}",
exception.toString());
}
@Test
public void validateAndParseUpdateWithWrongDate() {
String json = "{\"type\":\"BUSINESS_DATE\", \"locale\":\"hu\",\"dateFormat\":\"yyyy-MM-dd\",\"date\":\"11-06-2022\"}";
JsonCommand command = JsonCommand.from(json);
PlatformApiDataValidationException exception = assertThrows(PlatformApiDataValidationException.class,
() -> businessDateDataParserAndValidator.validateAndParseUpdate(command));
assertEquals(
"PlatformApiDataValidationException{errors=[The parameter `date` (value=11-06-2022) is invalid based on the dateFormat: `yyyy-MM-dd` and locale: `hu` provided.]}",
exception.toString());
}
@Test
public void validateAndParseUpdateWithRighDate() {
String json = "{\"type\":\"COB_DATE\", \"locale\":\"hu\",\"dateFormat\":\"yyyy-MM-dd\",\"date\":\"2022-06-11\"}";
JsonCommand command = JsonCommand.from(json);
BusinessDateData result = businessDateDataParserAndValidator.validateAndParseUpdate(command);
assertEquals("COB_DATE", result.getType());
assertEquals("Close of Business Date", result.getDescription());
assertEquals(LocalDate.of(2022, 6, 11), result.getDate());
}
}