/*
 * 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;
    static 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);
        
    }
    
    @Test 
    public void testPublicInstall() throws CommandException {
        file.setOwner(dataset);
        String expected = "Restricting files is not permitted on a public installation.";
        String actual = null;
        publicInstall = true;

        RestrictFileCommand cmd = new RestrictFileCommand(file, makeRequest(), restrict);
        try {
            engine.submit(cmd);
        } catch(Exception ex){
            actual = ex.getMessage();
        }
        assertEquals(expected, actual);
    }
    
}

@@ -315,112 +315,11 @@
 Data
-FileServiceBean;%0Aimport edu.harvard.iq.dataverse.Dataset;%0Aimport edu.harvard.iq.dataverse.DatasetVersion
+set
 ;%0Aim
@@ -560,60 +560,8 @@
 on;%0A
-import edu.harvard.iq.dataverse.mocks.MocksFactory;%0A
 impo
@@ -832,16 +832,16 @@
 ceBean;%0A
+
 import j
@@ -863,76 +863,8 @@
 mp;%0A
-import java.text.ParseException;%0Aimport java.text.SimpleDateFormat;%0A
 impo
@@ -3982,33 +3982,41 @@
 %0A        %7D catch
-(
+ (Command
 Exception ex) %7B%0A
@@ -4539,17 +4539,25 @@
  %7D catch
-(
+ (Command
 Exceptio
@@ -4561,16 +4561,17 @@
 tion ex)
+ 
 %7B%0A      
@@ -4648,24 +4648,55 @@
 d, actual);%0A
+        publicInstall = false;%0A
     %7D%0A    %0A%7D

fix broken test (I think, the non-determinism makes it a little hard to tell)