Skip to content

Commit

Permalink
Implemented ordering of deposits, by creation.timestamp or sequence-n…
Browse files Browse the repository at this point in the history
…umber.
  • Loading branch information
janvanmansum committed Nov 12, 2024
1 parent 9904827 commit c07a0d8
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 12 deletions.
21 changes: 15 additions & 6 deletions src/main/java/nl/knaw/dans/dvingest/core/Deposit.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NonNull;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import nl.knaw.dans.lib.dataverse.MetadataFieldDeserializer;
import nl.knaw.dans.lib.dataverse.model.dataset.Dataset;
Expand All @@ -38,6 +40,8 @@

@Getter
@Slf4j
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@ToString
public class Deposit implements Comparable<Deposit> {
private static final ObjectMapper MAPPER;

Expand All @@ -63,6 +67,7 @@ public Deposit(@NonNull Path location) {
}
}

@EqualsAndHashCode.Include
public UUID getId() {
return UUID.fromString(location.getFileName().toString());
}
Expand Down Expand Up @@ -93,12 +98,16 @@ public Path getFilesDir() {
return getBagDir().resolve("data");
}

public int getSeqNumber() {
return depositProperties.get("seqNumber") == null ? -1 : Integer.parseInt(depositProperties.getProperty("seqNumber"));
public int getSequenceNumber() {
return depositProperties.get("sequence-number") == null ? -1 : Integer.parseInt(depositProperties.getProperty("sequence-number"));
}

public OffsetDateTime getCreationTimestamp() {
return OffsetDateTime.parse(depositProperties.getProperty("creation.timestamp"));
var creationTimestamp = depositProperties.getProperty("creation-timestamp");
if (creationTimestamp == null) {
return null;
}
return OffsetDateTime.parse(creationTimestamp);
}

public void moveTo(Path targetDir) throws IOException {
Expand All @@ -108,14 +117,14 @@ public void moveTo(Path targetDir) throws IOException {

@Override
public int compareTo(@NotNull Deposit deposit) {
if (getSeqNumber() != -1 && deposit.getSeqNumber() != -1) {
return Integer.compare(getSeqNumber(), deposit.getSeqNumber());
if (getSequenceNumber() != -1 && deposit.getSequenceNumber() != -1) {
return Integer.compare(getSequenceNumber(), deposit.getSequenceNumber());
}
else if (getCreationTimestamp() != null && deposit.getCreationTimestamp() != null) {
return getCreationTimestamp().compareTo(deposit.getCreationTimestamp());
}
else {
throw new IllegalStateException("Deposit " + getId() + " or " + deposit.getId() + " has no sequence number");
throw new IllegalStateException("Deposit " + getId() + " should contain either a sequence number or a creation timestamp");
}
}
}
116 changes: 110 additions & 6 deletions src/test/java/nl/knaw/dans/dvingest/core/DepositTest.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,25 @@
/*
* Copyright (C) 2024 DANS - Data Archiving and Networked Services ([email protected])
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package nl.knaw.dans.dvingest.core;

import org.junit.jupiter.api.Test;

import java.nio.file.Files;
import java.util.Properties;
import java.util.TreeSet;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -39,23 +56,110 @@ public void getId_should_return_uuid_from_dir_name() throws Exception {
}

@Test
public void deposits_should_be_ordered_by_seqNums() throws Exception {
public void deposits_should_be_ordered_by_sequence_number() throws Exception {
// Given
var id1 = UUID.randomUUID().toString();
var id2 = UUID.randomUUID().toString();
var id3 = UUID.randomUUID().toString();

var deposit1 = testDir.resolve(id1);
Files.createDirectories(deposit1);
Files.createFile(deposit1.resolve("deposit.properties"));

var dir1 = testDir.resolve(id1);
Files.createDirectories(dir1);
var props1 = new Properties();
props1.setProperty("sequence-number", "1");
props1.store(Files.newBufferedWriter(dir1.resolve("deposit.properties")), "");
var dir2 = testDir.resolve(id2);
Files.createDirectories(dir2);
var props2 = new Properties();
props2.setProperty("sequence-number", "2");
props2.store(Files.newBufferedWriter(dir2.resolve("deposit.properties")), "");
var dir3 = testDir.resolve(id3);
Files.createDirectories(dir3);
var props3 = new Properties();
props3.setProperty("sequence-number", "3");
props3.store(Files.newBufferedWriter(dir3.resolve("deposit.properties")), "");

var deposit1 = new Deposit(dir1);
var deposit2 = new Deposit(dir2);
var deposit3 = new Deposit(dir3);

// When
var deposits = List.of(deposit2, deposit3, deposit1);
var deposits = new TreeSet<>();
deposits.add(deposit2);
deposits.add(deposit1);
deposits.add(deposit3);

// Then
assertThat(deposits).containsExactly(deposit1, deposit2, deposit3);
}

@Test
public void deposits_should_be_ordered_by_creation_timestamp() throws Exception {
// Given
var id1 = UUID.randomUUID().toString();
var id2 = UUID.randomUUID().toString();
var id3 = UUID.randomUUID().toString();

var dir1 = testDir.resolve(id1);
Files.createDirectories(dir1);
var props1 = new Properties();
props1.setProperty("creation-timestamp", "2023-01-01T10:00:00Z");
props1.store(Files.newBufferedWriter(dir1.resolve("deposit.properties")), "");

var dir2 = testDir.resolve(id2);
Files.createDirectories(dir2);
var props2 = new Properties();
props2.setProperty("creation-timestamp", "2023-01-02T10:00:00Z");
props2.store(Files.newBufferedWriter(dir2.resolve("deposit.properties")), "");

var dir3 = testDir.resolve(id3);
Files.createDirectories(dir3);
var props3 = new Properties();
props3.setProperty("creation-timestamp", "2023-01-03T10:00:00Z");
props3.store(Files.newBufferedWriter(dir3.resolve("deposit.properties")), "");

var deposit1 = new Deposit(dir1);
var deposit2 = new Deposit(dir2);
var deposit3 = new Deposit(dir3);

// When
var deposits = new TreeSet<>();
deposits.add(deposit2);
deposits.add(deposit1);
deposits.add(deposit3);

// Then
assertThat(deposits).containsExactly(deposit1, deposit2, deposit3);
}

@Test
public void ordering_should_fail_if_both_sequence_number_and_creation_timestamp_are_missing_in_one_of_the_deposits() throws Exception {
// Given
var id1 = UUID.randomUUID().toString();
var id2 = UUID.randomUUID().toString();
var id3 = UUID.randomUUID().toString();

var dir1 = testDir.resolve(id1);
Files.createDirectories(dir1);
var props1 = new Properties();
props1.store(Files.newBufferedWriter(dir1.resolve("deposit.properties")), "");

var dir2 = testDir.resolve(id2);
Files.createDirectories(dir2);
var props2 = new Properties();
props2.setProperty("sequence-number", "2");
props2.store(Files.newBufferedWriter(dir2.resolve("deposit.properties")), "");

var deposit1 = new Deposit(dir1);
var deposit2 = new Deposit(dir2);

// Then
assertThatIllegalStateException().isThrownBy(() -> {
// When
var deposits = new TreeSet<>();
deposits.add(deposit2);
deposits.add(deposit1);
})
.withMessage("Deposit " + deposit1.getId() + " should contain either a sequence number or a creation timestamp");
}

}

0 comments on commit c07a0d8

Please sign in to comment.