diff --git a/src/uk/org/floop/jenkins_pmd/Drafter.groovy b/src/uk/org/floop/jenkins_pmd/Drafter.groovy new file mode 100644 index 0000000..735ab48 --- /dev/null +++ b/src/uk/org/floop/jenkins_pmd/Drafter.groovy @@ -0,0 +1,39 @@ +package uk.org.floop.jenkins_pmd + +import groovy.json.JsonSlurper +import org.apache.http.HttpHost +import org.apache.http.client.fluent.Executor +import org.apache.http.client.fluent.Request + +class Drafter implements Serializable { + private Executor exec + private URI apiBase + private HttpHost host + + enum Include { + ALL("all"), OWNED("owned"), CLAIMABLE("claimable") + public final String value + Include(String v) { + this.value = v + } + } + + Drafter(String api, String user, String pass) { + this.apiBase = new URI(api) + this.host = new HttpHost(apiBase.getHost()) + exec = Executor.newInstance() + .auth(this.host, user, pass) + .authPreemptive(this.host) + } + + def listDraftsets(Include include=Include.ALL) { + def js = new JsonSlurper() + String path = (include == Include.ALL) ? "/v1/draftsets" : "/v1/draftsets?include=" + Include.value + def response = js.parse( + exec.execute( + Request.Get(apiBase.resolve(path)) + ).returnContent().asStream() + ) + response + } +} \ No newline at end of file diff --git a/test/integration/groovy/uk/org/floop/jenkins_pmd/DrafterTests.groovy b/test/integration/groovy/uk/org/floop/jenkins_pmd/DrafterTests.groovy index b30c0fd..a82d280 100644 --- a/test/integration/groovy/uk/org/floop/jenkins_pmd/DrafterTests.groovy +++ b/test/integration/groovy/uk/org/floop/jenkins_pmd/DrafterTests.groovy @@ -4,30 +4,37 @@ import com.cloudbees.plugins.credentials.SystemCredentialsProvider import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl -import com.github.tomakehurst.wiremock.junit.WireMockRule +import com.github.tomakehurst.wiremock.core.WireMockConfiguration +import com.github.tomakehurst.wiremock.junit.WireMockClassRule import org.jenkinsci.lib.configprovider.ConfigProvider import org.jenkinsci.plugins.configfiles.ConfigFileStore import org.jenkinsci.plugins.configfiles.GlobalConfigFiles import org.jenkinsci.plugins.configfiles.custom.CustomConfig - -import static com.github.tomakehurst.wiremock.client.WireMock.* -import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig - import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition import org.jenkinsci.plugins.workflow.job.WorkflowJob import org.jenkinsci.plugins.workflow.job.WorkflowRun import org.junit.Before +import org.junit.ClassRule import org.junit.Rule import org.junit.Test import org.jvnet.hudson.test.JenkinsRule +import static com.github.tomakehurst.wiremock.client.WireMock.* + class DrafterTests { @Rule public JenkinsRule rule = new JenkinsRule() + @ClassRule + public static WireMockClassRule wireMockRule = new WireMockClassRule(WireMockConfiguration.options() + .dynamicPort() + .usingFilesUnderClasspath("test/resources") + ) + @Rule - public WireMockRule wireMockRule = new WireMockRule(8123) + public WireMockClassRule instanceRule = wireMockRule; + @Before void configureGlobalGitLibraries() { @@ -43,13 +50,13 @@ .getExtensionList(ConfigProvider.class) .get(CustomConfig.CustomConfigProvider.class) globalConfigFiles.save( - new CustomConfig("pmd", "config.json", "Details of endpoint URLs and credentials", '''{ - "pmd_api": "https://production-drafter-ons-alpha.publishmydata.com", + new CustomConfig("pmd", "config.json", "Details of endpoint URLs and credentials", """{ + "pmd_api": "http://localhost:${wireMockRule.port()}", "credentials": "onspmd", - "pipeline_api": "http://production-grafter-ons-alpha.publishmydata.com/v1/pipelines", + "pipeline_api": "http://localhost:${wireMockRule.port()}", "default_mapping": "https://github.com/ONS-OpenData/ref_trade/raw/master/columns.csv", "base_uri": "http://gss-data.org.uk" -}''', configProvider.getProviderId())) +}""", configProvider.getProviderId())) } @Before @@ -86,19 +93,19 @@ } @Test - void "testing library that uses declarative pipeline libraries"() { + void "listDraftsets"() { stubFor(get(urlMatching("/v1/draftsets.*")) - .willReturn(okJson('{"blah"}'))) + .willReturn(ok().withBodyFile("listDraftsets.json"))) final CpsFlowDefinition flow = new CpsFlowDefinition(''' - - echo drafter.listDraftsets('http://localhost:8123', null, 'true') - '''.stripIndent(), true) + node { + echo drafter("pmd").listDraftsets()[0].id + }'''.stripIndent(), true) final WorkflowJob workflowJob = rule.createProject(WorkflowJob, 'project') workflowJob.definition = flow final WorkflowRun firstResult = rule.buildAndAssertSuccess(workflowJob) - rule.assertLogContains('Listing draftsets...', firstResult) verify(getRequestedFor(urlEqualTo("/v1/draftsets"))) + rule.assertLogContains('de305d54-75b4-431b-adb2-eb6b9e546014', firstResult) } } diff --git a/test/resources/__files/listDraftsets.json b/test/resources/__files/listDraftsets.json new file mode 100644 index 0000000..6741754 --- /dev/null +++ b/test/resources/__files/listDraftsets.json @@ -0,0 +1,19 @@ +[ + { + "id": "de305d54-75b4-431b-adb2-eb6b9e546014", + "changes": { + "http://opendatacommunities.org/graph/homelessness/households-accommodated/temporary-housing-types": { + "status": "updated" + }, + "http://opendatacommunities.org/data/labour-force/employment-rate/employment-rate-by-age": { + "status": "deleted" + } + }, + "display-name": "New Temporary Housing Figures", + "description": "Quarterly updates for Q4 2015", + "updated-at": "2016-01-04T13:35:21.000Z", + "created-at": "2016-01-01T13:35:21.000Z", + "current-owner": "admin@opendatacommunities.org", + "submitted-by": "editor@opendatacommunities.org" + } +] \ No newline at end of file diff --git a/vars/drafter.groovy b/vars/drafter.groovy index eeda594..40ef169 100644 --- a/vars/drafter.groovy +++ b/vars/drafter.groovy @@ -1,5 +1,16 @@ import uk.org.floop.jenkins_pmd.Drafter +def call(String configId) { + configFileProvider([configFile(fileId: configId, variable: 'configfile')]) { + def config = readJSON(text: readFile(file: configfile)) + String PMD = config['pmd_api'] + String credentials = config['credentials'] + withCredentials([usernamePassword(credentialsId: credentials, usernameVariable: 'USER', passwordVariable: 'PASS')]) { + return new Drafter(PMD, USER, PASS) + } + } +} + def listDraftsets(String baseUrl, String credentials, String include) { echo "Listing draftsets..." /* def response = httpRequest(acceptType: 'APPLICATION_JSON',