-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
HibernateTenancyFunctionalityTest.java
167 lines (127 loc) · 6.61 KB
/
HibernateTenancyFunctionalityTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package io.quarkus.it.hibernate.multitenancy.fruit;
import static io.restassured.RestAssured.given;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.arrayWithSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import jakarta.ws.rs.core.Response.Status;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.http.ContentType;
import io.restassured.response.Response;
/**
* Test various Hibernate Multitenancy operations running in Quarkus
*/
@QuarkusTest
public class HibernateTenancyFunctionalityTest {
@BeforeEach
public void cleanup() {
deleteIfExists("", "Dragonfruit");
deleteIfExists("", "Gooseberry");
deleteIfExists("/mycompany", "Damson");
deleteIfExists("/mycompany", "Grapefruit");
}
@Test
public void testAddDeleteDefaultTenant() throws Exception {
// Create fruit for default 'base' tenant
given().with().body(new Fruit("Delete")).contentType(ContentType.JSON).when().post("/fruits").then()
.assertThat().statusCode(is(Status.CREATED.getStatusCode()));
// Get it
Fruit newFruit = findByName("", "Delete");
// Delete it
given().pathParam("id", newFruit.getId()).contentType("application/json").accept("application/json")
.when().delete("/fruits/{id}").then().assertThat().statusCode(is(Status.NO_CONTENT.getStatusCode()));
}
@Test
public void testGetFruitsDefaultTenant() throws Exception {
Fruit[] fruits = given().when().get("/fruits").then().assertThat()
.statusCode(is(Status.OK.getStatusCode())).extract()
.as(Fruit[].class);
assertThat(fruits, arrayContaining(new Fruit(2, "Apple"), new Fruit(3, "Banana"), new Fruit(1, "Cherry")));
}
@Test
public void testGetFruitsTenantMycompany() throws Exception {
Fruit[] fruits = given().when().get("/mycompany/fruits").then().assertThat()
.statusCode(is(Status.OK.getStatusCode())).extract()
.as(Fruit[].class);
assertThat(fruits, arrayWithSize(3));
assertThat(fruits, arrayContaining(new Fruit(2, "Apricots"), new Fruit(1, "Avocado"), new Fruit(3, "Blackberries")));
}
@Test
public void testPostFruitDefaultTenant() throws Exception {
// Create fruit for default 'base' tenant
Fruit newFruit = new Fruit("Dragonfruit");
given().with().body(newFruit).contentType(ContentType.JSON).when().post("/fruits").then()
.assertThat()
.statusCode(is(Status.CREATED.getStatusCode()));
// Getting it directly should return the new fruit
Fruit dragonFruit = findByName("", newFruit.getName());
assertThat(dragonFruit, not(is(nullValue())));
// Getting fruit list should also contain the new fruit
Fruit[] baseFruits = given().when().get("/fruits").then().assertThat()
.statusCode(is(Status.OK.getStatusCode())).extract()
.as(Fruit[].class);
assertThat(baseFruits, arrayWithSize(4));
assertThat(baseFruits,
arrayContaining(new Fruit(2, "Apple"), new Fruit(3, "Banana"), new Fruit(1, "Cherry"), dragonFruit));
// The other tenant should NOT have the new fruit
Fruit[] mycompanyFruits = given().when().get("/mycompany/fruits").then().assertThat()
.statusCode(is(Status.OK.getStatusCode()))
.extract().as(Fruit[].class);
assertThat(mycompanyFruits, arrayWithSize(3));
assertThat(mycompanyFruits,
arrayContaining(new Fruit(2, "Apricots"), new Fruit(1, "Avocado"), new Fruit(3, "Blackberries")));
// Getting it directly should also NOT return the new fruit
assertThat(findByName("/mycompany", newFruit.getName()), is(nullValue()));
}
@Test
public void testUpdateFruitDefaultTenant() throws Exception {
// Create fruits for both tenants
Fruit newFruitBase = new Fruit("Dragonfruit");
given().with().body(newFruitBase).contentType(ContentType.JSON).when().post("/fruits").then()
.assertThat()
.statusCode(is(Status.CREATED.getStatusCode()));
Fruit baseFruit = findByName("", newFruitBase.getName());
assertThat(baseFruit, not(is(nullValue())));
Fruit newFruitMycompany = new Fruit("Damson");
given().with().body(newFruitMycompany).contentType(ContentType.JSON).when().post("/mycompany/fruits")
.then().assertThat()
.statusCode(is(Status.CREATED.getStatusCode()));
Fruit mycompanyFruit = findByName("/mycompany", newFruitMycompany.getName());
assertThat(mycompanyFruit, not(is(nullValue())));
// Update both
String baseFruitName = "Gooseberry";
baseFruit.setName(baseFruitName);
given().with().body(baseFruit).contentType(ContentType.JSON).when()
.put("/fruits/{id}", baseFruit.getId()).then().assertThat()
.statusCode(is(Status.OK.getStatusCode()));
String mycompanyFruitName = "Grapefruit";
mycompanyFruit.setName(mycompanyFruitName);
given().with().body(mycompanyFruit).contentType(ContentType.JSON).when()
.put("/mycompany/fruits/{id}", mycompanyFruit.getId())
.then().assertThat().statusCode(is(Status.OK.getStatusCode()));
// Check if we can get them back and they only exist for one tenant
assertThat(findByName("", baseFruitName), is(not(nullValue())));
assertThat(findByName("/mycompany", baseFruitName), is(nullValue()));
assertThat(findByName("/mycompany", mycompanyFruitName), is(not(nullValue())));
assertThat(findByName("", mycompanyFruitName), is(nullValue()));
}
private Fruit findByName(String tenantPath, String name) {
Response response = given().when().get(tenantPath + "/fruitsFindBy?type=name&value={name}", name);
if (response.getStatusCode() == Status.OK.getStatusCode()) {
return response.as(Fruit.class);
}
return null;
}
private void deleteIfExists(String tenantPath, String name) {
Fruit dragonFruit = findByName(tenantPath, name);
if (dragonFruit != null) {
given().pathParam("id", dragonFruit.getId()).when().delete(tenantPath + "/fruits/{id}").then()
.assertThat()
.statusCode(is(Status.NO_CONTENT.getStatusCode()));
}
}
}