Implement config changes and tests

This commit is contained in:
Winston Li
2015-10-25 11:19:02 +00:00
parent 946c1d56be
commit c3db415ce7
10 changed files with 161 additions and 120 deletions

View File

@@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.application;
import uk.ac.ic.wlgitbridge.application.config.Config;
import uk.ac.ic.wlgitbridge.application.exception.ConfigFileException;
import uk.ac.ic.wlgitbridge.application.exception.ArgsException;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;

View File

@@ -1,4 +1,4 @@
package uk.ac.ic.wlgitbridge.application;
package uk.ac.ic.wlgitbridge.application.config;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
@@ -9,6 +9,7 @@ import uk.ac.ic.wlgitbridge.snapshot.base.JSONSource;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* Created by Winston on 05/12/14.
@@ -22,13 +23,14 @@ public class Config implements JSONSource {
private String apiBaseURL;
private String postbackURL;
private String serviceName;
private Oauth2 oauth2;
public Config(String configFilePath) throws ConfigFileException, IOException {
try {
fromJSON(new Gson().fromJson(new FileReader(configFilePath), JsonElement.class));
} catch (JsonParseException e) {
throw new IOException();
}
this(new FileReader(configFilePath));
}
Config(Reader reader) {
fromJSON(new Gson().fromJson(reader, JsonElement.class));
}
@Override
@@ -48,6 +50,7 @@ public class Config implements JSONSource {
if (!postbackURL.endsWith("/")) {
postbackURL += "/";
}
oauth2 = new Gson().fromJson(configObject.get("oauth2"), Oauth2.class);
}
public int getPort() {
@@ -70,6 +73,25 @@ public class Config implements JSONSource {
return apiBaseURL;
}
public String getServiceName() {
return serviceName;
}
public String getPostbackURL() {
return postbackURL;
}
public boolean isUsingOauth2() {
return oauth2 != null;
}
public Oauth2 getOauth2() {
if (!isUsingOauth2()) {
throw new AssertionError("Getting oauth2 when not using it");
}
return oauth2;
}
private JsonElement getElement(JsonObject configObject, String name) {
JsonElement element = configObject.get(name);
if (element == null) {
@@ -86,12 +108,4 @@ public class Config implements JSONSource {
return element.getAsString();
}
public String getServiceName() {
return serviceName;
}
public String getPostbackURL() {
return postbackURL;
}
}

View File

@@ -0,0 +1,30 @@
package uk.ac.ic.wlgitbridge.application.config;
/**
* Created by winston on 25/10/15.
*/
public class Oauth2 {
private final String oauth2ClientID;
private final String oauth2ClientSecret;
private final String oauth2Server;
public Oauth2(String oauth2ClientID, String oauth2ClientSecret, String oauth2Server) {
this.oauth2ClientID = oauth2ClientID;
this.oauth2ClientSecret = oauth2ClientSecret;
this.oauth2Server = oauth2Server;
}
public String getOauth2ClientID() {
return oauth2ClientID;
}
public String getOauth2ClientSecret() {
return oauth2ClientSecret;
}
public String getOauth2Server() {
return oauth2Server;
}
}

View File

@@ -7,7 +7,7 @@ import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log;
import uk.ac.ic.wlgitbridge.application.Config;
import uk.ac.ic.wlgitbridge.application.config.Config;
import uk.ac.ic.wlgitbridge.application.jetty.NullLogger;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;