-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate tenant creation request data
Fix tenant already exists exception not being thrown
- Loading branch information
1 parent
fe1770e
commit f527679
Showing
7 changed files
with
110 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/main/java/org/fineract/messagegateway/tenants/constants/TenantConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.fineract.messagegateway.tenants.constants; | ||
|
||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
import org.fineract.messagegateway.constants.MessageGatewayConstants; | ||
|
||
public interface TenantConstants extends MessageGatewayConstants { | ||
|
||
String TENANTS_RESOURCE_NAME = "tenants"; | ||
|
||
String TENANT_ID = "tenantId"; | ||
String TENANT_DESCRIPTION = "description"; | ||
|
||
|
||
// list of allowed parameters for tenant creation request | ||
Set<String> CREATE_REQUEST_PARAMETERS = new HashSet<>(Arrays.asList(TENANT_ID, TENANT_DESCRIPTION)); | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/main/java/org/fineract/messagegateway/tenants/serialization/TenantSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package org.fineract.messagegateway.tenants.serialization; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.fineract.messagegateway.exception.PlatformApiDataValidationException; | ||
import org.fineract.messagegateway.helpers.ApiParameterError; | ||
import org.fineract.messagegateway.helpers.DataValidatorBuilder; | ||
import org.fineract.messagegateway.helpers.FromJsonHelper; | ||
import org.fineract.messagegateway.tenants.constants.TenantConstants; | ||
import org.fineract.messagegateway.tenants.domain.Tenant; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.google.gson.JsonElement; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
@Component | ||
public class TenantSerializer { | ||
|
||
private final FromJsonHelper fromJsonHelper; | ||
|
||
@Autowired | ||
public TenantSerializer(FromJsonHelper fromJsonHelper) { | ||
this.fromJsonHelper = fromJsonHelper; | ||
} | ||
|
||
public Tenant validateCreateRequest(final String json) { | ||
final Type typeOfMap = new TypeToken<Map<String, Object>>() {}.getType(); | ||
this.fromJsonHelper.checkForUnsupportedParameters(typeOfMap, json, | ||
TenantConstants.CREATE_REQUEST_PARAMETERS); | ||
|
||
final List<ApiParameterError> dataValidationErrors = new ArrayList<>(); | ||
final DataValidatorBuilder baseDataValidator = new DataValidatorBuilder(dataValidationErrors) | ||
.resource(TenantConstants.TENANTS_RESOURCE_NAME); | ||
final JsonElement element = this.fromJsonHelper.parse(json); | ||
|
||
|
||
final String tenantId = this.fromJsonHelper.extractStringNamed(TenantConstants.TENANT_ID, element); | ||
baseDataValidator.reset().parameter(TenantConstants.TENANT_ID) | ||
.value(tenantId).notBlank().notExceedingLengthOf(32); | ||
|
||
String tenantDescription = null; | ||
if(this.fromJsonHelper.parameterExists(TenantConstants.TENANT_DESCRIPTION, element)) { | ||
tenantDescription = this.fromJsonHelper.extractStringNamed(TenantConstants.TENANT_DESCRIPTION, | ||
element); | ||
baseDataValidator.reset().parameter(TenantConstants.TENANT_DESCRIPTION).value(tenantDescription) | ||
.notBlank().notExceedingLengthOf(500); | ||
} | ||
|
||
if (!dataValidationErrors.isEmpty()) { | ||
throw new PlatformApiDataValidationException("validation.msg.validation.errors.exist", | ||
"Validation errors exist.", dataValidationErrors); | ||
} | ||
|
||
return new Tenant(tenantId, null, tenantDescription); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters