Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/main/java/uk/gov/hmcts/ccd/domain/types/TextValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
public class TextValidator implements BaseTypeValidator {
public static final String TYPE_ID = "Text";

private static final int FIELD_VALUE_SUMMARY_MAX_LENGTH = 50;

@Override
public BaseType getType() {
return BaseType.get(TYPE_ID);
Expand All @@ -38,15 +40,17 @@ public List<ValidationResult> validate(final String dataFieldId,
final String value = dataValue.textValue();

if (!checkMax(caseFieldDefinition.getFieldTypeDefinition().getMax(), value)) {
String fieldValueSummary = getFieldValueSummary(value);
return Collections.singletonList(
new ValidationResult(value + " exceed maximum length " + caseFieldDefinition
new ValidationResult(fieldValueSummary + " exceeds the maximum length of " + caseFieldDefinition
.getFieldTypeDefinition().getMax(), dataFieldId)
);
}

if (!checkMin(caseFieldDefinition.getFieldTypeDefinition().getMin(), value)) {
String fieldValueSummary = getFieldValueSummary(value);
return Collections.singletonList(
new ValidationResult(value + " require minimum length " + caseFieldDefinition
new ValidationResult(fieldValueSummary + " requires a minimum length of " + caseFieldDefinition
.getFieldTypeDefinition().getMin(), dataFieldId)
);
}
Expand All @@ -58,6 +62,20 @@ public List<ValidationResult> validate(final String dataFieldId,
return Collections.emptyList();
}

private static String getFieldValueSummary(String value) {
if (value == null) {
return null;
}

int valueLength = value.length();

String truncatedValue = valueLength > FIELD_VALUE_SUMMARY_MAX_LENGTH
? value.substring(0, FIELD_VALUE_SUMMARY_MAX_LENGTH) + "..." : value;

return "\"%s\" (%d %s)"
.formatted(truncatedValue, valueLength, valueLength == 1 ? "character" : "characters");
}

static Boolean checkMax(final BigDecimal max, final String value) {
return max == null || value.length() <= max.intValue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.hmcts.ccd.data.definition.CaseDefinitionRepository;
import uk.gov.hmcts.ccd.domain.model.definition.CaseFieldDefinition;
import uk.gov.hmcts.ccd.test.CaseFieldDefinitionBuilder;
Expand All @@ -26,32 +27,30 @@


@DisplayName("BaseLocationValidator")
@ExtendWith(MockitoExtension.class)
class BaseLocationValidatorTest {
private static final JsonNodeFactory NODE_FACTORY = JsonNodeFactory.instance;
private static final String FIELD_ID = "TEST_FIELD_ID";

@Mock
private BaseType baseLocationBaseType;

@Mock
@Mock(lenient = true)
private CaseDefinitionRepository definitionRepository;

private BaseLocationValidator validator;
private TextValidator textValidator;
private CaseFieldDefinition caseFieldDefinition;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);

when(definitionRepository.getBaseTypes()).thenReturn(Collections.emptyList());
BaseType.setCaseDefinitionRepository(definitionRepository);
BaseType.initialise();

when(baseLocationBaseType.getType()).thenReturn(BaseLocationValidator.TYPE_ID);
BaseType.register(baseLocationBaseType);

textValidator = new TextValidator();
TextValidator textValidator = new TextValidator();
validator = new BaseLocationValidator(textValidator);

caseFieldDefinition = caseField().withMin(5)
Expand Down Expand Up @@ -80,11 +79,11 @@ void baseLocationFieldWithInvalidMinMax() {
assertAll(
() -> assertThat("Min not catched", validMinResults, hasSize(1)),
() -> assertThat("Max not catched", validMaxResults, hasSize(1)),
() -> assertThat(validMinResults, hasItem(
hasProperty("errorMessage", equalTo("Test require minimum length 5")))),
() -> assertThat(validMinResults, hasItem(hasProperty("errorMessage",
equalTo("\"Test\" (4 characters) requires a minimum length of 5")))),
() -> assertThat(validMinResults, hasItem(hasProperty("fieldId", equalTo(FIELD_ID)))),
() -> assertThat(validMaxResults, hasItem(
hasProperty("errorMessage", equalTo("Test Test Test exceed maximum length 10")))),
() -> assertThat(validMaxResults, hasItem(hasProperty("errorMessage",
equalTo("\"Test Test Test\" (14 characters) exceeds the maximum length of 10")))),
() -> assertThat(validMaxResults, hasItem(hasProperty("fieldId", equalTo(FIELD_ID))))
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.hmcts.ccd.data.definition.CaseDefinitionRepository;
import uk.gov.hmcts.ccd.domain.model.definition.CaseFieldDefinition;
import uk.gov.hmcts.ccd.test.CaseFieldDefinitionBuilder;
Expand All @@ -26,32 +27,30 @@


@DisplayName("RegionValidator")
@ExtendWith(MockitoExtension.class)
class RegionValidatorTest {
private static final JsonNodeFactory NODE_FACTORY = JsonNodeFactory.instance;
private static final String FIELD_ID = "TEST_FIELD_ID";

@Mock
private BaseType regionBaseType;

@Mock
@Mock(lenient = true)
private CaseDefinitionRepository definitionRepository;

private RegionValidator validator;
private TextValidator textValidator;
private CaseFieldDefinition caseFieldDefinition;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);

when(definitionRepository.getBaseTypes()).thenReturn(Collections.emptyList());
BaseType.setCaseDefinitionRepository(definitionRepository);
BaseType.initialise();

when(regionBaseType.getType()).thenReturn(RegionValidator.TYPE_ID);
BaseType.register(regionBaseType);

textValidator = new TextValidator();
TextValidator textValidator = new TextValidator();
validator = new RegionValidator(textValidator);

caseFieldDefinition = caseField().withMin(5)
Expand Down Expand Up @@ -80,11 +79,11 @@ void regionFieldWithInvalidMinMax() {
assertAll(
() -> assertThat("Min not catched", validMinResults, hasSize(1)),
() -> assertThat("Max not catched", validMaxResults, hasSize(1)),
() -> assertThat(validMinResults, hasItem(
hasProperty("errorMessage", equalTo("Test require minimum length 5")))),
() -> assertThat(validMinResults, hasItem(hasProperty("errorMessage",
equalTo("\"Test\" (4 characters) requires a minimum length of 5")))),
() -> assertThat(validMinResults, hasItem(hasProperty("fieldId", equalTo(FIELD_ID)))),
() -> assertThat(validMaxResults, hasItem(
hasProperty("errorMessage", equalTo("Test Test Test exceed maximum length 10")))),
() -> assertThat(validMaxResults, hasItem(hasProperty("errorMessage",
equalTo("\"Test Test Test\" (14 characters) exceeds the maximum length of 10")))),
() -> assertThat(validMaxResults, hasItem(hasProperty("fieldId", equalTo(FIELD_ID))))
);
}
Expand Down
31 changes: 23 additions & 8 deletions src/test/java/uk/gov/hmcts/ccd/domain/types/TextValidatorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.jupiter.MockitoExtension;
import uk.gov.hmcts.ccd.data.definition.CaseDefinitionRepository;
import uk.gov.hmcts.ccd.domain.model.definition.CaseFieldDefinition;
import uk.gov.hmcts.ccd.test.CaseFieldDefinitionBuilder;
Expand All @@ -25,23 +26,22 @@
import static org.mockito.Mockito.when;

@DisplayName("TextValidator")
@ExtendWith(MockitoExtension.class)
class TextValidatorTest {
private static final JsonNodeFactory NODE_FACTORY = JsonNodeFactory.instance;
private static final String FIELD_ID = "TEST_FIELD_ID";

@Mock
private BaseType textBaseType;

@Mock
@Mock(lenient = true)
private CaseDefinitionRepository definitionRepository;

private TextValidator validator;
private CaseFieldDefinition caseFieldDefinition;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);

when(definitionRepository.getBaseTypes()).thenReturn(Collections.emptyList());
BaseType.setCaseDefinitionRepository(definitionRepository);
BaseType.initialise();
Expand Down Expand Up @@ -75,17 +75,32 @@ void textFieldWithInvalidMinMax() {
final List<ValidationResult> validMaxResults = validator.validate(FIELD_ID, invalidMax, caseFieldDefinition);

assertAll(
() -> assertThat("Min not catched", validMinResults, hasSize(1)),
() -> assertThat("Max not catched", validMaxResults, hasSize(1)),
() -> assertThat("Min not caught", validMinResults, hasSize(1)),
() -> assertThat("Max not caught", validMaxResults, hasSize(1)),
() -> assertThat(validMinResults, hasItem(
hasProperty("errorMessage", equalTo("Test require minimum length 5")))),
hasProperty("errorMessage",
equalTo("\"Test\" (4 characters) requires a minimum length of 5")))),
() -> assertThat(validMinResults, hasItem(hasProperty("fieldId", equalTo(FIELD_ID)))),
() -> assertThat(validMaxResults, hasItem(
hasProperty("errorMessage", equalTo("Test Test Test exceed maximum length 10")))),
hasProperty("errorMessage",
equalTo("\"Test Test Test\" (14 characters) exceeds the maximum length of 10")))),
() -> assertThat(validMaxResults, hasItem(hasProperty("fieldId", equalTo(FIELD_ID))))
);
}

@Test
@DisplayName("should truncate field value in error when very long")
void textFieldValueTruncatedForInvalidMax() {
final JsonNode invalidMax = NODE_FACTORY.textNode("Test ".repeat(20));
final List<ValidationResult> validationList = validator.validate(FIELD_ID, invalidMax, caseFieldDefinition);

assertThat("Max not caught", validationList, hasSize(1));
assertThat(validationList, hasItem(hasProperty("errorMessage",
equalTo("\"Test Test Test Test Test Test Test Test Test Test ...\""
+ " (100 characters) exceeds the maximum length of 10"))));
assertThat(validationList, hasItem(hasProperty("fieldId", equalTo(FIELD_ID))));
}

@Test
@DisplayName("should be valid when no min and max defined")
void textFieldWithNoMinMax() {
Expand Down