-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added testing for retry method inside group import
- Loading branch information
1 parent
6373689
commit 3e193da
Showing
3 changed files
with
207 additions
and
2 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
32 changes: 32 additions & 0 deletions
32
src/main/java/de/adorsys/keycloak/config/util/ThreadSleepUtil.java
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,32 @@ | ||
/*- | ||
* ---license-start | ||
* keycloak-config-cli | ||
* --- | ||
* Copyright (C) 2017 - 2021 adorsys GmbH & Co. KG @ https://adorsys.com | ||
* --- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ---license-end | ||
*/ | ||
|
||
package de.adorsys.keycloak.config.util; | ||
|
||
public class ThreadSleepUtil { | ||
|
||
private ThreadSleepUtil() { | ||
} | ||
|
||
public static void sleep(long millis) throws InterruptedException { | ||
Thread.sleep(millis); | ||
} | ||
|
||
} |
169 changes: 169 additions & 0 deletions
169
src/test/java/de/adorsys/keycloak/config/service/GroupImportServiceTest.java
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,169 @@ | ||
/*- | ||
* ---license-start | ||
* keycloak-config-cli | ||
* --- | ||
* Copyright (C) 2017 - 2021 adorsys GmbH & Co. KG @ https://adorsys.com | ||
* --- | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* ---license-end | ||
*/ | ||
|
||
package de.adorsys.keycloak.config.service; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.when; | ||
|
||
import de.adorsys.keycloak.config.exception.ImportProcessingException; | ||
import de.adorsys.keycloak.config.properties.ImportConfigProperties; | ||
import de.adorsys.keycloak.config.repository.GroupRepository; | ||
import de.adorsys.keycloak.config.util.ThreadSleepUtil; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
import org.keycloak.representations.idm.GroupRepresentation; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.stubbing.OngoingStubbing; | ||
|
||
class GroupImportServiceTest { | ||
|
||
private final GroupRepository groupRepository = mock(GroupRepository.class); | ||
|
||
private final ImportConfigProperties importConfigProperties = mock(ImportConfigProperties.class); | ||
|
||
private final GroupImportService groupImportService = | ||
new GroupImportService(groupRepository, importConfigProperties); | ||
|
||
@Nested | ||
class CreatingGroupIT { | ||
|
||
private final String realmName = "someRealm"; | ||
|
||
private final String groupId = "someGroupId"; | ||
|
||
private final String groupName = "someGroup"; | ||
|
||
private final List<String> realmRoles = List.of("someRealmRole"); | ||
|
||
private final String clientId = "someClientId"; | ||
|
||
private final List<String> clientRoleNames = List.of("someClientRoleName"); | ||
|
||
private final GroupRepresentation subGroup = new GroupRepresentation(); | ||
|
||
private final GroupRepresentation group = new GroupRepresentation(); | ||
|
||
@BeforeEach | ||
void init() { | ||
group.setId(groupId); | ||
group.setName(groupName); | ||
group.setRealmRoles(realmRoles); | ||
group.setClientRoles(Map.of(clientId, clientRoleNames)); | ||
group.setSubGroups(List.of(subGroup)); | ||
|
||
String subGroupName = "someSubGroupName"; | ||
subGroup.setName(subGroupName); | ||
|
||
when(groupRepository.getGroupByName(realmName, groupName)).thenReturn(null).thenReturn(group); | ||
when(groupRepository.getSubGroupByName(realmName, groupId, subGroupName)).thenReturn(subGroup); | ||
} | ||
|
||
@Test | ||
void createOrUpdateGroups_shouldCreateGroup() { | ||
groupImportService.createOrUpdateGroups(List.of(group), realmName); | ||
verify(groupRepository).createGroup(realmName, group); | ||
} | ||
|
||
@Test | ||
void createOrUpdateGroups_shouldAddRealmRoles() { | ||
groupImportService.createOrUpdateGroups(List.of(group), realmName); | ||
verify(groupRepository).addRealmRoles(realmName, groupId, realmRoles); | ||
} | ||
|
||
@Test | ||
void createOrUpdateGroups_shouldAddClientRoles() { | ||
groupImportService.createOrUpdateGroups(List.of(group), realmName); | ||
verify(groupRepository).addClientRoles(realmName, groupId, clientId, clientRoleNames); | ||
} | ||
|
||
@Test | ||
void createOrUpdateGroups_shouldAddSubGroups() { | ||
groupImportService.createOrUpdateGroups(List.of(group), realmName); | ||
verify(groupRepository).addSubGroup(realmName, groupId, subGroup); | ||
} | ||
|
||
@Test | ||
void createOrUpdateGroups_shouldPassInterruptWhileWaitingForRetries() { | ||
try (MockedStatic<ThreadSleepUtil> util = mockStatic(ThreadSleepUtil.class)) { | ||
util.when(() -> ThreadSleepUtil.sleep(0L)).thenThrow(new InterruptedException()); | ||
|
||
when(groupRepository.getGroupByName(realmName, groupName)) | ||
.thenReturn(null) | ||
.thenReturn(null) | ||
.thenReturn(group); | ||
|
||
assertThat(Thread.interrupted()).isFalse(); | ||
|
||
groupImportService.createOrUpdateGroups(List.of(group), realmName); | ||
|
||
assertThat(Thread.interrupted()).isTrue(); | ||
} | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = {1, 2, 3, 4, 5}) | ||
void createOrUpdateGroups_shouldRetryGettingCreatedGroup(int retries) { | ||
OngoingStubbing<GroupRepresentation> getGroupNameStubbing = | ||
when(groupRepository.getGroupByName(realmName, groupName)); | ||
|
||
for (int i = 0; i < retries - 1; i++) { | ||
getGroupNameStubbing = getGroupNameStubbing.thenReturn(null); | ||
} | ||
|
||
getGroupNameStubbing.thenReturn(group); | ||
|
||
groupImportService.createOrUpdateGroups(List.of(group), realmName); | ||
|
||
verify(groupRepository, times(retries)).getGroupByName(realmName, groupName); | ||
} | ||
|
||
@Test | ||
void createOrUpdateGroups_shouldStopImportWithNullAfterEveryRetry() { | ||
when(groupRepository.getGroupByName(realmName, groupName)) | ||
.thenReturn(null) | ||
.thenReturn(null) | ||
.thenReturn(null) | ||
.thenReturn(null) | ||
.thenReturn(null) | ||
.thenReturn(null) | ||
.thenReturn(group); | ||
|
||
ImportProcessingException exception = assertThrows( | ||
ImportProcessingException.class, | ||
() -> groupImportService.createOrUpdateGroups(List.of(group), realmName) | ||
); | ||
|
||
assertThat(exception) | ||
.message() | ||
.isEqualTo(String.format("Cannot find created group '%s' in realm '%s'", groupName, realmName)); | ||
} | ||
} | ||
} |