State Case Test
This commit is contained in:
parent
7c3558f5a8
commit
bc73dc6624
7 changed files with 213 additions and 18 deletions
8
route-test/all_empty/fetch_chapter.test
Normal file
8
route-test/all_empty/fetch_chapter.test
Normal file
|
@ -0,0 +1,8 @@
|
|||
/chapters
|
||||
GET
|
||||
Authorization: Bearer <token:test@peerat.dev>
|
||||
|
||||
|
||||
200
|
||||
|
||||
[]
|
|
@ -57,18 +57,21 @@ public class Configuration {
|
|||
return this;
|
||||
}
|
||||
|
||||
public void load() throws Exception{
|
||||
if(!this._file.exists()) return;
|
||||
public Configuration load() throws Exception{
|
||||
if(!this._file.exists()) return this;
|
||||
BufferedReader reader = new BufferedReader(new FileReader(this._file));
|
||||
String line;
|
||||
while((line = reader.readLine()) != null){
|
||||
String[] split = line.split("=");
|
||||
Field field = getClass().getDeclaredField(split[0]);
|
||||
int index = line.indexOf('=');
|
||||
String key = line.substring(0, index);
|
||||
String value = line.substring(index+1);
|
||||
Field field = getClass().getDeclaredField(key);
|
||||
if(field == null) continue;
|
||||
field.setAccessible(true);
|
||||
injectValue(field, split[1]);
|
||||
injectValue(field, value);
|
||||
}
|
||||
reader.close();
|
||||
return this;
|
||||
}
|
||||
|
||||
private void injectValue(Field field, String value) throws IllegalAccessException {
|
||||
|
@ -131,48 +134,48 @@ public class Configuration {
|
|||
public boolean isProduction(){
|
||||
return this.prod;
|
||||
}
|
||||
|
||||
public String getDbHost() {
|
||||
|
||||
public String getDbHost(){
|
||||
return this.db_host;
|
||||
}
|
||||
|
||||
public int getDbPort() {
|
||||
public int getDbPort(){
|
||||
return this.db_port;
|
||||
}
|
||||
|
||||
public String getDbUser() {
|
||||
public String getDbUser(){
|
||||
return this.db_user;
|
||||
}
|
||||
|
||||
public String getDbDatabase() {
|
||||
public String getDbDatabase(){
|
||||
return this.db_database;
|
||||
}
|
||||
|
||||
public String getDbPassword() {
|
||||
public String getDbPassword(){
|
||||
return this.db_password;
|
||||
}
|
||||
|
||||
public String getSslKeystore() {
|
||||
|
||||
public String getSslKeystore(){
|
||||
return this.ssl_keystore;
|
||||
}
|
||||
|
||||
public String getTokenIssuer() {
|
||||
public String getTokenIssuer(){
|
||||
return this.token_issuer;
|
||||
}
|
||||
|
||||
public int getTokenExpiration() {
|
||||
public int getTokenExpiration(){
|
||||
return this.token_expiration;
|
||||
}
|
||||
|
||||
public String getSslKeystorePasswd() {
|
||||
public String getSslKeystorePasswd(){
|
||||
return this.ssl_keystorePasswd;
|
||||
}
|
||||
|
||||
public int getTcpPort() {
|
||||
public int getTcpPort(){
|
||||
return this.tcp_port;
|
||||
}
|
||||
|
||||
public boolean useSsl() {
|
||||
public boolean useSsl(){
|
||||
return this.use_ssl;
|
||||
}
|
||||
|
||||
|
|
|
@ -54,6 +54,9 @@ import dev.peerat.framework.RouteInterceptor;
|
|||
import dev.peerat.framework.Router;
|
||||
|
||||
public class Main{
|
||||
|
||||
private static Router<PeerAtUser> ACCESS_ROUTER;
|
||||
|
||||
public static void main(String[] args) throws Exception{
|
||||
Configuration config = new Configuration("config.txt")
|
||||
.addDefaultValue("users_files", "/tmp/users_files");
|
||||
|
@ -77,6 +80,7 @@ public class Main{
|
|||
"Access-Control-Allow-Origin: *",
|
||||
"Access-Control-Allow-Methods: *",
|
||||
"Access-Control-Allow-Headers: *");
|
||||
ACCESS_ROUTER = router;
|
||||
|
||||
router.setDefault((matcher, context, reader, writer) -> {
|
||||
context.response(404);
|
||||
|
|
|
@ -220,6 +220,7 @@ public class DatabaseRepository {
|
|||
|
||||
public int getPlayerId(String email){
|
||||
try {
|
||||
ensureConnection();
|
||||
PreparedStatement completionsStmt = DatabaseQuery.GET_PLAYER_EMAIL.prepare(this.con);
|
||||
completionsStmt.setString(1, email);
|
||||
ResultSet result = completionsStmt.executeQuery();
|
||||
|
|
7
test/dev/peerat/backend/routes/DatabaseSeeder.java
Normal file
7
test/dev/peerat/backend/routes/DatabaseSeeder.java
Normal file
|
@ -0,0 +1,7 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
public interface DatabaseSeeder{
|
||||
|
||||
void setup() throws Exception;
|
||||
|
||||
}
|
162
test/dev/peerat/backend/routes/StateTestCase.java
Normal file
162
test/dev/peerat/backend/routes/StateTestCase.java
Normal file
|
@ -0,0 +1,162 @@
|
|||
package dev.peerat.backend.routes;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.function.Function;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.opentest4j.AssertionFailedError;
|
||||
|
||||
import dev.peerat.backend.Configuration;
|
||||
import dev.peerat.backend.Main;
|
||||
import dev.peerat.backend.model.PeerAtUser;
|
||||
import dev.peerat.backend.repository.DatabaseRepository;
|
||||
import dev.peerat.framework.Locker;
|
||||
import dev.peerat.framework.Locker.Key;
|
||||
import dev.peerat.framework.RequestType;
|
||||
import dev.peerat.framework.Router;
|
||||
|
||||
public class StateTestCase{
|
||||
|
||||
@Test
|
||||
void main() throws Exception{
|
||||
Locker<Boolean> locker = new Locker<>();
|
||||
Thread server = new Thread(new Runnable(){
|
||||
@Override
|
||||
public void run(){
|
||||
try {
|
||||
locker.setValue(true);
|
||||
Main.main(null);
|
||||
} catch (Exception e){
|
||||
e.printStackTrace();
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
Key key = new Key();
|
||||
locker.init(key);
|
||||
server.start();
|
||||
Thread.sleep(3000);
|
||||
locker.lock(key);
|
||||
locker.unlock(key);
|
||||
|
||||
Configuration config = new Configuration("config.txt");
|
||||
config.load();
|
||||
|
||||
DatabaseRepository repo = new DatabaseRepository(config);
|
||||
|
||||
Field field = Main.class.getDeclaredField("ACCESS_ROUTER");
|
||||
field.setAccessible(true);
|
||||
Router<PeerAtUser> router = (Router<PeerAtUser>) field.get(null);
|
||||
Function<String, String> tokenProvider = (email) -> {
|
||||
try{
|
||||
return router.createAuthUser(new PeerAtUser(repo.getPlayerId(email)));
|
||||
}catch(Exception ex){
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
File dir = new File("./route-test/");
|
||||
for(File cases : dir.listFiles()){
|
||||
if(!cases.isDirectory()) continue;
|
||||
List<File> responses = new ArrayList<>();
|
||||
File dbsetup = null;
|
||||
for(File file : cases.listFiles()){
|
||||
if(file.getName().endsWith(".class")) dbsetup = file; else responses.add(file);
|
||||
}
|
||||
// Class<?> clazz = new URLClassLoader(new URL[]{cases.toURI().toURL()}).loadClass(dbsetup.getAbsolutePath().substring(cases.getAbsolutePath().length()+1).replace("/", ".").replace("\\", ".").replace(".class", ""));
|
||||
// DatabaseSeeder seeder = (DatabaseSeeder) clazz.newInstance();
|
||||
// seeder.setup();
|
||||
for(File file : responses){
|
||||
StateCase state = new StateCase(file, tokenProvider);
|
||||
state.execute("127.0.0.1", config.getTcpPort());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class StateCase{
|
||||
|
||||
private static final Pattern TOKEN_PATTERN = Pattern.compile("^(.*)<token:(.*)>(.*)$");
|
||||
|
||||
private String path;
|
||||
private RequestType type;
|
||||
private List<String> headers;
|
||||
private String payload;
|
||||
|
||||
private int responseCode;
|
||||
private List<String> responseHeaders;
|
||||
private String responsePayload;
|
||||
|
||||
public StateCase(File file, Function<String, String> tokenProvider) throws Exception{
|
||||
BufferedReader reader = new BufferedReader(new FileReader(file));
|
||||
String line;
|
||||
String payloadBuffer;
|
||||
this.headers = new ArrayList<>();
|
||||
this.responseHeaders = new ArrayList<>();
|
||||
|
||||
this.path = reader.readLine();
|
||||
this.type = RequestType.valueOf(reader.readLine());
|
||||
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) headers.add(line);
|
||||
|
||||
payloadBuffer = "";
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) payloadBuffer+=line;
|
||||
this.payload = payloadBuffer;
|
||||
|
||||
this.responseCode = Integer.parseInt(reader.readLine());
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) responseHeaders.add(line);
|
||||
|
||||
payloadBuffer = "";
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) payloadBuffer+=line;
|
||||
this.responsePayload = payloadBuffer;
|
||||
|
||||
reader.close();
|
||||
|
||||
this.headers = Arrays.asList(this.headers.stream().<String>map((header) -> {
|
||||
Matcher matcher = TOKEN_PATTERN.matcher(header);
|
||||
return matcher.matches() ? matcher.group(1)+tokenProvider.apply(matcher.group(2))+matcher.group(3) : header;
|
||||
}).toArray((size) -> new String[size]));
|
||||
}
|
||||
|
||||
public void execute(String host, int port) throws Exception{
|
||||
Socket socket = new Socket(host, port);
|
||||
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
|
||||
writer.write(this.type.toString()+" "+path+" HTTP/1.1\n");
|
||||
for(String header : headers) writer.write(header+"\n");
|
||||
writer.write("\n");
|
||||
if(payload != null) writer.write(payload+"\n");
|
||||
writer.flush();
|
||||
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
|
||||
int responseCode;
|
||||
Set<String> responseHeaders = new HashSet<>();
|
||||
String responseBody = "";
|
||||
|
||||
String line;
|
||||
responseCode = Integer.parseInt(reader.readLine().split("\\s+")[1]);
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) responseHeaders.add(line);
|
||||
|
||||
while(((line = reader.readLine() )!= null) && (!line.isEmpty())) responseBody+=line;
|
||||
|
||||
socket.close();
|
||||
|
||||
if(this.responseCode != responseCode) throw new AssertionFailedError("Excepted "+this.responseCode+" but have "+responseCode);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
10
test/dev/peerat/backend/routes/states/AllEmptyTests.java
Normal file
10
test/dev/peerat/backend/routes/states/AllEmptyTests.java
Normal file
|
@ -0,0 +1,10 @@
|
|||
package dev.peerat.backend.routes.states;
|
||||
|
||||
import dev.peerat.backend.routes.DatabaseSeeder;
|
||||
|
||||
public class AllEmptyTests implements DatabaseSeeder{
|
||||
|
||||
@Override
|
||||
public void setup() throws Exception{}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue