Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.23.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public StashPullRequestsBuilder(

public void run() {
logger.info(format("Build Start (%s).", project.getName()));
this.repository.init();
Collection<StashPullRequestResponseValue> targetPullRequests =
this.repository.getTargetPullRequests();
this.repository.addFutureBuildTasks(targetPullRequests);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,25 @@ public class StashRepository {
private StashApiClient client;

public StashRepository(@Nonnull Job<?, ?> job, @Nonnull StashBuildTrigger trigger) {
this(job, trigger, null);
this(job, trigger, makeStashApiClient(trigger));
}

// For unit tests only
// Visible for unit tests
StashRepository(
@Nonnull Job<?, ?> job, @Nonnull StashBuildTrigger trigger, StashApiClient client) {
this.job = job;
this.trigger = trigger;
this.client = client;
}

public void init() {
client =
new StashApiClient(
trigger.getStashHost(),
trigger.getUsername(),
trigger.getPassword(),
trigger.getProjectCode(),
trigger.getRepositoryName(),
trigger.isIgnoreSsl());
private static StashApiClient makeStashApiClient(StashBuildTrigger trigger) {
return new StashApiClient(
trigger.getStashHost(),
trigger.getUsername(),
trigger.getPassword(),
trigger.getProjectCode(),
trigger.getRepositoryName(),
trigger.isIgnoreSsl());
}

public Collection<StashPullRequestResponseValue> getTargetPullRequests() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
package stashpullrequestbuilder.stashpullrequestbuilder;

import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static java.lang.String.format;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.contains;
Expand All @@ -15,6 +23,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import hudson.model.BooleanParameterDefinition;
import hudson.model.FileParameterDefinition;
import hudson.model.FreeStyleProject;
Expand Down Expand Up @@ -54,6 +63,7 @@ public class StashRepositoryTest {

@Rule public JenkinsRule jenkinsRule = new JenkinsRule();
@Rule public MockitoRule rule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS);
@Rule public WireMockRule wireMockRule = new WireMockRule(wireMockConfig().dynamicPort());

private StashRepository stashRepository;

Expand Down Expand Up @@ -495,4 +505,27 @@ public void addFutureBuildTasks_skips_scheduling_build_if_getPullRequestComments

assertThat(Jenkins.getInstance().getQueue().getItems(), is(emptyArray()));
}

@Test
public void constructor_initializes_StashApiClient() throws Exception {
String projectName = "PROJ";
String repositoryName = "Repo";

when(trigger.getStashHost()).thenReturn(wireMockRule.baseUrl());
when(trigger.getUsername()).thenReturn("User");
when(trigger.getPassword()).thenReturn("Password");
when(trigger.getProjectCode()).thenReturn(projectName);
when(trigger.getRepositoryName()).thenReturn(repositoryName);
when(trigger.isIgnoreSsl()).thenReturn(false);

stubFor(
get(format(
"/rest/api/1.0/projects/%s/repos/%s/pull-requests?start=0",
projectName, repositoryName))
.willReturn(okJson("{\"isLastPage\": true, \"values\": []}")));

StashRepository newStashRepository = new StashRepository(project, trigger);
assertThat(newStashRepository.getTargetPullRequests(), is(empty()));
verify(getRequestedFor(anyUrl()));
}
}