Skip to content
Snippets Groups Projects
Commit ba9bac5b authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-5951 add realmExist before realm delete' (#27) from OZG-5951 into master

parents 5f3e5a8a 58d31552
No related branches found
No related tags found
No related merge requests found
...@@ -69,6 +69,9 @@ public class KeycloakRealmReconciler implements Reconciler<OzgCloudKeycloakRealm ...@@ -69,6 +69,9 @@ public class KeycloakRealmReconciler implements Reconciler<OzgCloudKeycloakRealm
LOG.info("keep data"); LOG.info("keep data");
return DeleteControl.defaultDelete(); return DeleteControl.defaultDelete();
} }
if (!service.realmExists(realm.getMetadata().getName())) {
return DeleteControl.defaultDelete();
}
return deleteRealm(realm); return deleteRealm(realm);
} }
......
...@@ -87,4 +87,8 @@ class KeycloakRealmService { ...@@ -87,4 +87,8 @@ class KeycloakRealmService {
public void deleteRealm(String realmName) { public void deleteRealm(String realmName) {
remoteService.deleteRealm(realmName); remoteService.deleteRealm(realmName);
} }
public boolean realmExists(String realmName) {
return keycloakGenericRemoteService.realmExists(realmName);
}
} }
...@@ -23,11 +23,9 @@ ...@@ -23,11 +23,9 @@
*/ */
package de.ozgcloud.operator.keycloak.realm; package de.ozgcloud.operator.keycloak.realm;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.doThrow; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
...@@ -49,6 +47,8 @@ class KeycloakRealmReconcilerTest { ...@@ -49,6 +47,8 @@ class KeycloakRealmReconcilerTest {
@Mock @Mock
private KeycloakRealmService service; private KeycloakRealmService service;
private final String REALM = OzgCloudKeycloakRealmTestFactory.METADATA_NAMESPACE;
@DisplayName("Reconcile") @DisplayName("Reconcile")
@Nested @Nested
class TestReconcile { class TestReconcile {
...@@ -59,7 +59,7 @@ class KeycloakRealmReconcilerTest { ...@@ -59,7 +59,7 @@ class KeycloakRealmReconcilerTest {
reconciler.reconcile(realm, null); reconciler.reconcile(realm, null);
verify(service).createOrUpdateRealm(realm.getSpec(), OzgCloudKeycloakRealmTestFactory.METADATA_NAMESPACE); verify(service).createOrUpdateRealm(realm.getSpec(), REALM);
} }
@Test @Test
...@@ -93,7 +93,7 @@ class KeycloakRealmReconcilerTest { ...@@ -93,7 +93,7 @@ class KeycloakRealmReconcilerTest {
void shouldNotCallService() { void shouldNotCallService() {
reconciler.cleanup(realm, null); reconciler.cleanup(realm, null);
verify(service, never()).deleteRealm(OzgCloudKeycloakRealmTestFactory.METADATA_NAMESPACE); verify(service, never()).deleteRealm(REALM);
} }
@Test @Test
...@@ -111,21 +111,50 @@ class KeycloakRealmReconcilerTest { ...@@ -111,21 +111,50 @@ class KeycloakRealmReconcilerTest {
private final OzgCloudKeycloakRealm realm = OzgCloudKeycloakRealmTestFactory.create(); private final OzgCloudKeycloakRealm realm = OzgCloudKeycloakRealmTestFactory.create();
@Test @Test
void shouldCallDeleteRealm() { void shouldDeleteRealmIfRealmExists() {
when(service.realmExists(any())).thenReturn(true);
reconciler.cleanup(realm, null); reconciler.cleanup(realm, null);
verify(reconciler).deleteRealm(realm); verify(reconciler).deleteRealm(realm);
} }
@Test @Test
void shouldReturnValueFromDeleteUser() { void shouldReturnValueFromDeleteRealm() {
DeleteControl expected = DeleteControl.defaultDelete(); DeleteControl expected = DeleteControl.defaultDelete();
when(reconciler.deleteRealm(realm)).thenReturn(expected); when(reconciler.deleteRealm(realm)).thenReturn(expected);
when(service.realmExists(any())).thenReturn(true);
DeleteControl response = reconciler.cleanup(realm, null); DeleteControl response = reconciler.cleanup(realm, null);
assertThat(response).isEqualTo(expected); assertThat(response).isEqualTo(expected);
} }
@Test
void shouldCallRealmExists() {
reconciler.cleanup(realm, null);
verify(service).realmExists(realm.getMetadata().getName());
}
@Test
void shouldNotDeleteRealmIfRealmNotExists() {
when(service.realmExists(any())).thenReturn(false);
reconciler.cleanup(realm, null);
verify(reconciler, never()).deleteRealm(realm);
}
@Test
void shouldReturnDeleteControl() {
when(service.realmExists(any())).thenReturn(false);
var control = reconciler.cleanup(realm, null);
assertThat(control).usingRecursiveComparison().isEqualTo(DeleteControl.defaultDelete());
}
} }
@DisplayName("test delete") @DisplayName("test delete")
...@@ -138,7 +167,7 @@ class KeycloakRealmReconcilerTest { ...@@ -138,7 +167,7 @@ class KeycloakRealmReconcilerTest {
void shouldCallServiceDelete() { void shouldCallServiceDelete() {
reconciler.deleteRealm(realm); reconciler.deleteRealm(realm);
verify(service).deleteRealm(OzgCloudKeycloakRealmTestFactory.METADATA_NAMESPACE); verify(service).deleteRealm(REALM);
} }
@Test @Test
...@@ -151,7 +180,7 @@ class KeycloakRealmReconcilerTest { ...@@ -151,7 +180,7 @@ class KeycloakRealmReconcilerTest {
@Test @Test
void shouldRescheduleOnError() { void shouldRescheduleOnError() {
doThrow(RuntimeException.class).when(service) doThrow(RuntimeException.class).when(service)
.deleteRealm(OzgCloudKeycloakRealmTestFactory.METADATA_NAMESPACE); .deleteRealm(REALM);
var control = reconciler.deleteRealm(realm); var control = reconciler.deleteRealm(realm);
......
...@@ -232,4 +232,15 @@ class KeycloakRealmServiceTest { ...@@ -232,4 +232,15 @@ class KeycloakRealmServiceTest {
verify(remoteService).deleteRealm(REALM_NAME); verify(remoteService).deleteRealm(REALM_NAME);
} }
} }
@Nested
class TestRealmExists {
@Test
void shouldCallGenericRemoteServiceRealmExists() {
service.realmExists(REALM_NAME);
verify(keycloakGenericRemoteService).realmExists(REALM_NAME);
}
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment