MinecraftManagerPlugin/src/main/java/xyz/etztech/minecraftmanager/test/Test.java

205 lines
10 KiB
Java

package xyz.etztech.minecraftmanager.test;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.apache.commons.lang.StringUtils;
import xyz.etztech.core.web.CoreWeb;
import xyz.etztech.core.web.ICallback;
import xyz.etztech.minecraftmanager.objects.Application;
import xyz.etztech.minecraftmanager.MCMAPI;
import xyz.etztech.minecraftmanager.objects.MCMResponse;
import xyz.etztech.minecraftmanager.objects.ModelResponse;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Test {
public static void main(String[] args) throws Exception {
String testUrl = "http://127.0.0.1:8000/api/";
String testApi = "Testing1";
try {
testUrl = args[0];
testApi = args[1];
} catch (Exception ex) {
System.out.println("Using default test URL and password");
}
System.out.println("URL: " + testUrl);
System.out.println("Password: " + testApi);
MCMAPI.test(testUrl, testApi);
int option = 0;
while (option != 99) {
System.out.println("===== Test Util =====");
System.out.println("1. Test Model Query - Search for applications with a username containing the number 1.");
System.out.println("2. Test Application Posting - Post two applications");
System.out.println("3. Test Application Action - Accept Testing1 application");
System.out.println("4. Test Application Action - Deny Testing2 application");
System.out.println("5. Test Application Clear - Clear Testing2 application");
System.out.println("6. Test Login - Spoof a login of user Etzelia with IP 127.0.0.1");
System.out.println("7. Test Ticket - Send a test ticket");
System.out.println("8. Test Warning - Send a test warning, medium importance, issued to Etzelia");
System.out.println("99. Exit");
System.out.print("Select Option: ");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
int choice = StringUtils.isNumeric(input) ? Integer.parseInt(input) : 0;
switch (choice) {
case 1:
option = 1;
break;
case 2:
option = 2;
break;
case 3:
option = 3;
break;
case 4:
option = 4;
break;
case 5:
option = 5;
break;
case 6:
option = 6;
break;
case 7:
option = 7;
break;
case 8:
option = 8;
break;
case 99:
option = 99;
break;
default:
break;
}
Map<String, String> data = MCMAPI.setup();
if (option == 1) {
// Query Model
data.put("username__icontains", "1");
ModelResponse response = new ModelResponse(CoreWeb.HTTP(MCMAPI.getModelUrl("application"),
CoreWeb.HttpMethod.GET, data));
JsonArray results = response.getResults();
System.out.println(results.size());
for (JsonElement jsonElement : results) {
System.out.println("Raw JSON: " + jsonElement.toString());
System.out.println("Username: " + jsonElement.getAsJsonObject().get("username").getAsString());
System.out.println("Ever Banned: " + jsonElement.getAsJsonObject().get("ever_banned").getAsBoolean());
}
} else if (option == 2) {
// Post two applications
Application app1 = new Application();
app1.setUsername("Testing1");
app1.setAge("20");
app1.setPlayerType("First test application.");
app1.setEverBanned("no");
app1.setEverBannedExplanation("");
app1.setReference("reddit");
app1.setReadRules("24karrot");
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/application/";
data.putAll(app1.getForm());
MCMResponse response1 = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== App 1 =====");
System.out.println("Status: " + response1.getStatus());
System.out.println("Message: " + response1.getMessage());
Application app2 = new Application();
app2.setUsername("Testing2");
app2.setAge("20");
app2.setPlayerType("Second test application.");
app2.setEverBanned("yes");
app2.setEverBannedExplanation("Griefing");
app2.setReference("planet minecraft");
app2.setReadRules("24karrot");
data = MCMAPI.setup();
data.putAll(app2.getForm());
MCMResponse response2 = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== App 2 =====");
System.out.println("Status: " + response2.getStatus());
System.out.println("Message: " + response2.getMessage());
} else if (option == 3 || option == 4) {
boolean accept = option == 3;
String username = option == 3 ? "Testing1" : "Testing2";
data.put("username__exact", username);
ModelResponse response = new ModelResponse(CoreWeb.HTTP(MCMAPI.getModelUrl("application"),
CoreWeb.HttpMethod.GET, data));
JsonArray array = response.getResults();
if (array.size() != 1) {
System.out.println("Couldn't find the application for " + username + ". Does it exist?");
} else {
String id = array.get(0).getAsJsonObject().get("id").getAsString();
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/application_action/";
data.put("application_id", id);
data.put("action", accept ? "True" : "False");
data.put("username", "Plugin Test");
MCMResponse mcmResponse = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== " + username + " =====");
System.out.println("Status: " + mcmResponse.getStatus());
System.out.println("Message: " + mcmResponse.getMessage());
}
} else if (option == 5) {
data.put("username__exact", "Testing2");
ModelResponse response = new ModelResponse(CoreWeb.HTTP(MCMAPI.getModelUrl("application"),
CoreWeb.HttpMethod.GET, data));
JsonArray array = response.getResults();
if (array.size() != 1) {
System.out.println("Couldn't find the application for Testing2. Does it exist?");
} else {
String id = array.get(0).getAsJsonObject().get("id").getAsString();
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/application_clear/";
data.put("application_id", id);
MCMResponse mcmResponse = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== Testing2 =====");
System.out.println("Status: " + mcmResponse.getStatus());
System.out.println("Message: " + mcmResponse.getMessage());
}
} else if (option == 6) {
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/login/";
data.put("uuid", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171");
data.put("username", "Etzelia");
data.put("ip", "127.0.0.1");
MCMResponse response = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== Login =====");
System.out.println("Status: " + response.getStatus());
System.out.println("Message: " + response.getMessage());
} else if (option == 7) {
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/ticket/";
data.put("uuid", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171");
data.put("message", "Test Ticket");
data.put("x", "1");
data.put("y", "2");
data.put("z", "3");
data.put("world", "O");
MCMResponse response = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== Ticket =====");
System.out.println("Status: " + response.getStatus());
System.out.println("Message: " + response.getMessage());
} else if (option == 8) {
String djangoUrl = MCMAPI.getDjangoUrl() + "plugin/warning/";
data.put("player", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171");
data.put("staff", "bf0446a8-9695-4c41-aa4c-7ff45bfd1171");
data.put("severity", "H");
data.put("message", "Test Warning");
MCMResponse response = new MCMResponse(CoreWeb.HTTP(djangoUrl, CoreWeb.HttpMethod.POST,
data));
System.out.println("===== Warning =====");
System.out.println("Status: " + response.getStatus());
System.out.println("Message: " + response.getMessage());
}
}
}
}