001package edu.pdx.cs.joy.grader.poa;
002
003import com.google.common.eventbus.Subscribe;
004import org.junit.jupiter.api.BeforeEach;
005import org.junit.jupiter.api.Test;
006import org.mockito.ArgumentCaptor;
007
008import java.io.IOException;
009import java.time.LocalDateTime;
010
011import static org.hamcrest.MatcherAssert.assertThat;
012import static org.hamcrest.Matchers.equalTo;
013import static org.mockito.Mockito.*;
014
015public class POASubmissionPresenterTest extends POASubmissionTestCase {
016
017  private POASubmissionView view;
018
019  @BeforeEach
020  @Override
021  public void setUp() {
022    super.setUp();
023
024    this.view = mock(POASubmissionView.class);
025    new POASubmissionPresenter(this.bus, this.view);
026  }
027
028  @Test
029  public void submissionInformationDisplayedOnSubmissionSelectionEvent() throws POASubmissionView.CouldNotParseContent {
030    String subject = "Subject";
031    String submitter = "Submitter";
032    LocalDateTime submitTime = LocalDateTime.now();
033    String content = "Test Content";
034    String contentType = "text/plain";
035    POASubmission submission = createPOASubmission(subject, submitter, submitTime, content, contentType);
036
037    this.bus.post(new POASubmissionSelected(submission));
038
039    verify(view).setSubmissionSubject(subject);
040    verify(view).setSubmissionSubmitter(submitter);
041    verify(view).setSubmissionTime(POASubmissionPresenter.formatSubmissionTime(submitTime));
042    verify(view).setContent(content, POASubmissionView.POAContentType.TEXT);
043  }
044
045  @Test
046  public void whenContentContentThrowsExceptionAnUnhandledExceptionEventIsPosted() throws POASubmissionView.CouldNotParseContent {
047    this.unhandledExceptionHandler = this::doNotFailTestWhenUnhandledExceptionEncountered;
048
049    IOException exception = new IOException();
050    POASubmissionView.CouldNotParseContent couldNotParse = new POASubmissionView.CouldNotParseContent(exception);
051    doThrow(couldNotParse).when(this.view).setContent(anyString(), eq(POASubmissionView.POAContentType.HTML));
052
053    UnhandledExceptionEventHandler handler = mock(UnhandledExceptionEventHandler.class);
054    this.bus.register(handler);
055
056    String subject = "Subject";
057    String submitter = "Submitter";
058    LocalDateTime submitTime = LocalDateTime.now();
059    String content = "Test Content";
060    String contentType = "text/html";
061    POASubmission submission = createPOASubmission(subject, submitter, submitTime, content, contentType);
062
063    this.bus.post(new POASubmissionSelected(submission));
064
065    verify(view).setContent(content, POASubmissionView.POAContentType.HTML);
066
067    ArgumentCaptor<UnhandledExceptionEvent> captor = ArgumentCaptor.forClass(UnhandledExceptionEvent.class);
068    verify(handler).handleUnhandledExceptionEvent(captor.capture());
069
070    assertThat(captor.getValue().getUnhandledException(), equalTo(couldNotParse));
071    assertThat(captor.getValue().getUnhandledException().getCause(), equalTo(exception));
072  }
073
074  interface UnhandledExceptionEventHandler {
075    @Subscribe
076    void handleUnhandledExceptionEvent(UnhandledExceptionEvent event);
077  }
078
079}