diff --git a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntitySetInvocationHandler.java b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntitySetInvocationHandler.java index 10c68f20e..209c8ea8c 100644 --- a/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntitySetInvocationHandler.java +++ b/ext/client-proxy/src/main/java/org/apache/olingo/ext/proxy/commons/EntitySetInvocationHandler.java @@ -55,6 +55,7 @@ import org.apache.olingo.ext.proxy.context.AttachedEntityStatus; import org.apache.olingo.ext.proxy.context.EntityContext; import org.apache.olingo.ext.proxy.context.EntityUUID; import org.apache.olingo.ext.proxy.utils.ClassUtils; +import org.apache.olingo.ext.proxy.utils.CoreUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -276,11 +277,29 @@ class EntitySetInvocationHandler, T ext LOG.debug("Execute query '{}'", uriBuilder.toString()); - final ODataRetrieveResponse res = - client.getRetrieveRequestFactory().getEntityRequest(uriBuilder.build()).execute(); + final CommonODataEntity entity; + final String etag; - handler = EntityTypeInvocationHandler.getInstance(res.getBody(), this, typeRef); - handler.setETag(res.getETag()); + if (isSingleton) { + final ODataRetrieveResponse res = + ((ODataClient) client).getRetrieveRequestFactory().getSingletonRequest(uri).execute(); + + entity = res.getBody(); + etag = res.getETag(); + } else { + final ODataRetrieveResponse res = + client.getRetrieveRequestFactory().getEntityRequest(uriBuilder.build()).execute(); + + etag = res.getETag(); + entity = res.getBody(); + + if (entity == null || !key.equals(CoreUtils.getKey(client, typeRef, entity))) { + throw new IllegalArgumentException("Invalid singleton " + typeRef.getSimpleName() + "(" + key + ")"); + } + } + + handler = EntityTypeInvocationHandler.getInstance(entity, this, typeRef); + handler.setETag(etag); } catch (Exception e) { LOG.info("Entity '" + uuid + "' not found", e); } diff --git a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java index d9467bc31..688675645 100644 --- a/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java +++ b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java @@ -19,27 +19,22 @@ package org.apache.olingo.fit.proxy.v4; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; -import org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.Company; -import org.apache.olingo.fit.proxy.v4.staticservice.microsoft.test.odata.services.odatawcfservice.types.CompanyCategory; import org.junit.Test; +/** + * This is the unit test class to check entity create operations. + */ public class SingletonTestITCase extends AbstractTestITCase { @Test public void read() { - final Company company = container.getCompany().get(); - assertEquals(0, company.getCompanyID(), 0); - assertEquals(CompanyCategory.IT, company.getCompanyCategory()); - } - - @Test - public void update() { - final Company company = container.getCompany().get(); - company.setRevenue(132520L); - - container.flush(); - - assertEquals(132520L, container.getCompany().get().getRevenue(), 0); + assertNotNull(container.getCompany().get(0)); + entityContext.detachAll(); + assertNotNull(container.getCompany().iterator().next()); + entityContext.detachAll(); + assertEquals(1, container.getCompany().count(), 0); + entityContext.detachAll(); } }