[OLINGO-200] More ref tests

This commit is contained in:
Francesco Chicchiriccò 2014-04-01 16:04:24 +02:00
parent 525767c0c9
commit 6aaec814c4
12 changed files with 195 additions and 46 deletions

View File

@ -25,9 +25,9 @@ import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import org.apache.olingo.client.api.v4.ODataClient;
import org.apache.olingo.commons.api.domain.CommonODataEntitySet;
import org.apache.olingo.commons.api.format.ODataPubFormat;
import org.apache.olingo.client.core.AbstractTest;
import org.apache.olingo.commons.api.domain.v4.ODataEntity;
import org.apache.olingo.commons.api.domain.v4.ODataEntitySet;
import org.apache.olingo.commons.core.op.ResourceFactory;
import org.junit.Test;
@ -48,8 +48,8 @@ public class EntitySetTest extends AbstractTest {
assertEquals(2, entitySet.getEntities().size());
assertNull(entitySet.getNext());
final CommonODataEntitySet written = getClient().getBinder().getODataEntitySet(getClient().
getBinder().getFeed(entitySet, ResourceFactory.feedClassForFormat(format == ODataPubFormat.ATOM)));
final ODataEntitySet written = getClient().getBinder().getODataEntitySet(getClient().getBinder().
getFeed(entitySet, ResourceFactory.feedClassForFormat(format == ODataPubFormat.ATOM)));
assertEquals(entitySet, written);
}
@ -62,4 +62,30 @@ public class EntitySetTest extends AbstractTest {
public void fromJSON() throws IOException {
read(ODataPubFormat.JSON);
}
private void ref(final ODataPubFormat format) {
final InputStream input = getClass().getResourceAsStream("collectionOfEntityReferences." + getSuffix(format));
final ODataEntitySet entitySet = getClient().getBinder().getODataEntitySet(
getClient().getDeserializer().toFeed(input, format).getObject());
assertNotNull(entitySet);
for (ODataEntity entity : entitySet.getEntities()) {
assertNotNull(entity.getReference());
}
entitySet.setCount(entitySet.getEntities().size());
final ODataEntitySet written = getClient().getBinder().getODataEntitySet(getClient().getBinder().
getFeed(entitySet, ResourceFactory.feedClassForFormat(format == ODataPubFormat.ATOM)));
assertEquals(entitySet, written);
}
@Test
public void atomRef() {
ref(ODataPubFormat.ATOM);
}
@Test
public void jsonRef() {
ref(ODataPubFormat.JSON);
}
}

View File

@ -224,4 +224,27 @@ public class EntityTest extends AbstractTest {
public void jsonWithStream() {
withStream(ODataPubFormat.JSON_FULL_METADATA);
}
private void ref(final ODataPubFormat format) {
final InputStream input = getClass().getResourceAsStream("entityReference." + getSuffix(format));
final ODataEntity entity = getClient().getBinder().getODataEntity(
getClient().getDeserializer().toEntry(input, format).getObject());
assertNotNull(entity);
assertNotNull(entity.getReference());
final ODataEntity written = getClient().getBinder().getODataEntity(getClient().getBinder().
getEntry(entity, ResourceFactory.entryClassForFormat(format == ODataPubFormat.ATOM)));
assertEquals(entity, written);
}
@Test
public void atomRef() {
ref(ODataPubFormat.ATOM);
}
@Test
public void jsonRef() {
ref(ODataPubFormat.JSON);
}
}

View File

@ -115,6 +115,20 @@ public class JSONTest extends AbstractTest {
assertEquals(orig, OBJECT_MAPPER.readTree(new ByteArrayInputStream(actual.getBytes())));
}
protected void feed(final String filename, final ODataPubFormat format) throws Exception {
final StringWriter writer = new StringWriter();
getClient().getSerializer().feed(getClient().getDeserializer().toFeed(
getClass().getResourceAsStream(filename + "." + getSuffix(format)), format).getObject(), writer);
assertSimilar(filename + "." + getSuffix(format), writer.toString());
}
@Test
public void feeds() throws Exception {
feed("Customers", getODataPubFormat());
feed("collectionOfEntityReferences", getODataPubFormat());
}
protected void entry(final String filename, final ODataPubFormat format) throws Exception {
final StringWriter writer = new StringWriter();
getClient().getSerializer().entry(getClient().getDeserializer().toEntry(
@ -137,6 +151,8 @@ public class JSONTest extends AbstractTest {
public void entries() throws Exception {
entry("Products_5", getODataPubFormat());
entry("VipCustomer", getODataPubFormat());
entry("Advertisements_f89dee73-af9f-4cd4-b330-db93c25ff3c7", getODataPubFormat());
entry("entityReference", getODataPubFormat());
}
protected void property(final String filename, final ODataFormat format) throws Exception {
@ -150,5 +166,8 @@ public class JSONTest extends AbstractTest {
@Test
public void properties() throws Exception {
property("Products_5_SkinColor", getODataFormat());
property("Products_5_CoverColors", getODataFormat());
property("Employees_3_HomeAddress", getODataFormat());
property("Employees_3_HomeAddress", getODataFormat());
}
}

View File

@ -28,26 +28,26 @@
m:context="http://odatae2etest.azurewebsites.net/javatest/DefaultService/$metadata#Customers">
<m:count>2</m:count>
<id>http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers</id>
<title />
<title/>
<updated>2014-03-31T09:35:14Z</updated>
<entry>
<id>http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)</id>
<category term="#Microsoft.Test.OData.Services.ODataWCFService.Customer" scheme="http://docs.oasis-open.org/odata/ns/scheme" />
<link rel="edit" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)" />
<link rel="http://docs.oasis-open.org/odata/ns/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)/Parent" />
<link rel="http://docs.oasis-open.org/odata/ns/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)/Orders" />
<link rel="http://docs.oasis-open.org/odata/ns/related/Company" type="application/atom+xml;type=entry" title="Company" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)/Company" />
<title />
<category term="#Microsoft.Test.OData.Services.ODataWCFService.Customer" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<link rel="edit" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)"/>
<link rel="http://docs.oasis-open.org/odata/ns/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)/Parent"/>
<link rel="http://docs.oasis-open.org/odata/ns/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)/Orders"/>
<link rel="http://docs.oasis-open.org/odata/ns/related/Company" type="application/atom+xml;type=entry" title="Company" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=1)/Company"/>
<title/>
<updated>2014-03-31T09:35:14Z</updated>
<author>
<name />
<name/>
</author>
<content type="application/xml">
<m:properties>
<d:PersonID m:type="Int32">1</d:PersonID>
<d:FirstName>Bob</d:FirstName>
<d:LastName>Cat</d:LastName>
<d:MiddleName m:null="true" />
<d:MiddleName m:null="true"/>
<d:HomeAddress m:type="#Microsoft.Test.OData.Services.ODataWCFService.HomeAddress">
<d:Street>1 Microsoft Way</d:Street>
<d:City>London</d:City>
@ -73,30 +73,30 @@
</entry>
<entry>
<id>http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)</id>
<category term="#Microsoft.Test.OData.Services.ODataWCFService.Customer" scheme="http://docs.oasis-open.org/odata/ns/scheme" />
<link rel="edit" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)" />
<link rel="http://docs.oasis-open.org/odata/ns/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)/Parent" />
<link rel="http://docs.oasis-open.org/odata/ns/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)/Orders" />
<link rel="http://docs.oasis-open.org/odata/ns/related/Company" type="application/atom+xml;type=entry" title="Company" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)/Company" />
<title />
<category term="#Microsoft.Test.OData.Services.ODataWCFService.Customer" scheme="http://docs.oasis-open.org/odata/ns/scheme"/>
<link rel="edit" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)"/>
<link rel="http://docs.oasis-open.org/odata/ns/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)/Parent"/>
<link rel="http://docs.oasis-open.org/odata/ns/related/Orders" type="application/atom+xml;type=feed" title="Orders" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)/Orders"/>
<link rel="http://docs.oasis-open.org/odata/ns/related/Company" type="application/atom+xml;type=entry" title="Company" href="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Customers(PersonID=2)/Company"/>
<title/>
<updated>2014-03-31T09:35:14Z</updated>
<author>
<name />
<name/>
</author>
<content type="application/xml">
<m:properties>
<d:PersonID m:type="Int32">2</d:PersonID>
<d:FirstName>Jill</d:FirstName>
<d:LastName>Jones</d:LastName>
<d:MiddleName m:null="true" />
<d:HomeAddress m:null="true" />
<d:MiddleName m:null="true"/>
<d:HomeAddress m:null="true"/>
<d:Home m:type="GeographyPoint">
<gml:Point gml:srsName="http://www.opengis.net/def/crs/EPSG/0/4326">
<gml:pos>15 161.8</gml:pos>
<gml:pos>15.0 161.8</gml:pos>
</gml:Point>
</d:Home>
<d:Numbers m:type="#Collection(String)" />
<d:Emails m:type="#Collection(String)" />
<d:Numbers m:type="#Collection(String)"/>
<d:Emails m:type="#Collection(String)"/>
<d:City>Sydney</d:City>
<d:Birthday m:type="DateTimeOffset">1983-01-15T00:00:00Z</d:Birthday>
<d:TimeBetweenLastTwoOrders m:type="Duration">PT0.0000002S</d:TimeBetweenLastTwoOrders>

View File

@ -0,0 +1,11 @@
{
"@odata.context": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/$metadata#Collection($ref)",
"value": [
{
"@odata.id": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/OrderDetails(OrderID=7,ProductID=5)"
},
{
"@odata.id": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/OrderDetails(OrderID=7,ProductID=6)"
}
]
}

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<feed xmlns:m="http://docs.oasis-open.org/odata/ns/metadata"
m:context="http://odatae2etest.azurewebsites.net/javatest/DefaultService/$metadata#Collection($ref)"
xmlns="http://www.w3.org/2005/Atom">
<m:ref id="http://odatae2etest.azurewebsites.net/javatest/DefaultService/OrderDetails(OrderID=7,ProductID=5)"/>
<m:ref id="http://odatae2etest.azurewebsites.net/javatest/DefaultService/OrderDetails(OrderID=7,ProductID=6)"/>
</feed>

View File

@ -0,0 +1,4 @@
{
"@odata.context": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/$metadata#$ref",
"@odata.id": "http://odatae2etest.azurewebsites.net/javatest/DefaultService/Employees(PersonID=3)"
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
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.
-->
<m:ref m:context="http://odatae2etest.azurewebsites.net/javatest/DefaultService/$metadata#$ref"
id="http://odatae2etest.azurewebsites.net/javatest/DefaultService/Employees(PersonID=3)"
xmlns:m="http://docs.oasis-open.org/odata/ns/metadata"/>

View File

@ -170,9 +170,9 @@ public interface Constants {
public final static String ATOM_ELEM_ENTRY_REF = "ref";
public final static String ATOM_ELEM_ENTRY_REF_ID = "id";
public final static String ATOM_ATTR_ID = "id";
public final static QName QNAME_ATOM_ELEM_ENTRY_REF_ID = new QName(ATOM_ELEM_ENTRY_REF_ID);
public final static QName QNAME_ATOM_ATTR_ID = new QName(ATOM_ATTR_ID);
public static final QName QNAME_ATOM_ELEM_ENTRY = new QName(NS_ATOM, ATOM_ELEM_ENTRY);

View File

@ -56,7 +56,7 @@ abstract class AbstractAtomDealer {
protected final QName contextQName;
protected final QName entityRefQName;
protected final QName entryRefQName;
protected final QName v4PropertyValueQName;
@ -92,7 +92,7 @@ abstract class AbstractAtomDealer {
new QName(version.getNamespaceMap().get(ODataServiceVersion.NS_DATASERVICES), Constants.NEXT_LINK_REL);
this.contextQName =
new QName(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.CONTEXT);
this.entityRefQName =
this.entryRefQName =
new QName(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.ATOM_ELEM_ENTRY_REF);
this.v4PropertyValueQName =
new QName(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA), Constants.VALUE);

View File

@ -171,9 +171,20 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
}
private AtomEntryImpl entryRef(final StartElement start) throws XMLStreamException {
final AtomEntryImpl entry = new AtomEntryImpl();
final Attribute entryRefId = start.getAttributeByName(Constants.QNAME_ATOM_ATTR_ID);
if (entryRefId != null) {
entry.setId(entryRefId.getValue());
}
return entry;
}
private AtomEntryImpl entry(final XMLEventReader reader, final StartElement start) throws XMLStreamException {
final AtomEntryImpl entry;
if (entityRefQName.equals(start.getName())) {
if (entryRefQName.equals(start.getName())) {
entry = entryRef(start);
} else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(start.getName())) {
entry = new AtomEntryImpl();
@ -296,18 +307,6 @@ public class AtomDeserializer extends AbstractAtomDealer {
return entry;
}
private AtomEntryImpl entryRef(final StartElement start) throws XMLStreamException {
final AtomEntryImpl entry = new AtomEntryImpl();
final Attribute entryRefId = start.getAttributeByName(Constants.QNAME_ATOM_ELEM_ENTRY_REF_ID);
if (entryRefId != null) {
entry.setId(entryRefId.getValue());
}
return entry;
}
private Container<AtomEntryImpl> entry(final InputStream input) throws XMLStreamException {
final XMLEventReader reader = FACTORY.createXMLEventReader(input);
final StartElement start = skipBeforeFirstStartElement(reader);
@ -365,6 +364,8 @@ public class AtomDeserializer extends AbstractAtomDealer {
}
} else if (Constants.QNAME_ATOM_ELEM_ENTRY.equals(event.asStartElement().getName())) {
feed.getEntries().add(entry(reader, event.asStartElement()));
} else if (entryRefQName.equals(event.asStartElement().getName())) {
feed.getEntries().add(entryRef(event.asStartElement()));
}
}

View File

@ -171,12 +171,25 @@ public class AtomSerializer extends AbstractAtomDealer {
writer.writeEndElement();
}
private void entryRef(final XMLStreamWriter writer, final Entry entry) throws XMLStreamException {
writer.writeStartElement(Constants.ATOM_ELEM_ENTRY_REF);
writer.writeNamespace(StringUtils.EMPTY, version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
writer.writeAttribute(Constants.ATOM_ATTR_ID, entry.getId());
}
private void entry(final Writer outWriter, final Entry entry) throws XMLStreamException {
final XMLStreamWriter writer = FACTORY.createXMLStreamWriter(outWriter);
startDocument(writer, Constants.ATOM_ELEM_ENTRY);
if (entry.getType() == null && entry.getProperties().isEmpty()) {
writer.writeStartDocument();
writer.setDefaultNamespace(version.getNamespaceMap().get(ODataServiceVersion.NS_METADATA));
entry(writer, entry);
entryRef(writer, entry);
} else {
startDocument(writer, Constants.ATOM_ELEM_ENTRY);
entry(writer, entry);
}
writer.writeEndElement();
writer.writeEndDocument();
@ -206,9 +219,14 @@ public class AtomSerializer extends AbstractAtomDealer {
}
for (Entry entry : feed.getEntries()) {
writer.writeStartElement(Constants.ATOM_ELEM_ENTRY);
entry(writer, entry);
writer.writeEndElement();
if (entry.getType() == null && entry.getProperties().isEmpty()) {
entryRef(writer, entry);
writer.writeEndElement();
} else {
writer.writeStartElement(Constants.ATOM_ELEM_ENTRY);
entry(writer, entry);
writer.writeEndElement();
}
}
if (feed.getNext() != null) {