Skip to content

Commit

Permalink
adding unit tests for RestrictFileCommand #3776
Browse files Browse the repository at this point in the history
  • Loading branch information
ferrys committed Jun 13, 2017
1 parent c115364 commit 8c8e3b0
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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);

}

}

0 comments on commit 8c8e3b0

Please sign in to comment.