Author: adietish
Date: 2010-10-21 09:39:41 -0400 (Thu, 21 Oct 2010)
New Revision: 25970
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/.settings/net.sf.jautodoc.prefs
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudMockClientIntegrationTest.java
Removed:
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudClientTest.java
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/.classpath
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/ChangeLog
Log:
[JBIDE-7371] added tests for 404 on bad requests, changed
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/.classpath
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/.classpath 2010-10-21
13:10:42 UTC (rev 25969)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/.classpath 2010-10-21
13:39:41 UTC (rev 25970)
@@ -3,8 +3,8 @@
<classpathentry exported="true" kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry exported="true" kind="con"
path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
- <classpathentry exported="true" kind="lib"
path="lib/httpclient-4.0.1.jar"/>
- <classpathentry exported="true" kind="lib"
path="lib/httpcore-4.0.1.jar"/>
+ <classpathentry exported="true" kind="lib"
path="lib/httpclient-4.0.1.jar"
sourcepath="/home/adietisheim/jboss-workspaces/jboss-tools/jbosstools-src/deltacloud/httpcomponents-client-4.0.3-src.zip"/>
+ <classpathentry exported="true" kind="lib"
path="lib/httpcore-4.0.1.jar"
sourcepath="/home/adietisheim/jboss-workspaces/jboss-tools/jbosstools-src/deltacloud/httpcomponents-core-4.0.1-src.zip"/>
<classpathentry exported="true" kind="lib"
path="lib/httpcore-nio-4.0.1.jar"/>
<classpathentry exported="true" kind="lib"
path="lib/httpmime-4.0.1.jar"/>
<classpathentry exported="true" kind="lib"
path="lib/apache-mime4j-0.6.jar"/>
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/.settings/net.sf.jautodoc.prefs
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/.settings/net.sf.jautodoc.prefs
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/.settings/net.sf.jautodoc.prefs 2010-10-21
13:39:41 UTC (rev 25970)
@@ -0,0 +1,6 @@
+#Thu Oct 21 13:54:40 CEST 2010
+add_header=true
+eclipse.preferences.version=1
+header_text=/*******************************************************************************\n
* Copyright (c) 2010 Red Hat, Inc.\n * Distributed under license by Red Hat, Inc. All
rights reserved.\n * This program is made available under the terms of the\n * Eclipse
Public License v1.0 which accompanies this distribution,\n * and is available at
http\://www.eclipse.org/legal/epl-v10.html\n *\n * Contributors\:\n * Red Hat, Inc. -
initial API and implementation\n
******************************************************************************/
+project_specific_settings=true
+replacements=<?xml version\="1.0"
standalone\="yes"?>\n\n<replacements>\n<replacement
key\="get" scope\="1" mode\="0">Gets
the</replacement>\n<replacement key\="set" scope\="1"
mode\="0">Sets the</replacement>\n<replacement key\="add"
scope\="1" mode\="0">Adds the</replacement>\n<replacement
key\="edit" scope\="1" mode\="0">Edits
the</replacement>\n<replacement key\="remove" scope\="1"
mode\="0">Removes the</replacement>\n<replacement
key\="init" scope\="1" mode\="0">Inits
the</replacement>\n<replacement key\="parse" scope\="1"
mode\="0">Parses the</replacement>\n<replacement
key\="create" scope\="1" mode\="0">Creates
the</replacement>\n<replacement key\="build" scope\="1"
mode\="0">Builds the</replacement>\n<replacement key\="is"
scope\="1" mode\="0">Checks if
is</replacement>\n<replacement key\="print" scope\="1"
mode\="0">Prints the</replacement>\n<replacement
key\="has" scope\="1" mode\="0">Checks
for</replacement>\n</replacements>\n\n
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/.settings/net.sf.jautodoc.prefs
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2010-10-21 13:10:42
UTC (rev 25969)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2010-10-21 13:39:41
UTC (rev 25970)
@@ -1,3 +1,9 @@
+2010-10-21 André Dietisheim <adietish(a)redhat.com>
+
+ * src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
+ (getRequest): extracted to its own method, changed to switch (from if-else)
+ (sendRequest): added http status code check (latest deltacloud 0.0.8 now reports 404 on
missing resource)
+
2010-10-20 André Dietisheim <adietish(a)redhat.com>
* src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java (getDeltaCloudType):
moved from UI to deltacloud client
@@ -2,3 +8,3 @@
(DCNS): Added API enum (for api requests)
- (getServerType): moved to instance method, reuse existing client code
+ (getServerType): moved to instance method, reuse existing client code (instead of
duplication)
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF 2010-10-21
13:10:42 UTC (rev 25969)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF 2010-10-21
13:39:41 UTC (rev 25970)
@@ -11,7 +11,8 @@
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
Export-Package:
org.jboss.tools.deltacloud.core;x-friends:="org.jboss.tools.deltacloud.ui",
- org.jboss.tools.deltacloud.core.client
+ org.jboss.tools.deltacloud.core.client,
+ org.apache.http.client.methods;x-friends:="org.jboss.tools.deltacloud.test"
Bundle-ClassPath: .,
lib/apache-mime4j-0.6.jar,
lib/commons-codec-1.3.jar,
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java 2010-10-21
13:10:42 UTC (rev 25969)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java 2010-10-21
13:39:41 UTC (rev 25970)
@@ -20,6 +20,7 @@
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
+import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -54,23 +55,43 @@
private static final String DOCUMENT_ELEMENT_DRIVER = "driver"; //$NON-NLS-1$
private static final String DOCUMENT_ELEMENT_API = "api"; //$NON-NLS-1$
+ private static final int HTTP_STATUSCODE_SERVERERROR = 500;
+ private static final int HTTP_STATUSCODE_CLIENTERROR = 400;
+ public static Logger logger = Logger.getLogger(DeltaCloudClient.class);
+
public static enum DeltaCloudType {
UNKNOWN, MOCK, EC2
}
- public static Logger logger = Logger.getLogger(DeltaCloudClient.class);
-
private static enum DCNS {
- API, INSTANCES, REALMS, IMAGES, HARDWARE_PROFILES, KEYS, START, STOP, REBOOT, DESTROY;
+ TYPE {
+ public String getResourceName() {
+ return "";
+ }
+ }
+ ,
+ INSTANCES
+ , REALMS
+ , IMAGES
+ , HARDWARE_PROFILES
+ , KEYS
+ , START
+ , STOP
+ , REBOOT
+ , DESTROY;
@Override
public String toString() {
- return "/" + name().toLowerCase();
+ return "/api/" + getResourceName();
}
+
+ public String getResourceName() {
+ return name().toLowerCase();
+ }
}
- private static enum RequestType {
+ protected static enum RequestType {
POST, GET, DELETE
};
@@ -96,48 +117,93 @@
}
private String sendRequest(String path, RequestType requestType) throws
DeltaCloudClientException {
- DefaultHttpClient httpClient = new DefaultHttpClient();
- if (username != null && password != null) {
- httpClient.getCredentialsProvider().setCredentials(
- new AuthScope(baseUrl.getHost(), baseUrl.getPort()),
- new UsernamePasswordCredentials(username, password));
- }
-
+ DefaultHttpClient httpClient = addCredentials(new DefaultHttpClient());
String requestUrl = baseUrl.toString() + path;
logger.debug("Sending Request to: " + requestUrl);
try {
- HttpUriRequest request = null;
- if (requestType == RequestType.POST) {
- request = new HttpPost(requestUrl);
- } else if (requestType == RequestType.DELETE) {
- request = new HttpDelete(requestUrl);
- } else {
- request = new HttpGet(requestUrl);
- }
-
- request.setHeader("Accept", "application/xml");
+ HttpUriRequest request = getRequest(requestType, requestUrl);
+ request.setHeader("Accept", "application/xml;q=1");
HttpResponse httpResponse = httpClient.execute(request);
-
- HttpEntity entity = httpResponse.getEntity();
-
- if (entity != null) {
- InputStream is = entity.getContent();
- String xml = readInputStreamToString(is);
- httpClient.getConnectionManager().shutdown();
-
- logger.debug("Response\n" + xml);
- return xml;
+ if (isHttpError(httpResponse.getStatusLine().getStatusCode())) {
+ throw new DeltaCloudClientException(
+ MessageFormat.format("the server reported an error \"{0}\" on
requesting \"{1}\"",
+ httpResponse.getStatusLine().getReasonPhrase(),requestUrl));
}
+ return getResponse(httpResponse.getEntity());
+ } catch (DeltaCloudClientException e) {
+ throw e;
} catch (IOException e) {
logger.error("Error processing request to: " + requestUrl, e);
throw new DeltaCloudClientException("Error processing request to: " +
requestUrl, e);
} catch (Exception e) {
throw new DeltaCloudClientException(e.getMessage());
+ } finally {
+ httpClient.getConnectionManager().shutdown();
}
- throw new DeltaCloudClientException("Could not execute request to:" +
requestUrl);
}
+ private String getResponse(HttpEntity entity) throws IOException,
+ DeltaCloudClientException {
+ if (entity == null) {
+ return null;
+ }
+ String xml = readInputStreamToString(entity.getContent());
+ logger.debug("Response\n" + xml);
+ return xml;
+ }
+
+ private boolean isHttpError(int statusCode) throws DeltaCloudClientException {
+ return isHttpServerError(statusCode)
+ || isHttpClientError(statusCode);
+ }
+
+ private boolean isHttpClientError(int statusCode) {
+ return (statusCode - HTTP_STATUSCODE_CLIENTERROR) > 0
+ && (statusCode - HTTP_STATUSCODE_CLIENTERROR) < 100;
+ }
+
+ private boolean isHttpServerError(int statusCode) {
+ return (statusCode - HTTP_STATUSCODE_SERVERERROR) > 0
+ && (statusCode - HTTP_STATUSCODE_SERVERERROR) < 100;
+ }
+
+ /**
+ * Returns a request instance for the given request type and url.
+ *
+ * @param requestType
+ * the request type to use
+ * @param requestUrl
+ * the requested url
+ * @return the request instance
+ */
+ protected HttpUriRequest getRequest(RequestType requestType, String requestUrl) {
+ switch (requestType) {
+ case POST:
+ return new HttpPost(requestUrl);
+ case DELETE:
+ return new HttpDelete(requestUrl);
+ default:
+ return new HttpGet(requestUrl);
+ }
+ }
+
+ /**
+ * Adds the credentials to the given http client.
+ *
+ * @param httpClient
+ * the http client
+ * @return the default http client
+ */
+ private DefaultHttpClient addCredentials(DefaultHttpClient httpClient) {
+ if (username != null && password != null) {
+ httpClient.getCredentialsProvider().setCredentials(
+ new AuthScope(baseUrl.getHost(), baseUrl.getPort()),
+ new UsernamePasswordCredentials(username, password));
+ }
+ return httpClient;
+ }
+
private static String readInputStreamToString(InputStream is) throws
DeltaCloudClientException {
try {
try {
@@ -164,7 +230,7 @@
DeltaCloudType serverType = DeltaCloudType.UNKNOWN;
try {
String query = "?format=xml";
- String apiResponse = sendRequest(DCNS.API + query, RequestType.GET);
+ String apiResponse = sendRequest(DCNS.TYPE + query, RequestType.GET);
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = db.parse(new InputSource(new StringReader(apiResponse)));
Modified: trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/ChangeLog
===================================================================
--- trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/ChangeLog 2010-10-21 13:10:42
UTC (rev 25969)
+++ trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/ChangeLog 2010-10-21 13:39:41
UTC (rev 25970)
@@ -1,3 +1,8 @@
+2010-10-21 André Dietisheim <adietish(a)redhat.com>
+
+ * src/org/jboss/tools/internal/deltacloud/test/DeltaCloudMockClientIntegrationTest.java
+ [JBIDE-7371] (reports404OnUnknownResource): added test for reporting 404 on bad requests
+
2010-10-20 André Dietisheim <adietish(a)redhat.com>
* src/org/jboss/tools/internal/deltacloud/test/DeltaCloudClientTest.java:
Deleted:
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudClientTest.java
===================================================================
---
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudClientTest.java 2010-10-21
13:10:42 UTC (rev 25969)
+++
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudClientTest.java 2010-10-21
13:39:41 UTC (rev 25970)
@@ -1,54 +0,0 @@
-package org.jboss.tools.internal.deltacloud.test;
-
-import static org.junit.Assert.assertEquals;
-
-import java.io.IOException;
-import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.URLConnection;
-
-import org.jboss.tools.deltacloud.core.DeltaCloudException;
-import org.jboss.tools.deltacloud.core.client.DeltaCloudClient;
-import org.jboss.tools.deltacloud.core.client.DeltaCloudClientException;
-import org.jboss.tools.internal.deltacloud.test.fakes.ServerFake;
-import org.junit.Before;
-import org.junit.Test;
-
-public class DeltaCloudClientTest {
-
- private static final String DELTACLOUD_URL = "http://localhost:3001";
- private static final String SERVERFAKE_URL = "http://localhost:3002";
- private static final String DELTACLOUD_USER = "user";
- private static final String DELTACLOUD_PASSWORD = "password";
-
- private DeltaCloudClient client;
- private DeltaCloudClient fakeServerClient;
-
- @Before
- public void createDeltaCloudClient() throws MalformedURLException {
- this.client = new DeltaCloudClient(new URL(DELTACLOUD_URL), DELTACLOUD_USER,
DELTACLOUD_PASSWORD);
- this.fakeServerClient = new DeltaCloudClient(new URL(SERVERFAKE_URL), DELTACLOUD_USER,
DELTACLOUD_PASSWORD);
- }
-
- @Test
- public void isDeltaCloudRunning() throws MalformedURLException, IOException {
- URLConnection connection = new URL(DELTACLOUD_URL).openConnection();
- connection.connect();
- }
-
- @Test
- public void canRecognizeMockDeltaCloud() throws IOException {
- assertEquals(DeltaCloudClient.DeltaCloudType.MOCK, new DeltaCloudClient(DELTACLOUD_URL,
DELTACLOUD_USER, DELTACLOUD_PASSWORD).getServerType());
- }
-
- @Test
- public void reportsUnknownUrl() throws IOException {
- ServerFake serverFake = new ServerFake(new URL(SERVERFAKE_URL).getPort(),
"<dummy></dummy>");
- serverFake.start();
- try {
- assertEquals(DeltaCloudClient.DeltaCloudType.UNKNOWN, new
DeltaCloudClient(SERVERFAKE_URL, DELTACLOUD_USER, DELTACLOUD_PASSWORD).getServerType());
- } finally {
- serverFake.stop();
- }
- }
-}
Copied:
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudMockClientIntegrationTest.java
(from rev 25954,
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudClientTest.java)
===================================================================
---
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudMockClientIntegrationTest.java
(rev 0)
+++
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudMockClientIntegrationTest.java 2010-10-21
13:39:41 UTC (rev 25970)
@@ -0,0 +1,84 @@
+package org.jboss.tools.internal.deltacloud.test;
+
+import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLConnection;
+
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.client.methods.HttpUriRequest;
+import org.jboss.tools.deltacloud.core.client.DeltaCloudClient;
+import org.jboss.tools.deltacloud.core.client.DeltaCloudClientException;
+import org.jboss.tools.internal.deltacloud.test.fakes.ServerFake;
+import org.junit.Before;
+import org.junit.Test;
+
+public class DeltaCloudMockClientIntegrationTest {
+
+ private static final String DELTACLOUD_URL = "http://localhost:3001";
+ private static final String SERVERFAKE_URL = "http://localhost:3002";
+ private static final String DELTACLOUD_USER = "mockuser";
+ private static final String DELTACLOUD_PASSWORD = "mockpassword";
+
+ private DeltaCloudClient client;
+
+ @Before
+ public void createClient() throws IOException {
+ assertTrue(isDeltaCloudRunning());
+ client = new DeltaCloudClient(DELTACLOUD_URL, DELTACLOUD_USER, DELTACLOUD_PASSWORD);
+ }
+
+ public boolean isDeltaCloudRunning() throws IOException {
+ URLConnection connection = new URL(DELTACLOUD_URL).openConnection();
+ connection.connect();
+ return true;
+ }
+
+ @Test
+ public void canRecognizeMockDeltaCloud() throws IOException {
+ assertEquals(DeltaCloudClient.DeltaCloudType.MOCK, client.getServerType());
+ }
+
+ @Test
+ public void reportsUnknownUrl() throws IOException {
+ ServerFake serverFake = new ServerFake(new URL(SERVERFAKE_URL).getPort(),
"<dummy></dummy>");
+ serverFake.start();
+ try {
+ assertEquals(DeltaCloudClient.DeltaCloudType.UNKNOWN, new
DeltaCloudClient(SERVERFAKE_URL, DELTACLOUD_USER,
+ DELTACLOUD_PASSWORD).getServerType());
+ } finally {
+ serverFake.stop();
+ }
+ }
+
+ @Test(expected = DeltaCloudClientException.class)
+ public void notAuthenticatedCannotListImages() throws MalformedURLException,
DeltaCloudClientException {
+ DeltaCloudClient client = new DeltaCloudClient(DELTACLOUD_URL, "badUser",
"badPassword");
+ client.listImages();
+ }
+
+ @Test
+ public void reports404OnUnknownResource() {
+ try {
+ DeltaCloudClient errorClient = new DeltaCloudClient(DELTACLOUD_URL) {
+ @Override
+ protected HttpUriRequest getRequest(RequestType requestType, String requestUrl) {
+ return new HttpGet(DELTACLOUD_URL + "/DUMMY");
+ }
+ };
+ errorClient.listImages();
+ } catch (Exception e) {
+ assertEquals(DeltaCloudClientException.class, e.getClass());
+ }
+ fail("no exception catched");
+ }
+
+ @Test
+ public void canListImages() throws DeltaCloudClientException {
+ client.listImages();
+ }
+}
Property changes on:
trunk/deltacloud/tests/org.jboss.tools.deltacloud.test/src/org/jboss/tools/internal/deltacloud/test/DeltaCloudMockClientIntegrationTest.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain