package de.codescape.jira.plugins.scrumpoker.rest;

import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.user.ApplicationUser;
import de.codescape.jira.plugins.scrumpoker.data.ScrumPokerStorage;
import de.codescape.jira.plugins.scrumpoker.model.ScrumPokerSession;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;

import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.*;

public class ScrumPokerSessionResourceTest {

    private static final String ISSUE_KEY = "ISSUE-1";
    private static final String USER_KEY = "userKey";
    private static final String CARD_VALUE = "5";

    @Rule
    public MockitoRule rule = MockitoJUnit.rule();

    @Mock
    private JiraAuthenticationContext jiraAuthenticationContext;

    @Mock
    private ScrumPokerStorage scrumPokerStorage;

    @InjectMocks
    private ScrumPokerSessionResource scrumPokerSessionResource;

    @Mock
    private ApplicationUser applicationUser;

    @Mock
    private ScrumPokerSession scrumPokerSession;

    @Test
    public void getSessionShouldReturnSessionForTheGivenIssueKey() {
        expectCurrentUserIs(USER_KEY);
        expectCurrentSessionForUser(ISSUE_KEY, USER_KEY);

        ScrumPokerSessionRest scrumPokerSessionRest = (ScrumPokerSessionRest) scrumPokerSessionResource.getSession(ISSUE_KEY).getEntity();

        assertThat(scrumPokerSessionRest.getIssueKey(), is(equalTo(ISSUE_KEY)));
    }

    @Test
    public void updateCardShouldUpdateTheCardForTheCurrentUser() {
        expectCurrentUserIs(USER_KEY);
        expectCurrentSessionForUser(ISSUE_KEY, USER_KEY);

        scrumPokerSessionResource.updateCard(ISSUE_KEY, CARD_VALUE);

        verify(scrumPokerSession).updateCard(USER_KEY, CARD_VALUE);
    }

    @Test
    public void revealingCardShouldRevealCardsForUnderlyingSession() {
        expectCurrentUserIs(USER_KEY);
        expectCurrentSessionForUser(ISSUE_KEY, USER_KEY);

        scrumPokerSessionResource.revealCards(ISSUE_KEY);

        verify(scrumPokerSession, times(1)).revealDeck();
    }

    @Test
    public void resettingSessionShouldResetTheUnderlyingSession() {
        expectCurrentUserIs(USER_KEY);
        expectCurrentSessionForUser(ISSUE_KEY, USER_KEY);

        scrumPokerSessionResource.resetSession(ISSUE_KEY);

        verify(scrumPokerSession, times(1)).resetDeck();
    }

    private void expectCurrentUserIs(String userKey) {
        when(jiraAuthenticationContext.getLoggedInUser()).thenReturn(applicationUser);
        when(applicationUser.getKey()).thenReturn(userKey);
    }

    private void expectCurrentSessionForUser(String issueKey, String userKey) {
        when(scrumPokerStorage.sessionForIssue(issueKey, userKey)).thenReturn(scrumPokerSession);
        when(scrumPokerSession.getIssueKey()).thenReturn(issueKey);
    }

}

@@ -223,16 +223,89 @@
 torage;%0A
+import de.codescape.jira.plugins.scrumpoker.data.StoryPointFieldSupport;%0A
 import d
@@ -916,16 +916,61 @@
 E = %225%22;
+%0A    private static final int ESTIMATION = 5;
 %0A%0A    @R
@@ -1157,24 +1157,94 @@
 erStorage;%0A%0A
+    @Mock%0A    private StoryPointFieldSupport storyPointFieldSupport;%0A%0A
     @InjectM
@@ -2750,32 +2750,470 @@
 tDeck();%0A    %7D%0A%0A
+    @Test%0A    public void confirmingEstimationShouldConfirmEstimationInUnderlyingSessionAndPersistEstimation() %7B%0A        expectCurrentUserIs(USER_KEY);%0A        expectCurrentSessionForUser(ISSUE_KEY, USER_KEY);%0A%0A        scrumPokerSessionResource.confirmEstimation(ISSUE_KEY, ESTIMATION);%0A%0A        verify(scrumPokerSession, times(1)).confirm(ESTIMATION);%0A        verify(storyPointFieldSupport, times(1)).save(ISSUE_KEY, ESTIMATION);%0A    %7D%0A%0A
     private void

add tests for REST endpoint