Skip to content

Commit

Permalink
add a GitHub Status API consumer with a simple contract test
Browse files Browse the repository at this point in the history
  • Loading branch information
gesellix committed Mar 1, 2015
1 parent 954cc89 commit d165f1b
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 4 deletions.
1 change: 1 addition & 0 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ dependencies {

compile("javax.mail:mail:1.4.7")

testCompile("org.spockframework:spock-core:0.7-groovy-2.0")
testCompile("org.easytesting:fest-assert:1.4")
testCompile("org.mockito:mockito-all:1.9.5")
testCompile("org.testng:testng:6.8.17")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.hypoport.example.backend

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.client.RestOperations

import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE
import static org.springframework.web.bind.annotation.RequestMethod.GET

@RestController
class GitHubStatusService {

final static String STATUS_API_BASEURL = 'https://status.github.com/api.json'
final static String STATUS_LINKNAME = 'status_url'

@Autowired
RestOperations restOperations

@RequestMapping(value = '/github/status', method = GET, produces = APPLICATION_JSON_VALUE)
def getCurrentGitHubStatus() {
def gitHubApiMethods = restOperations.getForObject(STATUS_API_BASEURL, Map, [])
String statusUrl = gitHubApiMethods[STATUS_LINKNAME]
def currentSystemStatus = restOperations.getForObject(statusUrl, Map, [])
return currentSystemStatus
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.hypoport.example.backend

import org.springframework.web.client.RestOperations
import spock.lang.Specification

import static org.hypoport.example.backend.GitHubStatusService.STATUS_API_BASEURL

class GitHubStatusServiceSpec extends Specification {

def gitHubStatusService = new GitHubStatusService()
RestOperations restOperations = Mock(RestOperations)

def setup() {
gitHubStatusService.restOperations = restOperations
}

def "should traverse from the official api entrypoint to the status api"() {
given:
1 * restOperations.getForObject(STATUS_API_BASEURL, Map, []) >> [
'status_url': "http://our.actual.status.url/as.json"]
1 * restOperations.getForObject("http://our.actual.status.url/as.json", Map, []) >> [
'status': "wonderful"]
when:
def gitHubStatus = gitHubStatusService.currentGitHubStatus
then:
gitHubStatus?.status == "wonderful"
}
}
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ subprojects {
def dependencyVersionsByGroup = [
'org.apache.tomcat.embed' : '8.0.12',
'org.springframework' : springVersion,
'org.codehaus.groovy': '2.3.9',
'org.slf4j' : '1.7.10',
'org.codehaus.groovy' : '2.3.9',
'org.slf4j' : '1.7.10',
'com.fasterxml.jackson.core' : jacksonVersion,
'com.fasterxml.jackson.datatype': jacksonVersion
]
Expand Down Expand Up @@ -188,7 +188,6 @@ def writeVersion(def version) {
new File(rootDir.absolutePath + "/version.txt").write(version)
}


task wrapper(type: Wrapper) {
gradleVersion = '2.1'
distributionUrl = "https://services.gradle.org/distributions/gradle-${gradleVersion}-all.zip"
Expand Down
20 changes: 20 additions & 0 deletions common/src/main/groovy/org/hypoport/example/web/HttpConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package org.hypoport.example.web
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.http.client.SimpleClientHttpRequestFactory
import org.springframework.http.converter.StringHttpMessageConverter
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
import org.springframework.web.client.RestOperations
import org.springframework.web.client.RestTemplate

@Configuration
class HttpConfig {
Expand All @@ -28,4 +31,21 @@ class HttpConfig {
converter.supportedMediaTypes = [MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON]
return converter
}

def httpsProxyAwareRequestFactory() {
def factory = new SimpleClientHttpRequestFactory()
if (System.env['https_proxy']) {
def httpsProxy = new URI(System.env['https_proxy'] as String)
factory.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(httpsProxy.host, httpsProxy.port)))
}
return factory
}

@Bean
public RestOperations restOperations() {
RestTemplate restTemplate = new RestTemplate(httpsProxyAwareRequestFactory())
restTemplate.messageConverters.add(0, stringHttpMessageConverter())
restTemplate.messageConverters.add(1, jackson2HttpMessageConverter())
return restTemplate
}
}
11 changes: 11 additions & 0 deletions contracttests/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apply plugin: 'com.github.ben-manes.versions'

dependencies {
compile 'org.spockframework:spock-core:0.7-groovy-2.0'
compile 'org.spockframework:spock-spring:0.7-groovy-2.0'
compile 'org.testng:testng:6.8.17'
compile 'junit:junit:4.12'
compile 'cglib:cglib-nodep:3.1'
compile project(':backend')
compile project(':common')
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.hypoport.example.contracttests

import org.hypoport.example.web.HttpConfig
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.test.context.ContextConfiguration
import org.springframework.web.client.RestOperations
import spock.lang.Specification

import static org.hypoport.example.backend.GitHubStatusService.STATUS_API_BASEURL
import static org.hypoport.example.backend.GitHubStatusService.STATUS_LINKNAME

@ContextConfiguration(classes = HttpConfig)
class GitHubStatusApiContractTest extends Specification {

final static String STATUS_PROPERTY_NAME = 'status'

@Autowired
def RestOperations restOperations

def setup() {
}

def "entrypoint should link to the status_url"() {
when:
def gitHubApiMethods = restOperations.getForObject(STATUS_API_BASEURL, Map, [])
then:
gitHubApiMethods[STATUS_LINKNAME] instanceof String
and:
new URI(gitHubApiMethods[STATUS_LINKNAME] as String).isAbsolute()
}

def "status_url should return status as property"() {
given:
def gitHubApiMethods = restOperations.getForObject(STATUS_API_BASEURL, Map, [])
String statusUrl = gitHubApiMethods[STATUS_LINKNAME]
when:
def currentSystemStatus = restOperations.getForObject(statusUrl, Map, [])
then:
currentSystemStatus[STATUS_PROPERTY_NAME] =~ "\\w+"
}
}
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ rootProject.name = 'gradle-with-docker'
include 'backend',
'common',
'frontend',
'e2e-tests'
'e2e-tests',
'contracttests'

0 comments on commit d165f1b

Please sign in to comment.