-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into 21-Update-graphql-java
- Loading branch information
Showing
12 changed files
with
311 additions
and
279 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
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
94 changes: 94 additions & 0 deletions
94
rest-adapter/src/test/groovy/com/intuit/graphql/adapter/rest/AddBookServicePostSpec.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,94 @@ | ||
package com.intuit.graphql.adapter.rest | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import com.intuit.graphql.adapter.core.ServiceAdapterRequest | ||
import com.intuit.graphql.adapter.rest.utils.TestUtil | ||
import graphql.GraphQLContext | ||
import groovy.json.JsonSlurper | ||
import org.springframework.web.reactive.function.server.ServerRequest | ||
import spock.lang.Specification | ||
|
||
class AddBookServicePostSpec extends Specification { | ||
|
||
public static String addBookDSL = ''' | ||
Service service as addBooks method POST { | ||
Url -> @Config("endpoint") | ||
Path -> ${"/books"} | ||
Timeout -> ${@Config("timeout")} | ||
@Header accept -> ${"application/json"} | ||
@Header "user_channel" -> ${requestContext.headers.user_channel} | ||
@Body -> ${requestContext.arguments.newBook} | ||
} | ||
Service service as addBooksWithBody method POST { | ||
Url -> @Config("endpoint") | ||
Path -> ${"/books"} | ||
Timeout -> ${@Config("timeout")} | ||
@Header accept -> ${"application/json"} | ||
@Header "user_channel" -> ${requestContext.headers.user_channel} | ||
@Body newBook -> ${requestContext.arguments.newBook} | ||
@Body newBookId -> ${requestContext.arguments.newBook.id} | ||
@Body chapters -> ${requestContext.arguments.newBook.chapters} | ||
@Body price -> ${requestContext.arguments.newBook.price} | ||
@Body isPublished -> ${requestContext.arguments.newBook.isPublished} | ||
@Body isHardPrint -> ${requestContext.arguments.newBook.isHardPrint} | ||
@Body author -> ${requestContext.arguments.newBook.author1} | ||
} | ||
Service service as addBooksNoKeyBody method POST { | ||
Url -> @Config("endpoint") | ||
Path -> ${"/custombody/nokey/books"} | ||
Timeout -> ${@Config("timeout")} | ||
@Header accept -> ${"application/json"} | ||
@Header "user_channel" -> ${requestContext.headers.user_channel} | ||
@Body -> ${requestContext.arguments.newBook} | ||
} | ||
Service service as addBooksPUT method PUT { | ||
Url -> @Config("endpoint") | ||
Path -> ${"/books"} | ||
Timeout -> ${@Config("timeout")} | ||
@Header accept -> ${"application/json"} | ||
@Header "user_channel" -> ${requestContext.headers.user_channel} | ||
@Body -> ${requestContext.arguments.newBook} | ||
}''' | ||
|
||
public static final ServiceAdapterRequest addBookServiceAdapterRequest; | ||
|
||
static { | ||
ServerRequest serverRequest = TestUtil.createServerRequest() | ||
GraphQLContext testGraphQLContext = TestUtil.createGraphQLContext(serverRequest) | ||
def jsonSlurper = new JsonSlurper() | ||
def addBookSingleArgument = jsonSlurper.parseText(''' | ||
{ | ||
"newBook": { | ||
"id": "book-1", | ||
"name": "The Book", | ||
"price": 100.00, | ||
"chapters": ["Chapter-1", "Chapter-2", "Chapter-3"], | ||
"isPublished": true, | ||
"isHardPrint": false, | ||
"author": null | ||
} | ||
} | ||
''') | ||
|
||
ObjectMapper objectMapper = new ObjectMapper(); | ||
ObjectNode requestContextNode = objectMapper.createObjectNode(); | ||
requestContextNode.replace("arguments", objectMapper.valueToTree(addBookSingleArgument)) | ||
|
||
Map<String, JsonNode> inputMap = new HashMap<>() | ||
inputMap.put("requestContext", requestContextNode) | ||
|
||
addBookServiceAdapterRequest = ServiceAdapterRequest.builder() | ||
.adapter(Optional.of("addBooks")) | ||
.inputMap(inputMap) | ||
.graphQLContext(testGraphQLContext) | ||
.operation("mutation") | ||
.requestedField("addBook") | ||
.requestType("GraphQL.Mutation") | ||
.build(); | ||
} | ||
} |
135 changes: 135 additions & 0 deletions
135
rest-adapter/src/test/groovy/com/intuit/graphql/adapter/rest/RestAdapterSpec.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,135 @@ | ||
package com.intuit.graphql.adapter.rest | ||
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.github.tomakehurst.wiremock.junit.WireMockRule | ||
import com.intuit.graphql.adapter.core.ServiceAdapterRequest | ||
import com.intuit.graphql.adapter.core.ServiceAdapterResponse | ||
import com.intuit.graphql.adapter.rest.utils.TestUtil | ||
import org.junit.Rule | ||
import spock.lang.Specification | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.any; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.containing; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; | ||
|
||
class RestAdapterSpec extends Specification { | ||
|
||
private static int WIREMOCK_PORT = 4050; | ||
@Rule | ||
public WireMockRule wireMockRule = new WireMockRule(WIREMOCK_PORT); | ||
private RestAdapter addBookRestAdapter | ||
|
||
def setup() { | ||
addBookRestAdapter = RestAdapter.builder() | ||
.dslResource(AddBookServicePostSpec.addBookDSL) | ||
.serviceId("TEST_SERVICE") | ||
.svcConfig(TestUtil.createServiceConfiguration(TestUtil.TIMEOUT, WIREMOCK_PORT)) | ||
.webClient(TestUtil.webClient) | ||
.build(); | ||
} | ||
|
||
def "canExecuteRequest with body"() { | ||
given: | ||
ServiceAdapterRequest serviceAdapterRequest = AddBookServicePostSpec.addBookServiceAdapterRequest | ||
stubFor(any(urlPathMatching("/books")) | ||
.withRequestBody(containing('''"id":"book-1''')) | ||
.willReturn(aResponse() | ||
.withStatus(200) | ||
.withBody(''' | ||
{ | ||
"id": "book-1", | ||
"name": "The Book", | ||
"price": 100 | ||
} | ||
''') | ||
.withHeader("Content-Type", "application/json;charset=UTF-8"))); | ||
when: | ||
ServiceAdapterResponse response = addBookRestAdapter.execute(serviceAdapterRequest).get() | ||
then: | ||
assert response.getData() != null | ||
JsonNode data = response.getData(); | ||
assert data.has("addBook") | ||
assert data.size() == 1 | ||
JsonNode addBook = data.get("addBook"); | ||
assert addBook.has("id") | ||
assert addBook.has("price") | ||
assert addBook.has("name") | ||
} | ||
|
||
def "cannot Execute Request Due to Http 400 Error"() { | ||
given: | ||
ServiceAdapterRequest serviceAdapterRequest = AddBookServicePostSpec.addBookServiceAdapterRequest | ||
stubFor(any(urlPathMatching("/books")) | ||
.withRequestBody(containing('''"id":"book-1''')) | ||
.willReturn(aResponse() | ||
.withStatus(400) | ||
.withBody(''' | ||
{ | ||
"error": "Bad Request" | ||
} | ||
''') | ||
.withHeader("Content-Type", "application/json;charset=UTF-8"))) | ||
when: | ||
Throwable cause = null | ||
try { | ||
addBookRestAdapter.execute(serviceAdapterRequest).get() | ||
} catch (Exception e) { | ||
cause = e.getCause(); | ||
} | ||
then: | ||
assert cause != null | ||
assert cause.getMessage().contains("Bad Request") | ||
assert cause.getMessage().contains("400") | ||
assert cause.class.simpleName == "ServiceAdapterException" | ||
} | ||
|
||
def "cannot execute Request due to HTTP 500 Error"() { | ||
given: | ||
ServiceAdapterRequest serviceAdapterRequest = AddBookServicePostSpec.addBookServiceAdapterRequest | ||
stubFor(any(urlPathMatching("/books")) | ||
.withRequestBody(containing('''"id":"book-1''')) | ||
.willReturn(aResponse() | ||
.withStatus(500) | ||
.withBody(''' | ||
{ | ||
"error": "Internal Server Error" | ||
} | ||
''') | ||
.withHeader("Content-Type", "application/json;charset=UTF-8"))); | ||
when: | ||
Throwable cause = null | ||
try { | ||
addBookRestAdapter.execute(serviceAdapterRequest).get() | ||
} catch (Exception e) { | ||
cause = e.getCause(); | ||
} | ||
then: | ||
assert cause != null | ||
assert cause.getMessage().contains("Internal Server Error") | ||
assert cause.getMessage().contains("500") | ||
assert cause.class.simpleName == "ServiceAdapterException" | ||
} | ||
|
||
def "cannot execute Request due to HTTP 300 Error"() { | ||
given: | ||
ServiceAdapterRequest serviceAdapterRequest = AddBookServicePostSpec.addBookServiceAdapterRequest | ||
stubFor(any(urlPathMatching("/books")) | ||
.withRequestBody(containing('''"id":"book-1''')) | ||
.willReturn(aResponse() | ||
.withStatus(300) | ||
.withHeader("Content-Type", "application/json;charset=UTF-8"))) | ||
when: | ||
Throwable cause = null | ||
try { | ||
addBookRestAdapter.execute(serviceAdapterRequest).get() | ||
} catch (Exception e) { | ||
cause = e.getCause(); | ||
} | ||
then: | ||
assert cause != null | ||
assert cause.getMessage().contains("300") | ||
assert cause.class.simpleName == "ServiceAdapterException" | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
rest-adapter/src/test/groovy/com/intuit/graphql/adapter/rest/ServiceLoaderSpec.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,22 @@ | ||
package com.intuit.graphql.adapter.rest | ||
|
||
import com.intuit.dsl.service.Service | ||
import spock.lang.Specification | ||
|
||
class ServiceLoaderSpec extends Specification { | ||
|
||
def "can load service from dsl"() { | ||
given: | ||
ServiceLoader loader = new ServiceLoader(AddBookServicePostSpec.addBookDSL) | ||
when: | ||
Service addBooks = loader.getService("addBooks"); | ||
Service addBooksWithBody = loader.getService("addBooksWithBody"); | ||
Service addBooksNoKeyBody = loader.getService("addBooksNoKeyBody"); | ||
Service addBooksPUT = loader.getService("addBooksPUT"); | ||
then: | ||
assert addBooks != null | ||
assert addBooksWithBody != null | ||
assert addBooksNoKeyBody != null | ||
assert addBooksPUT != null | ||
} | ||
} |
Oops, something went wrong.