From 7098cc307870e4e76e9b8de447711faaf6c2bb2d Mon Sep 17 00:00:00 2001 From: Will Norris Date: Tue, 27 Oct 2009 19:06:54 +0000 Subject: [PATCH] finish implementations of AX store request and response message git-svn-id: file:///Users/willnorris/Projects/svn-import/java-openid/trunk@85 eae3f9c0-6542-46b9-8785-326aab784c2f --- ...ributeExchangeUnmarshallingDispatcher.java | 2 +- .../openid/extensions/ax/StoreResponse.java | 15 ++++ .../extensions/ax/impl/StoreRequestImpl.java | 4 +- .../ax/impl/StoreRequestMarshaller.java | 43 ++++++----- .../ax/impl/StoreRequestUnmarshaller.java | 44 +++++++++++ .../extensions/ax/impl/StoreResponseImpl.java | 12 +-- .../ax/impl/StoreResponseMarshaller.java | 5 +- .../ax/impl/StoreResponseUnmarshaller.java | 8 +- .../extensions/ax/FetchResponseTest.java | 3 - .../extensions/ax/StoreRequestTest.java | 74 +++++++++++++++++++ .../ax/StoreResponseFailureTest.java | 74 +++++++++++++++++++ .../ax/StoreResponseSuccessTest.java | 64 ++++++++++++++++ .../openid/extensions/ax/StoreRequest.txt | 8 ++ .../extensions/ax/StoreResponseFailure.txt | 3 + .../extensions/ax/StoreResponseSuccess.txt | 2 + 15 files changed, 324 insertions(+), 37 deletions(-) create mode 100644 src/test/java/edu/internet2/middleware/openid/extensions/ax/StoreRequestTest.java create mode 100644 src/test/java/edu/internet2/middleware/openid/extensions/ax/StoreResponseFailureTest.java create mode 100644 src/test/java/edu/internet2/middleware/openid/extensions/ax/StoreResponseSuccessTest.java create mode 100644 src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreRequest.txt create mode 100644 src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseFailure.txt create mode 100644 src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseSuccess.txt diff --git a/src/main/java/edu/internet2/middleware/openid/extensions/ax/AttributeExchangeUnmarshallingDispatcher.java b/src/main/java/edu/internet2/middleware/openid/extensions/ax/AttributeExchangeUnmarshallingDispatcher.java index 17f7b51..caff029 100644 --- a/src/main/java/edu/internet2/middleware/openid/extensions/ax/AttributeExchangeUnmarshallingDispatcher.java +++ b/src/main/java/edu/internet2/middleware/openid/extensions/ax/AttributeExchangeUnmarshallingDispatcher.java @@ -83,7 +83,7 @@ public AttributeExchangeMessage unmarshall(ParameterMap parameters) throws Unmar unmarshaller = unmarshallers.get(StoreRequest.class); } else if (mode.equals(StoreResponse.MODE_SUCCESS) || mode.equals(StoreResponse.MODE_FAILURE)) { builder = builders.getBuilder(StoreResponse.class); - unmarshaller = unmarshallers.get(FetchRequest.class); + unmarshaller = unmarshallers.get(StoreResponse.class); } if (builder == null) { diff --git a/src/main/java/edu/internet2/middleware/openid/extensions/ax/StoreResponse.java b/src/main/java/edu/internet2/middleware/openid/extensions/ax/StoreResponse.java index f911a07..06d8a9f 100644 --- a/src/main/java/edu/internet2/middleware/openid/extensions/ax/StoreResponse.java +++ b/src/main/java/edu/internet2/middleware/openid/extensions/ax/StoreResponse.java @@ -31,6 +31,20 @@ public interface StoreResponse extends AttributeExchangeMessage { */ public static final String MODE_FAILURE = "store_response_failure"; + /** + * Get whether this response indicates success. + * + * @return if success + */ + public boolean getSuccess(); + + /** + * Set whether this response indicates success. + * + * @param newSuccess if success + */ + public void setSuccess(boolean newSuccess); + /** * A human-readable message indicating why the store request failed. * @@ -44,4 +58,5 @@ public interface StoreResponse extends AttributeExchangeMessage { * @param newError the error message */ public void setError(String newError); + } \ No newline at end of file diff --git a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestImpl.java b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestImpl.java index 6465c31..5aa5052 100644 --- a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestImpl.java +++ b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestImpl.java @@ -16,7 +16,7 @@ package edu.internet2.middleware.openid.extensions.ax.impl; -import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @@ -36,7 +36,7 @@ public class StoreRequestImpl extends BaseAttributeExchangeMessage implements St * Constructor. */ public StoreRequestImpl() { - attributes = new HashMap>(); + attributes = new LinkedHashMap>(); } /** {@inheritDoc} */ diff --git a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestMarshaller.java b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestMarshaller.java index 30f8941..89589b7 100644 --- a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestMarshaller.java +++ b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestMarshaller.java @@ -19,10 +19,14 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.QName; + +import edu.internet2.middleware.openid.common.NamespaceMap; import edu.internet2.middleware.openid.common.ParameterMap; import edu.internet2.middleware.openid.extensions.ax.AttributeExchange; import edu.internet2.middleware.openid.extensions.ax.AttributeExchangeMarshaller; import edu.internet2.middleware.openid.extensions.ax.StoreRequest; +import edu.internet2.middleware.openid.extensions.ax.AttributeExchange.Parameter; /** * StoreRequestMarshaller. @@ -31,34 +35,37 @@ public class StoreRequestMarshaller implements AttributeExchangeMarshaller> attributes = request.getAttributes(); - for (String name : attributes.keySet()) { - List values = attributes.get(name); - if (values.size() <= 0) { - continue; - } + for (String typeURI : attributes.keySet()) { + List values = attributes.get(typeURI); + String alias = types.add(typeURI); - aliasName = AttributeExchange.ALIAS_PREFIX + (++aliasCount); - valueCount = 0; + QName typeQName = new QName(AttributeExchange.AX_10_NS, Parameter.type.toString() + "." + alias); + parameters.put(typeQName, typeURI); - // add type parameter - // parameters.put(Parameter.type.toString() + "." + aliasName, name); - - if (values.size() == 1) { - // parameters.put(Parameter.value.toString() + "." + aliasName, values.get(0)); + if (values.isEmpty()) { + continue; + } else if (values.size() == 1) { + QName valueQName = new QName(AttributeExchange.AX_10_NS, Parameter.value.toString() + "." + alias); + parameters.put(valueQName, values.get(0)); } else { - // parameters.put(Parameter.count.toString() + "." + aliasName, values.size() + ""); - for (String value : values) { - // parameters.put(Parameter.value.toString() + "." + aliasName + "." + (++valueCount), value); + QName countQName = new QName(AttributeExchange.AX_10_NS, Parameter.count.toString() + "." + alias); + parameters.put(countQName, Integer.toString(values.size())); + + for (int i = 0; i < values.size(); i++) { + QName valueQName = new QName(AttributeExchange.AX_10_NS, Parameter.value.toString() + "." + alias + + "." + (i + 1)); + parameters.put(valueQName, values.get(i)); } } } + } } \ No newline at end of file diff --git a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestUnmarshaller.java b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestUnmarshaller.java index 1a8682b..8fa3068 100644 --- a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestUnmarshaller.java +++ b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreRequestUnmarshaller.java @@ -16,9 +16,17 @@ package edu.internet2.middleware.openid.extensions.ax.impl; +import java.util.ArrayList; +import java.util.List; + +import javax.xml.namespace.QName; + +import edu.internet2.middleware.openid.common.NamespaceMap; import edu.internet2.middleware.openid.common.ParameterMap; +import edu.internet2.middleware.openid.extensions.ax.AttributeExchange; import edu.internet2.middleware.openid.extensions.ax.AttributeExchangeUnmarshaller; import edu.internet2.middleware.openid.extensions.ax.StoreRequest; +import edu.internet2.middleware.openid.extensions.ax.AttributeExchange.Parameter; /** * StoreRequestUnmarshaller. @@ -27,6 +35,42 @@ public class StoreRequestUnmarshaller implements AttributeExchangeUnmarshaller values = new ArrayList(); + int count = 1; + + QName countQName = new QName(AttributeExchange.AX_10_NS, Parameter.count.toString() + "." + alias); + String countString = parameters.get(countQName); + if (countString != null) { + count = Integer.parseInt(countString); + } + + if (count == 1) { + QName valueQName = new QName(AttributeExchange.AX_10_NS, Parameter.value.toString() + "." + alias); + values.add(parameters.get(valueQName)); + } else { + for (int i = 0; i < count; i++) { + QName valueQName = new QName(AttributeExchange.AX_10_NS, Parameter.value.toString() + "." + alias + + "." + (i + 1)); + values.add(parameters.get(valueQName)); + } + } + + request.getAttributes().put(typeURI, values); + } + } } \ No newline at end of file diff --git a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreResponseImpl.java b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreResponseImpl.java index cf1b7f2..280a6e1 100644 --- a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreResponseImpl.java +++ b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreResponseImpl.java @@ -56,20 +56,12 @@ public void setError(String newError) { error = newError; } - /** - * Get whether this response indicates success. - * - * @return if success - */ + /** {@inheritDoc} */ public boolean getSuccess() { return success; } - /** - * Set whether this response indicates success. - * - * @param newSuccess if success - */ + /** {@inheritDoc} */ public void setSuccess(boolean newSuccess) { success = newSuccess; } diff --git a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreResponseMarshaller.java b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreResponseMarshaller.java index f8b51aa..1f29b73 100644 --- a/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreResponseMarshaller.java +++ b/src/main/java/edu/internet2/middleware/openid/extensions/ax/impl/StoreResponseMarshaller.java @@ -19,6 +19,7 @@ import edu.internet2.middleware.openid.common.ParameterMap; import edu.internet2.middleware.openid.extensions.ax.AttributeExchangeMarshaller; import edu.internet2.middleware.openid.extensions.ax.StoreResponse; +import edu.internet2.middleware.openid.extensions.ax.AttributeExchange.Parameter; /** * StoreResponseMarshaller. @@ -27,8 +28,8 @@ public class StoreResponseMarshaller implements AttributeExchangeMarshaller> expectedAttributes; + + /** + * Constructor. + * + * @throws MalformedURLException if URL is malformed + */ + public StoreRequestTest() throws MalformedURLException { + messageFile = "/data/edu/internet2/middleware/openid/extensions/ax/StoreRequest.txt"; + + expectedMode = StoreRequest.MODE; + expectedAttributes = new LinkedHashMap>(); + expectedAttributes.put("http://example.com/schema/fullname", Arrays.asList(new String[] { "Bob Smith" })); + expectedAttributes.put("http://example.com/schema/favourite_movie", Arrays.asList(new String[] { "Movie1", + "Movie2", })); + } + + /** {@inheritDoc} */ + public void testMessageMarshall() { + StoreRequest request = (StoreRequest) buildMessageExtension(StoreRequest.class); + + request.getAttributes().putAll(expectedAttributes); + + assertEquals(expectedParameters, request); + } + + /** {@inheritDoc} */ + public void testMessageUnmarshall() { + StoreRequest request = (StoreRequest) unmarshallMessageExtension(messageFile); + + String mode = request.getMode(); + assertEquals("StoreRequest mode was " + mode + ", expected " + expectedMode, expectedMode, mode); + + Map> attributes = request.getAttributes(); + assertEquals("StoreRequest attributes were " + attributes + ", expected " + expectedAttributes, + expectedAttributes, attributes); + } + +} \ No newline at end of file diff --git a/src/test/java/edu/internet2/middleware/openid/extensions/ax/StoreResponseFailureTest.java b/src/test/java/edu/internet2/middleware/openid/extensions/ax/StoreResponseFailureTest.java new file mode 100644 index 0000000..5f4f397 --- /dev/null +++ b/src/test/java/edu/internet2/middleware/openid/extensions/ax/StoreResponseFailureTest.java @@ -0,0 +1,74 @@ +/* + * Copyright 2009 University Corporation for Advanced Internet Development, Inc. + * + * 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 edu.internet2.middleware.openid.extensions.ax; + +import java.net.MalformedURLException; + +import edu.internet2.middleware.openid.extensions.BaseMessageExtensionTestCase; + +/** + * Test case for creating, marshalling, and unmarshalling {@link FetchRequest}. + */ +public class StoreResponseFailureTest extends BaseMessageExtensionTestCase { + + /** Expected mode. */ + private String expectedMode; + + /** Expected error. */ + private String expectedError; + + /** Expected success. */ + private boolean expectedSuccess; + + /** + * Constructor. + * + * @throws MalformedURLException if URL is malformed + */ + public StoreResponseFailureTest() throws MalformedURLException { + messageFile = "/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseFailure.txt"; + + expectedMode = StoreResponse.MODE_FAILURE; + expectedSuccess = false; + expectedError = "General storage failure"; + } + + /** {@inheritDoc} */ + public void testMessageMarshall() { + StoreResponse response = (StoreResponse) buildMessageExtension(StoreResponse.class); + + response.setSuccess(expectedSuccess); + response.setError(expectedError); + + assertEquals(expectedParameters, response); + } + + /** {@inheritDoc} */ + public void testMessageUnmarshall() { + StoreResponse response = (StoreResponse) unmarshallMessageExtension(messageFile); + + String mode = response.getMode(); + assertEquals("StoreResponse mode was " + mode + ", expected " + expectedMode, expectedMode, mode); + + boolean success = response.getSuccess(); + assertEquals("StoreResponse success was " + success + ", expected " + expectedSuccess, expectedSuccess, success); + + String error = response.getError(); + assertEquals("StoreResponse error was " + error + ", expected " + expectedError, expectedError, error); + } + +} \ No newline at end of file diff --git a/src/test/java/edu/internet2/middleware/openid/extensions/ax/StoreResponseSuccessTest.java b/src/test/java/edu/internet2/middleware/openid/extensions/ax/StoreResponseSuccessTest.java new file mode 100644 index 0000000..5edb98d --- /dev/null +++ b/src/test/java/edu/internet2/middleware/openid/extensions/ax/StoreResponseSuccessTest.java @@ -0,0 +1,64 @@ +/* + * Copyright 2009 University Corporation for Advanced Internet Development, Inc. + * + * 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 edu.internet2.middleware.openid.extensions.ax; + +import java.net.MalformedURLException; + +import edu.internet2.middleware.openid.extensions.BaseMessageExtensionTestCase; + +/** + * Test case for creating, marshalling, and unmarshalling {@link FetchRequest}. + */ +public class StoreResponseSuccessTest extends BaseMessageExtensionTestCase { + + /** Expected mode. */ + private String expectedMode; + + /** Expected success. */ + private boolean expectedSuccess; + + /** + * Constructor. + * + * @throws MalformedURLException if URL is malformed + */ + public StoreResponseSuccessTest() throws MalformedURLException { + messageFile = "/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseSuccess.txt"; + + expectedMode = StoreResponse.MODE_SUCCESS; + expectedSuccess = true; + } + + /** {@inheritDoc} */ + public void testMessageMarshall() { + StoreResponse response = (StoreResponse) buildMessageExtension(StoreResponse.class); + response.setSuccess(expectedSuccess); + assertEquals(expectedParameters, response); + } + + /** {@inheritDoc} */ + public void testMessageUnmarshall() { + StoreResponse response = (StoreResponse) unmarshallMessageExtension(messageFile); + + String mode = response.getMode(); + assertEquals("StoreResponse mode was " + mode + ", expected " + expectedMode, expectedMode, mode); + + boolean success = response.getSuccess(); + assertEquals("StoreResponse success was " + success + ", expected " + expectedSuccess, expectedSuccess, success); + } + +} \ No newline at end of file diff --git a/src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreRequest.txt b/src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreRequest.txt new file mode 100644 index 0000000..1c0c2ef --- /dev/null +++ b/src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreRequest.txt @@ -0,0 +1,8 @@ +ns.ax:http://openid.net/srv/ax/1.0 +ax.mode:store_request +ax.type.a0:http://example.com/schema/fullname +ax.value.a0:Bob Smith +ax.type.a1:http://example.com/schema/favourite_movie +ax.count.a1:2 +ax.value.a1.1:Movie1 +ax.value.a1.2:Movie2 diff --git a/src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseFailure.txt b/src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseFailure.txt new file mode 100644 index 0000000..9cad97f --- /dev/null +++ b/src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseFailure.txt @@ -0,0 +1,3 @@ +ns.ax:http://openid.net/srv/ax/1.0 +ax.mode:store_response_failure +ax.error:General storage failure diff --git a/src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseSuccess.txt b/src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseSuccess.txt new file mode 100644 index 0000000..40f041d --- /dev/null +++ b/src/test/resources/data/edu/internet2/middleware/openid/extensions/ax/StoreResponseSuccess.txt @@ -0,0 +1,2 @@ +ns.ax:http://openid.net/srv/ax/1.0 +ax.mode:store_response_success