Proper cleanup

This commit is contained in:
Hynek Mlnarik 2023-03-09 16:31:41 +01:00 committed by Hynek Mlnařík
parent 1b9da5dff6
commit 3e58d3da8d
5 changed files with 85 additions and 99 deletions

View file

@ -62,11 +62,6 @@ public class TestCleanup {
}
public TestCleanup addCleanup(Runnable r) {
genericCleanups.add(r);
return this;
}
public TestCleanup addCleanup(AutoCloseable c) {
genericCleanups.add(() -> {
try {

View file

@ -225,7 +225,7 @@ public class SAMLLoginResponseHandlingTest extends AbstractSAMLServletAdapterTes
try (Response response = protocolMappersResource.createMapper(mapper)) {
String createdId = getCreatedId(response);
getCleanup().addCleanup((Runnable) () -> {
getCleanup().addCleanup(() -> {
protocolMappersResource.delete(createdId);
mapper.setConfig(origConfig);
protocolMappersResource.createMapper(mapper).close();

View file

@ -2202,6 +2202,8 @@ public class UserTest extends AbstractAdminTest {
@Test
public void updateUserWithNewUsername() {
switchEditUsernameAllowedOn(true);
getCleanup().addCleanup(() -> switchEditUsernameAllowedOn(false));
String id = createUser();
UserResource user = realm.users().get(id);
@ -2211,14 +2213,12 @@ public class UserTest extends AbstractAdminTest {
userRep = realm.users().get(id).toRepresentation();
assertEquals("user11", userRep.getUsername());
// Revert
switchEditUsernameAllowedOn(false);
}
@Test
public void updateUserWithoutUsername() {
switchEditUsernameAllowedOn(true);
getCleanup().addCleanup(() -> switchEditUsernameAllowedOn(false));
String id = createUser();
@ -2242,14 +2242,12 @@ public class UserTest extends AbstractAdminTest {
assertEquals("user1@localhost", rep.getEmail());
assertEquals("Firstname", rep.getFirstName());
assertEquals("Lastname", rep.getLastName());
// Revert
switchEditUsernameAllowedOn(false);
}
@Test
public void updateUserWithEmailAsUsername() {
switchRegistrationEmailAsUsername(true);
getCleanup().addCleanup(() -> switchRegistrationEmailAsUsername(false));
String id = createUser();
@ -2262,8 +2260,6 @@ public class UserTest extends AbstractAdminTest {
userRep = realm.users().get(id).toRepresentation();
assertEquals("user11@localhost", userRep.getUsername());
switchRegistrationEmailAsUsername(false);
}
@Test

View file

@ -130,11 +130,14 @@ public class RealmTest extends AbstractAdminTest {
@Test
public void renameRealm() {
getCleanup()
.addCleanup(() -> adminClient.realms().realm("old").remove())
.addCleanup(() -> adminClient.realms().realm("new").remove());
RealmRepresentation rep = new RealmRepresentation();
rep.setId("old");
rep.setRealm("old");
try {
adminClient.realms().create(rep);
rep.setRealm("new");
@ -153,13 +156,13 @@ public class RealmTest extends AbstractAdminTest {
assertEquals(Constants.AUTH_BASE_URL_PROP, accountClient.getRootUrl());
assertEquals("/realms/new/account/", accountClient.getBaseUrl());
assertEquals("/realms/new/account/*", accountClient.getRedirectUris().get(0));
} finally {
adminClient.realms().realm(rep.getRealm()).remove();
}
}
@Test
public void createRealmEmpty() {
getCleanup()
.addCleanup(() -> adminClient.realms().realm("new-realm").remove());
RealmRepresentation rep = new RealmRepresentation();
rep.setRealm("new-realm");
@ -191,8 +194,9 @@ public class RealmTest extends AbstractAdminTest {
rep.setRealm("attributes");
adminClient.realms().create(rep);
getCleanup()
.addCleanup(() -> adminClient.realms().realm("attributes").remove());
try {
RealmRepresentation rep2 = adminClient.realm("attributes").toRepresentation();
if (rep2.getAttributes() != null) {
Arrays.asList(CibaConfig.CIBA_BACKCHANNEL_TOKEN_DELIVERY_MODE,
@ -218,9 +222,6 @@ public class RealmTest extends AbstractAdminTest {
assertThat(attributesKeys.size(), CoreMatchers.is(expectedAttributesCount));
assertThat(attributesKeys, CoreMatchers.is(expectedAttributes));
} finally {
adminClient.realm("attributes").remove();
}
}
@Test
@ -231,6 +232,8 @@ public class RealmTest extends AbstractAdminTest {
rep.getSmtpServer().put("password", "secret");
adminClient.realms().create(rep);
getCleanup()
.addCleanup(() -> adminClient.realms().realm("realm-with-smtp").remove());
RealmRepresentation returned = adminClient.realm("realm-with-smtp").toRepresentation();
assertEquals(ComponentRepresentation.SECRET_VALUE, returned.getSmtpServer().get("password"));
@ -251,8 +254,6 @@ public class RealmTest extends AbstractAdminTest {
RealmRepresentation realm = adminClient.realms().findAll().stream().filter(r -> r.getRealm().equals("realm-with-smtp")).findFirst().get();
assertEquals(ComponentRepresentation.SECRET_VALUE, realm.getSmtpServer().get("password"));
adminClient.realm("realm-with-smtp").remove();
}
@Test
@ -261,6 +262,8 @@ public class RealmTest extends AbstractAdminTest {
rep.setRealm("new-realm");
adminClient.realms().create(rep);
getCleanup()
.addCleanup(() -> adminClient.realms().realm("new-realm").remove());
assertEquals(null, adminClient.realm("new-realm").toRepresentation().getPasswordPolicy());
@ -271,22 +274,17 @@ public class RealmTest extends AbstractAdminTest {
adminClient.realms().create(rep);
assertEquals("length(8)", adminClient.realm("new-realm").toRepresentation().getPasswordPolicy());
adminClient.realms().realm("new-realm").remove();
}
@Test
public void createRealmFromJson() {
RealmRepresentation rep = loadJson(getClass().getResourceAsStream("/admin-test/testrealm.json"), RealmRepresentation.class);
try {
adminClient.realms().create(rep);
getCleanup()
.addCleanup(() -> adminClient.realms().realm("admin-test-1").remove());
RealmRepresentation created = adminClient.realms().realm("admin-test-1").toRepresentation();
assertRealm(rep, created);
} finally {
adminClient.realms().realm("admin-test-1").remove();
}
}
//KEYCLOAK-6146
@ -312,7 +310,6 @@ public class RealmTest extends AbstractAdminTest {
@Test
public void createRealmWithPasswordPolicyFromJsonWithValidPasswords() {
RealmRepresentation rep = loadJson(getClass().getResourceAsStream("/import/testrealm-keycloak-6146.json"), RealmRepresentation.class);
rep.setId(KeycloakModelUtils.generateId());
try (Creator<RealmResource> c = Creator.create(adminClient, rep)) {
RealmRepresentation created = c.resource().toRepresentation();
assertRealm(rep, created);
@ -354,6 +351,10 @@ public class RealmTest extends AbstractAdminTest {
*/
@Test
public void renameRealmTest() throws Exception {
getCleanup()
.addCleanup(() -> adminClient.realms().realm("test-immutable").remove())
.addCleanup(() -> adminClient.realms().realm("test-immutable-old").remove());
RealmRepresentation realm1 = new RealmRepresentation();
realm1.setRealm("test-immutable");
adminClient.realms().create(realm1);
@ -366,9 +367,6 @@ public class RealmTest extends AbstractAdminTest {
realm2.setRealm("test-immutable");
adminClient.realms().create(realm2);
assertThat(adminClient.realms().realm("test-immutable").toRepresentation(), notNullValue());
adminClient.realms().realm("test-immutable-old").remove();
adminClient.realms().realm("test-immutable").remove();
}
private RealmEventsConfigRepresentation copyRealmEventsConfigRepresentation(RealmEventsConfigRepresentation rep) {
@ -863,14 +861,12 @@ public class RealmTest extends AbstractAdminTest {
RealmRepresentation rep = new RealmRepresentation();
rep.setRealm("new-realm");
try {
adminClient.realms().create(rep);
getCleanup()
.addCleanup(() -> adminClient.realms().realm("new-realm").remove());
assertEquals(Constants.DEFAULT_SIGNATURE_ALGORITHM, adminClient.realm("master").toRepresentation().getDefaultSignatureAlgorithm());
assertEquals(Constants.DEFAULT_SIGNATURE_ALGORITHM, adminClient.realm("new-realm").toRepresentation().getDefaultSignatureAlgorithm());
} finally {
adminClient.realms().realm(rep.getRealm()).remove();
}
}
@Test
@ -878,10 +874,11 @@ public class RealmTest extends AbstractAdminTest {
RealmRepresentation rep = new RealmRepresentation();
rep.setRealm("new-realm");
try {
adminClient.realms().create(rep);
RealmResource realm = adminClient.realms().realm("new-realm");
getCleanup()
.addCleanup(() -> adminClient.realms().realm("new-realm").remove());
rep = realm.toRepresentation();
@ -897,9 +894,6 @@ public class RealmTest extends AbstractAdminTest {
supportedApplications = rep.getOtpSupportedApplications();
assertThat(supportedApplications, hasSize(1));
assertThat(supportedApplications, containsInAnyOrder("totpAppFreeOTPName"));
} finally {
adminClient.realms().realm(rep.getRealm()).remove();
}
}
private void setupTestAppAndUser() {

View file

@ -129,6 +129,7 @@ public class ComponentExportImportTest extends AbstractAuthTest {
}
});
getCleanup().addCleanup(testRealmResource()::remove);
testRealmResource().remove();
try {