Moving contained CRUD tests from external test service to fit

This commit is contained in:
Francesco Chicchiriccò 2014-04-18 11:05:37 +02:00
parent fe8df96fe9
commit e4a4f9e6eb
11 changed files with 271 additions and 119 deletions

View File

@ -122,17 +122,27 @@ public abstract class AbstractServices {
protected final ODataServiceVersion version; protected final ODataServiceVersion version;
protected final FITAtomDeserializer atomDeserializer;
protected final AtomSerializer atomSerializer;
protected final ObjectMapper mapper;
protected final DataBinder dataBinder;
protected final AbstractXMLUtilities xml; protected final AbstractXMLUtilities xml;
protected final AbstractJSONUtilities json; protected final AbstractJSONUtilities json;
@Context
protected UriInfo uriInfo;
protected Metadata metadata; protected Metadata metadata;
public AbstractServices(final ODataServiceVersion version) throws Exception { public AbstractServices(final ODataServiceVersion version) throws Exception {
this.version = version; this.version = version;
this.atomDeserializer = Commons.getAtomDeserializer(version);
this.atomSerializer = Commons.getAtomSerializer(version);
this.mapper = Commons.getJsonMapper(version);
this.dataBinder = new DataBinder(version);
if (version.compareTo(ODataServiceVersion.V30) <= 0) { if (version.compareTo(ODataServiceVersion.V30) <= 0) {
this.xml = new org.apache.olingo.fit.utils.v3.XMLUtilities(); this.xml = new org.apache.olingo.fit.utils.v3.XMLUtilities();
this.json = new org.apache.olingo.fit.utils.v3.JSONUtilities(); this.json = new org.apache.olingo.fit.utils.v3.JSONUtilities();
@ -453,10 +463,6 @@ public abstract class AbstractServices {
throw new ConcurrentModificationException("Concurrent modification"); throw new ConcurrentModificationException("Concurrent modification");
} }
final ObjectMapper mapper = Commons.getJsonMapper(version);
final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version);
final AtomSerializer atomSerializer = Commons.getAtomSerializer(version);
final Accept contentTypeValue = Accept.parse(contentType, version); final Accept contentTypeValue = Accept.parse(contentType, version);
final AtomEntryImpl entryChanges; final AtomEntryImpl entryChanges;
@ -471,7 +477,7 @@ public abstract class AbstractServices {
mapper.readValue(IOUtils.toInputStream(changes), new TypeReference<JSONEntryImpl>() { mapper.readValue(IOUtils.toInputStream(changes), new TypeReference<JSONEntryImpl>() {
}); });
entryChanges = (new DataBinder(version)).getAtomEntry(jcont.getObject()); entryChanges = dataBinder.getAtomEntry(jcont.getObject());
} }
final Container<AtomEntryImpl> container = atomDeserializer.read(entityInfo.getValue(), AtomEntryImpl.class); final Container<AtomEntryImpl> container = atomDeserializer.read(entityInfo.getValue(), AtomEntryImpl.class);
@ -554,9 +560,6 @@ public abstract class AbstractServices {
getUtilities(acceptType).readEntry(acceptType, IOUtils.toInputStream(entity))); getUtilities(acceptType).readEntry(acceptType, IOUtils.toInputStream(entity)));
} }
final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version);
final ObjectMapper mapper = Commons.getJsonMapper(version);
final Container<AtomEntryImpl> cres; final Container<AtomEntryImpl> cres;
if (acceptType == Accept.ATOM) { if (acceptType == Accept.ATOM) {
cres = atomDeserializer.read(res, AtomEntryImpl.class); cres = atomDeserializer.read(res, AtomEntryImpl.class);
@ -564,7 +567,7 @@ public abstract class AbstractServices {
final Container<JSONEntryImpl> jcont = mapper.readValue(res, new TypeReference<JSONEntryImpl>() { final Container<JSONEntryImpl> jcont = mapper.readValue(res, new TypeReference<JSONEntryImpl>() {
}); });
cres = new Container<AtomEntryImpl>(jcont.getContextURL(), jcont.getMetadataETag(), cres = new Container<AtomEntryImpl>(jcont.getContextURL(), jcont.getMetadataETag(),
(new DataBinder(version)).getAtomEntry(jcont.getObject())); dataBinder.getAtomEntry(jcont.getObject()));
} }
final String path = Commons.getEntityBasePath(entitySetName, entityId); final String path = Commons.getEntityBasePath(entitySetName, entityId);
@ -615,17 +618,12 @@ public abstract class AbstractServices {
AbstractUtilities utils = xml; AbstractUtilities utils = xml;
try { try {
final Accept acceptType = Accept.parse(accept, version); final Accept acceptType = Accept.parse(accept, version);
if (acceptType == Accept.XML || acceptType == Accept.TEXT) { if (acceptType == Accept.XML || acceptType == Accept.TEXT) {
throw new UnsupportedMediaTypeException("Unsupported media type"); throw new UnsupportedMediaTypeException("Unsupported media type");
} }
utils = getUtilities(acceptType); utils = getUtilities(acceptType);
final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version);
final AtomSerializer atomSerializer = Commons.getAtomSerializer(version);
final ObjectMapper mapper = Commons.getJsonMapper(version);
final Container<AtomEntryImpl> container; final Container<AtomEntryImpl> container;
final EntitySet entitySet = getMetadataObj().getEntitySet(entitySetName); final EntitySet entitySet = getMetadataObj().getEntitySet(entitySetName);
@ -670,7 +668,7 @@ public abstract class AbstractServices {
mapper.readValue(IOUtils.toInputStream(entity), new TypeReference<JSONEntryImpl>() { mapper.readValue(IOUtils.toInputStream(entity), new TypeReference<JSONEntryImpl>() {
}); });
entry = new DataBinder(version).getAtomEntry(jcontainer.getObject()); entry = dataBinder.getAtomEntry(jcontainer.getObject());
container = new Container<AtomEntryImpl>( container = new Container<AtomEntryImpl>(
jcontainer.getContextURL(), jcontainer.getContextURL(),
@ -933,8 +931,6 @@ public abstract class AbstractServices {
final InputStream feed = FSManager.instance(version).readFile(builder.toString(), Accept.ATOM); final InputStream feed = FSManager.instance(version).readFile(builder.toString(), Accept.ATOM);
final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version);
final AtomSerializer atomSerializer = Commons.getAtomSerializer(version);
final Container<AtomFeedImpl> container = atomDeserializer.read(feed, AtomFeedImpl.class); final Container<AtomFeedImpl> container = atomDeserializer.read(feed, AtomFeedImpl.class);
setInlineCount(container.getObject(), count); setInlineCount(container.getObject(), count);
@ -947,11 +943,9 @@ public abstract class AbstractServices {
writer.flush(); writer.flush();
writer.close(); writer.close();
} else { } else {
final ObjectMapper mapper = Commons.getJsonMapper(version);
mapper.writeValue( mapper.writeValue(
writer, new JsonFeedContainer<JSONFeedImpl>(container.getContextURL(), container.getMetadataETag(), writer, new JsonFeedContainer<JSONFeedImpl>(container.getContextURL(), container.getMetadataETag(),
new DataBinder(version).getJsonFeed(container.getObject()))); dataBinder.getJsonFeed(container.getObject())));
} }
return xml.createResponse( return xml.createResponse(
@ -1058,9 +1052,6 @@ public abstract class AbstractServices {
final InputStream entity = entityInfo.getValue(); final InputStream entity = entityInfo.getValue();
final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version);
final AtomSerializer atomSerializer = Commons.getAtomSerializer(version);
Container<AtomEntryImpl> container = atomDeserializer.read(entity, AtomEntryImpl.class); Container<AtomEntryImpl> container = atomDeserializer.read(entity, AtomEntryImpl.class);
if (container.getContextURL() == null) { if (container.getContextURL() == null) {
container = new Container<AtomEntryImpl>(URI.create(Constants.get(version, ConstantKey.DEFAULT_SERVICE_URL) container = new Container<AtomEntryImpl>(URI.create(Constants.get(version, ConstantKey.DEFAULT_SERVICE_URL)
@ -1375,7 +1366,7 @@ public abstract class AbstractServices {
final AbstractUtilities utils = getUtilities(null); final AbstractUtilities utils = getUtilities(null);
InputStream res = utils.putMediaInMemory(entitySetName, entityId, IOUtils.toInputStream(value)); final InputStream res = utils.putMediaInMemory(entitySetName, entityId, IOUtils.toInputStream(value));
final String location = uriInfo.getRequestUri().toASCIIString().replace("/$value", ""); final String location = uriInfo.getRequestUri().toASCIIString().replace("/$value", "");
@ -1551,10 +1542,9 @@ public abstract class AbstractServices {
} }
try { try {
LinkInfo linkInfo = xml.readLinks(entitySetName, entityId, path, Accept.XML); final LinkInfo linkInfo = xml.readLinks(entitySetName, entityId, path, Accept.XML);
final Map.Entry<String, List<String>> links = xml.extractLinkURIs(linkInfo.getLinks()); final Map.Entry<String, List<String>> links = xml.extractLinkURIs(linkInfo.getLinks());
InputStream stream = xml.readEntities(links.getValue(), path, links.getKey(), linkInfo.isFeed()); final InputStream stream = xml.readEntities(links.getValue(), path, links.getKey(), linkInfo.isFeed());
final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version);
final ByteArrayOutputStream content = new ByteArrayOutputStream(); final ByteArrayOutputStream content = new ByteArrayOutputStream();
final OutputStreamWriter writer = new OutputStreamWriter(content, Constants.ENCODING); final OutputStreamWriter writer = new OutputStreamWriter(content, Constants.ENCODING);
@ -1563,32 +1553,28 @@ public abstract class AbstractServices {
final Container<Feed> container = atomDeserializer.<Feed, AtomFeedImpl>read(stream, AtomFeedImpl.class); final Container<Feed> container = atomDeserializer.<Feed, AtomFeedImpl>read(stream, AtomFeedImpl.class);
if (acceptType == Accept.ATOM) { if (acceptType == Accept.ATOM) {
final AtomSerializer atomSerializer = Commons.getAtomSerializer(version);
atomSerializer.write(writer, container); atomSerializer.write(writer, container);
writer.flush(); writer.flush();
writer.close(); writer.close();
} else { } else {
final ObjectMapper mapper = Commons.getJsonMapper(version);
mapper.writeValue( mapper.writeValue(
writer, writer,
new JsonFeedContainer<JSONFeedImpl>(container.getContextURL(), new JsonFeedContainer<JSONFeedImpl>(container.getContextURL(),
container.getMetadataETag(), container.getMetadataETag(),
(new DataBinder(version)).getJsonFeed((AtomFeedImpl) container.getObject()))); dataBinder.getJsonFeed((AtomFeedImpl) container.getObject())));
} }
} else { } else {
final Container<Entry> container = atomDeserializer.<Entry, AtomEntryImpl>read(stream, AtomEntryImpl.class); final Container<Entry> container = atomDeserializer.<Entry, AtomEntryImpl>read(stream, AtomEntryImpl.class);
if (acceptType == Accept.ATOM) { if (acceptType == Accept.ATOM) {
final AtomSerializer atomSerializer = Commons.getAtomSerializer(version);
atomSerializer.write(writer, container); atomSerializer.write(writer, container);
writer.flush(); writer.flush();
writer.close(); writer.close();
} else { } else {
final ObjectMapper mapper = Commons.getJsonMapper(version);
mapper.writeValue( mapper.writeValue(
writer, writer,
new JsonEntryContainer<JSONEntryImpl>(container.getContextURL(), new JsonEntryContainer<JSONEntryImpl>(container.getContextURL(),
container.getMetadataETag(), container.getMetadataETag(),
(new DataBinder(version)).getJsonEntry((AtomEntryImpl) container.getObject()))); dataBinder.getJsonEntry((AtomEntryImpl) container.getObject())));
} }
} }

View File

@ -30,9 +30,11 @@ import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.Produces; import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType; import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import static javax.ws.rs.core.Response.status; import static javax.ws.rs.core.Response.status;
import javax.ws.rs.core.UriInfo;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.commons.api.data.Feed; import org.apache.olingo.commons.api.data.Feed;
@ -117,6 +119,7 @@ public class V3ActionOverloading extends AbstractServices {
@GET @GET
@Path("/Product({entityId})") @Path("/Product({entityId})")
public Response getProduct( public Response getProduct(
@Context UriInfo uriInfo,
@HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) final String accept, @HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) final String accept,
@PathParam("entityId") final String entityId, @PathParam("entityId") final String entityId,
@QueryParam("$format") @DefaultValue(StringUtils.EMPTY) final String format) { @QueryParam("$format") @DefaultValue(StringUtils.EMPTY) final String format) {
@ -158,6 +161,7 @@ public class V3ActionOverloading extends AbstractServices {
@GET @GET
@Path("/OrderLine(OrderId={orderId},ProductId={productId})") @Path("/OrderLine(OrderId={orderId},ProductId={productId})")
public Response getOrderLine( public Response getOrderLine(
@Context UriInfo uriInfo,
@HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) final String accept, @HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) final String accept,
@PathParam("orderId") final String orderId, @PathParam("orderId") final String orderId,
@PathParam("productId") final String productId, @PathParam("productId") final String productId,

View File

@ -56,7 +56,7 @@ public class V4OpenType {
public V4OpenType() throws Exception { public V4OpenType() throws Exception {
this.openMetadata = new Metadata(FSManager.instance(ODataServiceVersion.V40). this.openMetadata = new Metadata(FSManager.instance(ODataServiceVersion.V40).
readFile("openType" + StringUtils.capitalize(Constants.get(ODataServiceVersion.V40, ConstantKey.METADATA)), readFile("openType" + StringUtils.capitalize(Constants.get(ODataServiceVersion.V40, ConstantKey.METADATA)),
Accept.XML)); Accept.XML));
this.services = new V4Services() { this.services = new V4Services() {
@Override @Override
protected Metadata getMetadataObj() { protected Metadata getMetadataObj() {
@ -115,8 +115,7 @@ public class V4OpenType {
@QueryParam("$expand") @DefaultValue(StringUtils.EMPTY) String expand, @QueryParam("$expand") @DefaultValue(StringUtils.EMPTY) String expand,
@QueryParam("$select") @DefaultValue(StringUtils.EMPTY) String select) { @QueryParam("$select") @DefaultValue(StringUtils.EMPTY) String select) {
return replaceServiceName( return replaceServiceName(services.getEntityInternal(
services.getEntityInternal(
uriInfo.getRequestUri().toASCIIString(), accept, entitySetName, entityId, format, expand, select, false)); uriInfo.getRequestUri().toASCIIString(), accept, entitySetName, entityId, format, expand, select, false));
} }

View File

@ -20,16 +20,22 @@ package org.apache.olingo.fit;
import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.util.Map; import java.util.Map;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue; import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET; import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam; import javax.ws.rs.HeaderParam;
import javax.ws.rs.NotFoundException; import javax.ws.rs.NotFoundException;
import org.apache.olingo.fit.utils.XHTTPMethodInterceptor; import javax.ws.rs.POST;
import javax.ws.rs.Path; import javax.ws.rs.Path;
import javax.ws.rs.PathParam; import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam; import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
@ -41,10 +47,14 @@ import org.apache.olingo.commons.api.data.Feed;
import org.apache.olingo.commons.api.data.Property; import org.apache.olingo.commons.api.data.Property;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion; import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.core.data.AtomEntryImpl; import org.apache.olingo.commons.core.data.AtomEntryImpl;
import org.apache.olingo.commons.core.data.AtomFeedImpl;
import org.apache.olingo.commons.core.data.AtomSerializer;
import org.apache.olingo.commons.core.data.JSONEntryImpl; import org.apache.olingo.commons.core.data.JSONEntryImpl;
import org.apache.olingo.commons.core.data.JSONFeedImpl;
import org.apache.olingo.commons.core.edm.EdmTypeInfo; import org.apache.olingo.commons.core.edm.EdmTypeInfo;
import org.apache.olingo.fit.methods.PATCH; import org.apache.olingo.fit.methods.PATCH;
import org.apache.olingo.fit.serializer.FITAtomDeserializer; import org.apache.olingo.fit.serializer.FITAtomDeserializer;
import org.apache.olingo.fit.serializer.JsonFeedContainer;
import org.apache.olingo.fit.utils.AbstractUtilities; import org.apache.olingo.fit.utils.AbstractUtilities;
import org.apache.olingo.fit.utils.Accept; import org.apache.olingo.fit.utils.Accept;
import org.apache.olingo.fit.utils.Commons; import org.apache.olingo.fit.utils.Commons;
@ -54,6 +64,7 @@ import org.apache.olingo.fit.utils.DataBinder;
import org.apache.olingo.fit.utils.FSManager; import org.apache.olingo.fit.utils.FSManager;
import org.apache.olingo.fit.utils.LinkInfo; import org.apache.olingo.fit.utils.LinkInfo;
import org.apache.olingo.fit.utils.ResolvingReferencesInterceptor; import org.apache.olingo.fit.utils.ResolvingReferencesInterceptor;
import org.apache.olingo.fit.utils.XHTTPMethodInterceptor;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@Service @Service
@ -98,7 +109,7 @@ public class V4Services extends AbstractServices {
return utils.getValue().createResponse( return utils.getValue().createResponse(
FSManager.instance(version).readFile(Constants.get(version, ConstantKey.REF) FSManager.instance(version).readFile(Constants.get(version, ConstantKey.REF)
+ File.separatorChar + filename, utils.getKey()), + File.separatorChar + filename, utils.getKey()),
null, null,
utils.getKey()); utils.getKey());
} catch (Exception e) { } catch (Exception e) {
@ -120,7 +131,7 @@ public class V4Services extends AbstractServices {
final Response response = final Response response =
getEntityInternal(uriInfo.getRequestUri().toASCIIString(), getEntityInternal(uriInfo.getRequestUri().toASCIIString(),
accept, entitySetName, entityId, accept, StringUtils.EMPTY, StringUtils.EMPTY, false); accept, entitySetName, entityId, accept, StringUtils.EMPTY, StringUtils.EMPTY, false);
return response.getStatus() >= 400 return response.getStatus() >= 400
? postNewEntity(uriInfo, accept, contentType, prefer, entitySetName, changes) ? postNewEntity(uriInfo, accept, contentType, prefer, entitySetName, changes)
: super.patchEntity(uriInfo, accept, contentType, prefer, ifMatch, entitySetName, entityId, changes); : super.patchEntity(uriInfo, accept, contentType, prefer, ifMatch, entitySetName, entityId, changes);
@ -145,11 +156,17 @@ public class V4Services extends AbstractServices {
} }
} }
private StringBuilder containedPath(final String entityId, final String containedEntitySetName) {
return new StringBuilder("Accounts").append(File.separatorChar).
append(entityId).append(File.separatorChar).
append("links").append(File.separatorChar).
append(containedEntitySetName);
}
@GET @GET
@Path("/{entitySetName}({entityId})/{containedEntitySetName}({containedEntityId})") @Path("/Accounts({entityId})/{containedEntitySetName}({containedEntityId})")
public Response getContainedEntity( public Response getContainedEntity(
@HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) String accept, @HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) String accept,
@PathParam("entitySetName") String entitySetName,
@PathParam("entityId") String entityId, @PathParam("entityId") String entityId,
@PathParam("containedEntitySetName") String containedEntitySetName, @PathParam("containedEntitySetName") String containedEntitySetName,
@PathParam("containedEntityId") String containedEntityId, @PathParam("containedEntityId") String containedEntityId,
@ -162,23 +179,106 @@ public class V4Services extends AbstractServices {
} else { } else {
acceptType = Accept.parse(accept, version); acceptType = Accept.parse(accept, version);
} }
if (acceptType == Accept.XML || acceptType == Accept.TEXT) { if (acceptType == Accept.XML || acceptType == Accept.TEXT) {
throw new UnsupportedMediaTypeException("Unsupported media type"); throw new UnsupportedMediaTypeException("Unsupported media type");
} }
final LinkInfo links = xml.readLinks( final InputStream entry = FSManager.instance(version).
entitySetName, entityId, containedEntitySetName + "(" + containedEntityId + ")", acceptType); readFile(containedPath(entityId, containedEntitySetName).
append('(').append(containedEntityId).append(')').toString(), Accept.ATOM);
final Container<AtomEntryImpl> container = atomDeserializer.read(entry, AtomEntryImpl.class);
return xml.createResponse( return xml.createResponse(
links.getLinks(), null,
links.getEtag(), xml.writeEntry(acceptType, container),
null,
acceptType); acceptType);
} catch (Exception e) { } catch (Exception e) {
return xml.createFaultResponse(accept, e); return xml.createFaultResponse(accept, e);
} }
} }
@POST
@Path("/Accounts({entityId})/{containedEntitySetName:.*}")
public Response postContainedEntity(
@Context UriInfo uriInfo,
@HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) String accept,
@HeaderParam("Content-Type") @DefaultValue(StringUtils.EMPTY) String contentType,
@PathParam("entityId") String entityId,
@PathParam("containedEntitySetName") String containedEntitySetName,
final String entity) {
// default
try {
final Accept acceptType = Accept.parse(accept, version);
if (acceptType == Accept.XML || acceptType == Accept.TEXT) {
throw new UnsupportedMediaTypeException("Unsupported media type");
}
final AbstractUtilities utils = getUtilities(acceptType);
// 1. parse the entry (from Atom or JSON) into AtomEntryImpl
final Container<AtomEntryImpl> entryContainer;
final AtomEntryImpl entry;
final Accept contentTypeValue = Accept.parse(contentType, version);
if (Accept.ATOM == contentTypeValue) {
entryContainer = atomDeserializer.read(IOUtils.toInputStream(entity), AtomEntryImpl.class);
entry = entryContainer.getObject();
} else {
final Container<JSONEntryImpl> jcontainer =
mapper.readValue(IOUtils.toInputStream(entity), new TypeReference<JSONEntryImpl>() {
});
entry = dataBinder.getAtomEntry(jcontainer.getObject());
entryContainer = new Container<AtomEntryImpl>(
jcontainer.getContextURL(),
jcontainer.getMetadataETag(),
entry);
}
final EdmTypeInfo contained = new EdmTypeInfo.Builder().setTypeExpression(getMetadataObj().
getNavigationProperties("Accounts").get(containedEntitySetName).getType()).build();
final String entityKey = getUtilities(contentTypeValue).
getDefaultEntryKey(contained.getFullQualifiedName().getName(), entry);
// 2. Store the new entity
final String atomEntryRelativePath = containedPath(entityId, containedEntitySetName).
append('(').append(entityKey).append(')').toString();
FSManager.instance(version).putInMemory(
utils.writeEntry(Accept.ATOM, entryContainer),
FSManager.instance(version).getAbsolutePath(atomEntryRelativePath, Accept.ATOM));
// 3. Update the contained entity set
final String atomFeedRelativePath = containedPath(entityId, containedEntitySetName).toString();
final InputStream feedIS = FSManager.instance(version).readFile(atomFeedRelativePath, Accept.ATOM);
final Container<AtomFeedImpl> feedContainer = atomDeserializer.read(feedIS, AtomFeedImpl.class);
feedContainer.getObject().getEntries().add(entry);
final ByteArrayOutputStream content = new ByteArrayOutputStream();
final OutputStreamWriter writer = new OutputStreamWriter(content, Constants.ENCODING);
atomSerializer.write(writer, feedContainer);
writer.flush();
writer.close();
FSManager.instance(version).putInMemory(
new ByteArrayInputStream(content.toByteArray()),
FSManager.instance(version).getAbsolutePath(atomFeedRelativePath, Accept.ATOM));
// Finally, return
return utils.createResponse(
uriInfo.getRequestUri().toASCIIString() + "(" + entityKey + ")",
utils.writeEntry(acceptType, entryContainer),
null,
acceptType,
Response.Status.CREATED);
} catch (Exception e) {
LOG.error("While creating new contained entity", e);
return xml.createFaultResponse(accept, e);
}
}
@PATCH @PATCH
@Path("/{entitySetName}({entityId})/{containedEntitySetName}({containedEntityId})") @Path("/{entitySetName}({entityId})/{containedEntitySetName}({containedEntityId})")
public Response patchContainedEntity( public Response patchContainedEntity(
@ -211,7 +311,6 @@ public class V4Services extends AbstractServices {
final LinkInfo links = xml.readLinks( final LinkInfo links = xml.readLinks(
entitySetName, entityId, containedEntitySetName + "(" + containedEntityId + ")", Accept.ATOM); entitySetName, entityId, containedEntitySetName + "(" + containedEntityId + ")", Accept.ATOM);
final FITAtomDeserializer atomDeserializer = Commons.getAtomDeserializer(version);
Container<AtomEntryImpl> container = atomDeserializer.read(links.getLinks(), AtomEntryImpl.class); Container<AtomEntryImpl> container = atomDeserializer.read(links.getLinks(), AtomEntryImpl.class);
final AtomEntryImpl original = container.getObject(); final AtomEntryImpl original = container.getObject();
@ -225,12 +324,9 @@ public class V4Services extends AbstractServices {
getNavigationProperty(containedEntitySetName).getType(); getNavigationProperty(containedEntitySetName).getType();
final EdmTypeInfo typeInfo = new EdmTypeInfo.Builder().setTypeExpression(containedType).build(); final EdmTypeInfo typeInfo = new EdmTypeInfo.Builder().setTypeExpression(containedType).build();
final ObjectMapper mapper = Commons.getJsonMapper(version);
final DataBinder dataBinder = new DataBinder(version);
final Container<JSONEntryImpl> jsonContainer = mapper.readValue(IOUtils.toInputStream(changes), final Container<JSONEntryImpl> jsonContainer = mapper.readValue(IOUtils.toInputStream(changes),
new TypeReference<JSONEntryImpl>() { new TypeReference<JSONEntryImpl>() {
}); });
jsonContainer.getObject().setType(typeInfo.getFullQualifiedName().toString()); jsonContainer.getObject().setType(typeInfo.getFullQualifiedName().toString());
entryChanges = dataBinder.getAtomEntry(jsonContainer.getObject()); entryChanges = dataBinder.getAtomEntry(jsonContainer.getObject());
} }
@ -251,4 +347,93 @@ public class V4Services extends AbstractServices {
return xml.createFaultResponse(accept, e); return xml.createFaultResponse(accept, e);
} }
} }
@DELETE
@Path("/Accounts({entityId})/{containedEntitySetName}({containedEntityId})")
public Response removeContainedEntity(
@PathParam("entityId") String entityId,
@PathParam("containedEntitySetName") String containedEntitySetName,
@PathParam("containedEntityId") String containedEntityId) {
try {
// 1. Fetch the contained entity to be removed
final InputStream entry = FSManager.instance(version).
readFile(containedPath(entityId, containedEntitySetName).
append('(').append(containedEntityId).append(')').toString(), Accept.ATOM);
final Container<AtomEntryImpl> container = atomDeserializer.read(entry, AtomEntryImpl.class);
// 2. Remove the contained entity
final String atomEntryRelativePath = containedPath(entityId, containedEntitySetName).
append('(').append(containedEntityId).append(')').toString();
FSManager.instance(version).deleteFile(atomEntryRelativePath);
// 3. Update the contained entity set
final String atomFeedRelativePath = containedPath(entityId, containedEntitySetName).toString();
final InputStream feedIS = FSManager.instance(version).readFile(atomFeedRelativePath, Accept.ATOM);
final Container<AtomFeedImpl> feedContainer = atomDeserializer.read(feedIS, AtomFeedImpl.class);
feedContainer.getObject().getEntries().remove(container.getObject());
final ByteArrayOutputStream content = new ByteArrayOutputStream();
final OutputStreamWriter writer = new OutputStreamWriter(content, Constants.ENCODING);
atomSerializer.write(writer, feedContainer);
writer.flush();
writer.close();
FSManager.instance(version).putInMemory(
new ByteArrayInputStream(content.toByteArray()),
FSManager.instance(version).getAbsolutePath(atomFeedRelativePath, Accept.ATOM));
return xml.createResponse(null, null, null, null, Response.Status.NO_CONTENT);
} catch (Exception e) {
return xml.createFaultResponse(Accept.XML.toString(version), e);
}
}
@GET
@Path("/Accounts({entityId})/{containedEntitySetName:.*}")
public Response getContainedEntitySet(
@HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) String accept,
@PathParam("entityId") String entityId,
@PathParam("containedEntitySetName") String containedEntitySetName,
@QueryParam("$format") @DefaultValue(StringUtils.EMPTY) String format) {
try {
final Accept acceptType;
if (StringUtils.isNotBlank(format)) {
acceptType = Accept.valueOf(format.toUpperCase());
} else {
acceptType = Accept.parse(accept, version);
}
if (acceptType == Accept.XML || acceptType == Accept.TEXT) {
throw new UnsupportedMediaTypeException("Unsupported media type");
}
final InputStream feed = FSManager.instance(version).
readFile(containedPath(entityId, containedEntitySetName).toString(), Accept.ATOM);
final Container<AtomFeedImpl> container = atomDeserializer.read(feed, AtomFeedImpl.class);
final ByteArrayOutputStream content = new ByteArrayOutputStream();
final OutputStreamWriter writer = new OutputStreamWriter(content, Constants.ENCODING);
if (acceptType == Accept.ATOM) {
atomSerializer.write(writer, container);
writer.flush();
writer.close();
} else {
mapper.writeValue(
writer, new JsonFeedContainer<JSONFeedImpl>(container.getContextURL(), container.getMetadataETag(),
dataBinder.getJsonFeed(container.getObject())));
}
return xml.createResponse(
null,
new ByteArrayInputStream(content.toByteArray()),
null,
acceptType);
} catch (Exception e) {
return xml.createFaultResponse(accept, e);
}
}
} }

View File

@ -120,7 +120,6 @@ public abstract class AbstractUtilities {
* Retrieve entity links and inlines. * Retrieve entity links and inlines.
* *
* @param entitySetName * @param entitySetName
* @param entityKey
* @param is * @param is
* @return * @return
* @throws IOException * @throws IOException
@ -589,7 +588,7 @@ public abstract class AbstractUtilities {
final ObjectMapper mapper = Commons.getJsonMapper(version); final ObjectMapper mapper = Commons.getJsonMapper(version);
mapper.writeValue( mapper.writeValue(
writer, new JsonEntryContainer<JSONEntryImpl>(container.getContextURL(), container.getMetadataETag(), writer, new JsonEntryContainer<JSONEntryImpl>(container.getContextURL(), container.getMetadataETag(),
(new DataBinder(version)).getJsonEntry((AtomEntryImpl) container.getObject()))); new DataBinder(version).getJsonEntry((AtomEntryImpl) container.getObject())));
} }
return new ByteArrayInputStream(content.toByteArray()); return new ByteArrayInputStream(content.toByteArray());
@ -672,6 +671,8 @@ public abstract class AbstractUtilities {
} }
Commons.SEQUENCE.put(entitySetName, productDetailId); Commons.SEQUENCE.put(entitySetName, productDetailId);
Commons.SEQUENCE.put("Products", productId); Commons.SEQUENCE.put("Products", productId);
} else if ("PaymentInstrument".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "PaymentInstrumentID");
} else { } else {
throw new Exception(String.format("EntitySet '%s' not found", entitySetName)); throw new Exception(String.format("EntitySet '%s' not found", entitySetName));
} }

View File

@ -89,6 +89,7 @@ public abstract class Commons {
SEQUENCE.put("RowIndex", 1000); SEQUENCE.put("RowIndex", 1000);
SEQUENCE.put("Products", 1000); SEQUENCE.put("Products", 1000);
SEQUENCE.put("ProductDetails", 1000); SEQUENCE.put("ProductDetails", 1000);
SEQUENCE.put("PaymentInstrument", 10192);
MEDIA_CONTENT.put("CustomerInfo", "CustomerinfoId"); MEDIA_CONTENT.put("CustomerInfo", "CustomerinfoId");
MEDIA_CONTENT.put("Car", "VIN"); MEDIA_CONTENT.put("Car", "VIN");

View File

@ -102,10 +102,7 @@ public class FSManager {
public void putInMemory(final Container<AtomEntryImpl> container, final String relativePath) public void putInMemory(final Container<AtomEntryImpl> container, final String relativePath)
throws IOException { throws IOException {
try { try {
final ODataServiceVersion serviceVersion = final AtomSerializer atomSerializer = Commons.getAtomSerializer(version);
version == ODataServiceVersion.V30 ? ODataServiceVersion.V30 : ODataServiceVersion.V40;
final AtomSerializer atomSerializer = Commons.getAtomSerializer(serviceVersion);
final ByteArrayOutputStream content = new ByteArrayOutputStream(); final ByteArrayOutputStream content = new ByteArrayOutputStream();
final OutputStreamWriter writer = new OutputStreamWriter(content, Constants.ENCODING); final OutputStreamWriter writer = new OutputStreamWriter(content, Constants.ENCODING);
@ -116,13 +113,12 @@ public class FSManager {
putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.ATOM)); putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.ATOM));
content.reset(); content.reset();
final ObjectMapper mapper = Commons.getJsonMapper(serviceVersion); final ObjectMapper mapper = Commons.getJsonMapper(version);
mapper.writeValue( mapper.writeValue(
writer, new JsonEntryContainer<JSONEntryImpl>( writer, new JsonEntryContainer<JSONEntryImpl>(
container.getContextURL(), container.getContextURL(),
container.getMetadataETag(), container.getMetadataETag(),
(new DataBinder(version == ODataServiceVersion.V30 ? ODataServiceVersion.V30 : ODataServiceVersion.V40)). new DataBinder(version).getJsonEntry(container.getObject())));
getJsonEntry(container.getObject())));
putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.JSON_FULLMETA)); putInMemory(new ByteArrayInputStream(content.toByteArray()), getAbsolutePath(relativePath, Accept.JSON_FULLMETA));
} catch (Exception e) { } catch (Exception e) {
@ -164,7 +160,6 @@ public class FSManager {
} }
public void deleteFile(final String relativePath) { public void deleteFile(final String relativePath) {
for (Accept accept : Accept.values()) { for (Accept accept : Accept.values()) {
final String path = getAbsolutePath(relativePath, accept); final String path = getAbsolutePath(relativePath, accept);
LOG.info("Delete {}", path); LOG.info("Delete {}", path);

View File

@ -21,12 +21,8 @@ package org.apache.olingo.client.core.it.v4;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URI; import java.net.URI;
import java.util.Calendar; import java.util.Calendar;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.RandomUtils;
import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest; import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest;
import org.apache.olingo.client.api.communication.response.ODataDeleteResponse; import org.apache.olingo.client.api.communication.response.ODataDeleteResponse;
@ -43,31 +39,10 @@ import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.format.ODataPubFormat; import org.apache.olingo.commons.api.format.ODataPubFormat;
import org.apache.olingo.commons.core.domain.v4.ODataEntityImpl; import org.apache.olingo.commons.core.domain.v4.ODataEntityImpl;
import org.junit.Assume;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
public class EntityCreateTestITCase extends AbstractTestITCase { public class EntityCreateTestITCase extends AbstractTestITCase {
private static final String serviceRoot = "http://odatae2etest.azurewebsites.net/javatest/DefaultService";
// TODO: remove once fit provides contained entity CRUD
@BeforeClass
public static void checkServerIsOnline() throws IOException {
final Socket socket = new Socket();
boolean reachable = false;
try {
socket.connect(new InetSocketAddress("odatae2etest.azurewebsites.net", 80), 2000);
reachable = true;
} catch (Exception e) {
LOG.warn("External test service not reachable, ignoring this whole class: {}",
OperationImportInvokeTestITCase.class.getName());
} finally {
IOUtils.closeQuietly(socket);
}
Assume.assumeTrue(reachable);
}
private void order(final ODataPubFormat format, final int id) { private void order(final ODataPubFormat format, final int id) {
final ODataEntity order = new ODataEntityImpl( final ODataEntity order = new ODataEntityImpl(
new FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.Order")); new FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.Order"));
@ -116,8 +91,8 @@ public class EntityCreateTestITCase extends AbstractTestITCase {
} }
private void onContained(final ODataPubFormat format) { private void onContained(final ODataPubFormat format) {
final URI uri = getClient().getURIBuilder(serviceRoot).appendEntitySetSegment("Accounts").appendKeySegment(101). final URI uri = getClient().getURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("Accounts").
appendNavigationSegment("MyPaymentInstruments").build(); appendKeySegment(101).appendNavigationSegment("MyPaymentInstruments").build();
// 1. read contained collection before any operation // 1. read contained collection before any operation
ODataEntitySet instruments = getClient().getRetrieveRequestFactory().getEntitySetRequest(uri).execute().getBody(); ODataEntitySet instruments = getClient().getRetrieveRequestFactory().getEntitySetRequest(uri).execute().getBody();
@ -171,8 +146,9 @@ public class EntityCreateTestITCase extends AbstractTestITCase {
onContained(ODataPubFormat.JSON); onContained(ODataPubFormat.JSON);
} }
private void deepInsert(final ODataPubFormat format) throws EdmPrimitiveTypeException { private void deepInsert(final ODataPubFormat format, final int productId, final int productDetailId)
final int productId = RandomUtils.nextInt(10, 20); throws EdmPrimitiveTypeException {
final ODataEntity product = getClient().getObjectFactory(). final ODataEntity product = getClient().getObjectFactory().
newEntity(new FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.Product")); newEntity(new FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.Product"));
product.getProperties().add(getClient().getObjectFactory().newPrimitiveProperty("ProductID", product.getProperties().add(getClient().getObjectFactory().newPrimitiveProperty("ProductID",
@ -201,7 +177,6 @@ public class EntityCreateTestITCase extends AbstractTestITCase {
product.getProperty("CoverColors").getCollectionValue().add(getClient().getObjectFactory(). product.getProperty("CoverColors").getCollectionValue().add(getClient().getObjectFactory().
newEnumValue("Microsoft.Test.OData.Services.ODataWCFService.Color", "Red")); newEnumValue("Microsoft.Test.OData.Services.ODataWCFService.Color", "Red"));
final int productDetailId = RandomUtils.nextInt(10, 20);
final ODataEntity detail = getClient().getObjectFactory(). final ODataEntity detail = getClient().getObjectFactory().
newEntity(new FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.ProductDetail")); newEntity(new FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.ProductDetail"));
detail.getProperties().add(getClient().getObjectFactory().newPrimitiveProperty("ProductID", detail.getProperties().add(getClient().getObjectFactory().newPrimitiveProperty("ProductID",
@ -242,11 +217,11 @@ public class EntityCreateTestITCase extends AbstractTestITCase {
@Test @Test
public void atomDeepInsert() throws EdmPrimitiveTypeException { public void atomDeepInsert() throws EdmPrimitiveTypeException {
deepInsert(ODataPubFormat.ATOM); deepInsert(ODataPubFormat.ATOM, 10, 10);
} }
@Test @Test
public void jsonDeepInsert() throws EdmPrimitiveTypeException { public void jsonDeepInsert() throws EdmPrimitiveTypeException {
deepInsert(ODataPubFormat.JSON_FULL_METADATA); deepInsert(ODataPubFormat.JSON_FULL_METADATA, 11, 11);
} }
} }

View File

@ -248,16 +248,16 @@ public class EntityRetrieveTestITCase extends AbstractTestITCase {
} }
@Test @Test
public void retrieveEntityViaReferenceAsAtom() { public void atomReference() {
retrieveEntityViaReference(ODataPubFormat.ATOM); reference(ODataPubFormat.ATOM);
} }
@Test @Test
public void retrieveEntityViaReferenceAsJSON() { public void jsonReference() {
retrieveEntityViaReference(ODataPubFormat.JSON_FULL_METADATA); reference(ODataPubFormat.JSON_FULL_METADATA);
} }
private void retrieveEntityViaReference(final ODataPubFormat format) { private void reference(final ODataPubFormat format) {
final URIBuilder uriBuilder = client.getURIBuilder(getServiceRoot()). final URIBuilder uriBuilder = client.getURIBuilder(getServiceRoot()).
appendEntitySetSegment("Orders").appendKeySegment(8).appendNavigationSegment("CustomerForOrder"). appendEntitySetSegment("Orders").appendKeySegment(8).appendNavigationSegment("CustomerForOrder").
appendRefSegment(); appendRefSegment();

View File

@ -292,25 +292,22 @@ public final class EdmDateTimeOffset extends SingletonPrimitiveType {
final Integer precision) throws IllegalArgumentException { final Integer precision) throws IllegalArgumentException {
if (fractionalSeconds > 0) { if (fractionalSeconds > 0) {
if (precision == null || precision < String.valueOf(fractionalSeconds).length()) { String formatted = NANO_FORMAT.get().format(fractionalSeconds);
int actualLength = formatted.length();
boolean nonZeroFound = false;
for (int i = formatted.length() - 1; i >= 0 && !nonZeroFound; i--) {
if ('0' == formatted.charAt(i)) {
actualLength--;
} else {
nonZeroFound = true;
}
}
if (precision == null || precision < actualLength) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
} }
// Keep output similar to Calendar's, if possible result.append('.').append(formatted.substring(0, actualLength));
String fractionals = NANO_FORMAT.get().format(fractionalSeconds);
boolean canStart = false;
int firstZeroAfterNonZeroIdx = -1;
for (int i = 0; i < fractionals.length() && firstZeroAfterNonZeroIdx == -1; i++) {
if ('0' != fractionals.charAt(i)) {
canStart = true;
} else if (canStart && '0' == fractionals.charAt(i)) {
firstZeroAfterNonZeroIdx = i;
}
}
if (firstZeroAfterNonZeroIdx != -1 && 0 == Integer.valueOf(fractionals.substring(firstZeroAfterNonZeroIdx))) {
fractionals = fractionals.substring(0, firstZeroAfterNonZeroIdx);
}
result.append('.').append(fractionals);
} }
} }
} }

View File

@ -62,6 +62,15 @@ public class EdmDateTimeOffsetTest extends PrimitiveTypeBaseTest {
dateTime.setTimeZone(TimeZone.getTimeZone("GMT+11:00")); dateTime.setTimeZone(TimeZone.getTimeZone("GMT+11:00"));
assertEquals("2012-02-29T01:02:03+11:00", instance.valueToString(dateTime, null, null, null, null, null)); assertEquals("2012-02-29T01:02:03+11:00", instance.valueToString(dateTime, null, null, null, null, null));
dateTime.set(Calendar.MILLISECOND, 503);
assertEquals("2012-02-29T01:02:03.503+11:00", instance.valueToString(dateTime, null, null, 3, null, null));
dateTime.set(Calendar.MILLISECOND, 530);
assertEquals("2012-02-29T01:02:03.53+11:00", instance.valueToString(dateTime, null, null, 3, null, null));
dateTime.set(Calendar.MILLISECOND, 53);
assertEquals("2012-02-29T01:02:03.053+11:00", instance.valueToString(dateTime, null, null, 3, null, null));
final Long millis = 1330558323007L; final Long millis = 1330558323007L;
assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(millis, null, null, 3, null, null)); assertEquals("2012-02-29T23:32:03.007Z", instance.valueToString(millis, null, null, 3, null, null));
assertEquals("1969-12-31T23:59:59.9Z", instance.valueToString(-100L, null, null, 1, null, null)); assertEquals("1969-12-31T23:59:59.9Z", instance.valueToString(-100L, null, null, 1, null, null));