package guitests;

import java.io.IOException;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.junit.Test;

import seedu.taskell.TestApp;
import seedu.taskell.commons.core.Config;
import seedu.taskell.commons.core.LogsCenter;
import seedu.taskell.commons.exceptions.DataConversionException;
import seedu.taskell.logic.commands.SaveStorageLocationCommand;
import seedu.taskell.storage.JsonConfigStorage;

public class SaveStorageLocationCommandTest extends TaskManagerGuiTest {
    
    private static final Logger logger = LogsCenter.getLogger(SaveStorageLocationCommandTest.class);
    
    private static final String CONFIG_JSON = "config.json";
    private static final String CONFIG_LOCATION = "./src/test/data/SaveLocationCommandTest";
    
    private static final String DEFAULT_SAVE_LOCATION = TestApp.SAVE_LOCATION_FOR_TESTING;
    
    @Test
    public void saveToValidFilePath() throws DataConversionException {
        commandBox.runCommand("save ./src/test/data/SaveLocationCommandTest/newStorageLocation/");
        assertWriteToJsonSuccess();
    }
    
    @Test
    public void saveToInvalidFilePath() throws DataConversionException {
        JsonConfigStorage jsonConfigStorage = new JsonConfigStorage(CONFIG_LOCATION);

        commandBox.runCommand("save E:");   
        
        Optional<Config> newConfig = jsonConfigStorage.readConfig(CONFIG_JSON);
        
        String newFilePath = newConfig.get().getTaskManagerFilePath();

        logger.severe("New path: " + newFilePath);
        
        assert(newFilePath.equals(DEFAULT_SAVE_LOCATION));
    }
    
    /** NOTE: because of the way SaveStorageLocationCommand works, after running this command
     *          config.json in Taskell saves the test data so this method is necessary to reset
     *          config.json to default data
     * */
    @Test
    public void resetConfigFile() throws IOException {
        Config config = new Config();
        config.setAppTitle("Taskell");
        config.setLogLevel(Level.INFO);
        config.setUserPrefsFilePath("preferences.json");
        config.setTaskManagerFilePath("data/taskmanager.xml");
        config.setTaskManagerName("MyTaskManager");
        SaveStorageLocationCommand.setConfig(config);
        
        JsonConfigStorage jsonConfigStorage = new JsonConfigStorage(CONFIG_JSON);
        jsonConfigStorage.saveConfigFile(config);
    }
    
    private void assertWriteToJsonSuccess() throws DataConversionException {
        JsonConfigStorage jsonConfigStorage = new JsonConfigStorage(CONFIG_LOCATION);
        Optional<Config> config = jsonConfigStorage.readConfig(CONFIG_JSON);
        assert(config.isPresent());
    } 
    

}

@@ -23,27 +23,94 @@
 ort 
-java.io.IOException
+static org.junit.Assert.assertTrue;%0A%0Aimport java.io.IOException;%0Aimport java.util.List
 ;%0Aim
@@ -496,33 +496,272 @@
 ell.
-storage.JsonConfigStorage
+model.ReadOnlyTaskManager;%0Aimport seedu.taskell.model.TaskManager;%0Aimport seedu.taskell.model.task.UniqueTaskList.DuplicateTaskException;%0Aimport seedu.taskell.storage.JsonConfigStorage;%0Aimport seedu.taskell.testutil.TestTask;%0Aimport seedu.taskell.testutil.TestUtil
 ;%0A%0Ap
@@ -1193,20 +1193,16 @@
 ESTING;%0A
-    
 %0A    @Te
@@ -1276,47 +1276,79 @@
 tion
- %7B%0A        commandBox.runCommand(%22save 
+, IOException, DuplicateTaskException %7B%0A        String testFilePath = %22
 ./sr
@@ -1403,16 +1403,70 @@
 cation/%22
+;%0A        commandBox.runCommand(%22save %22 + testFilePath
 );%0A     
@@ -1492,24 +1492,59 @@
 nSuccess();%0A
+        assertWriteToXmlSuccess();%0A
     %7D%0A    %0A 
@@ -1846,25 +1846,16 @@
 _JSON);%0A
-        %0A
         
@@ -1917,17 +1917,16 @@
 Path();%0A
-%0A
         
@@ -1936,14 +1936,12 @@
 ger.
-severe
+info
 (%22Ne
@@ -3136,12 +3136,186 @@
  %7D %0A    
+%0A    private void assertWriteToXmlSuccess() %7B%0A        TestTask%5B%5D currentList = td.getTypicalTasks();%0A        assertTrue(taskListPanel.isListMatching(currentList));%0A    %7D%0A    
 %0A%0A%7D%0A

edit test