Adding singleton tests

This commit is contained in:
Francesco Chicchiriccò 2014-05-08 17:30:40 +02:00
parent 6ecb02643a
commit c82e021278
14 changed files with 213 additions and 16 deletions

View File

@ -34,6 +34,7 @@ import java.util.UUID;
import java.util.regex.Pattern;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
@ -42,8 +43,10 @@ import javax.ws.rs.NotFoundException;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriInfo;
@ -346,6 +349,21 @@ public class V4Services extends AbstractServices {
uriInfo.getRequestUri().toASCIIString(), accept, "Company", StringUtils.EMPTY, format, null, null, false);
}
@PATCH
@Path("/Company")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_JSON})
@Consumes({MediaType.APPLICATION_ATOM_XML, MediaType.APPLICATION_JSON})
public Response patchSingletonCompany(
@Context UriInfo uriInfo,
@HeaderParam("Accept") @DefaultValue(StringUtils.EMPTY) String accept,
@HeaderParam("Content-Type") @DefaultValue(StringUtils.EMPTY) String contentType,
@HeaderParam("Prefer") @DefaultValue(StringUtils.EMPTY) String prefer,
@HeaderParam("If-Match") @DefaultValue(StringUtils.EMPTY) String ifMatch,
final String changes) {
return super.patchEntity(uriInfo, accept, contentType, prefer, ifMatch, "Company", StringUtils.EMPTY, changes);
}
@GET
@Path("/Customers")
public Response getCustomers(

View File

@ -37,6 +37,7 @@ 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.ODataEnumValue;
import org.apache.olingo.commons.api.domain.v4.ODataProperty;
import org.apache.olingo.commons.api.domain.v4.Singleton;
import org.apache.olingo.commons.api.edm.Edm;
import org.apache.olingo.commons.api.edm.EdmAction;
import org.apache.olingo.commons.api.edm.EdmEntityContainer;
@ -64,16 +65,16 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
// GetEmployeesCount
URIBuilder builder = getClient().getURIBuilder(testStaticServiceRootURL).appendSingletonSegment("Company");
ODataEntityRequest<ODataEntity> entityReq =
getClient().getRetrieveRequestFactory().getEntityRequest(builder.build());
entityReq.setFormat(format);
ODataEntity entity = entityReq.execute().getBody();
assertNotNull(entity);
final ODataEntityRequest<Singleton> singletonReq =
getClient().getRetrieveRequestFactory().getSingletonRequest(builder.build());
singletonReq.setFormat(format);
final Singleton company = singletonReq.execute().getBody();
assertNotNull(company);
ODataOperation boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetEmployeesCount");
ODataOperation boundOp = company.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetEmployeesCount");
assertNotNull(boundOp);
EdmFunction func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()), entity.getTypeName(),
EdmFunction func = edm.getBoundFunction(new FullQualifiedName(boundOp.getTitle()), company.getTypeName(),
false, null);
assertNotNull(func);
@ -87,9 +88,10 @@ public class BoundOperationInvokeTestITCase extends AbstractTestITCase {
// GetProductDetails
builder = getClient().getURIBuilder(testStaticServiceRootURL).
appendEntitySetSegment("Products").appendKeySegment(5);
entityReq = getClient().getRetrieveRequestFactory().getEntityRequest(builder.build());
ODataEntityRequest<ODataEntity> entityReq = getClient().getRetrieveRequestFactory().
getEntityRequest(builder.build());
entityReq.setFormat(format);
entity = entityReq.execute().getBody();
ODataEntity entity = entityReq.execute().getBody();
assertNotNull(entity);
boundOp = entity.getOperation("Microsoft.Test.OData.Services.ODataWCFService.GetProductDetails");

View File

@ -47,6 +47,7 @@ import org.apache.olingo.client.api.uri.v4.URIBuilder;
import org.apache.olingo.client.api.v4.ODataClient;
import org.apache.olingo.client.core.ODataClientFactory;
import org.apache.olingo.commons.api.domain.v4.ODataEntity;
import org.apache.olingo.commons.api.domain.v4.ODataValuable;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeKind;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
@ -65,8 +66,9 @@ public class MediaEntityTestITCase extends AbstractTestITCase {
final ODataEntity entity = entityReq.execute().getBody();
assertNotNull(entity);
assertTrue(entity.isMediaEntity());
// cast to workaround JDK 6 bug, fixed in JDK 7
assertEquals(EdmPrimitiveTypeKind.DateTimeOffset.getFullQualifiedName().toString(),
entity.getProperty("AirDate").getValue().getTypeName());
((ODataValuable) entity.getProperty("AirDate")).getValue().getTypeName());
final ODataMediaRequest streamReq = client.getRetrieveRequestFactory().
getMediaRequest(entity.getMediaContentSource());

View File

@ -31,6 +31,7 @@ import org.apache.olingo.client.api.http.HttpMethod;
import org.apache.olingo.client.api.uri.v4.URIBuilder;
import org.apache.olingo.client.api.v4.ODataClient;
import org.apache.olingo.commons.api.domain.v4.ODataProperty;
import org.apache.olingo.commons.api.domain.v4.ODataValuable;
import org.apache.olingo.commons.api.format.ODataFormat;
import org.junit.Test;
@ -45,7 +46,9 @@ public class PropertyTestITCase extends AbstractTestITCase {
final ODataProperty prop = req.execute().getBody();
assertNotNull(prop);
assertEquals("Collection(Microsoft.Test.OData.Services.ODataWCFService.Color)", prop.getValue().getTypeName());
// cast to workaround JDK 6 bug, fixed in JDK 7
assertEquals("Collection(Microsoft.Test.OData.Services.ODataWCFService.Color)",
((ODataValuable) prop).getValue().getTypeName());
}
@Test
@ -72,7 +75,8 @@ public class PropertyTestITCase extends AbstractTestITCase {
final ODataProperty prop = req.execute().getBody();
assertNotNull(prop);
assertEquals("Edm.GeographyPoint", prop.getValue().getTypeName());
// cast to workaround JDK 6 bug, fixed in JDK 7
assertEquals("Edm.GeographyPoint", ((ODataValuable) prop).getValue().getTypeName());
}
@Test
@ -99,7 +103,9 @@ public class PropertyTestITCase extends AbstractTestITCase {
final ODataProperty prop = req.execute().getBody();
assertNotNull(prop);
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Address", prop.getValue().getTypeName());
// cast to workaround JDK 6 bug, fixed in JDK 7
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.Address",
((ODataValuable) prop).getValue().getTypeName());
}
@Test

View File

@ -29,6 +29,7 @@ import org.apache.olingo.client.api.communication.request.retrieve.ODataValueReq
import org.apache.olingo.client.api.uri.v4.URIBuilder;
import org.apache.olingo.commons.api.domain.ODataPrimitiveValue;
import org.apache.olingo.commons.api.domain.v4.ODataProperty;
import org.apache.olingo.commons.api.domain.v4.ODataValuable;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.api.format.ODataFormat;
@ -130,7 +131,8 @@ public class PropertyValueTestITCase extends AbstractTestITCase {
getPropertyRequest(uriBuilder.build());
req.setFormat(ODataFormat.XML);
final ODataProperty property = req.execute().getBody();
assertTrue(property.getValue().isCollection());
// cast to workaround JDK 6 bug, fixed in JDK 7
assertTrue(((ODataValuable)property).getValue().isCollection());
assertEquals("555-555-5555", property.getCollectionValue().iterator().next().asPrimitive().toString());
}

View File

@ -0,0 +1,102 @@
/*
* 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.olingo.fit.v4;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.net.URI;
import org.apache.olingo.client.api.communication.request.cud.ODataEntityUpdateRequest;
import org.apache.olingo.client.api.communication.request.cud.v4.UpdateType;
import org.apache.olingo.client.api.communication.request.retrieve.ODataEntityRequest;
import org.apache.olingo.client.api.communication.response.ODataEntityUpdateResponse;
import org.apache.olingo.client.api.uri.v4.URIBuilder;
import org.apache.olingo.client.api.v4.ODataClient;
import org.apache.olingo.commons.api.domain.v4.ODataValuable;
import org.apache.olingo.commons.api.domain.v4.Singleton;
import org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException;
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;
public class SingletonTestITCase extends AbstractTestITCase {
private void read(final ODataClient client, final ODataPubFormat format) throws EdmPrimitiveTypeException {
final URIBuilder builder = client.getURIBuilder(testStaticServiceRootURL).appendSingletonSegment("Company");
final ODataEntityRequest<Singleton> singleton =
client.getRetrieveRequestFactory().getSingletonRequest(builder.build());
singleton.setFormat(format);
final Singleton company = singleton.execute().getBody();
assertNotNull(company);
assertEquals(0, company.getProperty("CompanyID").getPrimitiveValue().toCastValue(Integer.class), 0);
// cast to workaround JDK 6 bug, fixed in JDK 7
assertEquals("Microsoft.Test.OData.Services.ODataWCFService.CompanyCategory",
((ODataValuable) company.getProperty("CompanyCategory")).getValue().getTypeName());
assertTrue(company.getProperty("CompanyCategory").hasEnumValue());
}
@Test
public void readfromAtom() throws EdmPrimitiveTypeException {
read(client, ODataPubFormat.ATOM);
}
@Test
public void readfromJSON() throws EdmPrimitiveTypeException {
read(edmClient, ODataPubFormat.JSON);
}
@Test
public void readfromJSONFull() throws EdmPrimitiveTypeException {
read(client, ODataPubFormat.JSON_FULL_METADATA);
}
private void update(final ODataPubFormat format) throws EdmPrimitiveTypeException {
final Singleton changes = getClient().getObjectFactory().newSingleton(
new FullQualifiedName("Microsoft.Test.OData.Services.ODataWCFService.Company"));
changes.getProperties().add(getClient().getObjectFactory().newPrimitiveProperty("Revenue",
getClient().getObjectFactory().newPrimitiveValueBuilder().
setType(EdmPrimitiveTypeKind.Int64).setText("132520").build()));
final URI uri = client.getURIBuilder(testStaticServiceRootURL).appendSingletonSegment("Company").build();
final ODataEntityUpdateRequest<Singleton> req = getClient().getCUDRequestFactory().
getSingletonUpdateRequest(uri, UpdateType.PATCH, changes);
req.setFormat(format);
final ODataEntityUpdateResponse<Singleton> res = req.execute();
assertEquals(204, res.getStatusCode());
final Singleton updated = getClient().getRetrieveRequestFactory().getSingletonRequest(uri).execute().getBody();
assertNotNull(updated);
assertEquals(132520, updated.getProperty("Revenue").getPrimitiveValue().toCastValue(Integer.class), 0);
}
@Test
public void atomUpdate() throws EdmPrimitiveTypeException {
update(ODataPubFormat.ATOM);
}
@Test
public void jsonUpdate() throws EdmPrimitiveTypeException {
update(ODataPubFormat.JSON);
}
}

View File

@ -18,7 +18,15 @@
*/
package org.apache.olingo.client.api.communication.request.cud.v4;
import java.net.URI;
import org.apache.olingo.client.api.communication.request.cud.CommonCUDRequestFactory;
import org.apache.olingo.client.api.communication.request.cud.ODataEntityUpdateRequest;
import org.apache.olingo.commons.api.domain.v4.Singleton;
public interface CUDRequestFactory extends CommonCUDRequestFactory<UpdateType> {
ODataEntityUpdateRequest<Singleton> getSingletonUpdateRequest(URI targetURI, UpdateType type, Singleton changes);
ODataEntityUpdateRequest<Singleton> getSingletonUpdateRequest(UpdateType type, Singleton entity);
}

View File

@ -27,6 +27,7 @@ import org.apache.olingo.client.api.communication.request.retrieve.ODataProperty
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.domain.v4.Singleton;
@SuppressWarnings("unchecked")
public interface RetrieveRequestFactory extends CommonRetrieveRequestFactory {
@ -37,11 +38,13 @@ public interface RetrieveRequestFactory extends CommonRetrieveRequestFactory {
@Override
ODataEntitySetIteratorRequest<ODataEntitySet, ODataEntity> getEntitySetIteratorRequest(URI uri);
ODataEntityRequest<Singleton> getSingletonRequest(URI uri);
@Override
ODataEntityRequest<ODataEntity> getEntityRequest(URI uri);
@Override
ODataPropertyRequest<ODataProperty> getPropertyRequest(URI uri);
ODataDeltaRequest getDeltaRequest(URI uri);
}

View File

@ -18,10 +18,13 @@
*/
package org.apache.olingo.client.core.communication.request.cud.v4;
import java.net.URI;
import org.apache.olingo.client.api.communication.request.cud.ODataEntityUpdateRequest;
import org.apache.olingo.client.api.v4.ODataClient;
import org.apache.olingo.client.api.communication.request.cud.v4.CUDRequestFactory;
import org.apache.olingo.client.api.communication.request.cud.v4.UpdateType;
import org.apache.olingo.client.core.communication.request.cud.AbstractCUDRequestFactory;
import org.apache.olingo.commons.api.domain.v4.Singleton;
public class CUDRequestFactoryImpl extends AbstractCUDRequestFactory<UpdateType>
implements CUDRequestFactory {
@ -32,4 +35,18 @@ public class CUDRequestFactoryImpl extends AbstractCUDRequestFactory<UpdateType>
super(client);
}
@Override
public ODataEntityUpdateRequest<Singleton> getSingletonUpdateRequest(
final UpdateType type, final Singleton entity) {
return super.getEntityUpdateRequest(type, entity);
}
@Override
public ODataEntityUpdateRequest<Singleton> getSingletonUpdateRequest(
final URI targetURI, final UpdateType type, final Singleton changes) {
return super.getEntityUpdateRequest(targetURI, type, changes);
}
}

View File

@ -35,6 +35,7 @@ import org.apache.olingo.client.core.communication.request.retrieve.ODataPropert
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.domain.v4.Singleton;
@SuppressWarnings("unchecked")
public class RetrieveRequestFactoryImpl extends AbstractRetrieveRequestFactory
@ -62,6 +63,11 @@ public class RetrieveRequestFactoryImpl extends AbstractRetrieveRequestFactory
return new ODataEntitySetIteratorRequestImpl<ODataEntitySet, ODataEntity>(client, uri);
}
@Override
public ODataEntityRequest<Singleton> getSingletonRequest(final URI uri) {
return new ODataEntityRequestImpl<Singleton>(client, uri);
}
@Override
public ODataEntityRequest<ODataEntity> getEntityRequest(final URI uri) {
return new ODataEntityRequestImpl<ODataEntity>(client, uri);

View File

@ -40,6 +40,8 @@ public interface ODataObjectFactory extends CommonODataObjectFactory {
@Override
ODataEntity newEntity(FullQualifiedName typeName, URI link);
Singleton newSingleton(FullQualifiedName typeName);
@Override
ODataLink newEntitySetNavigationLink(String name, URI link);

View File

@ -0,0 +1,22 @@
/*
* 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.olingo.commons.api.domain.v4;
public interface Singleton extends ODataEntity {
}

View File

@ -23,10 +23,11 @@ import java.util.List;
import org.apache.olingo.commons.api.domain.v4.ODataAnnotation;
import org.apache.olingo.commons.api.domain.v4.ODataEntity;
import org.apache.olingo.commons.api.domain.v4.ODataProperty;
import org.apache.olingo.commons.api.domain.v4.Singleton;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.core.domain.AbstractODataEntity;
public class ODataEntityImpl extends AbstractODataEntity implements ODataEntity {
public class ODataEntityImpl extends AbstractODataEntity implements ODataEntity, Singleton {
private static final long serialVersionUID = -3997704808753685990L;

View File

@ -33,6 +33,7 @@ import org.apache.olingo.commons.api.domain.v4.ODataLink;
import org.apache.olingo.commons.api.domain.v4.ODataLinkedComplexValue;
import org.apache.olingo.commons.api.domain.v4.ODataProperty;
import org.apache.olingo.commons.api.domain.v4.ODataValue;
import org.apache.olingo.commons.api.domain.v4.Singleton;
import org.apache.olingo.commons.api.edm.FullQualifiedName;
import org.apache.olingo.commons.api.edm.constants.ODataServiceVersion;
import org.apache.olingo.commons.core.domain.AbstractODataObjectFactory;
@ -65,6 +66,11 @@ public class ODataObjectFactoryImpl extends AbstractODataObjectFactory implement
return result;
}
@Override
public Singleton newSingleton(final FullQualifiedName typeName) {
return new ODataEntityImpl(typeName);
}
@Override
public ODataLink newEntityNavigationLink(final String name, final URI link) {
return new ODataLink.Builder().setVersion(version).setURI(link).