|
1 | 1 | package fi.helsinki.cs.tmc.cli.tmcstuff; |
2 | 2 |
|
| 3 | +import static com.google.common.truth.Truth.assertThat; |
| 4 | +import static org.mockito.Matchers.any; |
| 5 | +import static org.mockito.Matchers.anyBoolean; |
| 6 | +import static org.mockito.Matchers.anyString; |
| 7 | +import static org.mockito.Mockito.mock; |
| 8 | +import static org.powermock.api.mockito.PowerMockito.mockStatic; |
| 9 | +import static org.powermock.api.mockito.PowerMockito.verifyStatic; |
| 10 | +import static org.powermock.api.mockito.PowerMockito.when; |
| 11 | + |
| 12 | +import fi.helsinki.cs.tmc.cli.CliContext; |
3 | 13 | import fi.helsinki.cs.tmc.cli.io.ExternalsUtil; |
| 14 | +import fi.helsinki.cs.tmc.cli.io.TestIo; |
| 15 | +import fi.helsinki.cs.tmc.core.domain.submission.FeedbackAnswer; |
| 16 | +import fi.helsinki.cs.tmc.core.domain.submission.FeedbackQuestion; |
4 | 17 |
|
5 | | -import org.junit.Ignore; |
| 18 | +import org.junit.Before; |
6 | 19 | import org.junit.Test; |
7 | 20 | import org.junit.runner.RunWith; |
| 21 | +import org.mockito.ArgumentCaptor; |
| 22 | +import org.mockito.Captor; |
| 23 | +import org.mockito.Mockito; |
| 24 | +import org.powermock.api.mockito.PowerMockito; |
8 | 25 | import org.powermock.core.classloader.annotations.PrepareForTest; |
9 | 26 | import org.powermock.modules.junit4.PowerMockRunner; |
10 | 27 |
|
| 28 | +import java.net.URI; |
| 29 | +import java.util.Arrays; |
| 30 | +import java.util.List; |
| 31 | + |
11 | 32 | @RunWith(PowerMockRunner.class) |
12 | | -@PrepareForTest(ExternalsUtil.class) |
| 33 | +@PrepareForTest({ExternalsUtil.class, TmcUtil.class}) |
13 | 34 | public class FeedbackHandlerTest { |
| 35 | + private List<FeedbackQuestion> questions; |
| 36 | + private String a1; |
| 37 | + private String a2; |
| 38 | + private String a3; |
| 39 | + private TestIo io; |
| 40 | + private CliContext ctx; |
| 41 | + @Captor ArgumentCaptor<List<FeedbackAnswer>> answerCaptor; |
| 42 | + |
| 43 | + @Before |
| 44 | + public void setup() { |
| 45 | + a1 = a2 = a3 = ""; |
| 46 | + mockStatic(ExternalsUtil.class); |
| 47 | + mockStatic(TmcUtil.class); |
| 48 | + when(ExternalsUtil.getUserEditedMessage(anyString(), anyString(), anyBoolean())) |
| 49 | + .thenReturn("you're programm sucks"); |
| 50 | + FeedbackQuestion q1 = new FeedbackQuestion(); |
| 51 | + q1.setQuestion("What's your opinion on TMC-CLI?"); |
| 52 | + q1.setKind("text"); |
| 53 | + FeedbackQuestion q2 = new FeedbackQuestion(); |
| 54 | + q2.setQuestion("Please rate this program."); |
| 55 | + q2.setKind("intrange\\[0\\.\\.4]"); |
| 56 | + FeedbackQuestion q3 = new FeedbackQuestion(); |
| 57 | + q3.setQuestion("This type of question doesn't really exist but whatever"); |
| 58 | + q3.setKind("misc"); |
| 59 | + questions = Arrays.asList(q1, q2, q3); |
| 60 | + io = new TestIo(); |
| 61 | + ctx = mock(CliContext.class); |
| 62 | + Mockito.when(ctx.getIo()).thenReturn(io); |
| 63 | + } |
14 | 64 |
|
15 | | - @Ignore |
16 | 65 | @Test |
17 | | - public void todo() { |
| 66 | + public void sendingFeedbackWorks() { |
| 67 | + PowerMockito.when( |
| 68 | + TmcUtil.sendFeedback(any(CliContext.class), any(List.class), any(URI.class))) |
| 69 | + .thenReturn(true); |
| 70 | + io.addLinePrompt("1"); |
| 71 | + io.addLinePrompt("who cars"); |
| 72 | + io.addConfirmationPrompt(true); |
| 73 | + FeedbackHandler handler = new FeedbackHandler(ctx); |
| 74 | + handler.sendFeedback(questions, URI.create("https://eeeeeeeeeee.com")); |
| 75 | + |
| 76 | + verifyStatic(); |
| 77 | + TmcUtil.sendFeedback(any(CliContext.class), answerCaptor.capture(), any(URI.class)); |
| 78 | + List<FeedbackAnswer> answers = answerCaptor.getValue(); |
| 79 | + assertThat(answers.get(0).getQuestion().getQuestion()).isEqualTo( |
| 80 | + "What's your opinion on TMC-CLI?"); |
| 81 | + assertThat(answers.get(0).getAnswer()).isEqualTo("you're programm sucks"); |
| 82 | + io.assertContains("Please rate this program."); |
| 83 | + assertThat(answers.get(1).getQuestion().getQuestion()).isEqualTo( |
| 84 | + "Please rate this program."); |
| 85 | + assertThat(answers.get(1).getAnswer()).isEqualTo("1"); |
| 86 | + io.assertContains( |
| 87 | + "Feedback question: This type of question doesn't really exist but whatever"); |
| 88 | + assertThat(answers.get(2).getQuestion().getQuestion()).isEqualTo( |
| 89 | + "This type of question doesn't really exist but whatever"); |
| 90 | + assertThat(answers.get(2).getAnswer()).isEqualTo("who cars"); |
18 | 91 | } |
19 | 92 | } |
0 commit comments