-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding unit tests for RestrictFileCommand #3776
- Loading branch information
Showing
2 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
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
139 changes: 139 additions & 0 deletions
139
src/test/java/edu/harvard/iq/dataverse/engine/command/impl/RestrictFileCommandTest.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,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); | ||
|
||
} | ||
|
||
} |