FINERACT-2181: initial rounding-mode configuration

This commit is contained in:
mark.vituska
2025-03-14 13:05:30 +00:00
committed by Adam Saghy
parent a86baeeef4
commit e44a60f327
6 changed files with 182 additions and 55 deletions
@@ -22,18 +22,12 @@ import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.Map;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.apache.fineract.infrastructure.configuration.api.GlobalConfigurationConstants;
import org.apache.fineract.infrastructure.configuration.data.GlobalConfigurationPropertyData;
import org.apache.fineract.infrastructure.configuration.exception.GlobalConfigurationPropertyCannotBeModfied;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.core.domain.AbstractPersistableCustom;
import org.apache.fineract.infrastructure.security.exception.ForcePasswordResetException;
@Entity
@Table(name = "c_configuration")
@@ -64,53 +58,6 @@ public class GlobalConfigurationProperty extends AbstractPersistableCustom<Long>
@Column(name = "is_trap_door", nullable = false)
private boolean isTrapDoor;
public Map<String, Object> update(final JsonCommand command) {
final Map<String, Object> actualChanges = new LinkedHashMap<>(7);
if (this.isTrapDoor) {
throw new GlobalConfigurationPropertyCannotBeModfied(this.getId());
}
final String enabledParamName = "enabled";
if (command.isChangeInBooleanParameterNamed(enabledParamName, this.enabled)) {
final Boolean newValue = command.booleanPrimitiveValueOfParameterNamed(enabledParamName);
actualChanges.put(enabledParamName, newValue);
this.enabled = newValue;
}
final String valueParamName = "value";
final Long previousValue = this.value;
if (command.isChangeInLongParameterNamed(valueParamName, this.value)) {
final Long newValue = command.longValueOfParameterNamed(valueParamName);
actualChanges.put(valueParamName, newValue);
this.value = newValue;
}
final String dateValueParamName = "dateValue";
if (command.isChangeInDateParameterNamed(dateValueParamName, this.dateValue)) {
final LocalDate newDateValue = command.localDateValueOfParameterNamed(dateValueParamName);
actualChanges.put(dateValueParamName, newDateValue);
this.dateValue = newDateValue;
}
final String stringValueParamName = "stringValue";
if (command.isChangeInStringParameterNamed(stringValueParamName, this.stringValue)) {
final String newStringValue = command.stringValueOfParameterNamed(stringValueParamName);
actualChanges.put(stringValueParamName, newStringValue);
this.stringValue = newStringValue;
}
final String passwordPropertyName = GlobalConfigurationConstants.FORCE_PASSWORD_RESET_DAYS;
if (this.name.equalsIgnoreCase(passwordPropertyName)) {
if ((this.enabled == true && command.hasParameter(valueParamName) && (this.value == 0))
|| (this.enabled == true && !command.hasParameter(valueParamName) && (previousValue == 0))) {
throw new ForcePasswordResetException();
}
}
return actualChanges;
}
public static GlobalConfigurationProperty newSurveyConfiguration(final String name) {
return new GlobalConfigurationProperty().setName(name);
}
@@ -0,0 +1,93 @@
/**
* 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.configuration.service;
import java.time.LocalDate;
import java.util.LinkedHashMap;
import java.util.Map;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.fineract.infrastructure.configuration.api.GlobalConfigurationConstants;
import org.apache.fineract.infrastructure.configuration.domain.GlobalConfigurationProperty;
import org.apache.fineract.infrastructure.configuration.exception.GlobalConfigurationPropertyCannotBeModfied;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.apache.fineract.infrastructure.security.exception.ForcePasswordResetException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service
@AllArgsConstructor
public class GlobalConfigurationPropertyUpdateService {
private final JdbcTemplate jdbcTemplate;
public Map<String, Object> update(final GlobalConfigurationProperty property, final JsonCommand command) {
final Map<String, Object> actualChanges = new LinkedHashMap<>(7);
if (property.isTrapDoor() && isAnyProductAlreadyCreated()) {
throw new GlobalConfigurationPropertyCannotBeModfied(property.getId());
}
final String enabledParamName = "enabled";
if (command.isChangeInBooleanParameterNamed(enabledParamName, property.isEnabled())) {
final boolean newValue = command.booleanPrimitiveValueOfParameterNamed(enabledParamName);
actualChanges.put(enabledParamName, newValue);
property.setEnabled(newValue);
}
final String valueParamName = "value";
final Long previousValue = property.getValue();
if (command.isChangeInLongParameterNamed(valueParamName, property.getValue())) {
final Long newValue = command.longValueOfParameterNamed(valueParamName);
actualChanges.put(valueParamName, newValue);
property.setValue(newValue);
}
final String dateValueParamName = "dateValue";
if (command.isChangeInDateParameterNamed(dateValueParamName, property.getDateValue())) {
final LocalDate newDateValue = command.localDateValueOfParameterNamed(dateValueParamName);
actualChanges.put(dateValueParamName, newDateValue);
property.setDateValue(newDateValue);
}
final String stringValueParamName = "stringValue";
if (command.isChangeInStringParameterNamed(stringValueParamName, property.getStringValue())) {
final String newStringValue = command.stringValueOfParameterNamed(stringValueParamName);
actualChanges.put(stringValueParamName, newStringValue);
property.setStringValue(newStringValue);
}
final String passwordPropertyName = GlobalConfigurationConstants.FORCE_PASSWORD_RESET_DAYS;
if (property.getName().equalsIgnoreCase(passwordPropertyName)) {
if ((property.isEnabled() && command.hasParameter(valueParamName) && (property.getValue() == 0))
|| (property.isEnabled() && !command.hasParameter(valueParamName) && (previousValue == 0))) {
throw new ForcePasswordResetException();
}
}
return actualChanges;
}
private boolean isAnyProductAlreadyCreated() {
String productExistenceSql = "SELECT EXISTS (SELECT 1 FROM m_loan) OR EXISTS (SELECT 1 FROM m_savings_account) OR"
+ " EXISTS (SELECT 1 FROM m_share_account) AS products_exist";
Boolean productsExist = this.jdbcTemplate.queryForObject(productExistenceSql, Boolean.class);
return BooleanUtils.isTrue(productsExist);
}
}
@@ -47,6 +47,7 @@ public class GlobalConfigurationWritePlatformServiceJpaRepositoryImpl implements
private final GlobalConfigurationRepositoryWrapper repository;
private final GlobalConfigurationDataValidator globalConfigurationDataValidator;
private final ConfigurationDomainService configurationDomainService;
private final GlobalConfigurationPropertyUpdateService globalConfigurationPropertyUpdateService;
@Transactional
@Override
@@ -56,7 +57,7 @@ public class GlobalConfigurationWritePlatformServiceJpaRepositoryImpl implements
final GlobalConfigurationProperty configItemForUpdate = this.repository.findOneWithNotFoundDetection(configId);
final Map<String, Object> changes = configItemForUpdate.update(command);
final Map<String, Object> changes = globalConfigurationPropertyUpdateService.update(configItemForUpdate, command);
if (!changes.isEmpty()) {
this.configurationDomainService.removeGlobalConfigurationPropertyDataFromCache(configItemForUpdate.getName());
@@ -46,6 +46,7 @@ fineract.tenant.read-only-name=${FINERACT_DEFAULT_TENANTDB_RO_NAME:}
fineract.tenant.config.min-pool-size=${FINERACT_CONFIG_MIN_POOL_SIZE:-1}
fineract.tenant.config.max-pool-size=${FINERACT_CONFIG_MAX_POOL_SIZE:-1}
fineract.tenant.config.rounding-mode=${FINERACT_CONFIG_ROUNDING_MODE:6}
fineract.mode.read-enabled=${FINERACT_MODE_READ_ENABLED:true}
fineract.mode.write-enabled=${FINERACT_MODE_WRITE_ENABLED:true}
@@ -410,6 +411,7 @@ spring.liquibase.parameters.fineract.tenant.read-only-username=${fineract.tenant
spring.liquibase.parameters.fineract.tenant.read-only-password=${fineract.tenant.read-only-password}
spring.liquibase.parameters.fineract.tenant.read-only-parameters=${fineract.tenant.read-only-parameters}
spring.liquibase.parameters.fineract.tenant.read-only-name=${fineract.tenant.read-only-name}
spring.liquibase.parameters.fineract.tenant.rounding-mode=${fineract.tenant.config.rounding-mode}
springdoc.api-docs.path=${SPRINGDOC_API_DOCS_PATH:/api-docs}
springdoc.api-docs.enabled=${SPRINGDOC_API_DOCS_ENABLED:true}
@@ -217,7 +217,7 @@
<insert tableName="c_configuration">
<column name="id" valueNumeric="23"/>
<column name="name" value="rounding-mode"/>
<column name="value" valueNumeric="6"/>
<column name="value" valueNumeric="${fineract.tenant.rounding-mode}"/>
<column name="date_value"/>
<column name="string_value"/>
<column name="enabled" valueBoolean="true"/>
@@ -0,0 +1,84 @@
/**
* 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.integrationtests;
import org.apache.fineract.infrastructure.configuration.domain.GlobalConfigurationProperty;
import org.apache.fineract.infrastructure.configuration.exception.GlobalConfigurationPropertyCannotBeModfied;
import org.apache.fineract.infrastructure.configuration.service.GlobalConfigurationPropertyUpdateService;
import org.apache.fineract.infrastructure.core.api.JsonCommand;
import org.junit.jupiter.api.Assertions;
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.springframework.jdbc.core.JdbcTemplate;
@ExtendWith(MockitoExtension.class)
public class TrapDoorConfigurationTest {
@Mock
private JdbcTemplate jdbcTemplate;
@InjectMocks
private GlobalConfigurationPropertyUpdateService propertyUpdateService;
private GlobalConfigurationProperty configurationProperty;
private JsonCommand jsonCommand;
@BeforeEach
public void setUp() {
configurationProperty = new GlobalConfigurationProperty();
configurationProperty.setId(1L);
configurationProperty.setName("config-name");
configurationProperty.setTrapDoor(true);
configurationProperty.setEnabled(true);
configurationProperty.setValue(3L);
JsonCommand jsonCommandMock = Mockito.mock(JsonCommand.class);
Mockito.lenient().when(jsonCommandMock.isChangeInBooleanParameterNamed(Mockito.any(), Mockito.any())).thenReturn(false);
Mockito.lenient().when(jsonCommandMock.isChangeInLongParameterNamed(Mockito.any(), Mockito.any())).thenReturn(true);
Mockito.lenient().when(jsonCommandMock.longValueOfParameterNamed(Mockito.any())).thenReturn(4L);
Mockito.lenient().when(jsonCommandMock.isChangeInDateParameterNamed(Mockito.any(), Mockito.any())).thenReturn(false);
Mockito.lenient().when(jsonCommandMock.isChangeInStringParameterNamed(Mockito.any(), Mockito.any())).thenReturn(false);
jsonCommand = jsonCommandMock;
}
@Test
public void testTrapDoorConfigurationWhenProductsExist() {
Mockito.when(jdbcTemplate.queryForObject(Mockito.anyString(), Mockito.eq(Boolean.class))).thenReturn(true);
Assertions.assertThrows(GlobalConfigurationPropertyCannotBeModfied.class,
() -> propertyUpdateService.update(configurationProperty, jsonCommand));
}
@Test
public void testTrapDoorConfigurationWhenNoProductsExist() {
Mockito.when(jdbcTemplate.queryForObject(Mockito.anyString(), Mockito.eq(Boolean.class))).thenReturn(false);
Assertions.assertDoesNotThrow(() -> propertyUpdateService.update(configurationProperty, jsonCommand));
}
}