diff --git a/src/test/java/io/xstefank/wildfly/bot/PRAppendingMessageTest.java b/src/test/java/io/xstefank/wildfly/bot/PRAppendingMessageTest.java index 7cc5425..032e651 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRAppendingMessageTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRAppendingMessageTest.java @@ -4,7 +4,7 @@ import io.quarkus.test.junit.QuarkusTest; import io.xstefank.wildfly.bot.model.RuntimeConstants; import io.xstefank.wildfly.bot.utils.Action; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.TestConstants; import io.xstefank.wildfly.bot.utils.Util; @@ -37,7 +37,7 @@ public class PRAppendingMessageTest { PullRequestJson pullRequestJson; - MockedContext mockedContext; + MockedGHPullRequest mockedContext; @Test public void testEmptyBodyAppendMessage() throws IOException { @@ -47,7 +47,7 @@ public void testEmptyBodyAppendMessage() throws IOException { .description(null) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-00000 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> { @@ -74,7 +74,7 @@ public void testNonEmptyBodyAppendMessage() throws IOException { .description(body) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-00000 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> { @@ -94,7 +94,7 @@ public void testEmptyBodyAppendMessageMultipleLinks() throws IOException { .description(null) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-00002 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> { @@ -121,7 +121,7 @@ public void testNonEmptyBodyAppendMessageMultipleLinks() throws IOException { .description(body) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-00002 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()).event(GHEvent.PULL_REQUEST).then().github(mocks -> { @@ -142,7 +142,7 @@ public void testEmptyBodyAppendMessageMultipleDifferentLinks() throws IOExceptio .description(null) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-00002 commit") .commit("WFLY-00003 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -172,7 +172,7 @@ public void testNonEmptyBodyAppendMessageMultipleDifferentLinks() throws IOExcep .description(body) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-00002 commit") .commit("WFLY-00003 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -205,7 +205,7 @@ public void testBodyContainingSomeJirasAppendMessageMultipleDifferentLinks() thr .description(body) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-00002 commit") .commit("WFLY-00003 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -239,7 +239,7 @@ public void testBodyContainingAllJirasAppendMessageMultipleDifferentLinks() thro .description(body) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-00002 commit") .commit("WFLY-00003 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRAutomaticLabelingTest.java b/src/test/java/io/xstefank/wildfly/bot/PRAutomaticLabelingTest.java index e283360..a832836 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRAutomaticLabelingTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRAutomaticLabelingTest.java @@ -2,7 +2,8 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.TestConstants; import io.xstefank.wildfly.bot.utils.Util; @@ -29,7 +30,7 @@ public class PRAutomaticLabelingTest { private String wildflyConfigFile; private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @BeforeAll static void setupTests() throws IOException { @@ -40,8 +41,8 @@ static void setupTests() throws IOException { @Test public void testNotApplyAndRemoveRebaseThisLabel() throws IOException { - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prLabels(LABEL_NEEDS_REBASE, LABEL_FIX_ME); + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .labels(LABEL_NEEDS_REBASE, LABEL_FIX_ME); wildflyConfigFile = """ wildfly: rules: @@ -69,7 +70,7 @@ public void testApplyRebaseThisLabel() throws IOException { - id: "Label rule" title: WFLY """; - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .mergeable(Boolean.FALSE); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRChecksTest.java b/src/test/java/io/xstefank/wildfly/bot/PRChecksTest.java index d84f9dd..2151c09 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRChecksTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRChecksTest.java @@ -2,7 +2,8 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.Util; import org.junit.jupiter.api.Test; @@ -29,7 +30,7 @@ public class PRChecksTest { private static String wildflyConfigFile; private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @Test void testNoConfigFile() throws IOException { @@ -45,7 +46,7 @@ void testNoConfigFile() throws IOException { .title(INVALID_TITLE) .description(INVALID_DESCRIPTION) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit(INVALID_COMMIT_MESSAGE); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -108,7 +109,7 @@ void testAllChecksFail() throws IOException { .title(INVALID_TITLE) .description(INVALID_DESCRIPTION) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit(INVALID_COMMIT_MESSAGE); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRCommitCheckTest.java b/src/test/java/io/xstefank/wildfly/bot/PRCommitCheckTest.java index 28a4eaf..132a476 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRCommitCheckTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRCommitCheckTest.java @@ -2,7 +2,8 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.Util; import org.junit.jupiter.api.BeforeAll; @@ -32,7 +33,7 @@ public class PRCommitCheckTest { private static String wildflyConfigFile; private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @BeforeAll static void setUpGitHubJson() throws IOException { @@ -47,7 +48,7 @@ void testFailedCommitCheck() throws IOException { commit: enabled: true """; - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit(INVALID_COMMIT_MESSAGE); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) @@ -67,7 +68,7 @@ void testFailedCommitCheckCommitConfigNull() throws IOException { format: commit: """; - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit(INVALID_COMMIT_MESSAGE); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -112,7 +113,7 @@ void testSuccessfulMultilinedCommitCheck() throws IOException { enabled: true """; - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit(""" WFLY-18341 Restore incorrectly updated copyright dates in Jipijapa @@ -155,7 +156,7 @@ void testDisabledCommitCheck() throws IOException { commit: enabled: false """; - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit(INVALID_COMMIT_MESSAGE); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -176,7 +177,7 @@ public void testOverridingCommitMessage() throws IOException { message: "Lorem ipsum dolor sit amet" """; - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit(INVALID_COMMIT_MESSAGE); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -196,7 +197,7 @@ public void testDisableGlobalFormatCheck() throws IOException { format: enabled: false """; - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit(INVALID_COMMIT_MESSAGE); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson)) .when().payloadFromString(pullRequestJson.jsonString()) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRConfigFileChangeTest.java b/src/test/java/io/xstefank/wildfly/bot/PRConfigFileChangeTest.java index 8ee0115..230460d 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRConfigFileChangeTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRConfigFileChangeTest.java @@ -3,7 +3,7 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; import io.xstefank.wildfly.bot.model.RuntimeConstants; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import io.xstefank.wildfly.bot.utils.PullRequestJson; import org.apache.commons.io.IOUtils; import org.junit.jupiter.api.BeforeAll; @@ -59,8 +59,8 @@ void testUpdateIncorrectlyIndentedFile() throws IOException { - address@email.com""", "UTF-8")); - MockedContext.builder(pullRequestJson.id()) - .prFiles(".github/wildfly-bot.yml") + MockedGHPullRequest.builder(pullRequestJson.id()) + .files(".github/wildfly-bot.yml") .mock(mocks); }) .when().payloadFromString(pullRequestJson.jsonString()) @@ -99,8 +99,8 @@ void testUpdateToCorrectFile() throws IOException { - address@email.com""", "UTF-8")); - MockedContext.builder(pullRequestJson.id()) - .prFiles(".github/wildfly-bot.yml") + MockedGHPullRequest.builder(pullRequestJson.id()) + .files(".github/wildfly-bot.yml") .mock(mocks); }) .when().payloadFromString(pullRequestJson.jsonString()) @@ -137,8 +137,8 @@ void testUpdateWithIncorrectRulesFile() throws IOException { - address@email.com""", "UTF-8")); - MockedContext.builder(pullRequestJson.id()) - .prFiles(".github/wildfly-bot.yml") + MockedGHPullRequest.builder(pullRequestJson.id()) + .files(".github/wildfly-bot.yml") .mock(mocks); }) .when().payloadFromString(pullRequestJson.jsonString()) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRDirectoriesVerificationTest.java b/src/test/java/io/xstefank/wildfly/bot/PRDirectoriesVerificationTest.java index 4a767fe..37c30cb 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRDirectoriesVerificationTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRDirectoriesVerificationTest.java @@ -3,7 +3,9 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; import io.xstefank.wildfly.bot.model.RuntimeConstants; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; +import io.xstefank.wildfly.bot.utils.MockedGHRepository; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.TestConstants; import org.apache.commons.io.IOUtils; @@ -27,7 +29,7 @@ public class PRDirectoriesVerificationTest { private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @BeforeAll public static void setupTests() throws IOException { @@ -37,9 +39,10 @@ public static void setupTests() throws IOException { @Test public void existingDirectoryTest() throws IOException { - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles(".github/wildfly-bot.yml") - .repoDirectories("src"); + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files(".github/wildfly-bot.yml") + .mockNext(MockedGHRepository.builder()) + .directories("src"); given().github(mocks -> { mockedContext.mock(mocks); GHRepository repo = mocks.repository(TestConstants.TEST_REPO); @@ -66,8 +69,8 @@ public void existingDirectoryTest() throws IOException { @Test public void nonExistingDirectoryTest() throws IOException { - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles(".github/wildfly-bot.yml"); + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files(".github/wildfly-bot.yml"); given().github(mocks -> { mockedContext.mock(mocks); GHRepository repo = mocks.repository(TestConstants.TEST_REPO); @@ -96,9 +99,10 @@ public void nonExistingDirectoryTest() throws IOException { @Test public void existingFileTest() throws IOException { - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles(".github/wildfly-bot.yml") - .repoFiles("pom.xml"); + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files(".github/wildfly-bot.yml") + .mockNext(MockedGHRepository.builder()) + .files("pom.xml"); given().github(mocks -> { mockedContext.mock(mocks); GHRepository repo = mocks.repository(TestConstants.TEST_REPO); @@ -123,9 +127,10 @@ public void existingFileTest() throws IOException { @Test public void oneExistingSubdirectoryTest() throws IOException { - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles(".github/wildfly-bot.yml") - .repoDirectories("src", "src/main", "src/main/java"); + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files(".github/wildfly-bot.yml") + .mockNext(MockedGHRepository.builder()) + .directories("src", "src/main", "src/main/java"); given().github(mocks -> { mockedContext.mock(mocks); GHRepository repo = mocks.repository(TestConstants.TEST_REPO); @@ -155,10 +160,11 @@ public void oneExistingSubdirectoryTest() throws IOException { @Test public void oneExistingFileInSubdirectoryTest() throws IOException { - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles(".github/wildfly-bot.yml") - .repoDirectories("src", "src/main", "src/main/java") - .repoFiles("src/main/resources/application.properties"); + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files(".github/wildfly-bot.yml") + .mockNext(MockedGHRepository.builder()) + .directories("src", "src/main", "src/main/java") + .files("src/main/resources/application.properties"); given().github(mocks -> { mockedContext.mock(mocks); GHRepository repo = mocks.repository(TestConstants.TEST_REPO); diff --git a/src/test/java/io/xstefank/wildfly/bot/PRFormatOverrideProjectKeyTest.java b/src/test/java/io/xstefank/wildfly/bot/PRFormatOverrideProjectKeyTest.java index b3e0588..165b242 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRFormatOverrideProjectKeyTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRFormatOverrideProjectKeyTest.java @@ -3,8 +3,8 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; import io.xstefank.wildfly.bot.utils.Action; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import io.xstefank.wildfly.bot.utils.PullRequestJson; -import io.xstefank.wildfly.bot.utils.MockedContext; import io.xstefank.wildfly.bot.utils.Util; import org.junit.jupiter.api.Test; import org.kohsuke.github.GHEvent; @@ -30,7 +30,7 @@ public class PRFormatOverrideProjectKeyTest { projectKey: WFCORE """; private static PullRequestJson pullRequestJson; - private static MockedContext mockedContext; + private static MockedGHPullRequest mockedContext; @Test public void testOverridingProjectKeyCorrectTitle() throws IOException { @@ -38,7 +38,7 @@ public void testOverridingProjectKeyCorrectTitle() throws IOException { .action(Action.EDITED) .title("WFCORE-00000 title") .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("[WFCORE-123] Valid commit message"); given() @@ -56,7 +56,7 @@ public void testOverridingProjectKeyIncorrectTitle() throws IOException { pullRequestJson = PullRequestJson.builder(VALID_PR_TEMPLATE_JSON) .action(Action.EDITED) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("[WFCORE-123] Valid commit message"); given() diff --git a/src/test/java/io/xstefank/wildfly/bot/PRMentionsReviewsCombinationTest.java b/src/test/java/io/xstefank/wildfly/bot/PRMentionsReviewsCombinationTest.java index ddd2dd8..f7f6c2c 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRMentionsReviewsCombinationTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRMentionsReviewsCombinationTest.java @@ -2,7 +2,9 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; +import io.xstefank.wildfly.bot.utils.MockedGHRepository; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.TestConstants; import io.xstefank.wildfly.bot.utils.Util; @@ -28,7 +30,7 @@ public class PRMentionsReviewsCombinationTest { private static String wildflyConfigFile; private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @Test public void testMentionsReviewsBothHitSameRule() throws IOException { @@ -46,8 +48,9 @@ public void testMentionsReviewsBothHitSameRule() throws IOException { enabled: false """; pullRequestJson = PullRequestJson.builder(TestConstants.VALID_PR_TEMPLATE_JSON).build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles("src/main/java/resource/application.properties") + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("src/main/java/resource/application.properties") + .mockNext(MockedGHRepository.builder()) .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -82,7 +85,8 @@ public void testMentionsReviewsMentionHitSameRule() throws IOException { enabled: false """; pullRequestJson = PullRequestJson.builder(TestConstants.VALID_PR_TEMPLATE_JSON).build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .mockNext(MockedGHRepository.builder()) .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -113,8 +117,9 @@ public void testMentionsReviewsReviewsHitSameRule() throws IOException { pullRequestJson = PullRequestJson.builder(TestConstants.VALID_PR_TEMPLATE_JSON) .title(INVALID_TITLE) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles("src/main/java/resource/application.properties") + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("src/main/java/resource/application.properties") + .mockNext(MockedGHRepository.builder()) .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -154,8 +159,9 @@ public void testMentionsReviewsReviewsHitTwoRules() throws IOException { pullRequestJson = PullRequestJson.builder(TestConstants.VALID_PR_TEMPLATE_JSON) .title(INVALID_TITLE) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles("src/main/java/resource/application.properties") + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("src/main/java/resource/application.properties") + .mockNext(MockedGHRepository.builder()) .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -194,8 +200,9 @@ public void testMentionsReviewsMentionsReviewsHitTwoRules() throws IOException { """; pullRequestJson = PullRequestJson.builder(TestConstants.VALID_PR_TEMPLATE_JSON) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles("src/main/java/resource/application.properties") + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("src/main/java/resource/application.properties") + .mockNext(MockedGHRepository.builder()) .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRNotifyChangeOnPREditTest.java b/src/test/java/io/xstefank/wildfly/bot/PRNotifyChangeOnPREditTest.java index 4201e31..f6d8f82 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRNotifyChangeOnPREditTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRNotifyChangeOnPREditTest.java @@ -4,8 +4,10 @@ import io.quarkus.test.junit.QuarkusTest; import io.xstefank.wildfly.bot.config.WildFlyBotConfig; import io.xstefank.wildfly.bot.utils.Action; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; +import io.xstefank.wildfly.bot.utils.MockedGHRepository; import io.xstefank.wildfly.bot.utils.PullRequestJson; -import io.xstefank.wildfly.bot.utils.MockedContext; import io.xstefank.wildfly.bot.utils.TestConstants; import io.xstefank.wildfly.bot.utils.Util; import jakarta.inject.Inject; @@ -33,6 +35,7 @@ public class PRNotifyChangeOnPREditTest { private String wildflyConfigFile; private static PullRequestJson pullRequestJson; + private Mockable mockedContext; @Inject WildFlyBotConfig wildFlyBotConfig; @@ -55,11 +58,12 @@ public void updateReviewNoMentionsTest() throws IOException { - id: "previous rule" directories: [app] notify: [user1]"""; - MockedContext mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-123 commit") - .prFiles("src/pom.xml", "app/pom.xml") - .users("user1", "user2") - .reviewers("user1"); + .files("src/pom.xml", "app/pom.xml") + .reviewers("user1") + .mockNext(MockedGHRepository.builder()) + .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) .event(GHEvent.PULL_REQUEST) @@ -87,12 +91,13 @@ public void updateReviewKeepMentionsTest() throws IOException { - id: "test2" title: "WFLY" notify: [user2, user3]"""; - MockedContext mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .comment("/cc @user3", wildFlyBotConfig.githubName()) .commit("WFLY-123 commit") - .prFiles("src/pom.xml", "app/pom.xml") - .users("user1", "user2") - .reviewers("user1"); + .files("src/pom.xml", "app/pom.xml") + .reviewers("user1") + .mockNext(MockedGHRepository.builder()) + .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) .event(GHEvent.PULL_REQUEST) @@ -120,11 +125,12 @@ public void updateReviewUpdateMentionsTest() throws IOException { - id: "test2" title: "WFLY" notify: [user2, user3]"""; - MockedContext mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-123 commit") - .prFiles("src/pom.xml", "app/pom.xml") - .users("user1", "user2") - .reviewers("user1"); + .files("src/pom.xml", "app/pom.xml") + .reviewers("user1") + .mockNext(MockedGHRepository.builder()) + .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) .event(GHEvent.PULL_REQUEST) @@ -150,7 +156,7 @@ public void noReviewUpdateMentionsTest() throws IOException { - id: "previous rule" titleBody: "WFLY" notify: [user1]"""; - MockedContext mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .comment("/cc @user1", wildFlyBotConfig.githubName()) .commit("WFLY-123 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) @@ -174,11 +180,12 @@ public void keepReviewUpdateMentionsTest() throws IOException { - id: "test2" title: "WFLY" notify: [user2, user3]"""; - MockedContext mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-123 commit") - .prFiles("src/pom.xml", "app/pom.xml") - .users("user1", "user2") - .reviewers("user1", "user2"); + .files("src/pom.xml", "app/pom.xml") + .reviewers("user1", "user2") + .mockNext(MockedGHRepository.builder()) + .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) .event(GHEvent.PULL_REQUEST) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRReviewAssignmentTest.java b/src/test/java/io/xstefank/wildfly/bot/PRReviewAssignmentTest.java index 115cb30..f5d510a 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRReviewAssignmentTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRReviewAssignmentTest.java @@ -6,7 +6,9 @@ import io.quarkus.test.InMemoryLogHandler; import io.quarkus.test.junit.QuarkusTest; import io.xstefank.wildfly.bot.util.GithubProcessor; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; +import io.xstefank.wildfly.bot.utils.MockedGHRepository; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.TestConstants; import io.xstefank.wildfly.bot.utils.Util; @@ -62,7 +64,7 @@ record -> record.getLevel().intValue() >= Level.ALL.intValue()); """; private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @Inject MockMailbox mailbox; @@ -79,8 +81,8 @@ void setup() { @Test public void testOnlyMissingCollaboratorsNoReviewAssignment() throws IOException { - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles("src/main/java/resource/application.properties"); + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("src/main/java/resource/application.properties"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) .event(GHEvent.PULL_REQUEST) @@ -102,8 +104,9 @@ public void testOnlyMissingCollaboratorsNoReviewAssignment() throws IOException @Test public void testNoCommentOnlyReviewAssignment() throws IOException { - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles("src/main/java/resource/application.properties") + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("src/main/java/resource/application.properties") + .mockNext(MockedGHRepository.builder()) .users("user1", "user2"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) @@ -123,8 +126,9 @@ public void testNoCommentOnlyReviewAssignment() throws IOException { @Test public void testCommentAndReviewAssignmentCombination() throws IOException { - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles("src/main/java/resource/application.properties") + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("src/main/java/resource/application.properties") + .mockNext(MockedGHRepository.builder()) .users("user1"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRReviewFixMeLabelTest.java b/src/test/java/io/xstefank/wildfly/bot/PRReviewFixMeLabelTest.java index a7fe81f..c4ef11a 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRReviewFixMeLabelTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRReviewFixMeLabelTest.java @@ -2,7 +2,8 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import io.xstefank.wildfly.bot.utils.PullRequestReviewJson; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; @@ -33,13 +34,13 @@ public class PRReviewFixMeLabelTest { private static PullRequestReviewJson pullRequestReviewJson; - private MockedContext mockedContext; + private Mockable mockedContext; @Test void notAddingFixMeLabelOnPROpen() throws IOException { pullRequestReviewJson = PullRequestReviewJson.> builder( VALID_PR_REVIEW_TEMPLATE_JSON).build(); - given().github(mocks -> MockedContext.builder(pullRequestReviewJson.id()).mock(mocks)) + given().github(mocks -> MockedGHPullRequest.builder(pullRequestReviewJson.id()).mock(mocks)) .when().payloadFromString(pullRequestReviewJson.jsonString()) .event(GHEvent.PULL_REQUEST) .then().github(mocks -> { @@ -52,7 +53,7 @@ void notAddingFixMeLabelOnPROpen() throws IOException { void notAddingFixMeLabelOnReviewComment() throws IOException { pullRequestReviewJson = PullRequestReviewJson.> builder( VALID_PR_REVIEW_TEMPLATE_JSON).state(COMMENT).build(); - given().github(mocks -> MockedContext.builder(pullRequestReviewJson.id()).mock(mocks)) + given().github(mocks -> MockedGHPullRequest.builder(pullRequestReviewJson.id()).mock(mocks)) .when().payloadFromString(pullRequestReviewJson.jsonString()) .event(GHEvent.PULL_REQUEST_REVIEW) .then().github(mocks -> { @@ -65,7 +66,7 @@ void notAddingFixMeLabelOnReviewComment() throws IOException { void notAddingFixMeLabelOnReviewApprove() throws IOException { pullRequestReviewJson = PullRequestReviewJson.> builder( VALID_PR_REVIEW_TEMPLATE_JSON).state(APPROVE).build(); - given().github(mocks -> MockedContext.builder(pullRequestReviewJson.id()).mock(mocks)) + given().github(mocks -> MockedGHPullRequest.builder(pullRequestReviewJson.id()).mock(mocks)) .when().payloadFromString(pullRequestReviewJson.jsonString()) .event(GHEvent.PULL_REQUEST_REVIEW) .then().github(mocks -> { @@ -79,7 +80,7 @@ void addFixMeLabelOnReviewChangesRequested() throws IOException { pullRequestReviewJson = PullRequestReviewJson.> builder( VALID_PR_REVIEW_TEMPLATE_JSON).state(CHANGES_REQUESTED) .build(); - given().github(mocks -> MockedContext.builder(pullRequestReviewJson.id()).mock(mocks)) + given().github(mocks -> MockedGHPullRequest.builder(pullRequestReviewJson.id()).mock(mocks)) .when().payloadFromString(pullRequestReviewJson.jsonString()) .event(GHEvent.PULL_REQUEST_REVIEW) .then().github(mocks -> { @@ -95,9 +96,9 @@ void removeFixMeLabelOnReviewOnNewCommit() throws IOException { pullRequestReviewJson = PullRequestReviewJson.> builder( VALID_PR_TEMPLATE_JSON) .action(SYNCHRONIZE).build(); - given().github(mocks -> MockedContext.builder(pullRequestReviewJson.id()) + given().github(mocks -> MockedGHPullRequest.builder(pullRequestReviewJson.id()) .commit("Mock commit") - .prLabels(LABEL_FIX_ME) + .labels(LABEL_FIX_ME) .mock(mocks)) .when().payloadFromString(pullRequestReviewJson.jsonString()) .event(GHEvent.PULL_REQUEST) @@ -114,9 +115,9 @@ void notRemoveFixMeLabelOnReviewOnPullRequestEdited() throws IOException { pullRequestReviewJson = PullRequestReviewJson.> builder( VALID_PR_TEMPLATE_JSON) .action(EDITED).build(); - given().github(mocks -> MockedContext.builder(pullRequestReviewJson.id()) + given().github(mocks -> MockedGHPullRequest.builder(pullRequestReviewJson.id()) .commit("Mock commit") - .prLabels(LABEL_FIX_ME) + .labels(LABEL_FIX_ME) .mock(mocks)) .when().payloadFromString(pullRequestReviewJson.jsonString()) .event(GHEvent.PULL_REQUEST) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRRuleDirectoryHitTest.java b/src/test/java/io/xstefank/wildfly/bot/PRRuleDirectoryHitTest.java index fcaf853..ff23150 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRRuleDirectoryHitTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRRuleDirectoryHitTest.java @@ -2,7 +2,9 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; +import io.xstefank.wildfly.bot.utils.MockedGHRepository; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.Util; import org.hamcrest.MatcherAssert; @@ -33,7 +35,7 @@ public class PRRuleDirectoryHitTest { private static String wildflyConfigFile; private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @BeforeAll static void setUpGitHubJson() throws IOException { @@ -55,11 +57,12 @@ void testDirectoriesNotifyNewFileInDiff() throws IOException { title: enabled: false """; - mockedContext = MockedContext.builder(pullRequestJson.id()) - .users("7125767235") - .prFiles("appclient/test.txt", + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("appclient/test.txt", "microprofile/health-smallrye/pom.xml", - "testsuite/integration/basic/pom.xml"); + "testsuite/integration/basic/pom.xml") + .mockNext(MockedGHRepository.builder()) + .users("7125767235"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) @@ -82,11 +85,12 @@ void testDirectoriesNotifyChangeInSubdirectoryDiff() throws IOException { - microprofile/health-smallrye notify: [7125767235] """; - mockedContext = MockedContext.builder(pullRequestJson.id()) - .users("7125767235") - .prFiles("appclient/test.txt", + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("appclient/test.txt", "microprofile/health-smallrye/pom.xml", - "testsuite/integration/basic/pom.xml"); + "testsuite/integration/basic/pom.xml") + .mockNext(MockedGHRepository.builder()) + .users("7125767235"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) @@ -114,11 +118,12 @@ void testDirectoriesNotifyChangeInSubdirectoryOfConfiguredDirectoryDiff() throws - testsuite/integration notify: [7125767235] """; - mockedContext = MockedContext.builder(pullRequestJson.id()) - .users("7125767235") - .prFiles("appclient/test.txt", + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("appclient/test.txt", "microprofile/health-smallrye/pom.xml", - "testsuite/integration/basic/pom.xml"); + "testsuite/integration/basic/pom.xml") + .mockNext(MockedGHRepository.builder()) + .users("7125767235"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) @@ -146,8 +151,8 @@ void testDirectoriesNotifyNoHitInDiff() throws IOException { - transactions notify: [7125767235] """; - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles("appclient/test.txt", + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("appclient/test.txt", "microprofile/health-smallrye/pom.xml", "testsuite/integration/basic/pom.xml"); @@ -173,8 +178,8 @@ void testDirectoriesNoHitInDiffIfSubstring() throws IOException { - app notify: [7125767235] """; - mockedContext = MockedContext.builder(pullRequestJson.id()) - .prFiles("appclient/test.txt", + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) + .files("appclient/test.txt", "microprofile/health-smallrye/pom.xml", "testsuite/integration/basic/pom.xml"); diff --git a/src/test/java/io/xstefank/wildfly/bot/PRRuleLabelTest.java b/src/test/java/io/xstefank/wildfly/bot/PRRuleLabelTest.java index 47112a2..2627166 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRRuleLabelTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRRuleLabelTest.java @@ -2,7 +2,8 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHRepository; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.TestConstants; import io.xstefank.wildfly.bot.utils.Util; @@ -29,7 +30,7 @@ public class PRRuleLabelTest { private String wildflyConfigFile; private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @BeforeAll static void setupTests() throws IOException { @@ -47,8 +48,8 @@ public void testHittingLabelRuleLabelInRepository() throws IOException { title: WFLY labels: [label1] """; - mockedContext = MockedContext.builder(pullRequestJson.id()) - .repoLabels(Set.of("label1")); + mockedContext = MockedGHRepository.builder() + .labels(Set.of("label1")); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) .event(GHEvent.PULL_REQUEST) @@ -87,8 +88,8 @@ public void testHittingLabelRuleSomeLabelsMissingInRepository() throws IOExcepti title: WFLY labels: [label1, label2, label3, label4] """; - mockedContext = MockedContext.builder(pullRequestJson.id()) - .repoLabels(Set.of("label2", "label4")); + mockedContext = MockedGHRepository.builder() + .labels(Set.of("label2", "label4")); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) .event(GHEvent.PULL_REQUEST) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRSkipPullRequestTest.java b/src/test/java/io/xstefank/wildfly/bot/PRSkipPullRequestTest.java index e101107..d27baf3 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRSkipPullRequestTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRSkipPullRequestTest.java @@ -2,7 +2,8 @@ import io.quarkiverse.githubapp.testing.GitHubAppTest; import io.quarkus.test.junit.QuarkusTest; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.Util; import org.junit.jupiter.api.Test; @@ -23,7 +24,7 @@ public class PRSkipPullRequestTest { private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; private static final String wildflyConfigFile = """ wildfly: format: @@ -47,7 +48,7 @@ void testSkippingFormatCheck() throws IOException { finished""") .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()).commit(INVALID_COMMIT_MESSAGE); + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()).commit(INVALID_COMMIT_MESSAGE); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) .event(GHEvent.PULL_REQUEST) @@ -67,7 +68,7 @@ void testSkippingFormatCheckOnDraft() throws IOException { pullRequestJson = PullRequestJson.builder(VALID_PR_TEMPLATE_JSON) .title(INVALID_TITLE) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()).draft(); + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()).draft(); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) .event(GHEvent.PULL_REQUEST) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRTitleCheckTest.java b/src/test/java/io/xstefank/wildfly/bot/PRTitleCheckTest.java index ca532d0..11af628 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRTitleCheckTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRTitleCheckTest.java @@ -5,7 +5,8 @@ import io.xstefank.wildfly.bot.format.TitleCheck; import io.xstefank.wildfly.bot.model.RegexDefinition; import io.xstefank.wildfly.bot.utils.Action; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.Util; import org.junit.jupiter.api.Assertions; @@ -36,7 +37,7 @@ public class PRTitleCheckTest { private static String wildflyConfigFile; private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @Test void configFileNullTest() { @@ -332,7 +333,7 @@ public void testOverridingTitleMessage() throws IOException { pullRequestJson = PullRequestJson.builder(VALID_PR_TEMPLATE_JSON) .title(INVALID_TITLE) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("WFLY-00000 commit"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) diff --git a/src/test/java/io/xstefank/wildfly/bot/PRUpdateCommentOnEditTest.java b/src/test/java/io/xstefank/wildfly/bot/PRUpdateCommentOnEditTest.java index ff1febb..f94ca02 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PRUpdateCommentOnEditTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PRUpdateCommentOnEditTest.java @@ -4,7 +4,9 @@ import io.quarkus.test.junit.QuarkusTest; import io.xstefank.wildfly.bot.config.WildFlyBotConfig; import io.xstefank.wildfly.bot.utils.Action; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; +import io.xstefank.wildfly.bot.utils.MockedGHRepository; import io.xstefank.wildfly.bot.utils.PullRequestJson; import io.xstefank.wildfly.bot.utils.Util; import jakarta.inject.Inject; @@ -34,7 +36,7 @@ public class PRUpdateCommentOnEditTest { private static String wildflyConfigFile; private static PullRequestJson pullRequestJson; - private MockedContext mockedContext; + private Mockable mockedContext; @Inject WildFlyBotConfig wildFlyBotConfig; @@ -53,7 +55,7 @@ void testUpdateToValid() throws IOException { .title(INVALID_TITLE) .description(INVALID_DESCRIPTION) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .comment(FAILED_FORMAT_COMMENT.formatted(Stream.of( DEFAULT_COMMIT_MESSAGE.formatted(PROJECT_PATTERN_REGEX.formatted("WFLY")), DEFAULT_TITLE_MESSAGE.formatted(PROJECT_PATTERN_REGEX.formatted("WFLY")), @@ -83,14 +85,15 @@ void testRemoveCommentAndUpdateCommitStatusOnEditToSkipFormatCheck() throws IOEx .description("@%s skip format".formatted(wildFlyBotConfig.githubName())) .action(Action.EDITED) .build(); - mockedContext = MockedContext.builder(pullRequestJson.id()) - .commitStatuses(pullRequestJson.commitSHA(), "Format") + mockedContext = MockedGHPullRequest.builder(pullRequestJson.id()) .comment(FAILED_FORMAT_COMMENT.formatted(Stream.of( DEFAULT_COMMIT_MESSAGE.formatted(PROJECT_PATTERN_REGEX.formatted("WFLY")), DEFAULT_TITLE_MESSAGE.formatted(PROJECT_PATTERN_REGEX.formatted("WFLY")), "The PR description must contain a link to the JIRA issue") .map("- %s"::formatted) - .collect(Collectors.joining("\n\n"))), wildFlyBotConfig.githubName()); + .collect(Collectors.joining("\n\n"))), wildFlyBotConfig.githubName()) + .mockNext(MockedGHRepository.builder()) + .commitStatuses(pullRequestJson.commitSHA(), "Format"); given().github(mocks -> Util.mockRepo(mocks, wildflyConfigFile, pullRequestJson, mockedContext)) .when().payloadFromString(pullRequestJson.jsonString()) diff --git a/src/test/java/io/xstefank/wildfly/bot/PushEventTest.java b/src/test/java/io/xstefank/wildfly/bot/PushEventTest.java index 37515e4..1515327 100644 --- a/src/test/java/io/xstefank/wildfly/bot/PushEventTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/PushEventTest.java @@ -4,7 +4,7 @@ import io.quarkus.test.InMemoryLogHandler; import io.quarkus.test.junit.QuarkusTest; import io.xstefank.wildfly.bot.config.WildFlyBotConfig; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; import jakarta.inject.Inject; import org.jboss.logmanager.Level; import org.junit.jupiter.api.Assertions; @@ -74,7 +74,9 @@ void applyRebaseThisLabelOnUnmergablePullRequestsTest() throws Exception { if (i < 2) { // This happens as we mock repository content and on second mocking we invoke the mocked method... try { - MockedContext.builder(i).repository(TEST_REPO).prLabels(LABEL_NEEDS_REBASE).mock(mocks); + MockedGHPullRequest.builder(i) + .labels(LABEL_NEEDS_REBASE) + .mock(mocks); } catch (GHFileNotFoundException ignored) { } } diff --git a/src/test/java/io/xstefank/wildfly/bot/StartupEventTest.java b/src/test/java/io/xstefank/wildfly/bot/StartupEventTest.java index a8ce328..d2f9ac2 100644 --- a/src/test/java/io/xstefank/wildfly/bot/StartupEventTest.java +++ b/src/test/java/io/xstefank/wildfly/bot/StartupEventTest.java @@ -13,7 +13,9 @@ import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.QuarkusTestExtension; import io.xstefank.wildfly.bot.model.RuntimeConstants; -import io.xstefank.wildfly.bot.utils.MockedContext; +import io.xstefank.wildfly.bot.utils.Mockable; +import io.xstefank.wildfly.bot.utils.MockedGHPullRequest; +import io.xstefank.wildfly.bot.utils.MockedGHRepository; import io.xstefank.wildfly.bot.utils.PullRequestJson; import jakarta.enterprise.event.Event; import jakarta.inject.Inject; @@ -82,14 +84,14 @@ record -> record.getLevel().intValue() >= Level.ALL.intValue()); rootLogger.addHandler(inMemoryLogHandler); } - private MockedContext mockedContext; + private Mockable mockedContext; @RegisterExtension static QuarkusTestExtension TEST = new QuarkusTestExtension(); @BeforeEach void setup() { - mockedContext = MockedContext.builder(1371642823); + mockedContext = MockedGHPullRequest.builder(1371642823); } private class CustomGithubMockSetup implements GitHubMockSetup { @@ -303,7 +305,7 @@ public void testCreateMissingLabelsLabel() throws IOException { @Test public void testCreateOneMissingLabelsLabel() throws IOException { - mockedContext.repoLabels(Set.of(LABEL_FIX_ME)); + mockedContext = mockedContext.mockNext(MockedGHRepository.builder()).labels(Set.of(LABEL_FIX_ME)); given().github(new CustomGithubMockSetup(""" wildfly: rules: diff --git a/src/test/java/io/xstefank/wildfly/bot/utils/Mockable.java b/src/test/java/io/xstefank/wildfly/bot/utils/Mockable.java new file mode 100644 index 0000000..4aef065 --- /dev/null +++ b/src/test/java/io/xstefank/wildfly/bot/utils/Mockable.java @@ -0,0 +1,91 @@ +package io.xstefank.wildfly.bot.utils; + +import com.thoughtworks.xstream.InitializationException; +import io.quarkiverse.githubapp.testing.dsl.GitHubMockContext; + +import java.io.IOException; +import java.util.Collections; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.atomic.AtomicLong; + +import static io.xstefank.wildfly.bot.utils.TestConstants.VALID_PR_TEMPLATE_JSON; + +/** + * This class serves as base for grouping Mocking functionality + * for certain logical domains, such as {@link org.kohsuke.github.GHRepository} + * or {@link org.kohsuke.github.GHPullRequest}. + * Where you would create a {@code Mockable} for your specific domain and can + * chain other {@code Mockable}-s together, when mocking. See usage in tests. + * + * You can add your implementation of Mockable into {@code requiredMockables} Set. + * After all registered {@code Mockable}-s (by {@code mockNext}) have been executed + * and no instances from {@code requiredMockables} have been found, then default implementation + * will be executed. This is the implementation you add to the {@code requiredMockables}. + * + * Note: Only one {@code Mockable} per domain is allowed to be mocked. + * + * Parallel execution is not supported as we are relaying static variables. + */ +public abstract class Mockable { + + protected Mockable previousMock; + + private static final Set requiredMockables; + static { + Set requiredMockableCollector = new HashSet<>(); + requiredMockableCollector.add(MockedGHRepository.builder()); + try { + PullRequestJson pullRequestJson = PullRequestJson.builder(VALID_PR_TEMPLATE_JSON).build(); + requiredMockableCollector.add(MockedGHPullRequest.builder(pullRequestJson.id())); + } catch (IOException e) { + throw new InitializationException("Unable to initialize MockedGHPullRequest", e); + } + requiredMockables = Collections.unmodifiableSet(requiredMockableCollector); + } + private static final Set executedMockables = new HashSet<>(); + + public final void mock(GitHubMockContext mocks) throws IOException { + this.mockNext(mocks, new AtomicLong()); + } + + /** + * Implement this method to mock your specific domain. + * + * @param mocks context passed into {@code io.quarkiverse.githubapp.testing.dsl.GitHubMockVerification} interface + * @param idGenerator Common aggregate for mocked id generation, such that no duplicate id-s are created. + * @return {code idGenerator} should be returned after it was used. + * @throws IOException + */ + abstract AtomicLong mock(GitHubMockContext mocks, AtomicLong idGenerator) throws IOException; + + public final T mockNext(T mockable) { + mockable.previousMock = this; + return mockable; + } + + /** + * Runs chain of {@code Mockable}-s and checks for duplicates and executes defaults, if no overrides were found. + */ + private void mockNext(GitHubMockContext mocks, AtomicLong idGenerator) throws IOException { + idGenerator = this.mock(mocks, idGenerator); + Class clazz = this.getClass(); + if (executedMockables.stream().anyMatch(mockable -> mockable.getClass().equals(clazz))) { + throw new RuntimeException("Two instances of the same Mockable detected for class %s".formatted(clazz)); + } + executedMockables.add(this); + + if (previousMock != null) { + previousMock.mockNext(mocks, idGenerator); + } else { + // executes default implementations if no overrides provided + for (Mockable requiredMockable : requiredMockables) { + Class requiredMockableClazz = requiredMockable.getClass(); + if (executedMockables.stream().noneMatch(mockable -> mockable.getClass().equals(requiredMockableClazz))) { + idGenerator = requiredMockable.mock(mocks, idGenerator); + } + } + executedMockables.clear(); + } + } +} diff --git a/src/test/java/io/xstefank/wildfly/bot/utils/MockedContext.java b/src/test/java/io/xstefank/wildfly/bot/utils/MockedContext.java deleted file mode 100644 index 425274a..0000000 --- a/src/test/java/io/xstefank/wildfly/bot/utils/MockedContext.java +++ /dev/null @@ -1,250 +0,0 @@ -package io.xstefank.wildfly.bot.utils; - -import io.quarkiverse.githubapp.testing.GitHubAppMockito; -import io.quarkiverse.githubapp.testing.dsl.GitHubMockContext; -import io.smallrye.mutiny.tuples.Tuple2; -import org.kohsuke.github.GHCommitStatus; -import org.kohsuke.github.GHContent; -import org.kohsuke.github.GHFileNotFoundException; -import org.kohsuke.github.GHIssueComment; -import org.kohsuke.github.GHLabel; -import org.kohsuke.github.GHPullRequest; -import org.kohsuke.github.GHPullRequestCommitDetail; -import org.kohsuke.github.GHPullRequestFileDetail; -import org.kohsuke.github.GHRepository; -import org.kohsuke.github.GHUser; -import org.kohsuke.github.HttpException; -import org.kohsuke.github.PagedIterable; -import org.kohsuke.github.PagedSearchIterable; -import org.mockito.ArgumentMatchers; -import org.mockito.Mockito; -import org.mockito.invocation.InvocationOnMock; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.List; -import java.util.Set; -import java.util.concurrent.atomic.AtomicLong; - -import static io.xstefank.wildfly.bot.utils.TestConstants.INSTALLATION_ID; -import static io.xstefank.wildfly.bot.utils.TestConstants.TEST_REPO; -import static org.mockito.ArgumentMatchers.anyString; - -public class MockedContext { - - private final long pullRequest; - private Set prFiles = new LinkedHashSet<>(); - private final List> comments = new ArrayList<>(); - private Set users = new LinkedHashSet<>(); - private Set reviewers = new LinkedHashSet<>(); - private Set repoLabels = new LinkedHashSet<>(); - private Set prLabels = new LinkedHashSet<>(); - private Set commitStatuses = new LinkedHashSet<>(); - private final List commits = new ArrayList<>(); - private final Set repositoryDirectories = new LinkedHashSet<>(); - private final Set repositoryFiles = new LinkedHashSet<>(); - private String repository = TEST_REPO; - private Boolean mergeable = Boolean.TRUE; - private boolean isDraft = false; - - private MockedContext(long pullRequest) { - this.pullRequest = pullRequest; - } - - public static MockedContext builder(long pullRequest) { - return new MockedContext(pullRequest); - } - - public MockedContext prFiles(String... filenames) { - prFiles.addAll(new HashSet<>(Arrays.asList(filenames))); - return this; - } - - public MockedContext comment(String comment, String author) { - Tuple2 commentTuple = Tuple2.of(comment, author); - this.comments.add(commentTuple); - return this; - } - - public MockedContext commit(MockedCommit commit) { - this.commits.add(commit); - return this; - } - - public MockedContext commit(String commitMessage) { - return commit(MockedCommit.commit(commitMessage)); - } - - /** - * Creates List with one empty GHContent as response to the specified `directories` - * Otherwise an `GHFileNotFoundException` is thrown. To override the default - * behavior you have to mock it before calling `mock` method. - */ - public MockedContext repoDirectories(String... directories) { - this.repositoryDirectories.addAll(Arrays.asList(directories)); - return this; - } - - public MockedContext repoFiles(String... files) { - this.repositoryFiles.addAll(Arrays.asList(files)); - return this; - } - - public MockedContext users(String... users) { - this.users.addAll(Arrays.asList(users)); - return this; - } - - public MockedContext reviewers(String... reviewers) { - this.reviewers.addAll(Arrays.asList(reviewers)); - return this; - } - - public MockedContext repoLabels(Set labels) { - this.repoLabels = labels; - return this; - } - - public MockedContext prLabels(String... labels) { - this.prLabels.addAll(Arrays.asList(labels)); - return this; - } - - public MockedContext mergeable(Boolean mergeable) { - this.mergeable = mergeable; - return this; - } - - public MockedContext repository(String repository) { - this.repository = repository; - return this; - } - - public MockedContext draft() { - this.isDraft = true; - return this; - } - - public MockedContext commitStatuses(String... commitStatuses) { - this.commitStatuses.addAll(Arrays.asList(commitStatuses)); - return this; - } - - public void mock(GitHubMockContext mocks) throws IOException { - AtomicLong id = new AtomicLong(0L); - - GHPullRequest pullRequest = mocks.pullRequest(this.pullRequest); - - List mockedFileDetails = new ArrayList<>(); - for (String filename : prFiles) { - GHPullRequestFileDetail mocked = Mockito.mock(GHPullRequestFileDetail.class); - Mockito.when(mocked.getFilename()).thenReturn(filename); - mockedFileDetails.add(mocked); - } - PagedSearchIterable fileDetails = GitHubAppMockito - .mockPagedIterable(mockedFileDetails.toArray(GHPullRequestFileDetail[]::new)); - Mockito.when(pullRequest.listFiles()).thenReturn(fileDetails); - - List mockedComments = new ArrayList<>(); - for (int i = 0; i < comments.size(); i++) { - Tuple2 commentTuple = comments.get(i); - - GHIssueComment comment = mocks.issueComment(i); - GHUser user = mocks.ghObject(GHUser.class, id.incrementAndGet()); - - Mockito.when(user.getLogin()).thenReturn(commentTuple.getItem2()); - Mockito.when(comment.getBody()).thenReturn(commentTuple.getItem1()); - Mockito.when(comment.getUser()).thenReturn(user); - - mockedComments.add(comment); - } - PagedSearchIterable comments = GitHubAppMockito - .mockPagedIterable(mockedComments.toArray(GHIssueComment[]::new)); - Mockito.when(pullRequest.listComments()).thenReturn(comments); - - List mockedCommits = new ArrayList<>(); - for (MockedCommit commit : commits) { - GHPullRequestCommitDetail mockCommitDetail = Mockito.mock(GHPullRequestCommitDetail.class); - GHPullRequestCommitDetail.Commit mockCommit = Mockito.mock(GHPullRequestCommitDetail.Commit.class); - Mockito.when(mockCommitDetail.getCommit()).thenReturn(mockCommit); - GHUser user = mocks.ghObject(GHUser.class, id.incrementAndGet()); - - Mockito.when(mockCommit.getMessage()).thenReturn(commit.message); - Mockito.when(mockCommitDetail.getSha()).thenReturn(commit.sha); - Mockito.when(user.getLogin()).thenReturn(commit.author); - mockedCommits.add(mockCommitDetail); - } - - PagedSearchIterable commitDetails = GitHubAppMockito - .mockPagedIterable(mockedCommits.toArray(GHPullRequestCommitDetail[]::new)); - Mockito.when(pullRequest.listCommits()).thenReturn(commitDetails); - - GHRepository repository = mocks.repository(this.repository); - - List mockedCommitStatuses = new ArrayList<>(); - for (String commitStatus : commitStatuses) { - GHCommitStatus mockedCommitStatus = Mockito.mock(GHCommitStatus.class); - Mockito.when(mockedCommitStatus.getContext()).thenReturn(commitStatus); - mockedCommitStatuses.add(mockedCommitStatus); - } - - PagedIterable commitStatusesIterable = GitHubAppMockito - .mockPagedIterable(mockedCommitStatuses.toArray(GHCommitStatus[]::new)); - Mockito.when(repository.listCommitStatuses(anyString())).thenReturn(commitStatusesIterable); - - Mockito.when(repository.getCollaboratorNames()).thenReturn(this.users); - - for (String collaborator : users) { - GHUser user = mocks.ghObject(GHUser.class, id.incrementAndGet()); - Mockito.when(mocks.installationClient(INSTALLATION_ID).getUser(collaborator)).thenReturn(user); - Mockito.when(user.getLogin()).thenReturn(collaborator); - } - - List requestedReviewers = new ArrayList<>(); - for (String reviewer : reviewers) { - GHUser user = mocks.ghObject(GHUser.class, id.incrementAndGet()); - Mockito.when(user.getLogin()).thenReturn(reviewer); - requestedReviewers.add(user); - } - Mockito.when(pullRequest.getRequestedReviewers()).thenReturn(requestedReviewers); - - // we are mocking the PagedIterable#toList method here - List ghLabels = new ArrayList<>(); - PagedIterable allGHLabels = Mockito.mock(PagedIterable.class); - Mockito.when(repository.listLabels()).thenReturn(allGHLabels); - for (String label : repoLabels) { - GHLabel ghLabel = Mockito.mock(GHLabel.class); - Mockito.when(ghLabel.getName()).thenReturn(label); - ghLabels.add(ghLabel); - } - Mockito.when(allGHLabels.toList()).thenReturn(ghLabels); - - Collection pullRequestLabels = new ArrayList<>(); - for (String label : this.prLabels) { - GHLabel ghLabel = Mockito.mock(GHLabel.class); - Mockito.when(ghLabel.getName()).thenReturn(label); - pullRequestLabels.add(ghLabel); - } - Mockito.when(pullRequest.getLabels()).thenReturn(pullRequestLabels); - - Mockito.when(pullRequest.getMergeable()).thenReturn(mergeable); - - Mockito.when(repository.getDirectoryContent(ArgumentMatchers.anyString())).thenAnswer(this::getDirectoryContentMock); - - Mockito.when(pullRequest.isDraft()).thenReturn(isDraft); - } - - private List getDirectoryContentMock(InvocationOnMock invocationOnMock) - throws HttpException, GHFileNotFoundException { - if (repositoryDirectories.contains((String) invocationOnMock.getArgument(0))) { - return List.of(new GHContent()); - } else if (repositoryFiles.contains((String) invocationOnMock.getArgument(0))) { - throw new HttpException(200, null, "https://api.github.com/repos/xyz", null); - } - throw new GHFileNotFoundException(); - } -} diff --git a/src/test/java/io/xstefank/wildfly/bot/utils/MockedGHPullRequest.java b/src/test/java/io/xstefank/wildfly/bot/utils/MockedGHPullRequest.java new file mode 100644 index 0000000..1fcaa8f --- /dev/null +++ b/src/test/java/io/xstefank/wildfly/bot/utils/MockedGHPullRequest.java @@ -0,0 +1,153 @@ +package io.xstefank.wildfly.bot.utils; + +import io.quarkiverse.githubapp.testing.GitHubAppMockito; +import io.quarkiverse.githubapp.testing.dsl.GitHubMockContext; +import io.smallrye.mutiny.tuples.Tuple2; +import org.kohsuke.github.GHIssueComment; +import org.kohsuke.github.GHLabel; +import org.kohsuke.github.GHPullRequest; +import org.kohsuke.github.GHPullRequestCommitDetail; +import org.kohsuke.github.GHPullRequestFileDetail; +import org.kohsuke.github.GHUser; +import org.kohsuke.github.PagedSearchIterable; +import org.mockito.Mockito; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashSet; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.atomic.AtomicLong; + +public class MockedGHPullRequest extends Mockable { + + private final long pullRequest; + private Set prFiles = new LinkedHashSet<>(); + private final List> comments = new ArrayList<>(); + private Set reviewers = new LinkedHashSet<>(); + private Set prLabels = new LinkedHashSet<>(); + private final List commits = new ArrayList<>(); + private Boolean mergeable = Boolean.TRUE; + private boolean isDraft = false; + + private MockedGHPullRequest(long pullRequest) { + this.pullRequest = pullRequest; + } + + public static MockedGHPullRequest builder(long pullRequest) { + return new MockedGHPullRequest(pullRequest); + } + + public MockedGHPullRequest files(String... filenames) { + prFiles.addAll(new HashSet<>(Arrays.asList(filenames))); + return this; + } + + public MockedGHPullRequest comment(String comment, String author) { + Tuple2 commentTuple = Tuple2.of(comment, author); + this.comments.add(commentTuple); + return this; + } + + public MockedGHPullRequest commit(MockedCommit commit) { + this.commits.add(commit); + return this; + } + + public MockedGHPullRequest commit(String commitMessage) { + return commit(MockedCommit.commit(commitMessage)); + } + + public MockedGHPullRequest reviewers(String... reviewers) { + this.reviewers.addAll(Arrays.asList(reviewers)); + return this; + } + + public MockedGHPullRequest labels(String... labels) { + this.prLabels.addAll(Arrays.asList(labels)); + return this; + } + + public MockedGHPullRequest mergeable(Boolean mergeable) { + this.mergeable = mergeable; + return this; + } + + public MockedGHPullRequest draft() { + this.isDraft = true; + return this; + } + + public AtomicLong mock(GitHubMockContext mocks, AtomicLong idGenerator) throws IOException { + GHPullRequest pullRequest = mocks.pullRequest(this.pullRequest); + + List mockedFileDetails = new ArrayList<>(); + for (String filename : prFiles) { + GHPullRequestFileDetail mocked = Mockito.mock(GHPullRequestFileDetail.class); + Mockito.when(mocked.getFilename()).thenReturn(filename); + mockedFileDetails.add(mocked); + } + PagedSearchIterable fileDetails = GitHubAppMockito + .mockPagedIterable(mockedFileDetails.toArray(GHPullRequestFileDetail[]::new)); + Mockito.when(pullRequest.listFiles()).thenReturn(fileDetails); + + List mockedComments = new ArrayList<>(); + for (int i = 0; i < comments.size(); i++) { + Tuple2 commentTuple = comments.get(i); + + GHIssueComment comment = mocks.issueComment(i); + GHUser user = mocks.ghObject(GHUser.class, idGenerator.incrementAndGet()); + + Mockito.when(user.getLogin()).thenReturn(commentTuple.getItem2()); + Mockito.when(comment.getBody()).thenReturn(commentTuple.getItem1()); + Mockito.when(comment.getUser()).thenReturn(user); + + mockedComments.add(comment); + } + PagedSearchIterable comments = GitHubAppMockito + .mockPagedIterable(mockedComments.toArray(GHIssueComment[]::new)); + Mockito.when(pullRequest.listComments()).thenReturn(comments); + + List mockedCommits = new ArrayList<>(); + for (MockedCommit commit : commits) { + GHPullRequestCommitDetail mockCommitDetail = Mockito.mock(GHPullRequestCommitDetail.class); + GHPullRequestCommitDetail.Commit mockCommit = Mockito.mock(GHPullRequestCommitDetail.Commit.class); + Mockito.when(mockCommitDetail.getCommit()).thenReturn(mockCommit); + GHUser user = mocks.ghObject(GHUser.class, idGenerator.incrementAndGet()); + + Mockito.when(mockCommit.getMessage()).thenReturn(commit.message); + Mockito.when(mockCommitDetail.getSha()).thenReturn(commit.sha); + Mockito.when(user.getLogin()).thenReturn(commit.author); + mockedCommits.add(mockCommitDetail); + } + + PagedSearchIterable commitDetails = GitHubAppMockito + .mockPagedIterable(mockedCommits.toArray(GHPullRequestCommitDetail[]::new)); + Mockito.when(pullRequest.listCommits()).thenReturn(commitDetails); + + List requestedReviewers = new ArrayList<>(); + for (String reviewer : reviewers) { + GHUser user = mocks.ghObject(GHUser.class, idGenerator.incrementAndGet()); + Mockito.when(user.getLogin()).thenReturn(reviewer); + requestedReviewers.add(user); + } + Mockito.when(pullRequest.getRequestedReviewers()).thenReturn(requestedReviewers); + + Collection pullRequestLabels = new ArrayList<>(); + for (String label : this.prLabels) { + GHLabel ghLabel = Mockito.mock(GHLabel.class); + Mockito.when(ghLabel.getName()).thenReturn(label); + pullRequestLabels.add(ghLabel); + } + Mockito.when(pullRequest.getLabels()).thenReturn(pullRequestLabels); + + Mockito.when(pullRequest.getMergeable()).thenReturn(mergeable); + + Mockito.when(pullRequest.isDraft()).thenReturn(isDraft); + + return idGenerator; + } +} diff --git a/src/test/java/io/xstefank/wildfly/bot/utils/MockedGHRepository.java b/src/test/java/io/xstefank/wildfly/bot/utils/MockedGHRepository.java new file mode 100644 index 0000000..c3d5bab --- /dev/null +++ b/src/test/java/io/xstefank/wildfly/bot/utils/MockedGHRepository.java @@ -0,0 +1,135 @@ +package io.xstefank.wildfly.bot.utils; + +import io.quarkiverse.githubapp.testing.GitHubAppMockito; +import io.quarkiverse.githubapp.testing.dsl.GitHubMockContext; +import org.kohsuke.github.GHCommitStatus; +import org.kohsuke.github.GHContent; +import org.kohsuke.github.GHFileNotFoundException; +import org.kohsuke.github.GHLabel; +import org.kohsuke.github.GHRepository; +import org.kohsuke.github.GHUser; +import org.kohsuke.github.HttpException; +import org.kohsuke.github.PagedIterable; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +import org.mockito.invocation.InvocationOnMock; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Set; +import java.util.concurrent.atomic.AtomicLong; + +import static io.xstefank.wildfly.bot.utils.TestConstants.INSTALLATION_ID; +import static io.xstefank.wildfly.bot.utils.TestConstants.TEST_REPO; +import static org.mockito.ArgumentMatchers.anyString; + +public class MockedGHRepository extends Mockable { + private final Set users = new LinkedHashSet<>(); + private Set labels = new LinkedHashSet<>(); + private final Set directories = new LinkedHashSet<>(); + private final Set files = new LinkedHashSet<>(); + private final Set commitStatuses = new LinkedHashSet<>(); + private String repository = TEST_REPO; + + private MockedGHRepository() { + } + + private MockedGHRepository(String repository) { + this.repository = repository; + } + + public static MockedGHRepository builder() { + return new MockedGHRepository(); + } + + public static MockedGHRepository builder(String repository) { + return new MockedGHRepository(repository); + } + + public MockedGHRepository repository(String repository) { + this.repository = repository; + return this; + } + + /** + * Creates List with one empty GHContent as response to the specified `directories` + * Otherwise an `GHFileNotFoundException` is thrown. To override the default + * behavior you have to mock it before calling `mock` method. + */ + public MockedGHRepository directories(String... directories) { + this.directories.addAll(Arrays.asList(directories)); + return this; + } + + public MockedGHRepository files(String... files) { + this.files.addAll(Arrays.asList(files)); + return this; + } + + public MockedGHRepository users(String... users) { + this.users.addAll(Arrays.asList(users)); + return this; + } + + public MockedGHRepository labels(Set labels) { + this.labels = labels; + return this; + } + + public MockedGHRepository commitStatuses(String... commitStatuses) { + this.commitStatuses.addAll(Arrays.asList(commitStatuses)); + return this; + } + + @Override + public AtomicLong mock(GitHubMockContext mocks, AtomicLong idGenerator) throws IOException { + GHRepository repository = mocks.repository(this.repository); + + List mockedCommitStatuses = new ArrayList<>(); + for (String commitStatus : commitStatuses) { + GHCommitStatus mockedCommitStatus = Mockito.mock(GHCommitStatus.class); + Mockito.when(mockedCommitStatus.getContext()).thenReturn(commitStatus); + mockedCommitStatuses.add(mockedCommitStatus); + } + + PagedIterable commitStatusesIterable = GitHubAppMockito + .mockPagedIterable(mockedCommitStatuses.toArray(GHCommitStatus[]::new)); + Mockito.when(repository.listCommitStatuses(anyString())).thenReturn(commitStatusesIterable); + + Mockito.when(repository.getCollaboratorNames()).thenReturn(this.users); + + for (String collaborator : users) { + GHUser user = mocks.ghObject(GHUser.class, idGenerator.incrementAndGet()); + Mockito.when(mocks.installationClient(INSTALLATION_ID).getUser(collaborator)).thenReturn(user); + Mockito.when(user.getLogin()).thenReturn(collaborator); + } + + // we are mocking the PagedIterable#toList method here + List ghLabels = new ArrayList<>(); + PagedIterable allGHLabels = Mockito.mock(PagedIterable.class); + Mockito.when(repository.listLabels()).thenReturn(allGHLabels); + for (String label : labels) { + GHLabel ghLabel = Mockito.mock(GHLabel.class); + Mockito.when(ghLabel.getName()).thenReturn(label); + ghLabels.add(ghLabel); + } + Mockito.when(allGHLabels.toList()).thenReturn(ghLabels); + + Mockito.when(repository.getDirectoryContent(ArgumentMatchers.anyString())).thenAnswer(this::getDirectoryContentMock); + + return idGenerator; + } + + private List getDirectoryContentMock(InvocationOnMock invocationOnMock) + throws HttpException, GHFileNotFoundException { + if (directories.contains((String) invocationOnMock.getArgument(0))) { + return List.of(new GHContent()); + } else if (files.contains((String) invocationOnMock.getArgument(0))) { + throw new HttpException(200, null, "https://api.github.com/repos/xyz", null); + } + throw new GHFileNotFoundException(); + } +} diff --git a/src/test/java/io/xstefank/wildfly/bot/utils/PullRequestJson.java b/src/test/java/io/xstefank/wildfly/bot/utils/PullRequestJson.java index efa64f4..36389d1 100644 --- a/src/test/java/io/xstefank/wildfly/bot/utils/PullRequestJson.java +++ b/src/test/java/io/xstefank/wildfly/bot/utils/PullRequestJson.java @@ -24,7 +24,7 @@ public class PullRequestJson { private static final String LOGIN = "login"; private static final String NUMBER = "number"; - private static JsonNode file; + private final JsonNode file; protected PullRequestJson(Builder jsonHandlerBuilder) { file = jsonHandlerBuilder.jsonFile; diff --git a/src/test/java/io/xstefank/wildfly/bot/utils/Util.java b/src/test/java/io/xstefank/wildfly/bot/utils/Util.java index 4a0742d..c99d973 100644 --- a/src/test/java/io/xstefank/wildfly/bot/utils/Util.java +++ b/src/test/java/io/xstefank/wildfly/bot/utils/Util.java @@ -19,13 +19,13 @@ public class Util { public static void mockRepo(GitHubMockSetupContext mocks, String wildflyConfigFile, PullRequestJson pullRequestJson) throws IOException { mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(wildflyConfigFile); - MockedContext context = MockedContext.builder(pullRequestJson.id()) + MockedGHPullRequest context = MockedGHPullRequest.builder(pullRequestJson.id()) .commit("[WFLY-123] Valid commit message"); context.mock(mocks); } public static void mockRepo(GitHubMockSetupContext mocks, String wildflyConfigFile, PullRequestJson pullRequestJson, - MockedContext context) throws IOException { + Mockable context) throws IOException { mocks.configFile(RuntimeConstants.CONFIG_FILE_NAME).fromString(wildflyConfigFile); context.mock(mocks); }