forked from IQSS/dataverse
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pid): add dataset version pid configuration code IQSS#4499
This commit adds a new scope and setting to the JvmSettings, enabling the configuration of different modes for Dataset Version PIDs. These modes are depicted in VersionPidMode. A test ensures the parsability. In addition, VersionPidMode also contains a fine grained option to change the conduct of Dataverse collections and their datasets for these PIDs.
- Loading branch information
1 parent
51ff682
commit 8c8cc23
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
src/main/java/edu/harvard/iq/dataverse/pidproviders/VersionPidMode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package edu.harvard.iq.dataverse.pidproviders; | ||
|
||
import edu.harvard.iq.dataverse.Dataverse; | ||
|
||
import java.util.Arrays; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
/** | ||
* Enumlike class to bundle available options for PIDs of Dataset Versions. | ||
* Must be a class to ensure case-insensitive configuration values (not possible/reliable with enum) | ||
*/ | ||
public final class VersionPidMode { | ||
|
||
/** | ||
* Means feature is switched off, no version PIDs will be minted | ||
*/ | ||
public static final VersionPidMode OFF = new VersionPidMode("OFF"); | ||
|
||
/** | ||
* Means the feature is activated instance wide for all collections and datasets | ||
* (No opt-out so far!) | ||
*/ | ||
public static final VersionPidMode GLOBAL = new VersionPidMode("GLOBAL"); | ||
|
||
/** | ||
* Means the feature must be activated per Dataverse Collection (opt-in) | ||
*/ | ||
public static final VersionPidMode COLLECTION = new VersionPidMode("COLLECTION"); | ||
|
||
/** | ||
* A collection of conducts for mode {@link #COLLECTION}, used in {@link edu.harvard.iq.dataverse.Dataverse} | ||
* and {@link edu.harvard.iq.dataverse.DataverseServiceBean#wantsDatasetVersionPids(Dataverse)}: | ||
* <ol> | ||
* <li>Collection may inherit version pid behaviour from the parent collection(s),</li> | ||
* <li>Collection may choose to be actively enabling it</li> | ||
* <li>Collection may choose to opt out and skip the minting</li> | ||
* </ol> | ||
*/ | ||
public enum CollectionConduct { | ||
INHERIT("inherit"), | ||
ACTIVE("active"), | ||
SKIP("skip"); | ||
|
||
private final String name; | ||
|
||
CollectionConduct(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return this.name; | ||
} | ||
|
||
public static CollectionConduct findBy(String name) { | ||
return Arrays.stream(CollectionConduct.values()) | ||
.filter(cs -> cs.name.equalsIgnoreCase(name)) | ||
.findFirst() | ||
.orElse(null); | ||
} | ||
} | ||
|
||
|
||
// Init as unmodifiable set | ||
public static final Set<VersionPidMode> values; | ||
static { | ||
values = Set.of(OFF, GLOBAL, COLLECTION); | ||
} | ||
|
||
private final String mode; | ||
|
||
// Hide the no-arg constructor - no one shall get fancy ideas of extension. | ||
private VersionPidMode() { | ||
this.mode = ""; | ||
} | ||
|
||
// Hide the constructor - no one shall get fancy ideas of extension. | ||
private VersionPidMode(String mode) { | ||
this.mode = mode; | ||
} | ||
|
||
/** | ||
* Used to enable auto-conversion for MicroProfile Config. | ||
* Comparison is done case-insensitive to enable all variants of writing the config value. | ||
* | ||
* Note that a non-matching value will return null, which will trigger a {@link java.util.NoSuchElementException} | ||
* for the conversion. | ||
* | ||
* @see <a href="https://download.eclipse.org/microprofile/microprofile-config-3.0/microprofile-config-spec-3.0.html#_automatic_converters">MicroProfile Config Spec for Autoconverts</a> | ||
* | ||
* @param mode The mode to lookup | ||
* @return A matching constant or null if not matching. | ||
*/ | ||
public static VersionPidMode of(String mode) { | ||
return values.stream() | ||
.filter(m -> m.mode.equalsIgnoreCase(mode)) | ||
.findAny() | ||
.orElse(null); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof VersionPidMode)) return false; | ||
VersionPidMode that = (VersionPidMode) o; | ||
return mode.equalsIgnoreCase(that.mode); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(mode); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/test/java/edu/harvard/iq/dataverse/pidproviders/VersionPidModeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package edu.harvard.iq.dataverse.pidproviders; | ||
|
||
import edu.harvard.iq.dataverse.settings.JvmSettings; | ||
import edu.harvard.iq.dataverse.util.testing.JvmSetting; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.NoSuchElementException; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
class VersionPidModeTest { | ||
|
||
@Test | ||
@JvmSetting(key = JvmSettings.PID_VERSIONS_MODE, value = "collection") | ||
void setToValidValue() { | ||
assertEquals(VersionPidMode.COLLECTION, JvmSettings.PID_VERSIONS_MODE.lookup(VersionPidMode.class)); | ||
} | ||
|
||
@Test | ||
@JvmSetting(key = JvmSettings.PID_VERSIONS_MODE, value = "GloBal") | ||
void setToOtherValidValue() { | ||
assertEquals(VersionPidMode.GLOBAL, JvmSettings.PID_VERSIONS_MODE.lookup(VersionPidMode.class)); | ||
} | ||
|
||
@Test | ||
@JvmSetting(key = JvmSettings.PID_VERSIONS_MODE, value = "foobar") | ||
void setToInvalidValue() { | ||
assertThrows(NoSuchElementException.class, () -> JvmSettings.PID_VERSIONS_MODE.lookup(VersionPidMode.class)); | ||
} | ||
|
||
} |