From f5c42b152efd2d6f764861bbecf933019ad106fd Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Sat, 16 Mar 2019 16:40:31 +0100 Subject: [PATCH 01/11] Add stub for AuthFilterTest --- .../authorization/AuthFilterTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/test/java/edu/harvard/iq/dataverse/authorization/AuthFilterTest.java diff --git a/src/test/java/edu/harvard/iq/dataverse/authorization/AuthFilterTest.java b/src/test/java/edu/harvard/iq/dataverse/authorization/AuthFilterTest.java new file mode 100644 index 00000000000..a676f316b7d --- /dev/null +++ b/src/test/java/edu/harvard/iq/dataverse/authorization/AuthFilterTest.java @@ -0,0 +1,20 @@ +package edu.harvard.iq.dataverse.authorization; + +import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinAuthenticationProvider; +import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.GitHubOAuth2AP; +import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.GoogleOAuth2AP; +import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.OrcidOAuth2AP; +import edu.harvard.iq.dataverse.authorization.providers.shib.ShibAuthenticationProvider; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import org.junit.Test; +import static org.junit.Assert.*; + +public class AuthFilterTest { + + @Test + public void testIsNonLocalLoginEnabled() { + // TODO: + } +} From bc500f2c2868752b3daa2d49d7dddd95e5f392ed Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Sat, 16 Mar 2019 17:07:22 +0100 Subject: [PATCH 02/11] Add stub for Password reset init response test --- .../PasswordResetInitResponseTest.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java diff --git a/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java b/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java new file mode 100644 index 00000000000..4a893cbd162 --- /dev/null +++ b/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java @@ -0,0 +1,20 @@ +package edu.harvard.iq.dataverse.passwordreset; + +import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinAuthenticationProvider; +import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.GitHubOAuth2AP; +import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.GoogleOAuth2AP; +import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.OrcidOAuth2AP; +import edu.harvard.iq.dataverse.authorization.providers.shib.ShibAuthenticationProvider; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; +import org.junit.Test; +import static org.junit.Assert.*; + +public class PasswordResetInitResponseTest { + + @Test + public void testIsNonLocalLoginEnabled() { + // TODO: + } +} From 8b3fbbf07763e5092dd4ef5a1ed8b6ba77fdd87e Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Sat, 16 Mar 2019 18:32:42 +0100 Subject: [PATCH 03/11] Add test cases for FileSizeChecker.isAllowedFileSize #5634 --- .../datasetutility/FileSizeCheckerTest.java | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java b/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java index 3bb4ee59a6e..54315a35692 100644 --- a/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java @@ -6,12 +6,19 @@ package edu.harvard.iq.dataverse.datasetutility; import static edu.harvard.iq.dataverse.datasetutility.FileSizeChecker.bytesToHumanReadable; + +import edu.harvard.iq.dataverse.datasetutility.FileSizeChecker.FileSizeResponse; import edu.harvard.iq.dataverse.util.BundleUtil; +import edu.harvard.iq.dataverse.util.SystemConfig; + import java.util.ArrayList; import java.util.Arrays; import java.util.List; + +import org.junit.Before; import org.junit.Test; import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @@ -19,6 +26,20 @@ */ public class FileSizeCheckerTest { + private FileSizeChecker fileSizeChecker; + + @Before + public void setUp() { + // initialize a system config and instantiate a file size checker + // override the max file upload side to allow for testing + fileSizeChecker = new FileSizeChecker(new SystemConfig() { + @Override + public Long getMaxFileUploadSize() { + return 1000L; + } + }); + } + @Test public void testBytesToHumanReadable() { long[] sizes = {1L, 1023L, 1986L, 125707L, 2759516000L, 12039650000000L}; @@ -35,4 +56,40 @@ public void testBytesToHumanReadable() { assertEquals(expLongAns, longAns); } + @Test(expected = NullPointerException.class) + public void testIsAllowedFileSize_throwsOnNull() { + fileSizeChecker.isAllowedFileSize(null); + } + + @Test + public void testIsAllowedFileSize_allowsSmallerFileSize() { + FileSizeResponse response = fileSizeChecker.isAllowedFileSize(999L); + assertTrue(response.fileSizeOK); + } + + @Test + public void testIsAllowedFileSize_allowsEqualFileSize() { + FileSizeResponse response = fileSizeChecker.isAllowedFileSize(1000L); + assertTrue(response.fileSizeOK); + } + + @Test + public void testIsAllowedFileSize_rejectsBiggerFileSize() { + FileSizeResponse response = fileSizeChecker.isAllowedFileSize(1001L); + assertFalse(response.fileSizeOK); + } + + @Test(expected = NullPointerException.class) + public void testIsAllowedFileSize_allowsOnUnboundedFileSize() { + // initialize a system config and instantiate a file size checker + // ensure that a max filesize is not set + FileSizeChecker unboundedFileSizeChecker = new FileSizeChecker(new SystemConfig() { + @Override + public Long getMaxFileUploadSize() { + return null; + } + }); + FileSizeResponse response = unboundedFileSizeChecker.isAllowedFileSize(Long.MAX_VALUE); + assertTrue(response.fileSizeOK); + } } From 46dd8ce9116032c545c82d1c3a411db714e41ddb Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Sat, 16 Mar 2019 18:53:09 +0100 Subject: [PATCH 04/11] Add junit 5 stubs for password reset init response test --- .../PasswordResetInitResponseTest.java | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java b/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java index 4a893cbd162..9a59cfdea2a 100644 --- a/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java @@ -5,16 +5,27 @@ import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.GoogleOAuth2AP; import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.OrcidOAuth2AP; import edu.harvard.iq.dataverse.authorization.providers.shib.ShibAuthenticationProvider; + +import static org.junit.jupiter.api.Assertions.fail; + import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; -import org.junit.Test; -import static org.junit.Assert.*; + +import org.junit.jupiter.api.Test; public class PasswordResetInitResponseTest { + private PasswordResetData passwordResetData = new PasswordResetData() { + @Override + public String getToken() { + return "abcd"; + } + }; + @Test - public void testIsNonLocalLoginEnabled() { + public void testGeneratesValidResetUrl() { // TODO: + fail(); } } From a77b1d1fb0266d8f026aa197204390d446169a04 Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Sat, 16 Mar 2019 19:14:38 +0100 Subject: [PATCH 05/11] Move FileSizeChecker tests to Unit 5 --- .../datasetutility/FileSizeCheckerTest.java | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java b/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java index 54315a35692..656d9a99d8a 100644 --- a/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java @@ -15,9 +15,12 @@ import java.util.Arrays; import java.util.List; -import org.junit.Before; -import org.junit.Test; -import static org.junit.Assert.*; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; /** @@ -28,7 +31,7 @@ public class FileSizeCheckerTest { private FileSizeChecker fileSizeChecker; - @Before + @BeforeAll public void setUp() { // initialize a system config and instantiate a file size checker // override the max file upload side to allow for testing @@ -56,9 +59,11 @@ public void testBytesToHumanReadable() { assertEquals(expLongAns, longAns); } - @Test(expected = NullPointerException.class) + @Test public void testIsAllowedFileSize_throwsOnNull() { - fileSizeChecker.isAllowedFileSize(null); + assertThrows(NullPointerException.class, () -> { + fileSizeChecker.isAllowedFileSize(null); + }); } @Test @@ -79,7 +84,7 @@ public void testIsAllowedFileSize_rejectsBiggerFileSize() { assertFalse(response.fileSizeOK); } - @Test(expected = NullPointerException.class) + @Test public void testIsAllowedFileSize_allowsOnUnboundedFileSize() { // initialize a system config and instantiate a file size checker // ensure that a max filesize is not set From f4ae9310091d71348fddbf4f4b661ee629eac192 Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Sat, 16 Mar 2019 19:24:40 +0100 Subject: [PATCH 06/11] Add dependency on junit-jupiter-params| --- pom.xml | 177 ++++++++++++++++++++++++++++---------------------------- 1 file changed, 87 insertions(+), 90 deletions(-) diff --git a/pom.xml b/pom.xml index 67f5328877d..9ba03827af0 100644 --- a/pom.xml +++ b/pom.xml @@ -3,19 +3,17 @@ 4.0.0 edu.harvard.iq dataverse 4.11 war - dataverse - ${project.build.directory}/endorsed - UTF-8 - -Xdoclint:none + UTF-8 + -Xdoclint:none UTC @@ -26,12 +24,10 @@ This seems to play well with NetBeans 8.2, IDEA 2018.1 and mvn including compatibility with JaCoCo. --> -Duser.timezone=${project.timezone} -Dfile.encoding=${project.build.sourceEncoding} -Duser.language=${project.language} -Duser.region=${project.region} - 1.11.172 2.9.6 1.2 4.5.5 - 4.12 5.3.1 5.3.1 @@ -44,7 +40,6 @@ --> 0.8.1 - @@ -76,7 +71,6 @@ file://${project.basedir}/local_lib - @@ -107,7 +101,6 @@ - + org.javaswift joss @@ -503,27 +502,27 @@ scribejava-apis 3.1.0 - - - - - - - - com.lyncode - xoai-common - 4.1.0-header-patch - - - com.lyncode - xoai-data-provider - 4.1.0-header-patch - - - com.lyncode - xoai-service-provider - 4.1.0-header-patch - + + + + + + + + com.lyncode + xoai-common + 4.1.0-header-patch + + + com.lyncode + xoai-data-provider + 4.1.0-header-patch + + + com.lyncode + xoai-service-provider + 4.1.0-header-patch + com.google.auto.service @@ -534,16 +533,16 @@ - org.glassfish.jersey.containers - jersey-container-servlet - 2.23.2 - + org.glassfish.jersey.containers + jersey-container-servlet + 2.23.2 + - - org.glassfish.jersey.media - jersey-media-multipart - 2.23.2 - + + org.glassfish.jersey.media + jersey-media-multipart + 2.23.2 + com.mashape.unirest unirest-java @@ -551,23 +550,23 @@ - org.apache.commons - commons-compress - 1.18 - - + org.apache.commons + commons-compress + 1.18 + + org.duracloud common 4.4.6 - - org.slf4j - log4j-over-slf4j - - - ch.qos.logback - logback-classic - + + org.slf4j + log4j-over-slf4j + + + ch.qos.logback + logback-classic + @@ -575,20 +574,19 @@ storeclient 4.4.6 - - org.slf4j - log4j-over-slf4j - - - com.amazonaws - aws-java-sdk-sqs - - - ch.qos.logback - logback-classic - + + org.slf4j + log4j-over-slf4j + + + com.amazonaws + aws-java-sdk-sqs + + + ch.qos.logback + logback-classic + - commons-cli @@ -602,14 +600,13 @@ 1.19 - - - - - true - - - edu.harvard.iq.dataverse.NonEssentialTests - - - - all-unit-tests - - + + dev + + + + true + + + edu.harvard.iq.dataverse.NonEssentialTests + + + + all-unit-tests + + From 3d44bc08c62a8b337ea7a64e012d7c493685265a Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Sat, 16 Mar 2019 19:26:08 +0100 Subject: [PATCH 07/11] Refactor FileSizeCheckerTest for JUnit5 and @ParametrizedTest --- .../datasetutility/FileSizeCheckerTest.java | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java b/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java index 656d9a99d8a..e1731f02df7 100644 --- a/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java @@ -15,8 +15,10 @@ import java.util.Arrays; import java.util.List; -import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -31,7 +33,7 @@ public class FileSizeCheckerTest { private FileSizeChecker fileSizeChecker; - @BeforeAll + @BeforeEach public void setUp() { // initialize a system config and instantiate a file size checker // override the max file upload side to allow for testing @@ -66,21 +68,17 @@ public void testIsAllowedFileSize_throwsOnNull() { }); } - @Test - public void testIsAllowedFileSize_allowsSmallerFileSize() { - FileSizeResponse response = fileSizeChecker.isAllowedFileSize(999L); - assertTrue(response.fileSizeOK); - } - - @Test - public void testIsAllowedFileSize_allowsEqualFileSize() { - FileSizeResponse response = fileSizeChecker.isAllowedFileSize(1000L); + @ParameterizedTest + @ValueSource(longs = { 0L, 999L, 1000L }) + public void testIsAllowedFileSize_allowsSmallerOrEqualFileSize(Long fileSize) { + FileSizeResponse response = fileSizeChecker.isAllowedFileSize(fileSize); assertTrue(response.fileSizeOK); } - @Test - public void testIsAllowedFileSize_rejectsBiggerFileSize() { - FileSizeResponse response = fileSizeChecker.isAllowedFileSize(1001L); + @ParameterizedTest + @ValueSource(longs = { 1001L, Long.MAX_VALUE }) + public void testIsAllowedFileSize_rejectsBiggerFileSize(Long fileSize) { + FileSizeResponse response = fileSizeChecker.isAllowedFileSize(fileSize); assertFalse(response.fileSizeOK); } @@ -94,7 +92,7 @@ public Long getMaxFileUploadSize() { return null; } }); - FileSizeResponse response = unboundedFileSizeChecker.isAllowedFileSize(Long.MAX_VALUE); + FileSizeResponse response = unboundedFileSizeChecker.isAllowedFileSize(1000L); assertTrue(response.fileSizeOK); } } From 0a374c7a9accde5bca96b9df8f1402f4657579a3 Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Sat, 16 Mar 2019 20:29:26 +0100 Subject: [PATCH 08/11] Add tests for DataAccess.createNewStorageIO #5634 --- .../iq/dataverse/dataaccess/DataAccess.java | 30 +++---- .../authorization/AuthFilterTest.java | 20 ----- .../dataverse/dataaccess/DataAccessTest.java | 78 +++++++++++++++++++ .../PasswordResetInitResponseTest.java | 31 -------- 4 files changed, 93 insertions(+), 66 deletions(-) delete mode 100644 src/test/java/edu/harvard/iq/dataverse/authorization/AuthFilterTest.java create mode 100644 src/test/java/edu/harvard/iq/dataverse/dataaccess/DataAccessTest.java delete mode 100644 src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java diff --git a/src/main/java/edu/harvard/iq/dataverse/dataaccess/DataAccess.java b/src/main/java/edu/harvard/iq/dataverse/dataaccess/DataAccess.java index 21e33a31964..acb9336b5c6 100644 --- a/src/main/java/edu/harvard/iq/dataverse/dataaccess/DataAccess.java +++ b/src/main/java/edu/harvard/iq/dataverse/dataaccess/DataAccess.java @@ -35,7 +35,7 @@ public DataAccess() { public static final String DEFAULT_STORAGE_DRIVER_IDENTIFIER = System.getProperty("dataverse.files.storage-driver-id"); - + // The getStorageIO() methods initialize StorageIO objects for // datafiles that are already saved using one of the supported Dataverse // DataAccess IO drivers. @@ -45,7 +45,7 @@ public static StorageIO getStorageIO(T dvObject) throws //passing DVObject instead of a datafile to accomodate for use of datafiles as well as datasets public static StorageIO getStorageIO(T dvObject, DataAccessRequest req) throws IOException { - + if (dvObject == null || dvObject.getStorageIdentifier() == null || dvObject.getStorageIdentifier().isEmpty()) { @@ -57,34 +57,34 @@ public static StorageIO getStorageIO(T dvObject, DataAcc return new FileAccessIO<>(dvObject, req); } else if (dvObject.getStorageIdentifier().startsWith("swift://")){ return new SwiftAccessIO<>(dvObject, req); - } else if (dvObject.getStorageIdentifier().startsWith("s3://")){ + } else if (dvObject.getStorageIdentifier().startsWith("s3://")){ return new S3AccessIO<>(dvObject, req); } else if (dvObject.getStorageIdentifier().startsWith("tmp://")) { throw new IOException("DataAccess IO attempted on a temporary file that hasn't been permanently saved yet."); } - - // TODO: - // This code will need to be extended with a system of looking up - // available storage plugins by the storage tag embedded in the - // "storage identifier". + + // TODO: + // This code will need to be extended with a system of looking up + // available storage plugins by the storage tag embedded in the + // "storage identifier". // -- L.A. 4.0.2 - + throw new IOException("getDataAccessObject: Unsupported storage method."); } - - // Experimental extension of the StorageIO system allowing direct access to + + // Experimental extension of the StorageIO system allowing direct access to // stored physical files that may not be associated with any DvObjects - + public static StorageIO getDirectStorageIO(String storageLocation) throws IOException { if (storageLocation.startsWith("file://")) { return new FileAccessIO(storageLocation.substring(7)); } else if (storageLocation.startsWith("swift://")){ return new SwiftAccessIO<>(storageLocation.substring(8)); - } else if (storageLocation.startsWith("s3://")){ + } else if (storageLocation.startsWith("s3://")){ return new S3AccessIO<>(storageLocation.substring(5)); } - + throw new IOException("getDirectStorageIO: Unsupported storage method."); } @@ -123,6 +123,6 @@ public static StorageIO createNewStorageIO(T dvObject, S storageIO.open(DataAccessOption.WRITE_ACCESS); return storageIO; } - + } diff --git a/src/test/java/edu/harvard/iq/dataverse/authorization/AuthFilterTest.java b/src/test/java/edu/harvard/iq/dataverse/authorization/AuthFilterTest.java deleted file mode 100644 index a676f316b7d..00000000000 --- a/src/test/java/edu/harvard/iq/dataverse/authorization/AuthFilterTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package edu.harvard.iq.dataverse.authorization; - -import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinAuthenticationProvider; -import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.GitHubOAuth2AP; -import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.GoogleOAuth2AP; -import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.OrcidOAuth2AP; -import edu.harvard.iq.dataverse.authorization.providers.shib.ShibAuthenticationProvider; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import org.junit.Test; -import static org.junit.Assert.*; - -public class AuthFilterTest { - - @Test - public void testIsNonLocalLoginEnabled() { - // TODO: - } -} diff --git a/src/test/java/edu/harvard/iq/dataverse/dataaccess/DataAccessTest.java b/src/test/java/edu/harvard/iq/dataverse/dataaccess/DataAccessTest.java new file mode 100644 index 00000000000..48a92468f4f --- /dev/null +++ b/src/test/java/edu/harvard/iq/dataverse/dataaccess/DataAccessTest.java @@ -0,0 +1,78 @@ +package edu.harvard.iq.dataverse.dataaccess; + +import edu.harvard.iq.dataverse.DataFile; +import edu.harvard.iq.dataverse.Dataset; +import edu.harvard.iq.dataverse.mocks.MocksFactory; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import static org.junit.jupiter.api.Assertions.*; +import java.io.IOException; + + +public class DataAccessTest { + DataFile datafile; + Dataset dataset; + DataAccess dataAccess; + + @BeforeEach + void setUp() { + datafile = MocksFactory.makeDataFile(); + dataset = MocksFactory.makeDataset(); + datafile.setOwner(dataset); + } + + @Test + void testCreateNewStorageIO_throwsWithoutObject() { + assertThrows(IOException.class, () -> { + DataAccess.createNewStorageIO(null, "valid-tag"); + }); + } + + @Test + void testCreateNewStorageIO_throwsWithoutStorageTag() { + assertThrows(IOException.class, () -> { + DataAccess.createNewStorageIO(datafile, null); + }); + } + + @Test + void testCreateNewStorageIO_throwsWithEmptyStorageTag() { + assertThrows(IOException.class, () -> { + DataAccess.createNewStorageIO(datafile, ""); + }); + } + + @Test + void testCreateNewStorageIO_throwsOnUnsupported() { + assertThrows(IOException.class, () -> { + DataAccess.createNewStorageIO(datafile, "valid-tag", "zip-unsupported"); + }); + } + + @Test + void testCreateNewStorageIO_createsFileAccessIObyDefault() throws IOException { + StorageIO storageIo = DataAccess.createNewStorageIO(dataset, "valid-tag"); + assertTrue(storageIo.getClass().equals(FileAccessIO.class)); + } + + /* + TODO: testing these branches requires the possibility to inject swift and s3 client mocks + @Test + void testCreateNewStorageIO_updatesStorageIdentifier() throws IOException { + StorageIO storageIo = DataAccess.createNewStorageIO(dataset, "valid-tag"); + DvObject obj = storageIo.getDvObject(); + assertEquals("valid-tag", obj.getStorageIdentifier()); + } + + @Test + void testCreateNewStorageIO_createsSwiftAccessIO() throws IOException { + StorageIO storageIo = DataAccess.createNewStorageIO(dataset, "valid-tag", "swift"); + assertTrue(storageIo.getClass().equals(SwiftAccessIO.class)); + } + + @Test + void testCreateNewStorageIO_createsS3AccessIO() throws IOException { + StorageIO storageIo = DataAccess.createNewStorageIO(dataset, "valid-tag", "s3"); + assertTrue(storageIo.getClass().equals(S3AccessIO.class)); + } */ +} diff --git a/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java b/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java deleted file mode 100644 index 9a59cfdea2a..00000000000 --- a/src/test/java/edu/harvard/iq/dataverse/passwordreset/PasswordResetInitResponseTest.java +++ /dev/null @@ -1,31 +0,0 @@ -package edu.harvard.iq.dataverse.passwordreset; - -import edu.harvard.iq.dataverse.authorization.providers.builtin.BuiltinAuthenticationProvider; -import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.GitHubOAuth2AP; -import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.GoogleOAuth2AP; -import edu.harvard.iq.dataverse.authorization.providers.oauth2.impl.OrcidOAuth2AP; -import edu.harvard.iq.dataverse.authorization.providers.shib.ShibAuthenticationProvider; - -import static org.junit.jupiter.api.Assertions.fail; - -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; - -import org.junit.jupiter.api.Test; - -public class PasswordResetInitResponseTest { - - private PasswordResetData passwordResetData = new PasswordResetData() { - @Override - public String getToken() { - return "abcd"; - } - }; - - @Test - public void testGeneratesValidResetUrl() { - // TODO: - fail(); - } -} From 403199796446be9f9b7e6a5e3a902a05f355c1ea Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Sat, 16 Mar 2019 21:08:01 +0100 Subject: [PATCH 09/11] Add unit tests for FileMetadata.getFileDateToDisplay #5634 --- .../iq/dataverse/FileMetadataTest.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/test/java/edu/harvard/iq/dataverse/FileMetadataTest.java diff --git a/src/test/java/edu/harvard/iq/dataverse/FileMetadataTest.java b/src/test/java/edu/harvard/iq/dataverse/FileMetadataTest.java new file mode 100644 index 00000000000..e9e9ea4a051 --- /dev/null +++ b/src/test/java/edu/harvard/iq/dataverse/FileMetadataTest.java @@ -0,0 +1,49 @@ +package edu.harvard.iq.dataverse; + +import java.sql.Timestamp; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class FileMetadataTest { + FileMetadata metadata; + + @BeforeEach + void setUp() { + metadata = new FileMetadata(); + } + + @Test + void testGetFileDateToDisplay_missingFile() { + assertEquals("", metadata.getFileDateToDisplay()); + } + + @Test + void testGetFileDateToDisplay_fileWithoutDates() { + DataFile datafile = new DataFile(); + datafile.setPublicationDate(null); + datafile.setCreateDate(null); + metadata.setDataFile(datafile); + assertEquals("", metadata.getFileDateToDisplay()); + } + + @Test + void testGetFileDateToDisplay_fileWithoutPublicationDate() { + DataFile datafile = new DataFile(); + datafile.setPublicationDate(null); + datafile.setCreateDate(Timestamp.valueOf("2019-01-01 00:00:00")); + metadata.setDataFile(datafile); + assertEquals("Jan 1, 2019", metadata.getFileDateToDisplay()); + } + + @Test + void testGetFileDateToDisplay_fileWithPublicationDate() { + DataFile datafile = new DataFile(); + datafile.setPublicationDate(Timestamp.valueOf("2019-02-02 00:00:00")); + datafile.setCreateDate(Timestamp.valueOf("2019-01-02 00:00:00")); + metadata.setDataFile(datafile); + assertEquals("Feb 2, 2019", metadata.getFileDateToDisplay()); + } +} From 7b0fe8265f086f4691187e80c5c24ba25b62f6c3 Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Mon, 18 Mar 2019 22:12:23 +0100 Subject: [PATCH 10/11] Remove unused variable and tests --- .../dataverse/dataaccess/DataAccessTest.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/src/test/java/edu/harvard/iq/dataverse/dataaccess/DataAccessTest.java b/src/test/java/edu/harvard/iq/dataverse/dataaccess/DataAccessTest.java index 48a92468f4f..a1a96a363ed 100644 --- a/src/test/java/edu/harvard/iq/dataverse/dataaccess/DataAccessTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/dataaccess/DataAccessTest.java @@ -12,7 +12,6 @@ public class DataAccessTest { DataFile datafile; Dataset dataset; - DataAccess dataAccess; @BeforeEach void setUp() { @@ -54,25 +53,4 @@ void testCreateNewStorageIO_createsFileAccessIObyDefault() throws IOException { StorageIO storageIo = DataAccess.createNewStorageIO(dataset, "valid-tag"); assertTrue(storageIo.getClass().equals(FileAccessIO.class)); } - - /* - TODO: testing these branches requires the possibility to inject swift and s3 client mocks - @Test - void testCreateNewStorageIO_updatesStorageIdentifier() throws IOException { - StorageIO storageIo = DataAccess.createNewStorageIO(dataset, "valid-tag"); - DvObject obj = storageIo.getDvObject(); - assertEquals("valid-tag", obj.getStorageIdentifier()); - } - - @Test - void testCreateNewStorageIO_createsSwiftAccessIO() throws IOException { - StorageIO storageIo = DataAccess.createNewStorageIO(dataset, "valid-tag", "swift"); - assertTrue(storageIo.getClass().equals(SwiftAccessIO.class)); - } - - @Test - void testCreateNewStorageIO_createsS3AccessIO() throws IOException { - StorageIO storageIo = DataAccess.createNewStorageIO(dataset, "valid-tag", "s3"); - assertTrue(storageIo.getClass().equals(S3AccessIO.class)); - } */ } From 15e209ce80e7fd0f05df1ac9b41114912807672f Mon Sep 17 00:00:00 2001 From: Roland Schlaefli Date: Mon, 18 Mar 2019 22:13:05 +0100 Subject: [PATCH 11/11] Move overrides directly into test cases, add parametrized test for unbounded case --- .../datasetutility/FileSizeCheckerTest.java | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java b/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java index e1731f02df7..e562e3f9e0c 100644 --- a/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java +++ b/src/test/java/edu/harvard/iq/dataverse/datasetutility/FileSizeCheckerTest.java @@ -15,7 +15,6 @@ import java.util.Arrays; import java.util.List; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -30,21 +29,6 @@ * @author oscardssmith */ public class FileSizeCheckerTest { - - private FileSizeChecker fileSizeChecker; - - @BeforeEach - public void setUp() { - // initialize a system config and instantiate a file size checker - // override the max file upload side to allow for testing - fileSizeChecker = new FileSizeChecker(new SystemConfig() { - @Override - public Long getMaxFileUploadSize() { - return 1000L; - } - }); - } - @Test public void testBytesToHumanReadable() { long[] sizes = {1L, 1023L, 1986L, 125707L, 2759516000L, 12039650000000L}; @@ -63,6 +47,12 @@ public void testBytesToHumanReadable() { @Test public void testIsAllowedFileSize_throwsOnNull() { + FileSizeChecker fileSizeChecker = new FileSizeChecker(new SystemConfig() { + @Override + public Long getMaxFileUploadSize() { + return 1000L; + } + }); assertThrows(NullPointerException.class, () -> { fileSizeChecker.isAllowedFileSize(null); }); @@ -71,6 +61,14 @@ public void testIsAllowedFileSize_throwsOnNull() { @ParameterizedTest @ValueSource(longs = { 0L, 999L, 1000L }) public void testIsAllowedFileSize_allowsSmallerOrEqualFileSize(Long fileSize) { + // initialize a system config and instantiate a file size checker + // override the max file upload side to allow for testing + FileSizeChecker fileSizeChecker = new FileSizeChecker(new SystemConfig() { + @Override + public Long getMaxFileUploadSize() { + return 1000L; + } + }); FileSizeResponse response = fileSizeChecker.isAllowedFileSize(fileSize); assertTrue(response.fileSizeOK); } @@ -78,12 +76,21 @@ public void testIsAllowedFileSize_allowsSmallerOrEqualFileSize(Long fileSize) { @ParameterizedTest @ValueSource(longs = { 1001L, Long.MAX_VALUE }) public void testIsAllowedFileSize_rejectsBiggerFileSize(Long fileSize) { + // initialize a system config and instantiate a file size checker + // override the max file upload side to allow for testing + FileSizeChecker fileSizeChecker = new FileSizeChecker(new SystemConfig() { + @Override + public Long getMaxFileUploadSize() { + return 1000L; + } + }); FileSizeResponse response = fileSizeChecker.isAllowedFileSize(fileSize); assertFalse(response.fileSizeOK); } - @Test - public void testIsAllowedFileSize_allowsOnUnboundedFileSize() { + @ParameterizedTest + @ValueSource(longs = { 0L, 1000L, Long.MAX_VALUE }) + public void testIsAllowedFileSize_allowsOnUnboundedFileSize(Long fileSize) { // initialize a system config and instantiate a file size checker // ensure that a max filesize is not set FileSizeChecker unboundedFileSizeChecker = new FileSizeChecker(new SystemConfig() { @@ -92,7 +99,7 @@ public Long getMaxFileUploadSize() { return null; } }); - FileSizeResponse response = unboundedFileSizeChecker.isAllowedFileSize(1000L); + FileSizeResponse response = unboundedFileSizeChecker.isAllowedFileSize(fileSize); assertTrue(response.fileSizeOK); } }