Skip to content

Commit

Permalink
CCMSPUI-529 Added BooleanStringConverterTest
Browse files Browse the repository at this point in the history
Signed-off-by: Jamie Briggs <jamie.briggs@digital.justice.gov.uk>
  • Loading branch information
Jamie Briggs committed Feb 3, 2025
1 parent 6a328c9 commit c4ef4fb
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,40 @@
import jakarta.persistence.Converter;
import java.util.Optional;

/**
* Attribute converter class which handles the conversion of to and from a boolean value, and "true"
* ot "false".
*
* @author Jamie Briggs
*/
@Converter
public class BooleanStringConverter implements AttributeConverter<Boolean, String> {

/**
* Converts a Boolean value to its database column representation as a String.
* For non-null values, the method returns "true" or "false" based on the Boolean value.
* Null values are handled gracefully and returned as null.
*
* @param value the Boolean value to be converted; can be null
* @return the String representation of the Boolean value ("true", "false"),
* or null if the input is null
*/
@Override
public String convertToDatabaseColumn(Boolean aBoolean) {
return Optional.ofNullable(aBoolean)
.map(x -> x ? "true" : "false")
public String convertToDatabaseColumn(Boolean value) {
return Optional.ofNullable(value)
.map(x -> Boolean.TRUE.equals(x) ? "true" : "false")
.orElse(null);
}

/**
* Converts a database column value (String) to its corresponding entity attribute (Boolean).
*
* @param value the String value retrieved from the database column; typically "true" or "false".
* If null or not a valid Boolean string, it will evaluate to false by default.
* @return the Boolean equivalent of the input String.
*/
@Override
public Boolean convertToEntityAttribute(String value) {
return Optional.ofNullable(value)
.map(d -> d.equalsIgnoreCase("true"))
.orElse(null);
return Boolean.parseBoolean(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ public final class NotificationSearchRepository {
* @param feeEarnerId the ID of the fee earner to filter by
* @param includeClosed whether to include closed notifications in the results
* @param notificationType the type of notification to filter results by
* @param assignedDateFrom the start date for filtering notifications by assignment date
* @param assignedDateTo the end date for filtering notifications by assignment date
* @param assignedDateFrom the start date for filtering notifications by
* assignment date
* @param assignedDateTo the end date for filtering notifications by
* assignment date
* @param pageable the pagination and sorting information
* @return a paginated list of NotificationInfo entities matching the specified filters
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package uk.gov.laa.ccms.data.entity;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class BooleanStringConverterTest {

@InjectMocks
BooleanStringConverter booleanStringConverter;

@Nested
@DisplayName("Test ConvertToDb")
class ConvertToDbTest {

@Test
@DisplayName("Should return true")
void shouldReturnTrue(){
assertTrue(booleanStringConverter.convertToEntityAttribute("True"));
}

@Test
@DisplayName("Should return true lowercase")
void shouldReturnTrueLowercase(){
assertTrue(booleanStringConverter.convertToEntityAttribute("true"));
}

@Test
@DisplayName("Should return true uppercase")
void shouldReturnTrueUppercase(){
assertTrue(booleanStringConverter.convertToEntityAttribute("TRUE"));
}

@Test
@DisplayName("Should return false")
void shouldReturnFalse(){
assertFalse(booleanStringConverter.convertToEntityAttribute("False"));
}

@Test
@DisplayName("Should return false lowercase")
void shouldReturnFalseLowercase(){
assertFalse(booleanStringConverter.convertToEntityAttribute("false"));
}

@Test
@DisplayName("Should return false uppercase")
void shouldReturnFalseUppercase(){
assertFalse(booleanStringConverter.convertToEntityAttribute("FALSE"));
}

}

@Nested
@DisplayName("Test ConvertToDatabaseColumn")
class ConvertToDatabaseColumnTest {

@Test
@DisplayName("Should return true")
void shouldReturnTrue(){
assertEquals("true", booleanStringConverter.convertToDatabaseColumn(true));
}

@Test
@DisplayName("Should return false")
void shouldReturnFalse(){
assertEquals("false", booleanStringConverter.convertToDatabaseColumn(false));
}
}
}

0 comments on commit c4ef4fb

Please sign in to comment.