Skip to content

Commit 466ee95

Browse files
authored
[JAVA-49735] Move code to resolve article-code-matches-github job issues (#18996)
1 parent 46e07e7 commit 466ee95

File tree

6 files changed

+227
-65
lines changed

6 files changed

+227
-65
lines changed

testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssured2IntegrationTest.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,6 @@ public void givenUrl_whenVerifiesOddPricesAccuratelyByStatus_thenCorrect() {
4747
.body("odds.findAll { it.status > 0 }.price", hasItems(5.25f, 1.2f));
4848
}
4949

50-
@Test
51-
public void whenRequestedPost_thenCreated() {
52-
with().body(new Odd(5.25f, 1, 13.1f, "X"))
53-
.when()
54-
.request("POST", "/odds/new")
55-
.then()
56-
.statusCode(201);
57-
}
5850

5951
private static String getJson() {
6052
return Util.inputStreamToString(RestAssured2IntegrationTest.class.getResourceAsStream("/odds.json"));

testing-modules/rest-assured-2/src/test/java/com/baeldung/restassured/RestAssuredAdvancedLiveTest.java

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,7 @@ public void setup(){
2222
RestAssured.baseURI = "https://api.github.com";
2323
RestAssured.port = 443;
2424
}
25-
26-
@Test
27-
public void whenMeasureResponseTime_thenOK(){
28-
Response response = RestAssured.get("/users/eugenp");
29-
long timeInMS = response.time();
30-
long timeInS = response.timeIn(TimeUnit.SECONDS);
31-
32-
assertEquals(timeInS, timeInMS/1000);
33-
}
34-
35-
@Test
36-
public void whenValidateResponseTime_thenSuccess(){
37-
when().get("/users/eugenp").then().time(lessThan(5000L));
38-
}
3925

40-
@Test
41-
public void whenValidateResponseTimeInSeconds_thenSuccess(){
42-
when().get("/users/eugenp").then().time(lessThan(5L),TimeUnit.SECONDS);
43-
}
44-
4526
//===== parameter
4627

4728
@Test
@@ -98,42 +79,5 @@ public void whenUseCookieBuilder_thenOK(){
9879
Cookie myCookie = new Cookie.Builder("session_id", "1234").setSecured(true).setComment("session id cookie").build();
9980
given().cookie(myCookie).when().get("/users/eugenp").then().statusCode(200);
10081
}
101-
102-
// ====== request
103-
104-
@Test
105-
public void whenRequestGet_thenOK(){
106-
when().request("GET", "/users/eugenp").then().statusCode(200);
107-
}
108-
109-
@Test
110-
public void whenRequestHead_thenOK(){
111-
when().request("HEAD", "/users/eugenp").then().statusCode(200);
112-
}
113-
114-
//======= log
115-
116-
@Test
117-
public void whenLogRequest_thenOK(){
118-
given().log().all().when().get("/users/eugenp").then().statusCode(200);
119-
}
120-
121-
@Test
122-
public void whenLogResponse_thenOK(){
123-
when().get("/repos/eugenp/tutorials").then().log().body().statusCode(200);
124-
}
125-
126-
@Test
127-
public void whenLogResponseIfErrorOccurred_thenSuccess(){
128-
when().get("/users/eugenp").then().log().ifError();
129-
when().get("/users/eugenp").then().log().ifStatusCodeIsEqualTo(500);
130-
when().get("/users/eugenp").then().log().ifStatusCodeMatches(greaterThan(200));
131-
}
132-
133-
@Test
134-
public void whenLogOnlyIfValidationFailed_thenSuccess(){
135-
when().get("/users/eugenp").then().log().ifValidationFails().statusCode(200);
136-
given().log().ifValidationFails().when().get("/users/eugenp").then().statusCode(200);
137-
}
138-
82+
13983
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.restassured;
2+
3+
public class Odd {
4+
5+
float price;
6+
int status;
7+
float ck;
8+
String name;
9+
10+
Odd(float price, int status, float ck, String name) {
11+
this.price = price;
12+
this.status = status;
13+
this.ck = ck;
14+
this.name = name;
15+
}
16+
17+
public float getPrice() {
18+
return price;
19+
}
20+
21+
public void setPrice(float price) {
22+
this.price = price;
23+
}
24+
25+
public int getStatus() {
26+
return status;
27+
}
28+
29+
public void setStatus(int status) {
30+
this.status = status;
31+
}
32+
33+
public float getCk() {
34+
return ck;
35+
}
36+
37+
public void setCk(float ck) {
38+
this.ck = ck;
39+
}
40+
41+
public String getName() {
42+
return name;
43+
}
44+
45+
public void setName(String name) {
46+
this.name = name;
47+
}
48+
49+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.restassured;
2+
3+
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
4+
import static com.github.tomakehurst.wiremock.client.WireMock.configureFor;
5+
import static com.github.tomakehurst.wiremock.client.WireMock.containing;
6+
import static com.github.tomakehurst.wiremock.client.WireMock.post;
7+
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
8+
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
9+
import static io.restassured.RestAssured.get;
10+
import static io.restassured.RestAssured.with;
11+
import static org.hamcrest.Matchers.hasItems;
12+
13+
import org.junit.AfterClass;
14+
import org.junit.BeforeClass;
15+
import org.junit.Test;
16+
17+
import com.github.tomakehurst.wiremock.WireMockServer;
18+
19+
import io.restassured.RestAssured;
20+
21+
public class RestAssured2IntegrationTest {
22+
private static WireMockServer wireMockServer;
23+
24+
private static final String ODDS_PATH = "/odds";
25+
private static final String APPLICATION_JSON = "application/json";
26+
private static final String ODDS = getJson();
27+
28+
@BeforeClass
29+
public static void before() {
30+
System.out.println("Setting up!");
31+
final int port = Util.getAvailablePort();
32+
wireMockServer = new WireMockServer(port);
33+
wireMockServer.start();
34+
configureFor("localhost", port);
35+
RestAssured.port = port;
36+
stubFor(com.github.tomakehurst.wiremock.client.WireMock.get(urlEqualTo(ODDS_PATH))
37+
.willReturn(aResponse().withStatus(200)
38+
.withHeader("Content-Type", APPLICATION_JSON)
39+
.withBody(ODDS)));
40+
stubFor(post(urlEqualTo("/odds/new")).withRequestBody(containing("{\"price\":5.25,\"status\":1,\"ck\":13.1,\"name\":\"X\"}"))
41+
.willReturn(aResponse().withStatus(201)));
42+
}
43+
44+
@Test
45+
public void whenRequestedPost_thenCreated() {
46+
with().body(new Odd(5.25f, 1, 13.1f, "X"))
47+
.when()
48+
.request("POST", "/odds/new")
49+
.then()
50+
.statusCode(201);
51+
}
52+
53+
private static String getJson() {
54+
return Util.inputStreamToString(RestAssured2IntegrationTest.class.getResourceAsStream("/odds.json"));
55+
}
56+
57+
@AfterClass
58+
public static void after() {
59+
System.out.println("Running: tearDown");
60+
wireMockServer.stop();
61+
}
62+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.baeldung.restassured;
2+
3+
import static io.restassured.RestAssured.given;
4+
import static io.restassured.RestAssured.when;
5+
import static org.hamcrest.Matchers.greaterThan;
6+
import static org.hamcrest.Matchers.lessThan;
7+
import static org.junit.Assert.assertEquals;
8+
9+
import java.util.concurrent.TimeUnit;
10+
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
import io.restassured.RestAssured;
15+
import io.restassured.response.Response;
16+
17+
public class RestAssuredAdvancedLiveTest {
18+
19+
@BeforeEach
20+
public void setup() {
21+
RestAssured.baseURI = "https://api.github.com";
22+
RestAssured.port = 443;
23+
}
24+
25+
// ====== request
26+
@Test
27+
public void whenRequestGet_thenOK() {
28+
when().request("GET", "/users/eugenp")
29+
.then()
30+
.statusCode(200);
31+
}
32+
33+
@Test
34+
public void whenRequestHead_thenOK() {
35+
when().request("HEAD", "/users/eugenp")
36+
.then()
37+
.statusCode(200);
38+
}
39+
40+
@Test
41+
public void whenMeasureResponseTime_thenOK() {
42+
Response response = RestAssured.get("/users/eugenp");
43+
long timeInMS = response.time();
44+
long timeInS = response.timeIn(TimeUnit.SECONDS);
45+
46+
assertEquals(timeInS, timeInMS / 1000);
47+
}
48+
49+
@Test
50+
public void whenValidateResponseTime_thenSuccess() {
51+
when().get("/users/eugenp")
52+
.then()
53+
.time(lessThan(5000L));
54+
}
55+
56+
@Test
57+
public void whenValidateResponseTimeInSeconds_thenSuccess() {
58+
when().get("/users/eugenp")
59+
.then()
60+
.time(lessThan(5L), TimeUnit.SECONDS);
61+
}
62+
63+
//======= log
64+
65+
@Test
66+
public void whenLogRequest_thenOK(){
67+
given().log().all().when().get("/users/eugenp").then().statusCode(200);
68+
}
69+
70+
@Test
71+
public void whenLogResponse_thenOK(){
72+
when().get("/repos/eugenp/tutorials").then().log().body().statusCode(200);
73+
}
74+
75+
@Test
76+
public void whenLogResponseIfErrorOccurred_thenSuccess(){
77+
when().get("/users/eugenp").then().log().ifError();
78+
when().get("/users/eugenp").then().log().ifStatusCodeIsEqualTo(500);
79+
when().get("/users/eugenp").then().log().ifStatusCodeMatches(greaterThan(200));
80+
}
81+
82+
@Test
83+
public void whenLogOnlyIfValidationFailed_thenSuccess(){
84+
when().get("/users/eugenp").then().log().ifValidationFails().statusCode(200);
85+
given().log().ifValidationFails().when().get("/users/eugenp").then().statusCode(200);
86+
}
87+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"odds": [{
3+
"price": 1.30,
4+
"status": 0,
5+
"ck": 12.2,
6+
"name": "1"
7+
},
8+
{
9+
"price": 5.25,
10+
"status": 1,
11+
"ck": 13.1,
12+
"name": "X"
13+
},
14+
{
15+
"price": 2.70,
16+
"status": 0,
17+
"ck": 12.2,
18+
"name": "0"
19+
},
20+
{
21+
"price": 1.20,
22+
"status": 2,
23+
"ck": 13.1,
24+
"name": "2"
25+
}
26+
27+
]
28+
}

0 commit comments

Comments
 (0)