Add Unit Test System & Fix player details
This commit is contained in:
parent
d3c9ad22e5
commit
f23e139b9f
12 changed files with 242 additions and 27 deletions
|
@ -2,6 +2,7 @@
|
|||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="src" path="test"/>
|
||||
<classpathentry exported="true" kind="lib" path="json-simple-1.1.1.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="mysql-connector-java-8.0.28.jar"/>
|
||||
<classpathentry exported="true" kind="lib" path="password4j-1.6.3.jar"/>
|
||||
|
|
|
@ -3,27 +3,14 @@ package be.jeffcheasey88.peeratcode;
|
|||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.net.ssl.SSLServerSocket;
|
||||
import javax.net.ssl.SSLServerSocketFactory;
|
||||
|
||||
import org.jose4j.jwa.AlgorithmConstraints.ConstraintType;
|
||||
import org.jose4j.jwk.JsonWebKey;
|
||||
import org.jose4j.jwk.PublicJsonWebKey;
|
||||
import org.jose4j.jwk.RsaJsonWebKey;
|
||||
import org.jose4j.jwk.RsaJwkGenerator;
|
||||
import org.jose4j.jws.AlgorithmIdentifiers;
|
||||
import org.jose4j.jws.JsonWebSignature;
|
||||
import org.jose4j.jwt.JwtClaims;
|
||||
import org.jose4j.jwt.consumer.ErrorCodes;
|
||||
import org.jose4j.jwt.consumer.InvalidJwtException;
|
||||
import org.jose4j.jwt.consumer.JwtConsumer;
|
||||
import org.jose4j.jwt.consumer.JwtConsumerBuilder;
|
||||
import org.jose4j.lang.JoseException;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.repository.DatabaseRepository;
|
||||
import be.jeffcheasey88.peeratcode.routes.BadgeDetails;
|
||||
|
|
|
@ -34,22 +34,31 @@ public class Class {
|
|||
content = matcher.group(3);
|
||||
Pattern empty = Pattern.compile("^\\s*$");
|
||||
while(!(empty.matcher(content).matches())){
|
||||
int quotes = content.indexOf(';');
|
||||
int braces = content.indexOf('{');
|
||||
int equals = content.indexOf('=');
|
||||
int quotes = indexOf(content,";");
|
||||
int braces = indexOf(content,"\\{");
|
||||
int equals = indexOf(content,"=");
|
||||
if(quotes < braces && quotes < equals){
|
||||
Variable variable = new Variable();
|
||||
int index = variable.parse(content);
|
||||
this.vars.add(variable);
|
||||
content = content.substring(index);
|
||||
}else if(equals >= 0 && equals < braces){
|
||||
}else if(equals < braces){
|
||||
//variable with value
|
||||
System.out.println(content);
|
||||
System.out.println("equals < braces");
|
||||
Variable variable = new Variable();
|
||||
boolean quote = false;
|
||||
Variable last = null;
|
||||
do {
|
||||
Variable variable = (last == null) ? new Variable() : new Variable(last.getModifier(), last.getType());
|
||||
int index = variable.parse(content);
|
||||
this.vars.add(variable);
|
||||
content = content.substring(index);
|
||||
quote = content.startsWith(",");
|
||||
if(quote) {
|
||||
content = content.substring(1);
|
||||
last = variable;
|
||||
}
|
||||
}while(quote);
|
||||
break;
|
||||
}else{
|
||||
Function func = new Function();
|
||||
|
@ -62,6 +71,10 @@ public class Class {
|
|||
return matcher.group(1).length();
|
||||
}
|
||||
|
||||
private int indexOf(String value, String target){
|
||||
return value.split(target)[0].length();
|
||||
}
|
||||
|
||||
public int getModifier(){
|
||||
return this.modifier;
|
||||
}
|
||||
|
@ -70,6 +83,10 @@ public class Class {
|
|||
return this.name;
|
||||
}
|
||||
|
||||
public List<Variable> getVariables(){
|
||||
return this.vars;
|
||||
}
|
||||
|
||||
public void show(){
|
||||
System.out.println(Modifier.toString(modifier)+" "+this.name+"{");
|
||||
for(Variable v : this.vars) v.show(1);
|
||||
|
|
|
@ -15,6 +15,11 @@ public class Variable {
|
|||
|
||||
public Variable(){}
|
||||
|
||||
public Variable(int modifier, String type){
|
||||
this.modifier = modifier;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
//int i = 4;
|
||||
//int i,j,k,l=1;
|
||||
//int lm ;
|
||||
|
@ -29,6 +34,8 @@ public class Variable {
|
|||
Matcher matcher = PATTERN.matcher(content);
|
||||
matcher.matches();
|
||||
|
||||
int offset = matcher.group(1).length();
|
||||
|
||||
boolean hasEquals = false;
|
||||
boolean fromMinus = false;
|
||||
String body = matcher.group(2);
|
||||
|
@ -47,21 +54,25 @@ public class Variable {
|
|||
if(value.isEmpty()){
|
||||
do {
|
||||
body = body.substring(1);
|
||||
offset++;
|
||||
}while(indexOf(body, "\\s+") == 0);
|
||||
continue;
|
||||
}
|
||||
this.value = new Value(value);
|
||||
body = body.substring(value.length()+1);
|
||||
offset+=value.length()+1;
|
||||
break;
|
||||
}else if(fromMinus){
|
||||
System.out.println("fromMinus "+value);
|
||||
this.name = value;
|
||||
body = body.substring(value.length()+1);
|
||||
offset+=value.length()+1;
|
||||
break;
|
||||
} else if(min == space){
|
||||
if(value.isEmpty()){
|
||||
do {
|
||||
body = body.substring(1);
|
||||
offset++;
|
||||
}while(indexOf(body, "\\s+") == 0);
|
||||
continue;
|
||||
}
|
||||
|
@ -76,9 +87,12 @@ public class Variable {
|
|||
}
|
||||
}
|
||||
body = body.substring(value.length()+1);
|
||||
offset+=value.length()+1;
|
||||
}else if(min == equals){
|
||||
if(this.name == null) this.name = value;
|
||||
hasEquals = true;
|
||||
body = body.substring(value.length()+1);
|
||||
offset+=value.length()+1;
|
||||
}else if(min == minus){
|
||||
value = value+"<";
|
||||
System.out.println("MINUS");
|
||||
|
@ -95,16 +109,21 @@ public class Variable {
|
|||
}
|
||||
this.type = value;
|
||||
body = body.substring(value.length());
|
||||
offset+=value.length();
|
||||
while(indexOf(body, "\\s+") == 0){
|
||||
body = body.substring(1);
|
||||
offset++;
|
||||
}
|
||||
fromMinus = true;
|
||||
System.out.println("fromMinus on "+body);
|
||||
}else if(min == quote){
|
||||
if(this.name != null) break;
|
||||
this.name = value;
|
||||
body = body.substring(value.length());
|
||||
offset+=value.length();
|
||||
break;
|
||||
}else {
|
||||
offset+=value.length()+1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -113,7 +132,7 @@ public class Variable {
|
|||
show(0);
|
||||
System.out.println("-------------");
|
||||
|
||||
return 1;
|
||||
return offset;
|
||||
}
|
||||
|
||||
private int indexOf(String value, String target){
|
||||
|
|
|
@ -28,7 +28,7 @@ public class PlayerDetails implements Response {
|
|||
@Override
|
||||
public void exec(Matcher matcher, User user, HttpReader reader, HttpWriter writer) throws Exception {
|
||||
Player player;
|
||||
if (matcher.groupCount() > 0) {
|
||||
if (matcher.group(1) != null){
|
||||
player = databaseRepo.getPlayerDetails(matcher.group(1));
|
||||
} else {
|
||||
player = databaseRepo.getPlayerDetails(user.getId());
|
||||
|
|
|
@ -38,7 +38,7 @@ public class Client extends Thread{
|
|||
}
|
||||
|
||||
private User isLogin(HttpReader reader) throws Exception{
|
||||
String auth = HttpUtil.readAutorization(reader);
|
||||
String auth = HttpUtil.readAuthorization(reader);
|
||||
if(auth == null) return null;
|
||||
try {
|
||||
JwtConsumer jwtConsumer = new JwtConsumerBuilder()
|
||||
|
@ -54,6 +54,7 @@ public class Client extends Thread{
|
|||
JwtClaims jwtClaims = jwtConsumer.processToClaims(auth);
|
||||
return new User(jwtClaims);
|
||||
}catch(Exception e){
|
||||
e.printStackTrace();
|
||||
HttpUtil.responseHeaders(writer, 401, "Access-Control-Allow-Origin: *");
|
||||
}
|
||||
return null;
|
||||
|
|
|
@ -74,9 +74,9 @@ public class HttpUtil {
|
|||
return key;
|
||||
}
|
||||
|
||||
private static Pattern AUTORIZATION = Pattern.compile("Autorization: Bearer (.*)");
|
||||
private static Pattern AUTORIZATION = Pattern.compile("Authorization: Bearer (.*)");
|
||||
|
||||
public static String readAutorization(HttpReader reader) throws Exception {
|
||||
public static String readAuthorization(HttpReader reader) throws Exception {
|
||||
String line;
|
||||
String key = null;
|
||||
while(((line = reader.readLine()) != null) && (line.length() > 0)){
|
||||
|
|
|
@ -73,6 +73,8 @@ public class Router{
|
|||
|
||||
claims.setClaim("id", id);
|
||||
|
||||
claims.setSubject("Nani ???");
|
||||
|
||||
JsonWebSignature jws = new JsonWebSignature();
|
||||
jws.setPayload(claims.toJson());
|
||||
jws.setKey(rsaJsonWebKey.getPrivateKey());
|
||||
|
|
|
@ -7,7 +7,7 @@ public class User {
|
|||
private int id;
|
||||
|
||||
public User(JwtClaims jwtClaims){
|
||||
this.id = (int) jwtClaims.getClaimValue("id");
|
||||
this.id = ((Long)jwtClaims.getClaimValue("id")).intValue();
|
||||
}
|
||||
|
||||
public int getId(){
|
||||
|
|
|
@ -4,6 +4,8 @@ import static org.junit.Assert.assertNull;
|
|||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.parser.java.Variable.Value;
|
||||
|
@ -92,4 +94,55 @@ class VariableTest {
|
|||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void case6(){
|
||||
try {
|
||||
Class clazz = new Class();
|
||||
clazz.parse("public class Test{ int i ,j,k,l=1; } ");
|
||||
|
||||
List<Variable> vars = clazz.getVariables();
|
||||
assertEquals(vars.size(), 4);
|
||||
for(int i = 0; i < 3; i++){
|
||||
Variable v = vars.get(i);
|
||||
assertEquals(0, v.getModifier());
|
||||
assertEquals("int", v.getType());
|
||||
assertEquals((char)('i'+i), v.getName().charAt(0));
|
||||
assertNull(v.getValue());
|
||||
}
|
||||
Variable v = vars.get(3);
|
||||
assertEquals(0, v.getModifier());
|
||||
assertEquals("int", v.getType());
|
||||
assertEquals('l', v.getName().charAt(0));
|
||||
assertEquals("1", ((Value)v.getValue()).value());
|
||||
}catch(Exception e){
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void case7(){
|
||||
try {
|
||||
Class clazz = new Class();
|
||||
clazz.parse("public class Test{ int i =j=k=l=4; } ");
|
||||
|
||||
List<Variable> vars = clazz.getVariables();
|
||||
assertEquals(vars.size(), 4);
|
||||
for(int i = 0; i < 3; i++){
|
||||
Variable v = vars.get(i);
|
||||
assertEquals(0, v.getModifier());
|
||||
assertEquals("int", v.getType());
|
||||
assertEquals((char)('i'+i), v.getName().charAt(0));
|
||||
assertEquals((char)('i'+i+1), ((Value)v.getValue()).value());
|
||||
}
|
||||
Variable v = vars.get(3);
|
||||
assertEquals(0, v.getModifier());
|
||||
assertEquals("int", v.getType());
|
||||
assertEquals('l', v.getName().charAt(0));
|
||||
assertEquals("4", ((Value)v.getValue()).value());
|
||||
}catch(Exception e){
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,53 @@
|
|||
package be.jeffcheasey88.peeratcode.routes;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.Main;
|
||||
import be.jeffcheasey88.peeratcode.webclient.WebClient;
|
||||
|
||||
@TestInstance(Lifecycle.PER_CLASS)
|
||||
class PlayerDetailsTests {
|
||||
|
||||
private Thread server;
|
||||
private WebClient client;
|
||||
|
||||
@BeforeAll
|
||||
void init(){
|
||||
server = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run(){
|
||||
try {
|
||||
Main.main(null);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
};
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
client = new WebClient("localhost", 80);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
void close(){
|
||||
server.interrupt();
|
||||
}
|
||||
|
||||
@Test
|
||||
void test(){
|
||||
try {
|
||||
client.auth("JeffCheasey88", "TheoPueDesPieds");
|
||||
client.route("/player/","GET");
|
||||
|
||||
client.assertResponseCode(200);
|
||||
} catch (Exception e){
|
||||
fail(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
82
test/be/jeffcheasey88/peeratcode/webclient/WebClient.java
Normal file
82
test/be/jeffcheasey88/peeratcode/webclient/WebClient.java
Normal file
|
@ -0,0 +1,82 @@
|
|||
package be.jeffcheasey88.peeratcode.webclient;
|
||||
|
||||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.net.Socket;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import be.jeffcheasey88.peeratcode.webserver.HttpReader;
|
||||
import be.jeffcheasey88.peeratcode.webserver.HttpWriter;
|
||||
|
||||
public class WebClient {
|
||||
|
||||
private static Pattern AUTORIZATION = Pattern.compile("Authorization: Bearer (.*)");
|
||||
|
||||
private Socket socket;
|
||||
private HttpReader reader;
|
||||
private HttpWriter writer;
|
||||
|
||||
private String token;
|
||||
private int responseCode;
|
||||
private List<String> headers;
|
||||
|
||||
private String host;
|
||||
private int port;
|
||||
|
||||
public WebClient(String host, int port){
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.headers = new ArrayList<>();
|
||||
}
|
||||
|
||||
private void ensureConnection() throws Exception{
|
||||
this.socket = new Socket(this.host, this.port);
|
||||
this.reader = new HttpReader(socket);
|
||||
this.writer = new HttpWriter(socket);
|
||||
this.responseCode = -1;
|
||||
this.headers.clear();
|
||||
}
|
||||
|
||||
public void auth(String user, String password) throws Exception{
|
||||
JSONObject login = new JSONObject();
|
||||
login.put("pseudo", user);
|
||||
login.put("passwd", password);
|
||||
route("/login", "POST", login.toJSONString());
|
||||
|
||||
for(String line : this.headers){
|
||||
Matcher matcher = AUTORIZATION.matcher(line);
|
||||
if(matcher.matches()){
|
||||
this.token = matcher.group(1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void route(String route, String type, String... content) throws Exception{
|
||||
ensureConnection();
|
||||
this.writer.write(type+" "+route+" HTTP/1.1\n");
|
||||
if(this.token != null) this.writer.write("Authorization: Bearer "+this.token+"\n");
|
||||
|
||||
this.writer.write("\n");
|
||||
for(String value : content) this.writer.write(value+"\n");
|
||||
this.writer.flush();
|
||||
|
||||
this.responseCode = Integer.parseInt(this.reader.readLine().split("\\s+")[1]);
|
||||
String line;
|
||||
while(((line = reader.readLine()) != null) && line.length() > 0) this.headers.add(line);
|
||||
}
|
||||
|
||||
public void assertResponseCode(int expected){
|
||||
try {
|
||||
if(expected != responseCode) fail("Expected http reponse code <"+expected+"> but found <"+responseCode+">");
|
||||
} catch (Exception e){
|
||||
fail("Failed to get the response code: "+e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue