Adding core test for derived entity type (and derived complex type) CRUD

This commit is contained in:
Francesco Chicchiriccò 2014-05-09 12:15:48 +02:00
parent e5cfd8eb13
commit b497634529
10 changed files with 174 additions and 41 deletions

View File

@ -27,20 +27,30 @@ public class EntityType extends AbstractMetadataElement {
private final String name;
private String baseType;
private final Map<String, Property> properties;
private final Map<String, NavigationProperty> navigationProperties;
public EntityType(final String name) {
this.name = name;
properties = new HashMap<String, Property>();
navigationProperties = new HashMap<String, NavigationProperty>();
this.properties = new HashMap<String, Property>();
this.navigationProperties = new HashMap<String, NavigationProperty>();
}
public String getName() {
return name;
}
public String getBaseType() {
return baseType;
}
public void setBaseType(final String baseType) {
this.baseType = baseType;
}
public Collection<NavigationProperty> getNavigationProperties() {
return new HashSet<NavigationProperty>(navigationProperties.values());
}

View File

@ -31,10 +31,10 @@ import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.fit.utils.ConstantKey;
import org.apache.olingo.fit.utils.Constants;
import org.codehaus.plexus.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -135,14 +135,33 @@ public class Metadata extends AbstractMetadataElement {
}
public EntityType getEntityType(final String fqn) {
final int lastDotIndex = fqn.lastIndexOf('.');
final String ns = fqn.substring(0, lastDotIndex).replaceAll("^#", "");
final String name = fqn.substring(lastDotIndex + 1);
return getSchema(ns) == null ? null : getSchema(ns).getEntityType(name);
EntityType result = null;
final String ns = StringUtils.substringBeforeLast(fqn, ".");
if (getSchema(ns) != null) {
final String name = StringUtils.substringAfterLast(fqn, ".");
result = getSchema(ns).getEntityType(name);
if (result != null && result.getBaseType() != null) {
final String baseNS = StringUtils.substringBeforeLast(result.getBaseType(), ".");
if (getSchema(baseNS) != null) {
final String baseName = StringUtils.substringAfterLast(result.getBaseType(), ".");
final EntityType baseType = getSchema(baseNS).getEntityType(baseName);
if (baseType != null) {
for (Map.Entry<String, Property> entry : baseType.getPropertyMap().entrySet()) {
result.addProperty(entry.getKey(), entry.getValue());
}
for (Map.Entry<String, NavigationProperty> entry : baseType.getNavigationPropertyMap().entrySet()) {
result.addNavigationProperty(entry.getKey(), entry.getValue());
}
}
}
}
}
return result;
}
public Map<String, NavigationProperty> getNavigationProperties(final String entitySetName) {
for (Schema schema : getSchemas()) {
for (Container container : schema.getContainers()) {
final EntitySet entitySet = container.getEntitySet(entitySetName);
@ -279,6 +298,10 @@ public class Metadata extends AbstractMetadataElement {
private EntityType getEntityType(final StartElement start, final XMLEventReader reader) throws XMLStreamException {
final EntityType entityType = new EntityType(start.getAttributeByName(new QName("Name")).getValue());
final Attribute baseType = start.getAttributeByName(new QName("BaseType"));
if (baseType != null) {
entityType.setBaseType(baseType.getValue());
}
boolean completed = false;

View File

@ -681,13 +681,13 @@ public abstract class AbstractUtilities {
return res;
}
public String getDefaultEntryKey(final String entitySetName, final AtomEntityImpl entry) throws IOException {
public String getDefaultEntryKey(final String entitySetName, final AtomEntityImpl entity) throws IOException {
try {
String res;
if ("Message".equals(entitySetName)) {
int messageId;
if (entry.getProperty("MessageId") == null || entry.getProperty("FromUsername") == null) {
if (entity.getProperty("MessageId") == null || entity.getProperty("FromUsername") == null) {
if (Commons.SEQUENCE.containsKey(entitySetName)) {
messageId = Commons.SEQUENCE.get(entitySetName) + 1;
res = "MessageId=" + String.valueOf(messageId) + ",FromUsername=1";
@ -695,35 +695,35 @@ public abstract class AbstractUtilities {
throw new Exception(String.format("Unable to retrieve entity key value for %s", entitySetName));
}
} else {
messageId = Integer.valueOf(entry.getProperty("MessageId").getValue().asPrimitive().get());
res = "MessageId=" + entry.getProperty("MessageId").getValue().asPrimitive().get()
+ ",FromUsername=" + entry.getProperty("FromUsername").getValue().asPrimitive().get();
messageId = Integer.valueOf(entity.getProperty("MessageId").getValue().asPrimitive().get());
res = "MessageId=" + entity.getProperty("MessageId").getValue().asPrimitive().get()
+ ",FromUsername=" + entity.getProperty("FromUsername").getValue().asPrimitive().get();
}
Commons.SEQUENCE.put(entitySetName, messageId);
} else if ("Order".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "OrderId");
res = getDefaultEntryKey(entitySetName, entity, "OrderId");
} else if ("Orders".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "OrderID");
res = getDefaultEntryKey(entitySetName, entity, "OrderID");
} else if ("Customer".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "CustomerId");
res = getDefaultEntryKey(entitySetName, entity, "CustomerId");
} else if ("Person".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "PersonId");
res = getDefaultEntryKey(entitySetName, entity, "PersonId");
} else if ("ComputerDetail".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "ComputerDetailId");
res = getDefaultEntryKey(entitySetName, entity, "ComputerDetailId");
} else if ("AllGeoTypesSet".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "Id");
res = getDefaultEntryKey(entitySetName, entity, "Id");
} else if ("CustomerInfo".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "CustomerInfoId");
res = getDefaultEntryKey(entitySetName, entity, "CustomerInfoId");
} else if ("Car".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "VIN");
res = getDefaultEntryKey(entitySetName, entity, "VIN");
} else if ("RowIndex".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "Id");
res = getDefaultEntryKey(entitySetName, entity, "Id");
} else if ("Products".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "ProductID");
res = getDefaultEntryKey(entitySetName, entity, "ProductID");
} else if ("ProductDetails".equals(entitySetName)) {
int productId;
int productDetailId;
if (entry.getProperty("ProductID") == null || entry.getProperty("ProductDetailID") == null) {
if (entity.getProperty("ProductID") == null || entity.getProperty("ProductDetailID") == null) {
if (Commons.SEQUENCE.containsKey(entitySetName) && Commons.SEQUENCE.containsKey("Products")) {
productId = Commons.SEQUENCE.get("Products") + 1;
productDetailId = Commons.SEQUENCE.get(entitySetName) + 1;
@ -733,17 +733,19 @@ public abstract class AbstractUtilities {
}
Commons.SEQUENCE.put(entitySetName, productDetailId);
} else {
productId = Integer.valueOf(entry.getProperty("ProductID").getValue().asPrimitive().get());
productDetailId = Integer.valueOf(entry.getProperty("ProductDetailID").getValue().asPrimitive().get());
res = "ProductID=" + entry.getProperty("ProductID").getValue().asPrimitive().get()
+ ",ProductDetailID=" + entry.getProperty("ProductDetailID").getValue().asPrimitive().get();
productId = Integer.valueOf(entity.getProperty("ProductID").getValue().asPrimitive().get());
productDetailId = Integer.valueOf(entity.getProperty("ProductDetailID").getValue().asPrimitive().get());
res = "ProductID=" + entity.getProperty("ProductID").getValue().asPrimitive().get()
+ ",ProductDetailID=" + entity.getProperty("ProductDetailID").getValue().asPrimitive().get();
}
Commons.SEQUENCE.put(entitySetName, productDetailId);
Commons.SEQUENCE.put("Products", productId);
} else if ("PaymentInstrument".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entry, "PaymentInstrumentID");
res = getDefaultEntryKey(entitySetName, entity, "PaymentInstrumentID");
} else if ("Advertisements".equals(entitySetName)) {
res = UUID.randomUUID().toString();
} else if ("People".equals(entitySetName)) {
res = getDefaultEntryKey(entitySetName, entity, "PersonID");
} else {
throw new Exception(String.format("EntitySet '%s' not found", entitySetName));
}

View File

@ -96,6 +96,7 @@ public abstract class Commons {
SEQUENCE.put("Products", 1000);
SEQUENCE.put("ProductDetails", 1000);
SEQUENCE.put("PaymentInstrument", 10192);
SEQUENCE.put("People", 1000);
MEDIA_CONTENT.put("CustomerInfo",
new ImmutablePair<String, EdmPrimitiveTypeKind>("CustomerinfoId", EdmPrimitiveTypeKind.Int32));

View File

@ -247,6 +247,8 @@ public class DataBinder {
} else {
final EntityType entityType = entryType == null ? null : Commons.getMetadata(version).getEntityType(entryType);
if (entityType != null) {
System.out.println("ZZZZZZZZZZZZZ " + entityType + " " + jsonproperty.getName() + " "
+ entityType.getProperty(jsonproperty.getName()));
atomproperty.setType(entityType.getProperty(jsonproperty.getName()).getType());
}
}

View File

@ -20,10 +20,18 @@ package org.apache.olingo.fit.v4;
import static org.junit.Assert.assertEquals;
import org.apache.olingo.client.api.communication.request.cud.ODataDeleteRequest;
import org.apache.olingo.client.api.communication.request.cud.ODataEntityCreateRequest;
import org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest;
import org.apache.olingo.client.api.communication.request.retrieve.ODataEntitySetRequest;
import org.apache.olingo.client.api.communication.response.ODataEntityCreateResponse;
import org.apache.olingo.client.api.uri.v4.URIBuilder;
import org.apache.olingo.commons.api.domain.ODataComplexValue;
import org.apache.olingo.commons.api.domain.v4.ODataEntity;
import org.apache.olingo.commons.api.domain.v4.ODataEntitySet;
import org.apache.olingo.commons.api.domain.v4.ODataProperty;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.format.ODataPubFormat;
import org.junit.Test;
@ -64,4 +72,73 @@ public class DerivedTypeTestITCase extends AbstractTestITCase {
public void readfromJSON() {
read(ODataPubFormat.JSON_FULL_METADATA);
}
private void createDelete(final ODataPubFormat format) {
final ODataEntity customer = client.getObjectFactory().
newEntity(new FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.Customer"));
customer.getProperties().add(client.getObjectFactory().newPrimitiveProperty("PersonID",
client.getObjectFactory().newPrimitiveValueBuilder().buildInt32(976)));
customer.getProperties().add(client.getObjectFactory().newPrimitiveProperty("FirstName",
client.getObjectFactory().newPrimitiveValueBuilder().buildString("Test")));
customer.getProperties().add(client.getObjectFactory().newPrimitiveProperty("LastName",
client.getObjectFactory().newPrimitiveValueBuilder().buildString("Test")));
final ODataComplexValue<ODataProperty> homeAddress =
client.getObjectFactory().newComplexValue("Microsoft.Test.OData.Services.ODataWCFService.CompanyAddress");
homeAddress.add(client.getObjectFactory().newPrimitiveProperty("Street",
client.getObjectFactory().newPrimitiveValueBuilder().buildString("V.le Gabriele D'Annunzio")));
homeAddress.add(client.getObjectFactory().newPrimitiveProperty("City",
client.getObjectFactory().newPrimitiveValueBuilder().buildString("Pescara")));
homeAddress.add(client.getObjectFactory().newPrimitiveProperty("PostalCode",
client.getObjectFactory().newPrimitiveValueBuilder().buildString("65127")));
homeAddress.add(client.getObjectFactory().newPrimitiveProperty("CompanyName",
client.getObjectFactory().newPrimitiveValueBuilder().buildString("Tirasa")));
customer.getProperties().add(client.getObjectFactory().newComplexProperty("HomeAddress", homeAddress));
customer.getProperties().add(client.getObjectFactory().newCollectionProperty("Numbers",
client.getObjectFactory().newCollectionValue("Edm.String")));
customer.getProperties().add(client.getObjectFactory().newCollectionProperty("Emails",
client.getObjectFactory().newCollectionValue("Edm.String")));
customer.getProperties().add(client.getObjectFactory().newPrimitiveProperty("City",
client.getObjectFactory().newPrimitiveValueBuilder().buildString("Pescara")));
customer.getProperties().add(client.getObjectFactory().newPrimitiveProperty("Birthday",
client.getObjectFactory().newPrimitiveValueBuilder().
setType(EdmPrimitiveTypeKind.DateTimeOffset).setText("1977-09-08T00:00:00Z").build()));
customer.getProperties().add(client.getObjectFactory().newPrimitiveProperty("TimeBetweenLastTwoOrders",
client.getObjectFactory().newPrimitiveValueBuilder().
setType(EdmPrimitiveTypeKind.Duration).setText("PT0.0000002S").build()));
final ODataEntityCreateRequest<ODataEntity> createReq = client.getCUDRequestFactory().
getEntityCreateRequest(
client.getURIBuilder(testStaticServiceRootURL).appendEntitySetSegment("People").build(),
customer);
createReq.setFormat(format);
final ODataEntityCreateResponse<ODataEntity> createRes = createReq.execute();
assertEquals(201, createRes.getStatusCode());
final ODataEntityRequest<ODataEntity> fetchReq = client.getRetrieveRequestFactory().
getEntityRequest(createRes.getBody().getEditLink());
fetchReq.setFormat(format);
final ODataEntity actual = fetchReq.execute().getBody();
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Customer", actual.getTypeName().toString());
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.CompanyAddress",
actual.getProperty("HomeAddress").getValue().getTypeName());
final ODataDeleteRequest deleteReq = client.getCUDRequestFactory().getDeleteRequest(actual.getEditLink());
assertEquals(204, deleteReq.execute().getStatusCode());
}
@Test
public void createDeleteAsAtom() {
createDelete(ODataPubFormat.ATOM);
}
@Test
public void createDeleteAsJSON() {
createDelete(ODataPubFormat.JSON_FULL_METADATA);
}
}

View File

@ -40,8 +40,12 @@ public interface ODataPrimitiveValue extends ODataValue {
ODataPrimitiveValue buildBoolean(Boolean value);
ODataPrimitiveValue buildInt16(Short value);
ODataPrimitiveValue buildInt32(Integer value);
ODataPrimitiveValue buildInt64(Long value);
ODataPrimitiveValue buildSingle(Float value);
ODataPrimitiveValue buildDouble(Double value);

View File

@ -191,12 +191,18 @@ abstract class AbstractJsonSerializer<T> extends ODataJacksonSerializer<T> {
collection(jgen, typeInfo == null ? null : typeInfo.getFullQualifiedName().toString(), value.asCollection());
} else if (value.isComplex()) {
jgen.writeStartObject();
if (typeInfo != null) {
jgen.writeStringField(version.getJSONMap().get(ODataServiceVersion.JSON_TYPE), typeInfo.external(version));
}
for (Property property : value.asComplex().get()) {
valuable(jgen, property, property.getName());
}
if (value.isLinkedComplex()) {
links(value.asLinkedComplex(), jgen);
}
jgen.writeEndObject();
}
}

View File

@ -87,19 +87,17 @@ public class JSONEntitySerializer extends AbstractJsonSerializer<JSONEntityImpl>
valuable(jgen, property, property.getName());
}
if (serverMode) {
if (entity.getEditLink() != null && StringUtils.isNotBlank(entity.getEditLink().getHref())) {
final URI link = URI.create(entity.getEditLink().getHref());
final String editLink = link.isAbsolute() ? link.toASCIIString()
: URI.create(entity.getBaseURI() + "/" + link.toASCIIString()).normalize().toASCIIString();
if (serverMode && entity.getEditLink() != null && StringUtils.isNotBlank(entity.getEditLink().getHref())) {
final URI link = URI.create(entity.getEditLink().getHref());
final String editLink = link.isAbsolute() ? link.toASCIIString()
: URI.create(entity.getBaseURI() + "/" + link.toASCIIString()).normalize().toASCIIString();
jgen.writeStringField(
version.getJSONMap().get(ODataServiceVersion.JSON_EDIT_LINK), editLink);
if (entity.isMediaEntity()) {
jgen.writeStringField(
version.getJSONMap().get(ODataServiceVersion.JSON_EDIT_LINK), editLink);
if (entity.isMediaEntity()) {
jgen.writeStringField(
version.getJSONMap().get(ODataServiceVersion.JSON_MEDIAREAD_LINK), editLink + "/$value");
}
version.getJSONMap().get(ODataServiceVersion.JSON_MEDIAREAD_LINK), editLink + "/$value");
}
}

View File

@ -95,7 +95,7 @@ public abstract class AbstractODataPrimitiveValue extends AbstractODataValue imp
}
@Override
public AbstractODataPrimitiveValue build() {
public ODataPrimitiveValue build() {
if (getInstance().text == null && getInstance().value == null) {
throw new IllegalArgumentException("Must provide either text or value");
}
@ -137,11 +137,21 @@ public abstract class AbstractODataPrimitiveValue extends AbstractODataValue imp
return setType(EdmPrimitiveTypeKind.Boolean).setValue(value).build();
}
@Override
public ODataPrimitiveValue buildInt16(final Short value) {
return setType(EdmPrimitiveTypeKind.Int16).setValue(value).build();
}
@Override
public ODataPrimitiveValue buildInt32(final Integer value) {
return setType(EdmPrimitiveTypeKind.Int32).setValue(value).build();
}
@Override
public ODataPrimitiveValue buildInt64(final Long value) {
return setType(EdmPrimitiveTypeKind.Int64).setValue(value).build();
}
@Override
public ODataPrimitiveValue buildSingle(final Float value) {
return setType(EdmPrimitiveTypeKind.Single).setValue(value).build();