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

OZG-5400 add unittests for realm update

parent 28de3fe8
No related branches found
No related tags found
No related merge requests found
...@@ -51,7 +51,7 @@ class KeycloakRealmReconcilerTest { ...@@ -51,7 +51,7 @@ class KeycloakRealmReconcilerTest {
class TestReconcile { class TestReconcile {
@Test @Test
void shouldCallServiceAddRealm() { void shouldCallServiceCreateOrUpdateRealm() {
OzgCloudKeycloakRealm realm = OzgCloudKeycloakRealmTestFactory.create(); OzgCloudKeycloakRealm realm = OzgCloudKeycloakRealmTestFactory.create();
reconciler.reconcile(realm, null); reconciler.reconcile(realm, null);
......
...@@ -26,20 +26,28 @@ package de.ozgcloud.operator.keycloak.realm; ...@@ -26,20 +26,28 @@ package de.ozgcloud.operator.keycloak.realm;
import static org.mockito.ArgumentMatchers.*; import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.RealmRepresentation;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import de.ozgcloud.operator.keycloak.KeycloakGenericRemoteService; import de.ozgcloud.operator.keycloak.KeycloakGenericRemoteService;
import de.ozgcloud.operator.keycloak.client.ClientRepresentationTestFactory;
import de.ozgcloud.operator.keycloak.client.OzgCloudKeycloakClientSpecTestFactory;
class KeycloakRealmServiceTest { class KeycloakRealmServiceTest {
private static final OzgCloudKeycloakRealmSpec REALM = OzgCloudKeycloakRealmSpecTestFactory.create(); private static final OzgCloudKeycloakRealmSpec REALM = OzgCloudKeycloakRealmSpecTestFactory.create();
private static final String REALM_NAME = "TestRealmName"; private static final String REALM_NAME = "TestRealmName";
private static final String TEST_NAMESPACE = "TestNamespace";
@Spy @Spy
@InjectMocks @InjectMocks
...@@ -57,6 +65,61 @@ class KeycloakRealmServiceTest { ...@@ -57,6 +65,61 @@ class KeycloakRealmServiceTest {
@Mock @Mock
private KeycloakGenericRemoteService keycloakGenericRemoteService; private KeycloakGenericRemoteService keycloakGenericRemoteService;
@Nested
class TestCreanOrUpdateRealm {
@Test
void shouldCallCreateRealmMethodIfNotExists() {
when(keycloakGenericRemoteService.getRealmRepresentation(REALM_NAME)).thenReturn(Optional.empty());
service.createOrUpdateRealm(REALM, REALM_NAME);
verify(service).createRealm(REALM, REALM_NAME);
}
@Test
void shouldCallUpdateRealmIfAlreadyExists() {
var existingRealm = RealmRepresentationTestFactory.create();
when(keycloakGenericRemoteService.getRealmRepresentation(REALM_NAME)).thenReturn(Optional.of(existingRealm));
service.createOrUpdateRealm(REALM, REALM_NAME);
verify(service).updateRealm(existingRealm, REALM);
}
}
@DisplayName("Update Realm")
@Nested
class TestUpdateRealm {
@BeforeEach
void init() {
when(mapper.update(any(), any())).thenReturn(realmRepresentation);
}
@Test
void shouldUpdateRealmIfExists() {
service.updateRealm(realmRepresentation, REALM);
verify(remoteService).updateRealm(realmRepresentation);
}
@Test
void shouldCallMapper() {
service.updateRealm(realmRepresentation, REALM);
verify(mapper).update(realmRepresentation, REALM);
}
}
@Nested @Nested
class TestCreateRealm { class TestCreateRealm {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment