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 new file mode 100644 index 000000000..688675645 --- /dev/null +++ b/fit/src/test/java/org/apache/olingo/fit/proxy/v4/SingletonTestITCase.java @@ -0,0 +1,40 @@ +/* + * 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.proxy.v4; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import org.junit.Test; + +/** + * This is the unit test class to check entity create operations. + */ +public class SingletonTestITCase extends AbstractTestITCase { + + @Test + public void read() { + assertNotNull(container.getCompany().get(0)); + entityContext.detachAll(); + assertNotNull(container.getCompany().iterator().next()); + entityContext.detachAll(); + assertEquals(1, container.getCompany().count(), 0); + entityContext.detachAll(); + } +}