-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPetTest.java
More file actions
379 lines (297 loc) · 12.4 KB
/
PetTest.java
File metadata and controls
379 lines (297 loc) · 12.4 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package dansplugins.wildpets.pet;
import dansplugins.wildpets.helpers.ServerProvider;
import org.bukkit.Server;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Mob;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
public class PetTest {
@Mock
private ServerProvider mockServerProvider;
@Mock
private Server mockServer;
@Mock
private Mob mockMob;
@Mock
private Entity mockEntity; // Non-Mob entity for testing
private UUID entityUniqueId;
private UUID playerOwnerUniqueId;
private String playerOwnerName;
@Before
public void setup() {
MockitoAnnotations.openMocks(this);
when(mockServerProvider.get()).thenReturn(mockServer);
// Common test data
entityUniqueId = UUID.randomUUID();
when(mockServer.getEntity(entityUniqueId)).thenReturn(null);
playerOwnerUniqueId = UUID.randomUUID();
playerOwnerName = "Daniel";
}
@Test
public void testInitializeFromScratch() {
// execute
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
// verify
assertEquals(entityUniqueId, pet.getUniqueID());
assertEquals(playerOwnerUniqueId, pet.getOwnerUUID());
assertEquals(playerOwnerName + "'s_Pet", pet.getName());
assertEquals("Wandering", pet.getMovementState());
assertTrue(pet.getAccessList().contains(playerOwnerUniqueId));
assertEquals(0, pet.getAssignedID()); // Default should be 0
assertFalse(pet.isLocked()); // Default should be unlocked
}
@Test
public void testSaveToJson() {
// prepare
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
pet.setAssignedID(123);
// execute
Map<String, String> petData = pet.save();
// verify
assertEquals("\"" + entityUniqueId.toString() + "\"", petData.get("uniqueID"));
assertEquals("\"" + playerOwnerUniqueId.toString() + "\"", petData.get("owner"));
assertEquals("123", petData.get("assignedID"));
assertEquals("\"" + playerOwnerName + "\\u0027s_Pet\"", petData.get("name"));
assertEquals("0", petData.get("lastKnownX"));
assertEquals("0", petData.get("lastKnownY"));
assertEquals("0", petData.get("lastKnownZ"));
assertEquals("\"Wandering\"", petData.get("movementState"));
assertEquals("false", petData.get("locked"));
assertTrue(petData.get("accessList").contains(playerOwnerUniqueId.toString()));
assertEquals("[]", petData.get("parentIDs"));
assertEquals("[]", petData.get("childIDs"));
}
@Test
public void testInitializeFromJson() {
// prepare
Map<String, String> petData = createBasicPetData("Wandering");
// Mock the server to return null for entity (entity not found case)
when(mockServer.getEntity(any(UUID.class))).thenReturn(null);
// execute
Pet pet = new Pet(petData, mockServerProvider);
// verify
assertEquals(UUID.fromString(petData.get("uniqueID")), pet.getUniqueID());
assertEquals(UUID.fromString(petData.get("owner")), pet.getOwnerUUID());
assertEquals(petData.get("name"), pet.getName());
assertEquals(petData.get("movementState"), pet.getMovementState());
assertTrue(pet.getAccessList().contains(UUID.fromString(petData.get("owner"))));
assertTrue(pet.getParentUUIDs().isEmpty());
assertTrue(pet.getChildUUIDs().isEmpty());
}
@Test
public void testSetStaying() {
// Mock entity retrieval
when(mockServer.getEntity(entityUniqueId)).thenReturn(mockMob);
// Create pet with mocked ServerProvider
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
// execute
pet.setStaying();
// verify
assertEquals("Staying", pet.getMovementState());
verify(mockMob).setAware(false);
}
@Test
public void testSetWandering() {
// Mock entity retrieval
when(mockServer.getEntity(entityUniqueId)).thenReturn(mockMob);
// Create pet and set to Staying first
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
pet.setStaying(); // First set to Staying
verify(mockMob).setAware(false);
// Reset mock to verify new calls
reset(mockMob);
// execute
pet.setWandering();
// verify
assertEquals("Wandering", pet.getMovementState());
verify(mockMob).setAware(true);
}
@Test
public void testSetFollowing() {
// Mock entity retrieval
when(mockServer.getEntity(entityUniqueId)).thenReturn(mockMob);
// Create pet
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
pet.setStaying(); // First set to Staying
verify(mockMob).setAware(false);
// Reset mock to verify new calls
reset(mockMob);
// execute
pet.setFollowing();
// verify
assertEquals("Following", pet.getMovementState());
verify(mockMob).setAware(true);
}
@Test
public void testLoadStayingState() {
// prepare
Map<String, String> petData = createBasicPetData("Staying");
// Mock entity retrieval
when(mockServer.getEntity(UUID.fromString(petData.get("uniqueID")))).thenReturn(mockMob);
// Create pet with mocked ServerProvider
Pet pet = new Pet(petData, mockServerProvider);
// verify
assertEquals("Staying", pet.getMovementState());
verify(mockMob).setAware(false);
}
@Test
public void testLoadFollowingState() {
// prepare
Map<String, String> petData = createBasicPetData("Following");
// Mock entity retrieval
when(mockServer.getEntity(UUID.fromString(petData.get("uniqueID")))).thenReturn(mockMob);
// Create pet with mocked ServerProvider
Pet pet = new Pet(petData, mockServerProvider);
// verify
assertEquals("Following", pet.getMovementState());
verify(mockMob).setAware(true);
}
@Test
public void testAccessListFunctionality() {
// prepare
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
UUID newUser = UUID.randomUUID();
// verify initial state
assertTrue(pet.hasAccess(playerOwnerUniqueId));
assertFalse(pet.hasAccess(newUser));
// add new user
pet.addToAccessList(newUser);
// verify after add
assertTrue(pet.hasAccess(newUser));
assertEquals(2, pet.getAccessList().size());
// remove user
pet.removeFromAccessList(newUser);
// verify after remove
assertFalse(pet.hasAccess(newUser));
assertEquals(1, pet.getAccessList().size());
}
@Test
public void testLockingFunctionality() {
// prepare
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
// verify initial state
assertFalse(pet.isLocked());
// lock
pet.setLocked(true);
// verify locked
assertTrue(pet.isLocked());
// unlock
pet.setLocked(false);
// verify unlocked
assertFalse(pet.isLocked());
}
@Test
public void testFamilialRelationships() {
// prepare
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
UUID parent1 = UUID.randomUUID();
UUID parent2 = UUID.randomUUID();
UUID child1 = UUID.randomUUID();
// Add parents and children
pet.addParent(parent1);
pet.addParent(parent2);
pet.addChild(child1);
// verify
assertEquals(2, pet.getParentUUIDs().size());
assertTrue(pet.getParentUUIDs().contains(parent1));
assertTrue(pet.getParentUUIDs().contains(parent2));
assertEquals(1, pet.getChildUUIDs().size());
assertTrue(pet.getChildUUIDs().contains(child1));
// Test string methods
String parentString = pet.getParentsUUIDsSeparatedByCommas();
assertTrue(parentString.contains(parent1.toString()));
assertTrue(parentString.contains(parent2.toString()));
assertTrue(parentString.contains(", ")); // Ensure comma separation
String childString = pet.getChildrenUUIDsSeparatedByCommas();
assertTrue(childString.contains(child1.toString()));
assertFalse(childString.contains(", ")); // Only one child, no comma
}
@Test
public void testApplyAIStateWithNonMobEntity() {
// prepare - mockEntity is not a Mob
when(mockServer.getEntity(entityUniqueId)).thenReturn(mockEntity);
// Create pet
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
// execute - this should not throw an exception
pet.setStaying();
// verify movement state was changed, but no exception occurred
assertEquals("Staying", pet.getMovementState());
// We can't verify setAware wasn't called because mockEntity doesn't have that method
}
@Test
public void testApplyAIStateWithNullEntity() {
// prepare - entity not found
when(mockServer.getEntity(entityUniqueId)).thenReturn(null);
// Create pet
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
// execute - this should not throw an exception
pet.setStaying();
// verify movement state was changed, but no exception occurred
assertEquals("Staying", pet.getMovementState());
}
@Test
public void testEnsurePersistenceWithMob() {
// prepare - Mob extends LivingEntity
when(mockServer.getEntity(entityUniqueId)).thenReturn(mockMob);
// Create pet
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
// execute
pet.ensurePersistence();
// verify
verify(mockMob).setPersistent(true);
verify(mockMob).setRemoveWhenFarAway(false);
}
@Test
public void testEnsurePersistenceWithNonLivingEntity() {
// prepare - mockEntity is just Entity, not LivingEntity
when(mockServer.getEntity(entityUniqueId)).thenReturn(mockEntity);
// Create pet
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
// execute
pet.ensurePersistence();
// verify setPersistent is called, but setRemoveWhenFarAway is not (not a LivingEntity)
verify(mockEntity).setPersistent(true);
}
@Test
public void testEnsurePersistenceWithNullEntity() {
// prepare - entity not found (e.g., in unloaded chunk)
when(mockServer.getEntity(entityUniqueId)).thenReturn(null);
// Create pet
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, mockServerProvider);
// execute - should not throw an exception
pet.ensurePersistence();
// verify no interactions attempted on null entity
}
@Test
public void testEnsurePersistenceWithNullServerProvider() {
// Create pet with null server provider
Pet pet = new Pet(entityUniqueId, playerOwnerUniqueId, playerOwnerName, null);
// execute - should not throw an exception
pet.ensurePersistence();
// verify no exception occurred
}
// Helper method to create test data
private Map<String, String> createBasicPetData(String movementState) {
Map<String, String> petData = new HashMap<>();
petData.put("uniqueID", entityUniqueId.toString());
petData.put("owner", playerOwnerUniqueId.toString());
petData.put("assignedID", "0");
petData.put("name", "Daniel's_Pet");
petData.put("lastKnownX", "0");
petData.put("lastKnownY", "0");
petData.put("lastKnownZ", "0");
petData.put("movementState", movementState);
petData.put("locked", "false");
petData.put("accessList", "[\"" + petData.get("owner") + "\"]");
petData.put("parentIDs", "[]");
petData.put("childIDs", "[]");
return petData;
}
}