Skip to content

Commit

Permalink
JENKINS-33401 - Credentials Integration
Browse files Browse the repository at this point in the history
added test class
  • Loading branch information
prospero238 committed Sep 6, 2016
1 parent 2925ad0 commit 06717b0
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 31 deletions.
@@ -0,0 +1,37 @@
package org.jenkinsci.plugins.liquibase.common;

import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.tasks.Builder;
import liquibase.Contexts;
import liquibase.Liquibase;
import liquibase.exception.LiquibaseException;

import java.io.IOException;
import java.util.Properties;

import org.jenkinsci.plugins.liquibase.evaluator.AbstractLiquibaseBuilder;
import org.jenkinsci.plugins.liquibase.evaluator.ExecutedChangesetAction;

public class BuilderStub extends AbstractLiquibaseBuilder {
@Override
public void doPerform(AbstractBuild<?, ?> build,
BuildListener listener,
Liquibase liquibase,
Contexts contexts,
ExecutedChangesetAction executedChangesetAction, Properties configProperties)
throws InterruptedException, IOException, LiquibaseException {

}

@Override
public Descriptor<Builder> getDescriptor() {
return null;
}

@Override
public String getChangeLogFile() {
return "${token}";
}
}
@@ -0,0 +1,109 @@
package org.jenkinsci.plugins.liquibase.common;

import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.FreeStyleBuild;
import hudson.model.FreeStyleProject;

import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.ExecutionException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.jenkinsci.plugins.liquibase.evaluator.ChangesetEvaluator;
import org.jenkinsci.plugins.liquibase.integration.LiquibaseTestUtil;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.jvnet.hudson.test.JenkinsRule;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.CredentialsScope;
import com.cloudbees.plugins.credentials.domains.Domain;
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(MockitoJUnitRunner.class)
public class CredentialsTest {

private static final String DESCRIPTION = "sample";
@Mock
private AbstractBuild build;

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Rule
public JenkinsRule jenkinsRule = new JenkinsRule();
protected BuilderStub builderStub = new BuilderStub();
protected String username = RandomStringUtils.randomAlphabetic(10);
protected String password = RandomStringUtils.randomAlphabetic(10);
protected String credentialsId = RandomStringUtils.randomAlphabetic(4);


@Before
public void setup() throws IOException {
temporaryFolder.create();

UsernamePasswordCredentialsImpl credentials =
new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, DESCRIPTION, username, password);
CredentialsProvider.lookupStores(jenkinsRule.getInstance()).iterator().next()
.addCredentials(Domain.global(), credentials);

}
@Test
public void should_use_credentials() throws IOException, InterruptedException {
builderStub.setCredentialsId(credentialsId);
Properties properties = new Properties();
PropertiesAssembler.assembleFromProjectConfiguration(builderStub, properties, new EnvVars(), build);

String actualUsername = getProperty(properties, LiquibaseProperty.USERNAME);
String actualPassword = getProperty(properties, LiquibaseProperty.PASSWORD);

assertThat(actualUsername, is(username));
assertThat(actualPassword, is(password));

}

@Test
public void should_prefer_credentials_defined_in_credentials_config()
throws IOException, InterruptedException, ExecutionException {

File propertiesfile = temporaryFolder.newFile("liquibase.properties");
FileUtils.write(propertiesfile, "username=i_shouldnt_be_used\n");

FreeStyleProject project = jenkinsRule.createFreeStyleProject();
ChangesetEvaluator evaluator = new ChangesetEvaluator();
evaluator.setChangeLogFile(LiquibaseTestUtil
.createFileFromResource(temporaryFolder.getRoot(), "/example-changesets/single-changeset.xml").getAbsolutePath());
evaluator.setUrl(LiquibaseTestUtil.IN_MEMORY_JDBC_URL);
evaluator.setDatabaseEngine(LiquibaseTestUtil.H2);
evaluator.setLiquibasePropertiesPath(propertiesfile.getAbsolutePath());
evaluator.setCredentialsId(credentialsId);
project.getBuildersList().add(evaluator);


FreeStyleBuild build = project.scheduleBuild2(0).get();
Properties liquibaseProperties =
PropertiesAssembler.createLiquibaseProperties(evaluator, build, new EnvVars());

String resolvedUsername = getProperty(liquibaseProperties, LiquibaseProperty.USERNAME);
String resolvedPassword = getProperty(liquibaseProperties, LiquibaseProperty.PASSWORD);

assertThat(resolvedUsername, is(username));
assertThat(resolvedPassword, is(password));

}

private static String getProperty(Properties properties, LiquibaseProperty propertyName) {
return properties.getProperty(propertyName.propertyName());
}
}
Expand Up @@ -3,17 +3,10 @@
import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Descriptor;
import hudson.tasks.Builder;
import liquibase.Contexts;
import liquibase.Liquibase;
import liquibase.exception.LiquibaseException;

import java.io.IOException;
import java.util.Properties;

import org.jenkinsci.plugins.liquibase.evaluator.AbstractLiquibaseBuilder;
import org.jenkinsci.plugins.liquibase.evaluator.ExecutedChangesetAction;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
Expand Down Expand Up @@ -63,26 +56,4 @@ public void should_not_resolve_expression() {
}



private static class BuilderStub extends AbstractLiquibaseBuilder {
@Override
public void doPerform(AbstractBuild<?, ?> build,
BuildListener listener,
Liquibase liquibase,
Contexts contexts,
ExecutedChangesetAction executedChangesetAction, Properties configProperties)
throws InterruptedException, IOException, LiquibaseException {

}

@Override
public Descriptor<Builder> getDescriptor() {
return null;
}

@Override
public String getChangeLogFile() {
return "${token}";
}
}
}
Expand Up @@ -15,8 +15,8 @@
public class LiquibaseTestUtil {
static final String SUNNY_DAY_CHANGESET_XML = "/example-changesets/sunny-day-changeset.xml";
private static final String CHANGESET_WITH_ERROR_XML = "/example-changesets/changeset-with-error.xml";
static final String IN_MEMORY_JDBC_URL = "jdbc:h2:mem:test";
static final String H2 = "H2";
public static final String IN_MEMORY_JDBC_URL = "jdbc:h2:mem:test";
public static final String H2 = "H2";

static File createErrorFreeChangeset(TemporaryFolder temporaryFolder) throws IOException {
return createFileFromResource(temporaryFolder.getRoot(), SUNNY_DAY_CHANGESET_XML);
Expand Down

0 comments on commit 06717b0

Please sign in to comment.