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

OZG-6867 Tests for protected methods

parent 6f11063b
Branches
Tags 2.5.0
No related merge requests found
......@@ -4,6 +4,7 @@ import static de.ozgcloud.admin.keycloak.GroupRepresentationTestFactory.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
......@@ -13,8 +14,11 @@ import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;
import org.keycloak.representations.idm.GroupRepresentation;
import org.mapstruct.factory.Mappers;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
......@@ -28,42 +32,123 @@ class GroupMapperTest {
private GroupMapper mapper = Mappers.getMapper(GroupMapper.class);
@Nested
class TestGetOrganisationsEinheitId {
class TestFromGroupRepresentations {
private final GroupRepresentation groupRepresentation = GroupRepresentationTestFactory.create();
@Captor
private ArgumentCaptor<List<Group>> groupsCaptor;
@Test
void shouldReturnNullIfAttributesAreNull() {
var result = mapper.getOrganisationsEinheitId(null);
void shouldMapFromGroupRepresentation() {
givenMappedGroupWithOrganisationsEinheitId();
assertThat(result).isNull();
callMapper();
verify(mapper).fromGroupRepresentation(groupRepresentation);
}
@Test
void shouldReturnNullIfAttributeIsAbsent() {
givenOrganisationsEinheitIdProperty();
void shouldReturnListWithMappedGroupRepresentation() {
var group = givenMappedGroupWithOrganisationsEinheitId();
var result = mapper.getOrganisationsEinheitId(Map.of("dummy-attribute", List.of("123")));
var groups = callMapper();
assertThat(result).isNull();
assertThat(groups).containsExactly(group);
}
@ParameterizedTest
@NullAndEmptySource
void shouldReturnEmptyList(String organisationsEinheitId) {
givenMappedGroupWithOrganisationsEinheitId(organisationsEinheitId);
var groups = callMapper();
assertThat(groups).isEmpty();
}
@Test
void shouldReturnOrganisationsEinheitId() {
givenOrganisationsEinheitIdProperty();
var value = GroupRepresentationTestFactory.ORGANISATIONS_EINHEIT_ID;
var result = mapper.getOrganisationsEinheitId(Map.of(ORGANIZATIONS_EINHEIT_ID_ATTRIBUTE, List.of(value)));
void shouldDeleteGroupsWithoutOrganisationsEinheitId() {
var group = givenMappedGroupWithOrganisationsEinheitId();
assertThat(result).isEqualTo(value);
callMapper();
verify(mapper).deleteGroupsWithoutOrganisationsEinheitId(groupsCaptor.capture());
assertThat(groupsCaptor.getValue()).containsExactly(group);
}
private Group givenMappedGroupWithOrganisationsEinheitId() {
var group = GroupTestFactory.create();
doReturn(group).when(mapper).fromGroupRepresentation(groupRepresentation);
return group;
}
private void givenMappedGroupWithOrganisationsEinheitId(String id) {
var group = GroupTestFactory.createBuilder().organisationsEinheitId(id).build();
doReturn(group).when(mapper).fromGroupRepresentation(groupRepresentation);
}
private List<Group> callMapper() {
return mapper.fromGroupRepresentations(List.of(groupRepresentation));
}
}
@Nested
class TestDeleteGroupsWithoutOrganisationsEinheitId {
private final Group group = GroupTestFactory.create();
private final List<Group> groups = new ArrayList<>();
@BeforeEach
void init() {
groups.add(group);
}
@Test
void shouldReturnFirstValueIfMultipleAreAvailable() {
givenOrganisationsEinheitIdProperty();
var value = GroupRepresentationTestFactory.ORGANISATIONS_EINHEIT_ID;
var value2 = UUID.randomUUID().toString();
void shouldCheckIfGroupHasOrganisationsEinheitId() {
mapper.deleteGroupsWithoutOrganisationsEinheitId(groups);
var result = mapper.getOrganisationsEinheitId(Map.of(ORGANIZATIONS_EINHEIT_ID_ATTRIBUTE, List.of(value, value2)));
verify(mapper).isMissingOrganisationsEinheitId(group);
}
assertThat(result).isEqualTo(value);
@Test
void shouldKeepGroupsWithOrganisationsEinheitId() {
doReturn(false).when(mapper).isMissingOrganisationsEinheitId(group);
mapper.deleteGroupsWithoutOrganisationsEinheitId(groups);
assertThat(groups).containsExactly(group);
}
@Test
void shouldRemoveGroupsWithoutOrganisationsEinheitId() {
doReturn(true).when(mapper).isMissingOrganisationsEinheitId(group);
mapper.deleteGroupsWithoutOrganisationsEinheitId(groups);
assertThat(groups).isEmpty();
}
}
@Nested
class TestIsMissingOrganisationsEinheitId {
@ParameterizedTest
@ValueSource(strings = " ")
@NullAndEmptySource
void shouldReturnTrueIfIdIsBlank(String organisationsEinheitId) {
var group = GroupTestFactory.createBuilder().organisationsEinheitId(organisationsEinheitId).build();
var isMissing = mapper.isMissingOrganisationsEinheitId(group);
assertThat(isMissing).isTrue();
}
@Test
void shouldReturnFalseIfIdIsSet() {
var isMissing = mapper.isMissingOrganisationsEinheitId(GroupTestFactory.create());
assertThat(isMissing).isFalse();
}
}
......@@ -122,51 +207,42 @@ class GroupMapperTest {
}
@Nested
class TestFromGroupRepresentations {
private final GroupRepresentation groupRepresentation = GroupRepresentationTestFactory.create();
class TestGetOrganisationsEinheitId {
@Test
void shouldMapFromGroupRepresentation() {
givenMappedGroupWithOrganisationsEinheitId();
callMapper();
void shouldReturnNullIfAttributesAreNull() {
var result = mapper.getOrganisationsEinheitId(null);
verify(mapper).fromGroupRepresentation(groupRepresentation);
assertThat(result).isNull();
}
@Test
void shouldReturnListWithMappedGroupRepresentation() {
var group = givenMappedGroupWithOrganisationsEinheitId();
void shouldReturnNullIfAttributeIsAbsent() {
givenOrganisationsEinheitIdProperty();
var groups = callMapper();
var result = mapper.getOrganisationsEinheitId(Map.of("dummy-attribute", List.of("123")));
assertThat(groups).containsExactly(group);
assertThat(result).isNull();
}
@ParameterizedTest
@NullAndEmptySource
void shouldReturnEmptyList(String organisationsEinheitId) {
givenMappedGroupWithOrganisationsEinheitId(organisationsEinheitId);
var groups = callMapper();
@Test
void shouldReturnOrganisationsEinheitId() {
givenOrganisationsEinheitIdProperty();
var value = GroupRepresentationTestFactory.ORGANISATIONS_EINHEIT_ID;
var result = mapper.getOrganisationsEinheitId(Map.of(ORGANIZATIONS_EINHEIT_ID_ATTRIBUTE, List.of(value)));
assertThat(groups).isEmpty();
assertThat(result).isEqualTo(value);
}
private Group givenMappedGroupWithOrganisationsEinheitId() {
var group = GroupTestFactory.create();
doReturn(group).when(mapper).fromGroupRepresentation(groupRepresentation);
return group;
}
@Test
void shouldReturnFirstValueIfMultipleAreAvailable() {
givenOrganisationsEinheitIdProperty();
var value = GroupRepresentationTestFactory.ORGANISATIONS_EINHEIT_ID;
var value2 = UUID.randomUUID().toString();
private void givenMappedGroupWithOrganisationsEinheitId(String id) {
var group = GroupTestFactory.createBuilder().organisationsEinheitId(id).build();
doReturn(group).when(mapper).fromGroupRepresentation(groupRepresentation);
}
var result = mapper.getOrganisationsEinheitId(Map.of(ORGANIZATIONS_EINHEIT_ID_ATTRIBUTE, List.of(value, value2)));
private List<Group> callMapper() {
return mapper.fromGroupRepresentations(List.of(groupRepresentation));
assertThat(result).isEqualTo(value);
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment