Skip to content

Commit 491097a

Browse files
committed
use assertThatExceptionOfType instead of assertThatThrownBy
1 parent 7649ee5 commit 491097a

File tree

2 files changed

+46
-68
lines changed

2 files changed

+46
-68
lines changed

operator/src/test/java/it/aboutbits/postgresql/crd/defaultprivilege/DefaultPrivilegeReconcilerTest.java

Lines changed: 23 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import static it.aboutbits.postgresql.crd.defaultprivilege.DefaultPrivilegeObjectType.SEQUENCE;
4343
import static it.aboutbits.postgresql.crd.defaultprivilege.DefaultPrivilegeObjectType.TABLE;
4444
import static org.assertj.core.api.Assertions.assertThat;
45-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
45+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4646
import static org.awaitility.Awaitility.await;
4747

4848
@NullMarked
@@ -111,16 +111,15 @@ void failWhenDatabaseIsBlankOrEmptyString(
111111
String blankOrEmptyString
112112
) {
113113
// then
114-
assertThatThrownBy(() ->
114+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() ->
115115
// given / when
116116
given.one()
117117
.defaultPrivilege()
118118
.withDatabase(blankOrEmptyString)
119119
.withObjectType(SCHEMA)
120120
.withPrivileges(USAGE)
121121
.apply()
122-
).isInstanceOf(KubernetesClientException.class)
123-
.hasMessageContaining("The DefaultPrivilege database must not be empty.");
122+
).withMessageContaining("The DefaultPrivilege database must not be empty.");
124123
}
125124

126125
@Disabled("Test is way too slow with all combinations")
@@ -131,16 +130,15 @@ void failWhenRoleIsBlankOrEmptyString(
131130
String blankOrEmptyString
132131
) {
133132
// then
134-
assertThatThrownBy(() ->
133+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() ->
135134
// given / when
136135
given.one()
137136
.defaultPrivilege()
138137
.withRole(blankOrEmptyString)
139138
.withObjectType(SCHEMA)
140139
.withPrivileges(USAGE)
141140
.apply()
142-
).isInstanceOf(KubernetesClientException.class)
143-
.hasMessageContaining("The DefaultPrivilege role must not be empty.");
141+
).withMessageContaining("The DefaultPrivilege role must not be empty.");
144142
}
145143

146144
@Disabled("Test is way too slow with all combinations")
@@ -151,16 +149,15 @@ void failWhenOwnerIsBlankOrEmptyString(
151149
String blankOrEmptyString
152150
) {
153151
// then
154-
assertThatThrownBy(() ->
152+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() ->
155153
// given / when
156154
given.one()
157155
.defaultPrivilege()
158156
.withOwner(blankOrEmptyString)
159157
.withObjectType(SCHEMA)
160158
.withPrivileges(USAGE)
161159
.apply()
162-
).isInstanceOf(KubernetesClientException.class)
163-
.hasMessageContaining("The DefaultPrivilege owner must not be empty.");
160+
).withMessageContaining("The DefaultPrivilege owner must not be empty.");
164161
}
165162

166163
@Disabled("Test is way too slow with all combinations")
@@ -171,31 +168,29 @@ void failWhenSchemaIsBlankOrEmptyString(
171168
String blankOrEmptyString
172169
) {
173170
// then
174-
assertThatThrownBy(() ->
171+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() ->
175172
// given / when
176173
given.one()
177174
.defaultPrivilege()
178175
.withSchema(blankOrEmptyString)
179176
.withObjectType(SCHEMA)
180177
.withPrivileges(USAGE)
181178
.apply()
182-
).isInstanceOf(KubernetesClientException.class)
183-
.hasMessageContaining("The DefaultPrivilege schema must not be empty.");
179+
).withMessageContaining("The DefaultPrivilege schema must not be empty.");
184180
}
185181

186182
@Test
187183
@DisplayName("Should fail when the privileges are an empty List (CEL rule)")
188184
void failWhenPrivilegesAreAnEmptyList() {
189185
// then
190-
assertThatThrownBy(() ->
186+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() ->
191187
// given / when
192188
given.one()
193189
.defaultPrivilege()
194190
.withObjectType(SCHEMA)
195191
.withPrivileges(List.of())
196192
.apply()
197-
).isInstanceOf(KubernetesClientException.class)
198-
.hasMessageContaining("The DefaultPrivilege privileges must not be empty.");
193+
).withMessageContaining("The DefaultPrivilege privileges must not be empty.");
199194
}
200195
}
201196

@@ -212,16 +207,15 @@ void failWhenDatabaseChanges() {
212207
.returnFirst();
213208

214209
// then
215-
assertThatThrownBy(() -> {
210+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() -> {
216211
// when
217212
item.getSpec().setDatabase("new-database");
218213

219214
kubernetesClient.resources(DefaultPrivilege.class)
220215
.inNamespace(item.getMetadata().getNamespace())
221216
.withName(item.getMetadata().getName())
222217
.patch(item);
223-
}).isInstanceOf(KubernetesClientException.class)
224-
.hasMessageContaining("The DefaultPrivilege database is immutable.");
218+
}).withMessageContaining("The DefaultPrivilege database is immutable.");
225219
}
226220

227221
@Test
@@ -235,16 +229,15 @@ void failWhenRoleChanges() {
235229
.returnFirst();
236230

237231
// then
238-
assertThatThrownBy(() -> {
232+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() -> {
239233
// when
240234
item.getSpec().setRole("new-role");
241235

242236
kubernetesClient.resources(DefaultPrivilege.class)
243237
.inNamespace(item.getMetadata().getNamespace())
244238
.withName(item.getMetadata().getName())
245239
.patch(item);
246-
}).isInstanceOf(KubernetesClientException.class)
247-
.hasMessageContaining("The DefaultPrivilege role is immutable.");
240+
}).withMessageContaining("The DefaultPrivilege role is immutable.");
248241
}
249242

250243
@Test
@@ -258,16 +251,15 @@ void failWhenOwnerChanges() {
258251
.returnFirst();
259252

260253
// then
261-
assertThatThrownBy(() -> {
254+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() -> {
262255
// when
263256
item.getSpec().setOwner("new-owner");
264257

265258
kubernetesClient.resources(DefaultPrivilege.class)
266259
.inNamespace(item.getMetadata().getNamespace())
267260
.withName(item.getMetadata().getName())
268261
.patch(item);
269-
}).isInstanceOf(KubernetesClientException.class)
270-
.hasMessageContaining("The DefaultPrivilege owner is immutable.");
262+
}).withMessageContaining("The DefaultPrivilege owner is immutable.");
271263
}
272264

273265
@Test
@@ -281,16 +273,15 @@ void failWhenSchemaChanges() {
281273
.returnFirst();
282274

283275
// then
284-
assertThatThrownBy(() -> {
276+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() -> {
285277
// when
286278
item.getSpec().setSchema("new-schema");
287279

288280
kubernetesClient.resources(DefaultPrivilege.class)
289281
.inNamespace(item.getMetadata().getNamespace())
290282
.withName(item.getMetadata().getName())
291283
.patch(item);
292-
}).isInstanceOf(KubernetesClientException.class)
293-
.hasMessageContaining("The DefaultPrivilege schema is immutable.");
284+
}).withMessageContaining("The DefaultPrivilege schema is immutable.");
294285
}
295286

296287
@Test
@@ -304,16 +295,15 @@ void failWhenObjectTypeChanges() {
304295
.returnFirst();
305296

306297
// then
307-
assertThatThrownBy(() -> {
298+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() -> {
308299
// when
309300
item.getSpec().setObjectType(SEQUENCE);
310301

311302
kubernetesClient.resources(DefaultPrivilege.class)
312303
.inNamespace(item.getMetadata().getNamespace())
313304
.withName(item.getMetadata().getName())
314305
.patch(item);
315-
}).isInstanceOf(KubernetesClientException.class)
316-
.hasMessageContaining("The DefaultPrivilege objectType is immutable.");
306+
}).withMessageContaining("The DefaultPrivilege objectType is immutable.");
317307
}
318308
}
319309

@@ -323,16 +313,15 @@ class RelatedFields {
323313
@DisplayName("Should fail when objectType is SCHEMA but schema is set (CEL rule)")
324314
void failWhenDatabaseHasSchema() {
325315
// then
326-
assertThatThrownBy(() ->
316+
assertThatExceptionOfType(KubernetesClientException.class).isThrownBy(() ->
327317
// given / when
328318
given.one()
329319
.defaultPrivilege()
330320
.withSchema("some-schema")
331321
.withObjectType(SCHEMA)
332322
.withPrivileges(USAGE)
333323
.returnFirst()
334-
).isInstanceOf(KubernetesClientException.class)
335-
.hasMessageContaining("The DefaultPrivilege schema must be not set if objectType is 'schema'");
324+
).withMessageContaining("The DefaultPrivilege schema must be not set if objectType is 'schema'");
336325
}
337326

338327
@Test

0 commit comments

Comments
 (0)