Skip to content

Commit

Permalink
Merge pull request #26 from ankushkawanpure/spock
Browse files Browse the repository at this point in the history
Move tests to groovy spock framework
  • Loading branch information
ashpak-shaikh committed Oct 25, 2023
2 parents 80d1ac1 + 9ce705c commit bfdc525
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 275 deletions.
37 changes: 37 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,28 @@
<target>${java.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.gmavenplus</groupId>
<artifactId>gmavenplus-plugin</artifactId>
<version>1.12.1</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compileTests</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<includes>
<include>**/*Test.class</include>
<include>**/*Spec.class</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -265,6 +284,24 @@
<version>${service-evaluator-version}</version>
<artifactId>web-client-service-evaluator</artifactId>
</dependency>
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.3-groovy-2.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<version>2.4.21</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility-groovy</artifactId>
<version>3.0.0</version>
<scope>test</scope>
</dependency>
</dependencies>
</dependencyManagement>

Expand Down
16 changes: 15 additions & 1 deletion rest-adapter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,20 @@
<version>5.3.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-all</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility-groovy</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
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();
}
}
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"
}
}
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
}
}

This file was deleted.

Loading

0 comments on commit bfdc525

Please sign in to comment.