Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package uk.gov.companieshouse.api.filinghistory.utils;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class TransactionKindResultTest {

@Test
void constructorShouldSetFields() {
TransactionKind kind = TransactionKind.TOP_LEVEL;

TransactionKindResult result = new TransactionKindResult("encoded-id", kind);

assertEquals("encoded-id", result.getEncodedId());
assertEquals(kind, result.getKind());
}

@Test
void settersShouldUpdateFields() {
TransactionKindResult result = new TransactionKindResult(null, null);

result.setEncodedId("new-id");
result.setKind(TransactionKind.TOP_LEVEL);

assertEquals("new-id", result.getEncodedId());
assertEquals(TransactionKind.TOP_LEVEL, result.getKind());
}

@Test
void equalsShouldReturnTrueForSameValues() {
TransactionKindResult first = new TransactionKindResult("id1", TransactionKind.TOP_LEVEL);

TransactionKindResult second = new TransactionKindResult("id1", TransactionKind.TOP_LEVEL);

assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode());
}

@Test
void equalsShouldReturnFalseForDifferentEncodedId() {
TransactionKindResult first = new TransactionKindResult("id1", TransactionKind.TOP_LEVEL);

TransactionKindResult second = new TransactionKindResult("id2", TransactionKind.TOP_LEVEL);

assertNotEquals(first, second);
assertNotEquals(first.hashCode(), second.hashCode());
}

@Test
void equalsShouldReturnFalseForDifferentKind() {
TransactionKindResult first = new TransactionKindResult("id1", TransactionKind.TOP_LEVEL);

TransactionKindResult second = new TransactionKindResult("id1", TransactionKind.ASSOCIATED_FILING);

assertNotEquals(first, second);
assertNotEquals(first.hashCode(), second.hashCode());
}

@Test
void equalsShouldReturnFalseForDifferentObjectType() {
TransactionKindResult result = new TransactionKindResult("id1", TransactionKind.ANNOTATION);

assertNotEquals("not-a-TransactionKindResult", result);
}

@Test
void equalsShouldHandleNullFields() {
TransactionKindResult first = new TransactionKindResult(null, null);

TransactionKindResult second = new TransactionKindResult(null, null);

assertEquals(first, second);
}

@Test
void hashCodeShouldHandleNullValues() {
TransactionKindResult result = new TransactionKindResult(null, null);

int hashcode = result.hashCode();
assertNotEquals(0, hashcode);
}

@Test
void hashCodeShouldHandleNonNullValues() {
TransactionKindResult result = new TransactionKindResult("id1", TransactionKind.RESOLUTION);

int hashcode = result.hashCode();
assertNotEquals(0, hashcode);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package uk.gov.companieshouse.api.serialization;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.time.LocalDate;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -15,6 +17,8 @@

class LocalDateDeserializerTest {

private static final String DESERIALISATION_FALIED_EXCEPTION_MESSAGE = "Deserialization failed.";
private static final String DATE_FIELD_MISSING_OR_NULL = "$date field is missing or null";
private LocalDateDeserializer deserializer;

private ObjectMapper mapper;
Expand All @@ -31,15 +35,15 @@ void dateShouldDeserialize() {
String jsonTestString = "{\"date\":{\"$date\": \"2023-01-09T00:00:00Z\"}}";

LocalDate returnedDate = deserialize(jsonTestString);
Assertions.assertEquals(LocalDate.of(2023, 1, 9), returnedDate);
assertEquals(LocalDate.of(2023, 1, 9), returnedDate);
}

@Test
void longStringReturnsLong() {
String jsonTestString = "{\"date\":{\"$date\": {\"$numberLong\":\"-1431388800000\"}}}";

LocalDate returnedDate = deserialize(jsonTestString);
Assertions.assertEquals(LocalDate.of(1924, 8, 23), returnedDate);
assertEquals(LocalDate.of(1924, 8, 23), returnedDate);
}

@Test
Expand All @@ -52,19 +56,39 @@ void nullStringReturnsError() {
void invalidStringReturnsError() {
String jsonTestString = "{\"date\":{\"$date\": \"NotADate\"}}}";

assertThrows(java.time.format.DateTimeParseException.class, () -> deserialize(jsonTestString));
assertException(DESERIALISATION_FALIED_EXCEPTION_MESSAGE, jsonTestString);
}

@Test
void missingDateFieldReturnsError() {
String jsonTestString = "{\"date\":{}}";
assertThrows(BadRequestException.class, () -> deserialize(jsonTestString));

assertException(DATE_FIELD_MISSING_OR_NULL, jsonTestString);
}

@Test
void nullDateFieldReturnsError() {
String jsonTestString = "{\"date\":{\"$date\":null}}";
assertThrows(BadRequestException.class, () -> deserialize(jsonTestString));

assertException(DATE_FIELD_MISSING_OR_NULL, jsonTestString);
}

@Test
void invalidDateShouldReturnError() {

JsonParser parser = mock(JsonParser.class);
when(parser.readValueAsTree())
.thenThrow(new RuntimeException("Invalid JSON"));
BadRequestException exception = assertThrows(BadRequestException.class, () -> deserializer.deserialize(parser, null));

assertEquals(DESERIALISATION_FALIED_EXCEPTION_MESSAGE, exception.getMessage());
}

@Test
void invalidNumberLongReturnError() {
String jsonTestString = "{\"date\":{\"$date\": {\"$numberLong\":\"not-a-number\"}}}";

assertException(DESERIALISATION_FALIED_EXCEPTION_MESSAGE, jsonTestString);
}

private LocalDate deserialize(String jsonString) {
Expand All @@ -76,13 +100,14 @@ private LocalDate deserialize(String jsonString) {
// Pass null for DeserializationContext as it's not used in the deserializer
return deserializer.deserialize(parser, null);
} catch (Exception e) {
// Unwrap if it's a RuntimeException wrapping another exception
if (e instanceof RuntimeException && e.getCause() != null) {
throw (RuntimeException) e.getCause();
}
// Otherwise, rethrow as is
throw e;
}
}

private void assertException(String expectedMessage, String jsonTestString) {
BadRequestException exception = assertThrows(BadRequestException.class, () -> deserialize(jsonTestString));

assertEquals(expectedMessage, exception.getMessage());
}

}