Skip to content

Commit

Permalink
Merge pull request #47 from companieshouse/feature/bi-13964_update_pu…
Browse files Browse the repository at this point in the history
…blish_at_timestamp

BI-13964: Update published_at timestamp
  • Loading branch information
kinpang-CH authored Sep 6, 2024
2 parents 1618a76 + ab8ba73 commit aa6f0bd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import java.time.Instant;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.function.Supplier;
import org.springframework.context.annotation.Bean;
Expand All @@ -30,8 +30,8 @@ public Logger logger() {
}

@Bean
public Supplier<String> offsetDateTimeGenerator() {
return () -> String.valueOf(OffsetDateTime.now());
public Supplier<Instant> instantSupplier() {
return Instant::now;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/uk/gov/companieshouse/exemptions/util/DateUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package uk.gov.companieshouse.exemptions.util;

import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

public class DateUtils {
public static String publishedAtString(final Instant source) {
return source.atOffset(ZoneOffset.UTC)
.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'hh:mm:ss"));

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package uk.gov.companieshouse.exemptions.util;

import java.time.Instant;
import java.util.function.Supplier;
import org.springframework.stereotype.Component;
import uk.gov.companieshouse.api.chskafka.ChangedResource;
Expand All @@ -9,14 +10,14 @@
@Component
public class ResourceChangedRequestMapper {

private final Supplier<String> timestampGenerator;
private final Supplier<Instant> instantSupplier;

public ResourceChangedRequestMapper(Supplier<String> timestampGenerator) {
this.timestampGenerator = timestampGenerator;
public ResourceChangedRequestMapper(Supplier<Instant> instantSupplier) {
this.instantSupplier = instantSupplier;
}

public ChangedResource mapChangedResource(ResourceChangedRequest request) {
ChangedResourceEvent event = new ChangedResourceEvent().publishedAt(this.timestampGenerator.get());
ChangedResourceEvent event = new ChangedResourceEvent().publishedAt(DateUtils.publishedAtString(this.instantSupplier.get()));
ChangedResource changedResource = new ChangedResource()
.resourceUri(String.format("company/%s/exemptions", request.companyNumber()))
.resourceKind("company-exemptions")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.function.Supplier;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -21,10 +23,11 @@
class ResourceChangedRequestMapperTest {

private static final String EXPECTED_CONTEXT_ID = "35234234";
private static final String DATE = "date";
private static final Instant UPDATED_AT = Instant.now().truncatedTo(ChronoUnit.MILLIS);
private static final String PUBLISHED_AT = DateUtils.publishedAtString(UPDATED_AT);

@Mock
private Supplier<String> timestampGenerator;
private Supplier<Instant> instantSupplier;

@InjectMocks
private ResourceChangedRequestMapper mapper;
Expand All @@ -33,7 +36,7 @@ class ResourceChangedRequestMapperTest {
@MethodSource("resourceChangedScenarios")
void testMapper(ResourceChangedTestArgument argument) {
// given
when(timestampGenerator.get()).thenReturn(DATE);
when(instantSupplier.get()).thenReturn(UPDATED_AT);

// when
ChangedResource actual = mapper.mapChangedResource(argument.request());
Expand All @@ -50,15 +53,15 @@ static Stream<ResourceChangedTestArgument> resourceChangedScenarios() {
.withResourceUri("company/12345678/exemptions")
.withResourceKind("company-exemptions")
.withEventType("changed")
.withEventPublishedAt(DATE)
.withEventPublishedAt(PUBLISHED_AT)
.build(),
ResourceChangedTestArgument.builder()
.withRequest(new ResourceChangedRequest(EXPECTED_CONTEXT_ID, "12345678", null, null))
.withContextId(EXPECTED_CONTEXT_ID)
.withResourceUri("company/12345678/exemptions")
.withResourceKind("company-exemptions")
.withEventType("changed")
.withEventPublishedAt(DATE)
.withEventPublishedAt(PUBLISHED_AT)
.build(),
ResourceChangedTestArgument.builder()
.withRequest(new ResourceChangedRequest(EXPECTED_CONTEXT_ID, "12345678", new CompanyExemptions(), true))
Expand All @@ -67,7 +70,7 @@ static Stream<ResourceChangedTestArgument> resourceChangedScenarios() {
.withResourceKind("company-exemptions")
.withEventType("deleted")
.withDeletedData(new CompanyExemptions())
.withEventPublishedAt(DATE)
.withEventPublishedAt(PUBLISHED_AT)
.build()
);
}
Expand Down

0 comments on commit aa6f0bd

Please sign in to comment.