added integration test

parent 0d2175e9
...@@ -54,8 +54,20 @@ dependencies { ...@@ -54,8 +54,20 @@ dependencies {
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.3.1'
compile 'com.h2database:h2:1.3.156' compile 'com.h2database:h2:1.3.156'
// https://mvnrepository.com/artifact/io.rest-assured/rest-assured // https://mvnrepository.com/artifact/io.rest-assured/rest-assured
testCompile group: 'io.rest-assured', name: 'rest-assured', version: '4.3.0' //testCompile group: 'io.rest-assured', name: 'rest-assured', version: '4.3.0'
//compile 'org.hamcrest:hamcrest-all:2.1' testCompile group: 'io.rest-assured', name: 'rest-assured', version: '4.0.0'
testCompile group: 'io.rest-assured', name: 'rest-assured-common', version: '4.0.0'
testCompile group: 'io.rest-assured', name: 'json-path', version: '4.0.0'
testCompile group: 'io.rest-assured', name: 'xml-path', version: '4.0.0'
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.6'
// https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock
testCompile "com.github.tomakehurst:wiremock-jre8:2.26.3"
//testCompile "org.springframework.cloud:spring-cloud-starter-contract-stub-runner"
testCompile group: 'org.springframework.cloud', name: 'spring-cloud-starter-contract-stub-runner', version: '2.2.2.RELEASE'
} }
......
package com.nisum.offertransactionservice.controller;
import com.github.tomakehurst.wiremock.WireMockServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class AbstractTestBase {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractTestBase.class);
protected final static int HTTP_ENDPOINT_PORT = 9090;
protected static WireMockServer mWireMockServer = new WireMockServer(HTTP_ENDPOINT_PORT);
@BeforeClass
public static void setup() {
mWireMockServer.start();
}
@AfterClass
public static void tearDown() {
/* Stop the WireMock server. */
mWireMockServer.stop();
}
}
package com.nisum.offertransactionservice.controller; package com.nisum.offertransactionservice.controller;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import com.nisum.offertransactionservice.dto.Item; import com.nisum.offertransactionservice.dto.Item;
import com.nisum.offertransactionservice.dto.OfferTransactionRequest; import com.nisum.offertransactionservice.dto.OfferTransactionRequest;
import io.restassured.RestAssured; import com.nisum.offertransactionservice.dto.OfferTransactionResponse;
import org.hamcrest.Matchers; import org.junit.Before;
import org.junit.jupiter.api.BeforeEach; import org.junit.Rule;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import static io.restassured.RestAssured.with; import static com.github.tomakehurst.wiremock.client.WireMock.*;
@SpringBootTest
public class OfferTransactionControllerIntegrationTest {
@BeforeEach
public void setup() { @RunWith(SpringRunner.class)
RestAssured.baseURI = "http://localhost"; @SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.DEFINED_PORT)
RestAssured.port = 7072; @AutoConfigureWireMock
public class OfferTransactionControllerIntegrationTest extends AbstractTestBase{
@Rule
public WireMockRule wireMockRule = new WireMockRule(7072);
@Autowired
private TestRestTemplate testRestTemplate;
@Before
public void setUp() {
mockForOfferTransactionCall();
} }
private void mockForOfferTransactionCall() {
mWireMockServer.stubFor(post(urlEqualTo("/offerTransactionCall"))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
.withBodyFile("response.json")
));
}
@Test @Test
public void getOfferTransactionResponseTest() { public void getOfferTransactionResponseTest() {
with().body(getOfferTransactionRequest()) URI uri = URI.create("http://localhost:7072/offerTransactionCall");
.when() RequestEntity requestEntity = new RequestEntity(getOfferTransactionRequest(),HttpMethod.POST,uri);
.request("POST", "/offerTransactionCall")
.then() ResponseEntity<OfferTransactionResponse> responseEntity = testRestTemplate.exchange("http://localhost:7072/offerTransactionCall", HttpMethod.POST,requestEntity,
.statusCode(200) new ParameterizedTypeReference<OfferTransactionResponse>(){}
.body("offerTransactionCall.offerTransactionResponse.transactionId", Matchers.notNullValue()); );
OfferTransactionResponse offerTransactionResponse = responseEntity.getBody();
System.out.println(offerTransactionResponse);
} }
private OfferTransactionRequest getOfferTransactionRequest() { private OfferTransactionRequest getOfferTransactionRequest() {
......
{
"hhid": "54321",
"transactionId": "b89dc898-6e33-4e54-bda8-ada7a80340bf",
"discountedItemList": [
{
"itemName": "coke",
"itemId": "12",
"price": 1800.0
},
{
"itemName": "coke",
"itemId": "12",
"price": 900.0
}
]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment