diff --git a/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/layout/UserFormLayouts.java b/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/layout/UserFormLayouts.java index 3a00e4ba07e..69bb4649cb6 100644 --- a/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/layout/UserFormLayouts.java +++ b/client/idrepo/enduser/src/main/java/org/apache/syncope/client/enduser/layout/UserFormLayouts.java @@ -43,21 +43,14 @@ public Map getLayouts() { } public UserFormLayoutInfo getLayout(final String realm) { - if (!StringUtils.isBlank(realm)) { - UserFormLayoutInfo layout = layouts.get(realm); + for (String current = StringUtils.isBlank(realm) ? SyncopeConstants.ROOT_REALM : realm; + StringUtils.isNotBlank(current); + current = StringUtils.substringBeforeLast(current, "/")) { + + UserFormLayoutInfo layout = layouts.get(current); if (layout != null) { return layout; } - - String current = StringUtils.substringBeforeLast(realm, "/"); - while (!SyncopeConstants.ROOT_REALM.equals(current)) { - layout = layouts.get(current); - if (layout != null) { - return layout; - } - - current = StringUtils.substringBeforeLast(current, "/"); - } } return layouts.get(SyncopeConstants.ROOT_REALM); } diff --git a/client/idrepo/enduser/src/test/java/org/apache/syncope/client/enduser/layout/UserFormaLayoutTest.java b/client/idrepo/enduser/src/test/java/org/apache/syncope/client/enduser/layout/UserFormaLayoutTest.java new file mode 100644 index 00000000000..6f8bfbb6128 --- /dev/null +++ b/client/idrepo/enduser/src/test/java/org/apache/syncope/client/enduser/layout/UserFormaLayoutTest.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + */ +package org.apache.syncope.client.enduser.layout; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +import java.util.Map; +import org.apache.syncope.common.lib.SyncopeConstants; +import org.junit.jupiter.api.Test; + +class UserFormLayoutsTest { + + @Test + void shouldReturnExactRealmLayout() { + UserFormLayoutInfo customLayout = new UserFormLayoutInfo(); + UserFormLayouts userFormLayouts = new UserFormLayouts(Map.of("/even", customLayout)); + assertSame(customLayout, userFormLayouts.getLayout("/even")); + } + + @Test + void shouldReturnParentRealmLayout() { + UserFormLayoutInfo customLayout = new UserFormLayoutInfo(); + UserFormLayouts userFormLayouts = new UserFormLayouts(Map.of("/even", customLayout)); + assertSame(customLayout, userFormLayouts.getLayout("/even/two")); + } + + @Test + void shouldReturnRootRealmLayoutWhenNoParentExists() { + UserFormLayouts userFormLayouts = new UserFormLayouts(Map.of()); + UserFormLayoutInfo rootLayout = userFormLayouts.getLayouts().get(SyncopeConstants.ROOT_REALM); + assertNotNull(rootLayout); + assertSame(rootLayout, userFormLayouts.getLayout("/odd")); + } + + @Test + void shouldReturnCustomRootRealmLayout() { + UserFormLayoutInfo customLayout = new UserFormLayoutInfo(); + UserFormLayouts userFormLayouts = new UserFormLayouts(Map.of(SyncopeConstants.ROOT_REALM, customLayout)); + assertSame(customLayout, userFormLayouts.getLayout("/odd")); + } + + @Test + void shouldNotLoopWhenRealmDoesNotExist() { + UserFormLayouts userFormLayouts = new UserFormLayouts(Map.of()); + assertNotNull(userFormLayouts.getLayout("/unknown")); + } + + @Test + void shouldReturnCustomLayoutForRealm() { + UserFormLayoutInfo customLayout = new UserFormLayoutInfo(); + UserFormLayouts userFormLayouts = new UserFormLayouts(Map.of("/even", customLayout)); + assertSame(customLayout, userFormLayouts.getLayout("/even")); + } + + @Test + void shouldKeepCustomRootRealmLayout() { + UserFormLayoutInfo customLayout = new UserFormLayoutInfo(); + UserFormLayouts userFormLayouts = new UserFormLayouts(Map.of(SyncopeConstants.ROOT_REALM, customLayout)); + assertSame(customLayout, userFormLayouts.getLayout("/")); + } +}