Skip to content

Commit

Permalink
Refactor other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Dec 6, 2023
1 parent b783961 commit 94f4397
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 189 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/exaroton/api/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ public String getMotd() {
*/
public ServerMOTDInfo fetchMotd() throws APIException {
GetServerMOTDRequest request = new GetServerMOTDRequest(this.client, this.id);
return request.request().getData();
ServerMOTDInfo motd = request.request().getData();
this.motd = motd.getMotd();
return motd;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/test/java/APIClientTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import com.exaroton.api.ExarotonClient;
import com.exaroton.api.server.Server;

public abstract class APIClientTest {
protected static final String TEST_SERVER_ID = System.getenv("EXAROTON_TEST_SERVER");
protected final ExarotonClient client = new ExarotonClient(System.getenv("EXAROTON_API_TOKEN"));
protected final Server server = client.getServer(TEST_SERVER_ID);
}
16 changes: 16 additions & 0 deletions src/test/java/AccountTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import com.exaroton.api.APIException;
import com.exaroton.api.account.Account;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class AccountTest extends APIClientTest {
@Test
void getAccount() throws APIException {
Account a = client.getAccount();
assertNotNull(a.getName());
assertNotNull(a.getEmail());
assertTrue(a.getVerified());
assertTrue(a.getCredits() > 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,22 @@
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class SetProtocolTest extends APIClientTest {
public class ClientOptionsTest extends APIClientTest {
@Test
void apiToken() {
assertDoesNotThrow(() -> client.setAPIToken(System.getenv("EXAROTON_API_TOKEN")));
}

@Test
void apiTokenEmpty() {
assertThrows(IllegalArgumentException.class, () -> client.setAPIToken(""));
}

@Test
void apiTokenNull() {
assertThrows(IllegalArgumentException.class, () -> client.setAPIToken(null));
}

@Test
void protocolHTTP() {
assertDoesNotThrow(() -> client.setProtocol("http"));
Expand All @@ -31,4 +46,19 @@ void protocolEmpty() {
void protocolNull() {
assertThrows(IllegalArgumentException.class, () -> client.setProtocol(null));
}

@Test
void userAgent() {
assertDoesNotThrow(() -> client.setUserAgent("java-exaroton-api-tests"));
}

@Test
void userAgentEmpty() {
assertThrows(IllegalArgumentException.class, () -> client.setUserAgent(""));
}

@Test
void userAgentNull() {
assertThrows(IllegalArgumentException.class, () -> client.setUserAgent(null));
}
}
24 changes: 24 additions & 0 deletions src/test/java/ConfigTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import com.exaroton.api.APIException;
import com.exaroton.api.server.Server;
import com.exaroton.api.server.ServerFile;
import com.exaroton.api.server.config.ConfigOption;
import com.exaroton.api.server.config.ServerConfig;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ConfigTest extends APIClientTest {
@Test
public void testGetConfig() throws APIException {
ServerFile serverProperties = server.getFile("server.properties");
Assertions.assertNotNull(serverProperties);
ServerConfig config = serverProperties.getConfig();
Assertions.assertNotNull(config);

ConfigOption gamemodeOption = config.getOption("gamemode");
Assertions.assertNotNull(gamemodeOption);
Assertions.assertEquals("Gamemode", gamemodeOption.getLabel());
Assertions.assertNotNull(gamemodeOption.getValue());
Assertions.assertNotNull(gamemodeOption.getOptions());
Assertions.assertTrue(gamemodeOption.getOptions().length > 3);
}
}
51 changes: 51 additions & 0 deletions src/test/java/FileTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import com.exaroton.api.APIException;
import com.exaroton.api.server.Server;
import com.exaroton.api.server.ServerFile;
import org.junit.jupiter.api.Test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.*;

public class FileTest extends APIClientTest {
@Test
void getFile() throws APIException, IOException {
ServerFile whitelist = server.getFile("whitelist.json");
whitelist.putContent("[{\"name\":\"JulianVennen\", \"uuid\": \"abcd9e56-5ac2-490c-8bc9-6c1cad18f506\"}]");
assertNotNull(whitelist);
assertNotNull(whitelist.getInfo());
assertFalse(whitelist.isConfigFile());
assertTrue(whitelist.isTextFile());
assertFalse(whitelist.isDirectory());
assertFalse(whitelist.isLog());
assertTrue(whitelist.isReadable());
assertTrue(whitelist.isWritable());

String content = whitelist.getContent();
assertNotNull(content);

Path path = Paths.get("whitelist.json");
whitelist.download(path);
try (FileInputStream input = new FileInputStream("whitelist.json")) {
assertEquals(content,
new BufferedReader(new InputStreamReader(input)).lines().collect(Collectors.joining("\n")));
}

whitelist.delete();
assertThrows(APIException.class, whitelist::getInfo);
whitelist.putContent("[]");
assertEquals("[]", whitelist.getContent());

whitelist.upload(path);
assertEquals(content, whitelist.getContent());

Files.delete(path);
}
}
15 changes: 0 additions & 15 deletions src/test/java/GetAccountTest.java

This file was deleted.

26 changes: 0 additions & 26 deletions src/test/java/GetConfigTest.java

This file was deleted.

53 changes: 0 additions & 53 deletions src/test/java/GetFileTest.java

This file was deleted.

51 changes: 0 additions & 51 deletions src/test/java/GetServersTest.java

This file was deleted.

66 changes: 66 additions & 0 deletions src/test/java/ServerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import com.exaroton.api.APIException;
import com.exaroton.api.server.PlayerList;
import com.exaroton.api.server.Server;
import com.exaroton.api.server.ServerLog;
import com.exaroton.api.server.ServerMOTDInfo;
import com.exaroton.api.server.ServerRAMInfo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

public class ServerTest extends APIClientTest {
@Test
void testGetServers() throws APIException {
Server[] servers = client.getServers();
assertNotNull(servers);
for (Server server: servers) {
assertNotNull(server.getAddress());
assertNotNull(server.getName());
assertNotNull(server.getId());
}
}

@Test
void testGetServer() throws APIException {
server.get();
}

@Test
void testGetLog() throws APIException {
ServerLog log = server.getLog();
assertNotNull(log);
assertNotNull(log.getContent());
}

@Test
void testGetMotd() throws APIException {
assertNull(server.getMotd());
ServerMOTDInfo fetched = server.fetchMotd();
assertNotNull(fetched);
assertNotNull(server.getMotd());
assertEquals(fetched.getMotd(), server.getMotd());
}

@Test
void testGetRAM() throws APIException {
ServerRAMInfo ram = server.getRAM();
assertNotNull(ram);
assertTrue(ram.getRam() > 0);
}

@Test
void testGetPlayerLists() throws APIException {
String[] lists = server.getPlayerLists();
assertNotNull(lists);
assertTrue(lists.length > 0);

for (String name: lists) {
assertNotNull(name);
PlayerList list = server.getPlayerList(name);
assertNotNull(list);
assertNotNull(list.getName());
assertNotNull(list.getEntries());
}
}
}
21 changes: 0 additions & 21 deletions src/test/java/SetAPITokenTest.java

This file was deleted.

Loading

0 comments on commit 94f4397

Please sign in to comment.