Skip to content

Commit c87d989

Browse files
committed
Created function to find the dataset version
1 parent b3a17ca commit c87d989

File tree

2 files changed

+83
-16
lines changed

2 files changed

+83
-16
lines changed

zebedee-cms/src/main/java/com/github/onsdigital/zebedee/model/approval/ApproveTask.java

+20-10
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,9 @@ public class ApproveTask implements Callable<Boolean> {
6868
private final ContentDetailResolver contentDetailResolver;
6969
private final Notifier notifier;
7070
static ServiceSupplier<UploadService> uploadServiceSupplier;
71-
private static String correctDatasetVersion;
7271

7372
static {
7473
uploadServiceSupplier = () -> ZebedeeCmsService.getInstance().getUploadService();
75-
correctDatasetVersion = "";
7674
}
7775

7876
/**
@@ -361,24 +359,21 @@ protected void uploadNewEndpoint(Collection collection, CollectionReader collect
361359

362360
protected void uploadWhitelistedFiles(Collection collection, CollectionReader collectionReader)
363361
throws ZebedeeException, IOException {
362+
String correctDatasetVersion = findCorrectDatasetVersion(collectionReader.getReviewed().listUris());
364363
for (String uri : collectionReader.getReviewed().listUris()) {
365364
if (uri.endsWith(".csv") || uri.endsWith(".xlsx") || uri.endsWith(".xls") || uri.endsWith(".csdb")) {
366365
String fileName = uri.substring(1);
367366
Resource myFile = collectionReader.getResource(fileName);
368367
if (DatasetWhitelistChecker.isWhitelisted(myFile.getName())) {
369-
if (fileName.contains("previous")) {
370-
correctDatasetVersion = extractDatasetVersion(fileName);
371-
}
372368
info().data("filename", fileName).data("collectionId", collection.getDescription().getId())
373369
.log("File is whitelisted");
374-
uploadFile(myFile, fileName, collection.getDescription().getId());
370+
uploadFile(myFile, fileName, collection.getDescription().getId(), correctDatasetVersion);
375371
}
376372
}
377373
}
378-
correctDatasetVersion = "";
379374
}
380375

381-
protected void uploadFile(Resource myFile, String fileName, String collectionId)
376+
protected void uploadFile(Resource myFile, String fileName, String collectionId, String correctDatasetVersion)
382377
throws ZebedeeException, IOException {
383378
File file = new File("afile");
384379
try (FileOutputStream outputStream = new FileOutputStream(file)) {
@@ -397,7 +392,7 @@ protected void uploadFile(Resource myFile, String fileName, String collectionId)
397392
String baseFilename = datasetId.replaceAll(DatasetWhitelistChecker.REG_EX_STR, "");
398393
String datasetVersion = extractDatasetVersion(fileName);
399394
String generatedPath = filePathGenerator(datasetId, collection.getDescription().getPublishDate(),
400-
datasetVersion);
395+
datasetVersion, correctDatasetVersion);
401396
info().data("datasetId", datasetId).data("datasetVersion", datasetVersion).data("baseFilename", baseFilename).data("generatedPath", generatedPath)
402397
.log("file info");
403398
List<NameValuePair> params = createUploadParams(
@@ -483,7 +478,7 @@ protected String extractFileName(String fileName) {
483478
return fileName;
484479
}
485480

486-
protected String filePathGenerator(String datasetId, Date publishDate, String DatasetVersion) {
481+
protected String filePathGenerator(String datasetId, Date publishDate, String DatasetVersion, String correctDatasetVersion) {
487482
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
488483
if (publishDate == null) {
489484
publishDate = new Date();
@@ -523,4 +518,19 @@ protected String incrementDatasetVersionByOne(String datasetVersion) {
523518

524519
return letter + Integer.toString(number);
525520
}
521+
522+
protected String findCorrectDatasetVersion(List<String> listOfUris) {
523+
if (listOfUris == null) {
524+
throw new IllegalArgumentException("input array can't be null");
525+
}
526+
527+
String datasetVersion = "";
528+
for (String uri : listOfUris) {
529+
if (uri.contains("previous")) {
530+
datasetVersion = extractDatasetVersion(uri);
531+
return datasetVersion;
532+
}
533+
}
534+
return datasetVersion;
535+
}
526536
}

zebedee-cms/src/test/java/com/github/onsdigital/zebedee/model/approval/ApproveTaskTest.java

+63-6
Original file line numberDiff line numberDiff line change
@@ -509,12 +509,16 @@ public void testfilePathGenerator(){
509509
throw new RuntimeException(e);
510510
}
511511

512-
assertEquals(task.filePathGenerator("mm22", publishDate, "v123"), "ts-datasets/mm22/v123");
513-
assertEquals(task.filePathGenerator("a01jul2025", null, "v123"), "ts-datasets/other/" + today);
514-
assertEquals(task.filePathGenerator("x09jul2025", publishDate, "v123"), "ts-datasets/other/2024-07-18");
515-
assertEquals(task.filePathGenerator("dataset1", publishDate, "v123"), "ts-datasets/other/2024-07-18");
516-
assertEquals(task.filePathGenerator("rtisa", publishDate, "v123"), "ts-datasets/other/2024-07-18");
517-
assertEquals(task.filePathGenerator("cla01", publishDate, "v123"), "ts-datasets/other/2024-07-18");
512+
assertEquals(task.filePathGenerator("mm22", publishDate, "v123", ""), "ts-datasets/mm22/v123");
513+
assertEquals(task.filePathGenerator("a01jul2025", null, "v123", ""), "ts-datasets/other/" + today);
514+
assertEquals(task.filePathGenerator("x09jul2025", publishDate, "v123", ""), "ts-datasets/other/2024-07-18");
515+
assertEquals(task.filePathGenerator("dataset1", publishDate, "v123", ""), "ts-datasets/other/2024-07-18");
516+
assertEquals(task.filePathGenerator("rtisa", publishDate, "v123", ""), "ts-datasets/other/2024-07-18");
517+
assertEquals(task.filePathGenerator("cla01", publishDate, "v123", ""), "ts-datasets/other/2024-07-18");
518+
519+
assertEquals(task.filePathGenerator("mm22", publishDate, "v123", "v321"), "ts-datasets/mm22/v322");
520+
assertEquals(task.filePathGenerator("drsi", publishDate, "v456", "v654"), "ts-datasets/drsi/v655");
521+
assertEquals(task.filePathGenerator("pn2", publishDate, "current", ""), "ts-datasets/pn2/current");
518522
}
519523

520524
@Test
@@ -567,4 +571,57 @@ public void testIncrementDatasetVersionByOne_HappyPath() {
567571
assertEquals(expected, actual);
568572
}
569573

574+
@Test
575+
public void testfindCorrectDatasetVersion_nullInput() {
576+
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> task.findCorrectDatasetVersion(null));
577+
assertThat(ex.getMessage(), equalTo("input array can't be null"));
578+
}
579+
580+
@Test
581+
public void testfindCorrectDatasetVersion_correctInput() {
582+
List<String> listOfUris = new ArrayList<>();
583+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/augusttoseptember2024/diop.csv");
584+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/augusttoseptember2024/diop.xlsx");
585+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/augusttoseptember2024/previous/v1/data.json");
586+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/augusttoseptember2024/previous/v1/diop.csv");
587+
588+
String expected = "v1";
589+
String actual = task.findCorrectDatasetVersion(listOfUris);
590+
assertEquals(expected, actual);
591+
}
592+
593+
@Test
594+
public void testfindCorrectDatasetVersion_anotherCorrectInput_previousVersionExist() {
595+
List<String> listOfUris = new ArrayList<>();
596+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/current/diop.csv");
597+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/current/diop.xlsx");
598+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/current/previous/v123/data.json");
599+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/current/previous/v123/diop.csv");
600+
601+
String expected = "v123";
602+
String actual = task.findCorrectDatasetVersion(listOfUris);
603+
assertEquals(expected, actual);
604+
}
605+
606+
@Test
607+
public void testfindCorrectDatasetVersion_anotherCorrectInput_previousVersionDoesNotExist() {
608+
List<String> listOfUris = new ArrayList<>();
609+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/current/diop.csv");
610+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/current/diop.xlsx");
611+
listOfUris.add("/economy/grossdomesticproductgdp/datasets/mycollectionpagedltest1/september2024/diop.xlsx");
612+
613+
String expected = "";
614+
String actual = task.findCorrectDatasetVersion(listOfUris);
615+
assertEquals(expected, actual);
616+
}
617+
618+
@Test
619+
public void testfindCorrectDatasetVersion_emptyList() {
620+
List<String> listOfUris = new ArrayList<>();
621+
622+
String expected = "";
623+
String actual = task.findCorrectDatasetVersion(listOfUris);
624+
assertEquals(expected, actual);
625+
}
626+
570627
}

0 commit comments

Comments
 (0)