package uk.ac.ox.zoo.seeg.abraid.mp.modelwrapper.configuration;

import org.apache.commons.lang.RandomStringUtils;
import org.joda.time.DateTimeUtils;
import org.junit.Test;

import static org.fest.assertions.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
 * Tests for RunConfigurationFactoryImpl.
 * Copyright (c) 2014 University of Oxford
 */
public class RunConfigurationFactoryTest {
    @Test
    public void createDefaultConfigurationUsesCorrectDefaults() throws Exception {
        // Arrange
        ConfigurationService configurationService = mock(ConfigurationService.class);
        when(configurationService.getCacheDirectory()).thenReturn("expectation1");
        when(configurationService.getModelRepositoryVersion()).thenReturn("expectation2");
        when(configurationService.getMaxModelRunDuration()).thenReturn(12345);
        when(configurationService.getRExecutablePath()).thenReturn("");
        RunConfigurationFactory target = new RunConfigurationFactoryImpl(configurationService);
        DateTimeUtils.setCurrentMillisFixed(0);

        // Act
        RunConfiguration result = target.createDefaultConfiguration("foo");

        // Assert
        assertThat(result.getRunName()).isEqualTo("foo_1970-01-01-01-00-00");
        assertThat(result.getBaseDir().getName()).isEqualTo("expectation1");
        assertThat(result.getMaxRuntime()).isEqualTo(12345);
        assertThat(result.getModelVersion()).isEqualTo("expectation2");
    }

    @Test
    public void createDefaultConfigurationHandlesLongNames() throws Exception {
        // Arrange
        ConfigurationService configurationService = mock(ConfigurationService.class);
        when(configurationService.getCacheDirectory()).thenReturn("");
        when(configurationService.getModelRepositoryVersion()).thenReturn("");
        when(configurationService.getMaxModelRunDuration()).thenReturn(0);
        when(configurationService.getRExecutablePath()).thenReturn("");
        RunConfigurationFactory target = new RunConfigurationFactoryImpl(configurationService);
        DateTimeUtils.setCurrentMillisFixed(0);

        // Act
        String longName = RandomStringUtils.randomAlphanumeric(300);
        RunConfiguration result = target.createDefaultConfiguration(longName);

        // Assert
        assertThat(result.getRunName()).isEqualTo(longName.substring(0, 195) + "_1970-01-01-01-00-00");
    }
}

@@ -140,24 +140,60 @@
 eTimeUtils;%0A
+import org.joda.time.LocalDateTime;%0A
 import org.j
@@ -203,16 +203,16 @@
 t.Test;%0A
-
 %0Aimport 
@@ -1315,36 +1315,71 @@
 To(%22foo_
-1970-01-01-01-00-00%22
+%22 + LocalDateTime.now().toString(%22yyyy-MM-dd-HH-mm-ss%22)
 );%0A     
@@ -2459,16 +2459,33 @@
 EqualTo(
+%0A                
 longName
@@ -2511,28 +2511,63 @@
 + %22_
-1970-01-01-01-00-00%22
+%22 + LocalDateTime.now().toString(%22yyyy-MM-dd-HH-mm-ss%22)
 );%0A 

Make test not timezone iffy.