1+ package com .baeldung .rest .cucumber ;
2+
3+ import static org .junit .Assert .*;
4+ import static org .hamcrest .CoreMatchers .containsString ;
5+ import static com .github .tomakehurst .wiremock .client .WireMock .*;
6+
7+ import java .io .IOException ;
8+ import java .io .InputStream ;
9+ import java .util .Scanner ;
10+
11+ import org .apache .http .HttpResponse ;
12+ import org .apache .http .client .methods .HttpGet ;
13+ import org .apache .http .client .methods .HttpPost ;
14+ import org .apache .http .entity .StringEntity ;
15+ import org .apache .http .impl .client .CloseableHttpClient ;
16+ import org .apache .http .impl .client .HttpClients ;
17+
18+ import com .github .tomakehurst .wiremock .WireMockServer ;
19+
20+ import cucumber .api .java .en .Then ;
21+ import cucumber .api .java .en .When ;
22+
23+ public class StepDefinition {
24+ InputStream jsonInputStream = this .getClass ().getClassLoader ().getResourceAsStream ("cucumber.json" );
25+ String jsonString = new Scanner (jsonInputStream , "UTF-8" ).useDelimiter ("\\ Z" ).next ();
26+
27+ WireMockServer wireMockServer = new WireMockServer ();
28+ CloseableHttpClient httpClient = HttpClients .createDefault ();
29+
30+ @ When ("^users upload data on a project$" )
31+ public void usersUploadDataOnAProject () throws IOException {
32+ wireMockServer .start ();
33+
34+ configureFor ("localhost" , 8080 );
35+ stubFor (post (urlEqualTo ("/create" )).withHeader ("content-type" , equalTo ("application/json" )).withRequestBody (containing ("testing-framework" )).willReturn (aResponse ().withStatus (200 )));
36+
37+ HttpPost request = new HttpPost ("http://localhost:8080/create" );
38+ StringEntity entity = new StringEntity (jsonString );
39+ request .addHeader ("content-type" , "application/json" );
40+ request .setEntity (entity );
41+ HttpResponse response = httpClient .execute (request );
42+
43+ assertEquals (200 , response .getStatusLine ().getStatusCode ());
44+ verify (postRequestedFor (urlEqualTo ("/create" )).withHeader ("content-type" , equalTo ("application/json" )));
45+
46+ wireMockServer .stop ();
47+ }
48+
49+ @ When ("^users want to get information on the (.+) project$" )
50+ public void usersGetInformationOnAProject (String projectName ) throws IOException {
51+ wireMockServer .start ();
52+
53+ configureFor ("localhost" , 8080 );
54+ stubFor (get (urlEqualTo ("/projects/cucumber" )).withHeader ("accept" , equalTo ("application/json" )).willReturn (aResponse ().withBody (jsonString )));
55+
56+ HttpGet request = new HttpGet ("http://localhost:8080/projects/" + projectName .toLowerCase ());
57+ request .addHeader ("accept" , "application/json" );
58+ HttpResponse httpResponse = httpClient .execute (request );
59+ String responseString = convertResponseToString (httpResponse );
60+
61+ assertThat (responseString , containsString ("\" testing-framework\" : \" cucumber\" " ));
62+ assertThat (responseString , containsString ("\" website\" : \" cucumber.io\" " ));
63+ verify (getRequestedFor (urlEqualTo ("/projects/cucumber" )).withHeader ("accept" , equalTo ("application/json" )));
64+
65+ wireMockServer .stop ();
66+ }
67+
68+ @ Then ("^the server should handle it and return a success status$" )
69+ public void theServerShouldReturnASuccessStatus () {
70+ }
71+
72+ @ Then ("^the requested data is returned$" )
73+ public void theRequestedDataIsReturned () {
74+ }
75+
76+ private String convertResponseToString (HttpResponse response ) throws IOException {
77+ InputStream responseStream = response .getEntity ().getContent ();
78+ Scanner scanner = new Scanner (responseStream , "UTF-8" );
79+ String responseString = scanner .useDelimiter ("\\ Z" ).next ();
80+ scanner .close ();
81+ return responseString ;
82+ }
83+ }
0 commit comments