Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DMP-4813 Change update metadata from OffsetDateTime to String so date is in expected format #2642

Merged
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
Expand Up @@ -15,6 +15,7 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.UUID;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
Expand All @@ -34,7 +35,6 @@
@TestPropertySource(properties = {
"darts.storage.arm-api.url=http://localhost:${wiremock.server.port}"
})
@SuppressWarnings("PMD.CloseResource")
class ArmApiClientIntTest extends IntegrationBaseWithWiremock {

private static final String EXTERNAL_RECORD_ID = "7683ee65-c7a7-7343-be80-018b8ac13602";
Expand All @@ -47,7 +47,7 @@ class ArmApiClientIntTest extends IntegrationBaseWithWiremock {
private ArmApiClient armApiClient;

@Test
void updateMetadataShouldSucceedIfServerReturns200Success() {
void updateMetadata_ShouldSucceed_WhenServerReturns200Success() {
// Given
var bearerAuth = "Bearer some-token";
var externalRecordId = "7683ee65-c7a7-7343-be80-018b8ac13602";
Expand Down Expand Up @@ -76,7 +76,7 @@ void updateMetadataShouldSucceedIfServerReturns200Success() {
var updateMetadataRequest = UpdateMetadataRequest.builder()
.itemId(externalRecordId)
.manifest(UpdateMetadataRequest.Manifest.builder()
.eventDate(eventTimestamp)
.eventDate(formatDateTime(eventTimestamp))
.build())
.useGuidsForFields(false)
.build();
Expand All @@ -86,20 +86,71 @@ void updateMetadataShouldSucceedIfServerReturns200Success() {

// Then
verify(postRequestedFor(urlEqualTo(UPDATE_METADATA_PATH))
.withHeader(AUTHORIZATION, equalTo(bearerAuth))
.withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON_VALUE))
.withRequestBody(
matchingJsonPath("$.UseGuidsForFields", equalTo("false"))
.and(matchingJsonPath("$.manifest.event_date", equalTo(eventTimestamp.toString())))
.and(matchingJsonPath("$.itemId", equalTo(externalRecordId)))
));
.withHeader(AUTHORIZATION, equalTo(bearerAuth))
.withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON_VALUE))
.withRequestBody(
matchingJsonPath("$.UseGuidsForFields", equalTo("false"))
.and(matchingJsonPath("$.manifest.event_date", equalTo(formatDateTime(eventTimestamp))))
.and(matchingJsonPath("$.itemId", equalTo(externalRecordId)))
));

assertEquals(UUID.fromString(externalRecordId), updateMetadataResponse.getItemId());
}

@Test
void updateMetadata_ShouldSucceed_WithZeroTimes() {
// Given
var bearerAuth = "Bearer some-token";
var externalRecordId = "7683ee65-c7a7-7343-be80-018b8ac13602";
var eventTimestamp = OffsetDateTime.parse("2024-01-31T00:00:00.00000Z").plusYears(7);

stubFor(
WireMock.post(urlEqualTo(UPDATE_METADATA_PATH))
.willReturn(
aResponse()
.withHeader("Content-type", "application/json")
.withBody("""
{
"itemId": "7683ee65-c7a7-7343-be80-018b8ac13602",
"cabinetId": 101,
"objectId": "4bfe4fc7-4e2f-4086-8a0e-146cc4556260",
"objectType": 1,
"fileName": "UpdateMetadata-20241801-122819.json",
"isError": false,
"responseStatus": 0,
"responseStatusMessages": null
}
"""
)
.withStatus(200)));

var updateMetadataRequest = UpdateMetadataRequest.builder()
.itemId(externalRecordId)
.manifest(UpdateMetadataRequest.Manifest.builder()
.eventDate(formatDateTime(eventTimestamp))
.build())
.useGuidsForFields(false)
.build();

// When
UpdateMetadataResponse updateMetadataResponse = armApiClient.updateMetadata(bearerAuth, updateMetadataRequest);

// Then
verify(postRequestedFor(urlEqualTo(UPDATE_METADATA_PATH))
.withHeader(AUTHORIZATION, equalTo(bearerAuth))
.withHeader(CONTENT_TYPE, equalTo(APPLICATION_JSON_VALUE))
.withRequestBody(
matchingJsonPath("$.UseGuidsForFields", equalTo("false"))
.and(matchingJsonPath("$.manifest.event_date", equalTo(formatDateTime(eventTimestamp))))
.and(matchingJsonPath("$.itemId", equalTo(externalRecordId)))
));

assertEquals(UUID.fromString(externalRecordId), updateMetadataResponse.getItemId());
}

@Test
@SneakyThrows
void downloadArmDataShouldSucceedIfServerReturns200Success() {
void downloadArmData_ShouldSucceed_WhenServerReturns200Success() {
// Given
stubFor(
WireMock.get(urlPathMatching(DOWNLOAD_ARM_DATA_PATH))
Expand All @@ -110,10 +161,17 @@ void downloadArmDataShouldSucceedIfServerReturns200Success() {
.withStatus(200)));

// When
feign.Response response = armApiClient.downloadArmData("Bearer token", CABINET_ID, EXTERNAL_RECORD_ID, EXTERNAL_FILE_ID);
try (feign.Response response = armApiClient.downloadArmData("Bearer token", CABINET_ID, EXTERNAL_RECORD_ID, EXTERNAL_FILE_ID)) {

//Then
try (InputStream expectedInputStream = Files.newInputStream(Paths.get("src/integrationTest/resources/wiremock/__files/testAudio.mp3"))) {
assertTrue(IOUtils.contentEquals(response.body().asInputStream(), expectedInputStream));
}
}
}

//Then
InputStream expectedInputStream = Files.newInputStream(Paths.get("src/integrationTest/resources/wiremock/__files/testAudio.mp3"));
assertTrue(IOUtils.contentEquals(response.body().asInputStream(), expectedInputStream));
private String formatDateTime(OffsetDateTime offsetDateTime) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
return offsetDateTime.format(dateTimeFormatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

class DataStoreToArmHelperIntTest extends IntegrationBase {

private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ssX";
private static final String DATE_TIME_FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSX";
private static final LocalDateTime HEARING_DATE = LocalDateTime.of(2023, 9, 26, 10, 0, 0);
private MediaEntity savedMedia;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
import uk.gov.hmcts.darts.arm.client.ArmTokenClient;
import uk.gov.hmcts.darts.arm.client.model.ArmTokenRequest;
import uk.gov.hmcts.darts.arm.client.model.ArmTokenResponse;
Expand All @@ -26,6 +27,7 @@

import java.io.File;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

Expand All @@ -52,6 +54,7 @@ class ArmApiServiceIntTest extends IntegrationBaseWithWiremock {
private static final String ARM_ERROR_BODY = """
{ "itemId": "00000000-0000-0000-0000-000000000000", "cabinetId": 0, ...}
""";
public static final String BINARY_CONTENT = "some binary content";

private ArmTokenRequest armTokenRequest;

Expand All @@ -73,7 +76,7 @@ class ArmApiServiceIntTest extends IntegrationBaseWithWiremock {
@Autowired
private ObjectMapper objectMapper;

@MockitoBean
@MockitoSpyBean
private ArmDataManagementConfiguration armDataManagementConfiguration;

@TempDir
Expand Down Expand Up @@ -101,7 +104,7 @@ void setup() {
}

@Test
void updateMetadata() throws Exception {
void updateMetadata_WithNanoSeconds() throws Exception {

// Given
var eventTimestamp = OffsetDateTime.parse("2024-01-31T11:29:56.101701Z").plusYears(7);
Expand All @@ -112,7 +115,58 @@ void updateMetadata() throws Exception {
var updateMetadataRequest = UpdateMetadataRequest.builder()
.itemId(EXTERNAL_RECORD_ID)
.manifest(UpdateMetadataRequest.Manifest.builder()
.eventDate(eventTimestamp)
.eventDate(formatDateTime(eventTimestamp))
.retConfScore(scoreConfId.getId())
.retConfReason(reasonConf)
.build())
.useGuidsForFields(false)
.build();
var updateMetadataResponse = UpdateMetadataResponse.builder()
.itemId(UUID.fromString(EXTERNAL_RECORD_ID))
.cabinetId(101)
.objectId(UUID.fromString("4bfe4fc7-4e2f-4086-8a0e-146cc4556260"))
.objectType(1)
.fileName("UpdateMetadata-20241801-122819.json")
.isError(false)
.responseStatus(0)
.responseStatusMessages(null)
.build();

String dummyResponse = objectMapper.writeValueAsString(updateMetadataResponse);
String dummyRequest = objectMapper.writeValueAsString(updateMetadataRequest);

stubFor(
WireMock.post(urlPathMatching(uploadPath)).withRequestBody(equalToJson(dummyRequest))
.willReturn(
aResponse().withHeader("Content-Type", "application/json").withBody(dummyResponse)
.withStatus(200)));

// When
var responseToTest = armApiService.updateMetadata(EXTERNAL_RECORD_ID, eventTimestamp, scoreConfId, reasonConf);

// Then
verify(armTokenClient).getToken(armTokenRequest);

WireMock.verify(postRequestedFor(urlPathMatching(uploadPath))
.withHeader("Authorization", new RegexPattern(bearerAuth))
.withRequestBody(equalToJson(dummyRequest)));

assertEquals(updateMetadataResponse, responseToTest);
}

@Test
void updateMetadata_WithZeroTimes() throws Exception {

// Given
var eventTimestamp = OffsetDateTime.parse("2024-01-31T00:00:00.00Z").plusYears(7);

var bearerAuth = "Bearer some-token";
var reasonConf = "reason";
var scoreConfId = RetentionConfidenceScoreEnum.CASE_PERFECTLY_CLOSED;
var updateMetadataRequest = UpdateMetadataRequest.builder()
.itemId(EXTERNAL_RECORD_ID)
.manifest(UpdateMetadataRequest.Manifest.builder()
.eventDate(formatDateTime(eventTimestamp))
.retConfScore(scoreConfId.getId())
.retConfReason(reasonConf)
.build())
Expand Down Expand Up @@ -161,7 +215,7 @@ void updateMetadataFailureResultsInAnExceptionBeingThrown() throws Exception {
var updateMetadataRequest = UpdateMetadataRequest.builder()
.itemId(EXTERNAL_RECORD_ID)
.manifest(UpdateMetadataRequest.Manifest.builder()
.eventDate(eventTimestamp)
.eventDate(formatDateTime(eventTimestamp))
.retConfScore(scoreConfId.getId())
.retConfReason(reasonConf)
.build())
Expand All @@ -186,7 +240,7 @@ void updateMetadataFailureResultsInAnExceptionBeingThrown() throws Exception {
@SneakyThrows
void downloadArmData() {
// Given
byte[] binaryData = "some binary content".getBytes();
byte[] binaryData = BINARY_CONTENT.getBytes();

stubFor(
WireMock.get(urlPathMatching(getDownloadPath(downloadPath, CABINET_ID, EXTERNAL_RECORD_ID, EXTERNAL_FILE_ID)))
Expand All @@ -195,15 +249,16 @@ void downloadArmData() {
.withStatus(200)));

// When
DownloadResponseMetaData downloadResponseMetaData = armApiService.downloadArmData(EXTERNAL_RECORD_ID, EXTERNAL_FILE_ID);
try (DownloadResponseMetaData downloadResponseMetaData = armApiService.downloadArmData(EXTERNAL_RECORD_ID, EXTERNAL_FILE_ID)) {

// Then
verify(armTokenClient).getToken(armTokenRequest);
// Then
verify(armTokenClient).getToken(armTokenRequest);

WireMock.verify(getRequestedFor(urlPathMatching(getDownloadPath(downloadPath, CABINET_ID, EXTERNAL_RECORD_ID, EXTERNAL_FILE_ID)))
.withHeader("Authorization", new RegexPattern("Bearer some-token")));
WireMock.verify(getRequestedFor(urlPathMatching(getDownloadPath(downloadPath, CABINET_ID, EXTERNAL_RECORD_ID, EXTERNAL_FILE_ID)))
.withHeader("Authorization", new RegexPattern("Bearer some-token")));

assertThat(downloadResponseMetaData.getResource().getInputStream().readAllBytes()).isEqualTo(binaryData);
assertThat(downloadResponseMetaData.getResource().getInputStream().readAllBytes()).isEqualTo(binaryData);
}
}

@Test
Expand Down Expand Up @@ -248,4 +303,9 @@ private ArmTokenResponse getArmTokenResponse() {
private String getDownloadPath(String downloadPath, String cabinetId, String recordId, String fileId) {
return downloadPath.replace("{cabinet_id}", cabinetId).replace("{record_id}", recordId).replace("{file_id}", fileId);
}

private String formatDateTime(OffsetDateTime offsetDateTime) {
DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSX");
return offsetDateTime.format(dateTimeFormatter);
}
}
Loading