Skip to content

Commit

Permalink
Merge pull request #112 from companieshouse/feature/bi-13959_round_pu…
Browse files Browse the repository at this point in the history
…blished_at_to_seconds

BI-13959: Change published at precision to seconds
  • Loading branch information
sthompsonCH authored Sep 9, 2024
2 parents 25d6c3b + 0a7ef3b commit 9760416
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package uk.gov.companieshouse.insolvency.data.api;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.OffsetDateTime;
import java.time.Instant;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand All @@ -15,37 +14,38 @@
import uk.gov.companieshouse.insolvency.data.exceptions.MethodNotAllowedException;
import uk.gov.companieshouse.insolvency.data.exceptions.ServiceUnavailableException;
import uk.gov.companieshouse.insolvency.data.model.InsolvencyDocument;
import uk.gov.companieshouse.insolvency.data.util.DateTimeFormatter;
import uk.gov.companieshouse.logging.Logger;

@Service
public class InsolvencyApiService {

private static final String CHANGED_RESOURCE_URI = "/private/resource-changed";

private final Logger logger;
private final String chsKafkaUrl;
private final ApiClientService apiClientService;
private final ObjectMapper objectMapper;

/**
* Invoke Insolvency API.
*/
public InsolvencyApiService(@Value("${chs.kafka.api.endpoint}") String chsKafkaUrl,
ApiClientService apiClientService, ObjectMapper objectMapper,
Logger logger) {
ApiClientService apiClientService,
Logger logger) {
this.chsKafkaUrl = chsKafkaUrl;
this.apiClientService = apiClientService;
this.objectMapper = objectMapper;
this.logger = logger;
}

/**
* Call chs-kafka api.
*
* @param insolvencyDocument company insolvency document
* @return response returned from chs-kafka api
*/
public ApiResponse<Void> invokeChsKafkaApi(String contextId,
InsolvencyDocument insolvencyDocument,
EventType eventType) {
InsolvencyDocument insolvencyDocument,
EventType eventType) {
InternalApiClient internalApiClient = apiClientService.getInternalApiClient();
internalApiClient.setBasePath(chsKafkaUrl);
PrivateChangedResourcePost changedResourcePost =
Expand All @@ -71,13 +71,13 @@ CHANGED_RESOURCE_URI, mapChangedResource(
}

private ChangedResource mapChangedResource(String contextId,
InsolvencyDocument insolvencyDocument,
EventType eventType) {
InsolvencyDocument insolvencyDocument,
EventType eventType) {
String resourceUri = "/company/" + insolvencyDocument.getId() + "/insolvency";

ChangedResourceEvent event = new ChangedResourceEvent();
event.setType(eventType.getEvent());
event.publishedAt(String.valueOf(OffsetDateTime.now()));
event.publishedAt(DateTimeFormatter.formatPublishedAt(Instant.now()));

ChangedResource changedResource = new ChangedResource();
changedResource.setResourceUri(resourceUri);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package uk.gov.companieshouse.insolvency.data.util;

import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -15,6 +17,9 @@ public class DateTimeFormatter {
static java.time.format.DateTimeFormatter readDateTimeFormatter =
java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd");

static java.time.format.DateTimeFormatter publishedAtDateTimeFormatter =
java.time.format.DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss");

private DateTimeFormatter() {

}
Expand All @@ -39,4 +44,13 @@ public static String format(LocalDate localDate) {
return localDate.atStartOfDay().format(writeDateTimeFormatter);
}

/**
* Format publishedAt date
* @param now current time as Instant
* @return UTC time as string rounded to seconds
*/
public static String formatPublishedAt(Instant now) {
return publishedAtDateTimeFormatter.format(now.atZone(ZoneOffset.UTC));
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package uk.gov.companieshouse.insolvency.data.util;

import java.time.Instant;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DateTimeFormatterTest {
Expand All @@ -27,4 +29,17 @@ void shouldFormatGivenDateString() {
assertThat(formattedDate).isNotNull();
assertThat(formattedDate).isEqualTo("2015-06-26T00:00:00Z");
}

@Test
void shouldFormatPublishedAtDate() {
// given
Instant now = Instant.parse("2024-09-04T10:52:22.235486Z");
final String expected = "2024-09-04T10:52:22";

// when
final String actual = DateTimeFormatter.formatPublishedAt(now);

// then
assertEquals(expected, actual);
}
}

0 comments on commit 9760416

Please sign in to comment.