Author: [email protected]
Date: Fri May 13 11:28:00 2011
New Revision: 1135
Log:
[AMDATUAUTH-23] Added first working integration test (OAuthSignedRequestsTest)
Added:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/OAuthProtectedTestServlet.java
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/OAuthTestBase.java
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/OAuthTestConsumer.java
Removed:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthTestBase.java
Modified:
trunk/amdatu-auth/test-integration/base/src/main/java/org/amdatu/auth/test/integration/base/AuthFixture.java
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/AuthTest.java
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthSignedRequestsTest.java
Modified:
trunk/amdatu-auth/test-integration/base/src/main/java/org/amdatu/auth/test/integration/base/AuthFixture.java
==============================================================================
---
trunk/amdatu-auth/test-integration/base/src/main/java/org/amdatu/auth/test/integration/base/AuthFixture.java
(original)
+++
trunk/amdatu-auth/test-integration/base/src/main/java/org/amdatu/auth/test/integration/base/AuthFixture.java
Fri May 13 11:28:00 2011
@@ -24,6 +24,9 @@
import org.ops4j.pax.exam.Option;
public class AuthFixture {
+ public static final String HOSTNAME = "localhost";
+ public static final String PORTNR = "8080";
+
public Option provision() {
return org.ops4j.pax.exam.CoreOptions.provision(
mavenBundle().groupId("org.amdatu.auth").artifactId("org.amdatu.auth.test.integration.base").versionAsInProject(),
@@ -59,8 +62,8 @@
private Properties getOAuthServerCfg() {
Properties properties = new Properties();
- properties.put("hostname", "localhost"); // fixme private/missing
constants
- properties.put("portnr", "8080"); // fixme private/missing constants
+ properties.put("hostname", HOSTNAME); // fixme private/missing
constants
+ properties.put("portnr", PORTNR); // fixme private/missing constants
properties.put("authorizeurl", "/oauth-server/jsp/authorize.jsp"); //
fixme private/missing constants
return properties;
}
Modified:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/AuthTest.java
==============================================================================
---
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/AuthTest.java
(original)
+++
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/AuthTest.java
Fri May 13 11:28:00 2011
@@ -25,16 +25,25 @@
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import javax.servlet.Servlet;
import org.amdatu.auth.test.integration.base.AuthFixture;
+import org.amdatu.auth.test.integration.tests.util.OAuthProtectedTestServlet;
+import org.amdatu.auth.test.integration.tests.util.OAuthTestBase;
import org.amdatu.authentication.oauth.api.OAuthServiceConsumerRegistry;
import org.amdatu.authentication.oauth.api.OAuthServiceProvider;
import org.amdatu.authentication.oauth.server.OAuthAccessTokenServlet;
import org.amdatu.authentication.oauth.server.OAuthAuthorizeTokenServlet;
import org.amdatu.authentication.oauth.server.OAuthRequestTokenServlet;
+import org.amdatu.authentication.oauth.server.OAuthTokenProvider;
import org.amdatu.core.itest.base.CoreFixture;
import org.amdatu.core.itest.base.TestContext;
import org.amdatu.web.itest.base.WebFixture;
+import org.apache.felix.dm.Component;
+import org.apache.felix.dm.DependencyManager;
import org.apache.http.HttpStatus;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -60,8 +69,11 @@
private WebFixture m_webFixture = new WebFixture();
private AuthFixture m_authFixture = new AuthFixture();
+ private DependencyManager m_dependencyManager;
private LogService m_logService;
private OAuthServiceProvider m_oAuthServiceProvider;
+ private OAuthServiceConsumerRegistry m_consumerRegistry;
+
@Configuration
public Option[] config() {
return options(
@@ -122,25 +134,57 @@
}
@Test
- public void testWithServiceDependencies(BundleContext bundleContext)
throws Exception {
+ public void runTest(BundleContext bundleContext) throws Exception {
+ // Setup test context
TestContext testContext = testContextSetUp(bundleContext);
+ // Create the dependency manager
+ m_dependencyManager = new
DependencyManager(testContext.getBundleContext());
+
+ // Initialize services
m_logService = assertAvailable(testContext, LogService.class);
assertAvailable(testContext, OAuthRequestTokenServlet.class);
assertAvailable(testContext, OAuthAuthorizeTokenServlet.class);
assertAvailable(testContext, OAuthAccessTokenServlet.class);
- assertAvailable(testContext, OAuthServiceConsumerRegistry.class);
+ m_consumerRegistry = assertAvailable(testContext,
OAuthServiceConsumerRegistry.class);
assertAvailable(testContext, HttpService.class);
m_oAuthServiceProvider = assertAvailable(testContext,
OAuthServiceProvider.class);
- //Thread.sleep(30000);
+ // Register an oAuth protected test servlet we need during the tests
+ registerTestResource();
- // First wait for the oAuth servlets to become available
+ // Wait for the oAuth servlets to become available
waitForOAuthServlets();
+ // Execute the tests
+ OAuthSignedRequestsTest test = new OAuthSignedRequestsTest();
+ init(test);
+ test.execute();
+
+ // And we are done
testContext.tearDown();
}
+ private void registerTestResource() {
+ // Now register a test servlet
+ OAuthProtectedTestServlet m_testServlet = new
OAuthProtectedTestServlet();
+ Dictionary<String, String> servletProperties = new Hashtable<String,
String>();
+ servletProperties.put("alias",
OAuthProtectedTestServlet.SERVLET_ALIAS);
+ Component servletComponent = m_dependencyManager.createComponent()
+ .setImplementation(m_testServlet)
+ .setInterface(new String[] { Servlet.class.getName() },
servletProperties)
+
.add(m_dependencyManager.createServiceDependency().setService(LogService.class).setRequired(true))
+
.add(m_dependencyManager.createServiceDependency().setService(OAuthServiceConsumerRegistry.class).setRequired(true))
+
.add(m_dependencyManager.createServiceDependency().setService(OAuthTokenProvider.class).setRequired(true));
+ m_dependencyManager.add(servletComponent);
+ }
+
+ private void init(OAuthTestBase test) {
+ test.setLogService(m_logService);
+ test.setOAuthServiceProvider(m_oAuthServiceProvider);
+ test.setOAuthServiceConsumerRegistry(m_consumerRegistry);
+ }
+
protected void waitForOAuthServlets() throws MalformedURLException,
IOException {
// First wait for the request servlet to become available
m_logService.log(LogService.LOG_DEBUG, "Waiting for '" +
m_oAuthServiceProvider.getRequestTokenURL()
Modified:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthSignedRequestsTest.java
==============================================================================
---
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthSignedRequestsTest.java
(original)
+++
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/OAuthSignedRequestsTest.java
Fri May 13 11:28:00 2011
@@ -15,28 +15,37 @@
*/
package org.amdatu.auth.test.integration.tests;
-import org.junit.Test;
+import static
org.amdatu.auth.test.integration.tests.util.OAuthProtectedTestServlet.OAUTH_TYPE_PARAM;
+import static
org.amdatu.auth.test.integration.tests.util.OAuthProtectedTestServlet.OAUTH_TYPE_SIGNED_REQUEST;
+import static
org.amdatu.auth.test.integration.tests.util.OAuthProtectedTestServlet.SERVLET_ALIAS;
-
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Hashtable;
+
+import junit.framework.Assert;
+import net.oauth.OAuthMessage;
+
+import org.amdatu.auth.test.integration.base.AuthFixture;
+import org.amdatu.auth.test.integration.tests.util.OAuthTestBase;
+import org.amdatu.auth.test.integration.tests.util.OAuthTestConsumer;
+import org.amdatu.authentication.oauth.api.ConsumerNotFoundException;
+import org.amdatu.authentication.oauth.api.ConsumerRegistryStorageException;
+import org.amdatu.authentication.oauth.api.OAuthServiceConsumer;
+import org.amdatu.authentication.oauth.client.OAuthServiceConsumerClient;
+import org.osgi.service.log.LogService;
/**
* Test class for oAuth signed requests.
*
* @author ivol
*/
-//@RunWith(JUnit4TestRunner.class)
-public class OAuthSignedRequestsTest extends OAuthTestBase {
- @Test
- public void testOAuthSignedRequests() throws Exception {
- /*
- // First wait for the oAuth servlets to become available
- waitForOAuthServlets();
-
+public class OAuthSignedRequestsTest extends OAuthTestBase {
+
+ public void execute() throws Exception {
// Step 1: Register a service consumer
m_logService.log(LogService.LOG_DEBUG, "*** Step 1: Register service
consumer ***");
OAuthServiceConsumer consumer = new OAuthTestConsumer();
-
- // FIXME ad-hoc fix. Handle fixture properly (AMDATU-284)
try {
m_consumerRegistry.removeConsumer(consumer);
}
@@ -52,7 +61,7 @@
// Step 3a: Send a simple signed GET request and verify the result
m_logService.log(LogService.LOG_DEBUG, "*** Step 3a: Send simple
signed GET request ***");
- String url = "http://" + ConfigProvider.HOSTNAME + ":" +
ConfigProvider.PORTNR + SERVLET_ALIAS;
+ String url = "http://" + AuthFixture.HOSTNAME + ":" +
AuthFixture.PORTNR + SERVLET_ALIAS;
url += "?" + OAUTH_TYPE_PARAM + "=" + OAUTH_TYPE_SIGNED_REQUEST;
OAuthMessage message = consumerClient.invokeSignedRequest("GET", url,
null);
String body = message.readBodyAsString();
@@ -86,7 +95,6 @@
Assert.assertTrue(body.indexOf("content=" + testSentence) != -1);
// Step 4: Remove the consumer
- m_consumerRegistry.removeConsumer(consumer);
- */
+ m_consumerRegistry.removeConsumer(consumer);
}
}
Added:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/OAuthProtectedTestServlet.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/OAuthProtectedTestServlet.java
Fri May 13 11:28:00 2011
@@ -0,0 +1,168 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.amdatu.auth.test.integration.tests.util;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+import java.io.Reader;
+import java.io.StringWriter;
+import java.io.Writer;
+import java.net.URISyntaxException;
+import java.util.Enumeration;
+
+import javax.servlet.Servlet;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import net.oauth.OAuthAccessor;
+import net.oauth.OAuthConsumer;
+import net.oauth.OAuthException;
+import net.oauth.OAuthMessage;
+import net.oauth.server.OAuthServlet;
+
+import org.amdatu.authentication.oauth.api.OAuthServiceConsumerRegistry;
+import org.amdatu.authentication.oauth.server.OAuthTokenProvider;
+import org.osgi.service.log.LogService;
+
+public class OAuthProtectedTestServlet extends HttpServlet implements Servlet {
+ // The serial version UID of this servlet
+ private static final long serialVersionUID = 8834974378869705614L;
+
+ public static final String SERVLET_ALIAS = "/test/oauth/protectedresource";
+ public static final String OAUTH_TYPE_PARAM = "oauthtype";
+ public static final String OAUTH_TYPE_SIGNED_REQUEST = "signedrequest";
+ public static final String OAUTH_TYPE_TWO_LEGGED = "2legged";
+ public static final String OAUTH_TYPE_THREE_LEGGED = "3legged";
+
+
+ private volatile LogService m_logService;
+ private volatile OAuthTokenProvider m_tokenProvider;
+ private volatile OAuthServiceConsumerRegistry m_consumerRegistry;
+
+ public void doGet(HttpServletRequest request, HttpServletResponse response)
+ throws IOException, ServletException {
+ processRequest("GET", request, response);
+ }
+
+ public void doPost(HttpServletRequest request, HttpServletResponse
response)
+ throws IOException, ServletException {
+ processRequest("POST", request, response);
+ }
+
+ public void processRequest(String method, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
+ try {
+ // Get the oAuth scenario type
+ String oAuthType = request.getParameter(OAUTH_TYPE_PARAM);
+ m_logService.log(LogService.LOG_DEBUG, "Protected resource test
servlet received incoming request, oAuth type=" + oAuthType);
+
+ // Validate oAuth message and get userId from it
+ String body = "";
+ OAuthAccessor accessor = validateOAuth(request);
+ String consumerKey = accessor.consumer.consumerKey;
+ String userId = (String) accessor.getProperty("user");
+ if (oAuthType.equals(OAUTH_TYPE_SIGNED_REQUEST)) {
+ // In case of Signed Requests we return the received
parameters and headers and in case of a POST the body
+ if ("GET".equals(method)) {
+ body = "\ninputparameters:";
+ Enumeration<?> paramIt = request.getParameterNames();
+ while (paramIt.hasMoreElements()) {
+ String name = paramIt.nextElement().toString();
+ body += " " + name + "=" + request.getParameter(name);
+ }
+
+ body += "\nheaders:";
+ Enumeration<?> headerIt = request.getHeaderNames();
+ while (headerIt.hasMoreElements()) {
+ String name = headerIt.nextElement().toString();
+ body += " " + name + "=" + request.getHeader(name);
+ }
+ }
+ else if ("POST".equals(method)) {
+ body = "\nPOST!";
+ body += "\ncontent=" + toString(request.getInputStream());
+ }
+ }
+ else if (oAuthType.equals(OAUTH_TYPE_TWO_LEGGED)) {
+ // Validate if the userid did allow access to access this
protected resource before
+ if
(m_consumerRegistry.hasResourceAccess(m_consumerRegistry.getConsumer(consumerKey),
userId)) {
+ body = "userid=" + userId;
+ } else {
+ body = "access denied";
+ }
+ }
+ else if (oAuthType.equals(OAUTH_TYPE_THREE_LEGGED)) {
+ body = "userid=" + userId;
+ }
+ response.setContentType("text/plain");
+ PrintWriter out = null;
+ try {
+ out = response.getWriter();
+ out.print(body);
+ }
+ finally {
+ out.close();
+ }
+ }
+ catch (Exception e) {
+ throw new ServletException(e);
+ }
+ }
+
+ private OAuthAccessor validateOAuth(HttpServletRequest request) throws
IOException, OAuthException, URISyntaxException {
+ OAuthMessage requestMessage = OAuthServlet.getMessage(request, null);
+
+ // In case of tow-legged or three-legged oAuth, the oAuth message
contains a request or access token
+ // A Signed request however doesn't provide a token, we first verify
the type of oAuth request
+ OAuthAccessor accessor;
+ if (requestMessage.getToken() == null ||
"".equals(requestMessage.getToken())) {
+ // This is a signed request
+ OAuthConsumer consumer =
m_tokenProvider.getConsumer(requestMessage);
+ accessor = new OAuthAccessor(consumer);
+ }
+ else {
+ // This is 2-legged or 3-legged oAuth
+ accessor = m_tokenProvider.getAccessor(requestMessage);
+ }
+ m_tokenProvider.getOAuthValidator().validateMessage(requestMessage,
accessor);
+ return accessor;
+ }
+
+ private String toString(InputStream is) throws IOException {
+ if (is != null) {
+ Writer writer = new StringWriter();
+ char[] buffer = new char[1024];
+ try {
+ Reader reader = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
+ int size;
+ while ((size = reader.read(buffer)) != -1) {
+ writer.write(buffer, 0, size);
+ }
+ }
+ finally {
+ is.close();
+ }
+ return writer.toString();
+ }
+ else {
+ return "";
+ }
+ }
+}
Added:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/OAuthTestBase.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/OAuthTestBase.java
Fri May 13 11:28:00 2011
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.amdatu.auth.test.integration.tests.util;
+
+import org.amdatu.authentication.oauth.api.OAuthServiceConsumerRegistry;
+import org.amdatu.authentication.oauth.api.OAuthServiceProvider;
+import org.osgi.service.log.LogService;
+
+
+public abstract class OAuthTestBase {
+ protected volatile LogService m_logService;
+ protected volatile OAuthServiceProvider m_oAuthServiceProvider;
+ protected volatile OAuthServiceConsumerRegistry m_consumerRegistry;
+
+
+ /*
+ protected volatile ConfigurationAdmin m_configAdmin;
+ protected volatile DependencyManager m_dependencyManager;
+ protected volatile OAuthTokenProvider m_tokenProvider;
+
+ protected volatile UserAdmin m_userAdmin;
+ protected volatile TenantManagementService m_tenantService;*/
+
+ public abstract void execute() throws Exception;
+
+ public void setLogService(LogService logService) {
+ m_logService = logService;
+ }
+
+ public void setOAuthServiceProvider(OAuthServiceProvider provider) {
+ m_oAuthServiceProvider = provider;
+ }
+
+ public void setOAuthServiceConsumerRegistry(OAuthServiceConsumerRegistry
registry) {
+ m_consumerRegistry = registry;
+ }
+
+ /*
+ private HeaderElement m_cookieHeaderElement;
+
+
+
+ protected void initConfiguration() throws IOException {
+ m_configAdmin = getService(ConfigurationAdmin.class);
+
+ // Add cassandra and templates configs
+ ConfigProvider configProvider = new ConfigProvider();
+ configProvider.addTenantConfig(m_configAdmin);
+ configProvider.addFSTenantStoreConfig(m_configAdmin);
+ configProvider.addFelixHttpServiceConfig(m_configAdmin);
+ configProvider.addOAuthConfig(m_configAdmin);
+ configProvider.addLogConfig(m_configAdmin);
+ configProvider.addFSConsumerStoreConfig(m_configAdmin);
+ configProvider.addFSUserAdminConfig(m_configAdmin);
+ }
+
+ protected Component[] getDependencies(DependencyManager manager) {
+ // Now register a test servlet
+ OAuthProtectedTestServlet m_testServlet = new
OAuthProtectedTestServlet();
+ Dictionary<String, String> servletProperties = new Hashtable<String,
String>();
+ servletProperties.put("alias",
OAuthProtectedTestServlet.SERVLET_ALIAS);
+ Component servletComponent = manager.createComponent()
+ .setImplementation(m_testServlet)
+ .setInterface(new String[] { Servlet.class.getName() },
servletProperties)
+
.add(manager.createServiceDependency().setService(LogService.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(OAuthServiceConsumerRegistry.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(OAuthTokenProvider.class).setRequired(true));
+
+ Component testComponent = manager.createComponent()
+ .setImplementation(this)
+
.add(manager.createServiceDependency().setService(OAuthRequestTokenServlet.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(OAuthTokenProvider.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(OAuthServiceProvider.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(ConfigurationAdmin.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(OAuthServiceConsumerRegistry.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(HttpService.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(UserAdmin.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(LoginService.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(StorageProvider.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(TenantManagementService.class).setRequired(true))
+
.add(manager.createServiceDependency().setService(LogService.class).setRequired(true));
+
+ return new Component[] { servletComponent, testComponent };
+ }
+ protected void login() throws HttpException, IOException {
+ m_cookieHeaderElement = Login.login(ConfigProvider.HOSTNAME,
TEST_USERNAME, TEST_PASSWORD);
+ }
+
+ protected Map<String, String> getCookieHeader() {
+ Map<String, String> requestHeaders = new HashMap<String, String>();
+ if (m_cookieHeaderElement != null) {
+ String header = m_cookieHeaderElement.getName() + "=" +
m_cookieHeaderElement.getValue();
+ requestHeaders.put("Cookie", header);
+ }
+ return requestHeaders;
+ }*/
+}
Added:
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/OAuthTestConsumer.java
==============================================================================
--- (empty file)
+++
trunk/amdatu-auth/test-integration/tests/src/test/java/org/amdatu/auth/test/integration/tests/util/OAuthTestConsumer.java
Fri May 13 11:28:00 2011
@@ -0,0 +1,124 @@
+/*
+ * Copyright (c) 2010, 2011 The Amdatu Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.amdatu.auth.test.integration.tests.util;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.amdatu.authentication.oauth.api.OAuthServiceConsumer;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.XML;
+
+public class OAuthTestConsumer implements OAuthServiceConsumer {
+ // Defaults
+ public final static String DEFAULT_CONSUMER_NAME = "Integration Test
Consumer inc.";
+ public final static String DEFAULT_CONSUMER_KEY =
"IntegrationTestConsumerKey";
+ public final static String DEFAULT_CONSUMER_SECRET =
"IntegrationTestConsumerSecret";
+ public final static String DEFAULT_CALLBACK_URL =
"http://localhost/dummy/callback/url";
+
+ // Instance variables
+ private String m_name = DEFAULT_CONSUMER_NAME;
+ private String m_consumerKey = DEFAULT_CONSUMER_KEY;
+ private String m_consumerSecret = DEFAULT_CONSUMER_SECRET;
+ private String m_callbackUrl = DEFAULT_CALLBACK_URL;
+ private Map<String, String> m_properties = new HashMap<String, String>();
+
+ public void setName(String name) {
+ m_name = name;
+ }
+ public String getName() {
+ return m_name;
+ }
+
+ public void setCallbackUrl(String callbackUrl) {
+ m_callbackUrl = callbackUrl;
+ }
+
+ public String getCallbackUrl() {
+ return m_callbackUrl;
+ }
+
+ public void setConsumerKey(String consumerKey) {
+ m_consumerKey = consumerKey;
+ }
+
+ public String getConsumerKey() {
+ return m_consumerKey;
+ }
+
+ public void setConsumerSecret(String consumerSecret) {
+ m_consumerSecret = consumerSecret;
+ }
+
+ public String getConsumerSecret() {
+ return m_consumerSecret;
+ }
+
+ public Map<String, String> getProperties() {
+ return m_properties;
+ }
+
+ public void setProperties(Map<String, String> properties) {
+ m_properties = properties;
+ }
+
+ public JSONObject toJson() throws JSONException {
+ JSONObject consumer = new JSONObject();
+ consumer.put("name", getName());
+ consumer.put("callbackUrl", getCallbackUrl());
+ consumer.put("consumerKey", getConsumerKey());
+ consumer.put("consumerSecret", getConsumerSecret());
+ return new JSONObject().put("consumer", consumer);
+ }
+
+ public String toXML() throws JSONException {
+ JSONObject consumer = toJson();
+ return XML.toString(consumer);
+ }
+
+ public static OAuthTestConsumer fromJson(String json) throws JSONException
{
+ JSONObject jsonObject = new JSONObject(json).getJSONObject("consumer");
+ OAuthTestConsumer consumer = new OAuthTestConsumer();
+ consumer.setName(jsonObject.getString("name"));
+ consumer.setConsumerKey(jsonObject.getString("consumerKey"));
+ consumer.setConsumerSecret(jsonObject.getString("consumerSecret"));
+ consumer.setCallbackUrl(jsonObject.getString("callbackUrl"));
+ return consumer;
+ }
+
+ public static OAuthTestConsumer fromXML(String xml) throws JSONException {
+ return fromJson(XML.toJSONObject(xml).toString());
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (!(obj instanceof OAuthTestConsumer)) {
+ return false;
+ }
+ OAuthTestConsumer consumer = (OAuthTestConsumer) obj;
+ return consumer.getName().equals(getName())
+ && consumer.getConsumerKey().equals(getConsumerKey())
+ && consumer.getConsumerSecret().equals(getConsumerSecret())
+ && consumer.getCallbackUrl().equals(getCallbackUrl());
+ }
+
+ @Override
+ public String toString() {
+ return "Test consumer: name=" + getName() + ", key=" +
getConsumerKey() + ", secret="
+ + getConsumerSecret() + ", callbackurl=" + getCallbackUrl();
+ }
+}
_______________________________________________
Amdatu-commits mailing list
[email protected]
http://lists.amdatu.org/mailman/listinfo/amdatu-commits