-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
CompleteChallengeCommandTest.java
332 lines (294 loc) · 13.7 KB
/
CompleteChallengeCommandTest.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package world.bentobox.challenges.commands;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
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.ArgumentMatchers.eq;
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.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.meta.ItemMeta;
import org.eclipse.jdt.annotation.NonNull;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.stubbing.Answer;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.addons.GameModeAddon;
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.database.objects.Island;
import world.bentobox.bentobox.managers.CommandsManager;
import world.bentobox.bentobox.managers.IslandWorldManager;
import world.bentobox.bentobox.managers.IslandsManager;
import world.bentobox.bentobox.util.Util;
import world.bentobox.challenges.ChallengesAddon;
import world.bentobox.challenges.config.Settings;
import world.bentobox.challenges.config.SettingsUtils.VisibilityMode;
import world.bentobox.challenges.database.object.Challenge;
import world.bentobox.challenges.managers.ChallengesManager;
import world.bentobox.challenges.tasks.TryToComplete;
import world.bentobox.challenges.utils.Constants;
import world.bentobox.challenges.utils.Utils;
/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({ Bukkit.class, BentoBox.class, Utils.class, TryToComplete.class, Util.class })
public class CompleteChallengeCommandTest {
@Mock
private CompositeCommand ic;
@Mock
private User user;
@Mock
private IslandsManager im;
@Mock
private Island island;
@Mock
private ChallengesAddon addon;
private CompleteChallengeCommand cc;
@Mock
private World world;
@Mock
private ChallengesManager chm;
@Mock
private IslandWorldManager iwm;
@Mock
private GameModeAddon gameModeAddon;
@Mock
private Challenge challenge;
/**
*/
@SuppressWarnings("unchecked")
@Before
public void setUp() {
// Set up plugin
BentoBox plugin = mock(BentoBox.class);
Whitebox.setInternalState(BentoBox.class, "instance", plugin);
User.setPlugin(plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Addon
when(ic.getAddon()).thenReturn(addon);
when(ic.getPermissionPrefix()).thenReturn("bskyblock.");
when(ic.getLabel()).thenReturn("island");
when(ic.getTopLabel()).thenReturn("island");
when(ic.getWorld()).thenReturn(world);
when(ic.getTopLabel()).thenReturn("bsb");
// IWM friendly name
when(iwm.getFriendlyName(any())).thenReturn("BSkyBlock");
when(iwm.inWorld(any(World.class))).thenReturn(true);
Optional<GameModeAddon> optionalAddon = Optional.of(gameModeAddon);
when(iwm.getAddon(any())).thenReturn(optionalAddon);
when(plugin.getIWM()).thenReturn(iwm);
// Game Mode Addon
@NonNull
Optional<CompositeCommand> optionalAdmin = Optional.of(ic);
when(gameModeAddon.getAdminCommand()).thenReturn(optionalAdmin);
// World
when(world.toString()).thenReturn("world");
// Player
Player p = mock(Player.class);
// Sometimes use Mockito.withSettings().verboseLogging()
when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
when(user.getPermissionValue(anyString(), anyInt())).thenReturn(-1);
when(user.isPlayer()).thenReturn(true);
// Mock item factory (for itemstacks)
PowerMockito.mockStatic(Bukkit.class);
ItemFactory itemFactory = mock(ItemFactory.class);
when(Bukkit.getItemFactory()).thenReturn(itemFactory);
ItemMeta itemMeta = mock(ItemMeta.class);
when(itemFactory.getItemMeta(any())).thenReturn(itemMeta);
// Addon & Challenge Manager
when(addon.getChallengesManager()).thenReturn(chm);
when(chm.getAllChallengeLevelStatus(any(), any())).thenReturn(Collections.emptyList());
// Challenges exist
when(chm.hasAnyChallengeData(any(World.class))).thenReturn(true);
// Challenges
when(chm.getChallenge(anyString())).thenReturn(challenge);
List<String> nameList = Arrays.asList("world_maker", "world_placer", "bad_challenge_name", "world_breaker");
when(chm.getAllChallengesNames(any())).thenReturn(nameList);
// Settings
Settings settings = new Settings();
when(addon.getChallengesSettings()).thenReturn(settings);
settings.setVisibilityMode(VisibilityMode.VISIBLE);
// Island
when(plugin.getIslands()).thenReturn(im);
when(im.getIsland(any(), any(User.class))).thenReturn(island);
// Utils
PowerMockito.mockStatic(Utils.class);
when(Utils.getGameMode(any())).thenReturn("world");
// Try to complete
PowerMockito.mockStatic(TryToComplete.class);
// All challenges are successful!
when(TryToComplete.complete(any(), any(), any(), any(), anyString(), anyString(), anyInt())).thenReturn(true);
// Util
PowerMockito.mockStatic(Util.class);
when(Util.tabLimit(any(), any())).thenAnswer((Answer<List<String>>) invocation -> (List<String>)invocation.getArgument(0, List.class));
when(Util.translateColorCodes(anyString()))
.thenAnswer((Answer<String>) invocation -> invocation.getArgument(0, String.class));
// Command under test
cc = new CompleteChallengeCommand(addon, ic);
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#CompleteChallengeCommand(world.bentobox.bentobox.api.addons.Addon, world.bentobox.bentobox.api.commands.CompositeCommand)}.
*/
@Test
public void testCompleteChallengeCommand() {
assertEquals("complete", cc.getLabel());
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#setup()}.
*/
@Test
public void testSetup() {
assertEquals("bskyblock.challenges", cc.getPermission());
assertEquals("challenges.commands.user.complete.parameters", cc.getParameters());
assertEquals("challenges.commands.user.complete.description", cc.getDescription());
assertTrue(cc.isOnlyPlayer());
// No sub commands
assertEquals(0, cc.getSubCommands(true).size());
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringNoArgs() {
assertFalse(cc.execute(user, "complete", Collections.emptyList()));
PowerMockito.verifyStatic(Utils.class);
Utils.sendMessage(user, world, Constants.ERRORS + "no-name");
verify(user).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq("BSkyBlock"));
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringUnknownChallenge() {
when(chm.getChallenge(anyString())).thenReturn(null);
assertFalse(cc.execute(user, "complete", Collections.singletonList("mychal")));
PowerMockito.verifyStatic(Utils.class);
Utils.sendMessage(user, world, Constants.ERRORS + "unknown-challenge");
verify(user).sendMessage(eq("commands.help.header"), eq(TextVariables.LABEL), eq("BSkyBlock"));
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringKnownChallengeFail() {
when(TryToComplete.complete(any(), any(), any(), any(), anyString(), anyString(), anyInt())).thenReturn(false);
assertFalse(cc.execute(user, "complete", Collections.singletonList("mychal")));
verify(user, never()).sendMessage(any());
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringKnownChallengeSuccess() {
assertTrue(cc.execute(user, "complete", Collections.singletonList("mychal")));
verify(user, never()).sendMessage(any());
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringKnownChallengeSuccessMultipleTimesNoPerm() {
assertTrue(cc.execute(user, "complete", Arrays.asList("mychal", "5")));
PowerMockito.verifyStatic(Utils.class);
Utils.sendMessage(user, world, Constants.ERRORS + "no-multiple-permission");
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#execute(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testExecuteUserStringListOfStringKnownChallengeSuccessMultipleTimesHasPerm() {
when(user.hasPermission(anyString())).thenReturn(true);
assertTrue(cc.execute(user, "complete", Arrays.asList("mychal", "5")));
verify(user, never()).sendMessage(any());
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringNoArgs() {
cc.tabComplete(user, "complete", Collections.emptyList());
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringOneArg() {
List<String> list = cc.tabComplete(user, "complete", Collections.singletonList("arg")).get();
assertFalse(list.isEmpty());
assertEquals("help", list.get(0));
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringTwoArgs() {
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2")).get();
assertFalse(list.isEmpty());
assertEquals("help", list.get(0));
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringThreeArgs() {
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2", "arg3")).get();
assertFalse(list.isEmpty());
assertEquals("maker", list.get(0));
assertEquals("placer", list.get(1));
assertEquals("breaker", list.get(2));
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringFourArgs() {
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2", "arg3", "arg4")).get();
assertTrue(list.isEmpty());
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringFourArgsNumber() {
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2", "arg3", "4")).get();
assertFalse(list.isEmpty());
assertEquals("<number>", list.get(0));
}
/**
* Test method for {@link world.bentobox.challenges.commands.CompleteChallengeCommand#tabComplete(world.bentobox.bentobox.api.user.User, java.lang.String, java.util.List)}.
*/
@Test
public void testTabCompleteUserStringListOfStringFiveArgs() {
List<String> list = cc.tabComplete(user, "complete", Arrays.asList("arg1", "arg2", "arg23", "arg4", "arg5")).get();
assertFalse(list.isEmpty());
assertEquals("help", list.get(0));
}
}