From a7f3ecb975f42d92b81dff0b32d4bbec20a23ab8 Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 29 Apr 2010 04:49:51 +0000 Subject: Adding XML wire format binding implementation for REST binding, and adjusting/improving tests for XML and JSON wire formats git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@939194 13f79535-47bb-0310-9956-ffa450edef68 --- .../binding-rest-runtime/META-INF/MANIFEST.MF | 1 + .../trunk/modules/binding-rest-runtime/pom.xml | 6 ++ .../xml/provider/XMLWireFormatInterceptor.java | 73 ++++++++++++++ .../xml/provider/XMLWireFormatProviderFctory.java | 52 ++++++++++ .../provider/XMLWireFormatReferenceProvider.java | 54 ++++++++++ .../xml/provider/XMLWireFormatServiceProvider.java | 110 +++++++++++++++++++++ ....tuscany.sca.provider.WireFormatProviderFactory | 3 +- .../wireformat/json/CatalogServiceTestCase.java | 12 +-- .../wireformat/xml/CustomerServiceTestCase.java | 108 ++++++++++++++++++++ .../src/test/java/services/Catalog.java | 39 -------- .../src/test/java/services/CurrencyConverter.java | 29 ------ .../test/java/services/CurrencyConverterImpl.java | 38 ------- .../src/test/java/services/FruitsCatalogImpl.java | 64 ------------ .../src/test/java/services/Item.java | 50 ---------- .../src/test/java/services/customer/Customer.java | 70 +++++++++++++ .../java/services/customer/CustomerService.java | 41 ++++++++ .../services/customer/CustomerServiceImpl.java | 50 ++++++++++ .../src/test/java/services/store/Catalog.java | 40 ++++++++ .../java/services/store/CurrencyConverter.java | 29 ++++++ .../java/services/store/CurrencyConverterImpl.java | 38 +++++++ .../java/services/store/FruitsCatalogImpl.java | 64 ++++++++++++ .../src/test/java/services/store/Item.java | 50 ++++++++++ .../src/test/resources/customer.composite | 34 +++++++ .../src/test/resources/store.composite | 4 +- 24 files changed, 827 insertions(+), 232 deletions(-) create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatInterceptor.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatProviderFctory.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatReferenceProvider.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatServiceProvider.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverter.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverterImpl.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java delete mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Item.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/Customer.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerService.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerServiceImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/Catalog.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/CurrencyConverter.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/CurrencyConverterImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/FruitsCatalogImpl.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/Item.java create mode 100644 sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/customer.composite (limited to 'sca-java-2.x/trunk/modules/binding-rest-runtime') diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF b/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF index 410d2c9352..874dca6003 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/META-INF/MANIFEST.MF @@ -14,6 +14,7 @@ Import-Package: javax.servlet, org.apache.tuscany.sca.binding.rest;version="2.0.0", org.apache.tuscany.sca.binding.rest.operationselector.jaxrs, org.apache.tuscany.sca.binding.rest.wireformat.json, + org.apache.tuscany.sca.binding.rest.wireformat.xml, org.apache.tuscany.sca.common.http;version="2.0.0", org.apache.tuscany.sca.core;version="2.0.0", org.apache.tuscany.sca.host.http;version="2.0.0", diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml b/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml index 24130e429e..7dd4f380a5 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/pom.xml @@ -66,6 +66,12 @@ 2.0-SNAPSHOT + + org.apache.tuscany.sca + tuscany-databinding-jaxb + 2.0-SNAPSHOT + + org.apache.tuscany.sca tuscany-databinding-json diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatInterceptor.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatInterceptor.java new file mode 100644 index 0000000000..a1b4255c18 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatInterceptor.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.rest.wireformat.xml.provider; + +import java.io.CharArrayReader; +import java.io.CharArrayWriter; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.core.FactoryExtensionPoint; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * JSON wire format Interceptor. + * + * @version $Rev$ $Date$ +*/ +public class XMLWireFormatInterceptor implements Interceptor { + private XMLInputFactory inputFactory; + + private Invoker next; + + public XMLWireFormatInterceptor(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) { + FactoryExtensionPoint factories = extensionPoints.getExtensionPoint(FactoryExtensionPoint.class); + inputFactory = factories.getFactory(XMLInputFactory.class); + } + + public Invoker getNext() { + return next; + } + + public void setNext(Invoker next) { + this.next = next; + } + + public Message invoke(Message msg) { + try { + if(msg.getBody() != null) { + Object[] args = msg.getBody(); + CharArrayWriter data = (CharArrayWriter) args[0]; + XMLStreamReader xmlPayload = inputFactory.createXMLStreamReader(new CharArrayReader(data.toCharArray())); + msg.setBody(new Object[]{xmlPayload}); + } + } catch(Exception e) { + throw new RuntimeException("Unable to process xml paylod: " + msg.getBody().toString()); + } + + return getNext().invoke(msg); + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatProviderFctory.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatProviderFctory.java new file mode 100644 index 0000000000..6e15897b39 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatProviderFctory.java @@ -0,0 +1,52 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.rest.wireformat.xml.provider; + +import org.apache.tuscany.sca.binding.rest.wireformat.xml.XMLWireFormat; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.provider.WireFormatProvider; +import org.apache.tuscany.sca.provider.WireFormatProviderFactory; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +/** + * XML wire format Provider Factory. + * + * @version $Rev$ $Date$ +*/ +public class XMLWireFormatProviderFctory implements WireFormatProviderFactory{ + private ExtensionPointRegistry extensionPoints; + + public XMLWireFormatProviderFctory(ExtensionPointRegistry extensionPoints) { + this.extensionPoints = extensionPoints; + } + public WireFormatProvider createReferenceWireFormatProvider(RuntimeEndpointReference endpointReference) { + return new XMLWireFormatReferenceProvider(extensionPoints, endpointReference); + } + + public WireFormatProvider createServiceWireFormatProvider(RuntimeEndpoint endpoint) { + return new XMLWireFormatServiceProvider(extensionPoints, endpoint); + } + + public Class getModelType() { + return XMLWireFormat.class; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatReferenceProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatReferenceProvider.java new file mode 100644 index 0000000000..c9e0ac4578 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatReferenceProvider.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.rest.wireformat.xml.provider; + +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.provider.WireFormatProvider; +import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; + +/** + * XML wire format Reference Provider. + * + * @version $Rev$ $Date$ +*/ +public class XMLWireFormatReferenceProvider implements WireFormatProvider { + private ExtensionPointRegistry extensionPoints; + private RuntimeEndpointReference endpointReference; + + public XMLWireFormatReferenceProvider(ExtensionPointRegistry extensionPoints,RuntimeEndpointReference endpointReference ) { + this.extensionPoints = extensionPoints; + this.endpointReference = endpointReference; + } + public InterfaceContract configureWireFormatInterfaceContract(InterfaceContract interfaceContract) { + return null; + } + + public Interceptor createInterceptor() { + return null; + } + + public String getPhase() { + return Phase.REFERENCE_BINDING_WIREFORMAT; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatServiceProvider.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatServiceProvider.java new file mode 100644 index 0000000000..508b029b00 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/provider/XMLWireFormatServiceProvider.java @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.rest.wireformat.xml.provider; + +import java.util.List; + +import javax.xml.stream.XMLStreamReader; + +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.binding.rest.wireformat.xml.XMLWireFormat; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.javabeans.SimpleJavaDataBinding; +import org.apache.tuscany.sca.databinding.xml.XMLStringDataBinding; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.Interface; +import org.apache.tuscany.sca.interfacedef.InterfaceContract; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Phase; +import org.apache.tuscany.sca.provider.WireFormatProvider; +import org.apache.tuscany.sca.runtime.RuntimeEndpoint; + +/** + * XML wire format service provider. + * + * @version $Rev$ $Date$ +*/ +public class XMLWireFormatServiceProvider implements WireFormatProvider { + private static final String DATABABINDING = XMLStreamReader.class.getName(); + + private ExtensionPointRegistry extensionPoints; + private RuntimeEndpoint endpoint; + + private InterfaceContract serviceContract; + private Binding binding; + + public XMLWireFormatServiceProvider(ExtensionPointRegistry extensionPoints, RuntimeEndpoint endpoint) { + this.extensionPoints = extensionPoints; + this.endpoint = endpoint; + this.binding = endpoint.getBinding(); + } + + public InterfaceContract configureWireFormatInterfaceContract(InterfaceContract interfaceContract) { + serviceContract = interfaceContract; + + //make XML databinding default + serviceContract.getInterface().resetDataBinding(DATABABINDING); + + //set XML databinding + setDataBinding(serviceContract.getInterface()); + + return serviceContract; + } + + public Interceptor createInterceptor() { + if(binding.getRequestWireFormat() != null && binding.getRequestWireFormat() instanceof XMLWireFormat) { + return new XMLWireFormatInterceptor(extensionPoints, endpoint); + } + return null; + } + + public String getPhase() { + return Phase.SERVICE_BINDING_WIREFORMAT; + } + + + /** + * Utility method to reset data binding for the interface contract + * @param interfaze + */ + @SuppressWarnings({"deprecation", "unchecked"}) + private void setDataBinding(Interface interfaze) { + List operations = interfaze.getOperations(); + for (Operation operation : operations) { + operation.setDataBinding(DATABABINDING); + DataType> inputType = operation.getInputType(); + if (inputType != null) { + List logical = inputType.getLogical(); + for (DataType inArg : logical) { + if (!SimpleJavaDataBinding.NAME.equals(inArg.getDataBinding())) { + inArg.setDataBinding(DATABABINDING); + } + } + } + DataType outputType = operation.getOutputType(); + if (outputType != null) { + if (!SimpleJavaDataBinding.NAME.equals(outputType.getDataBinding())) { + outputType.setDataBinding(XMLStringDataBinding.NAME); + } + } + } + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.WireFormatProviderFactory b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.WireFormatProviderFactory index 7400523fb6..05a99af29b 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.WireFormatProviderFactory +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/main/resources/META-INF/services/org.apache.tuscany.sca.provider.WireFormatProviderFactory @@ -16,4 +16,5 @@ # under the License. # Implementation class for the wire format provider factory -org.apache.tuscany.sca.binding.rest.wireformat.json.provider.JSONWireFormatProviderFctory;model=org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat \ No newline at end of file +org.apache.tuscany.sca.binding.rest.wireformat.json.provider.JSONWireFormatProviderFctory;model=org.apache.tuscany.sca.binding.rest.wireformat.json.JSONWireFormat +org.apache.tuscany.sca.binding.rest.wireformat.xml.provider.XMLWireFormatProviderFctory;model=org.apache.tuscany.sca.binding.rest.wireformat.xml.XMLWireFormat \ No newline at end of file diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java index 80dbcff18b..694170e4f0 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/json/CatalogServiceTestCase.java @@ -31,8 +31,6 @@ import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; -import services.Catalog; - import com.meterware.httpunit.GetMethodWebRequest; import com.meterware.httpunit.PostMethodWebRequest; import com.meterware.httpunit.WebConversation; @@ -42,14 +40,13 @@ import com.meterware.httpunit.WebResponse; public class CatalogServiceTestCase { private static final String SERVICE_URL = "http://localhost:8085/Catalog"; - private static final String GET_RESPONSE = "[{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.Item\"},{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.Item\"}]"; + private static final String GET_RESPONSE = "[{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.store.Item\"},{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.store.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.store.Item\"}]"; private static final String NEW_ITEM = "{\"price\":\"$4.35\",\"name\":\"Grape\"}\""; - private static final String GET_NEW_RESPONSE = "[{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.Item\"},{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.Item\"},{\"price\":\"$4.35\",\"name\":\"Grape\",\"javaClass\":\"services.Item\"}]"; + private static final String GET_NEW_RESPONSE = "[{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.store.Item\"},{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.store.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.store.Item\"},{\"price\":\"$4.35\",\"name\":\"Grape\",\"javaClass\":\"services.store.Item\"}]"; private static final String UPDATED_ITEM = "{\"price\":\"$1.35\",\"name\":\"Grape\"}\""; - private static final String GET_UPDATED_RESPONSE = "[{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.Item\"},{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.Item\"},{\"price\":\"$1.35\",\"name\":\"Grape\",\"javaClass\":\"services.Item\"}]"; + private static final String GET_UPDATED_RESPONSE = "[{\"price\":\"$1.55\",\"name\":\"Pear\",\"javaClass\":\"services.store.Item\"},{\"price\":\"$2.99\",\"name\":\"Apple\",\"javaClass\":\"services.store.Item\"},{\"price\":\"$3.55\",\"name\":\"Orange\",\"javaClass\":\"services.store.Item\"},{\"price\":\"$1.35\",\"name\":\"Grape\",\"javaClass\":\"services.store.Item\"}]"; private static Node node; - private static Catalog catalogService; @BeforeClass public static void init() throws Exception { @@ -57,9 +54,6 @@ public class CatalogServiceTestCase { String contribution = ContributionLocationHelper.getContributionLocation(CatalogServiceTestCase.class); node = NodeFactory.newInstance().createNode("store.composite", new Contribution("catalog", contribution)); node.start(); - - catalogService = node.getService(Catalog.class, "Catalog"); - Assert.assertNotNull(catalogService); } catch (Exception e) { e.printStackTrace(); } diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java new file mode 100644 index 0000000000..ae9442214d --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/org/apache/tuscany/sca/binding/rest/wireformat/xml/CustomerServiceTestCase.java @@ -0,0 +1,108 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.tuscany.sca.binding.rest.wireformat.xml; + +import java.io.ByteArrayInputStream; +import java.net.Socket; + +import org.apache.tuscany.sca.node.Contribution; +import org.apache.tuscany.sca.node.ContributionLocationHelper; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; + +import com.meterware.httpunit.GetMethodWebRequest; +import com.meterware.httpunit.PostMethodWebRequest; +import com.meterware.httpunit.WebConversation; +import com.meterware.httpunit.WebRequest; +import com.meterware.httpunit.WebResponse; + +public class CustomerServiceTestCase { + private static final String SERVICE_URL = "http://localhost:8085/Customer"; + + private static final String GET_RESPONSE = "john@domain.comJohnJohn"; + private static final String UPDATED_ITEM = "john@updated-domain.comJohnJohn"; + private static final String GET_UPDATED_RESPONSE = "john@updated-domain.comJohnJohn"; + + private static Node node; + + @BeforeClass + public static void init() throws Exception { + try { + String contribution = ContributionLocationHelper.getContributionLocation(CustomerServiceTestCase.class); + node = NodeFactory.newInstance().createNode("customer.composite", new Contribution("customer", contribution)); + node.start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @AfterClass + public static void destroy() throws Exception { + if(node != null) { + node.stop(); + } + } + + @Test + public void testPing() throws Exception { + new Socket("127.0.0.1", 8085); + //System.in.read(); + } + + @Test + public void testGetInvocation() throws Exception { + WebConversation wc = new WebConversation(); + WebRequest request = new GetMethodWebRequest(SERVICE_URL); + WebResponse response = wc.getResource(request); + + //for debug purposes + //System.out.println(">>>" + GET_RESPONSE); + //System.out.println(">>>" + response.getText()); + + Assert.assertEquals(200, response.getResponseCode()); + Assert.assertEquals(GET_RESPONSE, response.getText()); + } + + + @Test + public void testPutInvocation() throws Exception { + //Add new item to catalog + WebConversation wc = new WebConversation(); + WebRequest request = new PostMethodWebRequest(SERVICE_URL, new ByteArrayInputStream(UPDATED_ITEM.getBytes("UTF-8")),"application/json"); + WebResponse response = wc.getResource(request); + + Assert.assertEquals(200, response.getResponseCode()); + + //read new results and expect to get new item back in the response + request = new GetMethodWebRequest(SERVICE_URL); + response = wc.getResource(request); + + //for debug purposes + //System.out.println(">>>" + GET_UPDATED_RESPONSE); + //System.out.println(">>>" + response.getText()); + + Assert.assertEquals(200, response.getResponseCode()); + Assert.assertEquals(GET_UPDATED_RESPONSE, response.getText()); + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java deleted file mode 100644 index 0c880b499e..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Catalog.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 services; - -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; - -import org.oasisopen.sca.annotation.Remotable; - -@Remotable -public interface Catalog { - - @GET - Item[] get(); - - @POST - void addItem(Item item); - - @PUT - void updateItem(Item item); -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverter.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverter.java deleted file mode 100644 index a064f3dd69..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverter.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 services; - -import org.oasisopen.sca.annotation.Remotable; - -@Remotable -public interface CurrencyConverter { - public double getConversion(String fromCurrenycCode, String toCurrencyCode, double amount); - - public String getCurrencySymbol(String currencyCode); -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverterImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverterImpl.java deleted file mode 100644 index c354aed447..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/CurrencyConverterImpl.java +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 services; - -public class CurrencyConverterImpl implements CurrencyConverter { - public double getConversion(String fromCurrencyCode, String toCurrencyCode, double amount) { - if (toCurrencyCode.equals("USD")) - return amount; - else if (toCurrencyCode.equals("EUR")) - return ((double)Math.round(amount * 0.7256 * 100)) /100; - return 0; - } - - public String getCurrencySymbol(String currencyCode) { - if (currencyCode.equals("USD")) - return "$"; - else if (currencyCode.equals("EUR")) - return "E"; //"€"; - return "?"; - } -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java deleted file mode 100644 index b97db14149..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/FruitsCatalogImpl.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 services; - -import java.util.HashMap; -import java.util.Map; - -import org.oasisopen.sca.annotation.Init; -import org.oasisopen.sca.annotation.Property; -import org.oasisopen.sca.annotation.Reference; -import org.oasisopen.sca.annotation.Scope; - -@Scope("COMPOSITE") -public class FruitsCatalogImpl implements Catalog { - - @Property - public String currencyCode = "USD"; - - @Reference - public CurrencyConverter currencyConverter; - - private Map catalog = new HashMap(); - - @Init - public void init() { - String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode); - catalog.put("Apple", new Item("Apple", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99))); - catalog.put("Orange", new Item("Orange", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55))); - catalog.put("Pear", new Item("Pear", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55))); - } - - public Item[] get() { - Item[] catalogArray = new Item[catalog.size()]; - catalog.values().toArray(catalogArray); - return catalogArray; - } - - public void addItem(Item item) { - catalog.put(item.getName(),item); - } - - public void updateItem(Item item) { - if(catalog.get(item.getName()) != null) { - catalog.put(item.getName(), item); - } - } -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Item.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Item.java deleted file mode 100644 index fe32cfc828..0000000000 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/Item.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you 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 services; - -public class Item { - private String name; - private String price; - - public Item() { - } - - public Item(String name, String price) { - this.name = name; - this.price = price; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getPrice() { - return price; - } - - public void setPrice(String price) { - this.price = price; - } - -} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/Customer.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/Customer.java new file mode 100644 index 0000000000..08506ebd48 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/Customer.java @@ -0,0 +1,70 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 services.customer; + + +/** + * Customer data + */ +public class Customer { + private String id; + private String email; + private String name; + + public Customer() { + super(); + } + + public Customer(String id, String name, String email) { + super(); + this.id = id; + this.email = email; + this.name = name; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getEmail() { + return email; + } + + public void setEmail(String email) { + this.email = email; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String toString() { + return "id: " + id + " name: " + name + " e-mail: " + email; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerService.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerService.java new file mode 100644 index 0000000000..9d640376e7 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerService.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 services.customer; + +import javax.jws.WebResult; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface CustomerService { + + @GET + @WebResult(name = "Customer", targetNamespace = "") + Customer get(); + + @POST + void addCustomer(Customer customer); + + @PUT + void updateCustomer(Customer customer); +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerServiceImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerServiceImpl.java new file mode 100644 index 0000000000..70c718a656 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/customer/CustomerServiceImpl.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 services.customer; + +import java.util.HashMap; +import java.util.Map; + +import org.oasisopen.sca.annotation.Init; +import org.oasisopen.sca.annotation.Scope; + +@Scope("COMPOSITE") +public class CustomerServiceImpl implements CustomerService { + private Map customers = new HashMap(); + + @Init + public void init() { + customers.put("John", new Customer("John", "John", "john@domain.com")); + } + + public Customer get() { + return customers.values().iterator().next(); + } + + public void addCustomer(Customer customer) { + customers.put(customer.getName(), customer); + } + + public void updateCustomer(Customer customer) { + if(customers.get(customer.getName()) != null) { + customers.put(customer.getName(), customer); + } + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/Catalog.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/Catalog.java new file mode 100644 index 0000000000..f30176f70a --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/Catalog.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 services.store; + +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; + +import org.oasisopen.sca.annotation.Remotable; + + +@Remotable +public interface Catalog { + + @GET + Item[] get(); + + @POST + void addItem(Item item); + + @PUT + void updateItem(Item item); +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/CurrencyConverter.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/CurrencyConverter.java new file mode 100644 index 0000000000..45f8949633 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/CurrencyConverter.java @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 services.store; + +import org.oasisopen.sca.annotation.Remotable; + +@Remotable +public interface CurrencyConverter { + public double getConversion(String fromCurrenycCode, String toCurrencyCode, double amount); + + public String getCurrencySymbol(String currencyCode); +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/CurrencyConverterImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/CurrencyConverterImpl.java new file mode 100644 index 0000000000..9956e207e1 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/CurrencyConverterImpl.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 services.store; + +public class CurrencyConverterImpl implements CurrencyConverter { + public double getConversion(String fromCurrencyCode, String toCurrencyCode, double amount) { + if (toCurrencyCode.equals("USD")) + return amount; + else if (toCurrencyCode.equals("EUR")) + return ((double)Math.round(amount * 0.7256 * 100)) /100; + return 0; + } + + public String getCurrencySymbol(String currencyCode) { + if (currencyCode.equals("USD")) + return "$"; + else if (currencyCode.equals("EUR")) + return "E"; //"€"; + return "?"; + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/FruitsCatalogImpl.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/FruitsCatalogImpl.java new file mode 100644 index 0000000000..b71bb28596 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/FruitsCatalogImpl.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 services.store; + +import java.util.HashMap; +import java.util.Map; + +import org.oasisopen.sca.annotation.Init; +import org.oasisopen.sca.annotation.Property; +import org.oasisopen.sca.annotation.Reference; +import org.oasisopen.sca.annotation.Scope; + +@Scope("COMPOSITE") +public class FruitsCatalogImpl implements Catalog { + + @Property + public String currencyCode = "USD"; + + @Reference + public CurrencyConverter currencyConverter; + + private Map catalog = new HashMap(); + + @Init + public void init() { + String currencySymbol = currencyConverter.getCurrencySymbol(currencyCode); + catalog.put("Apple", new Item("Apple", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 2.99))); + catalog.put("Orange", new Item("Orange", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 3.55))); + catalog.put("Pear", new Item("Pear", currencySymbol + currencyConverter.getConversion("USD", currencyCode, 1.55))); + } + + public Item[] get() { + Item[] catalogArray = new Item[catalog.size()]; + catalog.values().toArray(catalogArray); + return catalogArray; + } + + public void addItem(Item item) { + catalog.put(item.getName(),item); + } + + public void updateItem(Item item) { + if(catalog.get(item.getName()) != null) { + catalog.put(item.getName(), item); + } + } +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/Item.java b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/Item.java new file mode 100644 index 0000000000..d5a298eee5 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/java/services/store/Item.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 services.store; + +public class Item { + private String name; + private String price; + + public Item() { + } + + public Item(String name, String price) { + this.name = name; + this.price = price; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getPrice() { + return price; + } + + public void setPrice(String price) { + this.price = price; + } + +} diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/customer.composite b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/customer.composite new file mode 100644 index 0000000000..eb3f361571 --- /dev/null +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/customer.composite @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + diff --git a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/store.composite b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/store.composite index c10c1f3089..7a87929985 100644 --- a/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/store.composite +++ b/sca-java-2.x/trunk/modules/binding-rest-runtime/src/test/resources/store.composite @@ -23,7 +23,7 @@ name="store"> - + USD @@ -35,7 +35,7 @@ - + -- cgit v1.2.3