|
1 | 1 | package io.quarkiverse.githubapp.it.testingframework; |
2 | 2 |
|
| 3 | +import static io.quarkiverse.githubapp.testing.GitHubAppMockito.mockBuilder; |
| 4 | +import static io.quarkiverse.githubapp.testing.GitHubAppMockito.mockPagedIterable; |
3 | 5 | import static io.quarkiverse.githubapp.testing.GitHubAppTesting.given; |
4 | 6 | import static io.quarkiverse.githubapp.testing.GitHubAppTesting.when; |
5 | 7 | import static org.assertj.core.api.Assertions.assertThat; |
|
20 | 22 | import org.kohsuke.github.GHApp; |
21 | 23 | import org.kohsuke.github.GHAppInstallation; |
22 | 24 | import org.kohsuke.github.GHEvent; |
| 25 | +import org.kohsuke.github.GHIssueComment; |
| 26 | +import org.kohsuke.github.GHIssueCommentQueryBuilder; |
23 | 27 | import org.kohsuke.github.GHPullRequest; |
24 | 28 | import org.kohsuke.github.GHRepository; |
25 | 29 | import org.kohsuke.github.GitHub; |
@@ -55,36 +59,111 @@ void ghObjectMocking() { |
55 | 59 | Mockito.when(mocks.issue(750705278).getBody()).thenReturn("someValue"); |
56 | 60 | }) |
57 | 61 | .when().payloadFromClasspath("/issue-opened.json") |
58 | | - .event(GHEvent.ISSUES) |
59 | | - .then().github(mocks -> { |
60 | | - })) |
| 62 | + .event(GHEvent.ISSUES)) |
61 | 63 | .doesNotThrowAnyException(); |
62 | 64 | assertThat(capture[0]).isEqualTo("someValue"); |
63 | 65 | } |
64 | 66 |
|
65 | 67 | @Test |
66 | 68 | void ghObjectVerify() { |
67 | | - 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") |
68 | 72 | .event(GHEvent.ISSUES) |
69 | | - .then().github(mocks -> { |
70 | | - verify(mocks.issue(750705278)) |
71 | | - .addLabels("someValue"); |
| 73 | + .then().github(mocks -> { // <4> |
| 74 | + Mockito.verify(mocks.issue(750705278)) |
| 75 | + .comment("Hello from my GitHub App"); |
72 | 76 | }); |
73 | 77 |
|
74 | 78 | // Success |
75 | 79 | IssueEventListener.behavior = (payload, configFile) -> { |
76 | | - payload.getIssue().addLabels("someValue"); |
| 80 | + payload.getIssue().comment("Hello from my GitHub App"); |
77 | 81 | }; |
78 | 82 | assertThatCode(assertion).doesNotThrowAnyException(); |
79 | 83 |
|
80 | 84 | // Failure |
81 | 85 | IssueEventListener.behavior = (payload, configFile) -> { |
82 | | - payload.getIssue().addLabels("otherValue"); |
| 86 | + payload.getIssue().comment("otherValue"); |
83 | 87 | }; |
84 | 88 | assertThatThrownBy(assertion) |
85 | 89 | .isInstanceOf(AssertionError.class) |
86 | 90 | .hasMessageContaining("Actual invocations have different arguments:\n" + |
87 | | - "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"); |
88 | 167 | } |
89 | 168 |
|
90 | 169 | @Test |
@@ -276,18 +355,17 @@ void clientProvider() { |
276 | 355 | Mockito.when(mocks.applicationClient().getApp()).thenReturn(app); |
277 | 356 | Mockito.when(installation1.getId()).thenReturn(1L); |
278 | 357 | Mockito.when(installation2.getId()).thenReturn(2L); |
279 | | - PagedIterable<GHAppInstallation> appInstallations = MockHelper.mockPagedIterable(installation1, |
280 | | - installation2); |
| 358 | + PagedIterable<GHAppInstallation> appInstallations = mockPagedIterable(installation1, installation2); |
281 | 359 | Mockito.when(app.listInstallations()).thenReturn(appInstallations); |
282 | 360 |
|
283 | 361 | Mockito.when(installation1Repo1.getFullName()).thenReturn("quarkusio/quarkus"); |
284 | | - PagedSearchIterable<GHRepository> installation1Repos = MockHelper.mockPagedIterable(installation1Repo1); |
| 362 | + PagedSearchIterable<GHRepository> installation1Repos = mockPagedIterable(installation1Repo1); |
285 | 363 | Mockito.when(installation1.listRepositories()) |
286 | 364 | .thenReturn(installation1Repos); |
287 | 365 |
|
288 | 366 | Mockito.when(installation2Repo1.getFullName()).thenReturn("quarkiverse/quarkus-github-app"); |
289 | 367 | Mockito.when(installation2Repo2.getFullName()).thenReturn("quarkiverse/quarkus-github-api"); |
290 | | - PagedSearchIterable<GHRepository> installation2Repos = MockHelper.mockPagedIterable(installation2Repo1, |
| 368 | + PagedSearchIterable<GHRepository> installation2Repos = mockPagedIterable(installation2Repo1, |
291 | 369 | installation2Repo2); |
292 | 370 | Mockito.when(installation2.listRepositories()) |
293 | 371 | .thenReturn(installation2Repos); |
|
0 commit comments