Skip to content
Permalink
Browse files
JENKINS-40963 Job DSL integration
test refactorings, namely  use of matchers
  • Loading branch information
prospero238 committed Jan 11, 2017
1 parent 5585a30 commit 0e69df4193c2c8144194c8a9e417aa04249d8158
@@ -37,7 +37,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.core.IsCollectionContaining.hasItem;
import static org.jenkinsci.plugins.liquibase.matchers.BuildResultMatcher.isSuccessfulBuild;
import static org.jenkinsci.plugins.liquibase.matchers.BuildResultMatcher.isSuccessful;
import static org.jenkinsci.plugins.liquibase.matchers.ProjectWithName.isProjectWithName;
import static org.junit.Assert.assertThat;

@@ -68,7 +68,7 @@ public void should_spawn_liquibase_project() throws IOException, ExecutionExcept

LOG.debug("build log:{}", formatLogForLog(build.getLog(1000)));

assertThat(build, isSuccessfulBuild());
assertThat(build, isSuccessful());

List<AbstractProject> projects = jenkinsRule.getInstance().getItems(AbstractProject.class);

@@ -104,7 +104,7 @@ public void should_build_generated_update_project_successfully()

logBuildLog(build);

assertThat(build, isSuccessfulBuild());
assertThat(build, isSuccessful());

}

@@ -9,7 +9,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.jenkinsci.plugins.liquibase.integration.IsChangeSetDetail.hasId;
import static org.jenkinsci.plugins.liquibase.matchers.IsChangeSetDetail.hasId;
import static org.junit.Assert.assertThat;

public class ExecutedChangesetActionTest {
@@ -2,7 +2,6 @@

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import liquibase.Liquibase;
import liquibase.database.Database;
import liquibase.database.DatabaseConnection;
@@ -29,6 +28,7 @@
import org.jenkinsci.plugins.liquibase.evaluator.ChangeSetDetail;
import org.jenkinsci.plugins.liquibase.evaluator.ChangesetEvaluator;
import org.jenkinsci.plugins.liquibase.evaluator.ExecutedChangesetAction;
import org.jenkinsci.plugins.liquibase.matchers.IsChangeSetDetail;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Ignore;
@@ -42,8 +42,11 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
import static org.jenkinsci.plugins.liquibase.integration.IsChangeSetDetail.hasId;
import static org.jenkinsci.plugins.liquibase.integration.IsChangeSetDetail.isChangeSetDetail;
import static org.jenkinsci.plugins.liquibase.matchers.BuildResultMatcher.isFailure;
import static org.jenkinsci.plugins.liquibase.matchers.BuildResultMatcher.isSuccessful;
import static org.jenkinsci.plugins.liquibase.matchers.BuildResultMatcher.isUnstable;
import static org.jenkinsci.plugins.liquibase.matchers.IsChangeSetDetail.hasId;
import static org.jenkinsci.plugins.liquibase.matchers.IsChangeSetDetail.isChangeSetDetail;
import static org.junit.Assert.assertThat;

public class ChangesetEvaluatorBuildResultTest {
@@ -67,7 +70,7 @@ public void should_report_success_with_error_free_changeset()
throws IOException, ExecutionException, InterruptedException {

FreeStyleBuild freeStyleBuild = createAndBuildErrorFreeProject();
assertThat(freeStyleBuild.getResult(), is(Result.SUCCESS));
assertThat(freeStyleBuild, isSuccessful());
}

@Test
@@ -76,14 +79,14 @@ public void should_indicate_unstable_build_when_changeset_has_error()
File changesetFileWithError = LiquibaseTestUtil.createChangesetFileWithError(temporaryFolder);
FreeStyleProject project = createProjectWithChangelogFile(changesetFileWithError);
FreeStyleBuild build = launchBuildForProject(project);
assertThat(build.getResult(), is(Result.UNSTABLE));
assertThat(build, isUnstable());
}

@Test
public void should_indicate_success_with_yaml_formatted_changeset()
throws IOException, ExecutionException, InterruptedException {
FreeStyleBuild build = createAndBuildLiquibaseProject("/example-changesets/yaml-changeset-sunnyday.yml");
assertThat(build.getResult(), is(Result.SUCCESS));
assertThat(build, isSuccessful());
}

@Test
@@ -106,7 +109,8 @@ public void should_mark_liquibase_setup_problem_as_failure()

project.getBuildersList().add(evaluator);
FreeStyleBuild build = launchBuildForProject(project);
assertThat(build.getResult(), is(Result.FAILURE));
assertThat(build , isFailure());

}


@@ -126,7 +130,7 @@ public void should_use_liquibase_defaults_file() throws InterruptedException, Ex
evaluator.setLiquibasePropertiesPath(LiquibaseTestUtil.extractFilenameFromResourcePath(LIQUIBASE_PROPERTIES));
project.getBuildersList().add(evaluator);
FreeStyleBuild build = launchBuildForProject(project);
assertThat(build.getResult(), is(Result.SUCCESS));
assertThat(build, isSuccessful());
}


@@ -151,7 +155,7 @@ public void should_locate_change_log_using_basepath()

FreeStyleBuild build = launchBuildForProject(project);

assertThat(build.getResult(), is(Result.SUCCESS));
assertThat(build, isSuccessful());

ExecutedChangesetAction action = build.getAction(ExecutedChangesetAction.class);

@@ -163,7 +167,7 @@ public void should_locate_change_log_using_basepath()
public void should_handle_json_changesets_successfully()
throws InterruptedException, ExecutionException, IOException {
FreeStyleBuild build = createAndBuildLiquibaseProject("/example-changesets/json-changeset-sunnyday.json");
assertThat(build.getResult(), is(Result.SUCCESS));
assertThat(build, isSuccessful());
}

@Test
@@ -186,7 +190,8 @@ public void should_handle_changelog_with_include() throws IOException, Execution
LiquibaseTestUtil.createFileFromResource(rootDirectory, LiquibaseTestUtil.SUNNY_DAY_CHANGESET_XML);
FreeStyleBuild build = launchBuildForProject(project);

assertThat(build.getResult(), is(Result.SUCCESS));
assertThat(build, isSuccessful());

ExecutedChangesetAction action = build.getAction(ExecutedChangesetAction.class);

assertThat(action.getChangeSetDetails(), containsSunnyDayChangesetDetails());
@@ -211,7 +216,7 @@ public void should_handle_include_with_absolute_path() throws IOException, Execu

FreeStyleBuild build = launchBuildForProject(project);

assertThat(build.getResult(), is(Result.SUCCESS));
assertThat(build, isSuccessful());
ExecutedChangesetAction action = build.getAction(ExecutedChangesetAction.class);
assertThat(action.getChangeSetDetails(), containsSunnyDayChangesetDetails());
}
@@ -234,7 +239,7 @@ public void should_handle_include_all() throws IOException, ExecutionException,
project.setCustomWorkspace(includedDir.getParent());
FreeStyleBuild build = launchBuildForProject(project);

assertThat(build.getResult(), is(Result.SUCCESS));
assertThat(build, isSuccessful());
List<ChangeSetDetail> changeSetDetails = build.getAction(ExecutedChangesetAction.class).getChangeSetDetails();

assertThat(changeSetDetails, containsSunnyDayChangesetDetails());
@@ -289,7 +294,7 @@ public void should_handle_include_all_relative() throws IOException, ExecutionEx
FreeStyleProject project = createProjectWithChangelogFile(rootChangeset);
FreeStyleBuild build = launchBuildForProject(project);

assertThat(build.getResult(), is(Result.SUCCESS));
assertThat(build, isSuccessful());
List<ChangeSetDetail> changeSetDetails = build.getAction(ExecutedChangesetAction.class).getChangeSetDetails();

assertThat(changeSetDetails, containsSunnyDayChangesetDetails());
@@ -316,7 +321,7 @@ protected FreeStyleBuild createAndBuildErrorFreeProject()
* @return
*/
private static Matcher<Iterable<? extends ChangeSetDetail>> containsSunnyDayChangesetDetails() {
return contains(
return contains(
isChangeSetDetail(new ChangeSetDetail.Builder().withAuthor("keith").withId("create-table")
.withComments("This is a simple create table")
.withSuccessfullyExecuted(true).build()),
@@ -26,8 +26,8 @@
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.jenkinsci.plugins.liquibase.integration.InputCheckedMatcher.isChecked;
import static org.jenkinsci.plugins.liquibase.integration.InputCheckedMatcher.isNotChecked;
import static org.jenkinsci.plugins.liquibase.matchers.InputCheckedMatcher.isChecked;
import static org.jenkinsci.plugins.liquibase.matchers.InputCheckedMatcher.isNotChecked;
import static org.junit.Assert.assertThat;

public class DriverSelectionFormTest {
@@ -1,6 +1,5 @@
package org.jenkinsci.plugins.liquibase.integration;

import hudson.model.Result;
import liquibase.exception.LiquibaseException;

import java.io.File;
@@ -30,10 +29,10 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.jenkinsci.plugins.liquibase.matchers.BuildResultMatcher.isSuccessful;
import static org.junit.Assert.assertThat;


@@ -62,7 +61,8 @@ public void should_allow_friendly_update_name() throws ExecutionException, Inter
String updateScript = generateUpdatePipelineScript(workspace);
job.setDefinition(new CpsFlowDefinition(updateScript));
WorkflowRun workflowRun = job.scheduleBuild2(0).get();
assertThat(workflowRun.getResult(), is(Result.SUCCESS));
assertThat(workflowRun, isSuccessful());

}

@Test
@@ -72,7 +72,7 @@ public void should_allow_dbdoc_generation() throws IOException, ExecutionExcepti
job.setDefinition(new CpsFlowDefinition(script));
WorkflowRun workflowRun = job.scheduleBuild2(0).get();

assertThat(workflowRun.getResult(), is(Result.SUCCESS));
assertThat(workflowRun, isSuccessful());

File reportIndex = new File(new File(workspace, "doc"), "index.html");

@@ -88,7 +88,7 @@ public void should_allow_changelog_parameters() throws IOException, ExecutionExc
LiquibaseTestUtil.createFileFromResource(workspace, "/example-changesets/with-changelog-property.xml");
WorkflowRun workflowRun = job.scheduleBuild2(0).get();

assertThat(workflowRun.getResult(), is(Result.SUCCESS));
assertThat(workflowRun, isSuccessful());
ExecutedChangesetAction action = workflowRun.getAction(ExecutedChangesetAction.class);
assertThat(action, notNullValue());
List<ChangeSetDetail> changeSetDetails = action.getChangeSetDetails();
@@ -108,7 +108,7 @@ public void should_allow_rollback_dsl()
CpsFlowDefinition cpsFlowDefinition = new CpsFlowDefinition(script);
job.setDefinition(cpsFlowDefinition);
WorkflowRun run = job.scheduleBuild2(0).get();
assertThat(run.getResult(), is(Result.SUCCESS));
assertThat(run, isSuccessful());
RolledbackChangesetAction action = run.getAction(RolledbackChangesetAction.class);
assertThat(action.getRolledbackChangesets().size(), not(0));
}
@@ -2,7 +2,6 @@

import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import liquibase.exception.LiquibaseException;

import java.io.File;
@@ -25,10 +24,11 @@
import org.slf4j.LoggerFactory;

import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsCollectionContaining.hasItems;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.jenkinsci.plugins.liquibase.integration.IsChangeSetDetail.hasId;
import static org.jenkinsci.plugins.liquibase.matchers.BuildResultMatcher.isSuccessful;
import static org.jenkinsci.plugins.liquibase.matchers.BuildResultMatcher.isUnstable;
import static org.jenkinsci.plugins.liquibase.matchers.IsChangeSetDetail.hasId;
import static org.junit.Assert.assertThat;

public class RollbackResultTest {
@@ -68,7 +68,7 @@ public void should_report_success_with_successful_rollbacks()
buildStep.setNumberOfChangesetsToRollback(String.valueOf(numberOfChangesetsToRollback));
RolledbackChangesetAction action = launchBuild(buildStep);

assertThat(action.getBuild().getResult(), is(Result.SUCCESS));
assertThat(action.getBuild(), isSuccessful());
assertThat(action.getRolledbackChangesets(), hasSize(numberOfChangesetsToRollback));
}

@@ -84,7 +84,8 @@ public void should_rollback_using_tag_sucessfully()

RolledbackChangesetAction action = launchBuild(rollbackBuildStep);

assertThat(action.getBuild().getResult(), is(Result.SUCCESS));

assertThat(action.getBuild(), isSuccessful());
int amountOfChangesetsExistingAfterTag = 2;
assertThat(action.getRolledbackChangesets(), hasSize(amountOfChangesetsExistingAfterTag));
}
@@ -125,7 +126,8 @@ public void should_indicate_rollback_error_as_unstable() throws IOException, SQL

RolledbackChangesetAction resultAction = launchBuild(buildStep);
assertThat(resultAction, notNullValue());
assertThat(resultAction.getBuild().getResult(), is(Result.UNSTABLE));
assertThat(resultAction.getBuild(), isUnstable());

}

protected static RollbackBuilder createBaseBuildStep(RollbackBuilder.RollbackStrategy rollbackStrategy,
@@ -1,40 +1,43 @@
package org.jenkinsci.plugins.liquibase.matchers;

import hudson.model.FreeStyleBuild;
import hudson.model.Result;
import hudson.model.Run;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

public class BuildResultMatcher extends TypeSafeMatcher<FreeStyleBuild> {
public class BuildResultMatcher extends TypeSafeMatcher<Run> {

private Result expectedResult;


public BuildResultMatcher(Result expectedResult) {
this.expectedResult = expectedResult;

}

public static BuildResultMatcher isSuccessfulBuild() {
public static BuildResultMatcher isSuccessful() {
return new BuildResultMatcher(Result.SUCCESS);
}


public static BuildResultMatcher isUnstable() {
return new BuildResultMatcher(Result.UNSTABLE);
}
public static BuildResultMatcher isFailure() {
return new BuildResultMatcher(Result.FAILURE);
}

@Override
protected boolean matchesSafely(FreeStyleBuild item) {
protected boolean matchesSafely(Run item) {
return item.getResult().equals(expectedResult);
}

@Override
protected void describeMismatchSafely(FreeStyleBuild item, Description mismatchDescription) {
mismatchDescription.appendText("build whose result was ").appendText(item.getResult().toString());
protected void describeMismatchSafely(Run item, Description mismatchDescription) {
mismatchDescription.appendText("build whose result was ").appendValue(item.getResult().toString());
}

@Override
public void describeTo(Description description) {
description.appendText("build whose result is ").appendText(expectedResult.toString());
description.appendText("build whose result is ").appendValue(expectedResult.toString());
}

}
@@ -1,4 +1,4 @@
package org.jenkinsci.plugins.liquibase.integration;
package org.jenkinsci.plugins.liquibase.matchers;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
@@ -1,4 +1,4 @@
package org.jenkinsci.plugins.liquibase.integration;
package org.jenkinsci.plugins.liquibase.matchers;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
@@ -23,6 +23,10 @@ protected boolean matchesSafely(AbstractProject item) {
@Override
public void describeTo(Description description) {
description.appendText("a project with display name of ").appendValue(jobName);
}

@Override
protected void describeMismatchSafely(AbstractProject item, Description mismatchDescription) {
mismatchDescription.appendText("a project with display name of ").appendValue(item.getDisplayName());
}
}

0 comments on commit 0e69df4

Please sign in to comment.