Skip to content

Commit

Permalink
test(settings): make ConfigCheckService actually testable IQSS#9662
Browse files Browse the repository at this point in the history
  • Loading branch information
poikilotherm committed Aug 22, 2023
1 parent ec131f8 commit 848f564
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package edu.harvard.iq.dataverse.settings;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -17,35 +17,32 @@
import static java.nio.file.attribute.PosixFilePermission.OWNER_READ;

class ConfigCheckServiceTest {

@TempDir
static Path testDir;

private static final String testDirProp = "test.filesDir";

@AfterAll
static void tearDown() {
System.clearProperty(testDirProp);
}

@Nested
class TestDirNotWritable {
@TempDir
Path testDir;

private String oldUploadDirSetting;
Path notWriteableSubfolder = testDir.resolve("readonly");

@BeforeEach
void setUp() throws IOException {
Files.setPosixFilePermissions(this.testDir, Set.of(OWNER_READ, GROUP_READ));

// TODO: This is a workaround until PR #9273 is merged, providing the ability to lookup values for
// @JvmSetting from static methods. Should be deleted.
this.oldUploadDirSetting = System.getProperty(JvmSettings.UPLOADS_DIRECTORY.getScopedKey());
System.setProperty(JvmSettings.UPLOADS_DIRECTORY.getScopedKey(), this.testDir.toString());
}

@AfterEach
void tearDown() {
// TODO: This is a workaround until PR #9273 is merged, providing the ability to lookup values for
// @JvmSetting from static methods. Should be deleted.
if (this.oldUploadDirSetting != null)
System.setProperty(JvmSettings.UPLOADS_DIRECTORY.getScopedKey(), this.oldUploadDirSetting);
Files.createDirectory(notWriteableSubfolder);
Files.setPosixFilePermissions(notWriteableSubfolder, Set.of(OWNER_READ, GROUP_READ));
System.setProperty(testDirProp, notWriteableSubfolder.toString());
}

@Test
void writeCheckFails() {
Assumptions.assumeTrue(Files.exists(this.testDir));
Assumptions.assumeTrue(Files.exists(notWriteableSubfolder));

ConfigCheckService sut = new ConfigCheckService();
Assertions.assertFalse(sut.checkSystemDirectories());
Expand All @@ -54,38 +51,47 @@ void writeCheckFails() {

@Nested
class TestDirNotExistent {
@TempDir
Path testDir;
String subFolder = "foobar";

String oldUploadDirSetting;
Path notExistTestfolder = testDir.resolve("parent-readonly");
Path notExistConfigSubfolder = notExistTestfolder.resolve("foobar");

@BeforeEach
void setUp() throws IOException {
Files.createDirectory(notExistTestfolder);
// Make test dir not writeable, so the subfolder cannot be created
Files.setPosixFilePermissions(this.testDir, Set.of(OWNER_READ, GROUP_READ));
Files.setPosixFilePermissions(notExistTestfolder, Set.of(OWNER_READ, GROUP_READ));
System.setProperty(testDirProp, notExistConfigSubfolder.toString());
}

@Test
void mkdirFails() {
Assumptions.assumeTrue(Files.exists(notExistTestfolder));
Assumptions.assumeFalse(Files.exists(notExistConfigSubfolder));

// TODO: This is a workaround until PR #9273 is merged, providing the ability to lookup values for
// @JvmSetting from static methods. Should be deleted.
oldUploadDirSetting = System.getProperty(JvmSettings.UPLOADS_DIRECTORY.getScopedKey());
System.setProperty(JvmSettings.UPLOADS_DIRECTORY.getScopedKey(), this.testDir.resolve(this.subFolder).toString());
ConfigCheckService sut = new ConfigCheckService();
Assertions.assertFalse(sut.checkSystemDirectories());
}
}

@Nested
class TestDirCreated {

Path missingToBeCreatedTestfolder = testDir.resolve("create-me");
Path missingToBeCreatedSubfolder = missingToBeCreatedTestfolder.resolve("foobar");

@AfterEach
void tearDown() {
// TODO: This is a workaround until PR #9273 is merged, providing the ability to lookup values for
// @JvmSetting from static methods. Should be deleted.
if (this.oldUploadDirSetting != null)
System.setProperty(JvmSettings.UPLOADS_DIRECTORY.getScopedKey(), this.oldUploadDirSetting);
@BeforeEach
void setUp() throws IOException {
Files.createDirectory(missingToBeCreatedTestfolder);
System.setProperty(testDirProp, missingToBeCreatedSubfolder.toString());
}

@Test
void mkdirFails() {
Assumptions.assumeTrue(Files.exists(this.testDir));
Assumptions.assumeFalse(Files.exists(this.testDir.resolve(this.subFolder)));
void mkdirSucceeds() {
Assumptions.assumeTrue(Files.exists(missingToBeCreatedTestfolder));
Assumptions.assumeFalse(Files.exists(missingToBeCreatedSubfolder));

ConfigCheckService sut = new ConfigCheckService();
Assertions.assertFalse(sut.checkSystemDirectories());
Assertions.assertTrue(sut.checkSystemDirectories());
}
}

Expand Down
7 changes: 6 additions & 1 deletion src/test/resources/META-INF/microprofile-config.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@
dataverse.pid.ezid.api-url=http://example.org
# Also requires the username and the password to be present when used in production, use a default for unit testing.
dataverse.pid.ezid.username=Dataverse Unit Test
dataverse.pid.ezid.password=supersecret
dataverse.pid.ezid.password=supersecret

# To test ConfigCheckService, point our files directories to a common test dir
dataverse.files.directory=${test.filesDir}
dataverse.files.uploads=${test.filesDir}/uploads
dataverse.files.docroot=${test.filesDir}/docroot

0 comments on commit 848f564

Please sign in to comment.