|
1 | 1 | package io.quarkiverse.githubapp.it.testingframework; |
2 | 2 |
|
| 3 | +import static io.quarkiverse.githubapp.testing.GitHubAppMockito.mockBuilder; |
3 | 4 | import static io.quarkiverse.githubapp.testing.GitHubAppMockito.mockPagedIterable; |
4 | 5 | import static io.quarkiverse.githubapp.testing.GitHubAppTesting.given; |
5 | 6 | import static io.quarkiverse.githubapp.testing.GitHubAppTesting.when; |
|
21 | 22 | import org.kohsuke.github.GHApp; |
22 | 23 | import org.kohsuke.github.GHAppInstallation; |
23 | 24 | import org.kohsuke.github.GHEvent; |
| 25 | +import org.kohsuke.github.GHIssueComment; |
| 26 | +import org.kohsuke.github.GHIssueCommentQueryBuilder; |
24 | 27 | import org.kohsuke.github.GHPullRequest; |
25 | 28 | import org.kohsuke.github.GHRepository; |
26 | 29 | import org.kohsuke.github.GitHub; |
@@ -56,36 +59,111 @@ void ghObjectMocking() { |
56 | 59 | Mockito.when(mocks.issue(750705278).getBody()).thenReturn("someValue"); |
57 | 60 | }) |
58 | 61 | .when().payloadFromClasspath("/issue-opened.json") |
59 | | - .event(GHEvent.ISSUES) |
60 | | - .then().github(mocks -> { |
61 | | - })) |
| 62 | + .event(GHEvent.ISSUES)) |
62 | 63 | .doesNotThrowAnyException(); |
63 | 64 | assertThat(capture[0]).isEqualTo("someValue"); |
64 | 65 | } |
65 | 66 |
|
66 | 67 | @Test |
67 | 68 | void ghObjectVerify() { |
68 | | - ThrowingCallable assertion = () -> when().payloadFromClasspath("/issue-opened.json") |
| 69 | + // Do not change this, the documentation includes the exact same code |
| 70 | + ThrowingCallable assertion = () -> when() |
| 71 | + .payloadFromClasspath("/issue-opened.json") |
69 | 72 | .event(GHEvent.ISSUES) |
70 | | - .then().github(mocks -> { |
71 | | - verify(mocks.issue(750705278)) |
72 | | - .addLabels("someValue"); |
| 73 | + .then().github(mocks -> { // <4> |
| 74 | + Mockito.verify(mocks.issue(750705278)) |
| 75 | + .comment("Hello from my GitHub App"); |
73 | 76 | }); |
74 | 77 |
|
75 | 78 | // Success |
76 | 79 | IssueEventListener.behavior = (payload, configFile) -> { |
77 | | - payload.getIssue().addLabels("someValue"); |
| 80 | + payload.getIssue().comment("Hello from my GitHub App"); |
78 | 81 | }; |
79 | 82 | assertThatCode(assertion).doesNotThrowAnyException(); |
80 | 83 |
|
81 | 84 | // Failure |
82 | 85 | IssueEventListener.behavior = (payload, configFile) -> { |
83 | | - payload.getIssue().addLabels("otherValue"); |
| 86 | + payload.getIssue().comment("otherValue"); |
84 | 87 | }; |
85 | 88 | assertThatThrownBy(assertion) |
86 | 89 | .isInstanceOf(AssertionError.class) |
87 | 90 | .hasMessageContaining("Actual invocations have different arguments:\n" + |
88 | | - "GHIssue#750705278.addLabels(\"otherValue\");"); |
| 91 | + "GHIssue#750705278.comment(\n \"otherValue\"\n);"); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void ghObjectMockAndVerify() { |
| 96 | + // Do not change this, the documentation includes the exact same code |
| 97 | + ThrowingCallable assertion = () -> given() |
| 98 | + .github(mocks -> { |
| 99 | + Mockito.doThrow(new RuntimeException("Simulated exception")) |
| 100 | + .when(mocks.issue(750705278)) |
| 101 | + .comment(Mockito.any()); |
| 102 | + }) |
| 103 | + .when().payloadFromClasspath("/issue-opened.json") |
| 104 | + .event(GHEvent.ISSUES) |
| 105 | + .then().github(mocks -> { |
| 106 | + Mockito.verify(mocks.issue(750705278)) |
| 107 | + .createReaction(ReactionContent.CONFUSED); |
| 108 | + }); |
| 109 | + |
| 110 | + // Success |
| 111 | + IssueEventListener.behavior = (payload, configFile) -> { |
| 112 | + try { |
| 113 | + payload.getIssue().comment("Hello from my GitHub App"); |
| 114 | + } catch (RuntimeException e) { |
| 115 | + payload.getIssue().createReaction(ReactionContent.CONFUSED); |
| 116 | + } |
| 117 | + }; |
| 118 | + assertThatCode(assertion).doesNotThrowAnyException(); |
| 119 | + |
| 120 | + // Failure |
| 121 | + IssueEventListener.behavior = (payload, configFile) -> { |
| 122 | + payload.getIssue().comment("Hello from my GitHub App"); |
| 123 | + }; |
| 124 | + assertThatThrownBy(assertion) |
| 125 | + .isInstanceOf(AssertionError.class) |
| 126 | + .hasMessageContaining("The event handler threw an exception: Simulated exception"); |
| 127 | + } |
| 128 | + |
| 129 | + @Test |
| 130 | + void ghAppMockUtils() { |
| 131 | + // Do not change this, the documentation includes the exact same code |
| 132 | + var queryCommentsBuilder = mockBuilder(GHIssueCommentQueryBuilder.class); |
| 133 | + ThrowingCallable assertion = () -> given() |
| 134 | + .github(mocks -> { |
| 135 | + Mockito.when(mocks.issue(750705278).queryComments()) |
| 136 | + .thenReturn(queryCommentsBuilder); |
| 137 | + var previousCommentFromBotMock = mocks.ghObject(GHIssueComment.class, 2); |
| 138 | + var commentsMock = mockPagedIterable(previousCommentFromBotMock); |
| 139 | + Mockito.when(queryCommentsBuilder.list()) |
| 140 | + .thenReturn(commentsMock); |
| 141 | + }) |
| 142 | + .when().payloadFromClasspath("/issue-opened.json") |
| 143 | + .event(GHEvent.ISSUES) |
| 144 | + .then().github(mocks -> { |
| 145 | + Mockito.verify(mocks.issue(750705278)).queryComments(); |
| 146 | + // The bot already commented, it should not comment again. |
| 147 | + Mockito.verifyNoMoreInteractions(mocks.issue(750705278)); |
| 148 | + }); |
| 149 | + |
| 150 | + // Success |
| 151 | + IssueEventListener.behavior = (payload, configFile) -> { |
| 152 | + if (!payload.getIssue().queryComments().list().iterator().hasNext()) { |
| 153 | + payload.getIssue().comment("Hello from my GitHub App"); |
| 154 | + } |
| 155 | + }; |
| 156 | + assertThatCode(assertion).doesNotThrowAnyException(); |
| 157 | + |
| 158 | + // Failure |
| 159 | + IssueEventListener.behavior = (payload, configFile) -> { |
| 160 | + boolean ignored = !payload.getIssue().queryComments().list().iterator().hasNext(); |
| 161 | + // Comment regardless |
| 162 | + payload.getIssue().comment("Hello from my GitHub App"); |
| 163 | + }; |
| 164 | + assertThatThrownBy(assertion) |
| 165 | + .isInstanceOf(AssertionError.class) |
| 166 | + .hasMessageContaining("No interactions wanted here"); |
89 | 167 | } |
90 | 168 |
|
91 | 169 | @Test |
|
0 commit comments