diff --git a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/RestrictFileCommand.java b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/RestrictFileCommand.java index d03d06eeab5..40b1ece353b 100644 --- a/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/RestrictFileCommand.java +++ b/src/main/java/edu/harvard/iq/dataverse/engine/command/impl/RestrictFileCommand.java @@ -30,7 +30,7 @@ @RequiredPermissions(Permission.EditDataset) public class RestrictFileCommand extends AbstractVoidCommand { - private static final Logger logger = Logger.getLogger(RestrictFileCommand.class.getCanonicalName()); + //private static final Logger logger = Logger.getLogger(RestrictFileCommand.class.getCanonicalName()); private final DataFile file; private final boolean restrict; diff --git a/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/RestrictFileCommandTest.java b/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/RestrictFileCommandTest.java new file mode 100644 index 00000000000..ba81f0db0a7 --- /dev/null +++ b/src/test/java/edu/harvard/iq/dataverse/engine/command/impl/RestrictFileCommandTest.java @@ -0,0 +1,139 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package edu.harvard.iq.dataverse.engine.command.impl; + +import edu.harvard.iq.dataverse.DataFile; +import edu.harvard.iq.dataverse.DataFileServiceBean; +import edu.harvard.iq.dataverse.Dataset; +import edu.harvard.iq.dataverse.DatasetVersion; +import edu.harvard.iq.dataverse.FileMetadata; +import edu.harvard.iq.dataverse.engine.TestCommandContext; +import edu.harvard.iq.dataverse.engine.TestDataverseEngine; +import edu.harvard.iq.dataverse.engine.command.exception.CommandException; +import edu.harvard.iq.dataverse.mocks.MocksFactory; +import static edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataFile; +import static edu.harvard.iq.dataverse.mocks.MocksFactory.makeDataset; +import static edu.harvard.iq.dataverse.mocks.MocksFactory.makeRequest; +import edu.harvard.iq.dataverse.settings.SettingsServiceBean; +import java.sql.Timestamp; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; +import org.junit.After; +import org.junit.AfterClass; +import static org.junit.Assert.assertEquals; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import static org.junit.Assert.assertTrue; + + +/** + * + * @author sarahferry + */ +public class RestrictFileCommandTest { + + TestDataverseEngine engine; + private DataFile file; + private Dataset dataset; + boolean restrict = true; + boolean publicInstall = false; + + + public RestrictFileCommandTest() { + } + + @BeforeClass + public static void setUpClass() { + } + + @AfterClass + public static void tearDownClass() { + } + + @Before + public void setUp() { + dataset = makeDataset(); + file = makeDataFile(); + + engine = new TestDataverseEngine(new TestCommandContext(){ + + @Override + public SettingsServiceBean settings(){ + return new SettingsServiceBean(){ + //override for a public install, + //assume false + @Override + public boolean isTrueForKey(SettingsServiceBean.Key key, boolean defaultValue) { + return publicInstall; + } + }; + } + }); + + } + + @After + public void tearDown() { + } + + @Test + public void testRestrictUnpublishedFile() throws CommandException{ + file.setOwner(dataset); + RestrictFileCommand cmd = new RestrictFileCommand(file, makeRequest(), restrict); + engine.submit(cmd); + + assertTrue(file.isRestricted()); + assertTrue(file.getFileMetadata().isRestricted()); + + } + + @Test + public void testRestrictPublishedFile() throws Exception{ + file.setOwner(dataset); + dataset.setPublicationDate(new Timestamp(new Date().getTime())); + RestrictFileCommand cmd = new RestrictFileCommand(file, makeRequest(), restrict); + engine.submit(cmd); + + //asserts + assertTrue(!file.isRestricted()); + for (FileMetadata fmw : dataset.getEditVersion().getFileMetadatas()) { + if (file.equals(fmw.getDataFile())) { + assertEquals(fmw, file.getFileMetadata()); + assertTrue(fmw.isRestricted()); + } + } + } + + + @Test + public void testRestrictNewFile() throws Exception { + RestrictFileCommand cmd = new RestrictFileCommand(file, makeRequest(), restrict); + engine.submit(cmd); + assertTrue(file.isRestricted()); + assertTrue(file.getFileMetadata().isRestricted()); + } + + @Test + public void testRestrictRestrictedFile() throws Exception { + file.setOwner(dataset); + String expected = "File " + file.getDisplayName() + " is already restricted"; + String actual = null; + file.setRestricted(true); + file.getFileMetadata().setRestricted(restrict); + RestrictFileCommand cmd = new RestrictFileCommand(file, makeRequest(), restrict); + try { + engine.submit(cmd); + } catch(Exception ex) { + actual = ex.getMessage(); + } + + assertEquals(expected, actual); + + } + +}