Repository: olingo-odata4 Updated Branches: refs/heads/master bf4e2017f -> 1a739d46b
[OLINGO-1191] More Code Improvements Project: http://git-wip-us.apache.org/repos/asf/olingo-odata4/repo Commit: http://git-wip-us.apache.org/repos/asf/olingo-odata4/commit/1a739d46 Tree: http://git-wip-us.apache.org/repos/asf/olingo-odata4/tree/1a739d46 Diff: http://git-wip-us.apache.org/repos/asf/olingo-odata4/diff/1a739d46 Branch: refs/heads/master Commit: 1a739d46bf82d918eb6086714a87341af1ebca95 Parents: bf4e201 Author: ramya vasanth <ramya.vasa...@sap.com> Authored: Thu Mar 15 08:12:10 2018 +0530 Committer: ramya vasanth <ramya.vasa...@sap.com> Committed: Thu Mar 15 08:12:10 2018 +0530 ---------------------------------------------------------------------- .../api/edm/constants/ODataServiceVersion.java | 2 +- .../commons/api/format/AcceptCharset.java | 5 +- .../AcceptHeaderContentNegotiatorException.java | 6 --- .../olingo/server/core/ContentNegotiator.java | 6 +-- .../core/serializer/utils/ContextURLHelper.java | 40 ++------------- .../serializer/utils/ExpandSelectHelper.java | 8 +-- .../core/serializer/ExpandSelectMock.java | 45 +++++++++++++++++ .../json/ODataJsonSerializerTest.java | 20 ++++++++ .../serializer/utils/ContextURLHelperTest.java | 53 ++++++++++++++++++++ 9 files changed, 134 insertions(+), 51 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1a739d46/lib/commons-api/src/main/java/org/apache/olingo/commons/api/edm/constants/ODataServiceVersion.java ---------------------------------------------------------------------- diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/edm/constants/ODataServiceVersion.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/edm/constants/ODataServiceVersion.java index 1ef8929..4f6335b 100644 --- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/edm/constants/ODataServiceVersion.java +++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/edm/constants/ODataServiceVersion.java @@ -92,7 +92,7 @@ public enum ODataServiceVersion { final double version401 = Double.parseDouble(extractDataServiceVersionString(ODataServiceVersion.V401.toString())); final double other = Double.parseDouble(extractDataServiceVersionString(value)); - return (other == version4) || (other == version401); + return (Double.compare(other, version4) == 0) || (Double.compare(other, version401) == 0); } public static boolean isValidMaxODataVersion(String value) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1a739d46/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptCharset.java ---------------------------------------------------------------------- diff --git a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptCharset.java b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptCharset.java index 85eaf91..7d19245 100644 --- a/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptCharset.java +++ b/lib/commons-api/src/main/java/org/apache/olingo/commons/api/format/AcceptCharset.java @@ -34,6 +34,8 @@ public class AcceptCharset { private final String charset; private final Map<String, String> parameters; private final Float quality; + private static final String UTF8_CHARSET = "utf8"; + private static final String UTF8_CHARSET1 = "utf-8"; private AcceptCharset(final String charset) { parameters = TypeUtil.createParameterMap(); @@ -47,7 +49,8 @@ public class AcceptCharset { } catch (UnsupportedCharsetException e) { throw new UnsupportedCharsetException("Illegal charset in accept charset header:" + this.charset); } - if (!(this.charset.equalsIgnoreCase("utf8")) && !(this.charset.equalsIgnoreCase("utf-8"))) { + if (!(UTF8_CHARSET.equalsIgnoreCase(this.charset)) && + !(UTF8_CHARSET1.equalsIgnoreCase(this.charset))) { throw new IllegalArgumentException("Unsupported charset in accept charset header:" + this.charset); } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1a739d46/lib/server-core/src/main/java/org/apache/olingo/server/core/AcceptHeaderContentNegotiatorException.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/AcceptHeaderContentNegotiatorException.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/AcceptHeaderContentNegotiatorException.java index e051560..280da32 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/AcceptHeaderContentNegotiatorException.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/AcceptHeaderContentNegotiatorException.java @@ -40,12 +40,6 @@ public class AcceptHeaderContentNegotiatorException extends ContentNegotiatorExc super(developmentMessage, messageKey, parameters); } - public AcceptHeaderContentNegotiatorException(final String developmentMessage, final Throwable cause, - final MessageKey messageKey, - final String... parameters) { - super(developmentMessage, cause, messageKey, parameters); - } - @Override protected String getBundleName() { return DEFAULT_SERVER_BUNDLE_NAME; http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1a739d46/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java index 4ece2b8..a451088 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java @@ -41,6 +41,7 @@ public final class ContentNegotiator { private static final String APPLICATION_JSON = "application/json"; private static final String XML = "xml"; private static final String METADATA = "METADATA"; + private static final String COLON = ":"; private static final List<ContentType> DEFAULT_SUPPORTED_CONTENT_TYPES = Collections.unmodifiableList(Arrays.asList( @@ -95,16 +96,15 @@ public final class ContentNegotiator { List<AcceptCharset> charsets = null; if (acceptCharset != null) { try { - charsets = new ArrayList<AcceptCharset>(); charsets = AcceptCharset.create(acceptCharset); } catch (UnsupportedCharsetException e) { throw new AcceptHeaderContentNegotiatorException(e.getMessage(), AcceptHeaderContentNegotiatorException.MessageKeys.UNSUPPORTED_ACCEPT_CHARSET_HEADER_OPTIONS, - e.getMessage().substring(e.getMessage().lastIndexOf(":") + 1)); + e.getMessage().substring(e.getMessage().lastIndexOf(COLON) + 1)); } catch (IllegalArgumentException e) { throw new ContentNegotiatorException(e.getMessage(), ContentNegotiatorException.MessageKeys.UNSUPPORTED_ACCEPT_CHARSET, - e.getMessage().substring(e.getMessage().lastIndexOf(":") + 1)); + e.getMessage().substring(e.getMessage().lastIndexOf(COLON) + 1)); } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1a739d46/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelper.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelper.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelper.java index c2efcdd..10e21b6 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelper.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelper.java @@ -85,7 +85,7 @@ public final class ContextURLHelper { for (final String propertyName : type.getNavigationPropertyNames()) { constructSelectItemList(type, result, selectItems, selectedPropertyNames, propertyName); } - if ((result.toString().length() == 0 && selectItems.size() > 0) || + if ((result.toString().length() == 0 && !selectItems.isEmpty()) || (result.toString().split(",").length < selectItems.size())) { constructSelectItemListForActionsAndFunctions(type, result, selectItems); } @@ -113,7 +113,7 @@ public final class ContextURLHelper { if (result.length() > 0) { result.append(','); } - result.append(Encoder.encode(action.getName())); + result.append(Encoder.encode(action.getFullQualifiedName().getFullQualifiedNameAsString())); } } } else if (resource instanceof UriResourceFunction) { @@ -126,7 +126,7 @@ public final class ContextURLHelper { if (result.length() > 0) { result.append(','); } - result.append(Encoder.encode(function.getName())); + result.append(Encoder.encode(function.getFullQualifiedName().getFullQualifiedNameAsString())); } } } @@ -229,7 +229,7 @@ public final class ContextURLHelper { } } else if (part instanceof UriResourceNavigation && ((UriResourceNavigation) part).getProperty().getName().equalsIgnoreCase(propertyName)) { - return getNavigationPropertyPosition(selectedPaths, (UriResourceNavigation)part); + return -1; } i++; } @@ -240,38 +240,6 @@ public final class ContextURLHelper { /** * @param selectedPaths * @param part - * @return - */ - private static int getNavigationPropertyPosition(Set<List<String>> selectedPaths, UriResourceNavigation part) { - if (part.getTypeFilterOnCollection() != null) { - for (List<String> pathSel : selectedPaths) { - int i = 0; - for (String sel : pathSel) { - if (sel.equalsIgnoreCase(part.getTypeFilterOnCollection(). - getFullQualifiedName().getFullQualifiedNameAsString())) { - return i; - } - i++; - } - } - } else if (part.getTypeFilterOnEntry() != null) { - for (List<String> pathSel : selectedPaths) { - int i = 0; - for (String sel : pathSel) { - if (sel.equalsIgnoreCase(part.getTypeFilterOnEntry(). - getFullQualifiedName().getFullQualifiedNameAsString())) { - return i; - } - i++; - } - } - } - return -1; - } - - /** - * @param selectedPaths - * @param part */ private static int getComplexPropertyPosition(Set<List<String>> selectedPaths, UriResourceComplexProperty part) { for (List<String> pathSel : selectedPaths) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1a739d46/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ExpandSelectHelper.java ---------------------------------------------------------------------- diff --git a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ExpandSelectHelper.java b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ExpandSelectHelper.java index 25897e3..2fe3b4f 100644 --- a/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ExpandSelectHelper.java +++ b/lib/server-core/src/main/java/org/apache/olingo/server/core/serializer/utils/ExpandSelectHelper.java @@ -75,9 +75,9 @@ public abstract class ExpandSelectHelper { /** * This method creates selectedPath list checking if the resource has entity type filter, * complex type filter, or if resource is navigation property and if it has type filter - * @param selectItems - * @param propertyName - * @return + * @param selectItems items in the select clause + * @param propertyName propertyName + * @return Set<List<String>> return a list of selected paths */ public static Set<List<String>> getSelectedPathsWithTypeCasts( final List<SelectItem> selectItems, final String propertyName) { @@ -174,7 +174,7 @@ public abstract class ExpandSelectHelper { final List<UriResource> parts = item.getResourcePath().getUriResourceParts(); final UriResource resource = parts.get(0); if (resource instanceof UriResourceProperty) { - if (parts.size() > 0) { + if (!parts.isEmpty()) { List<String> path = new ArrayList<String>(); for (final UriResource part : parts.subList(0, parts.size())) { if (part instanceof UriResourceProperty) { http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1a739d46/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/ExpandSelectMock.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/ExpandSelectMock.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/ExpandSelectMock.java index 4cd6a0e..a890966 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/ExpandSelectMock.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/ExpandSelectMock.java @@ -22,9 +22,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.olingo.commons.api.edm.EdmAction; import org.apache.olingo.commons.api.edm.EdmComplexType; import org.apache.olingo.commons.api.edm.EdmElement; import org.apache.olingo.commons.api.edm.EdmEntitySet; +import org.apache.olingo.commons.api.edm.EdmFunction; import org.apache.olingo.commons.api.edm.EdmNavigationProperty; import org.apache.olingo.commons.api.edm.EdmProperty; import org.apache.olingo.commons.api.edm.EdmStructuredType; @@ -32,7 +34,9 @@ import org.apache.olingo.commons.api.edm.EdmType; import org.apache.olingo.commons.api.edm.constants.EdmTypeKind; import org.apache.olingo.server.api.uri.UriInfoResource; import org.apache.olingo.server.api.uri.UriResource; +import org.apache.olingo.server.api.uri.UriResourceAction; import org.apache.olingo.server.api.uri.UriResourceComplexProperty; +import org.apache.olingo.server.api.uri.UriResourceFunction; import org.apache.olingo.server.api.uri.UriResourceNavigation; import org.apache.olingo.server.api.uri.UriResourceProperty; import org.apache.olingo.server.api.uri.queryoption.ExpandItem; @@ -95,6 +99,47 @@ public final class ExpandSelectMock { return resource; } + public static SelectItem mockSelectItemHavingAction(final EdmEntitySet edmEntitySet, + final EdmAction action) { + final UriInfoResource resource = mockResourceOnAction( + edmEntitySet, action); + SelectItem selectItem = Mockito.mock(SelectItem.class); + Mockito.when(selectItem.getResourcePath()).thenReturn(resource); + return selectItem; + } + + public static SelectItem mockSelectItemHavingFunction(final EdmEntitySet edmEntitySet, + final EdmFunction function) { + final UriInfoResource resource = mockResourceOnFunction( + edmEntitySet, function); + SelectItem selectItem = Mockito.mock(SelectItem.class); + Mockito.when(selectItem.getResourcePath()).thenReturn(resource); + return selectItem; + } + + private static UriInfoResource mockResourceOnAction( + EdmEntitySet edmEntitySet, EdmAction action) { + List<UriResource> elements = new ArrayList<UriResource>(); + UriResourceAction element = Mockito.mock(UriResourceAction.class); + Mockito.when(element.getAction()).thenReturn(action); + elements.add(element); + + UriInfoResource resource = Mockito.mock(UriInfoResource.class); + Mockito.when(resource.getUriResourceParts()).thenReturn(elements); + return resource; + } + + private static UriInfoResource mockResourceOnFunction(EdmEntitySet edmEntitySet, EdmFunction function) { + UriResourceFunction element = Mockito.mock(UriResourceFunction.class); + Mockito.when(element.getFunction()).thenReturn(function); + List<UriResource> elements = new ArrayList<UriResource>(); + elements.add(element); + + UriInfoResource resource = Mockito.mock(UriInfoResource.class); + Mockito.when(resource.getUriResourceParts()).thenReturn(elements); + return resource; + } + /** * @param navProperty * @param elements http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1a739d46/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java index c3e8dad..c540693 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/json/ODataJsonSerializerTest.java @@ -2676,4 +2676,24 @@ public class ODataJsonSerializerTest { + "{\"@odata.type\":\"#olingo.odata.test1.CTPrimComp\"," + "\"propertyin...@odata.type\":\"#Int16\",\"PropertyInt16\":3}]")); } + + @Test + public void selectNavigationProperty() throws Exception { + final EdmEntitySet edmEntitySet = entityContainer.getEntitySet("ESTwoKeyNav"); + final Entity entity = data.readAll(edmEntitySet).getEntities().get(0); + final SelectItem selectItem = ExpandSelectMock.mockSelectItem(edmEntitySet, + "CollPropertyCompNav", "NavPropertyETTwoKeyNavOne"); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList(selectItem)); + InputStream result = serializer.entity(metadata, edmEntitySet.getEntityType(), entity, + EntitySerializerOptions.with() + .contextURL(ContextURL.with().entitySet(edmEntitySet).suffix(Suffix.ENTITY).build()) + .select(select) + .build()).getContent(); + final String resultString = IOUtils.toString(result); + final String expectedResult = "{\"@odata.context\":\"$metadata#ESTwoKeyNav/$entity\"," + + "\"@odata.metadataEtag\":\"W/\\\"metadataETag\\\"\"," + + "\"@odata.id\":\"ESTwoKeyNav(PropertyInt16=1,PropertyString='1')\"," + + "\"CollPropertyCompNav\":[{}]}"; + Assert.assertEquals(expectedResult, resultString); + } } http://git-wip-us.apache.org/repos/asf/olingo-odata4/blob/1a739d46/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java ---------------------------------------------------------------------- diff --git a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java index 9bb05ec..95bc600 100644 --- a/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java +++ b/lib/server-test/src/test/java/org/apache/olingo/server/core/serializer/utils/ContextURLHelperTest.java @@ -20,15 +20,18 @@ package org.apache.olingo.server.core.serializer.utils; import static org.junit.Assert.assertEquals; +import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import org.apache.olingo.commons.api.data.ContextURL; import org.apache.olingo.commons.api.edm.Edm; +import org.apache.olingo.commons.api.edm.EdmAction; import org.apache.olingo.commons.api.edm.EdmComplexType; import org.apache.olingo.commons.api.edm.EdmEntityContainer; import org.apache.olingo.commons.api.edm.EdmEntitySet; import org.apache.olingo.commons.api.edm.EdmEntityType; +import org.apache.olingo.commons.api.edm.EdmFunction; import org.apache.olingo.commons.api.edm.EdmProperty; import org.apache.olingo.commons.api.edm.FullQualifiedName; import org.apache.olingo.commons.api.edmx.EdmxReference; @@ -456,4 +459,54 @@ public class ContextURLHelperTest { assertEquals("$metadata#ESTwoKeyNav(CollPropertyCompNav/NavPropertyETTwoKeyNavMany)", ContextURLBuilder.create(contextURL).toASCIIString()); } + + @Test + public void buildSelectWithAction() throws Exception { + final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoKeyNav"); + final EdmAction action = edm.getBoundAction( + new FullQualifiedName("olingo.odata.test1.BAESTwoKeyNavRTESTwoKeyNav"), + new FullQualifiedName("olingo.odata.test1.ETTwoKeyNav"), true); + final SelectItem selectItem = ExpandSelectMock.mockSelectItemHavingAction( + entitySet, action); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList( + selectItem)); + final ContextURL contextURL = ContextURL.with().entitySet(entitySet) + .selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), null, select)).build(); + assertEquals("$metadata#ESTwoKeyNav(olingo.odata.test1.BAESTwoKeyNavRTESTwoKeyNav)", + ContextURLBuilder.create(contextURL).toASCIIString()); + } + + @Test + public void buildSelectWithPropertyAndAction() throws Exception { + final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoKeyNav"); + final EdmAction action = edm.getBoundAction( + new FullQualifiedName("olingo.odata.test1.BAESTwoKeyNavRTESTwoKeyNav"), + new FullQualifiedName("olingo.odata.test1.ETTwoKeyNav"), true); + final SelectItem selectItem1 = ExpandSelectMock.mockSelectItem( + entitySet, "PropertyString"); + final SelectItem selectItem2 = ExpandSelectMock.mockSelectItemHavingAction( + entitySet, action); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList( + selectItem1, selectItem2)); + final ContextURL contextURL = ContextURL.with().entitySet(entitySet) + .selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), null, select)).build(); + assertEquals("$metadata#ESTwoKeyNav(PropertyString,olingo.odata.test1.BAESTwoKeyNavRTESTwoKeyNav)", + ContextURLBuilder.create(contextURL).toASCIIString()); + } + + @Test + public void buildSelectWithFunction() throws Exception { + final EdmEntitySet entitySet = entityContainer.getEntitySet("ESTwoKeyNav"); + final EdmFunction function = edm.getBoundFunction( + new FullQualifiedName("olingo.odata.test1.BFCESTwoKeyNavRTString"), + new FullQualifiedName("olingo.odata.test1.ETTwoKeyNav"), true, new ArrayList<String>()); + final SelectItem selectItem = ExpandSelectMock.mockSelectItemHavingFunction( + entitySet, function); + final SelectOption select = ExpandSelectMock.mockSelectOption(Arrays.asList( + selectItem)); + final ContextURL contextURL = ContextURL.with().entitySet(entitySet) + .selectList(ContextURLHelper.buildSelectList(entitySet.getEntityType(), null, select)).build(); + assertEquals("$metadata#ESTwoKeyNav(olingo.odata.test1.BFCESTwoKeyNavRTString)", + ContextURLBuilder.create(contextURL).toASCIIString()); + } }