View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.provisioning.api.utils;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.HashSet;
26  import java.util.Optional;
27  import java.util.Set;
28  import org.apache.commons.lang3.tuple.Pair;
29  import org.apache.syncope.core.provisioning.api.AbstractTest;
30  import org.junit.jupiter.api.Test;
31  
32  public class RealmUtilsTest extends AbstractTest {
33  
34      @Test
35      public void getGroupOwnerRealm() {
36          String realmPath = "realmPath";
37          String groupKey = "groupKey";
38          assertEquals(realmPath + "@" + groupKey, RealmUtils.getGroupOwnerRealm(realmPath, groupKey));
39      }
40  
41      @Test
42      public void parseGroupOwnerRealm() {
43          assertEquals(
44                  Optional.of(Pair.of("realmPath", "groupKey")),
45                  RealmUtils.parseGroupOwnerRealm("realmPath@groupKey"));
46          assertFalse(RealmUtils.parseGroupOwnerRealm("realmPath").isPresent());
47      }
48  
49      @Test
50      public void normalizingAddTo() {
51          Set<String> realms = new HashSet<>();
52          realms.add("realm1");
53          realms.add("realm2");
54          String newRealm = "realm123";
55          assertFalse(RealmUtils.normalizingAddTo(realms, newRealm));
56          assertEquals(2, realms.size());
57  
58          realms.clear();
59          realms.add("testRealm1");
60          realms.add("realm2");
61          newRealm = "test";
62          assertTrue(RealmUtils.normalizingAddTo(realms, newRealm));
63          assertEquals(2, realms.size());
64      }
65  
66      @Test
67      public void getEffective() {
68          Set<String> allowedRealms = new HashSet<>();
69          String requestedRealm = "requestedRealm";
70          allowedRealms.add("testRealm1");
71          allowedRealms.add("testRealm2");
72          allowedRealms.add("testRealm3");
73          allowedRealms.add("requestedRealm");
74          Set<String> effective = RealmUtils.getEffective(allowedRealms, requestedRealm);
75          assertEquals(allowedRealms, effective);
76      }
77  }