Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
0a2bbfe
Initial setup for csv creation and upload
RebeccaHayleyPickles May 6, 2026
840b3b0
Update endpoint
RebeccaHayleyPickles May 6, 2026
a9cf2a2
Update
RebeccaHayleyPickles May 7, 2026
9ee8ffe
Fix slack message alert
RebeccaHayleyPickles May 8, 2026
af9c3e9
Update court data flow
RebeccaHayleyPickles May 12, 2026
54ab16e
Merge branch 'master' into FACT-2634-Csv-creation-for-cron-job
RebeccaHayleyPickles May 12, 2026
388266d
update csv formatter
RebeccaHayleyPickles May 13, 2026
75d3a61
Update CsvUtil.java
RebeccaHayleyPickles May 13, 2026
b1266a1
Add test file
RebeccaHayleyPickles May 13, 2026
2ad3866
fix sonar cloud
RebeccaHayleyPickles May 13, 2026
79a3533
increase test coverage
RebeccaHayleyPickles May 13, 2026
8e43a4b
Fix build
RebeccaHayleyPickles May 13, 2026
1498f22
Specify exceptions
RebeccaHayleyPickles May 13, 2026
488029e
Update exception handling
RebeccaHayleyPickles May 13, 2026
de8c63f
Update CsvUtilTest.java
RebeccaHayleyPickles May 13, 2026
e430aef
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 14, 2026
100985a
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 14, 2026
6f943d0
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 14, 2026
68966de
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 14, 2026
fcfbef8
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 14, 2026
3d3f94a
Address comments on PR
RebeccaHayleyPickles May 15, 2026
842fab1
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 18, 2026
6303edb
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 19, 2026
29e39fe
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 19, 2026
b227fcf
Merge branch 'master' into FACT-2634-Csv-creation-for-cron-job
RebeccaHayleyPickles May 19, 2026
1c537af
fix jackson dependency after merge
RebeccaHayleyPickles May 20, 2026
ba5b9be
Update jackson
RebeccaHayleyPickles May 21, 2026
aa731a5
Update CsvUtilTest.java
RebeccaHayleyPickles May 21, 2026
2d23137
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 22, 2026
cea789c
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 22, 2026
9b5d715
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 22, 2026
fd4f8a5
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 28, 2026
8c94044
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] May 28, 2026
79deae8
merge conflicts
amstevenson Jun 1, 2026
5d7ddc3
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 2, 2026
e36ff10
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 2, 2026
45b6675
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 3, 2026
a7b6070
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 3, 2026
74b6d60
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 3, 2026
9c465a0
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 4, 2026
1d9e9f8
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 4, 2026
37b2a64
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 4, 2026
d4a5b48
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 5, 2026
600f6e5
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 5, 2026
c29d996
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 8, 2026
bef1d8d
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 8, 2026
978dfce
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 10, 2026
3daf1de
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 12, 2026
a5540eb
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 15, 2026
01c86d7
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 16, 2026
a328d0b
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 17, 2026
bd3ac31
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 18, 2026
9130f7b
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 18, 2026
45ed01d
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 19, 2026
497796a
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 19, 2026
fdd07e3
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 19, 2026
b023678
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 24, 2026
0ec52c7
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 25, 2026
03de7bf
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 25, 2026
8e6cbaf
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 25, 2026
32bdbfa
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 25, 2026
346b852
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 25, 2026
169f0f9
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 26, 2026
51080ef
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 26, 2026
5a0e7ed
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 26, 2026
f100d90
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 29, 2026
53ef19e
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 29, 2026
37026a9
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jun 30, 2026
d3b6c53
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 1, 2026
fcad64f
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 2, 2026
3755db1
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 2, 2026
0751b5f
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 2, 2026
d0655ab
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 2, 2026
42e4a3d
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 3, 2026
4038cfa
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 3, 2026
be684cd
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 3, 2026
9b038c1
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 8, 2026
92c7d41
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 8, 2026
bb2153e
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 9, 2026
086f71b
Merge master into FACT-2634-Csv-creation-for-cron-job
github-actions[bot] Jul 9, 2026
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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ dependencies {

implementation group: 'com.azure.spring', name: 'spring-cloud-azure-starter-storage-blob', version: '7.3.0'


implementation group: 'tools.jackson.dataformat', name: 'jackson-dataformat-csv'
implementation group: 'tools.jackson.datatype', name: 'jackson-datatype-hibernate7'

implementation group: 'com.github.hmcts.java-logging', name: 'logging', version: '8.0.0'
Expand Down
4 changes: 4 additions & 0 deletions infrastructure/storage-account.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ module "storage_account" {
{
name = "photos",
access_type = "container"
},
{
name = "csv",
access_type = "container"
}
]

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package uk.gov.hmcts.reform.fact.functional.controllers;

import io.qameta.allure.Feature;
import io.restassured.response.Response;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import uk.gov.hmcts.reform.fact.functional.http.HttpClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.http.HttpStatus.UNAUTHORIZED;

@Feature("CSV Controller")
@DisplayName("CSV Controller")
class CsvControllerFunctionalTest {

private static final String CSV_ENDPOINT = "/csv/";
private static final String INVALID_TOKEN = "invalid-token";
private static final HttpClient http = new HttpClient();

@Test
@DisplayName("POST /csv/ without bearer token returns 401")
void shouldReturnUnauthorizedWithoutToken() {
final Response response = http.doPost(CSV_ENDPOINT, null, "");

assertThat(response.statusCode()).isEqualTo(UNAUTHORIZED.value());
}

@Test
@DisplayName("POST /csv/ with invalid bearer token returns 401")
void shouldReturnUnauthorizedWithInvalidToken() {
final Response response = http.doPost(CSV_ENDPOINT, null, INVALID_TOKEN);

assertThat(response.statusCode()).isEqualTo(UNAUTHORIZED.value());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import uk.gov.hmcts.reform.fact.data.api.services.AzureBlobService;

@Configuration
@Profile("test")
public class AzureBlobConfigurationTestConfiguration {
Expand All @@ -17,7 +19,10 @@ public class AzureBlobConfigurationTestConfiguration {
private BlobServiceClient blobServiceClient;

@Mock
private BlobContainerClient containerClient;
private BlobContainerClient photoContainerClient;

@Mock
private BlobContainerClient csvContainerClient;

@Mock
private BlobClient blobClient;
Expand All @@ -27,8 +32,13 @@ public AzureBlobConfigurationTestConfiguration() {
}

@Bean
public BlobContainerClient blobContainerClient() {
return containerClient;
public BlobContainerClient photoBlobContainerClient() {
return photoContainerClient;
}

@Bean
public BlobContainerClient csvBlobContainerClient() {
return csvContainerClient;
}

@Bean
Expand All @@ -40,4 +50,14 @@ public BlobClient blobClient() {
public BlobServiceClient blobServiceClient() {
return blobServiceClient;
}

@Bean
public AzureBlobService photoAzureBlobService() {
return new AzureBlobService(photoContainerClient);
}

@Bean
public AzureBlobService csvAzureBlobService() {
return new AzureBlobService(csvContainerClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package uk.gov.hmcts.reform.fact.data.api.controllers;

import io.qameta.allure.Feature;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.webmvc.test.autoconfigure.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import uk.gov.hmcts.reform.fact.data.api.clients.SlackClient;
import uk.gov.hmcts.reform.fact.data.api.services.AzureBlobService;
import uk.gov.hmcts.reform.fact.data.api.services.CourtDetailsViewService;
import uk.gov.hmcts.reform.fact.data.api.services.CourtService;

import java.util.Collections;

import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@Feature("CSV Controller")
@DisplayName("CSV Controller")
@SpringBootTest
@AutoConfigureMockMvc(addFilters = false)
@ActiveProfiles("test")
class CsvControllerTest {

@Autowired
private MockMvc mvc;

@MockitoBean(name = "csvAzureBlobService")
private AzureBlobService azureBlobService;

@MockitoBean
private CourtService courtService;

@MockitoBean
private CourtDetailsViewService courtDetailsViewService;

@MockitoBean
private SlackClient slackClient;

@Test
@DisplayName("POST /csv/ returns 200 when CSV is created and uploaded successfully")
void createAndUploadCsvReturns200() throws Exception {
when(courtService.getAllCourtDetails()).thenReturn(Collections.emptyList());
when(azureBlobService.uploadFile(anyString(), any())).thenReturn("http://example.com/blob");

mvc.perform(post("/csv/"))
Comment thread
RebeccaHayleyPickles marked this conversation as resolved.
.andExpect(status().isOk());

verify(azureBlobService, times(1)).uploadFile(anyString(), any());
}

@Test
@DisplayName("POST /csv/ returns 502 when Azure upload fails (e.g. container missing)")
void createAndUploadCsvReturns502OnAzureUploadException() throws Exception {
when(courtService.getAllCourtDetails()).thenReturn(Collections.emptyList());
when(azureBlobService.uploadFile(anyString(), any())).thenThrow(new RuntimeException("Container not found"));

mvc.perform(post("/csv/"))
.andExpect(status().isBadGateway())
.andExpect(jsonPath("$.message").value("Failed to upload CSV file to Azure Blob Storage"));
}

@Test
@DisplayName("POST /csv/ returns 500 when CSV creation fails")
void createAndUploadCsvReturns500OnCsvCreationException() throws Exception {
when(courtService.getAllCourtDetails()).thenThrow(new RuntimeException("DB error"));

mvc.perform(post("/csv/"))
.andExpect(status().isInternalServerError())
.andExpect(jsonPath("$.message").value("Failed to create CSV file"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,36 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import uk.gov.hmcts.reform.fact.data.api.services.AzureBlobService;
import org.springframework.beans.factory.annotation.Qualifier;

@Configuration
@Profile("!test")
public class AzureBlobConfiguration {

@Bean
public BlobContainerClient blobContainerClient(
public BlobContainerClient photoBlobContainerClient(
BlobServiceClient blobServiceClient,
@Value("${spring.cloud.azure.storage.blob.photo-container-name}") String containerName) {
return blobServiceClient.getBlobContainerClient(containerName);
}

@Bean
public BlobContainerClient csvBlobContainerClient(
BlobServiceClient blobServiceClient,
@Value("${spring.cloud.azure.storage.blob.container-name}") String containerName) {
@Value("${spring.cloud.azure.storage.blob.csv-container-name}") String containerName) {
return blobServiceClient.getBlobContainerClient(containerName);
}

@Bean
public AzureBlobService photoAzureBlobService(
@Qualifier("photoBlobContainerClient") BlobContainerClient photoBlobContainerClient) {
return new AzureBlobService(photoBlobContainerClient);
}

@Bean
public AzureBlobService csvAzureBlobService(
@Qualifier("csvBlobContainerClient") BlobContainerClient csvBlobContainerClient) {
return new AzureBlobService(csvBlobContainerClient);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uk.gov.hmcts.reform.fact.data.api.controllers;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import uk.gov.hmcts.reform.fact.data.api.security.SecuredFactRestController;
import uk.gov.hmcts.reform.fact.data.api.services.CsvService;

@SecuredFactRestController(
name = "CSV",
description = "Operations related to CSV file handling"
)
@RequestMapping("/csv")
public class CsvController {

private final CsvService csvService;

public CsvController(CsvService csvService) {
this.csvService = csvService;
}

@PostMapping("/")
@Operation(
summary = "Create and upload CSV file",
description = "Generates a CSV file with court data and uploads it to the configured storage service."
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "CSV file created and uploaded successfully"),
@ApiResponse(responseCode = "500", description = "Failed to create CSV file"),
@ApiResponse(responseCode = "502", description = "Failed to upload CSV file to storage")
})
public void createAndUploadCsv() {
csvService.createAndUploadCsv();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package uk.gov.hmcts.reform.fact.data.api.errorhandling;

import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.AzureUploadException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.CourtResourceNotFoundException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.CsvCreationException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.DuplicatedListItemException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.JsonConvertException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.InvalidAreaOfLawException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.InvalidDateRangeException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.InvalidFileException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.InvalidParameterCombinationException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.InvalidPostcodeException;
import uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions.NotFoundException;

import uk.gov.hmcts.reform.fact.data.api.validation.annotations.ValidUUID;

import java.time.LocalDateTime;
Expand Down Expand Up @@ -206,6 +210,27 @@ public ExceptionResponse handle(InvalidAreaOfLawException ex) {
return generateExceptionResponse(ex.getMessage());
}

@ExceptionHandler(JsonConvertException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ExceptionResponse handle(JsonConvertException ex) {
log.error("400, JSON conversion error. Details: {}", ex.getMessage());
return generateExceptionResponse(ex.getMessage());
}

@ExceptionHandler(AzureUploadException.class)
@ResponseStatus(HttpStatus.BAD_GATEWAY)
public ExceptionResponse handle(AzureUploadException ex) {
log.error("502, error while uploading CSV to Azure. Details: {}", ex.getMessage());
return generateExceptionResponse(ex.getMessage());
}

@ExceptionHandler(CsvCreationException.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ExceptionResponse handle(CsvCreationException ex) {
log.error("500, error while creating CSV file. Details: {}", ex.getMessage());
return generateExceptionResponse(ex.getMessage());
}

private ExceptionResponse generateExceptionResponse(String message) {
ExceptionResponse response = new ExceptionResponse();
response.setMessage(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions;

import lombok.experimental.StandardException;

@StandardException
public class CsvCreationException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.gov.hmcts.reform.fact.data.api.errorhandling.exceptions;

import lombok.experimental.StandardException;

@StandardException
public class JsonConvertException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package uk.gov.hmcts.reform.fact.data.api.models;

import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.InputStream;

/**
* An implementation of the {@link MultipartFile} interface that wraps a {@link String} content.
*
* <p>This class is used to represent a file created from a string, which can be then treated as a standard
* multipart file for operations like uploading to cloud storage.</p>
*/
@Getter
public class StringMultipartFile implements MultipartFile {

private final String name;
private final String originalFilename;
private final String contentType;
private final byte[] bytes;

/**
* Constructs a new StringMultipartFile.
*
* @param name the name of the file
* @param originalFilename the original filename
* @param contentType the content type of the file
* @param content the string content of the file
*/
public StringMultipartFile(String name, String originalFilename, String contentType, String content) {
this.name = name;
this.originalFilename = originalFilename;
this.contentType = contentType;
this.bytes = content.getBytes();
}

@Override
public boolean isEmpty() {
return bytes.length == 0;
}

@Override
public long getSize() {
return bytes.length;
}

@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(bytes);
}

@Override
public void transferTo(java.io.File dest) throws IllegalStateException {
// This implementation is currently empty as it's not required for current use cases,
// but the override is necessary for the interface.
}
}
Loading