From 96ab7a75f14d9990f3c8f1255f9790c496a64473 Mon Sep 17 00:00:00 2001 From: Maxim Epishchev Date: Tue, 26 Jan 2016 19:06:59 +0300 Subject: Bugfixes issue for continuously rebuilding PR If available TTP (aka "test this please") comment Jenkins PR builder continuously rebuilding PR. Now Jenkins post specific build comment. If you want to rebuild already rebuilded PR, post new TTP comment. --- src/test/java/BitbucketBuildFilterTest.java | 141 ++++++++++++++++++++++++++++ 1 file changed, 141 insertions(+) (limited to 'src/test/java') diff --git a/src/test/java/BitbucketBuildFilterTest.java b/src/test/java/BitbucketBuildFilterTest.java index 0765a4a..0fabfe4 100644 --- a/src/test/java/BitbucketBuildFilterTest.java +++ b/src/test/java/BitbucketBuildFilterTest.java @@ -6,6 +6,15 @@ import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketBuildFilter; import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketCause; +import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketPullRequestsBuilder; +import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketRepository; +import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.ApiClient; +import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; import java.util.regex.Pattern; import jenkins.plugins.git.AbstractGitSCMSource; import org.easymock.*; @@ -167,4 +176,136 @@ public class BitbucketBuildFilterTest { assertEquals("d:master d:feature-branch", BitbucketBuildFilter.FilterFromGitSCMSource(git, "")); assertEquals("d:master", BitbucketBuildFilter.FilterFromGitSCMSource(git, "")); } + + @Test + @WithoutJenkins + public void filterPRComments() { + BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); + EasyMock.expect(builder.getTrigger()).andReturn(null).anyTimes(); + EasyMock.replay(builder); + + List comments = new LinkedList(); + for(String commentContent : new String[] { + "check", + "", + "Hello from mock", + "Jenkins: test this please", + "TTP build flag [bid: #jenkins-902f259e962ff16100843123480a0970]", + "check", + "", + "Hello from mock", + "Jenkins: test this please", + "TTP build flag [bid: #jenkins-902f259e962ff16100843123480a0970]", + "TTP build flag [bid: #jenkins-902f259e962ff16100843123480a0970 #jenkins-foo]", + "TTP build flag [bid: #jenkins-902f259e962ff16100843123480a0970 #jenkins-foo #jenkins-bar]", + }) { + Pullrequest.Comment comment = EasyMock.createNiceMock(Pullrequest.Comment.class); + EasyMock.expect(comment.getContent()).andReturn(commentContent).anyTimes(); + EasyMock.expect(comment.getId()).andReturn(new java.sql.Timestamp(Calendar.getInstance().getTime().getTime()).getNanos()).anyTimes(); + EasyMock.replay(comment); + comments.add(comment); + } + + // Check twice + assertEquals("check", comments.get(0).getContent()); + assertEquals("check", comments.get(0).getContent()); + + assertEquals("Hello from mock", comments.get(2).getContent()); + + BitbucketRepository repo = new BitbucketRepository("", builder); + repo.init(EasyMock.createNiceMock(ApiClient.class)); + + List filteredComments = repo.filterPullRequestComments(comments); + + assertTrue(filteredComments.size() == 4); + assertEquals("Jenkins: test this please", filteredComments.get(filteredComments.size() - 1).getContent()); + } + + @Test + @WithoutJenkins + public void checkHashMyBuildTagTrue() { + BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); + EasyMock.expect(builder.getTrigger()).andReturn(null).anyTimes(); + EasyMock.replay(builder); + + IMockBuilder repoBuilder = EasyMock.partialMockBuilder(BitbucketRepository.class); + repoBuilder.addMockedMethod("getMyBuildTag"); + BitbucketRepository repo = repoBuilder.createMock(); + EasyMock.expect(repo.getMyBuildTag(EasyMock.anyString())).andReturn("#jenkins-902f259e962ff16100843123480a0970").anyTimes(); + EasyMock.replay(repo); + + List comments = new LinkedList(); + for(String commentContent : new String[] { + "TTP build flag [bid: #jenkins-902f259e962ff16100843123480a0970]", + "TTP build flag [bid: #jenkins-902f259e962ff16100843123480a0970 #jenkins-foo]", + "TTP build flag [bid: #jenkins-902f259e962ff16100843123480a0970 #jenkins-foo #jenkins-bar]", + "TTP build flag ```[bid: #jenkins-902f259e962ff16100843123480a0970 #jenkins-foo #jenkins-bar]```", + }) { + Pullrequest.Comment comment = EasyMock.createNiceMock(Pullrequest.Comment.class); + EasyMock.expect(comment.getContent()).andReturn(commentContent).anyTimes(); + EasyMock.expect(comment.getId()).andReturn(new java.sql.Timestamp(Calendar.getInstance().getTime().getTime()).getNanos()).anyTimes(); + EasyMock.replay(comment); + comments.add(comment); + } + + String myBuildKey = "902f259e962ff16100843123480a0970"; + for(Pullrequest.Comment comment : comments) + assertTrue(repo.hasMyBuildTagInTTPComment(comment.getContent(), myBuildKey)); + } + + @Test + @WithoutJenkins + public void checkHashMyBuildTagFalse() { + BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); + EasyMock.expect(builder.getTrigger()).andReturn(null).anyTimes(); + EasyMock.replay(builder); + + IMockBuilder repoBuilder = EasyMock.partialMockBuilder(BitbucketRepository.class); + repoBuilder.addMockedMethod("getMyBuildTag"); + BitbucketRepository repo = repoBuilder.createMock(); + EasyMock.expect(repo.getMyBuildTag(EasyMock.anyString())).andReturn("#jenkins-902f259e962ff16100843123480a0970").anyTimes(); + EasyMock.replay(repo); + + List comments = new LinkedList(); + for(String commentContent : new String[] { + "check", + "", + "Hello from mock", + "Jenkins: test this please", + "TTP build flag [bid: #jenkins]", + "TTP build flag [bid: #jenkins-foo]", + "TTP build flag [bid: #jenkins-foo #jenkins-bar]", + "TTP build flag ```[bid: #jenkins-foo #jenkins-bar]```", + }) { + Pullrequest.Comment comment = EasyMock.createNiceMock(Pullrequest.Comment.class); + EasyMock.expect(comment.getContent()).andReturn(commentContent).anyTimes(); + EasyMock.expect(comment.getId()).andReturn(new java.sql.Timestamp(Calendar.getInstance().getTime().getTime()).getNanos()).anyTimes(); + EasyMock.replay(comment); + comments.add(comment); + } + + String myBuildKey = "902f259e962ff16100843123480a0970"; + for(Pullrequest.Comment comment : comments) + assertFalse(repo.hasMyBuildTagInTTPComment(comment.getContent(), myBuildKey)); + } + + //@Test + @WithoutJenkins + public void ttpCommentTest() { + ApiClient client = EasyMock.createNiceMock(ApiClient.class); + Collection> prs = new LinkedList>(); + + prs.add(Arrays.asList(new Pullrequest[] { + new Pullrequest() + })); + + for(List pr : prs) EasyMock.expect(client.getPullRequests()).andReturn(pr).times(1); + BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); + EasyMock.replay(client, builder); + + BitbucketRepository repo = new BitbucketRepository("", builder); + repo.init(client); + + Collection targetPRs = repo.getTargetPullRequests(); + } } -- cgit v1.2.3