You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ka...@apache.org on 2014/01/30 12:28:41 UTC
git commit: updated refs/heads/4.3-forward to db3dc2e
Updated Branches:
refs/heads/4.3-forward 1e9f066df -> db3dc2ee1
Some rework stratosphere ssp plugin
* add missing command entry in commands.properties
* migrate httpclient 3.x to 4.x
* fix the broken SspClient
* add webapp session checking in mock ssp server
Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/db3dc2ee
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/db3dc2ee
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/db3dc2ee
Branch: refs/heads/4.3-forward
Commit: db3dc2ee17c699f1abf42ea2fbe09ec2748caa4a
Parents: 1e9f066
Author: Hiroaki KAWAI <ka...@stratosphere.co.jp>
Authored: Thu Jan 30 20:24:57 2014 +0900
Committer: U-kawai-x230\kawai <kawai@kawai-x230.(none)>
Committed: Thu Jan 30 20:28:31 2014 +0900
----------------------------------------------------------------------
client/tomcatconf/commands.properties.in | 5 +
.../cloudstack/network/element/SspClient.java | 272 +++++++++----------
.../cloudstack/network/element/SspElement.java | 10 +-
.../stratosphere-ssp/sspmock/sspmock.py | 58 ++--
.../network/element/SspClientTest.java | 75 ++---
5 files changed, 204 insertions(+), 216 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/db3dc2ee/client/tomcatconf/commands.properties.in
----------------------------------------------------------------------
diff --git a/client/tomcatconf/commands.properties.in b/client/tomcatconf/commands.properties.in
index 28789c1..add35dd 100644
--- a/client/tomcatconf/commands.properties.in
+++ b/client/tomcatconf/commands.properties.in
@@ -587,6 +587,11 @@ addBigSwitchVnsDevice=1
deleteBigSwitchVnsDevice=1
listBigSwitchVnsDevices=1
+#### stratosphere ssp commands
+
+addStratosphereSsp=1
+deleteStratoshereSsp=1
+
#### host simulator commands
configureSimulator=1
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/db3dc2ee/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java b/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java
index ee4d455..c0db92c 100644
--- a/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java
+++ b/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspClient.java
@@ -17,26 +17,33 @@
package org.apache.cloudstack.network.element;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
-
-import org.apache.commons.httpclient.HttpClient;
-import org.apache.commons.httpclient.HttpConnectionManager;
-import org.apache.commons.httpclient.HttpException;
-import org.apache.commons.httpclient.HttpMethod;
-import org.apache.commons.httpclient.HttpStatus;
-import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
-import org.apache.commons.httpclient.URIException;
-import org.apache.commons.httpclient.cookie.CookiePolicy;
-import org.apache.commons.httpclient.methods.DeleteMethod;
-import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.PutMethod;
-import org.apache.commons.httpclient.methods.RequestEntity;
-import org.apache.commons.httpclient.methods.StringRequestEntity;
-import org.apache.commons.httpclient.params.HttpClientParams;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Arrays;
+
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.entity.UrlEncodedFormEntity;
+import org.apache.http.client.methods.HttpDelete;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.methods.HttpRequestBase;
+import org.apache.http.client.params.ClientPNames;
+import org.apache.http.client.params.CookiePolicy;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.impl.conn.PoolingClientConnectionManager;
+import org.apache.http.message.BasicNameValuePair;
+import org.apache.http.params.CoreConnectionPNames;
import org.apache.log4j.Logger;
import com.google.gson.Gson;
+import com.google.gson.JsonIOException;
+import com.google.gson.JsonSyntaxException;
import com.google.gson.annotations.SerializedName;
/**
@@ -44,115 +51,73 @@ import com.google.gson.annotations.SerializedName;
*/
public class SspClient {
private static final Logger s_logger = Logger.getLogger(SspClient.class);
- private static final HttpConnectionManager s_httpclient_manager = new MultiThreadedHttpConnectionManager();
- private static final HttpClientParams s_httpclient_params = new HttpClientParams();
+ private static final HttpClient s_client = new DefaultHttpClient(
+ new PoolingClientConnectionManager());
static {
- s_httpclient_params.setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
+ s_client.getParams()
+ .setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY)
+ .setParameter(CoreConnectionPNames.SO_TIMEOUT, 10000);
}
private final String apiUrl;
private final String username;
private final String password;
- protected HttpClient client;
- protected PostMethod postMethod;
- protected DeleteMethod deleteMethod;
- protected PutMethod putMethod;
-
public SspClient(String apiUrl, String username, String password) {
super();
this.apiUrl = apiUrl;
this.username = username;
this.password = password;
- client = new HttpClient(s_httpclient_params, s_httpclient_manager);
- postMethod = new PostMethod(apiUrl);
- deleteMethod = new DeleteMethod(apiUrl);
- putMethod = new PutMethod(apiUrl);
}
- public boolean login(){
- PostMethod method = postMethod;
- method.setPath("/ws.v1/login"); // NOTE: /ws.v1/login is correct
- method.addParameter("username", username);
- method.addParameter("password", password);
+ protected HttpClient getHttpClient() { // for mock test
+ return s_client;
+ }
+ private HttpResponse innerExecuteMethod(HttpRequestBase req, String path) {
try {
- client.executeMethod(method);
- } catch (HttpException e) {
- s_logger.info("Login "+username+" to "+apiUrl+" failed", e);
- return false;
- } catch (IOException e) {
- s_logger.info("Login "+username+" to "+apiUrl+" failed", e);
- return false;
- } finally {
- method.releaseConnection();
+ URI base = new URI(apiUrl);
+ req.setURI(new URI(base.getScheme(), base.getUserInfo(), base.getHost(),
+ base.getPort(), path, null, null));
+ } catch (URISyntaxException e) {
+ s_logger.error("invalid API URL " + apiUrl + " path " + path, e);
+ return null;
}
- String apiCallPath = null;
+ HttpResponse res = null;
try {
- apiCallPath = method.getName() + " " + method.getURI().toString();
- } catch (URIException e) {
- s_logger.error("method getURI failed", e);
- }
- s_logger.info("ssp api call:" + apiCallPath + " user="+username+" status="+method.getStatusLine());
- if(method.getStatusCode() == HttpStatus.SC_OK){
- return true;
+ res = getHttpClient().execute(req);
+ s_logger.info("ssp api call:" + req + " status=" + res.getStatusLine());
+ } catch (IOException e) {
+ s_logger.error("ssp api call failed: " + req, e);
}
- return false;
+ return res;
}
- private String executeMethod(HttpMethod method){
- String apiCallPath = null;
- try {
- apiCallPath = method.getName() + " " + method.getURI().toString();
- } catch (URIException e) {
- s_logger.error("method getURI failed", e);
+ private HttpResponse executeMethod(HttpRequestBase req, String path) {
+ HttpResponse res = innerExecuteMethod(req, path);
+ if (res.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED && login()) {
+ req.reset();
+ res = innerExecuteMethod(req, path);
}
+ return res;
+ }
- String response = null;
+ public boolean login() {
+ HttpPost method = new HttpPost();
try {
- client.executeMethod(method);
- response = method.getResponseBodyAsString();
- } catch (HttpException e) {
- s_logger.error("ssp api call failed "+apiCallPath, e);
- return null;
- } catch (IOException e) {
- s_logger.error("ssp api call failed "+apiCallPath, e);
- return null;
- } finally {
- method.releaseConnection();
+ method.setEntity(new UrlEncodedFormEntity(Arrays.asList(
+ new BasicNameValuePair("username", username),
+ new BasicNameValuePair("password", password))));
+ } catch (UnsupportedEncodingException e) {
+ s_logger.error("invalid username or password", e);
+ return false;
}
- if(method.getStatusCode() == HttpStatus.SC_UNAUTHORIZED){
- if(!login()){
- return null;
- }
-
- try {
- client.executeMethod(method);
- response = method.getResponseBodyAsString();
- } catch (HttpException e) {
- s_logger.error("ssp api call failed "+apiCallPath, e);
- return null;
- } catch (IOException e) {
- s_logger.error("ssp api call failed "+apiCallPath, e);
- return null;
- } finally {
- method.releaseConnection();
- }
- }
- s_logger.info("ssp api call:" + apiCallPath + " user="+username+" status="+method.getStatusLine());
- if(method instanceof EntityEnclosingMethod){
- EntityEnclosingMethod emethod = (EntityEnclosingMethod)method;
- RequestEntity reqEntity = emethod.getRequestEntity();
- if(reqEntity instanceof StringRequestEntity){
- StringRequestEntity strReqEntity = (StringRequestEntity)reqEntity;
- s_logger.debug("ssp api request body:"+strReqEntity.getContent());
- }else{
- s_logger.debug("ssp api request body:"+emethod.getRequestEntity());
- }
+ HttpResponse res = this.innerExecuteMethod(method, "/ws.v1/login");
+ if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
+ return true;
}
- s_logger.debug("ssp api response body:" + response);
- return response;
+ return false;
}
public class TenantNetwork {
@@ -162,35 +127,36 @@ public class SspClient {
public String tenantUuid;
}
- public TenantNetwork createTenantNetwork(String tenantUuid, String networkName){
+ public TenantNetwork createTenantNetwork(String tenantUuid, String networkName) {
TenantNetwork req = new TenantNetwork();
req.name = networkName;
req.tenantUuid = tenantUuid;
- PostMethod method = postMethod;
- method.setPath("/ssp.v1/tenant-networks");
- StringRequestEntity entity = null;
- try {
- entity = new StringRequestEntity(new Gson().toJson(req), "application/json", "UTF-8");
- } catch (UnsupportedEncodingException e) {
- s_logger.error("failed creating http request body", e);
+ HttpPost method = new HttpPost();
+ method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
+ HttpResponse res = executeMethod(method, "/ssp.v1/tenant-networks");
+ if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
return null;
}
- method.setRequestEntity(entity);
-
- String response = executeMethod(method);
- if(response != null && method.getStatusCode() == HttpStatus.SC_CREATED){
- return new Gson().fromJson(response, TenantNetwork.class);
+ try {
+ return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
+ TenantNetwork.class);
+ } catch (JsonSyntaxException e) {
+ s_logger.error("reading response body failed", e);
+ } catch (JsonIOException e) {
+ s_logger.error("reading response body failed", e);
+ } catch (IllegalStateException e) {
+ s_logger.error("reading response body failed", e);
+ } catch (IOException e) {
+ s_logger.error("reading response body failed", e);
}
return null;
}
- public boolean deleteTenantNetwork(String tenantNetworkUuid){
- DeleteMethod method = deleteMethod;
- method.setPath("/ssp.v1/tenant-networks/"+tenantNetworkUuid);
-
- executeMethod(method);
- if(method.getStatusCode() == HttpStatus.SC_NO_CONTENT){
+ public boolean deleteTenantNetwork(String tenantNetworkUuid) {
+ HttpDelete method = new HttpDelete();
+ HttpResponse res = executeMethod(method, "/ssp.v1/tenant-networks/" + tenantNetworkUuid);
+ if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
return true;
}
return false;
@@ -209,63 +175,69 @@ public class SspClient {
public Integer vlanId;
}
- public TenantPort createTenantPort(String tenantNetworkUuid){
+ public TenantPort createTenantPort(String tenantNetworkUuid) {
TenantPort req = new TenantPort();
req.networkUuid = tenantNetworkUuid;
req.attachmentType = "NoAttachment";
- PostMethod method = postMethod;
- method.setPath("/ssp.v1/tenant-ports");
- StringRequestEntity entity = null;
- try {
- entity = new StringRequestEntity(new Gson().toJson(req), "application/json", "UTF-8");
- } catch (UnsupportedEncodingException e) {
- s_logger.error("failed creating http request body", e);
+ HttpPost method = new HttpPost();
+ method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
+ HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports");
+
+ if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) {
return null;
}
- method.setRequestEntity(entity);
-
- String response = executeMethod(method);
- if(response != null && method.getStatusCode() == HttpStatus.SC_CREATED){
- return new Gson().fromJson(response, TenantPort.class);
+ try {
+ return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
+ TenantPort.class);
+ } catch (JsonSyntaxException e) {
+ s_logger.error("reading response body failed", e);
+ } catch (JsonIOException e) {
+ s_logger.error("reading response body failed", e);
+ } catch (IllegalStateException e) {
+ s_logger.error("reading response body failed", e);
+ } catch (IOException e) {
+ s_logger.error("reading response body failed", e);
}
return null;
}
- public boolean deleteTenantPort(String tenantPortUuid){
- DeleteMethod method = deleteMethod;
- method.setPath("/ssp.v1/tenant-ports/"+tenantPortUuid);
+ public boolean deleteTenantPort(String tenantPortUuid) {
+ HttpDelete method = new HttpDelete();
+ HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports/" + tenantPortUuid);
- executeMethod(method);
- if(method.getStatusCode() == HttpStatus.SC_NO_CONTENT){
+ if (res != null && res.getStatusLine().getStatusCode() == HttpStatus.SC_NO_CONTENT) {
return true;
}
return false;
}
- public TenantPort updateTenantVifBinding(String portUuid, String hypervisorIpAddress){
+ public TenantPort updateTenantVifBinding(String portUuid, String hypervisorIpAddress) {
TenantPort req = new TenantPort();
- if(hypervisorIpAddress != null){
+ if (hypervisorIpAddress != null) {
req.attachmentType = "VifAttachment";
req.hypervisorIpAddress = hypervisorIpAddress;
- }else{
+ } else {
req.attachmentType = "NoAttachment";
}
- PutMethod method = putMethod;
- method.setPath("/ssp.v1/tenant-ports/"+portUuid);
- StringRequestEntity entity = null;
- try {
- entity = new StringRequestEntity(new Gson().toJson(req), "application/json", "UTF-8");
- } catch (UnsupportedEncodingException e) {
- s_logger.error("failed creating http request body", e);
+ HttpPut method = new HttpPut();
+ method.setEntity(new StringEntity(new Gson().toJson(req), ContentType.APPLICATION_JSON));
+ HttpResponse res = executeMethod(method, "/ssp.v1/tenant-ports/" + portUuid);
+ if (res == null || res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
return null;
}
- method.setRequestEntity(entity);
-
- String response = executeMethod(method);
- if(response != null && method.getStatusCode() == HttpStatus.SC_OK){
- return new Gson().fromJson(response, TenantPort.class);
+ try {
+ return new Gson().fromJson(new InputStreamReader(res.getEntity().getContent()),
+ TenantPort.class);
+ } catch (JsonSyntaxException e) {
+ s_logger.error("reading response body failed", e);
+ } catch (JsonIOException e) {
+ s_logger.error("reading response body failed", e);
+ } catch (IllegalStateException e) {
+ s_logger.error("reading response body failed", e);
+ } catch (IOException e) {
+ s_logger.error("reading response body failed", e);
}
return null;
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/db3dc2ee/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspElement.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspElement.java b/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspElement.java
index fa0488d..0364e68 100644
--- a/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspElement.java
+++ b/plugins/network-elements/stratosphere-ssp/src/org/apache/cloudstack/network/element/SspElement.java
@@ -189,14 +189,10 @@ public class SspElement extends AdapterBase implements ConnectivityProvider, Ssp
public boolean isReady(PhysicalNetworkServiceProvider provider) {
PhysicalNetwork physicalNetwork = _physicalNetworkDao.findById(provider.getPhysicalNetworkId());
assert(physicalNetwork!=null);
- if(physicalNetwork != null){
- if(fetchSspClients(physicalNetwork.getId(), physicalNetwork.getDataCenterId(), false).size() > 0){
- return true;
- }
- s_logger.warn("Ssp api endpoint not found. "+physicalNetwork.toString());
- }else{
- s_logger.warn("PhysicalNetwork is NULL.");
+ if(fetchSspClients(physicalNetwork.getId(), physicalNetwork.getDataCenterId(), false).size() > 0){
+ return true;
}
+ s_logger.warn("Ssp api endpoint not found. "+physicalNetwork.toString());
return false;
}
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/db3dc2ee/plugins/network-elements/stratosphere-ssp/sspmock/sspmock.py
----------------------------------------------------------------------
diff --git a/plugins/network-elements/stratosphere-ssp/sspmock/sspmock.py b/plugins/network-elements/stratosphere-ssp/sspmock/sspmock.py
index 5e7e610..9ac646e 100644
--- a/plugins/network-elements/stratosphere-ssp/sspmock/sspmock.py
+++ b/plugins/network-elements/stratosphere-ssp/sspmock/sspmock.py
@@ -17,7 +17,8 @@
import json
import uuid
-from flask import Flask
+from flask import Flask,request,make_response
+from beaker.middleware import SessionMiddleware
app = Flask(__name__)
tenant_networks = []
@@ -25,50 +26,59 @@ tenant_ports = []
@app.route("/ws.v1/login", methods=["POST",])
def login():
- response.content_type = "application/json"
- return ""
+ assert "username" in request.form
+ assert "password" in request.form
+ request.environ["beaker.session"]["login"] = True
+ res = make_response("", 200)
+ res.headers["Content-type"] = "application/json"
+ return res
@app.route("/ssp.v1/tenant-networks", methods=["POST",])
def create_tenant_network():
- response.content_type = "application/json"
- response.status = 201
+ if "login" not in request.environ["beaker.session"]:
+ return make_response("", 401)
obj = request.json
obj["uuid"] = str(uuid.uuid1())
tenant_networks.append(obj)
- return json.dumps(obj)
+ res = make_response(json.dumps(obj), 201)
+ res.headers["Content-type"] = "application/json"
+ return res
@app.route("/ssp.v1/tenant-networks/<tenant_net_uuid>", methods=["DELETE",])
def delete_tenant_network(tenant_net_uuid):
+ if "login" not in request.environ["beaker.session"]:
+ return make_response("", 401)
for net in tenant_networks:
if net["uuid"] == tenant_net_uuid:
tenant_networks.remove(net)
- response.status = 204
- return ""
- response.status = 404
- return ""
+ return make_response("", 204)
+ return make_response("", 404)
@app.route("/ssp.v1/tenant-ports", methods=["POST",])
def create_tenant_port():
- response.content_type = "application/json"
- response.status = 201
+ if "login" not in request.environ["beaker.session"]:
+ return make_response("", 401)
obj = request.json
obj["uuid"] = str(uuid.uuid1())
tenant_ports.append(obj)
- return json.dumps(obj)
+ res = make_response(json.dumps(obj), 201)
+ res.headers["Content-type"] = "application/json"
+ return res
@app.route("/ssp.v1/tenant-ports/<tenant_port_uuid>", methods=["DELETE",])
def delete_tenant_port(tenant_port_uuid):
+ if "login" not in request.environ["beaker.session"]:
+ return make_response("", 401)
for port in tenant_ports:
if port["uuid"] == tenant_port_uuid:
tenant_ports.remove(port)
- response.status = 204
- return ""
- response.status = 404
- return ""
+ return make_response("", 204)
+ return make_response("", 404)
@app.route("/ssp.v1/tenant-ports/<tenant_port_uuid>", methods=["PUT",])
def update_tenant_port(tenant_port_uuid):
- response.content_type = "application/json"
+ if "login" not in request.environ["beaker.session"]:
+ return make_response("", 401)
for port in tenant_ports:
if port["uuid"] == tenant_port_uuid:
obj = request.json
@@ -76,10 +86,14 @@ def update_tenant_port(tenant_port_uuid):
obj["vlan_id"] = 100
tenant_ports.remove(port)
tenant_ports.append(obj)
- response.status = 200
- return json.dumps(obj)
- response.status = 404
- return ""
+ res = make_response(json.dumps(obj), 200)
+ res.headers["Content-type"] = "application/json"
+ return res
+ return make_response("", 404)
if __name__=="__main__":
+ app.wsgi_app = SessionMiddleware(app.wsgi_app, {
+ "session.auto":True,
+ "session.type":"cookie",
+ "session.validate_key":"hoge"})
app.run(host="0.0.0.0", port=9080, debug=True)
http://git-wip-us.apache.org/repos/asf/cloudstack/blob/db3dc2ee/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java
----------------------------------------------------------------------
diff --git a/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java b/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java
index 795d1c1..627cc87 100644
--- a/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java
+++ b/plugins/network-elements/stratosphere-ssp/test/org/apache/cloudstack/network/element/SspClientTest.java
@@ -16,49 +16,41 @@
// under the License.
package org.apache.cloudstack.network.element;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.any;
+import static org.mockito.Mockito.spy;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+
+import java.io.ByteArrayInputStream;
import java.util.UUID;
-import org.apache.cloudstack.network.element.SspClient;
-import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
-import org.apache.commons.httpclient.URI;
-import org.apache.commons.httpclient.methods.DeleteMethod;
-import org.apache.commons.httpclient.methods.PostMethod;
-import org.apache.commons.httpclient.methods.PutMethod;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpUriRequest;
import org.junit.Test;
-import static org.junit.Assert.*;
-import static org.mockito.Mockito.*;
-
public class SspClientTest {
- HttpClient _client = mock(HttpClient.class);
- PostMethod _postMethod = mock(PostMethod.class);
- PutMethod _putMethod = mock(PutMethod.class);
- DeleteMethod _deleteMethod = mock(DeleteMethod.class);
-
String uuid = UUID.randomUUID().toString();
String apiUrl = "http://a.example.jp/";
String username = "foo";
String password = "bar";
- SspClient sspClient = new SspClient(apiUrl, username, password){
- {
- client = _client;
- postMethod = _postMethod;
- putMethod = _putMethod;
- deleteMethod = _deleteMethod;
- }
- };
-
- @SuppressWarnings("deprecation")
- private URI getUri() throws Exception{
- return new URI(apiUrl);
- }
@Test
public void loginTest() throws Exception {
- when(_postMethod.getURI()).thenReturn(getUri());
- when(_postMethod.getStatusCode()).thenReturn(HttpStatus.SC_OK);
+ SspClient sspClient = spy(new SspClient(apiUrl, username, password));
+
+ HttpClient client = mock(HttpClient.class);
+ HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
+ doReturn(client).when(sspClient).getHttpClient();
+ when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
+ when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_OK);
+
assertTrue(sspClient.login());
assertTrue(sspClient.login());
assertTrue(sspClient.login());
@@ -68,13 +60,18 @@ public class SspClientTest {
public void createNetworkTest() throws Exception {
String networkName = "example network 1";
String tenant_net_uuid = UUID.randomUUID().toString();
+ SspClient sspClient = spy(new SspClient(apiUrl, username, password));
+
+ HttpClient client = mock(HttpClient.class);
+ HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
+ doReturn(client).when(sspClient).getHttpClient();
+ when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
+ when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_CREATED);
+ String body = "{\"uuid\":\"" + tenant_net_uuid + "\",\"name\":\"" + networkName
+ + "\",\"tenant_uuid\":\"" + uuid + "\"}";
+ when(res.getEntity().getContent()).thenReturn(
+ new ByteArrayInputStream(body.getBytes("UTF-8")));
- when(_postMethod.getURI()).thenReturn(getUri());
- when(_postMethod.getStatusCode()).thenReturn(HttpStatus.SC_CREATED);
- when(_postMethod.getResponseBodyAsString()).thenReturn(
- "{\"uuid\":\""+tenant_net_uuid+
- "\",\"name\":\""+networkName+
- "\",\"tenant_uuid\":\""+uuid+"\"}");
SspClient.TenantNetwork tnet = sspClient.createTenantNetwork(uuid, networkName);
assertEquals(tnet.name, networkName);
assertEquals(tnet.uuid, tenant_net_uuid);
@@ -84,9 +81,13 @@ public class SspClientTest {
@Test
public void deleteNetworkTest() throws Exception {
String tenant_net_uuid = UUID.randomUUID().toString();
+ SspClient sspClient = spy(new SspClient(apiUrl, username, password));
- when(_deleteMethod.getURI()).thenReturn(getUri());
- when(_deleteMethod.getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT);
+ HttpClient client = mock(HttpClient.class);
+ HttpResponse res = mock(HttpResponse.class, RETURNS_DEEP_STUBS);
+ doReturn(client).when(sspClient).getHttpClient();
+ when(client.execute(any(HttpUriRequest.class))).thenReturn(res);
+ when(res.getStatusLine().getStatusCode()).thenReturn(HttpStatus.SC_NO_CONTENT);
sspClient.deleteTenantNetwork(tenant_net_uuid);
}