Skip to content

Commit 31a7344

Browse files
committed
Test PlayerList
1 parent ef8e9fb commit 31a7344

File tree

7 files changed

+294
-174
lines changed

7 files changed

+294
-174
lines changed

src/main/java/com/exaroton/api/request/server/AddPlayerListEntriesRequest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import org.jetbrains.annotations.NotNull;
99

1010
import java.net.http.HttpRequest;
11+
import java.util.ArrayList;
12+
import java.util.Collection;
1113
import java.util.HashMap;
1214
import java.util.List;
1315

@@ -20,10 +22,10 @@ public AddPlayerListEntriesRequest(
2022
@NotNull Gson gson,
2123
@NotNull String id,
2224
@NotNull String list,
23-
@NotNull List<@NotNull String> entries
25+
@NotNull Collection<@NotNull String> entries
2426
) {
2527
super(client, gson, id, list);
26-
this.entries = ParameterValidator.requireNonEmpty(entries, "entries");
28+
this.entries = new ArrayList<>(ParameterValidator.requireNonEmpty(entries, "entries"));
2729
}
2830

2931
@Override

src/main/java/com/exaroton/api/server/PlayerList.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.jetbrains.annotations.NotNull;
1111

1212
import java.io.IOException;
13+
import java.util.Collection;
1314
import java.util.List;
1415
import java.util.Objects;
1516
import java.util.concurrent.CompletableFuture;
@@ -64,14 +65,38 @@ public CompletableFuture<List<String>> getEntries() throws IOException {
6465
* @return completable future with all players in the list
6566
* @throws IOException Connection errors
6667
*/
67-
public CompletableFuture<List<String>> add(List<String> entries) throws IOException {
68+
public CompletableFuture<List<String>> add(String... entries) throws IOException {
69+
ParameterValidator.requireNonEmpty(entries, "entries");
70+
return add(List.of(entries));
71+
}
72+
73+
/**
74+
* add players to list
75+
*
76+
* @param entries player names
77+
* @return completable future with all players in the list
78+
* @throws IOException Connection errors
79+
*/
80+
public CompletableFuture<List<String>> add(Collection<String> entries) throws IOException {
6881
if (entries.isEmpty()) {
6982
throw new IllegalArgumentException("Can't add empty list");
7083
}
7184

7285
return client.request(new AddPlayerListEntriesRequest(this.client, this.gson, this.serverId, this.name, entries));
7386
}
7487

88+
/**
89+
* remove players from list
90+
*
91+
* @param entries player names
92+
* @return completable future with all players in the list
93+
* @throws IOException Connection errors
94+
*/
95+
public CompletableFuture<List<String>> remove(String... entries) throws IOException {
96+
ParameterValidator.requireNonEmpty(entries, "entries");
97+
return remove(List.of(entries));
98+
}
99+
75100
/**
76101
* remove players from list
77102
*

src/main/java/com/exaroton/api/util/ParameterValidator.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static String requireNonEmpty(String string, String name) {
4242
* @param name name of the parameter
4343
* @param <C> collection type
4444
* @param <I> item type
45-
* @return the string
45+
* @return the collection
4646
* @throws IllegalArgumentException if the array is null or empty
4747
*/
4848
public static <C extends Collection<I>, I> C requireNonEmpty(C collection, String name) {
@@ -69,7 +69,7 @@ public static <C extends Collection<I>, I> C requireNonEmpty(C collection, Strin
6969
* @param <M> map type
7070
* @param <K> key type
7171
* @param <V> value type
72-
* @return the string
72+
* @return the map
7373
* @throws IllegalArgumentException if the array is null or empty
7474
*/
7575
public static <M extends Map<K, V>, K, V> M requireNonEmpty(M map, String name) {
@@ -90,6 +90,30 @@ public static <M extends Map<K, V>, K, V> M requireNonEmpty(M map, String name)
9090
return map;
9191
}
9292

93+
/**
94+
* Require that the argument is not null or empty
95+
*
96+
* @param array array to validate
97+
* @param name name of the parameter
98+
* @param <I> item type
99+
* @return the array
100+
* @throws IllegalArgumentException if the array is null or empty
101+
*/
102+
public static <I> I[] requireNonEmpty(I[] array, String name) {
103+
if (array == null || array.length == 0) {
104+
throw new IllegalArgumentException(name + " must not be empty");
105+
}
106+
107+
for (int i = 0; i < array.length; i++) {
108+
if (array[i] == null) {
109+
throw new IllegalArgumentException(name + " must not contain null values (index: " + i + ")");
110+
}
111+
}
112+
113+
return array;
114+
}
115+
116+
93117
/**
94118
* Require that a number is not null and positive (&gt; 0)
95119
*

src/test/java/FileTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class FileTest extends APIClientTest {
1818
@Test
1919
void getFile() throws IOException {
2020
ServerFile whitelist = server.getFile("whitelist.json");
21-
whitelist.putContent("[{\"name\":\"JulianVennen\", \"uuid\": \"abcd9e56-5ac2-490c-8bc9-6c1cad18f506\"}]").join();
21+
whitelist.putContent("[{\"name\":\"Aternos\", \"uuid\": \"d6a91995-04bf-4f11-823f-5b18d412062a\"}]").join();
2222
assertNotNull(whitelist);
2323
assertNotNull(whitelist.fetch().join());
2424
assertFalse(whitelist.isConfigFile());

src/test/java/PlayerListTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import com.exaroton.api.server.PlayerList;
2+
import org.junit.jupiter.api.BeforeEach;
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.IOException;
6+
import java.util.List;
7+
8+
import static org.junit.jupiter.api.Assertions.*;
9+
10+
public class PlayerListTest extends APIClientTest {
11+
protected PlayerList whitelist;
12+
13+
@Override
14+
@BeforeEach
15+
void setUp() {
16+
super.setUp();
17+
whitelist = server.getPlayerList("whitelist");
18+
}
19+
20+
@Test
21+
void testGetPlayerLists() throws IOException {
22+
List<String> lists = server.getPlayerLists().join();
23+
assertNotNull(lists);
24+
assertFalse(lists.isEmpty(), "Expected at least one player list, got none");
25+
26+
for (String name : lists) {
27+
assertNotNull(name);
28+
PlayerList list = server.getPlayerList(name);
29+
assertNotNull(list);
30+
assertNotNull(list.getName());
31+
assertNotNull(list.getEntries());
32+
}
33+
}
34+
35+
@Test
36+
void testGetEntries() throws IOException {
37+
List<String> entries = whitelist.getEntries().join();
38+
assertNotNull(entries);
39+
assertTrue(entries.contains("Aternos"), "Expected 'Aternos' to be in whitelist");
40+
}
41+
42+
@Test
43+
void testAddRemoveEntries() throws IOException {
44+
List<String> entries = whitelist.add("exaroton").join();
45+
assertNotNull(entries);
46+
assertTrue(entries.contains("exaroton"), "Expected 'exaroton' to be in whitelist");
47+
48+
entries = whitelist.remove("exaroton").join();
49+
assertNotNull(entries);
50+
assertFalse(entries.contains("exaroton"), "Expected 'exaroton' not to be in whitelist");
51+
}
52+
53+
@Test
54+
void testRemoveEmpty() {
55+
assertThrows(IllegalArgumentException.class, () -> whitelist.remove());
56+
assertThrows(IllegalArgumentException.class, () -> whitelist.remove(List.of()));
57+
}
58+
59+
@Test
60+
void testAddEmpty() {
61+
assertThrows(IllegalArgumentException.class, () -> whitelist.add());
62+
assertThrows(IllegalArgumentException.class, () -> whitelist.add(List.of()));
63+
}
64+
}

src/test/java/ServerTest.java

Lines changed: 0 additions & 168 deletions
Original file line numberDiff line numberDiff line change
@@ -126,172 +126,4 @@ void testGetSetRAM() throws IOException {
126126
assertEquals(16, server.getRAM().join().getRam());
127127
server.setRAM(ram.getRam()).join();
128128
}
129-
130-
@Test
131-
void testGetPlayerLists() throws IOException {
132-
List<String> lists = server.getPlayerLists().join();
133-
assertNotNull(lists);
134-
assertFalse(lists.isEmpty(), "Expected at least one player list, got none");
135-
136-
for (String name : lists) {
137-
assertNotNull(name);
138-
PlayerList list = server.getPlayerList(name);
139-
assertNotNull(list);
140-
assertNotNull(list.getName());
141-
assertNotNull(list.getEntries());
142-
}
143-
}
144-
145-
@Test
146-
void testRemoveSubscribersWithoutWebsocket() {
147-
server.removeStatusSubscriber((oldServer, newServer) -> {
148-
});
149-
server.removeConsoleSubscriber(x -> {
150-
});
151-
server.removeHeapSubscriber(x -> {
152-
});
153-
server.removeStatsSubscriber(x -> {
154-
});
155-
server.removeTickSubscriber(x -> {
156-
});
157-
}
158-
159-
/**
160-
* Perform a series of tests that require the server to be online
161-
*/
162-
@Test
163-
void testStartServer() throws IOException, ExecutionException, InterruptedException, TimeoutException {
164-
server.fetch().join();
165-
166-
startServer();
167-
testHeapSubscriber();
168-
testStatsSubscriber();
169-
testTickSubscriber();
170-
testExecuteCommand();
171-
restartServer();
172-
stopServer();
173-
174-
assertNull(server.getWebSocket());
175-
}
176-
177-
void testHeapSubscriber() throws ExecutionException, InterruptedException, TimeoutException {
178-
CompletableFuture<HeapUsage> heapFuture = new CompletableFuture<>();
179-
HeapSubscriber heapSubscriber = heapFuture::complete;
180-
181-
server.addHeapSubscriber(heapSubscriber);
182-
HeapUsage heapUsage = heapFuture.get(1, TimeUnit.MINUTES);
183-
server.removeHeapSubscriber(heapSubscriber);
184-
185-
assertNotNull(heapUsage);
186-
assertTrue(heapUsage.getUsage() > 0, "Expected heap usage to be greater than 0");
187-
}
188-
189-
void testStatsSubscriber() throws ExecutionException, InterruptedException, TimeoutException {
190-
CompletableFuture<StatsData> statsFuture = new CompletableFuture<>();
191-
StatsSubscriber statsSubscriber = statsFuture::complete;
192-
193-
server.addStatsSubscriber(statsSubscriber);
194-
StatsData stats = statsFuture.get(1, TimeUnit.MINUTES);
195-
server.removeStatsSubscriber(statsSubscriber);
196-
197-
assertNotNull(stats);
198-
assertTrue(stats.getMemory().getUsage() > 0, "Expected memory usage to be greater than 0");
199-
assertTrue(stats.getMemory().getPercent() > 0, "Expected memory usage to be greater than 0%");
200-
assertTrue(stats.getMemory().getPercent() < 100, "Expected memory usage to be less than 100%");
201-
}
202-
203-
void testTickSubscriber() throws ExecutionException, InterruptedException, TimeoutException {
204-
CompletableFuture<TickData> tickFuture = new CompletableFuture<>();
205-
TickSubscriber tickSubscriber = tickFuture::complete;
206-
207-
server.addTickSubscriber(tickSubscriber);
208-
TickData tick = tickFuture.get(1, TimeUnit.MINUTES);
209-
server.removeTickSubscriber(tickSubscriber);
210-
211-
assertNotNull(tick);
212-
assertTrue(tick.getAverageTickTime() > 0, "Expected tick time to be greater than 0");
213-
assertTrue(tick.calculateTPS() > 0, "Expected tps to be greater than 0");
214-
assertTrue(tick.calculateTPS() <= 20, "Expected memory usage to be greater than 0%");
215-
}
216-
217-
void testExecuteCommand() throws IOException, ExecutionException, InterruptedException, TimeoutException {
218-
var flagA = new FlagSubscriber();
219-
var flagB = new FlagSubscriber();
220-
221-
server.addConsoleSubscriber(flagA);
222-
server.addConsoleSubscriber(flagB);
223-
224-
server.executeCommand("say " + flagA.value).join();
225-
226-
Server serverWithoutWS = client.getServer(TEST_SERVER_ID);
227-
serverWithoutWS.executeCommand("say " + flagB.value).join();
228-
229-
flagA.future.get(10, TimeUnit.SECONDS);
230-
flagB.future.get(10, TimeUnit.SECONDS);
231-
232-
server.removeConsoleSubscriber(flagA);
233-
server.removeConsoleSubscriber(flagB);
234-
}
235-
236-
void startServer() throws IOException, ExecutionException, InterruptedException, TimeoutException {
237-
assertTrue(server.hasStatus(ServerStatus.GROUP_OFFLINE));
238-
239-
AtomicBoolean receivedStatusUpdate = new AtomicBoolean(false);
240-
var statusSubscriber = new ServerStatusSubscriber() {
241-
@Override
242-
public void handleStatusUpdate(Server oldServer, Server newServer) {
243-
receivedStatusUpdate.set(true);
244-
assertSame(server, newServer);
245-
}
246-
};
247-
server.addStatusSubscriber(statusSubscriber);
248-
249-
server.start().join();
250-
server.waitForStatus(ServerStatus.ONLINE).get(3, TimeUnit.MINUTES);
251-
assertEquals(ServerStatus.ONLINE, server.getStatus());
252-
253-
server.removeStatusSubscriber(statusSubscriber);
254-
assertTrue(receivedStatusUpdate.get());
255-
}
256-
257-
void restartServer() throws IOException, ExecutionException, InterruptedException, TimeoutException {
258-
assertEquals(ServerStatus.ONLINE, server.getStatus());
259-
260-
var restartingFuture = server.waitForStatus(ServerStatus.RESTARTING);
261-
assertNotNull(server.getWebSocket());
262-
server.getWebSocket().waitForReady().get(1, TimeUnit.MINUTES);
263-
264-
server.restart().join();
265-
restartingFuture.get(1, TimeUnit.MINUTES);
266-
assertEquals(ServerStatus.RESTARTING, server.getStatus());
267-
268-
269-
server.waitForStatus(ServerStatus.ONLINE).get(3, TimeUnit.MINUTES);
270-
assertEquals(ServerStatus.ONLINE, server.getStatus());
271-
}
272-
273-
void stopServer() throws IOException, ExecutionException, InterruptedException, TimeoutException {
274-
assertFalse(server.hasStatus(ServerStatus.GROUP_OFFLINE));
275-
assertFalse(server.hasStatus(ServerStatus.GROUP_STOPPING));
276-
277-
server.stop().join();
278-
server.waitForStatus(ServerStatus.GROUP_OFFLINE).get(3, TimeUnit.MINUTES);
279-
assertTrue(server.hasStatus(ServerStatus.GROUP_OFFLINE), "Expected server to be offline or crashed");
280-
}
281-
282-
private static class FlagSubscriber implements ConsoleSubscriber {
283-
final String value;
284-
final CompletableFuture<Void> future = new CompletableFuture<>();
285-
286-
public FlagSubscriber() {
287-
this.value = "exaroton-api-tests-" + new Random().nextInt();
288-
}
289-
290-
@Override
291-
public void handleLine(String message) {
292-
if (message.contains(value)) {
293-
future.complete(null);
294-
}
295-
}
296-
}
297129
}

0 commit comments

Comments
 (0)