-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from mfolnovic/issue-80-rename-group
#80: implement support for renaming group
- Loading branch information
Showing
4 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/de/klg71/keycloakmigration/changeControl/actions/group/RenameGroupAction.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package de.klg71.keycloakmigration.changeControl.actions.group | ||
|
||
import de.klg71.keycloakmigration.changeControl.actions.Action | ||
import de.klg71.keycloakmigration.changeControl.actions.MigrationException | ||
import de.klg71.keycloakmigration.keycloakapi.existsGroup | ||
import de.klg71.keycloakmigration.keycloakapi.groupByName | ||
import de.klg71.keycloakmigration.keycloakapi.model.Group | ||
import de.klg71.keycloakmigration.keycloakapi.model.UpdateGroup | ||
|
||
class RenameGroupAction( | ||
realm: String? = null, | ||
private val name: String, | ||
private val newName: String) : Action(realm) { | ||
|
||
private lateinit var group: Group | ||
|
||
private fun updateGroup() = UpdateGroup(newName, group.path, group.attributes, | ||
//FIXME | ||
group.access!!, | ||
group.clientRoles, group.realmRoles, group.subGroups | ||
) | ||
|
||
override fun execute() { | ||
if (!client.existsGroup(name, realm())) { | ||
throw MigrationException("Group with name: $name does not exists in realm: ${realm()}!") | ||
} | ||
|
||
group = client.groupByName(name, realm()).run { | ||
client.group(realm(), id) | ||
} | ||
|
||
client.updateGroup(updateGroup(), realm(), group.id) | ||
} | ||
|
||
override fun undo() { | ||
client.updateGroup(UpdateGroup(group.name, group.path, group.attributes, group.access!!, | ||
group.clientRoles, group.realmRoles, group.subGroups), | ||
realm(), group.id) | ||
} | ||
|
||
override fun name() = "RenameGroup $name to $newName" | ||
|
||
} |
44 changes: 44 additions & 0 deletions
44
...est/kotlin/de/klg71/keycloakmigration/changeControl/actions/group/RenameGroupIntegTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package de.klg71.keycloakmigration.changeControl.actions.group | ||
|
||
import de.klg71.keycloakmigration.AbstractIntegrationTest | ||
import de.klg71.keycloakmigration.keycloakapi.KeycloakApiException | ||
import de.klg71.keycloakmigration.keycloakapi.KeycloakClient | ||
import de.klg71.keycloakmigration.keycloakapi.groupByName | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.assertj.core.api.Assertions.assertThatThrownBy | ||
import org.junit.Test | ||
import org.koin.core.component.inject | ||
|
||
class RenameGroupIntegTest : AbstractIntegrationTest() { | ||
|
||
val client by inject<KeycloakClient>() | ||
|
||
@Test | ||
fun testRenameGroup() { | ||
AddGroupAction(testRealm, "integrationTest").executeIt() | ||
val createdGroup = client.groupByName("integrationTest", testRealm) | ||
|
||
RenameGroupAction(testRealm, "integrationTest", "newName").executeIt() | ||
|
||
assertThat(client.groupByName("newName", testRealm).id).isEqualTo(createdGroup.id) | ||
assertThatThrownBy { client.groupByName("integrationTest", testRealm) } | ||
.hasMessage("Group with name: integrationTest does not exist in realm: $testRealm!") | ||
.isInstanceOf(KeycloakApiException::class.java) | ||
} | ||
|
||
@Test | ||
fun testUndoRenameGroup() { | ||
AddGroupAction(testRealm, "integrationTest").executeIt() | ||
val createdGroup = client.groupByName("integrationTest", testRealm) | ||
|
||
val renameGroupAction = RenameGroupAction(testRealm, "integrationTest", "newName") | ||
renameGroupAction.executeIt() | ||
|
||
renameGroupAction.undoIt() | ||
|
||
assertThat(client.groupByName("integrationTest", testRealm).id).isEqualTo(createdGroup.id) | ||
assertThatThrownBy { client.groupByName("newName", testRealm) } | ||
.hasMessage("Group with name: newName does not exist in realm: $testRealm!") | ||
.isInstanceOf(KeycloakApiException::class.java) | ||
} | ||
} |