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,33 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
<profile default="false" name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<processorPath useClasspath="true" />
<module name="writelatex-git-bridge" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="writelatex-git-bridge" target="1.7" />
</bytecodeTargetLevel>
</component>
</project>

View file

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="IdProvider" IDEtalkID="7A88E1BAD9A613745B3F0AE6CAB24EEC" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_6" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View file

@ -5,5 +5,10 @@
"username": "REDACTED", "username": "REDACTED",
"password": "REDACTED", "password": "REDACTED",
"postbackBaseUrl": "http://127.0.0.1", "postbackBaseUrl": "http://127.0.0.1",
"serviceName": "Overleaf" "serviceName": "Overleaf",
"oauth2": {
"oauth2ClientID": "clientID",
"oauth2ClientSecret": "oauth2 client secret",
"oauth2Server": "https://www.overleaf.com"
}
} }

View file

@ -102,6 +102,26 @@
<artifactId>joda-time</artifactId> <artifactId>joda-time</artifactId>
<version>2.7</version> <version>2.7</version>
</dependency> </dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-gson</artifactId>
<version>1.20.0</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View file

@ -1,5 +1,6 @@
package uk.ac.ic.wlgitbridge.application; 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.ConfigFileException;
import uk.ac.ic.wlgitbridge.application.exception.ArgsException; import uk.ac.ic.wlgitbridge.application.exception.ArgsException;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException; 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.Gson;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
@ -9,6 +9,7 @@ import uk.ac.ic.wlgitbridge.snapshot.base.JSONSource;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.Reader;
/** /**
* Created by Winston on 05/12/14. * Created by Winston on 05/12/14.
@ -22,13 +23,14 @@ public class Config implements JSONSource {
private String apiBaseURL; private String apiBaseURL;
private String postbackURL; private String postbackURL;
private String serviceName; private String serviceName;
private Oauth2 oauth2;
public Config(String configFilePath) throws ConfigFileException, IOException { public Config(String configFilePath) throws ConfigFileException, IOException {
try { this(new FileReader(configFilePath));
fromJSON(new Gson().fromJson(new FileReader(configFilePath), JsonElement.class));
} catch (JsonParseException e) {
throw new IOException();
} }
Config(Reader reader) {
fromJSON(new Gson().fromJson(reader, JsonElement.class));
} }
@Override @Override
@ -48,6 +50,7 @@ public class Config implements JSONSource {
if (!postbackURL.endsWith("/")) { if (!postbackURL.endsWith("/")) {
postbackURL += "/"; postbackURL += "/";
} }
oauth2 = new Gson().fromJson(configObject.get("oauth2"), Oauth2.class);
} }
public int getPort() { public int getPort() {
@ -70,6 +73,25 @@ public class Config implements JSONSource {
return apiBaseURL; 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) { private JsonElement getElement(JsonObject configObject, String name) {
JsonElement element = configObject.get(name); JsonElement element = configObject.get(name);
if (element == null) { if (element == null) {
@ -86,12 +108,4 @@ public class Config implements JSONSource {
return element.getAsString(); 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.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder; import org.eclipse.jetty.servlet.ServletHolder;
import org.eclipse.jetty.util.log.Log; 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.application.jetty.NullLogger;
import uk.ac.ic.wlgitbridge.bridge.BridgeAPI; import uk.ac.ic.wlgitbridge.bridge.BridgeAPI;
import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException; import uk.ac.ic.wlgitbridge.git.exception.InvalidRootDirectoryPathException;

View file

@ -0,0 +1,69 @@
package uk.ac.ic.wlgitbridge.application.config;
import org.junit.Test;
import java.io.Reader;
import java.io.StringReader;
import static org.junit.Assert.*;
/**
* Created by winston on 25/10/15.
*/
public class ConfigTest {
@Test
public void testConstructWithOauth() {
Reader reader = new StringReader("{\n" +
" \"port\": 80,\n" +
" \"rootGitDirectory\": \"/var/wlgb/git\",\n" +
" \"apiBaseUrl\": \"http://127.0.0.1:60000/api/v0\",\n" +
" \"username\": \"REDACTED\",\n" +
" \"password\": \"REDACTED\",\n" +
" \"postbackBaseUrl\": \"http://127.0.0.1\",\n" +
" \"serviceName\": \"Overleaf\",\n" +
" \"oauth2\": {\n" +
" \"oauth2ClientID\": \"clientID\",\n" +
" \"oauth2ClientSecret\": \"oauth2 client secret\",\n" +
" \"oauth2Server\": \"https://www.overleaf.com\"\n" +
" }\n" +
"}\n");
Config config = new Config(reader);
assertEquals(80, config.getPort());
assertEquals("/var/wlgb/git", config.getRootGitDirectory());
assertEquals("http://127.0.0.1:60000/api/v0/", config.getAPIBaseURL());
assertEquals("REDACTED", config.getUsername());
assertEquals("REDACTED", config.getPassword());
assertEquals("http://127.0.0.1/", config.getPostbackURL());
assertEquals("Overleaf", config.getServiceName());
assertTrue(config.isUsingOauth2());
assertEquals("clientID", config.getOauth2().getOauth2ClientID());
assertEquals("oauth2 client secret", config.getOauth2().getOauth2ClientSecret());
assertEquals("https://www.overleaf.com", config.getOauth2().getOauth2Server());
}
@Test (expected = AssertionError.class)
public void testConstructWithoutOauth() {
Reader reader = new StringReader("{\n" +
" \"port\": 80,\n" +
" \"rootGitDirectory\": \"/var/wlgb/git\",\n" +
" \"apiBaseUrl\": \"http://127.0.0.1:60000/api/v0\",\n" +
" \"username\": \"REDACTED\",\n" +
" \"password\": \"REDACTED\",\n" +
" \"postbackBaseUrl\": \"http://127.0.0.1\",\n" +
" \"serviceName\": \"Overleaf\",\n" +
" \"oauth2\": null\n" +
"}\n");
Config config = new Config(reader);
assertEquals(80, config.getPort());
assertEquals("/var/wlgb/git", config.getRootGitDirectory());
assertEquals("http://127.0.0.1:60000/api/v0/", config.getAPIBaseURL());
assertEquals("REDACTED", config.getUsername());
assertEquals("REDACTED", config.getPassword());
assertEquals("http://127.0.0.1/", config.getPostbackURL());
assertEquals("Overleaf", config.getServiceName());
assertFalse(config.isUsingOauth2());
config.getOauth2();
}
}

View file

@ -1,48 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_7" inherit-compiler-output="false">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/test/unit" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/test/system" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/it/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/it/resources" type="java-test-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.10" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" name="Maven: org.jmock:jmock-junit4:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.jmock:jmock:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-library:1.1" level="project" />
<orderEntry type="library" name="Maven: junit:junit-dep:4.4" level="project" />
<orderEntry type="library" name="Maven: org.jmock:jmock-legacy:2.6.0" level="project" />
<orderEntry type="library" name="Maven: org.objenesis:objenesis:1.0" level="project" />
<orderEntry type="library" name="Maven: cglib:cglib-nodep:2.1_3" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jetty.aggregate:jetty-all:9.2.6.v20141205" level="project" />
<orderEntry type="library" name="Maven: javax.websocket:javax.websocket-api:1.0" level="project" />
<orderEntry type="library" name="Maven: javax.servlet:javax.servlet-api:3.1.0" level="project" />
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.3.1" level="project" />
<orderEntry type="library" name="Maven: com.ning:async-http-client:1.9.3" level="project" />
<orderEntry type="library" name="Maven: io.netty:netty:3.9.5.Final" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.7" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jgit:org.eclipse.jgit:3.6.0.201412230720-r" level="project" />
<orderEntry type="library" name="Maven: com.jcraft:jsch:0.1.50" level="project" />
<orderEntry type="library" name="Maven: com.googlecode.javaewah:JavaEWAH:0.7.9" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpclient:4.1.3" level="project" />
<orderEntry type="library" name="Maven: org.apache.httpcomponents:httpcore:4.1.4" level="project" />
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.1.1" level="project" />
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.4" level="project" />
<orderEntry type="library" name="Maven: org.eclipse.jgit:org.eclipse.jgit.http.server:3.6.0.201412230720-r" level="project" />
<orderEntry type="library" name="Maven: org.xerial:sqlite-jdbc:3.8.7" level="project" />
<orderEntry type="library" name="Maven: org.slf4j:slf4j-nop:1.7.9" level="project" />
<orderEntry type="library" name="Maven: joda-time:joda-time:2.7" level="project" />
</component>
</module>