[OLINGO-753] Incorrect uri formed when the uri parameter contains /$count in URIUtils class

Signed-off-by: Christian Amend <christian.amend@sap.com>
This commit is contained in:
i050510 2017-04-27 15:50:00 +05:30 committed by Christian Amend
parent f55205561e
commit 74a2da7d5e
3 changed files with 335 additions and 2 deletions

View File

@ -41,6 +41,7 @@ import org.apache.olingo.client.api.domain.ClientValue;
import org.apache.olingo.client.core.uri.ParameterAlias;
import org.apache.olingo.commons.api.http.HttpStatusCode;
import org.apache.olingo.fit.tecsvc.TecSvcConst;
import org.apache.olingo.fit.util.StringHelper;
import org.junit.Test;
public class FunctionImportITCase extends AbstractParamTecSvcITCase {
@ -388,4 +389,136 @@ public class FunctionImportITCase extends AbstractParamTecSvcITCase {
parameters.put("ParameterString", getFactory().newPrimitiveValueBuilder().buildString(parameterString));
return parameters;
}
@Test
public void test1OLINGO753() throws Exception {
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
String parameterString = "1";
final ClientPrimitiveValue value = getClient().getObjectFactory().newPrimitiveValueBuilder().
buildString(parameterString);
parameters.put("ParameterString", value);
ODataInvokeRequest<ClientEntitySet> request = getClient().getInvokeRequestFactory()
.getFunctionInvokeRequest(
getClient().newURIBuilder(TecSvcConst.BASE_URI).appendEntitySetSegment("ESKeyNav").
appendOperationCallSegment("olingo.odata.test1.BFCESKeyNavRTETKeyNavParam").
appendNavigationSegment("NavPropertyETTwoKeyNavMany").build(),
ClientEntitySet.class, parameters);
assertNotNull(request);
setCookieHeader(request);
final ODataInvokeResponse<ClientEntitySet> response = request.execute();
saveCookieHeader(response);
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
final ClientEntitySet entity = response.getBody();
assertNotNull(entity);
assertEquals(3, entity.getEntities().size());
}
@Test
public void test2OLINGO753() throws Exception {
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
Short parameterInt = 1;
final ClientPrimitiveValue value = getClient().getObjectFactory().newPrimitiveValueBuilder().
buildInt16(parameterInt);
parameters.put("ParameterInt16", value);
ODataInvokeRequest<ClientProperty> request = getClient().getInvokeRequestFactory()
.getFunctionInvokeRequest(
getClient().newURIBuilder(TecSvcConst.BASE_URI).
appendOperationCallSegment("FICRTETTwoKeyNavParam").appendPropertySegment("PropertyString").
appendValueSegment().build(),
ClientProperty.class, parameters);
assertNotNull(request);
request.setAccept("text/plain");
setCookieHeader(request);
final ODataInvokeResponse<ClientProperty> response = request.execute();
saveCookieHeader(response);
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
String result = StringHelper.asString(response.getRawResponse());
assertNotNull(result);
assertEquals(2, Integer.parseInt(result));
}
@Test
public void test3OLINGO753() throws Exception {
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
String parameterString = "1";
final ClientPrimitiveValue value = getClient().getObjectFactory().newPrimitiveValueBuilder().
buildString(parameterString);
parameters.put("ParameterString", value);
ODataInvokeRequest<ClientProperty> request = getClient().getInvokeRequestFactory()
.getFunctionInvokeRequest(
getClient().newURIBuilder(TecSvcConst.BASE_URI).appendEntitySetSegment("ESKeyNav").
appendOperationCallSegment("olingo.odata.test1.BFCESKeyNavRTETKeyNavParam").
appendNavigationSegment("NavPropertyETTwoKeyNavMany").count().build(),
ClientProperty.class, parameters);
assertNotNull(request);
request.setAccept("text/plain");
setCookieHeader(request);
final ODataInvokeResponse<ClientProperty> response = request.execute();
saveCookieHeader(response);
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
String result = StringHelper.asString(response.getRawResponse());
assertNotNull(result);
assertEquals(3, Integer.parseInt(result));
}
@Test
public void test4OLINGO753() throws Exception {
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
String parameterString = "1";
final ClientPrimitiveValue value = getClient().getObjectFactory().newPrimitiveValueBuilder().
buildString(parameterString);
parameters.put("ParameterString", value);
ODataInvokeRequest<ClientProperty> request = getClient().getInvokeRequestFactory()
.getFunctionInvokeRequest(
getClient().newURIBuilder(TecSvcConst.BASE_URI).appendEntitySetSegment("ESKeyNav").
appendOperationCallSegment("olingo.odata.test1.BFCESKeyNavRTETKeyNavParam").
appendNavigationSegment("NavPropertyETTwoKeyNavMany").count().
filter("substring(PropertyString,2) eq 'am String Property 1'").build(),
ClientProperty.class, parameters);
assertNotNull(request);
request.setAccept("text/plain");
setCookieHeader(request);
final ODataInvokeResponse<ClientProperty> response = request.execute();
saveCookieHeader(response);
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
String result = StringHelper.asString(response.getRawResponse());
assertNotNull(result);
assertEquals(1, Integer.parseInt(result));
}
@Test
public void test5OLINGO753() throws Exception {
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
String parameterString = "'1'";
parameters.put("ParameterString", getFactory().newPrimitiveValueBuilder().setValue(
new ParameterAlias("first")).build());
ODataInvokeRequest<ClientProperty> request = getClient().getInvokeRequestFactory()
.getFunctionInvokeRequest(
getClient().newURIBuilder(TecSvcConst.BASE_URI).appendEntitySetSegment("ESKeyNav").
appendOperationCallSegment("olingo.odata.test1.BFCESKeyNavRTETKeyNavParam").
appendNavigationSegment("NavPropertyETTwoKeyNavMany").count().
addParameterAlias("first", parameterString).
filter("substring(PropertyString,2) eq 'am String Property 1'").build(),
ClientProperty.class, parameters);
assertNotNull(request);
request.setAccept("text/plain");
setCookieHeader(request);
final ODataInvokeResponse<ClientProperty> response = request.execute();
saveCookieHeader(response);
assertEquals(HttpStatusCode.OK.getStatusCode(), response.getStatusCode());
String result = StringHelper.asString(response.getRawResponse());
assertNotNull(result);
assertEquals(1, Integer.parseInt(result));
}
}

View File

@ -74,6 +74,7 @@ public final class URIUtils {
// private static final Logger LOG = LoggerFactory.getLogger(URIUtils.class);
private static final Pattern ENUM_VALUE = Pattern.compile("(.+\\.)?.+'.+'");
private static final String URI_OPTIONS = "/$";
private URIUtils() {
// Empty private constructor for static utility classes
@ -325,11 +326,34 @@ public final class URIUtils {
public static URI buildFunctionInvokeURI(final URI uri, final Map<String, ClientValue> parameters) {
final String rawQuery = uri.getRawQuery();
String baseURI = StringUtils.substringBefore(uri.toASCIIString(), "?" + rawQuery);
String baseURI = null;
String uriOption = "";
String pathSegments = null;
// Check if Query contains /$ and extract options like /$count, /$value and /$ref
if (uri.toASCIIString().indexOf(URI_OPTIONS) != -1) {
uriOption = uri.toASCIIString().substring(uri.toASCIIString().indexOf(URI_OPTIONS),
(rawQuery == null ? uri.toASCIIString().length() : uri.toASCIIString().indexOf(rawQuery) - 1));
}
if (rawQuery != null) {
baseURI = StringUtils.substringBefore(uri.toASCIIString(), uriOption + "?" + rawQuery);
} else if (uriOption.length() > 0) {
baseURI = StringUtils.substringBefore(uri.toASCIIString(), uriOption);
} else {
baseURI = StringUtils.substringBefore(uri.toASCIIString(), null);
}
if (baseURI.endsWith("()")) {
baseURI = baseURI.substring(0, baseURI.length() - 2);
} else {
/**
* If FunctionName is followed by a Navigation segment or Actions,
* then get the substring till function name so that parameters can be appended to it.
*/
int bracIndex = baseURI.indexOf("()");
if (bracIndex != -1) {
pathSegments = baseURI.substring(bracIndex + 2);
baseURI = baseURI.substring(0, bracIndex);
}
}
final StringBuilder inlineParams = new StringBuilder();
for (Map.Entry<String, ClientValue> param : parameters.entrySet()) {
inlineParams.append(param.getKey()).append("=");
@ -353,6 +377,8 @@ public final class URIUtils {
}
return URI.create(baseURI + "(" + Encoder.encode(inlineParams.toString()) + ")"
+ (pathSegments == null ? StringUtils.EMPTY : pathSegments)
+ (!uriOption.equals(StringUtils.EMPTY) ? "/" + Encoder.encode(uriOption.substring(1)) : StringUtils.EMPTY)
+ (StringUtils.isNotBlank(rawQuery) ? "?" + rawQuery : StringUtils.EMPTY));
}
}

View File

@ -19,16 +19,23 @@
package org.apache.olingo.client.core.uri;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.olingo.client.api.ODataClient;
import org.apache.olingo.client.api.domain.ClientPrimitiveValue;
import org.apache.olingo.client.api.domain.ClientValue;
import org.apache.olingo.client.api.serialization.ODataDeserializerException;
import org.apache.olingo.client.api.uri.QueryOption;
import org.apache.olingo.client.api.uri.URIBuilder;
import org.apache.olingo.client.core.AbstractTest;
import org.apache.olingo.client.core.ODataClientFactory;
import org.junit.Test;
public class URIBuilderTest extends AbstractTest {
@ -260,4 +267,171 @@ public class URIBuilderTest extends AbstractTest {
assertEquals(new URI("http://host/service/Products?%24search=blue%20OR%20green"), uriBuilder.build());
}
@Test
public void test1OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).
appendOperationCallSegment("functionName").count().build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().buildString("parameterValue");
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/functionName(parameterName%3D'parameterValue')"
+ "/%24count", newUri.toASCIIString());
}
@Test
public void test2OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).appendOperationCallSegment("functionName").
filter("paramName eq 1").format("json").count().build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().buildString("parameterValue");
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/functionName(parameterName%3D'parameterValue')"
+ "/%24count?%24filter=paramName%20eq%201&%24format=json", newUri.toASCIIString());
}
@Test
public void test3OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).appendOperationCallSegment("functionName").
filter("paramName eq 1").format("json").build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().buildString("parameterValue");
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/functionName(parameterName%3D'parameterValue')"
+ "?%24filter=paramName%20eq%201&%24format=json", newUri.toASCIIString());
}
@Test
public void test4OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
appendOperationCallSegment("functionName").count().build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().buildString("parameterValue");
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/EntitySet/functionName(parameterName%3D'parameterValue')"
+ "/%24count", newUri.toASCIIString());
}
@Test
public void test5OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
appendOperationCallSegment("functionName").count().filter("PropertyString eq '1'").build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().buildString("parameterValue");
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/EntitySet/functionName(parameterName%3D'parameterValue')"
+ "/%24count?%24filter=PropertyString%20eq%20'1'", newUri.toASCIIString());
}
@Test
public void test6OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).
appendOperationCallSegment("functionName").count().filter("PropertyString eq '1'").build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/functionName()"
+ "/%24count?%24filter=PropertyString%20eq%20'1'", newUri.toASCIIString());
}
@Test
public void test7OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
appendOperationCallSegment("functionName").filter("PropertyString eq '1'").
appendNavigationSegment("NavSeg").count().build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().buildString("parameterValue");
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/EntitySet/functionName(parameterName%3D'parameterValue')/NavSeg"
+ "/%24count?%24filter=PropertyString%20eq%20'1'", newUri.toASCIIString());
}
@Test
public void test8OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
appendOperationCallSegment("functionName").filter("PropertyString eq '1'").
appendNavigationSegment("NavSeg").appendActionCallSegment("ActionName").count().build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().buildString("parameterValue");
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/EntitySet/functionName(parameterName%3D'parameterValue')/NavSeg/ActionName"
+ "/%24count?%24filter=PropertyString%20eq%20'1'", newUri.toASCIIString());
}
@Test
public void test9OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
appendOperationCallSegment("functionName").
appendNavigationSegment("NavSeg").appendActionCallSegment("ActionName").appendValueSegment().build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().buildString("parameterValue");
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/EntitySet/functionName(parameterName%3D'parameterValue')"
+ "/NavSeg/ActionName/%24value", newUri.toASCIIString());
}
@Test
public void test10OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
appendOperationCallSegment("functionName").
appendNavigationSegment("NavSeg").appendRefSegment().build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().buildString("parameterValue");
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/EntitySet/functionName(parameterName%3D'parameterValue')"
+ "/NavSeg/%24ref", newUri.toASCIIString());
}
@Test
public void test11OLINGO753() throws ODataDeserializerException {
final ODataClient client = ODataClientFactory.getClient();
final URI uri = client.newURIBuilder(SERVICE_ROOT).appendEntitySetSegment("EntitySet").
appendOperationCallSegment("functionName").
appendNavigationSegment("NavSeg").count().addParameterAlias("first", "'1'").build();
final Map<String, ClientValue> parameters = new HashMap<String, ClientValue>();
final ClientPrimitiveValue value = client.getObjectFactory().
newPrimitiveValueBuilder().setValue(new ParameterAlias("first")).build();
parameters.put("parameterName", value);
URI newUri = URIUtils.buildFunctionInvokeURI(uri, parameters);
assertNotNull(newUri);
assertEquals("http://host/service/EntitySet/functionName(parameterName%3D%40first)/"
+ "NavSeg/%24count?%40first='1'", newUri.toASCIIString());
}
}