summaryrefslogtreecommitdiffstatsabout
diff options
context:
space:
mode:
authorPiotr Janik2011-06-16 11:44:25 (EDT)
committer Tomasz Zarna2011-06-16 11:44:25 (EDT)
commit6808ca00abde5f7df9c6c3c09f8c0921833107a5 (patch)
treec9a6d7778041018299905a59e1682b54c3365906
parentc30069281dbbce9f4aa21e85d1a7aefb3009e3af (diff)
downloadorg.eclipse.orion.server-6808ca00abde5f7df9c6c3c09f8c0921833107a5.zip
org.eclipse.orion.server-6808ca00abde5f7df9c6c3c09f8c0921833107a5.tar.gz
org.eclipse.orion.server-6808ca00abde5f7df9c6c3c09f8c0921833107a5.tar.bz2
bug 349014 - [server] Add test for GET
/gitapi/config/{key}/clone/file/{path} https://bugs.eclipse.org/bugs/show_bug.cgi?id=349014
-rw-r--r--bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitConfigHandlerV1.java64
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/ReflectionUtils.java80
-rw-r--r--tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitConfigTest.java432
3 files changed, 501 insertions, 75 deletions
diff --git a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitConfigHandlerV1.java b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitConfigHandlerV1.java
index 58fb7a1..922d9f4 100644
--- a/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitConfigHandlerV1.java
+++ b/bundles/org.eclipse.orion.server.git/src/org/eclipse/orion/server/git/servlets/GitConfigHandlerV1.java
@@ -140,18 +140,15 @@ public class GitConfigHandlerV1 extends ServletResourceHandler<String> {
URI baseLocation = getURI(request);
String key = p.segment(0);
- if (key == null || key.isEmpty())
- return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Confign entry key must be provided in URI", null));
+ String[] keySegments = keyToSegments(key);
+ if (keySegments == null)
+ return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Config entry key must be provided in the following form: section[.subsection].name", null));
JSONObject toPut = OrionServlet.readJSONRequest(request);
String value = toPut.optString(GitConstants.KEY_CONFIG_ENTRY_VALUE, null);
if (value == null || value.isEmpty())
return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Confign entry value must be provided", null));
- String[] keySegments = keyToSegments(key);
- if (keySegments == null)
- return statusHandler.handleRequest(request, response, new ServerStatus(IStatus.ERROR, HttpServletResponse.SC_BAD_REQUEST, "Config entry key must be provided in the following form: section[.subsection].name", null));
-
// PUT allows only to modify existing config entries
if (!variableExist(config, keySegments)) {
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
@@ -197,7 +194,9 @@ public class GitConfigHandlerV1 extends ServletResourceHandler<String> {
return false;
}
- // retrieves local config without any base config
+ /**
+ * Retrieves local config without any base config.
+ */
private FileBasedConfig getLocalConfig(File gitDirectory) throws IOException, ConfigInvalidException {
FileRepository db = new FileRepository(gitDirectory);
FileBasedConfig config = new FileBasedConfig(db.getConfig().getFile(), FS.detect());
@@ -205,7 +204,9 @@ public class GitConfigHandlerV1 extends ServletResourceHandler<String> {
return config;
}
- // converts whole config to JSON representation
+ /**
+ * Converts whole config to JSON representation.
+ */
JSONObject configToJSON(Config config, URI baseLocation) throws JSONException, URISyntaxException {
JSONObject result = new JSONObject();
JSONArray children = new JSONArray();
@@ -222,7 +223,9 @@ public class GitConfigHandlerV1 extends ServletResourceHandler<String> {
return result;
}
- // reads configuration entry and converts it to JSON representation
+ /**
+ * Reads configuration entry and converts it to JSON representation.
+ */
JSONObject configEntryToJSON(Config config, String[] keySegments, URI baseLocation) throws JSONException, URISyntaxException {
String value = config.getString(keySegments[0], keySegments[1], keySegments[2]);
if (value == null)
@@ -236,7 +239,9 @@ public class GitConfigHandlerV1 extends ServletResourceHandler<String> {
return result;
}
- // checks if given variable exist in configuration
+ /**
+ * Checks if given variable exist in configuration.
+ */
boolean variableExist(Config config, String[] keySegments) {
if (keySegments[1] != null && !config.getNames(keySegments[0], keySegments[1]).contains(keySegments[2]))
return false;
@@ -246,8 +251,11 @@ public class GitConfigHandlerV1 extends ServletResourceHandler<String> {
return true;
}
- // converts segments of key to key representation
- // returns null if input array is invalid
+ /**
+ * Converts array of the key segments to the string representation.
+ * @param segments array containing three elements: section, subsection and name
+ * @return string representation of the key or <code>null</code> if input array is invalid
+ */
private String segmentsToKey(String[] segments) {
if (segments.length == 3)
// check if there is subsection part
@@ -256,17 +264,27 @@ public class GitConfigHandlerV1 extends ServletResourceHandler<String> {
return null;
}
- // converts key representation to key segments
- // returns null if input string is invalid
+ /**
+ * Converts the string key representation to the key segments array.
+ * @param string key representation, expected format: section[.subsection].name
+ * @return array containing segments of keys or <code>null</code> if key is invalid
+ */
private String[] keyToSegments(String key) {
- String[] keyParts = key.split("\\.");
- if (keyParts.length == 3)
- // section, subsection and name provided
- return keyParts;
- else if (keyParts.length == 2)
- // no subsection provided
- return keyParts = new String[] {keyParts[0], null, keyParts[1]};
-
- return null;
+ int firstDot = key.indexOf('.');
+ int lastDot = key.lastIndexOf('.');
+ // we expect at least one dot character
+ if (firstDot == -1 || lastDot == -1)
+ return null;
+
+ // section is required
+ String section = key.substring(0, firstDot);
+ // subsection is optional
+ String subsection = null;
+ if (firstDot != lastDot)
+ subsection = key.substring(firstDot + 1, lastDot);
+ // name is required
+ String name = key.substring(lastDot + 1);
+
+ return new String[] {section, subsection, name};
}
}
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/ReflectionUtils.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/ReflectionUtils.java
new file mode 100644
index 0000000..1b36ce5
--- /dev/null
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/ReflectionUtils.java
@@ -0,0 +1,80 @@
+/*******************************************************************************
+ * Copyright (c) 2011 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * IBM Corporation - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.orion.server.tests;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+
+public class ReflectionUtils {
+
+ public static Object callMethod(Object object, String name, Object args[]) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
+ Class types[] = new Class[args.length];
+ for (int i = 0; i < args.length; i++) {
+ types[i] = args[i].getClass();
+ }
+ Method method = null;
+ Class clazz = object.getClass();
+ NoSuchMethodException ex = null;
+ while (method == null && clazz != null) {
+ try {
+ method = clazz.getDeclaredMethod(name, types);
+ } catch (NoSuchMethodException e) {
+ if (ex == null) {
+ ex = e;
+ }
+ clazz = clazz.getSuperclass();
+ }
+ }
+ if (method == null) {
+ throw ex;
+ }
+ method.setAccessible(true);
+ Object ret = method.invoke(object, args);
+ return ret;
+ }
+
+ public static Object callConstructor(Class clazz, Object args[]) throws IllegalAccessException, InvocationTargetException, InstantiationException {
+ Class types[] = new Class[args.length];
+ for (int i = 0; i < args.length; i++) {
+ types[i] = args[i].getClass();
+ }
+ IllegalArgumentException ex = null;
+ Object newInstance = null;
+ while (newInstance == null && clazz != null) {
+ Constructor[] constructors = clazz.getDeclaredConstructors();
+ for (Constructor c : constructors) {
+ c.setAccessible(true);
+ try {
+ newInstance = c.newInstance(args);
+ } catch (IllegalArgumentException e) {
+ if (ex == null) {
+ ex = e;
+ }
+ }
+ }
+ clazz = clazz.getSuperclass();
+ }
+ if (newInstance == null) {
+ throw ex;
+ }
+ return newInstance;
+ }
+
+ public static Object getField(Object object, String name) throws IllegalArgumentException, IllegalAccessException, SecurityException, NoSuchFieldException {
+ Field field = object.getClass().getDeclaredField(name);
+ field.setAccessible(true);
+ Object ret = field.get(object);
+ return ret;
+ }
+
+}
diff --git a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitConfigTest.java b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitConfigTest.java
index 4332c30..bb6740e 100644
--- a/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitConfigTest.java
+++ b/tests/org.eclipse.orion.server.tests/src/org/eclipse/orion/server/tests/servlets/git/GitConfigTest.java
@@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.orion.server.tests.servlets.git;
+import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
@@ -20,12 +21,16 @@ import java.net.URI;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.orion.internal.server.servlets.ProtocolConstants;
+import org.eclipse.orion.internal.server.servlets.ServletStatusHandler;
import org.eclipse.orion.server.git.GitConstants;
+import org.eclipse.orion.server.git.servlets.GitConfigHandlerV1;
+import org.eclipse.orion.server.tests.ReflectionUtils;
import org.eclipse.orion.server.tests.servlets.internal.DeleteMethodWebRequest;
import org.json.JSONArray;
import org.json.JSONException;
@@ -112,7 +117,7 @@ public class GitConfigTest extends GitTest {
}
@Test
- public void testSetAndUnsetConfigValues() throws Exception {
+ public void testGetListOfConfigEntries() throws Exception {
URI workspaceLocation = createWorkspace(getMethodName());
JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
@@ -131,11 +136,41 @@ public class GitConfigTest extends GitTest {
WebResponse response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
JSONObject project = new JSONObject(response.getText());
- JSONObject gitSection = project.optJSONObject(GitConstants.KEY_GIT);
- assertNotNull(gitSection);
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);
+
+ // get list of config entries
+ request = getGetGitConfigRequest(gitConfigUri);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject configResponse = new JSONObject(response.getText());
+ JSONArray configEntries = configResponse.optJSONArray(ProtocolConstants.KEY_CHILDREN);
+ assertNotNull(configEntries);
+ }
+ }
+
+ @Test
+ public void testAddConfigEntry() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
+
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project metadata
+ WebRequest request = getGetFilesRequest(contentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);
- assertNotNull(gitConfigUri);
// get list of config entries
request = getGetGitConfigRequest(gitConfigUri);
@@ -147,8 +182,8 @@ public class GitConfigTest extends GitTest {
int initialConfigEntriesCount = configEntries.length();
// set some dummy value
- final String ENTRY_KEY = "sectionA.subsectionB.nameC";
- final String ENTRY_VALUE = "valueXYZ";
+ final String ENTRY_KEY = "a.b.c";
+ final String ENTRY_VALUE = "v";
request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE);
response = webConversation.getResponse(request);
@@ -172,85 +207,378 @@ public class GitConfigTest extends GitTest {
}
assertNotNull(entryLocation);
+ // double check
+ Config config = getRepositoryForContentLocation(contentLocation).getConfig();
+ assertEquals(ENTRY_VALUE, config.getString("a", "b", "c"));
+ }
+ }
+
+ @Test
+ public void testGetSingleConfigEntry() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
+
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
+
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project metadata
+ WebRequest request = getGetFilesRequest(contentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);
+
+ // set some dummy value
+ final String ENTRY_KEY = "a.b.c";
+ final String ENTRY_VALUE = "v";
+
+ request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
+ JSONObject configResponse = new JSONObject(response.getText());
+ String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION);
+
+ // get value of config entry
+ request = getGetGitConfigRequest(entryLocation);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ configResponse = new JSONObject(response.getText());
+ assertEquals(ENTRY_KEY, configResponse.getString(GitConstants.KEY_CONFIG_ENTRY_KEY));
+ assertEquals(ENTRY_VALUE, configResponse.getString(GitConstants.KEY_CONFIG_ENTRY_VALUE));
+ }
+ }
+
+ @Test
+ public void testUpdateConfigEntryUsingPOST() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
+
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
+
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project metadata
+ WebRequest request = getGetFilesRequest(contentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);
+
+ // set some dummy value
+ final String ENTRY_KEY = "a.b.c";
+ final String ENTRY_VALUE = "v";
+
+ request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
+
// update config entry using POST
- final String NEW_ENTRY_VALUE_1 = "valueABC";
+ final String NEW_ENTRY_VALUE = "valueABC";
- request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, NEW_ENTRY_VALUE_1);
+ request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, NEW_ENTRY_VALUE);
response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
- // get list of config entries again
- request = getGetGitConfigRequest(gitConfigUri);
+ JSONObject configResponse = new JSONObject(response.getText());
+ String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION);
+
+ // get value of config entry
+ request = getGetGitConfigRequest(entryLocation);
response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
configResponse = new JSONObject(response.getText());
- configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN);
- assertEquals(initialConfigEntriesCount + 1, configEntries.length());
+ assertEquals(ENTRY_KEY, configResponse.getString(GitConstants.KEY_CONFIG_ENTRY_KEY));
+ assertEquals(NEW_ENTRY_VALUE, configResponse.getString(GitConstants.KEY_CONFIG_ENTRY_VALUE));
+ }
+ }
- entryLocation = null;
- for (int i = 0; i < configEntries.length(); i++) {
- JSONObject configEntry = configEntries.getJSONObject(i);
- if (ENTRY_KEY.equals(configEntry.getString(GitConstants.KEY_CONFIG_ENTRY_KEY))) {
- assertEquals(NEW_ENTRY_VALUE_1, configEntry.getString(GitConstants.KEY_CONFIG_ENTRY_VALUE));
- entryLocation = configEntry.getString(ProtocolConstants.KEY_LOCATION);
- }
- }
- assertNotNull(entryLocation);
+ @Test
+ public void testUpdateConfigEntryUsingPUT() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
+
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
+
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project metadata
+ WebRequest request = getGetFilesRequest(contentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);
+
+ // set some dummy value
+ final String ENTRY_KEY = "a.b.c";
+ final String ENTRY_VALUE = "v";
+
+ request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
+
+ JSONObject configResponse = new JSONObject(response.getText());
+ String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION);
// update config entry using PUT
- final String NEW_ENTRY_VALUE_2 = "valueABCXYZ";
+ final String NEW_ENTRY_VALUE = "v2";
- request = getPutGitConfigRequest(entryLocation, NEW_ENTRY_VALUE_2);
+ request = getPutGitConfigRequest(entryLocation, NEW_ENTRY_VALUE);
response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
- // get list of config entries again
- request = getGetGitConfigRequest(gitConfigUri);
+ // get value of config entry
+ request = getGetGitConfigRequest(entryLocation);
response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
configResponse = new JSONObject(response.getText());
- configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN);
- assertEquals(initialConfigEntriesCount + 1, configEntries.length());
+ assertEquals(ENTRY_KEY, configResponse.getString(GitConstants.KEY_CONFIG_ENTRY_KEY));
+ assertEquals(NEW_ENTRY_VALUE, configResponse.getString(GitConstants.KEY_CONFIG_ENTRY_VALUE));
+ }
+ }
- entryLocation = null;
- for (int i = 0; i < configEntries.length(); i++) {
- JSONObject configEntry = configEntries.getJSONObject(i);
- if (ENTRY_KEY.equals(configEntry.getString(GitConstants.KEY_CONFIG_ENTRY_KEY))) {
- assertEquals(NEW_ENTRY_VALUE_2, configEntry.getString(GitConstants.KEY_CONFIG_ENTRY_VALUE));
- entryLocation = configEntry.getString(ProtocolConstants.KEY_LOCATION);
- }
- }
- assertNotNull(entryLocation);
+ @Test
+ public void testDeleteConfigEntry() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
+
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
+
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project metadata
+ WebRequest request = getGetFilesRequest(contentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);
- // test PUT with invalid entry
- String invalidEntryLocation = entryLocation.replace(ENTRY_KEY, "qwerty.asdfg");
- request = getPutGitConfigRequest(invalidEntryLocation, NEW_ENTRY_VALUE_2);
+ // set some dummy value
+ final String ENTRY_KEY = "a.b.c";
+ final String ENTRY_VALUE = "v";
+
+ request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE);
response = webConversation.getResponse(request);
- assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode());
+ assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
+
+ JSONObject configResponse = new JSONObject(response.getText());
+ String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION);
+
+ // check if it exists
+ request = getGetGitConfigRequest(entryLocation);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
// delete config entry
request = getDeleteGitConfigRequest(entryLocation);
response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
- // get list of config entries again
- request = getGetGitConfigRequest(gitConfigUri);
+ // it shouldn't exist
+ request = getGetGitConfigRequest(entryLocation);
response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode());
+
+ // so next delete operation should fail
+ request = getDeleteGitConfigRequest(entryLocation);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode());
+ }
+ }
+
+ @Test
+ public void testCreateInvalidConfigEntry() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
+
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
+
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project metadata
+ WebRequest request = getGetFilesRequest(contentLocation);
+ WebResponse response = webConversation.getResponse(request);
assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
- configResponse = new JSONObject(response.getText());
- configEntries = configResponse.getJSONArray(ProtocolConstants.KEY_CHILDREN);
- assertEquals(initialConfigEntriesCount, configEntries.length());
+ JSONObject project = new JSONObject(response.getText());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);
- boolean found = false;
- for (int i = 0; i < configEntries.length(); i++) {
- JSONObject configEntry = configEntries.getJSONObject(i);
- if (ENTRY_KEY.equals(configEntry.getString(GitConstants.KEY_CONFIG_ENTRY_KEY)))
- found = true;
- }
- assertEquals(false, found);
+ final String INVALID_ENTRY_KEY = "a"; // no name specified, dot missing
+ final String ENTRY_VALUE = "v";
+
+ // try to set entry with invalid key
+ request = getPostGitConfigRequest(gitConfigUri, INVALID_ENTRY_KEY, ENTRY_VALUE);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
+ }
+ }
+
+ @Test
+ public void testUpdateUnexistingConfigEntryUsingPUT() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
+
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
+
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project metadata
+ WebRequest request = getGetFilesRequest(contentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);
+
+ final String ENTRY_KEY = "a.b.c";
+ final String ENTRY_VALUE = "v";
+
+ String invalidEntryLocation = gitConfigUri.replace(GitConstants.CONFIG_RESOURCE, GitConstants.CONFIG_RESOURCE + "/" + ENTRY_KEY);
+
+ // check if it doesn't exist
+ request = getGetGitConfigRequest(invalidEntryLocation);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode());
+
+ // try to update unexisting config entry using PUT (not allowed)
+ request = getPutGitConfigRequest(invalidEntryLocation, ENTRY_VALUE);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_NOT_FOUND, response.getResponseCode());
+ }
+ }
+
+ @Test
+ public void testRequestWithMissingArguments() throws Exception {
+ URI workspaceLocation = createWorkspace(getMethodName());
+ JSONObject projectTop = createProjectOrLink(workspaceLocation, getMethodName() + "-top", null);
+ IPath clonePathTop = new Path("file").append(projectTop.getString(ProtocolConstants.KEY_ID)).makeAbsolute();
+
+ JSONObject projectFolder = createProjectOrLink(workspaceLocation, getMethodName() + "-folder", null);
+ IPath clonePathFolder = new Path("file").append(projectFolder.getString(ProtocolConstants.KEY_ID)).append("folder").makeAbsolute();
+
+ IPath[] clonePaths = new IPath[] {clonePathTop, clonePathFolder};
+
+ for (IPath clonePath : clonePaths) {
+ // clone a repo
+ String contentLocation = clone(clonePath).getString(ProtocolConstants.KEY_CONTENT_LOCATION);
+
+ // get project metadata
+ WebRequest request = getGetFilesRequest(contentLocation);
+ WebResponse response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
+ JSONObject project = new JSONObject(response.getText());
+ JSONObject gitSection = project.getJSONObject(GitConstants.KEY_GIT);
+ String gitConfigUri = gitSection.getString(GitConstants.KEY_CONFIG);
+
+ final String ENTRY_KEY = "a.b.c";
+ final String ENTRY_VALUE = "v";
+
+ // missing key
+ request = getPostGitConfigRequest(gitConfigUri, null, ENTRY_VALUE);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
+
+ // missing value
+ request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, null);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
+
+ // missing key and value
+ request = getPostGitConfigRequest(gitConfigUri, null, null);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
+
+ // add some config
+ request = getPostGitConfigRequest(gitConfigUri, ENTRY_KEY, ENTRY_VALUE);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_CREATED, response.getResponseCode());
+ JSONObject configResponse = new JSONObject(response.getText());
+ String entryLocation = configResponse.getString(ProtocolConstants.KEY_LOCATION);
+
+ // put without value
+ request = getPutGitConfigRequest(entryLocation, null);
+ response = webConversation.getResponse(request);
+ assertEquals(HttpURLConnection.HTTP_BAD_REQUEST, response.getResponseCode());
}
}
+ @Test
+ public void testKeyToSegmentsMethod() throws Exception {
+ Object configHandler = ReflectionUtils.callConstructor(GitConfigHandlerV1.class, new Object[] {new ServletStatusHandler()});
+
+ String[] segments = (String[]) ReflectionUtils.callMethod(configHandler, "keyToSegments", new Object[] {"a.b.c"});
+ assertArrayEquals(new String[] {"a", "b", "c"}, segments);
+
+ segments = (String[]) ReflectionUtils.callMethod(configHandler, "keyToSegments", new Object[] {"a.c"});
+ assertArrayEquals(new String[] {"a", null, "c"}, segments);
+
+ segments = (String[]) ReflectionUtils.callMethod(configHandler, "keyToSegments", new Object[] {"a.b.c.d"});
+ assertArrayEquals(new String[] {"a", "b.c", "d"}, segments);
+
+ segments = (String[]) ReflectionUtils.callMethod(configHandler, "keyToSegments", new Object[] {"a"});
+ assertArrayEquals(null, segments);
+ }
+
+ @Test
+ public void testSegmentsToKeyMethod() throws Exception {
+ Object configHandler = ReflectionUtils.callConstructor(GitConfigHandlerV1.class, new Object[] {new ServletStatusHandler()});
+
+ String key = (String) ReflectionUtils.callMethod(configHandler, "segmentsToKey", new Object[] {new String[] {"a", "b", "c"}});
+ assertEquals("a.b.c", key);
+
+ key = (String) ReflectionUtils.callMethod(configHandler, "segmentsToKey", new Object[] {new String[] {"a", null, "c"}});
+ assertEquals("a.c", key);
+
+ key = (String) ReflectionUtils.callMethod(configHandler, "segmentsToKey", new Object[] {new String[] {"a", "b.c", "d"}});
+ assertEquals("a.b.c.d", key);
+
+ key = (String) ReflectionUtils.callMethod(configHandler, "segmentsToKey", new Object[] {new String[] {"a", "b"}});
+ assertEquals(null, key);
+
+ key = (String) ReflectionUtils.callMethod(configHandler, "segmentsToKey", new Object[] {new String[] {"a", "b", "c", "d"}});
+ assertEquals(null, key);
+ }
+
static WebRequest getDeleteGitConfigRequest(String location) {
String requestURI;
if (location.startsWith("http://"))