You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@olingo.apache.org by fm...@apache.org on 2013/07/26 13:22:45 UTC
[40/51] [partial] initial commit
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/data/ODataExpressionParserTest.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/data/ODataExpressionParserTest.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/data/ODataExpressionParserTest.java
new file mode 100644
index 0000000..b4d7fc3
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/data/ODataExpressionParserTest.java
@@ -0,0 +1,586 @@
+/*******************************************************************************
+ * 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.processor.core.jpa.access.data;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmFacets;
+import org.apache.olingo.odata2.api.edm.EdmLiteralKind;
+import org.apache.olingo.odata2.api.edm.EdmMapping;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmSimpleType;
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
+import org.apache.olingo.odata2.api.edm.EdmTypeKind;
+import org.apache.olingo.odata2.api.edm.EdmTyped;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.api.uri.KeyPredicate;
+import org.apache.olingo.odata2.api.uri.expression.BinaryExpression;
+import org.apache.olingo.odata2.api.uri.expression.BinaryOperator;
+import org.apache.olingo.odata2.api.uri.expression.CommonExpression;
+import org.apache.olingo.odata2.api.uri.expression.ExpressionKind;
+import org.apache.olingo.odata2.api.uri.expression.FilterExpression;
+import org.apache.olingo.odata2.api.uri.expression.LiteralExpression;
+import org.apache.olingo.odata2.api.uri.expression.MemberExpression;
+import org.apache.olingo.odata2.api.uri.expression.MethodExpression;
+import org.apache.olingo.odata2.api.uri.expression.MethodOperator;
+import org.apache.olingo.odata2.api.uri.expression.PropertyExpression;
+import org.apache.olingo.odata2.api.uri.expression.UnaryExpression;
+import org.apache.olingo.odata2.api.uri.expression.UnaryOperator;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.processor.core.jpa.ODataExpressionParser;
+import org.apache.olingo.odata2.processor.core.jpa.common.ODataJPATestConstants;
+
+public class ODataExpressionParserTest {
+
+ private static final String EXPECTED_STR_1 = "gwt1.SalesOrder = 1234";
+ private static final String EXPECTED_STR_2 = "gwt1.SalesOrder >= 1234 AND gwt1.SalesABC <> XYZ";
+ private static final String EXPECTED_STR_3 = "gwt1.SalesOrder >= 1234 OR gwt1.SalesABC <> XYZ";
+ private static final String EXPECTED_STR_4 = "gwt1.SalesOrder < 1234 AND gwt1.SalesABC <= XYZ";
+ private static final String EXPECTED_STR_5 = "gwt1.LineItems > 2345 AND gwt1.SalesOrder >= Amazon";
+ private static final String EXPECTED_STR_6 = "gwt1.Address.city = \'City_3\'";
+ private static final String EXPECTED_STR_7 = "gwt1.Address.city.area = \'BTM\'";
+ private static final String EXPECTED_STR_8 = "gwt1.field1 = 1 AND gwt1.field2 = 'abc'";
+ private static final String EXPECTED_STR_9 = "gwt1.BuyerAddress, gwt1.BuyerName, gwt1.BuyerId";
+ private static final String EXPECTED_STR_10 = "gwt1.SalesOrder";
+ private static final String EXPECTED_STR_11 = "NOT(gwt1.deliveryStatus)";
+ private static final String EXPECTED_STR_12 = "(CASE WHEN gwt1.currencyCode LIKE '%Ru%' THEN TRUE ELSE FALSE END) = true";
+ private static final String EXPECTED_STR_13 = "SUBSTRING(gwt1.currencyCode, 1 + 1 , 2) = 'NR'";
+ private static final String EXPECTED_STR_14 = "LOWER(gwt1.currencyCode) = 'inr rupees'";
+ private static final String EXPECTED_STR_15 = "(CASE WHEN LOWER(gwt1.currencyCode) LIKE '%nr rupees%' THEN TRUE ELSE FALSE END) = true";
+ private static final String EXPECTED_STR_16 = "(CASE WHEN gwt1.currencyCode LIKE '%INR%' THEN TRUE ELSE FALSE END) = true";
+ private static final String EXPECTED_STR_17 = "(CASE WHEN LOWER(gwt1.currencyCode) LIKE '%nr rupees%' THEN TRUE ELSE FALSE END) = true";
+
+ private static final String ADDRESS = "Address";
+ private static final String CITY = "city";
+ private static final String AREA = "area";
+ private static final String SALES_ORDER = "SalesOrder";
+ private static final String SALES_ABC = "SalesABC";
+ private static final String SAMPLE_DATA_1 = "1234";
+ private static final String SAMPLE_DATA_2 = "2345";
+ private static final String SAMPLE_DATA_XYZ = "XYZ";
+ private static final String SAMPLE_DATA_BTM = "\'BTM\'";
+ private static final String SAMPLE_DATA_CITY_3 = "\'City_3\'";
+ private static final String SAMPLE_DATA_LINE_ITEMS = "LineItems";
+ private static final String SAMPLE_DATA_AMAZON = "Amazon";
+ private static final String SAMPLE_DATA_FIELD1 = "field1";
+ private static final String SAMPLE_DATA_FIELD2 = "field2";
+
+ private static final String TABLE_ALIAS = "gwt1"; //$NON-NLS-1$
+
+ @Test
+ public void testParseWhereExpression() {
+ try {
+ String parsedStr = ODataJPATestConstants.EMPTY_STRING;
+ // Simple Binary query -
+ parsedStr = ODataExpressionParser.parseToJPAWhereExpression(
+ getBinaryExpressionMockedObj(BinaryOperator.EQ, ExpressionKind.PROPERTY,
+ SALES_ORDER, SAMPLE_DATA_1), TABLE_ALIAS);
+
+ assertEquals(EXPECTED_STR_1, parsedStr);
+ // complex query -
+ parsedStr = ODataJPATestConstants.EMPTY_STRING;
+
+ CommonExpression exp1 = getBinaryExpressionMockedObj(
+ BinaryOperator.GE, ExpressionKind.PROPERTY, SALES_ORDER, SAMPLE_DATA_1);
+ CommonExpression exp2 = getBinaryExpressionMockedObj(
+ BinaryOperator.NE, ExpressionKind.PROPERTY, SALES_ABC, SAMPLE_DATA_XYZ);
+ parsedStr = ODataExpressionParser.parseToJPAWhereExpression(
+ getBinaryExpression(exp1, BinaryOperator.AND, exp2), TABLE_ALIAS);
+ assertEquals(EXPECTED_STR_2, parsedStr);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ @Test
+ public void testMoreThanOneBinaryExpression() {
+ // complex query -
+ String parsedStr = ODataJPATestConstants.EMPTY_STRING;
+ CommonExpression exp1 = getBinaryExpressionMockedObj(BinaryOperator.GE,
+ ExpressionKind.PROPERTY, SALES_ORDER, SAMPLE_DATA_1);
+ CommonExpression exp2 = getBinaryExpressionMockedObj(BinaryOperator.NE,
+ ExpressionKind.PROPERTY, SALES_ABC, SAMPLE_DATA_XYZ);
+ try {
+ parsedStr = ODataExpressionParser.parseToJPAWhereExpression(
+ getBinaryExpression(exp1, BinaryOperator.AND, exp2),
+ TABLE_ALIAS);
+ assertEquals(EXPECTED_STR_2, parsedStr);
+ parsedStr = ODataExpressionParser
+ .parseToJPAWhereExpression(
+ getBinaryExpression(exp1, BinaryOperator.OR, exp2),
+ TABLE_ALIAS);
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ assertEquals(EXPECTED_STR_3, parsedStr);
+ }
+
+ @Test
+ public void testParseFilterExpression() {
+ try {
+ assertEquals(EXPECTED_STR_10,
+ ODataExpressionParser.parseToJPAWhereExpression(
+ getFilterExpressionMockedObj(ExpressionKind.PROPERTY,
+ SALES_ORDER), TABLE_ALIAS));
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ @Test
+ public void testAllBinaryOperators() { // Test for all Binary Operators
+ // complex query -
+ String parsedStr1 = ODataJPATestConstants.EMPTY_STRING;
+ String parsedStr2 = ODataJPATestConstants.EMPTY_STRING;
+
+ CommonExpression exp1 = getBinaryExpressionMockedObj(BinaryOperator.LT,
+ ExpressionKind.PROPERTY, SALES_ORDER, SAMPLE_DATA_1);
+ CommonExpression exp2 = getBinaryExpressionMockedObj(BinaryOperator.LE,
+ ExpressionKind.PROPERTY, SALES_ABC, SAMPLE_DATA_XYZ);
+
+ try {
+ parsedStr1 = ODataExpressionParser.parseToJPAWhereExpression(
+ (BinaryExpression) getBinaryExpression(exp1,
+ BinaryOperator.AND, exp2), TABLE_ALIAS);
+ assertEquals(EXPECTED_STR_4, parsedStr1);
+
+ CommonExpression exp3 = getBinaryExpressionMockedObj(BinaryOperator.GT,
+ ExpressionKind.PROPERTY, SAMPLE_DATA_LINE_ITEMS, SAMPLE_DATA_2);
+ CommonExpression exp4 = getBinaryExpressionMockedObj(BinaryOperator.GE,
+ ExpressionKind.PROPERTY, SALES_ORDER, SAMPLE_DATA_AMAZON);
+
+ parsedStr2 = ODataExpressionParser.parseToJPAWhereExpression(
+ getBinaryExpression(exp3, BinaryOperator.AND, exp4),
+ TABLE_ALIAS);
+
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ assertEquals(EXPECTED_STR_5, parsedStr2);
+ }
+
+ @Test
+ public void testParseMemberExpression() {
+ try {
+ assertEquals(EXPECTED_STR_6,
+ ODataExpressionParser
+ .parseToJPAWhereExpression(
+ getBinaryExpression(
+ getMemberExpressionMockedObj(ADDRESS,
+ CITY),
+ BinaryOperator.EQ,
+ getLiteralExpressionMockedObj(SAMPLE_DATA_CITY_3)),
+ TABLE_ALIAS));
+ assertEquals(EXPECTED_STR_7,
+ ODataExpressionParser
+ .parseToJPAWhereExpression(
+ getBinaryExpression(
+ getMultipleMemberExpressionMockedObj(ADDRESS, CITY, AREA),
+ BinaryOperator.EQ,
+ getLiteralExpressionMockedObj(SAMPLE_DATA_BTM)),
+ TABLE_ALIAS));
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ @Test
+ public void testParseMethodExpression() {
+ try {
+ assertEquals(EXPECTED_STR_12,
+ ODataExpressionParser
+ .parseToJPAWhereExpression(
+ getBinaryExpression(
+ getMethodExpressionMockedObj(MethodOperator.SUBSTRINGOF, "'Ru'", "currencyCode", null, 2),
+ BinaryOperator.EQ,
+ getLiteralExpressionMockedObj("true")),
+ TABLE_ALIAS));
+ assertEquals(EXPECTED_STR_13,
+ ODataExpressionParser
+ .parseToJPAWhereExpression(
+ getBinaryExpression(
+ getMethodExpressionMockedObj(MethodOperator.SUBSTRING, "currencyCode", "1", "2", 3),
+ BinaryOperator.EQ,
+ getLiteralExpressionMockedObj("'NR'")),
+ TABLE_ALIAS));
+ assertEquals(EXPECTED_STR_14,
+ ODataExpressionParser
+ .parseToJPAWhereExpression(
+ getBinaryExpression(
+ getMethodExpressionMockedObj(MethodOperator.TOLOWER, "currencyCode", null, null, 1),
+ BinaryOperator.EQ,
+ getLiteralExpressionMockedObj("'inr rupees'")),
+ TABLE_ALIAS));
+ assertEquals(EXPECTED_STR_15,
+ ODataExpressionParser
+ .parseToJPAWhereExpression(
+ getBinaryExpression(
+ getMultipleMethodExpressionMockedObj(MethodOperator.SUBSTRINGOF, "'nr rupees'", MethodOperator.TOLOWER, "currencyCode", 2, 1),
+ BinaryOperator.EQ,
+ getLiteralExpressionMockedObj("true")),
+ TABLE_ALIAS));
+ assertEquals(EXPECTED_STR_16,
+ ODataExpressionParser
+ .parseToJPAWhereExpression(getFilterExpressionForFunctionsMockedObj(MethodOperator.SUBSTRINGOF, "'INR'", null, "currencyCode", 2, null)
+ /*getBinaryExpression(
+ getMemberExpressionMockedObj(ADDRESS,
+ CITY),
+ BinaryOperator.EQ,
+ getLiteralExpressionMockedObj(SAMPLE_DATA_CITY_3))*/,
+ TABLE_ALIAS));
+ assertEquals(EXPECTED_STR_17,
+ ODataExpressionParser
+ .parseToJPAWhereExpression(getFilterExpressionForFunctionsMockedObj(MethodOperator.SUBSTRINGOF, "'nr rupees'", MethodOperator.TOLOWER, "currencyCode", 2, 1)
+ /*getBinaryExpression(
+ getMemberExpressionMockedObj(ADDRESS,
+ CITY),
+ BinaryOperator.EQ,
+ getLiteralExpressionMockedObj(SAMPLE_DATA_CITY_3))*/,
+ TABLE_ALIAS));
+
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ private CommonExpression getMethodExpressionMockedObj(final MethodOperator methodOperator, final String firstName, final String secondName, final String thirdName, final Integer parameterCount) {
+
+ List<CommonExpression> parameters = new ArrayList<CommonExpression>();
+
+ if (methodOperator == MethodOperator.SUBSTRINGOF) {
+ parameters.add(getLiteralExpressionMockedObj(firstName));
+ parameters.add(getPropertyExpressionMockedObj(ExpressionKind.PROPERTY, secondName));
+ }
+
+ else if (methodOperator == MethodOperator.SUBSTRING) {
+ parameters.add(getPropertyExpressionMockedObj(ExpressionKind.PROPERTY, firstName));
+ parameters.add(getLiteralExpressionMockedObj(secondName));
+ parameters.add(getLiteralExpressionMockedObj(thirdName));
+ }
+ else if (methodOperator == MethodOperator.TOLOWER) {
+ parameters.add(getPropertyExpressionMockedObj(ExpressionKind.PROPERTY, firstName));
+ }
+
+ MethodExpression methodExpression = EasyMock.createMock(MethodExpression.class);
+
+ EasyMock.expect(methodExpression.getKind()).andStubReturn(ExpressionKind.METHOD);
+ EasyMock.expect(methodExpression.getMethod()).andStubReturn(methodOperator);
+ EasyMock.expect(methodExpression.getParameterCount()).andStubReturn(parameterCount);
+ EasyMock.expect(methodExpression.getParameters()).andStubReturn(parameters);
+ EasyMock.replay(methodExpression);
+
+ return methodExpression;
+ }
+
+ private CommonExpression getMultipleMethodExpressionMockedObj(final MethodOperator methodOperator1, final String firstName, final MethodOperator methodOperator2, final String secondName, final Integer parameterCount1, final Integer parameterCount2) {
+
+ //complex query
+ List<CommonExpression> parameters = new ArrayList<CommonExpression>();
+
+ parameters.add(getLiteralExpressionMockedObj(firstName));
+ parameters.add(getMethodExpressionMockedObj(methodOperator2, secondName, null, null, 1));
+
+ MethodExpression methodExpression = EasyMock.createMock(MethodExpression.class);
+
+ EasyMock.expect(methodExpression.getKind()).andStubReturn(ExpressionKind.METHOD);
+ EasyMock.expect(methodExpression.getMethod()).andStubReturn(methodOperator1);
+ EasyMock.expect(methodExpression.getParameterCount()).andStubReturn(parameterCount1);
+ EasyMock.expect(methodExpression.getParameters()).andStubReturn(parameters);
+ EasyMock.replay(methodExpression);
+
+ return methodExpression;
+ }
+
+ private CommonExpression getMultipleMemberExpressionMockedObj(final String string1, final String string2, final String string3) {
+
+ MemberExpression memberExpression = EasyMock.createMock(MemberExpression.class);
+
+ EasyMock.expect(memberExpression.getPath()).andStubReturn(getMemberExpressionMockedObj(string1, string2));
+ EasyMock.expect(memberExpression.getProperty()).andStubReturn(getPropertyExpressionMockedObj(ExpressionKind.PROPERTY, string3));
+ EasyMock.expect(memberExpression.getKind()).andStubReturn(ExpressionKind.MEMBER);
+ EasyMock.replay(memberExpression);
+
+ return memberExpression;
+ }
+
+ @Test
+ public void testParseUnaryExpression() {
+
+ UnaryExpression unaryExpression = getUnaryExpressionMockedObj(
+ getPropertyExpressionMockedObj(ExpressionKind.PROPERTY,
+ "deliveryStatus"),
+ org.apache.olingo.odata2.api.uri.expression.UnaryOperator.NOT);
+ try {
+ assertEquals(EXPECTED_STR_11, ODataExpressionParser.parseToJPAWhereExpression(
+ unaryExpression, TABLE_ALIAS));
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ }
+
+ private UnaryExpression getUnaryExpressionMockedObj(
+ final CommonExpression operand, final UnaryOperator unaryOperator) {
+ UnaryExpression unaryExpression = EasyMock
+ .createMock(UnaryExpression.class);
+ EasyMock.expect(unaryExpression.getKind())
+ .andStubReturn(ExpressionKind.UNARY);
+ EasyMock.expect(unaryExpression.getOperand()).andStubReturn(operand);
+ EasyMock.expect(unaryExpression.getOperator()).andStubReturn(unaryOperator);
+
+ EasyMock.replay(unaryExpression);
+ return unaryExpression;
+ }
+
+ private CommonExpression getMemberExpressionMockedObj(
+ final String pathUriLiteral, final String propertyUriLiteral) {
+ MemberExpression memberExpression = EasyMock
+ .createMock(MemberExpression.class);
+ EasyMock.expect(memberExpression.getPath())
+ .andStubReturn(
+ getPropertyExpressionMockedObj(ExpressionKind.PROPERTY,
+ pathUriLiteral));
+ EasyMock.expect(memberExpression.getProperty())
+ .andStubReturn(
+ getPropertyExpressionMockedObj(ExpressionKind.PROPERTY,
+ propertyUriLiteral));
+ EasyMock.expect(memberExpression.getKind())
+ .andStubReturn(ExpressionKind.MEMBER);
+
+ EasyMock.replay(memberExpression);
+ return memberExpression;
+ }
+
+ private LiteralExpression getLiteralExpressionMockedObj(final String uriLiteral) {
+ LiteralExpression rightOperandLiteralExpresion = EasyMock
+ .createMock(LiteralExpression.class);
+ EasyMock.expect(rightOperandLiteralExpresion.getKind())
+ .andStubReturn(ExpressionKind.LITERAL);
+ EasyMock.expect(rightOperandLiteralExpresion.getUriLiteral())
+ .andStubReturn(uriLiteral);// SAMPLE_DATA
+ EasyMock.expect(rightOperandLiteralExpresion.getEdmType())
+ .andStubReturn(getEdmSimpleTypeMockedObj(uriLiteral));
+ EasyMock.replay(rightOperandLiteralExpresion);
+ return rightOperandLiteralExpresion;
+
+ }
+
+ private EdmSimpleType getEdmSimpleTypeMockedObj(final String value) {
+ EdmSimpleType edmSimpleType = EasyMock.createMock(EdmSimpleType.class);
+ try {
+ EasyMock.expect(edmSimpleType.getName()).andReturn(value);
+ EasyMock.expect(edmSimpleType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.expect(edmSimpleType.valueOfString(value, EdmLiteralKind.URI,
+ getEdmFacetsMockedObj(), null)).andStubReturn(value);
+ EasyMock.expect(edmSimpleType.valueOfString(value, EdmLiteralKind.URI, null, null)).andStubReturn(value);
+ EasyMock.expect(edmSimpleType.valueToString(value, EdmLiteralKind.DEFAULT,
+ getEdmFacetsMockedObj())).andStubReturn(value);
+ EasyMock.expect(edmSimpleType.valueToString(value, EdmLiteralKind.DEFAULT, null)).andStubReturn(value);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.expect(edmSimpleType.getDefaultType()).andStubReturn(null);
+ EasyMock.replay(edmSimpleType);
+ return edmSimpleType;
+ }
+
+ private EdmFacets getEdmFacetsMockedObj() {
+ EdmFacets facets = EasyMock.createMock(EdmFacets.class);
+
+ EasyMock.replay(facets);
+ return facets;
+ }
+
+ private PropertyExpression getPropertyExpressionMockedObj(
+ final ExpressionKind expKind, final String propertyName) {
+ PropertyExpression leftOperandPropertyExpresion = EasyMock.createMock(PropertyExpression.class);
+ EasyMock.expect(leftOperandPropertyExpresion.getKind()).andStubReturn(ExpressionKind.PROPERTY);
+ EasyMock.expect(leftOperandPropertyExpresion.getPropertyName()).andStubReturn(propertyName);
+ EasyMock.expect(leftOperandPropertyExpresion.getEdmProperty()).andStubReturn(getEdmTypedMockedObj(propertyName));
+ EasyMock.replay(leftOperandPropertyExpresion);
+ return leftOperandPropertyExpresion;
+ }
+
+ private EdmTyped getEdmTypedMockedObj(final String propertyName) {
+ EdmProperty mockedEdmProperty = EasyMock.createMock(EdmProperty.class);
+ try {
+ EasyMock.expect(mockedEdmProperty.getMapping()).andStubReturn(getEdmMappingMockedObj(propertyName));
+ EasyMock.replay(mockedEdmProperty);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ return mockedEdmProperty;
+ }
+
+ private EdmMapping getEdmMappingMockedObj(final String propertyName) {
+ EdmMapping mockedEdmMapping = EasyMock.createMock(EdmMapping.class);
+ EasyMock.expect(mockedEdmMapping.getInternalName())
+ .andStubReturn(propertyName);
+ EasyMock.replay(mockedEdmMapping);
+ return mockedEdmMapping;
+ }
+
+ private BinaryExpression getBinaryExpressionMockedObj(
+ final BinaryOperator operator, final ExpressionKind leftOperandExpKind,
+ final String propertyName, final String literalStr) {
+ BinaryExpression binaryExpression = EasyMock
+ .createMock(BinaryExpression.class);
+ EasyMock.expect(binaryExpression.getKind())
+ .andStubReturn(ExpressionKind.BINARY);
+ EasyMock.expect(binaryExpression.getLeftOperand())
+ .andStubReturn(
+ getPropertyExpressionMockedObj(leftOperandExpKind,
+ propertyName));
+ EasyMock.expect(binaryExpression.getOperator()).andStubReturn(operator);
+ EasyMock.expect(binaryExpression.getRightOperand())
+ .andStubReturn(getLiteralExpressionMockedObj(literalStr));
+
+ EasyMock.replay(binaryExpression);
+ return binaryExpression;
+ }
+
+ private FilterExpression getFilterExpressionMockedObj(
+ final ExpressionKind leftOperandExpKind, final String propertyName) {
+ FilterExpression filterExpression = EasyMock
+ .createMock(FilterExpression.class);
+ EasyMock.expect(filterExpression.getKind())
+ .andStubReturn(ExpressionKind.FILTER);
+ EasyMock.expect(filterExpression.getExpression())
+ .andStubReturn(
+ getPropertyExpressionMockedObj(leftOperandExpKind,
+ propertyName));
+
+ EasyMock.replay(filterExpression);
+ return filterExpression;
+ }
+
+ private FilterExpression getFilterExpressionForFunctionsMockedObj(
+ final MethodOperator methodOperator1, final String firstName, final MethodOperator methodOperator2, final String secondName, final Integer parameterCount1, final Integer parameterCount2) {
+ //default value handling of SUBSTRINGOF
+ FilterExpression filterExpression = EasyMock
+ .createMock(FilterExpression.class);
+ EasyMock.expect(filterExpression.getKind())
+ .andStubReturn(ExpressionKind.FILTER);
+ if ((methodOperator2 != null) && (parameterCount2 != null)) {
+ EasyMock.expect(filterExpression.getExpression())
+ .andStubReturn(
+ getMultipleMethodExpressionMockedObj(methodOperator1, firstName, methodOperator2, secondName, parameterCount1, parameterCount2));
+ }
+ else {
+ EasyMock.expect(filterExpression.getExpression())
+ .andStubReturn(
+ getMethodExpressionMockedObj(methodOperator1, firstName, secondName, null, parameterCount1));
+ }
+
+ EasyMock.replay(filterExpression);
+ return filterExpression;
+ }
+
+ private CommonExpression getBinaryExpression(
+ final CommonExpression leftOperand, final BinaryOperator operator,
+ final CommonExpression rightOperand) {
+ BinaryExpression binaryExpression = EasyMock
+ .createMock(BinaryExpression.class);
+ EasyMock.expect(binaryExpression.getKind())
+ .andStubReturn(ExpressionKind.BINARY);
+ EasyMock.expect(binaryExpression.getLeftOperand())
+ .andStubReturn(leftOperand);
+ EasyMock.expect(binaryExpression.getRightOperand())
+ .andStubReturn(rightOperand);
+ EasyMock.expect(binaryExpression.getOperator()).andStubReturn(operator);
+
+ EasyMock.replay(binaryExpression);
+ return binaryExpression;
+ }
+
+ @Test
+ public void testParseKeyPredicates() {
+ // Setting up the expected value
+ KeyPredicate keyPredicate1 = EasyMock.createMock(KeyPredicate.class);
+ EdmProperty kpProperty1 = EasyMock.createMock(EdmProperty.class);
+ EasyMock.expect(keyPredicate1.getLiteral()).andStubReturn("1");
+ KeyPredicate keyPredicate2 = EasyMock.createMock(KeyPredicate.class);
+ EdmProperty kpProperty2 = EasyMock.createMock(EdmProperty.class);
+ EasyMock.expect(keyPredicate2.getLiteral()).andStubReturn("abc");
+ EdmMapping edmMapping = EasyMock.createMock(EdmMapping.class);
+ try {
+ EasyMock.expect(kpProperty1.getName()).andStubReturn(SAMPLE_DATA_FIELD1);
+ EasyMock.expect(kpProperty1.getType()).andStubReturn(
+ EdmSimpleTypeKind.Int32.getEdmSimpleTypeInstance());
+ EasyMock.expect(kpProperty2.getName()).andStubReturn(SAMPLE_DATA_FIELD2);
+ EasyMock.expect(kpProperty2.getType()).andStubReturn(
+ EdmSimpleTypeKind.String.getEdmSimpleTypeInstance());
+ EasyMock.expect(keyPredicate1.getProperty()).andStubReturn(kpProperty1);
+ EasyMock.expect(kpProperty1.getMapping()).andReturn(edmMapping);
+ EasyMock.expect(edmMapping.getInternalName()).andReturn(SAMPLE_DATA_FIELD1);
+ EasyMock.expect(keyPredicate2.getProperty()).andStubReturn(kpProperty2);
+ EasyMock.expect(kpProperty2.getMapping()).andReturn(edmMapping);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.expect(edmMapping.getInternalName()).andReturn(SAMPLE_DATA_FIELD2);
+ EasyMock.replay(edmMapping);
+ EasyMock.replay(kpProperty1, keyPredicate1, kpProperty2, keyPredicate2);
+
+ ArrayList<KeyPredicate> keyPredicates = new ArrayList<KeyPredicate>();
+ keyPredicates.add(keyPredicate1);
+ keyPredicates.add(keyPredicate2);
+ String str = null;
+
+ try {
+ str = ODataExpressionParser.parseKeyPredicates(keyPredicates,
+ TABLE_ALIAS);
+ } catch (ODataJPARuntimeException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ assertEquals(EXPECTED_STR_8, str);
+ }
+
+ @Test
+ public void testParseToJPASelectExpression() {
+
+ ArrayList<String> selectedFields = new ArrayList<String>();
+ selectedFields.add("BuyerAddress");
+ selectedFields.add("BuyerName");
+ selectedFields.add("BuyerId");
+
+ assertEquals(EXPECTED_STR_9,
+ ODataExpressionParser.parseToJPASelectExpression(TABLE_ALIAS,
+ selectedFields));
+ assertEquals(TABLE_ALIAS, ODataExpressionParser.parseToJPASelectExpression(
+ TABLE_ALIAS, null));
+
+ selectedFields.clear();
+ assertEquals(TABLE_ALIAS, ODataExpressionParser.parseToJPASelectExpression(
+ TABLE_ALIAS, selectedFields));
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/data/TestUtil.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/data/TestUtil.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/data/TestUtil.java
new file mode 100644
index 0000000..f238a99
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/data/TestUtil.java
@@ -0,0 +1,391 @@
+/*******************************************************************************
+ * 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.processor.core.jpa.access.data;
+
+import static org.junit.Assert.fail;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.easymock.EasyMock;
+
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.edm.EdmException;
+import org.apache.olingo.odata2.api.edm.EdmMapping;
+import org.apache.olingo.odata2.api.edm.EdmMultiplicity;
+import org.apache.olingo.odata2.api.edm.EdmNavigationProperty;
+import org.apache.olingo.odata2.api.edm.EdmProperty;
+import org.apache.olingo.odata2.api.edm.EdmType;
+import org.apache.olingo.odata2.api.edm.EdmTypeKind;
+import org.apache.olingo.odata2.api.edm.EdmTyped;
+import org.apache.olingo.odata2.api.ep.callback.WriteEntryCallbackContext;
+import org.apache.olingo.odata2.api.ep.callback.WriteFeedCallbackContext;
+import org.apache.olingo.odata2.api.uri.ExpandSelectTreeNode;
+import org.apache.olingo.odata2.api.uri.NavigationPropertySegment;
+import org.apache.olingo.odata2.processor.core.jpa.common.ODataJPATestConstants;
+import org.apache.olingo.odata2.processor.core.jpa.cud.SalesOrderLineItem;
+
+public class TestUtil {
+
+ public static ExpandSelectTreeNode mockExpandSelectTreeNode() {
+ ExpandSelectTreeNode nextExpandNode = EasyMock
+ .createMock(ExpandSelectTreeNode.class);
+ Map<String, ExpandSelectTreeNode> nextLink = null;
+ EasyMock.expect(nextExpandNode.getLinks()).andStubReturn(nextLink);
+ EasyMock.replay(nextExpandNode);
+ ExpandSelectTreeNode expandNode = EasyMock
+ .createMock(ExpandSelectTreeNode.class);
+ Map<String, ExpandSelectTreeNode> links = new HashMap<String, ExpandSelectTreeNode>();
+ links.put("SalesOrderLineItemDetails", nextExpandNode);
+ EasyMock.expect(expandNode.getLinks()).andStubReturn(links);
+ EasyMock.replay(expandNode);
+ return expandNode;
+ }
+
+ public static ExpandSelectTreeNode mockCurrentExpandSelectTreeNode() {
+ ExpandSelectTreeNode expandNode = EasyMock
+ .createMock(ExpandSelectTreeNode.class);
+ Map<String, ExpandSelectTreeNode> links = new HashMap<String, ExpandSelectTreeNode>();
+ EasyMock.expect(expandNode.getLinks()).andStubReturn(links);
+ EasyMock.replay(expandNode);
+ return expandNode;
+ }
+
+ public static List<ArrayList<NavigationPropertySegment>> getExpandList() {
+ List<ArrayList<NavigationPropertySegment>> expandList = new ArrayList<ArrayList<NavigationPropertySegment>>();
+ ArrayList<NavigationPropertySegment> expands = new ArrayList<NavigationPropertySegment>();
+ expands.add(mockNavigationPropertySegment());
+ expandList.add(expands);
+ return expandList;
+ }
+
+ public static WriteFeedCallbackContext getWriteFeedCallBackContext() {
+ URI selfLink = null;
+ WriteFeedCallbackContext writeContext = new WriteFeedCallbackContext();
+ try {
+ selfLink = new URI("SalesOrders(2L)/SalesOrderLineItemDetails");
+ writeContext.setSelfLink(selfLink);
+ writeContext
+ .setCurrentExpandSelectTreeNode(mockCurrentExpandSelectTreeNode());
+ writeContext.setNavigationProperty(mockNavigationProperty());
+ writeContext.setSourceEntitySet(mockSourceEntitySet());
+ writeContext.setEntryData(getFeedData());
+
+ } catch (URISyntaxException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ return writeContext;
+ }
+
+ public static WriteEntryCallbackContext getWriteEntryCallBackContext() {
+ WriteEntryCallbackContext writeContext = new WriteEntryCallbackContext();
+ writeContext
+ .setCurrentExpandSelectTreeNode(mockCurrentExpandSelectTreeNode());
+ writeContext.setNavigationProperty(mockNavigationProperty());
+ writeContext.setSourceEntitySet(mockSourceEntitySet());
+ writeContext.setEntryData(getEntryData());
+ return writeContext;
+ }
+
+ private static EdmEntitySet mockSourceEntitySet() {
+ EdmEntitySet entitySet = EasyMock.createMock(EdmEntitySet.class);
+ try {
+ EasyMock.expect(entitySet.getEntityType()).andStubReturn(
+ mockSourceEdmEntityType());
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(entitySet);
+ return entitySet;
+ }
+
+ public static EdmEntityType mockSourceEdmEntityType() {
+ EdmEntityType entityType = EasyMock.createMock(EdmEntityType.class);
+ EdmMapping mapping = EasyMock.createMock(EdmMapping.class);
+ List<String> navigationPropertyNames = new ArrayList<String>();
+ List<String> propertyNames = new ArrayList<String>();
+ propertyNames.add("id");
+ propertyNames.add("description");
+ navigationPropertyNames.add("SalesOrderLineItemDetails");
+ try {
+ EasyMock.expect(mapping.getInternalName()).andStubReturn(
+ "SalesOrderHeader");
+ EasyMock.replay(mapping);
+ EasyMock.expect(entityType.getName()).andStubReturn(
+ "SalesOrderHeader");
+ EasyMock.expect(entityType.getMapping()).andStubReturn(mapping);
+ EasyMock.expect(entityType.getNavigationPropertyNames())
+ .andStubReturn(navigationPropertyNames);
+ EasyMock.expect(entityType.getProperty("SalesOrderLineItemDetails"))
+ .andStubReturn(mockNavigationProperty());
+ EdmProperty property1 = mockEdmPropertyOfSource1();
+ EasyMock.expect(entityType.getProperty("id")).andStubReturn(
+ property1);
+ EasyMock.expect(entityType.getProperty("description"))
+ .andStubReturn(mockEdmPropertyOfSource2());
+ EasyMock.expect(entityType.getPropertyNames()).andStubReturn(
+ propertyNames);
+
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(entityType);
+ return entityType;
+ }
+
+ private static EdmTyped mockEdmPropertyOfSource2() {
+ EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
+ EdmType type = EasyMock.createMock(EdmType.class);
+ EasyMock.expect(type.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.replay(type);
+ EdmMapping mapping = EasyMock.createMock(EdmMapping.class);
+ EasyMock.expect(mapping.getInternalName()).andStubReturn("description");
+ EasyMock.replay(mapping);
+ try {
+ EasyMock.expect(edmProperty.getName()).andStubReturn("description");
+ EasyMock.expect(edmProperty.getType()).andStubReturn(type);
+ EasyMock.expect(edmProperty.getMapping()).andStubReturn(mapping);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(edmProperty);
+ return edmProperty;
+ }
+
+ private static EdmProperty mockEdmPropertyOfSource1() {
+ EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
+ EdmType type = EasyMock.createMock(EdmType.class);
+ EasyMock.expect(type.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.replay(type);
+ EdmMapping mapping = EasyMock.createMock(EdmMapping.class);
+ EasyMock.expect(mapping.getInternalName()).andStubReturn("id");
+ EasyMock.replay(mapping);
+ try {
+ EasyMock.expect(edmProperty.getName()).andStubReturn("id");
+ EasyMock.expect(edmProperty.getType()).andStubReturn(type);
+ EasyMock.expect(edmProperty.getMapping()).andStubReturn(mapping);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(edmProperty);
+ return edmProperty;
+ }
+
+ private static Map<String, Object> getFeedData() {
+ Map<String, Object> entryData = new HashMap<String, Object>();
+ entryData.put("id", 1);
+ entryData.put("description", "laptop");
+ List<SalesOrderLineItem> salesOrderLineItems = new ArrayList<SalesOrderLineItem>();
+ salesOrderLineItems.add(new SalesOrderLineItem(23));
+ salesOrderLineItems.add(new SalesOrderLineItem(45));
+ entryData.put("SalesOrderLineItemDetails", salesOrderLineItems);
+ return entryData;
+ }
+
+ private static Map<String, Object> getEntryData() {
+ Map<String, Object> entryData = new HashMap<String, Object>();
+ entryData.put("id", 1);
+ entryData.put("description", "laptop");
+ entryData.put("SalesOrderLineItemDetails", new SalesOrderLineItem(23));
+ return entryData;
+ }
+
+ private static NavigationPropertySegment mockNavigationPropertySegment() {
+ NavigationPropertySegment navigationPropSegment = EasyMock
+ .createMock(NavigationPropertySegment.class);
+ EasyMock.expect(navigationPropSegment.getNavigationProperty())
+ .andStubReturn(mockNavigationProperty());
+ EasyMock.expect(navigationPropSegment.getTargetEntitySet())
+ .andStubReturn(mockTargetEntitySet());
+ EasyMock.replay(navigationPropSegment);
+ return navigationPropSegment;
+ }
+
+ public static NavigationPropertySegment mockThirdNavigationPropertySegment()
+ {
+ NavigationPropertySegment navigationPropSegment = EasyMock
+ .createMock(NavigationPropertySegment.class);
+ EasyMock.expect(navigationPropSegment.getNavigationProperty())
+ .andStubReturn(mockSecondNavigationProperty());
+ EasyMock.expect(navigationPropSegment.getTargetEntitySet())
+ .andStubReturn(mockThirdEntitySet());
+ EasyMock.replay(navigationPropSegment);
+ return navigationPropSegment;
+ }
+
+ public static EdmNavigationProperty mockSecondNavigationProperty() {
+ EdmNavigationProperty navigationProperty = EasyMock
+ .createMock(EdmNavigationProperty.class);
+ EdmMapping mapping = EasyMock.createMock(EdmMapping.class);
+ EasyMock.expect(mapping.getInternalName()).andStubReturn(
+ "materials");
+ EasyMock.replay(mapping);
+ try {
+ EasyMock.expect(navigationProperty.getMultiplicity())
+ .andStubReturn(EdmMultiplicity.ONE);
+ EasyMock.expect(navigationProperty.getMapping()).andStubReturn(
+ mapping);
+ EasyMock.expect(navigationProperty.getName()).andStubReturn(
+ "MaterialDetails");
+ EasyMock.expect(navigationProperty.getFromRole()).andStubReturn("SalesOrderLineItem");
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(navigationProperty);
+ return navigationProperty;
+ }
+
+ public static EdmEntitySet mockTargetEntitySet() {
+ EdmEntitySet entitySet = EasyMock.createMock(EdmEntitySet.class);
+ try {
+ EasyMock.expect(entitySet.getEntityType()).andStubReturn(
+ mockTargetEdmEntityType());
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(entitySet);
+ return entitySet;
+ }
+
+ public static EdmEntitySet mockThirdEntitySet() {
+ EdmEntitySet entitySet = EasyMock.createMock(EdmEntitySet.class);
+ try {
+ EasyMock.expect(entitySet.getEntityType()).andStubReturn(
+ mockThirdEdmEntityType());
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(entitySet);
+ return entitySet;
+
+ }
+
+ private static EdmEntityType mockThirdEdmEntityType() {
+ EdmEntityType entityType = EasyMock.createMock(EdmEntityType.class);
+ EdmMapping mapping = EasyMock.createMock(EdmMapping.class);
+
+ List<String> propertyNames = new ArrayList<String>();
+ propertyNames.add("price");
+ try {
+ EasyMock.expect(mapping.getInternalName()).andStubReturn(
+ "Material");
+ EasyMock.replay(mapping);
+ EasyMock.expect(entityType.getName()).andStubReturn(
+ "Material");
+ EasyMock.expect(entityType.getMapping()).andStubReturn(mapping);
+ EdmProperty property = mockEdmPropertyOfTarget();
+ EasyMock.expect(entityType.getProperty("price")).andStubReturn(
+ property);
+ EasyMock.expect(entityType.getPropertyNames()).andStubReturn(
+ propertyNames);
+
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(entityType);
+ return entityType;
+ }
+
+ public static EdmEntityType mockTargetEdmEntityType() {
+ EdmEntityType entityType = EasyMock.createMock(EdmEntityType.class);
+ EdmMapping mapping = EasyMock.createMock(EdmMapping.class);
+
+ List<String> propertyNames = new ArrayList<String>();
+ propertyNames.add("price");
+ try {
+ EasyMock.expect(mapping.getInternalName()).andStubReturn(
+ "SalesOrderLineItem");
+ EasyMock.replay(mapping);
+ EasyMock.expect(entityType.getName()).andStubReturn(
+ "SalesOrderLineItem");
+ EasyMock.expect(entityType.getMapping()).andStubReturn(mapping);
+ EdmProperty property = mockEdmPropertyOfTarget();
+ EasyMock.expect(entityType.getProperty("price")).andStubReturn(
+ property);
+ EasyMock.expect(entityType.getPropertyNames()).andStubReturn(
+ propertyNames);
+
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(entityType);
+ return entityType;
+ }
+
+ private static EdmProperty mockEdmPropertyOfTarget() {
+ EdmProperty edmProperty = EasyMock.createMock(EdmProperty.class);
+
+ EdmType type = EasyMock.createMock(EdmType.class);
+ EasyMock.expect(type.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.replay(type);
+ EdmMapping mapping = EasyMock.createMock(EdmMapping.class);
+ EasyMock.expect(mapping.getInternalName()).andStubReturn("price");
+ EasyMock.replay(mapping);
+ try {
+ EasyMock.expect(edmProperty.getName()).andStubReturn("price");
+ EasyMock.expect(edmProperty.getType()).andStubReturn(type);
+ EasyMock.expect(edmProperty.getMapping()).andStubReturn(mapping);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(edmProperty);
+ return edmProperty;
+ }
+
+ public static EdmNavigationProperty mockNavigationProperty() {
+ EdmNavigationProperty navigationProperty = EasyMock
+ .createMock(EdmNavigationProperty.class);
+ EdmMapping mapping = EasyMock.createMock(EdmMapping.class);
+ EasyMock.expect(mapping.getInternalName()).andStubReturn(
+ "salesOrderLineItems");
+ EasyMock.replay(mapping);
+ try {
+ EasyMock.expect(navigationProperty.getMultiplicity())
+ .andStubReturn(EdmMultiplicity.MANY);
+ EasyMock.expect(navigationProperty.getMapping()).andStubReturn(
+ mapping);
+ EasyMock.expect(navigationProperty.getName()).andStubReturn(
+ "SalesOrderLineItemDetails");
+ EasyMock.expect(navigationProperty.getFromRole()).andStubReturn("SalesOrderHeader");
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(navigationProperty);
+ return navigationProperty;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmMappingModelServiceTest.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmMappingModelServiceTest.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmMappingModelServiceTest.java
new file mode 100644
index 0000000..e9b89dd
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmMappingModelServiceTest.java
@@ -0,0 +1,201 @@
+/*******************************************************************************
+ * 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.processor.core.jpa.access.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+import java.io.InputStream;
+
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import org.apache.olingo.odata2.processor.core.jpa.mock.ODataJPAContextMock;
+
+public class JPAEdmMappingModelServiceTest extends JPAEdmMappingModelService {
+
+ private static JPAEdmMappingModelServiceTest objJPAEdmMappingModelServiceTest;
+
+ private static final String MAPPING_FILE_CORRECT = "SalesOrderProcessingMappingModels.xml";
+ private static final String MAPPING_FILE_INCORRECT = "TEST.xml";
+
+ private static int VARIANT_MAPPING_FILE; // 0 FOR INCORRECT, 1 FOR CORRECT
+
+ private static String PERSISTENCE_UNIT_NAME_JPA = "salesorderprocessing";
+ private static String PERSISTENCE_UNIT_NAME_EDM = "SalesOrderProcessing";
+
+ private static String ENTITY_TYPE_NAME_JPA = "SalesOrderHeader";
+ private static String ENTITY_TYPE_NAME_EDM = "SalesOrder";
+ private static String ENTITY_SET_NAME_EDM = "SalesOrders";
+ private static String RELATIONSHIP_NAME_JPA = "salesOrderItems";
+ private static String RELATIONSHIP_NAME_EDM = "SalesOrderItemDetails";
+ private static String ATTRIBUTE_NAME_JPA = "netAmount";
+ private static String ATTRIBUTE_NAME_EDM = "NetAmount";
+ private static String EMBEDDABLE_TYPE_NAME_JPA = "SalesOrderItemKey";
+ private static String EMBEDDABLE_ATTRIBUTE_NAME_JPA = "liId";
+ private static String EMBEDDABLE_ATTRIBUTE_NAME_EDM = "ID";
+ private static String EMBEDDABLE_TYPE_2_NAME_JPA = "SalesOrderItemKey";
+
+ private static String ENTITY_TYPE_NAME_JPA_WRONG = "SalesOrderHeaders";
+ private static String RELATIONSHIP_NAME_JPA_WRONG = "value";
+ private static String EMBEDDABLE_TYPE_NAME_JPA_WRONG = "SalesOrderItemKeys";
+
+ public JPAEdmMappingModelServiceTest() {
+ super(ODataJPAContextMock.mockODataJPAContext());
+ }
+
+ @BeforeClass
+ public static void setup() {
+ objJPAEdmMappingModelServiceTest = new JPAEdmMappingModelServiceTest();
+ VARIANT_MAPPING_FILE = 1;
+ objJPAEdmMappingModelServiceTest.loadMappingModel();
+ }
+
+ @Test
+ public void testLoadMappingModel() {
+ VARIANT_MAPPING_FILE = 1;
+ loadMappingModel();
+ assertTrue(isMappingModelExists());
+ }
+
+ @Test
+ public void testLoadMappingModelNegative() {
+ VARIANT_MAPPING_FILE = 0;
+ loadMappingModel();
+ assertFalse(isMappingModelExists());
+ // reset it for other JUnits
+ VARIANT_MAPPING_FILE = 1;
+ loadMappingModel();
+ }
+
+ @Test
+ public void testIsMappingModelExists() {
+ assertTrue(objJPAEdmMappingModelServiceTest.isMappingModelExists());
+ }
+
+ @Test
+ public void testGetJPAEdmMappingModel() {
+ assertNotNull(objJPAEdmMappingModelServiceTest.getJPAEdmMappingModel());
+ }
+
+ @Test
+ public void testMapJPAPersistenceUnit() {
+ assertEquals(PERSISTENCE_UNIT_NAME_EDM, objJPAEdmMappingModelServiceTest.mapJPAPersistenceUnit(PERSISTENCE_UNIT_NAME_JPA));
+ }
+
+ @Test
+ public void testMapJPAPersistenceUnitNegative() {
+ assertNull(objJPAEdmMappingModelServiceTest.mapJPAPersistenceUnit(PERSISTENCE_UNIT_NAME_EDM));// Wrong value to bring null
+ }
+
+ @Test
+ public void testMapJPAEntityType() {
+ assertEquals(ENTITY_TYPE_NAME_EDM, objJPAEdmMappingModelServiceTest.mapJPAEntityType(ENTITY_TYPE_NAME_JPA));
+ }
+
+ @Test
+ public void testMapJPAEntityTypeNegative() {
+ assertNull(objJPAEdmMappingModelServiceTest.mapJPAEntityType(ENTITY_TYPE_NAME_JPA_WRONG));// Wrong value to bring null
+ }
+
+ @Test
+ public void testMapJPAEntitySet() {
+ assertEquals(ENTITY_SET_NAME_EDM, objJPAEdmMappingModelServiceTest.mapJPAEntitySet(ENTITY_TYPE_NAME_JPA));
+ }
+
+ @Test
+ public void testMapJPAEntitySetNegative() {
+ assertNull(objJPAEdmMappingModelServiceTest.mapJPAEntitySet(ENTITY_TYPE_NAME_JPA_WRONG));// Wrong value to bring null
+ }
+
+ @Test
+ public void testMapJPAAttribute() {
+ assertEquals(ATTRIBUTE_NAME_EDM, objJPAEdmMappingModelServiceTest.mapJPAAttribute(ENTITY_TYPE_NAME_JPA, ATTRIBUTE_NAME_JPA));
+ }
+
+ @Test
+ public void testMapJPAAttributeNegative() {
+ assertNull(objJPAEdmMappingModelServiceTest.mapJPAAttribute(ENTITY_TYPE_NAME_JPA, ATTRIBUTE_NAME_JPA + "AA"));// Wrong value to bring null
+ }
+
+ @Test
+ public void testMapJPARelationship() {
+ assertEquals(RELATIONSHIP_NAME_EDM, objJPAEdmMappingModelServiceTest.mapJPARelationship(ENTITY_TYPE_NAME_JPA, RELATIONSHIP_NAME_JPA));
+ }
+
+ @Test
+ public void testMapJPARelationshipNegative() {
+ assertNull(objJPAEdmMappingModelServiceTest.mapJPARelationship(ENTITY_TYPE_NAME_JPA, RELATIONSHIP_NAME_JPA_WRONG));// Wrong value to bring null
+ }
+
+ @Test
+ public void testMapJPAEmbeddableType() {
+ assertEquals("SalesOrderLineItemKey", objJPAEdmMappingModelServiceTest.mapJPAEmbeddableType("SalesOrderItemKey"));
+ }
+
+ @Test
+ public void testMapJPAEmbeddableTypeNegative() {
+ assertNull(objJPAEdmMappingModelServiceTest.mapJPAEmbeddableType(EMBEDDABLE_TYPE_NAME_JPA_WRONG));// Wrong value to bring null
+ }
+
+ @Test
+ public void testMapJPAEmbeddableTypeAttribute() {
+ assertEquals(EMBEDDABLE_ATTRIBUTE_NAME_EDM, objJPAEdmMappingModelServiceTest.mapJPAEmbeddableTypeAttribute(EMBEDDABLE_TYPE_NAME_JPA, EMBEDDABLE_ATTRIBUTE_NAME_JPA));
+ }
+
+ @Test
+ public void testMapJPAEmbeddableTypeAttributeNegative() {
+ assertNull(objJPAEdmMappingModelServiceTest.mapJPAEmbeddableTypeAttribute(EMBEDDABLE_TYPE_NAME_JPA_WRONG, EMBEDDABLE_ATTRIBUTE_NAME_JPA));
+ }
+
+ @Test
+ public void testCheckExclusionOfJPAEntityType() {
+ assertTrue(!objJPAEdmMappingModelServiceTest.checkExclusionOfJPAEntityType(ENTITY_TYPE_NAME_JPA));
+ }
+
+ @Test
+ public void testCheckExclusionOfJPAAttributeType() {
+ assertTrue(!objJPAEdmMappingModelServiceTest.checkExclusionOfJPAAttributeType(ENTITY_TYPE_NAME_JPA, ATTRIBUTE_NAME_JPA));
+ }
+
+ @Test
+ public void testCheckExclusionOfJPAEmbeddableType() {
+ assertTrue(!objJPAEdmMappingModelServiceTest.checkExclusionOfJPAEmbeddableType(EMBEDDABLE_TYPE_2_NAME_JPA));
+ }
+
+ @Test
+ public void testCheckExclusionOfJPAEmbeddableAttributeType() {
+ assertTrue(!objJPAEdmMappingModelServiceTest.checkExclusionOfJPAEmbeddableAttributeType(EMBEDDABLE_TYPE_NAME_JPA, EMBEDDABLE_ATTRIBUTE_NAME_JPA));
+ }
+
+ /**
+ * This method is for loading the xml file for testing.
+ */
+ @Override
+ protected InputStream loadMappingModelInputStream() {
+ if (VARIANT_MAPPING_FILE == 1) {
+ return ClassLoader.getSystemResourceAsStream(MAPPING_FILE_CORRECT);
+ } else {
+ return ClassLoader.getSystemResourceAsStream(MAPPING_FILE_INCORRECT);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmNameBuilderTest.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmNameBuilderTest.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmNameBuilderTest.java
new file mode 100644
index 0000000..d94a192
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPAEdmNameBuilderTest.java
@@ -0,0 +1,93 @@
+/*******************************************************************************
+ * 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.processor.core.jpa.access.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import org.easymock.EasyMock;
+import org.junit.Test;
+
+import org.apache.olingo.odata2.api.edm.provider.ComplexProperty;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmComplexPropertyView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmEntityTypeView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmPropertyView;
+import org.apache.olingo.odata2.processor.core.jpa.ODataJPAContextImpl;
+import org.apache.olingo.odata2.processor.core.jpa.mock.model.JPAAttributeMock;
+import org.apache.olingo.odata2.processor.core.jpa.mock.model.JPAEntityTypeMock;
+
+public class JPAEdmNameBuilderTest {
+
+ @SuppressWarnings("rawtypes")
+ @Test
+ public void testBuildJPAEdmComplexPropertyViewJPAEdmPropertyView() {
+ JPAEdmComplexPropertyView complexPropertyView = EasyMock.createMock(JPAEdmComplexPropertyView.class);
+ ComplexProperty complexProperty = new ComplexProperty();
+ EasyMock.expect(complexPropertyView.getEdmComplexProperty()).andStubReturn(complexProperty);
+ ODataJPAContextImpl oDataJPAContext = new ODataJPAContextImpl();
+ JPAEdmMappingModelService mappingModelService = new JPAEdmMappingModelService(oDataJPAContext);
+ EasyMock.expect(complexPropertyView.getJPAEdmMappingModelAccess()).andStubReturn(mappingModelService);
+
+ // Mocking EDMProperty
+ JPAEdmPropertyView propertyView = EasyMock.createMock(JPAEdmPropertyView.class);
+ JPAEdmEntityTypeView entityTypeView = EasyMock.createMock(JPAEdmEntityTypeView.class);
+ EasyMock.expect(entityTypeView.getJPAEntityType()).andStubReturn(new JEntityType());
+ EasyMock.replay(entityTypeView);
+ EasyMock.expect(propertyView.getJPAAttribute()).andStubReturn(new JAttribute());
+ EasyMock.expect(propertyView.getJPAEdmEntityTypeView()).andStubReturn(entityTypeView);
+ EasyMock.replay(complexPropertyView);
+ EasyMock.replay(propertyView);
+
+ JPAEdmNameBuilder.build(complexPropertyView, propertyView);
+ assertEquals("Id", complexPropertyView.getEdmComplexProperty().getName());
+
+ }
+
+ @SuppressWarnings("hiding")
+ class JAttribute<Object, String> extends JPAAttributeMock<Object, java.lang.String>
+ {
+
+ @Override
+ public java.lang.String getName() {
+ return "id";
+ }
+
+ @Override
+ public Class<java.lang.String> getJavaType() {
+ return java.lang.String.class;
+ }
+
+ }
+
+ class JEntityType<Object> extends JPAEntityTypeMock<Object>
+ {
+
+ @Override
+ public java.lang.String getName() {
+ return "SalesOrderHeader";
+ }
+
+ }
+
+ @Test
+ public void testBuildJPAEdmComplexPropertyViewString() {
+ assertTrue(true);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPATypeConvertorTest.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPATypeConvertorTest.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPATypeConvertorTest.java
new file mode 100644
index 0000000..7d0e9b1
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/access/model/JPATypeConvertorTest.java
@@ -0,0 +1,104 @@
+/*******************************************************************************
+ * 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.processor.core.jpa.access.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+import java.math.BigDecimal;
+import java.util.UUID;
+
+import org.junit.Test;
+
+import org.apache.olingo.odata2.api.edm.EdmSimpleTypeKind;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.core.jpa.common.ODataJPATestConstants;
+
+public class JPATypeConvertorTest {
+
+ private EdmSimpleTypeKind edmSimpleKindTypeString;
+ private EdmSimpleTypeKind edmSimpleKindTypeByteArr;
+ private EdmSimpleTypeKind edmSimpleKindTypeLong;
+ private EdmSimpleTypeKind edmSimpleKindTypeShort;
+ private EdmSimpleTypeKind edmSimpleKindTypeInteger;
+ private EdmSimpleTypeKind edmSimpleKindTypeDouble;
+ private EdmSimpleTypeKind edmSimpleKindTypeFloat;
+ private EdmSimpleTypeKind edmSimpleKindTypeBigDecimal;
+ private EdmSimpleTypeKind edmSimpleKindTypeByte;
+ private EdmSimpleTypeKind edmSimpleKindTypeBoolean;
+ private EdmSimpleTypeKind edmSimpleKindTypeUUID;
+
+ @Test
+ public void testConvertToEdmSimpleType() {
+ String str = "entity";
+ byte[] byteArr = new byte[3];
+ Long longObj = new Long(0);
+ Short shortObj = new Short((short) 0);
+ Integer integerObj = new Integer(0);
+ Double doubleObj = new Double(0);
+ Float floatObj = new Float(0);
+ BigDecimal bigDecimalObj = new BigDecimal(0);
+ Byte byteObj = new Byte((byte) 0);
+ Boolean booleanObj = Boolean.TRUE;
+ UUID uUID = new UUID(0, 0);
+
+ try {
+ edmSimpleKindTypeString = JPATypeConvertor
+ .convertToEdmSimpleType(str.getClass(), null);
+ edmSimpleKindTypeByteArr = JPATypeConvertor
+ .convertToEdmSimpleType(byteArr.getClass(), null);
+ edmSimpleKindTypeLong = JPATypeConvertor
+ .convertToEdmSimpleType(longObj.getClass(), null);
+ edmSimpleKindTypeShort = JPATypeConvertor
+ .convertToEdmSimpleType(shortObj.getClass(), null);
+ edmSimpleKindTypeInteger = JPATypeConvertor
+ .convertToEdmSimpleType(integerObj.getClass(), null);
+ edmSimpleKindTypeDouble = JPATypeConvertor
+ .convertToEdmSimpleType(doubleObj.getClass(), null);
+ edmSimpleKindTypeFloat = JPATypeConvertor
+ .convertToEdmSimpleType(floatObj.getClass(), null);
+ edmSimpleKindTypeBigDecimal = JPATypeConvertor
+ .convertToEdmSimpleType(bigDecimalObj.getClass(), null);
+ edmSimpleKindTypeByte = JPATypeConvertor
+ .convertToEdmSimpleType(byteObj.getClass(), null);
+ edmSimpleKindTypeBoolean = JPATypeConvertor
+ .convertToEdmSimpleType(booleanObj.getClass(), null);
+ /*edmSimpleKindTypeDate = JPATypeConvertor
+ .convertToEdmSimpleType(dateObj.getClass(),null);*/
+ edmSimpleKindTypeUUID = JPATypeConvertor
+ .convertToEdmSimpleType(uUID.getClass(), null);
+ } catch (ODataJPAModelException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage() + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ assertEquals(EdmSimpleTypeKind.String, edmSimpleKindTypeString);
+ assertEquals(EdmSimpleTypeKind.Binary, edmSimpleKindTypeByteArr);
+ assertEquals(EdmSimpleTypeKind.Int64, edmSimpleKindTypeLong);
+ assertEquals(EdmSimpleTypeKind.Int16, edmSimpleKindTypeShort);
+ assertEquals(EdmSimpleTypeKind.Int32, edmSimpleKindTypeInteger);
+ assertEquals(EdmSimpleTypeKind.Double, edmSimpleKindTypeDouble);
+ assertEquals(EdmSimpleTypeKind.Single, edmSimpleKindTypeFloat);
+ assertEquals(EdmSimpleTypeKind.Decimal, edmSimpleKindTypeBigDecimal);
+ assertEquals(EdmSimpleTypeKind.Byte, edmSimpleKindTypeByte);
+ assertEquals(EdmSimpleTypeKind.Boolean, edmSimpleKindTypeBoolean);
+ //assertEquals(EdmSimpleTypeKind.DateTime, edmSimpleKindTypeDate);
+ assertEquals(EdmSimpleTypeKind.Guid, edmSimpleKindTypeUUID);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/common/ODataJPATestConstants.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/common/ODataJPATestConstants.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/common/ODataJPATestConstants.java
new file mode 100644
index 0000000..066cc26
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/common/ODataJPATestConstants.java
@@ -0,0 +1,26 @@
+/*******************************************************************************
+ * 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.processor.core.jpa.common;
+
+public final class ODataJPATestConstants {
+
+ public static final String EXCEPTION_MSG_PART_1 = "Exception [ ";
+ public static final String EXCEPTION_MSG_PART_2 = " ] not expected";
+ public static final String EMPTY_STRING = "gwt1";
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/cud/JPACreateRequestTest.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/cud/JPACreateRequestTest.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/cud/JPACreateRequestTest.java
new file mode 100644
index 0000000..8e75313
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/cud/JPACreateRequestTest.java
@@ -0,0 +1,375 @@
+/*******************************************************************************
+ * 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.processor.core.jpa.cud;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.junit.Test;
+
+import org.apache.olingo.odata2.api.edm.EdmEntitySet;
+import org.apache.olingo.odata2.api.edm.EdmStructuralType;
+import org.apache.olingo.odata2.api.ep.entry.ODataEntry;
+import org.apache.olingo.odata2.api.uri.info.PostUriInfo;
+import org.apache.olingo.odata2.core.ep.entry.EntryMetadataImpl;
+import org.apache.olingo.odata2.core.ep.entry.MediaMetadataImpl;
+import org.apache.olingo.odata2.core.ep.entry.ODataEntryImpl;
+import org.apache.olingo.odata2.core.ep.feed.FeedMetadataImpl;
+import org.apache.olingo.odata2.core.ep.feed.ODataFeedImpl;
+import org.apache.olingo.odata2.core.uri.ExpandSelectTreeNodeImpl;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.processor.core.jpa.common.ODataJPATestConstants;
+
+public class JPACreateRequestTest {
+
+ @Test
+ public void testProcess() {
+
+ JPACreateRequest createRequest = new JPACreateRequest(JPATestUtil.mockMetaModel());
+ PostUriInfo postUriInfo = JPATestUtil.getPostUriInfo();
+ try {
+ createRequest.process(postUriInfo, null, "application/xml");
+ } catch (ODataJPARuntimeException e) {
+ if (e.isCausedByMessageException()) {
+ assertTrue(true);
+ } else {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ }
+
+ @Test
+ public void testGetSetterName()
+ {
+ JPACreateRequest createRequest = new JPACreateRequest();
+ Method method = getMethodForTesting("getSetterName", createRequest);
+ if (method != null)
+ {
+ method.setAccessible(true);
+ Object[] actualParams = { "salesOrderItems" };
+ try {
+ String result = (String) method.invoke(createRequest, actualParams);
+ assertEquals("setSalesOrderItems", result);
+ } catch (IllegalArgumentException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (InvocationTargetException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ }
+
+ @Test
+ public void testParse2JPAEntityValueMap() {
+ JPACreateRequest createRequest = new JPACreateRequest();
+ EdmStructuralType edmEntityType = JPATestUtil.getEdmStructuralType();
+ Object result = null;
+ try {
+ result = createRequest.parse2JPAEntityValueMap(JPATestUtil.getJPAEntity(), edmEntityType, JPATestUtil.getPropertyValueMap(), "SalesOrderHeader");
+ } catch (ODataJPARuntimeException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ assertNotNull(result);
+ assertEquals(((SalesOrderHeader) result).getId(), 1);
+
+ }
+
+ @Test
+ public void testCreateInlinedEntities()
+ {
+ JPACreateRequest createRequest = new JPACreateRequest(JPATestUtil.mockMetaModel());
+ Method method = getMethodForTesting("createInlinedEntities", createRequest);
+ if (method != null)
+ {
+ method.setAccessible(true);
+ EdmEntitySet edmEntitySet = JPATestUtil.mockSourceEdmEntitySet();
+ ODataEntryImpl odataEntry = createODataEntry();
+ Object[] actualParams = { JPATestUtil.getJPAEntity(), edmEntitySet, odataEntry, "SalesOrderHeader" };
+ try {
+
+ method.invoke(createRequest, actualParams);
+
+ } catch (IllegalArgumentException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (InvocationTargetException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ }
+
+ }
+
+ @Test
+ public void testGetSettersForNavigationProperties()
+ {
+ JPACreateRequest createRequest = new JPACreateRequest();
+ Method method = getMethodForTesting("getSettersForNavigationProperties", createRequest);
+ if (method != null)
+ {
+ method.setAccessible(true);
+ Map<String, Class<?>> relatedClassMap = new HashMap<String, Class<?>>();
+ relatedClassMap.put("salesOrderLineItems", SalesOrderLineItem.class);
+ Object[] actualParams = { JPATestUtil.getJPAEntity(), JPATestUtil.mockSourceEdmEntitySet(), relatedClassMap };
+ try {
+ @SuppressWarnings("unchecked")
+ List<HashMap<?, ?>> result = (List<HashMap<?, ?>>) method.invoke(createRequest, actualParams);
+ assertEquals(2, result.size());
+ } catch (IllegalArgumentException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (InvocationTargetException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ }
+ }
+
+ @Test
+ public void testSetNavigationProperties()
+ {
+ JPACreateRequest createRequest = new JPACreateRequest();
+ Method method = getMethodForTesting("setNavigationProperties", createRequest);
+
+ if (method != null)
+ {
+ method.setAccessible(true);
+ Map<String, Class<?>> relatedClassMap = new HashMap<String, Class<?>>();
+ relatedClassMap.put("salesOrderLineItems", SalesOrderLineItem.class);
+ Map<String, Object> propertyValueMap = new HashMap<String, Object>();
+ propertyValueMap.put("id", 1);
+ List<Object> objectList = new ArrayList<Object>();
+ objectList.add(new SalesOrderLineItem(23));
+ propertyValueMap.put("salesOrderLineItems", objectList);
+ Object[] actualParams = { JPATestUtil.getJPAEntity(), JPATestUtil.mockSourceEdmEntitySet(), propertyValueMap, "SalesOrderHeader", relatedClassMap };
+ try {
+ method.invoke(createRequest, actualParams);
+ //This method returns nothing. It only sets value in an object.
+ // If no exception is thrown then we assert true
+ assertTrue(true);
+ } catch (IllegalArgumentException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (InvocationTargetException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+ }
+
+ @Test
+ public void testPopulateEmbeddableKey()
+ {
+ JPACreateRequest createRequest = new JPACreateRequest();
+ Method method = getMethodForTesting("populateEmbeddableKey", createRequest);
+ if (method != null) {
+ method.setAccessible(true);
+ SalesOrderLineItemKey embeddableKeyObject = new SalesOrderLineItemKey();
+ String key = "soId";
+ String setterName = "setSoId";
+ Map<String, Object> propertyValueMap = new HashMap<String, Object>();
+ propertyValueMap.put("soId", 23);
+ propertyValueMap.put("liId", 45);
+ Field field = null;
+ Map<String, Class<?>> jpaEmbeddableObjectKeyMap = new HashMap<String, Class<?>>();
+ jpaEmbeddableObjectKeyMap.put("soId", int.class);
+ jpaEmbeddableObjectKeyMap.put("liId", int.class);
+ Object[] actualParams = { embeddableKeyObject, key, setterName, propertyValueMap };
+ try {
+ for (Field f : createRequest.getClass().getSuperclass().getDeclaredFields()) {
+ if (f.getName().equalsIgnoreCase("jpaEmbeddableKeyObjectMap")) {
+ field = f;
+ break;
+ }
+ }
+ field.setAccessible(true);
+ field.set(createRequest, jpaEmbeddableObjectKeyMap);
+ method.invoke(createRequest, actualParams);
+ } catch (IllegalArgumentException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (InvocationTargetException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (SecurityException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+ }
+
+ @Test
+ public void testPopulateEmbeddableKeyInvocationIssue()
+ {
+ JPACreateRequest createRequest = new JPACreateRequest();
+ Method method = getMethodForTesting("populateEmbeddableKey", createRequest);
+ if (method != null) {
+ method.setAccessible(true);
+ SalesOrderLineItemKey embeddableKeyObject = new SalesOrderLineItemKey();
+ String key = "soId";
+ String setterName = "setSoId";
+ Map<String, Object> propertyValueMap = new HashMap<String, Object>();
+ propertyValueMap.put("soId", "23");
+ propertyValueMap.put("liId", "45");
+ Field field = null;
+ Map<String, Class<?>> jpaEmbeddableObjectKeyMap = new HashMap<String, Class<?>>();
+ jpaEmbeddableObjectKeyMap.put("soId", int.class);
+ jpaEmbeddableObjectKeyMap.put("liId", int.class);
+ Object[] actualParams = { embeddableKeyObject, key, setterName, propertyValueMap };
+ // Object[] incorrectParams = {embeddableKeyObject,key,setterName,propertyValueMap};
+ try {
+ for (Field f : createRequest.getClass().getSuperclass().getDeclaredFields()) {
+ if (f.getName().equalsIgnoreCase("jpaEmbeddableKeyObjectMap")) {
+ field = f;
+ break;
+ }
+ }
+ field.setAccessible(true);
+ field.set(createRequest, jpaEmbeddableObjectKeyMap);
+ method.invoke(createRequest, actualParams);
+ } catch (IllegalArgumentException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (InvocationTargetException e) {
+ assertTrue(true);
+ } catch (SecurityException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+ }
+
+ @Test
+ public void testPopulateEmbeddableKeyNoSuchMethod()
+ {
+ JPACreateRequest createRequest = new JPACreateRequest();
+ Method method = getMethodForTesting("populateEmbeddableKey", createRequest);
+ if (method != null) {
+ method.setAccessible(true);
+ SalesOrderLineItemKey embeddableKeyObject = new SalesOrderLineItemKey();
+ String key = "soId";
+ String setterName = "setPoId";
+ Map<String, Object> propertyValueMap = new HashMap<String, Object>();
+ propertyValueMap.put("soId", 23);
+ propertyValueMap.put("liId", 45);
+ Field field = null;
+ Map<String, Class<?>> jpaEmbeddableObjectKeyMap = new HashMap<String, Class<?>>();
+ jpaEmbeddableObjectKeyMap.put("soId", int.class);
+ jpaEmbeddableObjectKeyMap.put("liId", int.class);
+ Object[] actualParams = { embeddableKeyObject, key, setterName, propertyValueMap };
+ try {
+ for (Field f : createRequest.getClass().getSuperclass().getDeclaredFields()) {
+ if (f.getName().equalsIgnoreCase("jpaEmbeddableKeyObjectMap")) {
+ field = f;
+ break;
+ }
+ }
+ field.setAccessible(true);
+ field.set(createRequest, jpaEmbeddableObjectKeyMap);
+ method.invoke(createRequest, actualParams);
+ } catch (IllegalArgumentException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (InvocationTargetException e) {
+ assertTrue(true);
+ } catch (SecurityException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+ }
+
+ private ODataEntryImpl createODataEntry() {
+ HashMap<String, Object> data = new HashMap<String, Object>();
+ HashMap<String, Object> data1 = new HashMap<String, Object>();
+ List<ODataEntry> oDataEntryList = new ArrayList<ODataEntry>();
+
+ MediaMetadataImpl mediaMetadata = new MediaMetadataImpl();
+ mediaMetadata.setContentType("application/xml");
+ EntryMetadataImpl entryMetadata = new EntryMetadataImpl();
+ entryMetadata.setId("http://localhost:8080/org.apache.olingo.odata2.processor.ref.web/SalesOrderProcessing.svc/SalesOrders(5L)");
+ ExpandSelectTreeNodeImpl expandSelectTree = new ExpandSelectTreeNodeImpl();
+ expandSelectTree.putLink("SalesOrderLineItemDetails", new ExpandSelectTreeNodeImpl());
+ expandSelectTree.setExpanded();
+ expandSelectTree.setAllExplicitly();
+ data1.put("id", 1);
+ data1.put("SalesOrderLineItemDetails", new SalesOrderLineItem(23));
+ ODataEntry lineItemOdataEntry = new ODataEntryImpl(data1, mediaMetadata, entryMetadata, expandSelectTree);
+ oDataEntryList.add(lineItemOdataEntry);
+ FeedMetadataImpl feedMetadata = new FeedMetadataImpl();
+ ODataFeedImpl odataFeedImpl = new ODataFeedImpl(oDataEntryList, feedMetadata);
+ data.put("id", 1);
+ data.put("SalesOrderLineItemDetails", odataFeedImpl);
+ ODataEntryImpl odataEntry = new ODataEntryImpl(data, mediaMetadata, entryMetadata, expandSelectTree);
+ odataEntry.setContainsInlineEntry(true);
+ return odataEntry;
+ }
+
+ private Method getMethodForTesting(final String methodName, final Object object)
+ {
+ Method method = null;
+ for (Method m : object.getClass().getDeclaredMethods()) {
+ if (m.getName().equals(methodName))
+ {
+ method = m;
+ break;
+ }
+ }
+ return method;
+ }
+
+}