diff options
author | Igor Pashev <pashev.igor@gmail.com> | 2017-12-05 11:37:56 +0300 |
---|---|---|
committer | Igor Pashev <pashev.igor@gmail.com> | 2017-12-13 21:39:09 +0300 |
commit | 5fa1952a2e582f2c428584c5ccc1800132559df0 (patch) | |
tree | 7446f557a3bc5448fd48292a327eae7f0f3afa19 /src/test | |
parent | 92cf04a50b051cb6c96d0000eb8763797b239496 (diff) | |
download | bbprb-5fa1952a2e582f2c428584c5ccc1800132559df0.tar.gz |
Version 0.1.00.1.0
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/java/BitbucketBuildFilterTest.java | 308 | ||||
-rw-r--r-- | src/test/java/BitbucketBuildRepositoryTest.java | 354 |
2 files changed, 0 insertions, 662 deletions
diff --git a/src/test/java/BitbucketBuildFilterTest.java b/src/test/java/BitbucketBuildFilterTest.java deleted file mode 100644 index ad9892c..0000000 --- a/src/test/java/BitbucketBuildFilterTest.java +++ /dev/null @@ -1,308 +0,0 @@ - -import antlr.ANTLRException; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketBuildFilter; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketBuildTrigger; -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.Calendar; -import java.util.LinkedList; -import java.util.List; -import java.util.regex.Pattern; - -import jenkins.plugins.git.AbstractGitSCMSource; -import org.jvnet.hudson.test.JenkinsRule; -import org.jvnet.hudson.test.WithoutJenkins; - -import org.easymock.*; -import org.junit.Test; -import org.junit.Rule; -import static org.junit.Assert.*; - -/** - * Tests - */ -public class BitbucketBuildFilterTest { - - @Rule - public JenkinsRule jRule = new JenkinsRule(); - - @Test - @WithoutJenkins - public void mockTest() { - BitbucketCause cause = EasyMock.createMock(BitbucketCause.class); - EasyMock.expect(cause.getTargetBranch()).andReturn("mock").anyTimes(); - EasyMock.replay(cause); - for(Integer i : new Integer[] {1, 2, 3, 4, 5}) assertEquals("mock", cause.getTargetBranch()); - } - - @Test - @WithoutJenkins - public void anyFilter() { - BitbucketCause cause = EasyMock.createMock(BitbucketCause.class); - EasyMock.expect(cause.getTargetBranch()).andReturn("master").anyTimes(); - EasyMock.replay(cause); - - for(String f : new String[] {"", "*", "any"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertTrue(filter.approved(cause)); - } - - for(String f : new String[] {"foo", "bar", " baz "}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertFalse(filter.approved(cause)); - } - } - - @Test - @WithoutJenkins - public void onlyDestinationFilter() { - BitbucketCause cause = EasyMock.createMock(BitbucketCause.class); - EasyMock.expect(cause.getTargetBranch()).andReturn("master-branch").anyTimes(); - EasyMock.replay(cause); - - for(String f : new String[] {"master-branch", "r:^master", "r:branch$", " master-branch "}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertTrue(filter.approved(cause)); - } - - for(String f : new String[] {"develop", "feature-good-thing", "r:develop$"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertFalse(filter.approved(cause)); - } - } - - @Test - @WithoutJenkins - public void rxSourceDestCheck() { - for(String f : new String[] {"", "master", "r:master", "*"}) - assertFalse(Pattern.compile("(s:)|(d:)").matcher(f).find()); - - for(String f : new String[] {"s:master d:feature-master", "s:master d:r:^feature", "s:r:^master d:r:^feature"}) - assertTrue(Pattern.compile("(s:)|(d:)").matcher(f).find()); - } - - @Test - @WithoutJenkins - public void sourceAndDestFilter() { - BitbucketCause cause = EasyMock.createMock(BitbucketCause.class); - EasyMock.expect(cause.getTargetBranch()).andReturn("master").anyTimes(); - EasyMock.expect(cause.getSourceBranch()).andReturn("feature-for-master").anyTimes(); - EasyMock.replay(cause); - - for(String f : new String[] {"s:feature-for-master d:master", "s:r:^feature d:master", "s:feature-for-master d:r:^m", "s:r:^feature d:r:^master"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertTrue(filter.approved(cause)); - } - - for(String f : new String[] {"s:feature-for-master d:foo", "s:bar d:master", "s:foo d:bar"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertFalse(filter.approved(cause)); - } - } - - @Test - @WithoutJenkins - public void multipleSrcDestFilter() { - BitbucketCause cause = EasyMock.createMock(BitbucketCause.class); - EasyMock.expect(cause.getTargetBranch()).andReturn("master").anyTimes(); - EasyMock.expect(cause.getSourceBranch()).andReturn("feature-master").anyTimes(); - EasyMock.replay(cause); - - for(String f : new String[] {"s: d:", "s:r:^feature s:good-branch d:r:.*", "s:good-branch s:feature-master d:r:.*", "s: d:r:.*", "d:master d:foo d:bar s:"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertTrue(filter.approved(cause)); - } - - for(String f : new String[] {"d:ggg d:ooo d:333 s:feature-master", "s:111 s:222 s:333 d:master"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertFalse(filter.approved(cause)); - } - } - - @Test - @WithoutJenkins - public void sourceAndDestPartiallyFilter() { - BitbucketCause cause = EasyMock.createMock(BitbucketCause.class); - EasyMock.expect(cause.getTargetBranch()).andReturn("master").anyTimes(); - EasyMock.expect(cause.getSourceBranch()).andReturn("feature-master").anyTimes(); - EasyMock.replay(cause); - - for(String f : new String[] {"s:feature-master d:", "d:master s:"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertTrue(filter.approved(cause)); - } - - for(String f : new String[] {"s:feature-master", "d:master"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertFalse(filter.approved(cause)); - } - } - - @Test - @WithoutJenkins - public void authorFilter() { - BitbucketCause cause = EasyMock.createMock(BitbucketCause.class); - EasyMock.expect(cause.getTargetBranch()).andReturn("master").anyTimes(); - EasyMock.expect(cause.getSourceBranch()).andReturn("feature-master").anyTimes(); - EasyMock.expect(cause.getPullRequestAuthor()).andReturn("test").anyTimes(); - EasyMock.replay(cause); - - for(String f : new String[] {"a:test", "a:r:^test", "d: s: a:", "a:", "a:foo a:test"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertTrue(filter.approved(cause)); - } - - for(String f : new String[] {"s:feature-master", "d:master", "s:feature-master d: a:foo", "a:bar"}) { - BitbucketBuildFilter filter = BitbucketBuildFilter.instanceByString(f); - assertFalse(filter.approved(cause)); - } - } - - @Test - @WithoutJenkins - public void emptyGitSCMFilter() { - BitbucketCause cause = EasyMock.createMock(BitbucketCause.class); - EasyMock.expect(cause.getTargetBranch()).andReturn("master").anyTimes(); - EasyMock.replay(cause); - - assertTrue(BitbucketBuildFilter.filterFromGitSCMSource(null, "").isEmpty()); - assertEquals("default", BitbucketBuildFilter.filterFromGitSCMSource(null, "default")); - - assertTrue(BitbucketBuildFilter.instanceByString( - BitbucketBuildFilter.filterFromGitSCMSource(null, "")).approved(cause) - ); - } - - @Test - @WithoutJenkins - public void fromGitSCMFilter() { - AbstractGitSCMSource git = EasyMock.createMock(AbstractGitSCMSource.class); - EasyMock.expect(git.getIncludes()) - .andReturn("").times(1) - .andReturn("").times(1) - .andReturn("*/master */feature-branch").times(1) - .andReturn("*/master").anyTimes(); - EasyMock.replay(git); - - assertTrue(git.getIncludes().isEmpty()); - assertEquals("", BitbucketBuildFilter.filterFromGitSCMSource(git, "")); - assertEquals("d:master d:feature-branch", BitbucketBuildFilter.filterFromGitSCMSource(git, "")); - assertEquals("d:master", BitbucketBuildFilter.filterFromGitSCMSource(git, "")); - } - - @Test - @WithoutJenkins - public void filterPRComments() throws ANTLRException { - BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); - EasyMock.expect(builder.getTrigger()).andReturn(null).anyTimes(); - EasyMock.replay(builder); - - List<Pullrequest.Comment> comments = new LinkedList<Pullrequest.Comment>(); - 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<Pullrequest.Comment> 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<BitbucketRepository> 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<Pullrequest.Comment> comments = new LinkedList<Pullrequest.Comment>(); - 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<BitbucketRepository> 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<Pullrequest.Comment> comments = new LinkedList<Pullrequest.Comment>(); - 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)); - } -} diff --git a/src/test/java/BitbucketBuildRepositoryTest.java b/src/test/java/BitbucketBuildRepositoryTest.java deleted file mode 100644 index 42694a3..0000000 --- a/src/test/java/BitbucketBuildRepositoryTest.java +++ /dev/null @@ -1,354 +0,0 @@ - -import antlr.ANTLRException; - -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketBuildTrigger; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketPullRequestsBuilder; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.BitbucketRepository; -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.ApiClient; - -import bitbucketpullrequestbuilder.bitbucketpullrequestbuilder.bitbucket.Pullrequest; -import com.cloudbees.plugins.credentials.CredentialsProvider; -import com.cloudbees.plugins.credentials.CredentialsScope; -import com.cloudbees.plugins.credentials.CredentialsStore; -import com.cloudbees.plugins.credentials.domains.Domain; -import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; - -import com.google.common.base.Function; -import com.google.common.collect.Collections2; - -import java.io.UnsupportedEncodingException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; -import java.util.logging.Logger; -import org.easymock.*; -import org.junit.Test; -import static org.junit.Assert.*; -import org.junit.Rule; -import org.junit.Assert; -import org.jvnet.hudson.test.JenkinsRule; - -import jenkins.model.Jenkins; - -import org.apache.commons.codec.binary.Hex; -import org.apache.commons.httpclient.Credentials; -import org.apache.commons.httpclient.HttpClient; -import org.apache.commons.httpclient.HttpState; -import org.apache.commons.httpclient.UsernamePasswordCredentials; -import org.apache.commons.httpclient.auth.AuthScope; - - -interface ICredentialsInterceptor { - void assertCredentials(Credentials actual); -} - -/** - * Utility class for interceptor functionality - * @param <T> - */ -class HttpClientInterceptor<T extends ICredentialsInterceptor> extends HttpClient { - private static final Logger logger = Logger.getLogger(HttpClientInterceptor.class.getName()); - - class CredentialsInterceptor<T extends ICredentialsInterceptor> extends HttpState { - private final T interceptor; - public CredentialsInterceptor(T interceptor) { this.interceptor = interceptor; } - - @Override - public synchronized void setCredentials(AuthScope authscope, Credentials credentials) { - logger.fine("Inject setCredentials"); - super.setCredentials(authscope, credentials); - this.interceptor.assertCredentials(credentials); - throw new AssertionError(); - } - } - - private final T interceptor; - public HttpClientInterceptor(T interceptor) { this.interceptor = interceptor; } - - @Override - public synchronized HttpState getState() { return new CredentialsInterceptor(this.interceptor); } -} - -/** - * Utility class for credentials assertion - * Used with - * @author maxvodo - */ -class AssertCredentials implements ICredentialsInterceptor { - private static final Logger logger = Logger.getLogger(AssertCredentials.class.getName()); - - private final Credentials expected; - public AssertCredentials(Credentials expected) { this.expected = expected; } - - public void assertCredentials(Credentials actual) { - logger.fine("Assert credential"); - if (actual == null) assertTrue(this.expected == null); - else assertTrue(this.expected != null); - - if (actual instanceof UsernamePasswordCredentials) { - UsernamePasswordCredentials actual_ = (UsernamePasswordCredentials)actual, - expected_ = (UsernamePasswordCredentials)this.expected; - assertNotNull(expected_); - Assert.assertArrayEquals(new Object[] { - actual_.getUserName(), actual_.getPassword() - }, new Object[] { - expected_.getUserName(), expected_.getPassword() - }); - } - } -} - -/** - * Tests - */ -public class BitbucketBuildRepositoryTest { - - @Rule - public JenkinsRule jRule = new JenkinsRule(); - - @Test - public void repositorySimpleUserPasswordTest() throws Exception { - BitbucketBuildTrigger trigger = new BitbucketBuildTrigger( - "", "@hourly", - "JenkinsCID", - "foo", - "bar", - "", "", - "", true, - "", "", "", - true, - true, - false, BitbucketRepository.DEFAULT_COMMENT_TRIGGER - ); - - BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); - EasyMock.expect(builder.getTrigger()).andReturn(trigger).anyTimes(); - EasyMock.replay(builder); - - ApiClient.HttpClientFactory httpFactory = EasyMock.createNiceMock(ApiClient.HttpClientFactory.class); - EasyMock.expect(httpFactory.getInstanceHttpClient()).andReturn( - new HttpClientInterceptor(new AssertCredentials(new UsernamePasswordCredentials("foo", "bar"))) - ).anyTimes(); - EasyMock.replay(httpFactory); - - BitbucketRepository repo = new BitbucketRepository("", builder); - repo.init(httpFactory); - - try { repo.postPullRequestApproval("prId"); } catch(Error e) { assertTrue(e instanceof AssertionError); } - } - - @Test - public void repositoryCtorWithTriggerTest() throws Exception { - BitbucketBuildTrigger trigger = new BitbucketBuildTrigger( - "", "@hourly", - "JenkinsCID", - "foo", - "bar", - "", "", - "", true, - "", "", "", - true, - true, - false, BitbucketRepository.DEFAULT_COMMENT_TRIGGER - ); - - BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); - EasyMock.expect(builder.getTrigger()).andReturn(trigger).anyTimes(); - EasyMock.replay(builder); - - CredentialsStore store = CredentialsProvider.lookupStores(Jenkins.getInstance()).iterator().next(); - assertNotNull(store); - store.addCredentials(Domain.global(), new UsernamePasswordCredentialsImpl( - CredentialsScope.GLOBAL, "JenkinsCID", "description", "username", "password" - )); - - ApiClient.HttpClientFactory httpFactory = EasyMock.createNiceMock(ApiClient.HttpClientFactory.class); - EasyMock.expect(httpFactory.getInstanceHttpClient()).andReturn( - new HttpClientInterceptor(new AssertCredentials(new UsernamePasswordCredentials("username", "password"))) - ).anyTimes(); - EasyMock.replay(httpFactory); - - BitbucketRepository repo = new BitbucketRepository("", builder); - repo.init(httpFactory); - - try { repo.postPullRequestApproval("prId"); } catch(Error e) { assertTrue(e instanceof AssertionError); } - } - - class MD5HasherFunction implements Function<String, String> { - protected final MessageDigest MD5; - public MD5HasherFunction(MessageDigest md5) { this.MD5 = md5; } - public String apply(String f) { - try { return new String(Hex.encodeHex(MD5.digest(f.getBytes("UTF-8")))); } catch(UnsupportedEncodingException e) { } - return null; - } - } - - class SHA1HasherFunction implements Function<String, String> { - protected final MessageDigest SHA1; - public SHA1HasherFunction(MessageDigest sha1) { this.SHA1 = sha1; } - public String apply(String f) { - try { return new String(Hex.encodeHex(SHA1.digest(f.getBytes("UTF-8")))); } catch(UnsupportedEncodingException e) { } - return null; - } - } - - @Test - public void repositoryProjectIdTest() throws ANTLRException, NoSuchAlgorithmException, UnsupportedEncodingException { - BitbucketBuildTrigger trigger = new BitbucketBuildTrigger( - "", "@hourly", - "JenkinsCID", - "foo", - "bar", - "", "", - "", true, - "jenkins", "Jenkins", "", - true, - true, - false, BitbucketRepository.DEFAULT_COMMENT_TRIGGER - ); - - BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); - EasyMock.expect(builder.getTrigger()).andReturn(trigger).anyTimes(); - - final MessageDigest MD5 = MessageDigest.getInstance("MD5"); - - String[] projectIds = new String[] { - "one", - "Second project", - "Project abstract 1.1", - "Good project, careated at " + (new java.util.Date()).toString(), - }; - - Collection<String> hashedProjectIdsCollection = Collections2.transform(Arrays.asList(projectIds), new MD5HasherFunction(MD5)); - String[] hashedPojectIds = hashedProjectIdsCollection.toArray(new String[hashedProjectIdsCollection.size()]); - - for(String projectId : hashedPojectIds) { - EasyMock.expect(builder.getProjectId()).andReturn(projectId).times(1); - } - EasyMock.replay(builder); - - BitbucketRepository repo = new BitbucketRepository("", builder); - repo.init(); - - for(String projectId : projectIds) { - String hashMD5 = new String(Hex.encodeHex(MD5.digest(projectId.getBytes("UTF-8")))); - String buildStatusKey = repo.getClient().buildStatusKey(builder.getProjectId()); - - assertTrue(buildStatusKey.length() <= ApiClient.MAX_KEY_SIZE_BB_API); - assertEquals(buildStatusKey, "jenkins-" + hashMD5); - } - } - - @Test - public void triggerLongCIKeyTest() throws ANTLRException, NoSuchAlgorithmException { - BitbucketBuildTrigger trigger = new BitbucketBuildTrigger( - "", "@hourly", - "JenkinsCID", - "foo", - "bar", - "", "", - "", true, - "jenkins-too-long-ci-key", "Jenkins", "", - true, - true, - false, BitbucketRepository.DEFAULT_COMMENT_TRIGGER - ); - - final MessageDigest MD5 = MessageDigest.getInstance("MD5"); - final MessageDigest SHA1 = MessageDigest.getInstance("SHA1"); - - BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); - EasyMock.expect(builder.getTrigger()).andReturn(trigger).anyTimes(); - EasyMock.expect(builder.getProjectId()).andReturn((new MD5HasherFunction(MD5)).apply("projectId")).anyTimes(); - EasyMock.replay(builder); - - BitbucketRepository repo = new BitbucketRepository("", builder); - repo.init(); - - String buildStatusKey = repo.getClient().buildStatusKey(builder.getProjectId()); - assertTrue(buildStatusKey.length() <= ApiClient.MAX_KEY_SIZE_BB_API); - assertFalse(buildStatusKey.startsWith("jenkins-")); - assertEquals((new SHA1HasherFunction(SHA1)).apply("jenkins-too-long-ci-key" + "-" + builder.getProjectId()), buildStatusKey); - } - - @Test - public void getTargetPullRequestsWithNullDestinationCommit() throws Exception { - // arrange - - // setup mock BitbucketBuildTrigger - final BitbucketBuildTrigger trigger = EasyMock.createMock(BitbucketBuildTrigger.class); - EasyMock.expect(trigger.getCiSkipPhrases()).andReturn(""); - EasyMock.expect(trigger.getBranchesFilterBySCMIncludes()).andReturn(false); - EasyMock.expect(trigger.getBranchesFilter()).andReturn(""); - EasyMock.replay(trigger); - - // setup mock BitbucketPullRequestsBuilder - final BitbucketPullRequestsBuilder builder = EasyMock.createMock(BitbucketPullRequestsBuilder.class); - EasyMock.expect(builder.getTrigger()).andReturn(trigger).anyTimes(); - EasyMock.expect(builder.getProjectId()).andReturn("").anyTimes(); - EasyMock.replay(builder); - - // setup PRs to return from mock ApiClient - final Pullrequest pullRequest = new Pullrequest(); - - final Pullrequest.Repository sourceRepo = new Pullrequest.Repository(); - sourceRepo.setFullName("Owner/Name"); - - final Pullrequest.Repository destRepo = new Pullrequest.Repository(); - destRepo.setFullName("Owner/Name"); - - final Pullrequest.Branch sourceBranch = new Pullrequest.Branch(); - sourceBranch.setName("Name"); - - final Pullrequest.Branch destBranch = new Pullrequest.Branch(); - destBranch.setName("Name"); - - final Pullrequest.Commit sourceCommit = new Pullrequest.Commit(); - sourceCommit.setHash("Hash"); - - final Pullrequest.Commit destCommit = null; // the crux of the test - - final Pullrequest.Revision sourceRevision = new Pullrequest.Revision(); - sourceRevision.setBranch(sourceBranch); - sourceRevision.setRepository(sourceRepo); - sourceRevision.setCommit(sourceCommit); - - final Pullrequest.Revision destRevision = new Pullrequest.Revision(); - destRevision.setBranch(destBranch); - destRevision.setRepository(destRepo); - destRevision.setCommit(destCommit); - - final Pullrequest.Author author = new Pullrequest.Author(); - author.setDisplayName("DisplayName"); - author.setUsername("Username"); - - pullRequest.setSource(sourceRevision); - pullRequest.setDestination(destRevision); - pullRequest.setId("Id"); - pullRequest.setTitle("Title"); - pullRequest.setState("OPEN"); - pullRequest.setAutohor(author); - - final List<Pullrequest> pullRequests = new ArrayList<>(Arrays.asList(pullRequest)); - - // setup mock ApiClient - final ApiClient client = EasyMock.createNiceMock(ApiClient.class); - EasyMock.expect(client.getPullRequests()).andReturn(pullRequests); - EasyMock.replay(client); - - // setup SUT - final BitbucketRepository repo = new BitbucketRepository("", builder); - - // act - repo.init(client); - - // assert - Collection<Pullrequest> targetPullRequests = repo.getTargetPullRequests(); - - assertEquals(pullRequests.size(), targetPullRequests.size()); - assertEquals(pullRequest, targetPullRequests.iterator().next()); - } -} |