Search in sources :

Example 26 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method createSubject.

private static Subject createSubject(String name, Subject... parents) {
    Subject subject = Mockito.mock(Subject.class);
    List<Subject> ls = Arrays.asList(parents);
    Mockito.when(subject.getIdentifier()).thenReturn(name);
    Mockito.when(subject.getOption(Mockito.anySetOf(Context.class), Mockito.eq("nucleus.list.weight"))).then(x -> ls.stream().map(y -> y.getOption("nucleus.list.weight")).filter(Optional::isPresent).findFirst().orElse(Optional.empty()));
    Mockito.when(subject.getOption(Mockito.eq("nucleus.list.weight"))).then(x -> ls.stream().map(y -> y.getOption("nucleus.list.weight")).filter(Optional::isPresent).findFirst().orElse(Optional.empty()));
    List<SubjectReference> lsr = getSubjectReferences(Arrays.asList(parents));
    Mockito.when(subject.getParents()).thenReturn(lsr);
    Mockito.when(subject.getParents(Mockito.anySetOf(Context.class))).thenReturn(lsr);
    return subject;
}
Also used : Context(org.spongepowered.api.service.context.Context) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) Subject(org.spongepowered.api.service.permission.Subject)

Example 27 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method testWeightsAreBeingAppliedCorrectlyWithAliases.

@Test
@SuppressWarnings("all")
public void testWeightsAreBeingAppliedCorrectlyWithAliases() {
    // Create two groups.
    Subject admin = createSubjectWithWeight("admin", 1);
    Subject ace = createSubjectWithWeight("ace", 0);
    // The player is in both groups. Also, order is important.
    List<Subject> parents = Lists.newArrayList(admin, ace);
    Player player = Mockito.mock(Player.class);
    List<SubjectReference> lsr = getSubjectReferences(parents);
    Mockito.when(player.getParents()).thenReturn(lsr);
    Mockito.when(player.getParents(Mockito.anySet())).thenReturn(lsr);
    // Create our map.
    Map<Player, List<String>> map = Maps.newHashMap();
    map.put(player, Lists.newArrayList("admin", "ace"));
    // No aliases.
    Map<String, String> aliases = Maps.newHashMap();
    aliases.put("admin", "Admin");
    aliases.put("ace", "Ace");
    // Now, let's run it through our method.
    Map<String, List<Player>> result = ListPlayerCommand.linkPlayersToGroups(parents, aliases, map);
    Assert.assertEquals("There should only be one entry", 1, result.size());
    List<Player> players = result.get("Admin");
    Assert.assertNotNull("Players is null", players);
    Assert.assertEquals("There should only be one player!", 1, players.size());
    Assert.assertTrue("map is not empty", map.isEmpty());
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) SubjectReference(org.spongepowered.api.service.permission.SubjectReference) List(java.util.List) Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Example 28 with Subject

use of org.spongepowered.api.service.permission.Subject in project Nucleus by NucleusPowered.

the class ListGroupTests method testTwoGroupsWithWeights.

@Test
public void testTwoGroupsWithWeights() {
    Subject admin = createSubjectWithWeight("admin", 1);
    Subject ace = createSubjectWithWeight("ace", 0);
    Assert.assertEquals(-1, ListPlayerCommand.groupComparison(ListPlayerCommand.weightingFunction, admin, ace));
}
Also used : Subject(org.spongepowered.api.service.permission.Subject) Test(org.junit.Test)

Aggregations

Subject (org.spongepowered.api.service.permission.Subject)28 SubjectReference (org.spongepowered.api.service.permission.SubjectReference)11 Test (org.junit.Test)10 List (java.util.List)9 Player (org.spongepowered.api.entity.living.player.Player)9 Context (org.spongepowered.api.service.context.Context)6 Optional (java.util.Optional)5 Collectors (java.util.stream.Collectors)5 PermissionService (org.spongepowered.api.service.permission.PermissionService)5 Text (org.spongepowered.api.text.Text)5 Lists (com.google.common.collect.Lists)4 Maps (com.google.common.collect.Maps)4 Util (io.github.nucleuspowered.nucleus.Util)4 Map (java.util.Map)4 CommandSource (org.spongepowered.api.command.CommandSource)4 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)3 ListPlayerCommand (io.github.nucleuspowered.nucleus.modules.playerinfo.commands.ListPlayerCommand)3 Arrays (java.util.Arrays)3 HashMap (java.util.HashMap)3 CompletableFuture (java.util.concurrent.CompletableFuture)3