Skip to content

Commit

Permalink
Closes #31, closes #32; Implemented "Parameter" and "Parameter Value"…
Browse files Browse the repository at this point in the history
… validators.
  • Loading branch information
AngelaMariaDespotopoulou committed Nov 9, 2022
1 parent f614e3a commit 3e0d4bf
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package eu.datacrop.maize.model_repository.commons.dtos.requests.auxiliaries;

import eu.datacrop.maize.model_repository.commons.dtos.requests.templates.RequestDto;
import eu.datacrop.maize.model_repository.commons.enums.ResponseCode;
import eu.datacrop.maize.model_repository.commons.error.messages.ParameterErrorMessages;
import eu.datacrop.maize.model_repository.commons.wrappers.ResponseWrapper;
import eu.datacrop.maize.model_repository.commons.wrappers.single.auxiliaries.ParameterResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.io.Serial;
Expand All @@ -14,6 +18,7 @@
* @author Angela-Maria Despotopoulou [Athens, Greece]
* @since version 0.4.0
*********************************************************************************************************************/
@Slf4j
public class ParameterRequestDto extends RequestDto implements Serializable {

@Serial
Expand Down Expand Up @@ -193,6 +198,33 @@ public JSONObject toJSON() {
*****************************************************************************************************************/
@Override
public ResponseWrapper performValidation() {
return null;
ParameterResponseWrapper wrapper;
try {
// Validating attributes.
wrapper = (ParameterResponseWrapper) super.getValidator().validateAttributes(this);

// If we already have an error there is no point in checking further.
if (wrapper == null || !wrapper.getCode().equals(ResponseCode.SUCCESS)) {
log.debug("Issues discovered during attribute validation.");
return wrapper;
}

// Validating relationships (if applicable).
wrapper = (ParameterResponseWrapper) super.getValidator().validateRelationships(this);

// If an error has been discovered report it and return.
if (wrapper == null || !wrapper.getCode().equals(ResponseCode.SUCCESS)) {
log.debug("Issues discovered during attribute validation.");
return wrapper;
}

// Reporting that the validation discovered no issues.
log.debug("Validation of the Request DTO has no issues to report.");
return wrapper;
} catch (IllegalArgumentException e) {
String message = "Error occurred during Request DTO validation.";
log.error(message);
return new ParameterResponseWrapper(ResponseCode.ERROR, message, null, ParameterErrorMessages.INTERNAL_SERVER_ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package eu.datacrop.maize.model_repository.commons.dtos.requests.auxiliaries;

import eu.datacrop.maize.model_repository.commons.dtos.requests.templates.RequestDto;
import eu.datacrop.maize.model_repository.commons.enums.ResponseCode;
import eu.datacrop.maize.model_repository.commons.error.messages.ParameterValueErrorMessages;
import eu.datacrop.maize.model_repository.commons.wrappers.ResponseWrapper;
import eu.datacrop.maize.model_repository.commons.wrappers.single.auxiliaries.ParameterValueResponseWrapper;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

Expand Down Expand Up @@ -142,6 +145,33 @@ public JSONObject toJSON() {
*****************************************************************************************************************/
@Override
public ResponseWrapper performValidation() {
return null; //TODO
ParameterValueResponseWrapper wrapper;
try {
// Validating attributes.
wrapper = (ParameterValueResponseWrapper) super.getValidator().validateAttributes(this);

// If we already have an error there is no point in checking further.
if (wrapper == null || !wrapper.getCode().equals(ResponseCode.SUCCESS)) {
log.debug("Issues discovered during attribute validation.");
return wrapper;
}

// Validating relationships (if applicable).
wrapper = (ParameterValueResponseWrapper) super.getValidator().validateRelationships(this);

// If an error has been discovered report it and return.
if (wrapper == null || !wrapper.getCode().equals(ResponseCode.SUCCESS)) {
log.debug("Issues discovered during attribute validation.");
return wrapper;
}

// Reporting that the validation discovered no issues.
log.debug("Validation of the Request DTO has no issues to report.");
return wrapper;
} catch (IllegalArgumentException e) {
String message = "Error occurred during Request DTO validation.";
log.error(message);
return new ParameterValueResponseWrapper(ResponseCode.ERROR, message, null, ParameterValueErrorMessages.INTERNAL_SERVER_ERROR);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package eu.datacrop.maize.model_repository.persistence.validators;

import eu.datacrop.maize.model_repository.commons.dtos.requests.auxiliaries.ParameterRequestDto;
import eu.datacrop.maize.model_repository.commons.dtos.requests.templates.RequestDto;
import eu.datacrop.maize.model_repository.commons.enums.ResponseCode;
import eu.datacrop.maize.model_repository.commons.error.messages.ParameterErrorMessages;
import eu.datacrop.maize.model_repository.commons.validators.Validator;
import eu.datacrop.maize.model_repository.commons.wrappers.single.auxiliaries.ParameterResponseWrapper;

import java.util.Vector;

/**********************************************************************************************************************
* This class implements methods to validate whether an incoming HTTP Request body contains attributes that are
* valid according to the business rules for a Parameter entity.
*
* @author Angela-Maria Despotopoulou [Athens, Greece]
* @since version 0.4.0
*********************************************************************************************************************/
public class ParameterValidator implements Validator {

/******************************************************************************************************************
* This method triggers validation of the data transfer object's attributes.
*
* @param requestDto A data transfer object to validate, not null.
* @return A ResponseWrapper (the user will receive a more elaborate one, here it is used only for
* internal intra-module communication).
*
* throws IllegalArgumentException, if requestDto is null.
*****************************************************************************************************************/
@Override
public ParameterResponseWrapper validateAttributes(RequestDto requestDto) throws IllegalArgumentException {

// Checking input parameters.
if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method ParameterValidator.validate().");
}

ParameterRequestDto parameterRequestDto = (ParameterRequestDto) requestDto;

String message;
try {
// Checking the request for mandatory fields that were erroneously provided without content.
Vector<String> fields = getNamesOfFieldsThatAreErroneouslyNull(parameterRequestDto);
if (!fields.isEmpty()) {
message = ParameterErrorMessages.MANDATORY_FIELDS_MISSING.toString().concat(" Field(s): ").concat(fields.toString());
return new ParameterResponseWrapper(ResponseCode.BAD_REQUEST, message, null, ParameterErrorMessages.MANDATORY_FIELDS_MISSING);
}
} catch (IllegalArgumentException e) {
// Reporting internal server error.
throw new IllegalArgumentException(e.getMessage());
}

// Reporting that attribute validation found no issues.
return new ParameterResponseWrapper(ResponseCode.SUCCESS, "Validation success.", null, null);
}

/******************************************************************************************************************
* This method triggers validation of the data transfer object's external relationships.
*
* @param requestDto A data transfer object to validate, not null.
* @return A ResponseWrapper (the user will receive a more elaborate one, here it is used only for
* internal intra-module communication).
*
* throws IllegalArgumentException, if requestDto is null.
*****************************************************************************************************************/
@Override
public ParameterResponseWrapper validateRelationships(RequestDto requestDto) throws IllegalArgumentException {

if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method ParameterValidator.validate().");
}

// Always returns SUCCESS according to business logic Parameter do not refer to other entities.
return new ParameterResponseWrapper(ResponseCode.SUCCESS, "Validation success.", null, null);
}

/******************************************************************************************************************
* This method parses a Parameter Request Data Transfer Object for fields that should have not been left
* null according to business logic. Returns empty Vector if no erroneous fields have been located.
*
* @param requestDto The data transfer object to be parsed, not null.
* @return A collection of fields that have been marked as erroneous.
*
* @throws IllegalArgumentException if the method parameter is null.
*****************************************************************************************************************/
private Vector<String> getNamesOfFieldsThatAreErroneouslyNull(ParameterRequestDto requestDto) throws IllegalArgumentException {

// Checking input parameters.
if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method ParameterValidator.getNamesOfFieldsThatAreErroneouslyNull().");
}

Vector<String> fields = new Vector<String>();

// Checking mandatory fields "Name" and "Description".
if (requestDto.getName() == null || requestDto.getName().isBlank()) fields.add("name");
if (requestDto.getDescription() == null) fields.add("description");
if (requestDto.getType() == null) fields.add("type");

// Logging and returning result.
return fields;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package eu.datacrop.maize.model_repository.persistence.validators;

import eu.datacrop.maize.model_repository.commons.dtos.requests.auxiliaries.ParameterValueRequestDto;
import eu.datacrop.maize.model_repository.commons.dtos.requests.templates.RequestDto;
import eu.datacrop.maize.model_repository.commons.enums.ResponseCode;
import eu.datacrop.maize.model_repository.commons.error.messages.ParameterValueErrorMessages;
import eu.datacrop.maize.model_repository.commons.validators.Validator;
import eu.datacrop.maize.model_repository.commons.wrappers.single.auxiliaries.ParameterValueResponseWrapper;

import java.util.Vector;

/**********************************************************************************************************************
* This class implements methods to validate whether an incoming HTTP Request body contains attributes that are
* valid according to the business rules for a Parameter Value entity.
*
* @author Angela-Maria Despotopoulou [Athens, Greece]
* @since version 0.4.0
*********************************************************************************************************************/
public class ParameterValueValidator implements Validator {

/******************************************************************************************************************
* This method triggers validation of the data transfer object's attributes.
*
* @param requestDto A data transfer object to validate, not null.
* @return A ResponseWrapper (the user will receive a more elaborate one, here it is used only for
* internal intra-module communication).
*
* throws IllegalArgumentException, if requestDto is null.
*****************************************************************************************************************/
@Override
public ParameterValueResponseWrapper validateAttributes(RequestDto requestDto) throws IllegalArgumentException {

// Checking input parameters.
if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method ParameterValueValidator.validate().");
}

ParameterValueRequestDto parameterValueRequestDto = (ParameterValueRequestDto) requestDto;

String message;
try {
// Checking the request for mandatory fields that were erroneously provided without content.
Vector<String> fields = getNamesOfFieldsThatAreErroneouslyNull(parameterValueRequestDto);
if (!fields.isEmpty()) {
message = ParameterValueErrorMessages.MANDATORY_FIELDS_MISSING.toString().concat(" Field(s): ").concat(fields.toString());
return new ParameterValueResponseWrapper(ResponseCode.BAD_REQUEST, message, null, ParameterValueErrorMessages.MANDATORY_FIELDS_MISSING);
}
} catch (IllegalArgumentException e) {
// Reporting internal server error.
throw new IllegalArgumentException(e.getMessage());
}

// Reporting that attribute validation found no issues.
return new ParameterValueResponseWrapper(ResponseCode.SUCCESS, "Validation success.", null, null);
}

/******************************************************************************************************************
* This method triggers validation of the data transfer object's external relationships.
*
* @param requestDto A data transfer object to validate, not null.
* @return A ResponseWrapper (the user will receive a more elaborate one, here it is used only for
* internal intra-module communication).
*
* throws IllegalArgumentException, if requestDto is null.
*****************************************************************************************************************/
@Override
public ParameterValueResponseWrapper validateRelationships(RequestDto requestDto) throws IllegalArgumentException {

if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method ParameterValueValidator.validate().");
}

// Always returns SUCCESS according to business logic Parameter Values do not refer to other entities.
return new ParameterValueResponseWrapper(ResponseCode.SUCCESS, "Validation success.", null, null);
}

/******************************************************************************************************************
* This method parses a Parameter Value Request Data Transfer Object for fields that should have not been left
* null according to business logic. Returns empty Vector if no erroneous fields have been located.
*
* @param requestDto The data transfer object to be parsed, not null.
* @return A collection of fields that have been marked as erroneous.
*
* @throws IllegalArgumentException if the method parameter is null.
*****************************************************************************************************************/
private Vector<String> getNamesOfFieldsThatAreErroneouslyNull(ParameterValueRequestDto requestDto) throws IllegalArgumentException {

// Checking input parameters.
if (requestDto == null) {
throw new IllegalArgumentException("Invalid parameter detected for method ParameterValueValidator.getNamesOfFieldsThatAreErroneouslyNull().");
}

Vector<String> fields = new Vector<String>();

// Checking mandatory fields "Name" and "Description".
if (requestDto.getName() == null || requestDto.getName().isBlank()) fields.add("name");
if (requestDto.getValue() == null) fields.add("value");

// Logging and returning result.
return fields;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
@Service
public class SystemValidator implements Validator {

/******************************************************************************************************************
* This method triggers validation of the data transfer object's attributes.
*
* @param requestDto A data transfer object to validate, not null.
* @return An abstract ResponseWrapper (the user will receive a more elaborate one, here it is used only for
* internal intra-module communication).
*
* throws IllegalArgumentException, if requestDto is null.
*****************************************************************************************************************/
@Override
public SystemResponseWrapper validateAttributes(RequestDto requestDto) throws IllegalArgumentException {
// Checking input parameters.
Expand Down Expand Up @@ -74,7 +83,7 @@ public SystemResponseWrapper validateRelationships(RequestDto requestDto) throws
throw new IllegalArgumentException("Invalid parameter detected for method SystemValidator.validate().");
}

// Always returns SUCCESS according to business logic Locations do not refer to other entities.
// Always returns SUCCESS according to business logic Systems do not refer to other entities.
return new SystemResponseWrapper(ResponseCode.SUCCESS, "Validation success.", null, null);
}

Expand Down

0 comments on commit 3e0d4bf

Please sign in to comment.