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

OZG-5422 add java unittest fpr realm roles

parent 287ac95c
No related branches found
No related tags found
No related merge requests found
......@@ -24,8 +24,6 @@
package de.ozgcloud.operator.keycloak.realm;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
......@@ -33,17 +31,15 @@ import java.util.Set;
import java.util.stream.Collectors;
import org.keycloak.representations.idm.RealmRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.RolesRepresentation;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.ReportingPolicy;
import de.ozgcloud.operator.keycloak.realm.OzgCloudKeycloakRealmSpec.RealmRole;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.RolesRepresentation;
@Mapper(unmappedTargetPolicy = ReportingPolicy.IGNORE, unmappedSourcePolicy = ReportingPolicy.IGNORE)
interface KeycloakRealmMapper {
......@@ -55,7 +51,7 @@ interface KeycloakRealmMapper {
@Mapping(target = "internationalizationEnabled", constant = "true")
@Mapping(target = "passwordPolicy", constant = "upperCase(1) and lowerCase(1) and length(8) and notUsername")
@Mapping(target = "actionTokenGeneratedByUserLifespan", constant = "900")
@Mapping(target = "roles", source = "realmRoles",qualifiedByName ="roles")
@Mapping(target = "roles", source = "realmRoles", qualifiedByName = "roles")
public RealmRepresentation map(OzgCloudKeycloakRealmSpec realm);
@Named("supportedLocales")
......@@ -64,26 +60,29 @@ interface KeycloakRealmMapper {
}
@Named("roles")
default RolesRepresentation maprealmRoles(List<OzgCloudKeycloakRealmSpec.RealmRole> realRoles) {
default RolesRepresentation maprealmRoles(List<OzgCloudKeycloakRealmSpec.RealmRole> realmRoles) {
RolesRepresentation roles = new RolesRepresentation();
List<String> roleNames = realRoles.stream()
.map(RealmRole::getName)
.collect(Collectors.toList());
List<RoleRepresentation> rolerepresentationList = new ArrayList<>(roleNames.size());
Map<String, List<RoleRepresentation>> client = new HashMap<String, List<RoleRepresentation>>();
if (realmRoles != null) {
List<String> roleNames = realmRoles.stream()
.map(RealmRole::getName)
.collect(Collectors.toList());
List<RoleRepresentation> rolerepresentationList = new ArrayList<>(roleNames.size());
roleNames.forEach(roleName -> {
RoleRepresentation newRepresentation = new RoleRepresentation();
newRepresentation.setName(roleName);
rolerepresentationList.add(newRepresentation);
});
roleNames.forEach(roleName -> {
RoleRepresentation newRepresentation = new RoleRepresentation();
newRepresentation.setName(roleName);
rolerepresentationList.add(newRepresentation); } );
roles.setRealm(rolerepresentationList);
roles.setRealm(rolerepresentationList);
roles.setClient(client);
Map<String, List<RoleRepresentation>> client = new HashMap<>();
roles.setClient(client);
}
return roles;
}
}
......@@ -23,10 +23,8 @@
*/
package de.ozgcloud.operator.keycloak.realm;
import java.util.List;
import java.util.ArrayList;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.RolesRepresentation;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
......@@ -50,10 +48,9 @@ class OzgCloudKeycloakRealmSpec {
private String displayName;
@Builder.Default
private List<RealmRole> realmRoles = new ArrayList<>();
@Builder.Default
private List<RealmRole> realmRoles= new ArrayList<>();
@Getter
@Setter
@Builder
......@@ -65,5 +62,3 @@ class OzgCloudKeycloakRealmSpec {
private String name;
}
}
......@@ -97,14 +97,11 @@ class KeycloakRealmMapperTest {
assertThat(mapped.getRoles().getRealm().size()).isEqualTo(2);
}
/* @Test
@Test
void shouldContainRealmRolesValues() {
var mapped = mapper.map(OzgCloudKeycloakRealmSpecTestFactory.create());
var mappedRealmRoles = mapped.getRoles();
assertThat(mappedRealmRoles.getRealm().getName()).isEqualTo("host");
var role = mapper.mapRole(OzgCloudKeycloakClientSpecTestFactory.ROLE1);
assertThat(role.getName()).isEqualTo(OzgCloudKeycloakClientSpecTestFactory.ROLE_NAME_1);
}*/
assertThat(mappedRealmRoles.getRealm().get(0).getName()).isEqualTo(OzgCloudKeycloakRealmSpecTestFactory.ROLE_NAME_1);
assertThat(mappedRealmRoles.getRealm().get(1).getName()).isEqualTo(OzgCloudKeycloakRealmSpecTestFactory.ROLE_NAME_2);
}
}
......@@ -154,6 +154,15 @@ class KeycloakRealmReconcilerTest {
assertThat(control).usingRecursiveComparison()
.isEqualTo(DeleteControl.noFinalizerRemoval().rescheduleAfter(Config.RECONCILER_RETRY_SECONDS_ON_ERROR));
}
@Test
void shouldCreateRealmWithSpecWithoutRoles() {
OzgCloudKeycloakRealm realm = OzgCloudKeycloakRealmTestFactory.create();
reconciler.reconcile(realm, null);
var spec = realm.getSpec();
spec.setRealmRoles(null);
verify(service).createRealm(spec, OzgCloudKeycloakRealmTestFactory.METADATA_NAMESPACE);
}
}
}
}
......@@ -36,7 +36,7 @@ public class OzgCloudKeycloakRealmSpecTestFactory {
public static final RealmRole ROLE1 = RealmRole.builder().name(ROLE_NAME_1).build();
public static final String ROLE_NAME_2 = "RoleName2";
public static final RealmRole ROLE2 = RealmRole.builder().name(ROLE_NAME_2).build();
public static final List<RealmRole> ROLES = List.of(ROLE1,ROLE2);
public static final List<RealmRole> ROLES = List.of(ROLE1, ROLE2);
public static OzgCloudKeycloakRealmSpec create() {
return createBuilder().build();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment