Skip to content

Commit 35234ad

Browse files
Add missing tutorial tests (#2694)
* add missing tutorial tests * add tutorial tests for transformation and orchestration
1 parent 3ff329d commit 35234ad

11 files changed

Lines changed: 356 additions & 5 deletions
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.predic8.membrane.tutorials.advanced;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static io.restassured.RestAssured.given;
6+
7+
public class ChooseTutorialTest extends AbstractAdvancedTutorialTest {
8+
9+
@Override
10+
protected String getTutorialYaml() {
11+
return "65-Choose.yaml";
12+
}
13+
14+
15+
@Test
16+
void withoutHeader_returns400() {
17+
// @formatter:off
18+
given()
19+
.when()
20+
.get("http://localhost:2000/")
21+
.then()
22+
.statusCode(400);
23+
// @formatter:on
24+
}
25+
26+
@Test
27+
void withXFoo_returns200() {
28+
// @formatter:off
29+
given()
30+
.header("X-Foo", "1")
31+
.when()
32+
.get("http://localhost:2000/")
33+
.then()
34+
.statusCode(200);
35+
// @formatter:on
36+
}
37+
38+
@Test
39+
void withXBar_returns300_andConnectionClosed() {
40+
// @formatter:off
41+
given()
42+
.header("X-Bar", "1")
43+
.when()
44+
.get("http://localhost:2000/")
45+
.then()
46+
.statusCode(300)
47+
.header("Connection", "close");
48+
// @formatter:on
49+
}
50+
51+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.predic8.membrane.tutorials.advanced;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static io.restassured.RestAssured.given;
6+
7+
public class IfElseTutorialTest extends AbstractAdvancedTutorialTest {
8+
9+
@Override
10+
protected String getTutorialYaml() {
11+
return "61-If-Else.yaml";
12+
}
13+
14+
@Test
15+
void withoutHeader_returns400() {
16+
// @formatter:off
17+
given()
18+
.when()
19+
.get("http://localhost:2000/")
20+
.then()
21+
.statusCode(400);
22+
// @formatter:on
23+
}
24+
25+
@Test
26+
void withHeader_returns200() {
27+
// @formatter:off
28+
given()
29+
.header("X-Foo", "1")
30+
.when()
31+
.get("http://localhost:2000/")
32+
.then()
33+
.statusCode(200);
34+
// @formatter:on
35+
}
36+
37+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.predic8.membrane.tutorials.advanced;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.PrintStream;
7+
8+
import static io.restassured.RestAssured.given;
9+
import static org.junit.jupiter.api.Assertions.assertFalse;
10+
import static org.junit.jupiter.api.Assertions.assertTrue;
11+
12+
public class RemovingHttpHeadersTutorialTest extends AbstractAdvancedTutorialTest {
13+
14+
@Override
15+
protected String getTutorialYaml() {
16+
return "80-Removing-HTTP-Headers.yaml";
17+
}
18+
19+
@Test
20+
void logsBeforeAndAfterHeaders() {
21+
synchronized (System.out) {
22+
ByteArrayOutputStream out = new ByteArrayOutputStream();
23+
PrintStream original = System.out;
24+
System.setOut(new PrintStream(out));
25+
26+
try {
27+
// @formatter:off
28+
given()
29+
.header("X-Foo", "1")
30+
.header("X-bar", "2")
31+
.when()
32+
.get("http://localhost:2000")
33+
.then()
34+
.statusCode(200);
35+
// @formatter:on
36+
} finally {
37+
System.setOut(original);
38+
}
39+
40+
String console = out.toString();
41+
42+
int beforeIdx = lastIndexOfIgnoreCase(console, "Before:");
43+
int afterIdx = lastIndexOfIgnoreCase(console, "After:");
44+
assertTrue(beforeIdx >= 0, "Missing 'Before:' log.");
45+
assertTrue(afterIdx > beforeIdx, "Missing 'After:' log or wrong order.");
46+
47+
String before = console.substring(beforeIdx, afterIdx);
48+
String after = console.substring(afterIdx);
49+
50+
assertTrue(containsIgnoreCase(before, "X-Foo: 1"));
51+
assertTrue(containsIgnoreCase(before, "X-bar: 2"));
52+
53+
assertTrue(containsIgnoreCase(after, "Host:"));
54+
assertTrue(containsIgnoreCase(after, "Accept:"));
55+
assertTrue(containsIgnoreCase(after, "X-Foo: 1"));
56+
57+
assertFalse(containsIgnoreCase(after, "X-bar:"));
58+
assertFalse(containsIgnoreCase(after, "User-Agent:"));
59+
assertFalse(containsIgnoreCase(after, "X-Forwarded-"));
60+
}
61+
}
62+
63+
private static boolean containsIgnoreCase(String haystack, String needle) {
64+
return haystack.toLowerCase().contains(needle.toLowerCase());
65+
}
66+
67+
private static int lastIndexOfIgnoreCase(String haystack, String needle) {
68+
return haystack.toLowerCase().lastIndexOf(needle.toLowerCase());
69+
}
70+
71+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.predic8.membrane.tutorials.orchestration;
2+
3+
import com.predic8.membrane.tutorials.AbstractMembraneTutorialTest;
4+
5+
public abstract class AbstractOrchestrationTutorialTest extends AbstractMembraneTutorialTest {
6+
7+
@Override
8+
protected String getTutorialDir() {
9+
return "orchestration";
10+
}
11+
12+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.predic8.membrane.tutorials.orchestration;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static io.restassured.RestAssured.given;
6+
import static org.hamcrest.Matchers.equalTo;
7+
8+
public class OrchestrationTutorialTest extends AbstractOrchestrationTutorialTest {
9+
10+
@Override
11+
protected String getTutorialYaml() {
12+
return "10-Orchestration.yaml";
13+
}
14+
15+
@Test
16+
void books_areReturnedById() {
17+
// @formatter:off
18+
given()
19+
.when()
20+
.get("http://localhost:2000/books/{id}", "OL29474405M")
21+
.then()
22+
.statusCode(200)
23+
.body("title", equalTo("So Long, and Thanks for All the Fish"))
24+
.body("author", equalTo("Douglas Adams"));
25+
26+
given()
27+
.when()
28+
.get("http://localhost:2000/books/{id}", "OL26333978M")
29+
.then()
30+
.statusCode(200)
31+
.body("title", equalTo("Foucault's pendulum"))
32+
.body("author", equalTo("Umberto Eco"));
33+
// @formatter:on
34+
}
35+
36+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.predic8.membrane.tutorials.transformation;
2+
3+
import com.predic8.membrane.tutorials.AbstractMembraneTutorialTest;
4+
5+
public abstract class AbstractTransformationTutorialTest extends AbstractMembraneTutorialTest {
6+
7+
@Override
8+
protected String getTutorialDir() {
9+
return "transformation";
10+
}
11+
12+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.predic8.membrane.tutorials.transformation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static io.restassured.RestAssured.given;
6+
import static io.restassured.RestAssured.withArgs;
7+
import static org.hamcrest.Matchers.*;
8+
9+
public class GetToPostTutorialTest extends AbstractTransformationTutorialTest {
10+
11+
@Override
12+
protected String getTutorialYaml() {
13+
return "20-GET-to-POST.yaml";
14+
}
15+
16+
@Test
17+
void addedProductIsPresentInUpstreamList() {
18+
var added =
19+
// @formatter:off
20+
given()
21+
.queryParam("name", "Lemon")
22+
.queryParam("price", "0.30")
23+
.when()
24+
.get("http://localhost:2000/add")
25+
.then()
26+
.statusCode(201)
27+
.body("id", notNullValue())
28+
.body("name", equalTo("Lemon"))
29+
.body("price", equalTo(0.3F))
30+
.body("self_link", startsWith("/shop/v2/products/"))
31+
.extract()
32+
.jsonPath();
33+
// @formatter:on
34+
35+
int id = added.getInt("id");
36+
37+
// @formatter:off
38+
given()
39+
.relaxedHTTPSValidation()
40+
.queryParam("limit", 1000)
41+
.when()
42+
.get("https://api.predic8.de/shop/v2/products")
43+
.then()
44+
.statusCode(200)
45+
.body("products.find { it.id == %s }.name", withArgs(id), equalTo("Lemon"))
46+
.body("products.find { it.id == %s }.self_link", withArgs(id), equalTo("/shop/v2/products/" + id));
47+
// @formatter:on
48+
}
49+
50+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.predic8.membrane.tutorials.transformation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.util.List;
6+
7+
import static io.restassured.RestAssured.given;
8+
import static org.hamcrest.Matchers.*;
9+
import static org.junit.jupiter.api.Assertions.assertTrue;
10+
11+
public class PostToGetTutorialTest extends AbstractTransformationTutorialTest {
12+
13+
@Override
14+
protected String getTutorialYaml() {
15+
return "10-POST-to-GET.yaml";
16+
}
17+
18+
@Test
19+
void verifyProductListIsSortedAfterPost() {
20+
var res =
21+
// @formatter:off
22+
given()
23+
.contentType("application/json")
24+
.body("""
25+
{"limit":100,"sort":"name"}"""
26+
)
27+
.when()
28+
.post("http://localhost:2000/shop/v2/products")
29+
.then()
30+
.statusCode(200)
31+
.body("meta", notNullValue())
32+
.body("meta.count", greaterThan(0))
33+
.body("products", notNullValue())
34+
.body("products.size()", greaterThan(0))
35+
.body("products[0].name", not(empty()))
36+
.body("products[0].self_link", startsWith("/shop/v2/products/"))
37+
.extract();
38+
// @formatter:on
39+
40+
List<String> names = res.jsonPath().getList("products.name", String.class);
41+
42+
for (int i = 1; i < names.size(); i++) {
43+
String prev = names.get(i - 1);
44+
String cur = names.get(i);
45+
if (prev == null || cur == null) continue;
46+
assertTrue(prev.compareToIgnoreCase(cur) <= 0);
47+
}
48+
}
49+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.predic8.membrane.tutorials.transformation;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static io.restassured.RestAssured.given;
6+
import static org.hamcrest.Matchers.*;
7+
8+
public class TemplateJsonFromMapTutorialTest extends AbstractTransformationTutorialTest {
9+
10+
@Override
11+
protected String getTutorialYaml() {
12+
return "30-Template-JSON-from-MAP.yaml";
13+
}
14+
15+
@Test
16+
void responseMap() {
17+
// @formatter:off
18+
given()
19+
.when()
20+
.get("http://localhost:2000")
21+
.then()
22+
.statusCode(200)
23+
.contentType(containsString("application/json"))
24+
.body("Host", not(empty()))
25+
.body("Accept", notNullValue())
26+
.body("User-Agent", not(empty()))
27+
.body("X-Forwarded-For", not(empty()))
28+
.body("X-Forwarded-Proto", anyOf(equalTo("http"), equalTo("https")))
29+
.body("X-Forwarded-Host", not(empty()));
30+
// @formatter:on
31+
}
32+
33+
}

distribution/tutorials/advanced/61-If-Else.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# Tutorial: If Else
44
#
55
# Try:
6-
# 1.) curl localhost:2000
7-
# 2.) curl -H "X-Foo: 1" localhost:2000
6+
# 1.) curl -v localhost:2000
7+
# 2.) curl -v -H "X-Foo: 1" localhost:2000
88
#
99
api:
1010
port: 2000

0 commit comments

Comments
 (0)