http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/EdmURIBuilderImpl.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/EdmURIBuilderImpl.java b/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/EdmURIBuilderImpl.java new file mode 100644 index 0000000..567d3bc --- /dev/null +++ b/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/EdmURIBuilderImpl.java @@ -0,0 +1,491 @@ +package org.apache.olingo.odata2.client.core.uri; + +import java.net.URI; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.api.edm.EdmFunctionImport; +import org.apache.olingo.odata2.api.edm.EdmLiteralKind; +import org.apache.olingo.odata2.api.edm.EdmMultiplicity; +import org.apache.olingo.odata2.api.edm.EdmNavigationProperty; +import org.apache.olingo.odata2.api.edm.EdmParameter; +import org.apache.olingo.odata2.api.edm.EdmProperty; +import org.apache.olingo.odata2.api.edm.EdmSimpleType; +import org.apache.olingo.odata2.api.edm.EdmSimpleTypeException; +import org.apache.olingo.odata2.api.edm.EdmTypeKind; +import org.apache.olingo.odata2.client.api.uri.EdmURIBuilder; +import org.apache.olingo.odata2.client.api.uri.QueryOption; +import org.apache.olingo.odata2.client.api.uri.SegmentType; +import org.apache.olingo.odata2.client.core.uri.util.UriUtil; +import org.apache.olingo.odata2.core.commons.Encoder; + +/** + * This is a builder class that constructs URI with edm validations + * + */ +public class EdmURIBuilderImpl implements EdmURIBuilder{ + protected final List<Segment> segments = new ArrayList<Segment>(); + private SegmentType state = SegmentType.INITIAL; + + /** + * Insertion-order map of query options. + */ + protected final Map<String, String> queryOptions = new LinkedHashMap<String, String>(); + + /** + * Insertion-order map of custom query options. + */ + protected final Map<String, String> customQueryOptions = new LinkedHashMap<String, String>(); + + /** + * Insertion-order map of function import parameters. + */ + protected final Map<String, Object> functionImportParameters = new LinkedHashMap<String, Object>(); + + /** + * Constructor. + * + * @param serviceRoot absolute URL (schema, host and port included) representing the location of the root of the data + * service. + */ + public EdmURIBuilderImpl(final String serviceRoot) { + state = SegmentType.INITIAL; + segments.add(new Segment(SegmentType.INITIAL, serviceRoot)); + } + + @Override + public EdmURIBuilder appendCountSegment() { + switch (state) { + case INITIAL: + case SIMPLEPROPERTY: + case COMPLEXPROPERTY: + case NAVIGATION_TO_ONE: + case NAVIGATION_TO_MANY_WITH_KEY: + case FUNCTIONIMPORT_WITH_KEY: + case ENTITY: + throw new RuntimeException("Can't specify a key at this position");//NOSONAR + case ENTITYSET: + appendCount(); + break; + case NAVIGATION_TO_MANY: + appendCount(); + break; + case FUNCTIONIMPORT_MANY: + appendCount(); + break; + default: + throw new RuntimeException("Unkown state:" + state);//NOSONAR + } + return this; + } + + private void appendCount() { + segments.add(new Segment(SegmentType.COUNT, SegmentType.COUNT.getValue())); + state = SegmentType.COUNT; + } + + @Override + public EdmURIBuilder appendValueSegment() { + switch (state) { + case INITIAL: + case COMPLEXPROPERTY: + case ENTITYSET: + case NAVIGATION_TO_MANY: + case ENTITY: + case NAVIGATION_TO_MANY_WITH_KEY: + case NAVIGATION_TO_ONE: + throw new RuntimeException("Can't specify a navigation at this position");//NOSONAR + case SIMPLEPROPERTY: + addValueSegment(); + break; + default: + throw new RuntimeException("Unkown state:" + state);//NOSONAR + } + return this; + } + + private void addValueSegment() { + segments.add(new Segment(SegmentType.VALUE, SegmentType.VALUE.getValue())); + } + + @Override + public EdmURIBuilder appendMetadataSegment() { + segments.add(new Segment(SegmentType.METADATA, SegmentType.METADATA.getValue())); + return this; + } + + @Override + public EdmURIBuilder format(String format) { //NOSONAR + switch (state) { + case INITIAL: + case COUNT: + case VALUE: + throw new RuntimeException("Can't specify a format at this position");//NOSONAR + case NAVIGATION_TO_ONE: + case SIMPLEPROPERTY: + case COMPLEXPROPERTY: + case ENTITYSET: + case NAVIGATION_TO_MANY: + case ENTITY: + case NAVIGATION_TO_MANY_WITH_KEY: + addFormat(format); + break; + default: + throw new RuntimeException("Unkown state:" + state);//NOSONAR + } + return this; + } + + private void addFormat(String format) { + addQueryOption(QueryOption.FORMAT.toString(), format, true); + } + + @Override + public EdmURIBuilder appendEntitySetSegment(EdmEntitySet entitySet) { + state = SegmentType.ENTITYSET; + try { + segments.add(new Segment(SegmentType.ENTITYSET, entitySet.getName())); + } catch (EdmException e) { + throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR + } + return this; + } + + @Override + public EdmURIBuilder appendNavigationSegment(EdmNavigationProperty property) { + switch (state) { + case INITIAL: + case SIMPLEPROPERTY: + case COMPLEXPROPERTY: + case ENTITYSET: + case NAVIGATION_TO_MANY: + case FUNCTIONIMPORT_MANY: + throw new RuntimeException("Can't specify a navigation at this position");//NOSONAR + case NAVIGATION_TO_ONE: + addNavigationSegment(property); + break; + case ENTITY: + case NAVIGATION_TO_MANY_WITH_KEY: + case FUNCTIONIMPORT_WITH_KEY: + addNavigationSegment(property); + break; + default: + throw new RuntimeException("Unkown state:" + state);//NOSONAR + } + return this; + } + + private void addNavigationSegment(EdmNavigationProperty property) { + try { + state = property.getMultiplicity() == EdmMultiplicity.MANY? SegmentType.NAVIGATION_TO_MANY: + SegmentType.NAVIGATION_TO_ONE; + segments.add(new Segment(state, property.getName())); + } catch (EdmException e) { + throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR + } + } + + @Override + public EdmURIBuilder appendKeySegment(EdmProperty property, Object value) { + switch (state) { + case INITIAL: + case SIMPLEPROPERTY: + case COMPLEXPROPERTY: + case NAVIGATION_TO_ONE: + case NAVIGATION_TO_MANY_WITH_KEY: + case FUNCTIONIMPORT_WITH_KEY: + case FUNCTIONIMPORT: + case ENTITY: + throw new RuntimeException("Can't specify a key at this position");//NOSONAR + case ENTITYSET: + state = SegmentType.ENTITY; + appendKey(property, value); + break; + case NAVIGATION_TO_MANY: + state = SegmentType.NAVIGATION_TO_MANY_WITH_KEY; + appendKey(property, value); + break; + case FUNCTIONIMPORT_MANY: + state = SegmentType.FUNCTIONIMPORT_WITH_KEY; + appendKey(property, value); + break; + default: + throw new RuntimeException("Unkown state:" + state);//NOSONAR + } + return this; + } + + private void appendKey(EdmProperty property, Object value) { + String key = ""; + try { + key = getKey(property, value, false); + } catch (EdmSimpleTypeException e) { + throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR + } catch (EdmException e) { + throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR + } + segments.add(new Segment(SegmentType.KEY, key)); + } + + /** + * @param property + * @param value + * @return + * @throws EdmException + * @throws EdmSimpleTypeException + */ + private String getKey(EdmProperty property, Object value, boolean isSegment) + throws EdmException { + String key = ""; + EdmSimpleType edmType = (EdmSimpleType) property.getType(); + if (value instanceof String) { + value = Encoder.encode(value.toString()); //NOSONAR + } + if (!isSegment) { + key = "(" + edmType.valueToString(value, EdmLiteralKind.URI, property.getFacets()) + ")"; + } else { + key = edmType.valueToString(value, EdmLiteralKind.URI, property.getFacets()); + } + return key; + } + + @Override + public EdmURIBuilder appendKeySegment(Map<EdmProperty, Object> segmentValues) { + switch (state) { + case INITIAL: + case SIMPLEPROPERTY: + case COMPLEXPROPERTY: + case NAVIGATION_TO_ONE: + case ENTITY: + case NAVIGATION_TO_MANY_WITH_KEY: + case FUNCTIONIMPORT: + case FUNCTIONIMPORT_WITH_KEY: + throw new RuntimeException("Can't specify a key at this position");//NOSONAR + case ENTITYSET: + state = SegmentType.ENTITY; + appendKey(segmentValues); + break; + case NAVIGATION_TO_MANY: + state = SegmentType.NAVIGATION_TO_MANY_WITH_KEY; + appendKey(segmentValues); + break; + case FUNCTIONIMPORT_MANY: + state = SegmentType.FUNCTIONIMPORT_WITH_KEY; + appendKey(segmentValues); + break; + default: + throw new RuntimeException("Unkown state:" + state);//NOSONAR + } + return this; + } + + private void appendKey(Map<EdmProperty, Object> segmentValues) { + String key = ""; + try { + key = buildMultiKeySegment(segmentValues, ','); + } catch (EdmSimpleTypeException e) { + throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR + } catch (EdmException e) { + throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR + } + segments.add(new Segment(SegmentType.KEY, key)); + } + + protected String buildMultiKeySegment(final Map<EdmProperty, Object> segmentValues, + final char separator) throws EdmException { + final StringBuilder keyBuilder = new StringBuilder().append('('); + for (Map.Entry<EdmProperty, Object> entry : segmentValues.entrySet()) { + keyBuilder.append(entry.getKey().getName()).append('=').append( + getKey((EdmProperty)entry.getKey(), entry.getValue(), true)); + keyBuilder.append(separator); + } + keyBuilder.deleteCharAt(keyBuilder.length() - 1).append(')'); + + return keyBuilder.toString(); + } + + @Override + public EdmURIBuilder filter(String filter) { + return replaceQueryOption(QueryOption.FILTER, filter); + } + + @Override + public EdmURIBuilder select(String... selectItems) { + return addQueryOption(QueryOption.SELECT, UriUtil.join(selectItems, ",")); + } + + @Override + public EdmURIBuilder orderBy(String property) { + return addQueryOption(QueryOption.ORDERBY, property); + } + + @Override + public EdmURIBuilder top(int top) { + return replaceQueryOption(QueryOption.TOP, String.valueOf(top)); + } + + @Override + public EdmURIBuilder skip(int skip) { + return replaceQueryOption(QueryOption.SKIP, String.valueOf(skip)); + } + + @Override + public URI build() { + return UriUtil.getUri(segments, queryOptions, customQueryOptions, functionImportParameters); + } + + @Override + public EdmURIBuilder addQueryOption(QueryOption option, String value) { + return addQueryOption(option.toString(), value, false); + } + + public EdmURIBuilder replaceQueryOption(QueryOption option, String value) { + return addQueryOption(option.toString(), value, true); + } + + public EdmURIBuilder addQueryOption(String option, String value, boolean replace) { //NOSONAR + if (option.equalsIgnoreCase(QueryOption.EXPAND.toString())) { + if (state == SegmentType.COUNT) { + throw new RuntimeException("Can't specify a query option " + option + " at this position");//NOSONAR + } else { + UriUtil.appendQueryOption(option, value, queryOptions, replace); + } + } else { + switch (state) { + case INITIAL: + case SIMPLEPROPERTY: + case COMPLEXPROPERTY: + throw new RuntimeException("Can't specify a query option " + option + " at this position");//NOSONAR + case NAVIGATION_TO_ONE: + case NAVIGATION_TO_MANY_WITH_KEY: + case ENTITY: + entityQueryOption(option, value, replace); + break; + case ENTITYSET: + case NAVIGATION_TO_MANY: + UriUtil.appendQueryOption(option, value, queryOptions, replace); + break; + case COUNT: + countQueryOption(option, value, replace); + break; + default: + throw new RuntimeException("Unkown state:" + state);//NOSONAR + } + } + return this; + } + + /** + * @param option + * @param value + * @param replace + */ + private void countQueryOption(String option, String value, boolean replace) { + if (option.equalsIgnoreCase(QueryOption.SELECT.toString())) { + throw new RuntimeException("Can't specify a query option " + option + " at this position");//NOSONAR + } else { + UriUtil.appendQueryOption(option, value, queryOptions, replace); + } + } + + /** + * @param option + * @param value + * @param replace + */ + private void entityQueryOption(String option, String value, boolean replace) { + if (option.equalsIgnoreCase(QueryOption.SELECT.toString())) { + UriUtil.appendQueryOption(option, value, queryOptions, replace); + } else { + throw new RuntimeException("Can't specify a query option " + option + " at this position");//NOSONAR + } + } + + @Override + public EdmURIBuilder appendPropertySegment(EdmProperty property, String segmentValue) { + switch (state) { + case INITIAL: + case SIMPLEPROPERTY: + case NAVIGATION_TO_ONE: + case ENTITYSET: + case NAVIGATION_TO_MANY: + case FUNCTIONIMPORT_MANY: + throw new RuntimeException("Can't specify a property at this position");//NOSONAR + case COMPLEXPROPERTY: + appendProperty(property, segmentValue); + break; + case ENTITY: + appendProperty(property, segmentValue); + break; + case NAVIGATION_TO_MANY_WITH_KEY: + case FUNCTIONIMPORT_WITH_KEY: + case FUNCTIONIMPORT: + appendProperty(property, segmentValue); + break; + default: + throw new RuntimeException("Unkown state:" + state);//NOSONAR + } + return this; + } + + private void appendProperty(EdmProperty property, String segmentValue) { + try { + state = property.getType().getKind() == EdmTypeKind.SIMPLE? SegmentType.SIMPLEPROPERTY : + SegmentType.COMPLEXPROPERTY; + } catch (EdmException e) { + throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR + } + segments.add(new Segment(state, segmentValue)); + } + + @Override + public EdmURIBuilder expand(String... expandItems) { + return addQueryOption(QueryOption.EXPAND, UriUtil.join(expandItems, ",")); + } + + @Override + public EdmURIBuilder addCustomQueryOption(String paramName, Object paramValue) { + UriUtil.appendQueryOption(paramName, paramValue.toString(), + customQueryOptions, true); + return this; + } + + @Override + public EdmURIBuilder appendFunctionImportSegment(EdmFunctionImport functionImport) { + try { + state = functionImport.getReturnType() != null ? + (functionImport.getReturnType().getMultiplicity() == EdmMultiplicity.MANY && + functionImport.getReturnType().getType().getKind() == EdmTypeKind.ENTITY + ? SegmentType.FUNCTIONIMPORT_MANY : SegmentType.FUNCTIONIMPORT) : SegmentType.FUNCTIONIMPORT; + segments.add(new Segment(state, functionImport.getName())); + } catch (EdmException e) { + throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR + } + return this; + } + + @Override + public EdmURIBuilder appendFunctionImportParameters(Map<EdmParameter, Object> functionImportParams) { + try { + if (functionImportParams != null) { + for (Map.Entry<EdmParameter, Object> param : functionImportParams.entrySet()) { + EdmParameter edmParam = param.getKey(); + EdmSimpleType edmType = (EdmSimpleType) edmParam.getType(); + Object value = param.getValue(); + if (value instanceof String) { + value = value.toString(); + } + value = edmType.valueToString(value, EdmLiteralKind.URI, edmParam.getFacets()); + functionImportParameters.put(edmParam.getName(), value); + } + } + } catch (EdmException e) { + throw new RuntimeException("Unexpected EDM Exception: ", e);//NOSONAR + } + return this; + } +}
http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/Segment.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/Segment.java b/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/Segment.java new file mode 100644 index 0000000..9cbf3bf --- /dev/null +++ b/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/Segment.java @@ -0,0 +1,32 @@ +package org.apache.olingo.odata2.client.core.uri; + +import org.apache.olingo.odata2.client.api.uri.SegmentType; + +/** + * The objects of this class provide the uri segment type and value + * + */ +public class Segment { + + private final SegmentType type; + + private final String value; + + /** + * + * @param type + * @param value + */ + public Segment(final SegmentType type, final String value) { + this.type = type; + this.value = value; + } + + public SegmentType getType() { + return type; + } + + public String getValue() { + return value; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/URIBuilderImpl.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/URIBuilderImpl.java b/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/URIBuilderImpl.java new file mode 100644 index 0000000..9fe74f4 --- /dev/null +++ b/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/URIBuilderImpl.java @@ -0,0 +1,217 @@ +package org.apache.olingo.odata2.client.core.uri; + +import java.net.URI; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.apache.olingo.odata2.client.api.uri.QueryOption; +import org.apache.olingo.odata2.client.api.uri.SegmentType; +import org.apache.olingo.odata2.client.api.uri.URIBuilder; +import org.apache.olingo.odata2.client.core.uri.util.UriUtil; +import org.apache.olingo.odata2.core.commons.Encoder; + +/** + * This is a builder class that constructs URI without edm validations + * + */ +public class URIBuilderImpl implements URIBuilder { + protected final List<Segment> segments = new ArrayList<Segment>(); + + /** + * Insertion-order map of query options. + */ + protected final Map<String, String> queryOptions = new LinkedHashMap<String, String>(); + + /** + * Insertion-order map of custom query options. + */ + protected final Map<String, String> customQueryOptions = new LinkedHashMap<String, String>(); + + /** + * Insertion-order map of function import parameters. + */ + protected final Map<String, Object> functionImportParameters = new LinkedHashMap<String, Object>(); + + /** + * Constructor. + * + * @param serviceRoot absolute URL (schema, host and port included) representing the location of the root of the data + * service. + */ + public URIBuilderImpl(final String serviceRoot) { + segments.add(new Segment(SegmentType.INITIAL, serviceRoot)); + } + + @Override + public URIBuilder appendCountSegment() { + segments.add(new Segment(SegmentType.COUNT, SegmentType.COUNT.getValue())); + return this; + } + + @Override + public URIBuilder appendMetadataSegment() { + segments.add(new Segment(SegmentType.METADATA, SegmentType.METADATA.getValue())); + return this; + } + + @Override + public URIBuilder format(String format) { + UriUtil.appendQueryOption(QueryOption.FORMAT.toString(), format, queryOptions, true); + return this; + } + + @Override + public URIBuilder appendValueSegment() { + segments.add(new Segment(SegmentType.VALUE, SegmentType.VALUE.getValue())); + return this; + } + + @Override + public URIBuilder addQueryOption(QueryOption option, String value) { + UriUtil.appendQueryOption(option.toString(), value, queryOptions, false); + return this; + } + + @Override + public URIBuilder filter(String filter) { + return replaceQueryOption(QueryOption.FILTER, filter); + } + + @Override + public URIBuilder top(int top) { + return replaceQueryOption(QueryOption.TOP, String.valueOf(top)); + } + + @Override + public URIBuilder skip(int skip) { + return replaceQueryOption(QueryOption.SKIP, String.valueOf(skip)); + } + + @Override + public URIBuilder addCustomQueryOption(String paramName, Object paramValue) { + UriUtil.appendQueryOption(paramName, paramValue.toString(), + customQueryOptions, true); + return this; + } + + @Override + public URI build() { + return UriUtil.getUri(segments, queryOptions, customQueryOptions, functionImportParameters); + } + + @Override + public URIBuilder appendEntitySetSegment(String entitySet) { + segments.add(new Segment(SegmentType.ENTITYSET, entitySet)); + return this; + } + + @Override + public URIBuilder appendNavigationSegment(String navigationProperty) { + segments.add(new Segment(SegmentType.NAVIGATION, navigationProperty)); + return this; + } + + @Override + public URIBuilder appendKeySegment(Object value) { + String key = getKey(value, false); + segments.add(new Segment(SegmentType.KEY, key)); + return this; + } + + private String getKey(Object value, boolean isSegment) { + String key = ""; + if (!isSegment) { + if (value instanceof String) { + value = Encoder.encode(value.toString()); //NOSONAR + key += "('" + value + "')"; + } else { + key += "(" + value + ")"; + } + } else { + if (value instanceof String) { + value = Encoder.encode(value.toString()); //NOSONAR + key += "'" + value + "'"; + } else { + key += value; + } + } + return key; + } + + @Override + public URIBuilder appendKeySegment(Map<String, Object> segmentValues) { + String key = buildMultiKeySegment(segmentValues, ','); + segments.add(new Segment(SegmentType.KEY, key)); + return this; + } + + private String buildMultiKeySegment(Map<String, Object> segmentValues, char separator) { + if (segmentValues == null || segmentValues.isEmpty()) { + return ""; + } else { + final StringBuilder keyBuilder = new StringBuilder().append('('); + for (Map.Entry<String, Object> entry : segmentValues.entrySet()) { + keyBuilder.append(entry.getKey()).append('=').append( + getKey(entry.getValue(), true)); + keyBuilder.append(separator); + } + keyBuilder.deleteCharAt(keyBuilder.length() - 1).append(')'); + + return keyBuilder.toString(); + } + } + + @Override + public URIBuilder appendPropertySegment(String segmentValue) { + segments.add(new Segment(SegmentType.PROPERTY, segmentValue)); + return this; + } + + @Override + public URIBuilder expand(String... expandItems) { + return addQueryOption(QueryOption.EXPAND, UriUtil.join(expandItems, ",")); + } + + @Override + public URIBuilder select(String... selectItems) { + return addQueryOption(QueryOption.SELECT, UriUtil.join(selectItems, ",")); + } + + @Override + public URIBuilder orderBy(String order) { + return addQueryOption(QueryOption.ORDERBY, order); + } + + /** + * + * @param option + * @param value + * @return URIBuilder + */ + public URIBuilder replaceQueryOption(QueryOption option, String value) { + UriUtil.appendQueryOption(option.toString(), value, queryOptions, true); + return this; + } + + @Override + public URIBuilder appendFunctionImportSegment(String functionImport) { + segments.add(new Segment(SegmentType.FUNCTIONIMPORT, functionImport)); + return this; + } + + @Override + public URIBuilder appendFunctionImportParameters(Map<String, Object> functionImportParams) { + if (functionImportParams != null && !functionImportParams.isEmpty()) { + for (Map.Entry<String, Object> param : functionImportParams.entrySet()) { + Object value = param.getValue(); + if (value instanceof String) { + value = "'" + value + "'"; + } + functionImportParameters.put(param.getKey(), value); + } + } + return this; + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/util/UriUtil.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/util/UriUtil.java b/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/util/UriUtil.java new file mode 100644 index 0000000..914d808 --- /dev/null +++ b/odata2-lib/odata-client-core/src/main/java/org/apache/olingo/odata2/client/core/uri/util/UriUtil.java @@ -0,0 +1,186 @@ +package org.apache.olingo.odata2.client.core.uri.util; + +import java.net.URI; +import java.util.List; +import java.util.Map; + +import org.apache.olingo.odata2.client.api.uri.QueryOption; +import org.apache.olingo.odata2.client.api.uri.SegmentType; +import org.apache.olingo.odata2.client.core.uri.Segment; +import org.apache.olingo.odata2.core.commons.Encoder; + +/** + * Util class + * + */ +public class UriUtil { + + private UriUtil() { + + } + + /** + * + * @param segments + * @param queryOptions + * @param customQueryOptions + * @param functionImportParameters + * @return URI + */ + public static URI getUri(List<Segment> segments, Map<String, String> queryOptions, + Map<String, String> customQueryOptions, Map<String, Object> functionImportParameters) { //NOPMD - suppressed + final StringBuilder segmentsBuilder = new StringBuilder(); + + if (segments.size() == 1 && segments.get(0).getType() == SegmentType.INITIAL + && customQueryOptions.isEmpty() + && queryOptions.isEmpty() && functionImportParameters.isEmpty()) { + segmentsBuilder.append(segments.get(0).getValue()); + if (segmentsBuilder.charAt(segmentsBuilder.length() - 1) != '/') { + segmentsBuilder.append('/'); + } + return URI.create(segmentsBuilder.toString()); + } + for (Segment seg : segments) { + if (segmentsBuilder.length() > 0 && seg.getType() != SegmentType.KEY && + seg.getType() != SegmentType.NAVIGATION_TO_MANY_WITH_KEY && + seg.getType() != SegmentType.FUNCTIONIMPORT_WITH_KEY && + segmentsBuilder.charAt(segmentsBuilder.length() - 1) != '/') { + segmentsBuilder.append('/'); + } + segmentsBuilder.append(seg.getValue()); + } + + try { + if (!queryOptions.isEmpty()) { + appendQuerySegmentDelimiter(true, true, segmentsBuilder); + int i = 0; + for (Map.Entry<String, String> option : queryOptions.entrySet()) { + //Appends a system query option to uri builder + i++; + appendQuerySegments(QueryOption.valueOf(option.getKey()).getValue(), option.getValue(), + segmentsBuilder, true); + if (i < queryOptions.size()) { + segmentsBuilder.append("&"); + } + } + } + + if (!customQueryOptions.isEmpty()) { + appendQuerySegmentDelimiter(queryOptions.isEmpty(), true, segmentsBuilder); + int i = 0; + for (Map.Entry<String, String> option : customQueryOptions.entrySet()) { + //Appends a custom query option to uri builder + i++; + appendQuerySegments(option.getKey(), option.getValue(), + segmentsBuilder, false); + if (i < customQueryOptions.size()) { + segmentsBuilder.append("&"); + } + } + } + + if (!functionImportParameters.isEmpty()) { + appendQuerySegmentDelimiter(queryOptions.isEmpty(), customQueryOptions.isEmpty(), segmentsBuilder); + int i = 0; + for (Map.Entry<String, Object> funcParam : functionImportParameters.entrySet()) { + //Appends a function import URI with parameters to uri builder + i++; + appendQuerySegments(funcParam.getKey(), funcParam.getValue(), + segmentsBuilder, false); + if (i < functionImportParameters.size()) { + segmentsBuilder.append("&"); + } + } + } + + return URI.create(segmentsBuilder.toString()); + } catch (IllegalArgumentException e) { + throw new IllegalArgumentException("Could not build valid URI", e); + } + } + + /** + * + * @param isQueryOptions + * @param isCustomQueryOptions + * @param segmentsBuilder + */ + private static void appendQuerySegmentDelimiter(boolean isQueryOptions, + boolean isCustomQueryOptions, StringBuilder segmentsBuilder) { + if (!isQueryOptions || !isCustomQueryOptions) { + segmentsBuilder.append("&"); + } else { + segmentsBuilder.append("?"); + } + } + + /** + * + * @param name + * @param value + * @param segmentsBuilder + */ + private static void appendQuerySegments(String name, Object value, + StringBuilder segmentsBuilder, boolean isQueryOption) { + if (isQueryOption) { + segmentsBuilder.append(name); + } else { + segmentsBuilder.append(Encoder.encode(name)); + } + segmentsBuilder.append("="); + segmentsBuilder.append(Encoder.encode(value.toString())); + } + + /** + * + * @param items + * @param separator + * @return String + */ + public static String join(String[] items, String separator) { + return join(items, separator, 0, items.length); + } + + private static String join(String[] items, String separator, int startIndex, int endIndex) { + if (items == null) { + return null; + } + if (separator == null) { + separator = ""; //NOSONAR + } + + final int noOfItems = endIndex - startIndex; + if (noOfItems <= 0) { + return ""; + } + + final StringBuilder buf = new StringBuilder(noOfItems * 16); + + for (int i = startIndex; i < endIndex; i++) { + if (i > startIndex) { + buf.append(separator); + } + if (items[i] != null) { + buf.append(items[i]); + } + } + return buf.toString(); + } + + /** + * + * @param paramName + * @param paramValue + * @param queryOptions + * @param replace + */ + public static void appendQueryOption(String paramName, String paramValue, Map<String, + String> queryOptions, boolean replace) { + final StringBuilder builder = new StringBuilder(); + if (!replace && queryOptions.containsKey(paramName)) { + builder.append(queryOptions.get(paramName)).append(','); + } + builder.append(paramValue); + queryOptions.put(paramName, builder.toString()); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmAnnotationImplTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmAnnotationImplTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmAnnotationImplTest.java new file mode 100644 index 0000000..032809e --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmAnnotationImplTest.java @@ -0,0 +1,95 @@ +/******************************************************************************* + * 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.odata2.client.core.edm.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.odata2.api.edm.EdmAnnotationAttribute; +import org.apache.olingo.odata2.api.edm.EdmAnnotationElement; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmAnnotationAttributeImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmAnnotationElementImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmAnnotationsImpl; +import org.junit.Test; + +public class EdmAnnotationImplTest { + + @Test + public void annotationAttributeTest(){ + EdmAnnotationAttributeImpl annotation = new EdmAnnotationAttributeImpl(); + annotation.setName("name"); + annotation.setNamespace("namespace"); + annotation.setPrefix("prefix"); + annotation.setText("text"); + assertEquals("name", annotation.getName()); + assertEquals("namespace", annotation.getNamespace()); + assertEquals("prefix", annotation.getPrefix()); + assertEquals("text", annotation.getText()); + } + + @Test + public void annotationElementTest(){ + EdmAnnotationElementImpl annotation = new EdmAnnotationElementImpl(); + annotation.setName("name"); + annotation.setNamespace("namespace"); + annotation.setPrefix("prefix"); + annotation.setText("text"); + annotation.setAttributes(null); + annotation.setChildElements(null); + assertEquals("name", annotation.getName()); + assertEquals("namespace", annotation.getNamespace()); + assertEquals("prefix", annotation.getPrefix()); + assertEquals("text", annotation.getText()); + assertNull(annotation.getAttributes()); + assertNull(annotation.getChildElements()); + } + + @Test + public void annotationTest(){ + EdmAnnotationsImpl annotation = new EdmAnnotationsImpl(); + EdmAnnotationElementImpl annotationElement = new EdmAnnotationElementImpl(); + EdmAnnotationAttributeImpl annotationAttribute = new EdmAnnotationAttributeImpl(); + List<EdmAnnotationAttribute> annotationAttributeList = + new ArrayList<EdmAnnotationAttribute>(); + List<EdmAnnotationElement> annotationElementList = + new ArrayList<EdmAnnotationElement>(); + annotationAttributeList.add(annotationAttribute); + annotationElementList.add(annotationElement); + annotationAttribute.setName("name"); + annotationAttribute.setNamespace("namespace"); + annotationAttribute.setPrefix("prefix"); + annotationAttribute.setText("text"); + annotationElement.setName("name"); + annotationElement.setNamespace("namespace"); + annotationElement.setPrefix("prefix"); + annotationElement.setText("text"); + annotationElement.setAttributes(annotationAttributeList); + annotationElement.setChildElements(null); + annotation.setAnnotationAttributes(annotationAttributeList); + annotation.setAnnotationElements(annotationElementList); + assertNotNull(annotation.getAnnotationAttribute("name", "namespace")); + assertNotNull(annotation.getAnnotationAttributes()); + assertNotNull(annotation.getAnnotationElement("name", "namespace")); + assertNotNull(annotation.getAnnotationElements()); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmAssociationImplTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmAssociationImplTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmAssociationImplTest.java new file mode 100644 index 0000000..934e289 --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmAssociationImplTest.java @@ -0,0 +1,64 @@ +/******************************************************************************* + * 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.odata2.client.core.edm.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; + +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.client.core.edm.EdmMetadataAssociationEnd; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmAnnotationsImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmAssociationEndImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmAssociationImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmDocumentationImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmReferentialConstraintImpl; +import org.junit.Test; + +public class EdmAssociationImplTest { + + @Test + public void associationTest() throws EdmException { + EdmAssociationImpl association= new EdmAssociationImpl(); + ArrayList<EdmMetadataAssociationEnd> ends = new ArrayList<EdmMetadataAssociationEnd>(); + ends.add(new EdmAssociationEndImpl()); + ends.add(new EdmAssociationEndImpl()); + association.setAnnotations(new EdmAnnotationsImpl()); + association.setAssociationEnds(ends); + association.setDocumentation(new EdmDocumentationImpl()); + association.setEdm(new EdmImpl()); + association.setName("name"); + association.setNamespace("namespace"); + association.setReferentialConstraint(new EdmReferentialConstraintImpl()); + assertNotNull(association); + assertNotNull(association.getAnnotations()); + assertNotNull(association.getAssociationEnds()); + assertNotNull(association.getDocumentation()); + assertNull(association.getEnd("role")); + assertNotNull(association.getEnd1()); + assertNotNull(association.getEnd2()); + assertNull(association.getEndMultiplicity("role")); + assertNotNull(association.getKind()); + assertNotNull(association.getName()); + assertNotNull(association.getNamespace()); + assertNotNull(association.getReferentialConstraint()); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmDocumentationImplTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmDocumentationImplTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmDocumentationImplTest.java new file mode 100644 index 0000000..1043d1b --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmDocumentationImplTest.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * 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.odata2.client.core.edm.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.util.ArrayList; +import java.util.List; + +import org.apache.olingo.odata2.api.edm.EdmAnnotationAttribute; +import org.apache.olingo.odata2.api.edm.EdmAnnotationElement; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmDocumentationImpl; +import org.junit.Test; + +public class EdmDocumentationImplTest { + + @Test + public void testDocumentation(){ + EdmDocumentationImpl doc = new EdmDocumentationImpl(); + List<EdmAnnotationAttribute> annotationAttributes = + new ArrayList<EdmAnnotationAttribute>(); + doc.setAnnotationAttributes(annotationAttributes); + List<EdmAnnotationElement> annotationElements = + new ArrayList<EdmAnnotationElement>(); + doc.setAnnotationElements(annotationElements ); + doc.setLongDescription("longDescription"); + doc.setSummary("summary"); + assertNotNull(doc.getAnnotationAttributes()); + assertNotNull(doc.getAnnotationElements()); + assertNotNull(doc.getLongDescription()); + assertNotNull(doc.getClass()); + assertEquals(doc.getLongDescription(),"longDescription"); + assertEquals(doc.getSummary(), "summary"); + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmEntityImplTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmEntityImplTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmEntityImplTest.java new file mode 100644 index 0000000..ada8553 --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmEntityImplTest.java @@ -0,0 +1,215 @@ +/******************************************************************************* + * 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.odata2.client.core.edm.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.apache.olingo.odata2.api.edm.EdmAssociation; +import org.apache.olingo.odata2.api.edm.EdmAssociationSet; +import org.apache.olingo.odata2.api.edm.EdmComplexType; +import org.apache.olingo.odata2.api.edm.EdmCustomizableFeedMappings; +import org.apache.olingo.odata2.api.edm.EdmEntityContainer; +import org.apache.olingo.odata2.api.edm.EdmEntitySet; +import org.apache.olingo.odata2.api.edm.EdmException; +import org.apache.olingo.odata2.api.edm.EdmFunctionImport; +import org.apache.olingo.odata2.api.edm.EdmMapping; +import org.apache.olingo.odata2.api.edm.EdmNavigationProperty; +import org.apache.olingo.odata2.api.edm.EdmProperty; +import org.apache.olingo.odata2.api.edm.EdmTypeKind; +import org.apache.olingo.odata2.api.edm.FullQualifiedName; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmAnnotationsImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmAssociationImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmComplexTypeImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmCustomizableFeedMappingsImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmDocumentationImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmEntityContainerImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmEntitySetImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmEntityTypeImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmMappingImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmNavigationPropertyImpl; +import org.junit.Before; +import org.junit.Test; + +public class EdmEntityImplTest { + + private EdmAnnotationsImpl annotations; + private EdmDocumentationImpl documentation; + private EdmImpl edm; + private EdmEntityContainerImpl edmEntityContainer; + private EdmEntityTypeImpl edmEntityType; + private FullQualifiedName fqName; + private FullQualifiedName entityTypeName; + private EdmMapping mapping; + private String name; + private EdmCustomizableFeedMappings edmCustomizableFeedMappings; + private List<EdmProperty> edmKeyProperties; + private List<String> edmKeyPropertyNames; + private List<EdmNavigationProperty> navigationProperties; + private EdmComplexTypeImpl structuralType; + + @Before + public void initialize() throws EdmException { + annotations = new EdmAnnotationsImpl(); + documentation = new EdmDocumentationImpl(); + edm = new EdmImpl(); + edmEntityContainer = new EdmEntityContainerImpl(edm); + edmEntityType = new EdmEntityTypeImpl(); + fqName = new FullQualifiedName("namespace", "name"); + entityTypeName = new FullQualifiedName("namespace", "name"); + mapping = new EdmMappingImpl(); + name = "name"; + edmCustomizableFeedMappings = new EdmCustomizableFeedMappingsImpl(); + edmKeyProperties = new ArrayList<EdmProperty>(); + edmKeyPropertyNames = new ArrayList<String>(); + navigationProperties = new ArrayList<EdmNavigationProperty>(); + structuralType = new EdmComplexTypeImpl(); + structuralType.setName("name"); + structuralType.setNamespace("namespace"); + structuralType.setMapping(mapping); + + Map<FullQualifiedName, EdmComplexType> edmComplexTypes = new HashMap<FullQualifiedName, EdmComplexType>(); + edmComplexTypes.put(new FullQualifiedName("namespace", "name"), structuralType); + edm.setEdmComplexTypes(edmComplexTypes); + + Map<FullQualifiedName, EdmAssociation> associationmap = new HashMap<FullQualifiedName, EdmAssociation>(); + associationmap.put(entityTypeName, new EdmAssociationImpl()); + edm.setEdmAssociations(associationmap); + + Map<String, String> aliasToNamespaceInfo = new HashMap<String, String>(); + aliasToNamespaceInfo.put("alias", "namespace"); + edm.setAliasToNamespaceInfo(aliasToNamespaceInfo ); + } + + @Test + public void entitySetTest() throws EdmException { + EdmEntitySetImpl entity = new EdmEntitySetImpl(); + entity.setAnnotations(annotations); + entity.setDocumentation(documentation); + entity.setEdm(edm); + entity.setEdmEntityContainer(edmEntityContainer); + entity.setEdmEntityType(edmEntityType); + entity.setEdmEntityTypeName(fqName); + entity.setEntityTypeName(entityTypeName); + entity.setMapping(mapping); + entity.setName(name); + assertNotNull(entity); + assertNotNull(entity.getAnnotations()); + assertNotNull(entity.getDocumentation()); + assertNotNull(entity.getEdmEntityContainer()); + assertNotNull(entity.getEdmEntityType()); + assertNotNull(entity.getEntityContainer()); + assertNotNull(entity.getEntityType()); + assertNotNull(entity.getEntityTypeName()); + assertNotNull(entity.getMapping()); + assertNotNull(entity.getName()); + + } + + @Test + public void entityTypeTest() throws EdmException { + EdmEntityTypeImpl entity = new EdmEntityTypeImpl(); + entity.setAnnotations(annotations); + entity.setAbstract(true); + entity.setEdm(edm); + entity.setBaseType(entityTypeName); + entity.setCustomizableFeedMappings(edmCustomizableFeedMappings); + entity.setEdmBaseType(edmEntityType); + entity.setEdmKeyProperties(edmKeyProperties); + entity.setEdmKeyPropertyNames(edmKeyPropertyNames); + entity.setName(name); + entity.setEdmNavigationPropertyNames(edmKeyPropertyNames); + entity.setEdmPropertyNames(edmKeyPropertyNames); + entity.setEdmTypeKind(EdmTypeKind.SIMPLE); + entity.setHasStream(true); + entity.setNamespace("namespace"); + entity.setNavigationProperties(navigationProperties); + entity.setProperties(edmKeyProperties); + entity.setStructuralType(structuralType); + assertNotNull(entity); + assertNotNull(entity.getAnnotations()); + assertNotNull(entity.getBaseType()); + assertNotNull(entity.getBaseTypeName()); + assertNotNull(entity.getCustomizableFeedMappings()); + assertNotNull(entity.getEdmBaseType()); + assertNotNull(entity.getEdmTypeKind()); + assertNotNull(entity.getKeyProperties()); + assertNotNull(entity.getKeyPropertyNames()); + assertNotNull(entity.getKind()); + assertNotNull(entity.getMapping()); + assertNotNull(entity.getName()); + assertNotNull(entity.getNamespace()); + assertNotNull(entity.getNavigationPropertyNames()); + assertNotNull(entity.getProperties()); + assertNotNull(entity.getPropertyNames()); + assertNotNull(entity.getStructuralType()); + + } + + @Test + public void entityContainerTest() throws EdmException { + EdmEntityContainerImpl cont = new EdmEntityContainerImpl(edm); + cont.setAnnotations(annotations); + cont.setDefaultContainer(true); + cont.setDocumentation(documentation); + cont.setEdm(edm); + cont.setEdmAssociationSets(new ArrayList<EdmAssociationSet>()); + List<EdmEntitySet> entitySets = new ArrayList<EdmEntitySet>(); + cont.setEdmEntitySets(entitySets); + cont.setEdmExtendedEntityContainer(new EdmEntityContainerImpl(edm)); + cont.setEdmFunctionImports(new ArrayList<EdmFunctionImport>()); + cont.setEntityContainerHierachy(new ArrayList<EdmEntityContainer>()); + cont.setExtendz(name); + cont.setName(name); + assertNotNull(cont); + EdmEntitySet entitySet = new EdmEntitySetImpl(); + ((EdmEntitySetImpl) entitySet).setName(name); + EdmNavigationProperty nav = new EdmNavigationPropertyImpl(); + ((EdmNavigationPropertyImpl) nav).setEdm(edm); + ((EdmNavigationPropertyImpl) nav).setRelationshipName(entityTypeName); + entitySets.add(entitySet); + assertNotNull(cont.getAssociationSets()); + assertNotNull(cont.getDocumentation()); + assertNotNull(cont.getEdm()); + assertNotNull(cont.getEdmAssociationSets()); + assertNotNull(cont.getEdmEntitySets()); + assertNotNull(cont.getEdmExtendedEntityContainer()); + assertNotNull(cont.getEdmFunctionImports()); + assertNotNull(cont.getEntitySet(name)); + assertNotNull(cont.getEntitySets()); + assertNotNull(cont.getExtendz()); + assertNull(cont.getFunctionImport(name)); + assertNotNull(cont.getName()); + } + + @Test + public void complexTypeTest() throws EdmException { + EdmComplexType complexType = edm.getComplexType("namespace", "name"); + assertNotNull(complexType); + + complexType = edm.getComplexType("alias", "name"); + assertNotNull(complexType); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmMappingImplTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmMappingImplTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmMappingImplTest.java new file mode 100644 index 0000000..25cf026 --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmMappingImplTest.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * 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.odata2.client.core.edm.impl; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import org.apache.olingo.odata2.client.core.edm.Impl.EdmMappingImpl; +import org.junit.Test; + +public class EdmMappingImplTest { + + @Test + public void mapTest() { + EdmMappingImpl map= new EdmMappingImpl(); + map.setInternalName("name"); + map.setMediaResourceMimeTypeKey("media"); + map.setMediaResourceSourceKey("mediaKey"); + map.setObject(null); + assertNotNull(map); + assertEquals("name", map.getInternalName()); + assertEquals("media", map.getMediaResourceMimeTypeKey()); + assertEquals("mediaKey", map.getMediaResourceSourceKey()); + assertNull(map.getObject()); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmOnDeleteImplTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmOnDeleteImplTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmOnDeleteImplTest.java new file mode 100644 index 0000000..c7ef400 --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/edm/impl/EdmOnDeleteImplTest.java @@ -0,0 +1,48 @@ +/******************************************************************************* + * 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.odata2.client.core.edm.impl; + +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; + +import java.util.ArrayList; + +import org.apache.olingo.odata2.api.edm.EdmAnnotationAttribute; +import org.apache.olingo.odata2.api.edm.EdmAnnotationElement; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmDocumentationImpl; +import org.apache.olingo.odata2.client.core.edm.Impl.EdmOnDeleteImpl; +import org.junit.Test; + +public class EdmOnDeleteImplTest { + + @Test + public void testOnDelete(){ + EdmOnDeleteImpl del = new EdmOnDeleteImpl(); + del.setAction(null); + del.setAnnotationAttributes(new ArrayList<EdmAnnotationAttribute>()); + del.setAnnotationElements(new ArrayList<EdmAnnotationElement>()); + del.setDocumentation(new EdmDocumentationImpl()); + assertNotNull(del); + assertNull(del.getAction()); + assertNotNull(del.getAnnotationAttributes()); + assertNotNull(del.getAnnotationElements()); + assertNotNull(del.getDocumentation()); + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/AbstractProviderTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/AbstractProviderTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/AbstractProviderTest.java new file mode 100644 index 0000000..ad04601 --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/AbstractProviderTest.java @@ -0,0 +1,214 @@ +/******************************************************************************* + * 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.odata2.client.core.ep; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; +import java.util.TimeZone; + +import org.apache.olingo.odata2.api.edm.Edm; +import org.apache.olingo.odata2.api.ep.EntityProviderException; +import org.apache.olingo.odata2.api.ep.callback.TombstoneCallback; +import org.apache.olingo.odata2.api.exception.ODataException; +import org.apache.olingo.odata2.api.processor.ODataContext; +import org.apache.olingo.odata2.api.uri.PathInfo; +import org.apache.olingo.odata2.client.api.ep.Entity; +import org.apache.olingo.odata2.client.api.ep.EntityCollection; +import org.apache.olingo.odata2.client.api.ep.EntitySerializerProperties; +import org.custommonkey.xmlunit.SimpleNamespaceContext; +import org.custommonkey.xmlunit.XMLUnit; +import org.junit.Before; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * +*/ +public abstract class AbstractProviderTest extends AbstractXmlProducerTestHelper{ + + + public AbstractProviderTest(final StreamWriterImplType type) { + super(type); + } + + protected final Logger log = LoggerFactory.getLogger(this.getClass()); + + protected static final URI BASE_URI; + + static { + try { + BASE_URI = new URI("http://host:80/service/"); + } catch (URISyntaxException e) { + throw new RuntimeException(e); + } + } + protected static final EntitySerializerProperties DEFAULT_PROPERTIES = + EntitySerializerProperties.serviceRoot( + BASE_URI).includeMetadata(true).build(); + + protected Entity employeeData; + + protected EntityCollection employeesData; + + protected Entity photoData; + + protected Entity roomData; + + protected Entity buildingData; + + protected EntityCollection roomsData; + + { + employeeData = new Entity(); + + Calendar date = Calendar.getInstance(TimeZone.getTimeZone("GMT")); + date.clear(); + date.set(1999, 0, 1); + + employeeData.addProperty("EmployeeId", "1"); + employeeData.addProperty("ImmageUrl", null); + employeeData.addProperty("ManagerId", "1"); + employeeData.addProperty("Age", new Integer(52)); + employeeData.addProperty("RoomId", "1"); + employeeData.addProperty("EntryDate", date); + employeeData.addProperty("TeamId", "42"); + employeeData.addProperty("EmployeeName", "Walter Winter"); + // employeeData.put("getImageType", "abc"); + + Entity locationData = new Entity(); + Entity cityData = new Entity(); + cityData.addProperty("PostalCode", "33470"); + cityData.addProperty("CityName", "Duckburg"); + locationData.addProperty("City", cityData); + locationData.addProperty("Country", "Calisota"); + + employeeData.addProperty("Location", locationData); + + Entity employeeData2 = new Entity(); + employeeData2.addProperty("EmployeeId", "1"); + employeeData2.addProperty("ImmageUrl", null); + employeeData2.addProperty("ManagerId", "1"); + employeeData2.addProperty("Age", new Integer(52)); + employeeData2.addProperty("RoomId", "1"); + employeeData2.addProperty("EntryDate", date); + employeeData2.addProperty("TeamId", "42"); + employeeData2.addProperty("EmployeeName", "Walter Winter"); + + Entity locationData2 = new Entity(); + Entity cityData2 = new Entity(); + cityData2.addProperty("PostalCode", "33470"); + cityData2.addProperty("CityName", "Duckburg"); + locationData2.addProperty("City", cityData2); + locationData2.addProperty("Country", "Calisota"); + + employeeData2.addProperty("Location", locationData2); + + employeesData = new EntityCollection(); + employeesData.addEntity(employeeData); + employeesData.addEntity(employeeData2); + + photoData = new Entity(); + photoData.addProperty("Id", Integer.valueOf(1)); + photoData.addProperty("Name", "Mona Lisa"); + photoData.addProperty("Type", "image/png"); + photoData + .addProperty( + "ImageUrl", + "http://www.mopo.de/image/view/2012/6/4/16548086,13385561,medRes,maxh,234,maxw,234," + + "Parodia_Mona_Lisa_Lego_Hamburger_Morgenpost.jpg"); + Entity imageData = new Entity(); + imageData.addProperty("Image", new byte[] { 1, 2, 3, 4 }); + imageData.addProperty("getImageType", "image/png"); + photoData.addProperty("Image", imageData); + photoData.addProperty("BinaryData", new byte[] { -1, -2, -3, -4 }); + photoData.addProperty("СодеÑжание", "РлеÑÑ ÑÑÐ¼Ð¸Ñ Ð²Ð¾Ð´Ð¾Ð¿Ð°Ð´. ÐÑли он не ÑоÑопиÑÑÑ Ð¿ÑоÑп водÑ"); + + roomData = new Entity(); + roomData.addProperty("Id", "1"); + roomData.addProperty("Name", "Neu Schwanstein"); + roomData.addProperty("Seats", new Integer(20)); + roomData.addProperty("Version", new Integer(3)); + + buildingData = new Entity(); + buildingData.addProperty("Id", "1"); + buildingData.addProperty("Name", "WDF03"); + buildingData.addProperty("Image", "image"); + } + + protected void initializeRoomData(final int count) { + roomsData = new EntityCollection(); + for (int i = 1; i <= count; i++) { + Entity tmp = new Entity(); + tmp.addProperty("Id", "" + i); + tmp.addProperty("Name", "Neu Schwanstein" + i); + tmp.addProperty("Seats", new Integer(20)); + tmp.addProperty("Version", new Integer(3)); + roomsData.addEntity(tmp); + } + } + + @Before + public void setXmlNamespacePrefixes() throws Exception { + Map<String, String> prefixMap = new HashMap<String, String>(); + prefixMap.put("a", Edm.NAMESPACE_ATOM_2005); + prefixMap.put("d", Edm.NAMESPACE_D_2007_08); + prefixMap.put("m", Edm.NAMESPACE_M_2007_08); + prefixMap.put("xml", Edm.NAMESPACE_XML_1998); + prefixMap.put("ÃÆââ¬Ëâââ¬Å¡Ã¬ÃÆââ¬ËÃâ ââ¬â¢", "http://localhost"); + prefixMap.put("custom", "http://localhost"); + prefixMap.put("at", TombstoneCallback.NAMESPACE_TOMBSTONE); + XMLUnit.setXpathNamespaceContext(new SimpleNamespaceContext(prefixMap)); + } + + protected ODataContext createContextMock() throws ODataException { + PathInfo pathInfo = mock(PathInfo.class); + when(pathInfo.getServiceRoot()).thenReturn(BASE_URI); + ODataContext ctx = mock(ODataContext.class); + when(ctx.getPathInfo()).thenReturn(pathInfo); + return ctx; + } + + protected AtomSerializerDeserializer createAtomEntityProvider() throws EntityProviderException { + return new AtomSerializerDeserializer(); + } + + public Entity getEmployeeData() { + return employeeData; + } + + public EntityCollection getEmployeesData() { + return employeesData; + } + + public Entity getRoomData() { + return roomData; + } + + public EntityCollection getRoomsData() { + return roomsData; + } + + +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/AbstractXmlProducerTestHelper.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/AbstractXmlProducerTestHelper.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/AbstractXmlProducerTestHelper.java new file mode 100644 index 0000000..052b931 --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/AbstractXmlProducerTestHelper.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * 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.odata2.client.core.ep; + +import java.util.Arrays; +import java.util.List; + +import org.apache.olingo.odata2.testutil.fit.BaseTest; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +@RunWith(Parameterized.class) +public abstract class AbstractXmlProducerTestHelper extends BaseTest { + + public enum StreamWriterImplType { + WOODSTOCKIMPL, SUNINTERNALIMPL; + } + + // CHECKSTYLE:OFF + public AbstractXmlProducerTestHelper(final StreamWriterImplType type) { + switch (type) { + case WOODSTOCKIMPL: + System.setProperty("javax.xml.stream.XMLInputFactory", "com.ctc.wstx.stax.WstxInputFactory"); // NOSONAR + System.setProperty("javax.xml.stream.XMLOutputFactory", "com.ctc.wstx.stax.WstxOutputFactory"); // NOSONAR + break; + case SUNINTERNALIMPL: + System.setProperty("javax.xml.stream.XMLInputFactory", "com.sun.xml.internal.stream.XMLInputFactoryImpl"); // NOSONAR + System.setProperty("javax.xml.stream.XMLOutputFactory", "com.sun.xml.internal.stream.XMLOutputFactoryImpl"); // NOSONAR + break; + default: + System.setProperty("javax.xml.stream.XMLOutputFactory", "com.sun.xml.internal.stream.XMLOutputFactoryImpl"); // NOSONAR + System.setProperty("javax.xml.stream.XMLInputFactory", "com.sun.xml.internal.stream.XMLInputFactoryImpl"); // NOSONAR + break; + } + } + + // CHECKSTYLE:On + + @Parameterized.Parameters + public static List<Object[]> data() { + // If desired this can be made dependent on runtime variables + Object[][] a; + a = new Object[2][1]; + a[0][0] = StreamWriterImplType.WOODSTOCKIMPL; + a[1][0] = StreamWriterImplType.SUNINTERNALIMPL; + + return Arrays.asList(a); + } + +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/DeserializerPropertiesTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/DeserializerPropertiesTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/DeserializerPropertiesTest.java new file mode 100644 index 0000000..96e95bf --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/DeserializerPropertiesTest.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * 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.odata2.client.core.ep; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.sql.Timestamp; +import java.util.HashMap; +import java.util.Map; + +import org.apache.olingo.odata2.client.api.ep.DeserializerProperties; +import org.apache.olingo.odata2.testutil.fit.BaseTest; +import org.junit.Test; + +/** + * + */ +public class DeserializerPropertiesTest extends BaseTest { + + @Test + public void buildReadProperties() throws Exception { + DeserializerProperties properties = DeserializerProperties.init() + .build(); + + assertNotNull(properties.getTypeMappings()); + assertEquals(0, properties.getTypeMappings().size()); + assertNotNull(properties.getValidatedPrefixNamespaceUris()); + assertEquals(0, properties.getValidatedPrefixNamespaceUris().size()); + } + + @Test + public void buildPropertiesAllSet() throws Exception { + Map<String, String> namespaces = new HashMap<String, String>(); + namespaces.put("aNamespace", new String()); + Map<String, Object> typeMappings = new HashMap<String, Object>(); + typeMappings.put("Property", Timestamp.class); + final DeserializerProperties properties = DeserializerProperties.init() + .addValidatedPrefixes(namespaces) + .isValidatingFacets(true) + .addTypeMappings(typeMappings) + .build(); + + assertNotNull(properties.getValidatedPrefixNamespaceUris()); + assertNotNull(properties.getTypeMappings()); + assertTrue("validating facets should be set", properties.isValidatingFacets()); + } +} http://git-wip-us.apache.org/repos/asf/olingo-odata2/blob/9e949e40/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/JSON_XMLErrorConsumerTest.java ---------------------------------------------------------------------- diff --git a/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/JSON_XMLErrorConsumerTest.java b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/JSON_XMLErrorConsumerTest.java new file mode 100644 index 0000000..b9488f6 --- /dev/null +++ b/odata2-lib/odata-client-core/src/test/java/org/apache/olingo/odata2/client/core/ep/JSON_XMLErrorConsumerTest.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * 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.odata2.client.core.ep; + +import static org.junit.Assert.assertEquals; + +import java.util.Locale; + +import org.apache.olingo.odata2.api.ep.EntityProviderException; +import org.apache.olingo.odata2.api.processor.ODataErrorContext; +import org.apache.olingo.odata2.client.api.ODataClient; +import org.apache.olingo.odata2.client.core.ep.deserializer.AbstractDeserializerTest; +import org.apache.olingo.odata2.testutil.helper.StringHelper; +import org.junit.Test; + +/** + * + */ +public class JSON_XMLErrorConsumerTest extends AbstractDeserializerTest { + + private static final String JSON = "application/json"; + private static final String XML = "application/xml"; + + @Test + public void readErrorDocumentJson() throws EntityProviderException { + ODataClient providerFacade = ODataClient.newInstance(); + String errorDoc = "{\"error\":{\"code\":\"ErrorCode\",\"message\":{\"lang\":\"en-US\",\"value\":\"Message\"}}}"; + ODataErrorContext errorContext = providerFacade.createDeserializer(JSON). + readErrorDocument(StringHelper.encapsulate(errorDoc)); + // + assertEquals("Wrong content type", "application/json", errorContext.getContentType()); + assertEquals("Wrong message", "Message", errorContext.getMessage()); + assertEquals("Wrong error code", "ErrorCode", errorContext.getErrorCode()); + assertEquals("Wrong locale for lang", Locale.US, errorContext.getLocale()); + } + + @Test + public void readErrorDocumentXml() throws EntityProviderException { + ODataClient providerFacade = ODataClient.newInstance(); + String errorDoc = + "<?xml version='1.0' encoding='UTF-8'?>\n" + + "<error xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">\n" + + "\t<code>ErrorCode</code>\n" + + "\t<message xml:lang=\"en-US\">Message</message>\n" + + "</error>"; + ODataErrorContext errorContext = providerFacade.createDeserializer(XML). + readErrorDocument(StringHelper.encapsulate(errorDoc)); + // + assertEquals("Wrong content type", "application/xml", errorContext.getContentType()); + assertEquals("Wrong message", "Message", errorContext.getMessage()); + assertEquals("Wrong error code", "ErrorCode", errorContext.getErrorCode()); + assertEquals("Wrong locale for lang", Locale.US, errorContext.getLocale()); + } +}