Newer
Older
package de.ozgcloud.admin.keycloak;
import static de.ozgcloud.admin.keycloak.GroupRepresentationTestFactory.*;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
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.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;
class GroupMapperTest {
@Mock
private KeycloakApiProperties properties;
@InjectMocks
private GroupMapper mapper = Mappers.getMapper(GroupMapper.class);
class TestFromGroupRepresentations {
private final GroupRepresentation groupRepresentation = GroupRepresentationTestFactory.create();
@Captor
private ArgumentCaptor<List<Group>> groupsCaptor;
void shouldMapFromGroupRepresentation() {
givenMappedGroupWithOrganisationsEinheitId();
callMapper();
verify(mapper).fromGroupRepresentation(groupRepresentation);
void shouldReturnListWithMappedGroupRepresentation() {
var group = givenMappedGroupWithOrganisationsEinheitId();
assertThat(groups).containsExactly(group);
}
@ParameterizedTest
@NullAndEmptySource
void shouldReturnEmptyList(String organisationsEinheitId) {
givenMappedGroupWithOrganisationsEinheitId(organisationsEinheitId);
var groups = callMapper();
assertThat(groups).isEmpty();
void shouldDeleteGroupsWithoutOrganisationsEinheitId() {
var group = givenMappedGroupWithOrganisationsEinheitId();
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);
void shouldCheckIfGroupHasOrganisationsEinheitId() {
mapper.deleteGroupsWithoutOrganisationsEinheitId(groups);
verify(mapper).isMissingOrganisationsEinheitId(group);
}
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
@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();
}
}
@Nested
class TestFromGroupRepresentation {
private final GroupRepresentation groupRepresentation = GroupRepresentationTestFactory.create();
@BeforeEach
void init() {
doReturn(GroupRepresentationTestFactory.ORGANISATIONS_EINHEIT_ID).when(mapper)
.getOrganisationsEinheitId(ATTRIBUTES);
doReturn(GroupRepresentationTestFactory.SUB_GROUP_ORGANISATIONS_EINHEIT_ID).when(mapper)
.getOrganisationsEinheitId(SUB_GROUP_ATTRIBUTES);
}
@Test
void shouldGetOrganisationsEinheitId() {
callMapper();
verify(mapper).getOrganisationsEinheitId(groupRepresentation.getAttributes());
}
@Test
void shouldSetId() {
var group = callMapper();
assertThat(group.getId()).isEqualTo(groupRepresentation.getId());
}
@Test
void shouldSetOrganisationsEinheitId() {
var group = callMapper();
assertThat(group.getOrganisationsEinheitId()).isEqualTo(GroupRepresentationTestFactory.ORGANISATIONS_EINHEIT_ID);
}
@Test
void shouldSetName() {
var group = callMapper();
assertThat(group.getName()).isEqualTo(GroupRepresentationTestFactory.NAME);
}
@Test
var group = callMapper();
assertThat(group.getSubGroups()).usingRecursiveFieldByFieldElementComparator()
.containsExactlyElementsOf(GroupTestFactory.create().getSubGroups());
}
private Group callMapper() {
return mapper.fromGroupRepresentation(groupRepresentation);
}
}
@Nested
void shouldReturnNullIfAttributesAreNull() {
var result = mapper.getOrganisationsEinheitId(null);
void shouldReturnNullIfAttributeIsAbsent() {
givenOrganisationsEinheitIdProperty();
var result = mapper.getOrganisationsEinheitId(Map.of("dummy-attribute", List.of("123")));
@Test
void shouldReturnOrganisationsEinheitId() {
givenOrganisationsEinheitIdProperty();
var value = GroupRepresentationTestFactory.ORGANISATIONS_EINHEIT_ID;
var result = mapper.getOrganisationsEinheitId(Map.of(ORGANIZATIONS_EINHEIT_ID_ATTRIBUTE, List.of(value)));
@Test
void shouldReturnFirstValueIfMultipleAreAvailable() {
givenOrganisationsEinheitIdProperty();
var value = GroupRepresentationTestFactory.ORGANISATIONS_EINHEIT_ID;
var value2 = UUID.randomUUID().toString();
var result = mapper.getOrganisationsEinheitId(Map.of(ORGANIZATIONS_EINHEIT_ID_ATTRIBUTE, List.of(value, value2)));
class TestToGroupRepresentation {
private final AddGroupData addGroupData = AddGroupDataTestFactory.create();
@BeforeEach
void init() {
doReturn(ATTRIBUTES).when(mapper).getAttributes(addGroupData);
}
@Test
void shouldSetName() {
var groupRepresentation = callMapper();
assertThat(groupRepresentation.getName()).isEqualTo(addGroupData.getName());
}
@Test
void shouldNotSetSubGroups() {
var groupRepresentation = callMapper();
assertThat(groupRepresentation.getSubGroups()).isEmpty();
}
@Test
void shouldGetAttributes() {
callMapper();
verify(mapper).getAttributes(addGroupData);
}
@Test
void shouldSetAttributeOrganisationsEinheitId() {
var groupRepresentation = callMapper();
assertThat(groupRepresentation.getAttributes()).isNotNull().containsKey(ORGANIZATIONS_EINHEIT_ID_ATTRIBUTE);
}
private GroupRepresentation callMapper() {
return mapper.toGroupRepresentation(addGroupData);
}
}
@Nested
class TestGetAttributes {
@ParameterizedTest
@NullAndEmptySource
void shouldReturnEmptyMap(String organisationsEinheitId) {
var addGroupData = AddGroupDataTestFactory.createBuilder().organisationsEinheitId(organisationsEinheitId).build();
var attributes = mapper.getAttributes(addGroupData);
assertThat(attributes).isEmpty();
}
@Test
void shouldAddOrganisationsEinheitIdToAttributes() {
givenOrganisationsEinheitIdProperty();
var attributes = mapper.getAttributes(AddGroupDataTestFactory.create());
assertThat(attributes).hasSize(1).containsEntry(ORGANIZATIONS_EINHEIT_ID_ATTRIBUTE, List.of(GroupTestFactory.ORGANISATIONS_EINHEIT_ID));
}
}
private void givenOrganisationsEinheitIdProperty() {
when(properties.getOrganisationsEinheitIdKey()).thenReturn(ORGANIZATIONS_EINHEIT_ID_ATTRIBUTE);
}