Refactor code and create RestAPI client so that spock integation test and uat can
use the common code to call deploydb locally or from another host - Fixes #163
This commit is contained in:
parent
09ecbf0e24
commit
1a8eeb9de7
|
@ -1,13 +1,12 @@
|
|||
package deploydb
|
||||
package dropwizardintegtest
|
||||
|
||||
import deploydb.models.Artifact
|
||||
import javax.ws.rs.core.Response
|
||||
|
||||
class IntegrationModelHelper {
|
||||
|
||||
private IntegrationTestAppHelper integrationTestAppHelper = null
|
||||
IntegrationModelHelper(IntegrationTestAppHelper integTestAppHelper) {
|
||||
integrationTestAppHelper = integTestAppHelper
|
||||
private IntegrationRestApiClient integrationRestApiClient = null
|
||||
IntegrationModelHelper(IntegrationRestApiClient integTestAppHelper) {
|
||||
integrationRestApiClient = integTestAppHelper
|
||||
}
|
||||
boolean sendCreateArtifact() {
|
||||
String path = "/api/artifacts"
|
||||
|
@ -19,7 +18,7 @@ class IntegrationModelHelper {
|
|||
"sourceUrl" : "http://example.com/cucumber.jar"
|
||||
}
|
||||
"""
|
||||
return (integrationTestAppHelper.postJsonToPath(path, messageBody, false)).status == 201
|
||||
return (integrationRestApiClient.postJsonToPath(path, messageBody, false)).status == 201
|
||||
|
||||
}
|
||||
|
||||
|
@ -30,7 +29,7 @@ class IntegrationModelHelper {
|
|||
"status" : "STARTED"
|
||||
}
|
||||
"""
|
||||
Response response = integrationTestAppHelper.patchJsonToPath(path, messageBody)
|
||||
Response response = integrationRestApiClient.patchJsonToPath(path, messageBody)
|
||||
response.close()
|
||||
return response.status == 200
|
||||
}
|
||||
|
@ -42,7 +41,7 @@ class IntegrationModelHelper {
|
|||
"status" : "COMPLETED"
|
||||
}
|
||||
"""
|
||||
Response response = integrationTestAppHelper.patchJsonToPath(path, messageBody)
|
||||
Response response = integrationRestApiClient.patchJsonToPath(path, messageBody)
|
||||
response.close()
|
||||
return response.status == 200
|
||||
}
|
||||
|
@ -56,7 +55,7 @@ class IntegrationModelHelper {
|
|||
"infoUrl" : "http://local.lookout.com/jenkins/job-id/2/results"
|
||||
}
|
||||
"""
|
||||
Response response = integrationTestAppHelper.postJsonToPath(path, messageBody, false)
|
||||
Response response = integrationRestApiClient.postJsonToPath(path, messageBody, false)
|
||||
response.close()
|
||||
return response.status == 201
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
package dropwizardintegtest
|
||||
|
||||
import com.github.mustachejava.DefaultMustacheFactory
|
||||
import com.github.mustachejava.Mustache
|
||||
|
||||
import org.glassfish.jersey.client.ClientConfig
|
||||
import javax.ws.rs.client.Client
|
||||
import javax.ws.rs.client.ClientBuilder
|
||||
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider
|
||||
import org.glassfish.jersey.client.JerseyInvocation
|
||||
import javax.ws.rs.core.Response
|
||||
import javax.ws.rs.client.Entity
|
||||
|
||||
class IntegrationRestApiClient {
|
||||
private Client jerseyClient = null
|
||||
String host = "http://localhost"
|
||||
int port = 8080
|
||||
|
||||
String processTemplate(String buffer, Map scope) {
|
||||
DefaultMustacheFactory mf = new DefaultMustacheFactory()
|
||||
StringWriter writer = new StringWriter()
|
||||
Mustache m = mf.compile(new StringReader(buffer),
|
||||
'cuke-stash-compiler')
|
||||
m.execute(writer, scope)
|
||||
|
||||
return writer.toString()
|
||||
}
|
||||
|
||||
Client getClient() {
|
||||
if (this.jerseyClient == null) {
|
||||
ClientConfig clientConfig = new ClientConfig()
|
||||
clientConfig.connectorProvider(new ApacheConnectorProvider())
|
||||
this.jerseyClient = ClientBuilder.newClient(clientConfig)
|
||||
}
|
||||
return this.jerseyClient
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the proper full URL for our running app with the given path.
|
||||
*
|
||||
* If this is an admin request, we'll hit the admin port correctly
|
||||
*/
|
||||
String urlWithPort(String path, Boolean isAdmin) {
|
||||
return host + String.format(":%d${path}", port)
|
||||
}
|
||||
|
||||
|
||||
JerseyInvocation makeRequestToPath(String path, String method, Entity entity) {
|
||||
return this.makeRequestToPath(path, method, entity, false)
|
||||
}
|
||||
|
||||
JerseyInvocation makeRequestToPath(String path, String method, Entity entity, Boolean isAdmin) {
|
||||
return client.target(urlWithPort(path, isAdmin))
|
||||
.request()
|
||||
.build(method, entity)
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a POST to the test server for step definitions
|
||||
*/
|
||||
Response postJsonToPath(String path, String requestBody, Boolean isAdmin) {
|
||||
return this.makeRequestToPath(path, 'POST', Entity.json(requestBody), isAdmin).invoke()
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a PATCH to the test server for step definitions
|
||||
*/
|
||||
Response patchJsonToPath(String path, String requestBody) {
|
||||
return this.makeRequestToPath(path, 'PATCH', Entity.json(requestBody)).invoke()
|
||||
}
|
||||
|
||||
Response deleteFromPath(String path) {
|
||||
return this.makeRequestToPath(path, 'DELETE', null).invoke()
|
||||
}
|
||||
|
||||
/**
|
||||
* Minor convenience method to make sure we're dispatching GET requests to the
|
||||
* right port in our test application
|
||||
*/
|
||||
Response getFromPath(String path, boolean isAdmin) {
|
||||
return this.makeRequestToPath(path, 'GET', null , isAdmin).invoke()
|
||||
}
|
||||
}
|
|
@ -1 +1 @@
|
|||
include "dropwizard-integtest"
|
||||
include "dropwizard-integtest", "uat"
|
|
@ -2,10 +2,14 @@ package deploydb
|
|||
|
||||
import spock.lang.*
|
||||
|
||||
import dropwizardintegtest.IntegrationModelHelper
|
||||
import dropwizardintegtest.IntegrationRestApiClient
|
||||
|
||||
class DeploymentCompletedNotificationsSpec extends Specification {
|
||||
|
||||
IntegrationTestAppHelper integAppHelper = new IntegrationTestAppHelper()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integAppHelper)
|
||||
IntegrationRestApiClient integrationRestApiClient = new IntegrationRestApiClient()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integrationRestApiClient)
|
||||
private WebhooksModelConfigHelper mcfgHelper = new WebhooksModelConfigHelper()
|
||||
|
||||
def setup() {
|
||||
|
|
|
@ -1,11 +1,14 @@
|
|||
package deploydb
|
||||
|
||||
import spock.lang.*
|
||||
import dropwizardintegtest.IntegrationModelHelper
|
||||
import dropwizardintegtest.IntegrationRestApiClient
|
||||
|
||||
class DeploymentCreatedNotificationsSpec extends Specification {
|
||||
|
||||
IntegrationTestAppHelper integAppHelper = new IntegrationTestAppHelper()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integAppHelper)
|
||||
IntegrationRestApiClient integrationRestApiClient = new IntegrationRestApiClient()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integrationRestApiClient)
|
||||
private WebhooksModelConfigHelper mcfgHelper = new WebhooksModelConfigHelper()
|
||||
|
||||
def setup() {
|
||||
|
|
|
@ -2,10 +2,14 @@ package deploydb
|
|||
|
||||
import spock.lang.*
|
||||
|
||||
import dropwizardintegtest.IntegrationModelHelper
|
||||
import dropwizardintegtest.IntegrationRestApiClient
|
||||
|
||||
class DeploymentStartedNotificationsSpec extends Specification {
|
||||
|
||||
IntegrationTestAppHelper integAppHelper = new IntegrationTestAppHelper()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integAppHelper)
|
||||
IntegrationRestApiClient integrationRestApiClient = new IntegrationRestApiClient()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integrationRestApiClient)
|
||||
private WebhooksModelConfigHelper mcfgHelper = new WebhooksModelConfigHelper()
|
||||
|
||||
def setup() {
|
||||
|
|
|
@ -1,16 +1,6 @@
|
|||
package deploydb
|
||||
|
||||
import deploydb.models.Webhook.Webhook
|
||||
import com.github.mustachejava.DefaultMustacheFactory
|
||||
import com.github.mustachejava.Mustache
|
||||
|
||||
import org.glassfish.jersey.client.ClientConfig
|
||||
import javax.ws.rs.client.Client
|
||||
import javax.ws.rs.client.ClientBuilder
|
||||
import org.glassfish.jersey.apache.connector.ApacheConnectorProvider
|
||||
import org.glassfish.jersey.client.JerseyInvocation
|
||||
import javax.ws.rs.core.Response
|
||||
import javax.ws.rs.client.Entity
|
||||
|
||||
import org.hibernate.Session
|
||||
import org.hibernate.SessionFactory
|
||||
|
@ -19,10 +9,8 @@ import dropwizardintegtest.StubAppRunner
|
|||
import dropwizardintegtest.WebhookTestServerAppRunner
|
||||
import dropwizardintegtest.webhookTestServerApp
|
||||
|
||||
|
||||
class IntegrationTestAppHelper {
|
||||
private StubAppRunner runner = null
|
||||
private Client jerseyClient = null
|
||||
private WebhookTestServerAppRunner webhookRunner = null
|
||||
|
||||
SessionFactory getSessionFactory() {
|
||||
|
@ -46,65 +34,6 @@ class IntegrationTestAppHelper {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
String processTemplate(String buffer, Map scope) {
|
||||
DefaultMustacheFactory mf = new DefaultMustacheFactory()
|
||||
StringWriter writer = new StringWriter()
|
||||
Mustache m = mf.compile(new StringReader(buffer),
|
||||
'cuke-stash-compiler')
|
||||
m.execute(writer, scope)
|
||||
|
||||
return writer.toString()
|
||||
}
|
||||
|
||||
Client getClient() {
|
||||
if (this.jerseyClient == null) {
|
||||
ClientConfig clientConfig = new ClientConfig()
|
||||
clientConfig.connectorProvider(new ApacheConnectorProvider())
|
||||
this.jerseyClient = ClientBuilder.newClient(clientConfig)
|
||||
}
|
||||
return this.jerseyClient
|
||||
}
|
||||
|
||||
/**
|
||||
* Create the proper full URL for our running app with the given path.
|
||||
*
|
||||
* If this is an admin request, we'll hit the admin port correctly
|
||||
*/
|
||||
String urlWithPort(String path, Boolean isAdmin) {
|
||||
int port = isAdmin ? runner.adminPort : runner.localPort
|
||||
return String.format("http://localhost:%d${path}", port)
|
||||
}
|
||||
|
||||
|
||||
JerseyInvocation makeRequestToPath(String path, String method, Entity entity) {
|
||||
return this.makeRequestToPath(path, method, entity, false)
|
||||
}
|
||||
|
||||
JerseyInvocation makeRequestToPath(String path, String method, Entity entity, Boolean isAdmin) {
|
||||
return client.target(urlWithPort(path, isAdmin))
|
||||
.request()
|
||||
.build(method, entity)
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a POST to the test server for step definitions
|
||||
*/
|
||||
Response postJsonToPath(String path, String requestBody, Boolean isAdmin) {
|
||||
return this.makeRequestToPath(path, 'POST', Entity.json(requestBody), isAdmin).invoke()
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a PATCH to the test server for step definitions
|
||||
*/
|
||||
Response patchJsonToPath(String path, String requestBody) {
|
||||
return this.makeRequestToPath(path, 'PATCH', Entity.json(requestBody)).invoke()
|
||||
}
|
||||
|
||||
Response deleteFromPath(String path) {
|
||||
return this.makeRequestToPath(path, 'DELETE', null).invoke()
|
||||
}
|
||||
|
||||
/*
|
||||
* Get url path from webhook config body
|
||||
*/
|
||||
|
@ -115,14 +44,6 @@ class IntegrationTestAppHelper {
|
|||
return getUrlPathFromWebhook(webhook, configBody, eventType)
|
||||
}
|
||||
|
||||
/**
|
||||
* Minor convenience method to make sure we're dispatching GET requests to the
|
||||
* right port in our test application
|
||||
*/
|
||||
Response getFromPath(String path, boolean isAdmin) {
|
||||
return this.makeRequestToPath(path, 'GET', null , isAdmin).invoke()
|
||||
}
|
||||
|
||||
/** Set config directory */
|
||||
void setConfigDirectory(String configDirectory) {
|
||||
this.runner.setConfigDirectory(configDirectory)
|
||||
|
|
|
@ -2,10 +2,14 @@ package deploydb
|
|||
|
||||
import spock.lang.*
|
||||
|
||||
import dropwizardintegtest.IntegrationModelHelper
|
||||
import dropwizardintegtest.IntegrationRestApiClient
|
||||
|
||||
class MultipleEnvironmentsNotificationsSpec extends Specification {
|
||||
|
||||
IntegrationTestAppHelper integAppHelper = new IntegrationTestAppHelper()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integAppHelper)
|
||||
IntegrationRestApiClient integrationRestApiClient = new IntegrationRestApiClient()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integrationRestApiClient)
|
||||
private WebhooksModelConfigHelper mcfgHelper = new WebhooksModelConfigHelper()
|
||||
|
||||
def setup() {
|
||||
|
|
|
@ -2,10 +2,14 @@ package deploydb
|
|||
|
||||
import spock.lang.*
|
||||
|
||||
import dropwizardintegtest.IntegrationModelHelper
|
||||
import dropwizardintegtest.IntegrationRestApiClient
|
||||
|
||||
class PromotionCompletedNotificationsSpec extends Specification {
|
||||
|
||||
IntegrationTestAppHelper integAppHelper = new IntegrationTestAppHelper()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integAppHelper)
|
||||
IntegrationRestApiClient integrationRestApiClient = new IntegrationRestApiClient()
|
||||
IntegrationModelHelper integModelHelper = new IntegrationModelHelper(integrationRestApiClient)
|
||||
private WebhooksModelConfigHelper mcfgHelper = new WebhooksModelConfigHelper()
|
||||
|
||||
def setup() {
|
||||
|
|
Loading…
Reference in New Issue