package net.congstar.jira.plugins.scrumpoker.data;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.junit.Assert.assertThat;
import net.congstar.jira.plugins.scrumpoker.model.ScrumPokerSession;

import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class DefaultPlanningPokerStorageTest {

    private static final String SOME_ISSUE = "Issue-1";

    private static final String YOUNG_ISSUE = "Young Issue";

    private static final String OLD_ISSUE = "Old Issue";

    private DefaultPlanningPokerStorage storage;

    @Before
    public void before() {
        storage = new DefaultPlanningPokerStorage();
    }

    @After
    public void after() {
        DateTimeUtils.setCurrentMillisSystem();
    }

    @Test
    public void shouldProduceNewSessionIfNonExists() {
        assertThat(storage.sessionForIssue(SOME_ISSUE), is(not(nullValue())));
    }

    @Test
    public void shouldReturnExistingSessionIfExists() {
        ScrumPokerSession sessionForIssue = storage.sessionForIssue(SOME_ISSUE);
        assertThat(storage.sessionForIssue(SOME_ISSUE), is(equalTo(sessionForIssue)));
    }

    @Test
    public void shoudlRemoveSessionsOlderThanOneDay() {
        createOldScrumPokerSession(OLD_ISSUE, DateTime.now().minusDays(2).getMillis());
        assertThat(storage.sessionForIssue(OLD_ISSUE).getStartedOn(), is(equalTo(DateTime.now())));
    }

    @Test
    public void shoudlKeepSessionsYoungerThanOneDay() {
        DateTime youngEnough = DateTime.now().minusHours(14);
        createOldScrumPokerSession(YOUNG_ISSUE, youngEnough.getMillis());
        assertThat(storage.sessionForIssue(YOUNG_ISSUE).getStartedOn(), is(equalTo(youngEnough)));
    }

    private void createOldScrumPokerSession(String issueKey, long creationDate) {
        DateTimeUtils.setCurrentMillisFixed(creationDate);
        storage.sessionForIssue(issueKey);
        DateTimeUtils.setCurrentMillisSystem();
    }

}
@@ -535,16 +535,181 @@
 Test %7B%0A%0A
+    private static final DateTime TWO_DAYS_AGO = DateTime.now().minusDays(2);%0A    private static final DateTime FOURTEEN_HOURS_AGO = DateTime.now().minusHours(14);%0A%0A
     priv
@@ -756,17 +756,16 @@
 sue-1%22;%0A
-%0A
     priv
@@ -809,33 +809,32 @@
  %22Young Issue%22;%0A
-%0A
     private stat
@@ -1538,18 +1538,18 @@
 oid shou
-d
 l
+d
 RemoveSe
@@ -1580,35 +1580,32 @@
 %7B%0A        create
-Old
 ScrumPokerSessio
@@ -1609,47 +1609,46 @@
 sion
-(OLD_ISSUE, DateTime.now().minusDays(2)
+WithCreationOn(OLD_ISSUE, TWO_DAYS_AGO
 .get
@@ -1799,10 +1799,10 @@
 shou
-d
 l
+d
 Keep
@@ -1835,70 +1835,8 @@
 ) %7B%0A
-        DateTime youngEnough = DateTime.now().minusHours(14);%0A
     
@@ -1841,27 +1841,24 @@
       create
-Old
 ScrumPokerSe
@@ -1866,33 +1866,54 @@
 sion
-(YOUNG_ISSUE, youngEnough
+WithCreationOn(YOUNG_ISSUE, FOURTEEN_HOURS_AGO
 .get
@@ -2010,19 +2010,26 @@
 lTo(
-youngEnough
+FOURTEEN_HOURS_AGO
 )));
@@ -2059,19 +2059,16 @@
 d create
-Old
 ScrumPok
@@ -2076,16 +2076,30 @@
 rSession
+WithCreationOn
 (String 

fix typos and improve readability