-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RemoveWarpCommandTest.java
202 lines (176 loc) · 6.69 KB
/
RemoveWarpCommandTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package world.bentobox.parkour.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.HashMap;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.powermock.modules.junit4.PowerMockRunner;
import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.LocalesManager;
import world.bentobox.bentobox.managers.RanksManager;
import world.bentobox.parkour.AbstractParkourTest;
import world.bentobox.parkour.ParkourManager;
import world.bentobox.parkour.Settings;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
public class RemoveWarpCommandTest extends AbstractParkourTest {
@Mock
private CompositeCommand ac;
@Mock
private LocalesManager lm;
private UUID uuid;
@Mock
private World world;
private RemoveWarpCommand cmd;
@Mock
private ParkourManager parkourManager;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
super.setUp();
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false);
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(4);
when(user.getWorld()).thenReturn(world);
uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.getTranslation(any())).thenAnswer(invocation -> invocation.getArgument(0, String.class));
User.setPlugin(plugin);
// Parent command has no aliases
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
when(ac.getWorld()).thenReturn(world);
when(ac.getAddon()).thenReturn(addon);
// Islands
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(world, user)).thenReturn(island);
when(im.hasIsland(world, user)).thenReturn(true);
when(im.inTeam(world, uuid)).thenReturn(true);
when(island.getRankCommand(anyString())).thenReturn(RanksManager.OWNER_RANK);
when(island.getRank(user)).thenReturn(RanksManager.MEMBER_RANK);
when(im.userIsOnIsland(any(), any())).thenReturn(true);
// Parkour Manager
// Warp spot available by default
when(parkourManager.getWarpSpot(island)).thenReturn(Optional.of(mock(Location.class)));
when(addon.getParkourManager()).thenReturn(parkourManager);
// IWM
when(plugin.getIWM()).thenReturn(iwm);
when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock.");
// Settings
Settings settings = new Settings();
when(addon.getSettings()).thenReturn(settings);
// DUT
cmd = new RemoveWarpCommand(ac);
}
/**
* Test method for
* {@link world.bentobox.parkour.commands.RemoveWarpCommand#RemoveWarpCommand(world.bentobox.bentobox.api.commands.CompositeCommand)}.
*/
@Test
public void testRemoveWarpCommand() {
assertNotNull(cmd);
}
/**
* Test method for
* {@link world.bentobox.parkour.commands.RemoveWarpCommand#setup()}.
*/
@Test
public void testSetup() {
assertEquals("removewarp", cmd.getPermission());
assertEquals("parkour.commands.parkour.removewarp.description", cmd.getDescription());
assertTrue(cmd.isConfigurableRankCommand());
assertTrue(cmd.isOnlyPlayer());
}
/**
* Test method for
* {@link world.bentobox.parkour.commands.RemoveWarpCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteFailHelp() {
// Help
assertFalse(cmd.canExecute(user, "", List.of("something")));
verify(user).sendMessage("commands.help.header", "[label]", null);
}
/**
* Test method for
* {@link world.bentobox.parkour.commands.RemoveWarpCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteFailNoRank() {
// Insufficient rank
assertFalse(cmd.canExecute(user, "", List.of()));
verify(user).sendMessage("general.errors.insufficient-rank", TextVariables.RANK, RanksManager.MEMBER_RANK_REF);
}
/**
* Test method for {@link world.bentobox.parkour.commands.RemoveWarpCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecuteFailNoWarpSpot() {
// Has rank
when(island.getRankCommand(anyString())).thenReturn(RanksManager.MEMBER_RANK);
when(parkourManager.getWarpSpot(island)).thenReturn(Optional.empty());
assertFalse(cmd.canExecute(user, "", List.of()));
verify(user).sendMessage("parkour.errors.no-warp");
}
/**
* Test method for {@link world.bentobox.parkour.commands.RemoveWarpCommand#canExecute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testCanExecutePass() {
// Has rank
when(island.getRankCommand(anyString())).thenReturn(RanksManager.MEMBER_RANK);
assertTrue(cmd.canExecute(user, "", List.of()));
verify(user, never()).sendMessage(any());
}
/**
* Test method for
* {@link world.bentobox.parkour.commands.RemoveWarpCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfString() {
assertTrue(cmd.execute(user, "", List.of()));
verify(user).sendMessage("parkour.warp.removed");
verify(parkourManager).setWarpSpot(island, null);
}
/**
* Test method for {@link world.bentobox.parkour.commands.RemoveWarpCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringWarpRemove() {
when(parkourManager.getWarpSpot(island)).thenReturn(Optional.of(mock(Location.class)));
assertTrue(cmd.execute(user, "", List.of()));
verify(user).sendMessage("parkour.warp.removed");
verify(parkourManager).setWarpSpot(island, null);
}
}