-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a GitHub Status API consumer with a simple contract test
- Loading branch information
Showing
8 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
backend/src/main/groovy/org/hypoport/example/backend/GitHubStatusService.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/src/test/groovy/org/hypoport/example/backend/GitHubStatusServiceSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
41 changes: 41 additions & 0 deletions
41
...sts/src/main/groovy/org/hypoport/example/contracttests/GitHubStatusApiContractTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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+" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters