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:47 UTC
[42/51] [partial] initial commit
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/model/JPAEdmReferentialConstraintRole.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/model/JPAEdmReferentialConstraintRole.java b/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/model/JPAEdmReferentialConstraintRole.java
new file mode 100644
index 0000000..54ccfec
--- /dev/null
+++ b/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/model/JPAEdmReferentialConstraintRole.java
@@ -0,0 +1,279 @@
+/*******************************************************************************
+ * 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.model;
+
+import java.lang.reflect.AnnotatedElement;
+import java.lang.reflect.ParameterizedType;
+import java.lang.reflect.Type;
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.persistence.JoinColumn;
+import javax.persistence.JoinColumns;
+import javax.persistence.metamodel.Attribute;
+
+import org.apache.olingo.odata2.api.edm.provider.Association;
+import org.apache.olingo.odata2.api.edm.provider.AssociationEnd;
+import org.apache.olingo.odata2.api.edm.provider.EntityType;
+import org.apache.olingo.odata2.api.edm.provider.Property;
+import org.apache.olingo.odata2.api.edm.provider.PropertyRef;
+import org.apache.olingo.odata2.api.edm.provider.ReferentialConstraintRole;
+import org.apache.olingo.odata2.processor.api.jpa.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmAssociationView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmEntityTypeView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmMapping;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmPropertyView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmReferentialConstraintRoleView;
+
+public class JPAEdmReferentialConstraintRole extends JPAEdmBaseViewImpl
+ implements JPAEdmReferentialConstraintRoleView {
+ /*
+ * Static Buffer
+ */
+ private static Attribute<?, ?> bufferedJPAAttribute = null;
+ private static ArrayList<JoinColumn> bufferedJoinColumns = new ArrayList<JoinColumn>();
+ /*
+ * Static Buffer
+ */
+
+ private boolean firstBuild = true;
+
+ private JPAEdmEntityTypeView entityTypeView;
+ private JPAEdmReferentialConstraintRoleView.RoleType roleType;
+
+ private Attribute<?, ?> jpaAttribute;
+ private ArrayList<String> jpaColumnNames;
+ private Association association;
+
+ private boolean roleExists = false;
+
+ private JPAEdmRefConstraintRoleBuilder builder;
+ private ReferentialConstraintRole currentRole;
+
+ public JPAEdmReferentialConstraintRole(
+ final JPAEdmReferentialConstraintRoleView.RoleType roleType,
+ final JPAEdmEntityTypeView entityTypeView,
+ final JPAEdmPropertyView propertyView,
+ final JPAEdmAssociationView associationView) {
+
+ super(entityTypeView);
+ this.entityTypeView = entityTypeView;
+ this.roleType = roleType;
+
+ jpaAttribute = propertyView.getJPAAttribute();
+ association = associationView.getEdmAssociation();
+
+ }
+
+ @Override
+ public boolean isExists() {
+ return roleExists;
+
+ }
+
+ @Override
+ public JPAEdmBuilder getBuilder() {
+ if (builder == null) {
+ builder = new JPAEdmRefConstraintRoleBuilder();
+ }
+
+ return builder;
+ }
+
+ @Override
+ public RoleType getRoleType() {
+ return roleType;
+ }
+
+ @Override
+ public ReferentialConstraintRole getEdmReferentialConstraintRole() {
+ return currentRole;
+ }
+
+ @Override
+ public String getJPAColumnName() {
+ return null;
+ }
+
+ @Override
+ public String getEdmEntityTypeName() {
+ return null;
+ }
+
+ @Override
+ public String getEdmAssociationName() {
+ return null;
+ }
+
+ private class JPAEdmRefConstraintRoleBuilder implements JPAEdmBuilder {
+
+ @Override
+ public void build() throws ODataJPAModelException {
+ if (firstBuild) {
+ firstBuild();
+ } else if (roleExists) {
+ try {
+ buildRole();
+ } catch (SecurityException e) {
+ throw ODataJPAModelException.throwException(
+ ODataJPAModelException.GENERAL.addContent(e
+ .getMessage()), e);
+ } catch (NoSuchFieldException e) {
+ throw ODataJPAModelException.throwException(
+ ODataJPAModelException.GENERAL.addContent(e
+ .getMessage()), e);
+ }
+ }
+
+ }
+
+ private void firstBuild() {
+ firstBuild = false;
+ isConsistent = false;
+
+ extractJoinColumns();
+
+ if (!roleExists) {
+ return;
+ }
+
+ jpaColumnNames = new ArrayList<String>();
+
+ for (JoinColumn joinColumn : bufferedJoinColumns) {
+ if (roleType == RoleType.PRINCIPAL) {
+ jpaColumnNames.add(joinColumn.referencedColumnName());
+ } else if (roleType == RoleType.DEPENDENT) {
+ jpaColumnNames.add(joinColumn.name());
+ }
+ }
+
+ }
+
+ private void buildRole() throws SecurityException, NoSuchFieldException {
+
+ if (currentRole == null) {
+ currentRole = new ReferentialConstraintRole();
+ String jpaAttributeType = null;
+ EntityType edmEntityType = null;
+
+ if (roleType == RoleType.PRINCIPAL) {
+ jpaAttributeType = jpaAttribute.getJavaType()
+ .getSimpleName();
+ if (jpaAttributeType.equals("List")) {
+ Type type = ((ParameterizedType) jpaAttribute
+ .getJavaMember().getDeclaringClass()
+ .getDeclaredField(jpaAttribute.getName())
+ .getGenericType()).getActualTypeArguments()[0];
+ int lastIndexOfDot = type.toString().lastIndexOf(".");
+ jpaAttributeType = type.toString().substring(
+ lastIndexOfDot + 1);
+ }
+ edmEntityType = entityTypeView
+ .searchEdmEntityType(jpaAttributeType);
+
+ }
+
+ else if (roleType == RoleType.DEPENDENT) {
+ edmEntityType = entityTypeView
+ .searchEdmEntityType(jpaAttribute
+ .getDeclaringType().getJavaType()
+ .getSimpleName());
+ }
+
+ List<PropertyRef> propertyRefs = new ArrayList<PropertyRef>();
+ if (edmEntityType != null) {
+ for (String columnName : jpaColumnNames) {
+ for (Property property : edmEntityType.getProperties()) {
+ if (columnName.equals(((JPAEdmMapping) property
+ .getMapping()).getJPAColumnName())) {
+ PropertyRef propertyRef = new PropertyRef();
+ propertyRef.setName(property.getName());
+ propertyRefs.add(propertyRef);
+ break;
+ }
+ }
+ }
+ currentRole.setPropertyRefs(propertyRefs);
+ if (propertyRefs.isEmpty()) {
+ isConsistent = false;
+ return;
+ }
+ AssociationEnd end = association.getEnd1();
+ if (end.getType().getName().equals(edmEntityType.getName())) {
+ currentRole.setRole(end.getRole());
+ isConsistent = true;
+ } else {
+ end = association.getEnd2();
+ if (end.getType().getName()
+ .equals(edmEntityType.getName())) {
+ currentRole.setRole(end.getRole());
+ isConsistent = true;
+ }
+ }
+ }
+
+ }
+ }
+
+ private void extractJoinColumns() {
+ /*
+ * Check against Static Buffer whether the join column was already
+ * extracted.
+ */
+ if (!jpaAttribute.equals(bufferedJPAAttribute)) {
+ bufferedJPAAttribute = jpaAttribute;
+ bufferedJoinColumns.clear();
+ } else if (bufferedJoinColumns.isEmpty()) {
+ roleExists = false;
+ return;
+ } else {
+ roleExists = true;
+ return;
+ }
+
+ AnnotatedElement annotatedElement = (AnnotatedElement) jpaAttribute
+ .getJavaMember();
+
+ if (annotatedElement == null) {
+ return;
+ }
+
+ JoinColumn joinColumn = annotatedElement
+ .getAnnotation(JoinColumn.class);
+ if (joinColumn == null) {
+ JoinColumns joinColumns = annotatedElement
+ .getAnnotation(JoinColumns.class);
+
+ if (joinColumns != null) {
+ JoinColumn[] joinColumnArray = joinColumns.value();
+
+ for (JoinColumn element : joinColumnArray) {
+ bufferedJoinColumns.add(element);
+ }
+ } else {
+ return;
+ }
+ } else {
+ bufferedJoinColumns.add(joinColumn);
+ }
+ roleExists = true;
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/model/JPAEdmSchema.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/model/JPAEdmSchema.java b/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/model/JPAEdmSchema.java
new file mode 100644
index 0000000..40a92a3
--- /dev/null
+++ b/jpa-core/src/main/java/org/apache/olingo/odata2/processor/core/jpa/model/JPAEdmSchema.java
@@ -0,0 +1,235 @@
+/*******************************************************************************
+ * 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.model;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+
+import org.apache.olingo.odata2.api.edm.provider.Association;
+import org.apache.olingo.odata2.api.edm.provider.ComplexType;
+import org.apache.olingo.odata2.api.edm.provider.EntityType;
+import org.apache.olingo.odata2.api.edm.provider.NavigationProperty;
+import org.apache.olingo.odata2.api.edm.provider.Schema;
+import org.apache.olingo.odata2.processor.api.jpa.access.JPAEdmBuilder;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmAssociationView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmComplexTypeView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmEntityContainerView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmEntitySetView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmEntityTypeView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmModelView;
+import org.apache.olingo.odata2.processor.api.jpa.model.JPAEdmSchemaView;
+import org.apache.olingo.odata2.processor.core.jpa.access.model.JPAEdmNameBuilder;
+
+public class JPAEdmSchema extends JPAEdmBaseViewImpl implements
+ JPAEdmSchemaView {
+
+ private Schema schema;
+ private JPAEdmComplexTypeView complexTypeView;
+ private JPAEdmEntityContainerView entityContainerView;
+ private JPAEdmAssociationView associationView = null;
+ private List<String> nonKeyComplexList = null;
+ private HashMap<Class<?>, String[]> customOperations = null;
+
+ public JPAEdmSchema(final JPAEdmModelView modelView) {
+ super(modelView);
+ if (nonKeyComplexList == null) {
+ nonKeyComplexList = new ArrayList<String>();
+ }
+ }
+
+ @Override
+ public List<String> getNonKeyComplexTypeList() {
+ return nonKeyComplexList;
+ }
+
+ @Override
+ public void addNonKeyComplexName(final String complexTypeName) {
+ nonKeyComplexList.add(complexTypeName);
+ }
+
+ @Override
+ public Schema getEdmSchema() {
+ return schema;
+ }
+
+ @Override
+ public JPAEdmEntityContainerView getJPAEdmEntityContainerView() {
+ return entityContainerView;
+ }
+
+ @Override
+ public JPAEdmComplexTypeView getJPAEdmComplexTypeView() {
+ return complexTypeView;
+ }
+
+ @Override
+ public JPAEdmBuilder getBuilder() {
+ if (builder == null) {
+ builder = new JPAEdmSchemaBuilder();
+ }
+
+ return builder;
+ }
+
+ @Override
+ public void clean() {
+ super.clean();
+ schema = null;
+ }
+
+ private class JPAEdmSchemaBuilder implements JPAEdmBuilder {
+ /*
+ *
+ * Each call to build method creates a new EDM Schema. The newly created
+ * schema is built with Entity Containers, associations, Complex Types
+ * and Entity Types.
+ *
+ * ************************************************************ Build
+ * EDM Schema - STEPS
+ * ************************************************************ 1) Build
+ * Name for EDM Schema 2) Build EDM Complex Types from JPA Embeddable
+ * Types 3) Add EDM Complex Types to EDM Schema 4) Build EDM Entity
+ * Container 5) Add EDM Entity Container to EDM Schema 6) Fetch Built
+ * EDM Entity Types from EDM Entity Container 7) Add EDM Entity Types to
+ * EDM Schema 8) Fetch Built EDM Association Sets from EDM Entity
+ * Container 9) Fetch Built EDM Associations from EDM Association Set
+ * 10) Add EDM Association to EDM Schema
+ * ************************************************************ Build
+ * EDM Schema - STEPS
+ * ************************************************************
+ */
+ @Override
+ public void build() throws ODataJPAModelException,
+ ODataJPARuntimeException {
+
+ schema = new Schema();
+ JPAEdmNameBuilder.build(JPAEdmSchema.this);
+
+ associationView = new JPAEdmAssociation(JPAEdmSchema.this);
+
+ complexTypeView = new JPAEdmComplexType(JPAEdmSchema.this);
+ complexTypeView.getBuilder().build();
+
+ if (getJPAEdmExtension() != null) {
+ getJPAEdmExtension()
+ .extend(JPAEdmSchema.this);
+ }
+
+ entityContainerView = new JPAEdmEntityContainer(JPAEdmSchema.this);
+ entityContainerView.getBuilder().build();
+ schema.setEntityContainers(entityContainerView
+ .getConsistentEdmEntityContainerList());
+
+ JPAEdmEntitySetView entitySetView = entityContainerView
+ .getJPAEdmEntitySetView();
+ if (entitySetView.isConsistent()
+ && entitySetView.getJPAEdmEntityTypeView() != null) {
+ JPAEdmEntityTypeView entityTypeView = entitySetView
+ .getJPAEdmEntityTypeView();
+ if (entityTypeView.isConsistent()
+ && !entityTypeView.getConsistentEdmEntityTypes()
+ .isEmpty()) {
+ schema.setEntityTypes(entityTypeView
+ .getConsistentEdmEntityTypes());
+ }
+ }
+ if (complexTypeView.isConsistent()) {
+ List<ComplexType> complexTypes = complexTypeView
+ .getConsistentEdmComplexTypes();
+ List<ComplexType> existingComplexTypes = new ArrayList<ComplexType>();
+ for (ComplexType complexType : complexTypes) {
+ if (complexType != null && nonKeyComplexList.contains(complexType.getName())) {//null check for exclude
+ existingComplexTypes.add(complexType);
+ }
+ }
+ if (!existingComplexTypes.isEmpty()) {
+ schema.setComplexTypes(existingComplexTypes);
+ }
+ }
+
+ List<String> existingAssociationList = new ArrayList<String>();
+ if (associationView.isConsistent()
+ && !associationView.getConsistentEdmAssociationList()
+ .isEmpty()) {
+
+ List<Association> consistentAssociationList = associationView
+ .getConsistentEdmAssociationList();
+ schema.setAssociations(consistentAssociationList);
+ for (Association association : consistentAssociationList) {
+ existingAssociationList.add(association.getName());
+ }
+
+ }
+ List<EntityType> entityTypes = entityContainerView
+ .getJPAEdmEntitySetView().getJPAEdmEntityTypeView()
+ .getConsistentEdmEntityTypes();
+ List<NavigationProperty> navigationProperties;
+ if (entityTypes != null && !entityTypes.isEmpty()) {
+ for (EntityType entityType : entityTypes) {
+
+ List<NavigationProperty> consistentNavigationProperties = null;
+ navigationProperties = entityType.getNavigationProperties();
+ if (navigationProperties != null) {
+ consistentNavigationProperties = new ArrayList<NavigationProperty>();
+ for (NavigationProperty navigationProperty : navigationProperties) {
+ if (existingAssociationList
+ .contains(navigationProperty
+ .getRelationship().getName())) {
+ consistentNavigationProperties
+ .add(navigationProperty);
+ }
+ }
+ if (consistentNavigationProperties.isEmpty()) {
+ entityType.setNavigationProperties(null);
+ } else {
+ entityType
+ .setNavigationProperties(consistentNavigationProperties);
+ }
+ }
+
+ }
+ }
+
+ }
+
+ }
+
+ @Override
+ public final JPAEdmAssociationView getJPAEdmAssociationView() {
+ return associationView;
+ }
+
+ @Override
+ public void registerOperations(final Class<?> customClass, final String[] methodNames) {
+ if (customOperations == null) {
+ customOperations = new HashMap<Class<?>, String[]>();
+ }
+
+ customOperations.put(customClass, methodNames);
+
+ }
+
+ @Override
+ public HashMap<Class<?>, String[]> getRegisteredOperations() {
+ return customOperations;
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/main/resources/jpaprocessor_msg.properties
----------------------------------------------------------------------
diff --git a/jpa-core/src/main/resources/jpaprocessor_msg.properties b/jpa-core/src/main/resources/jpaprocessor_msg.properties
new file mode 100644
index 0000000..5eb96fb
--- /dev/null
+++ b/jpa-core/src/main/resources/jpaprocessor_msg.properties
@@ -0,0 +1,61 @@
+#-------------------------------------------------------------------------------
+# 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.
+#-------------------------------------------------------------------------------
+# Fall Back translations
+#
+
+#JPA EDM Provider Errors
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.GENERAL="OData - JPA Metadata: Internal error [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.INNER_EXCEPTION="%1$s: %2$s.\n"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.INVALID_ENTITY_TYPE="OData - JPA Model Processor: Invalid Entity Type [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.INVALID_COMPLEX_TYPE="OData - JPA Model Processor: Invalid Complex Type [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.INVALID_ASSOCIATION ="OData - JPA Model Processor: Invalid Association [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.INVALID_ENTITYSET="OData - JPA Model Processor: Invalid Entity set [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.INVALID_ENTITYCONTAINER="OData - JPA Model Processor: Invalid Entity Container [%1$s]"
+org.apache.olingo.odata2.processor.jap.exception.ODataJPAModelException.INVALID_ASSOCIATION_SET="OData - JPA Model Processor: Invalid Association Set [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.INVALID_FUNC_IMPORT="OData - JPA Model Processor: Invalid Function Import [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.BUILDER_NULL="OData - JPA Model Processor: JPA Builder not initialized"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.FUNC_ENTITYSET_EXP="OData - JPA Model Processor: Entity Set expected for Function Import with return type as Entity Type with Multiplicity Many"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.FUNC_RETURN_TYPE_EXP="OData - JPA Model Processor: Return type expected. Class: [%1$s], Method: [%2$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.FUNC_RETURN_TYPE_ENTITY_NOT_FOUND="OData - JPA Model Processor: Return type not found. Class: [%1$s], Method: [%2$s], Type: [%3$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.FUNC_PARAM_NAME_EXP="OData - JPA Model Processor: Parameter Name for function import expected. Class: [%1$s]", Method: [%2$s]"
+
+#JPA Type converter Errors
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException.TYPE_NOT_SUPPORTED="OData - JPA Type Converter: Type [%1$s] not supported"
+
+#JPA Runtime Errors
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ENTITY_MANAGER_NOT_INITIALIZED="OData - JPA Service Factory: Java Persistence Entity Manager not initialized"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.GENERAL="OData - JPA Runtime: Internal error [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.INNER_EXCEPTION="%1$s: %2$s.\n"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.JOIN_CLAUSE_EXPECTED="OData - JPA Runtime: Join Clause expected"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.RESOURCE_NOT_FOUND="OData - JPA Runtime: Resource not found"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_ODATA_FILTER_CONDITION="OData - JPA Runtime: Filter condition not correct"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_JPQL_QUERY_CREATE="OData - JPA Runtime: JPA query syntax is not correct"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_JPQL_CREATE_REQUEST="OData - JPA Runtime: JPA create request is not correct"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_JPQL_UPDATE_REQUEST="OData - JPA Runtime: JPA update request is not correct"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_JPQL_DELETE_REQUEST="OData - JPA Runtime: JPA delete URL is not correct"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_JPQL_KEY_VALUE="Key parameter value is not correct for [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_JPQL_PARAM_VALUE="Parameter value is not correct for [%1$s]"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_JPQL_UNIQUE_CONSTRAINT="Violation of unique constraint"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_JPQL_INTEGRITY_CONSTRAINT="Integrity constraint violation"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.RELATIONSHIP_INVALID="OData - JPA Runtime: Invalid link"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.RESOURCE_X_NOT_FOUND="OData - JPA Runtime: Resource [%1$s] not found"
+
+#JPA Common Errors
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAException.ODATA_JPACTX_NULL="OData JPA: OData JPA Context cannot be null"
+org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException.ERROR_JPQLCTXBLDR_CREATE="OData JPA: Error creating JPQL Context Builder"
http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/ff2b0a0e/jpa-core/src/test/java/META-INF/MANIFEST.MF
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/META-INF/MANIFEST.MF b/jpa-core/src/test/java/META-INF/MANIFEST.MF
new file mode 100644
index 0000000..5e94951
--- /dev/null
+++ b/jpa-core/src/test/java/META-INF/MANIFEST.MF
@@ -0,0 +1,3 @@
+Manifest-Version: 1.0
+Class-Path:
+
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/JPAEntityParserTest.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/JPAEntityParserTest.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/JPAEntityParserTest.java
new file mode 100644
index 0000000..454b64e
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/JPAEntityParserTest.java
@@ -0,0 +1,652 @@
+/*******************************************************************************
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+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.easymock.EasyMock;
+import org.junit.Test;
+
+import org.apache.olingo.odata2.api.edm.EdmException;
+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.EdmStructuralType;
+import org.apache.olingo.odata2.api.edm.EdmType;
+import org.apache.olingo.odata2.api.edm.EdmTypeKind;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.processor.core.jpa.access.data.JPAEntityParser;
+import org.apache.olingo.odata2.processor.core.jpa.common.ODataJPATestConstants;
+
+public class JPAEntityParserTest {
+ /*
+ * TestCase - JPAResultParser is a singleton class Check if the same
+ * instance is returned when create method is called
+ */
+ @Test
+ public void testCreate() {
+ JPAEntityParser resultParser1 = JPAEntityParser.create();
+ JPAEntityParser resultParser2 = JPAEntityParser.create();
+
+ assertEquals(resultParser1, resultParser2);
+ }
+
+ @Test
+ public void testparse2EdmPropertyValueMap() {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ Object jpaEntity = new demoItem("abc", 10);
+ EdmStructuralType structuralType = EasyMock
+ .createMock(EdmStructuralType.class);
+ EdmProperty edmTyped = EasyMock.createMock(EdmProperty.class);
+ EdmType edmType = EasyMock.createMock(EdmType.class);
+ EdmProperty edmTyped01 = EasyMock.createMock(EdmProperty.class);
+ EdmType edmType01 = EasyMock.createMock(EdmType.class);
+ EdmMapping edmMapping = EasyMock.createMock(EdmMapping.class);
+ EdmMapping edmMapping01 = EasyMock.createMock(EdmMapping.class);
+
+ try {
+ EasyMock.expect(edmType.getKind())
+ .andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.expect(edmTyped.getName()).andStubReturn("identifier");
+ EasyMock.replay(edmType);
+ EasyMock.expect(edmMapping.getInternalName()).andStubReturn("id");
+ EasyMock.replay(edmMapping);
+ EasyMock.expect(edmTyped.getType()).andStubReturn(edmType);
+ EasyMock.expect(edmTyped.getMapping()).andStubReturn(edmMapping);
+ EasyMock.replay(edmTyped);
+ EasyMock.expect(structuralType.getProperty("identifier"))
+ .andStubReturn(edmTyped);
+
+ EasyMock.expect(edmType01.getKind()).andStubReturn(
+ EdmTypeKind.SIMPLE);
+ EasyMock.expect(edmTyped01.getName()).andStubReturn("Value");
+ EasyMock.replay(edmType01);
+ EasyMock.expect(edmMapping01.getInternalName()).andStubReturn(
+ "value");
+ EasyMock.replay(edmMapping01);
+ EasyMock.expect(edmTyped01.getType()).andStubReturn(edmType01);
+ EasyMock.expect(edmTyped01.getMapping())
+ .andStubReturn(edmMapping01);
+ EasyMock.replay(edmTyped01);
+ EasyMock.expect(structuralType.getProperty("value")).andStubReturn(
+ edmTyped01);
+
+ List<String> propNames = new ArrayList<String>();
+ propNames.add("identifier");
+ propNames.add("value");
+ EasyMock.expect(structuralType.getPropertyNames()).andReturn(
+ propNames);
+ EasyMock.replay(structuralType);
+
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ try {
+ Map<String, Object> result = resultParser.parse2EdmPropertyValueMap(jpaEntity, structuralType);
+ assertEquals(2, result.size());
+ } catch (ODataJPARuntimeException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ }
+
+ @Test
+ public void testparse2EdmPropertyValueMapEdmExcep() {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ Object jpaEntity = new demoItem("abc", 10);
+ EdmStructuralType structuralType = EasyMock
+ .createMock(EdmStructuralType.class);
+ EdmProperty edmTyped = EasyMock.createMock(EdmProperty.class);
+ EdmType edmType = EasyMock.createMock(EdmType.class);
+ EdmProperty edmTyped01 = EasyMock.createMock(EdmProperty.class);
+ EdmType edmType01 = EasyMock.createMock(EdmType.class);
+ EdmMapping edmMapping = EasyMock.createMock(EdmMapping.class);
+ EdmMapping edmMapping01 = EasyMock.createMock(EdmMapping.class);
+
+ try {
+ EasyMock.expect(edmType.getKind())
+ .andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.expect(edmType.getName()).andStubThrow(
+ new EdmException(null));
+ EasyMock.replay(edmType);
+ EasyMock.expect(edmMapping.getInternalName()).andStubReturn("id");
+ EasyMock.replay(edmMapping);
+ EasyMock.expect(edmTyped.getType()).andStubThrow(
+ new EdmException(null));
+ EasyMock.expect(edmTyped.getMapping()).andStubReturn(edmMapping);
+ EasyMock.replay(edmTyped);
+ EasyMock.expect(structuralType.getProperty("identifier"))
+ .andStubReturn(edmTyped);
+
+ EasyMock.expect(edmType01.getKind()).andStubReturn(
+ EdmTypeKind.SIMPLE);
+ EasyMock.expect(edmType01.getName()).andStubReturn("value");
+ EasyMock.replay(edmType01);
+ EasyMock.expect(edmMapping01.getInternalName()).andStubReturn(
+ "value");
+ EasyMock.replay(edmMapping01);
+ EasyMock.expect(edmTyped01.getType()).andStubReturn(edmType01);
+ EasyMock.expect(edmTyped01.getMapping())
+ .andStubReturn(edmMapping01);
+ EasyMock.replay(edmTyped01);
+ EasyMock.expect(structuralType.getProperty("value")).andStubReturn(
+ edmTyped01);
+
+ List<String> propNames = new ArrayList<String>();
+ propNames.add("identifier");
+ propNames.add("value");
+ EasyMock.expect(structuralType.getPropertyNames()).andReturn(
+ propNames);
+ EasyMock.replay(structuralType);
+
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2); // assertTrue(false);
+ }
+
+ try {
+ resultParser.parse2EdmPropertyValueMap(jpaEntity, structuralType);
+ } catch (ODataJPARuntimeException e) {
+ assertTrue(true);
+ }
+
+ }
+
+ @Test
+ public void testparse2EdmPropertyListMap()
+ {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ Map<String, Object> edmEntity = new HashMap<String, Object>();
+ edmEntity.put("SoId", 1);
+ DemoRelatedEntity relatedEntity = new DemoRelatedEntity("NewOrder");
+ demoItem jpaEntity = new demoItem("laptop", 1);
+ jpaEntity.setRelatedEntity(relatedEntity);
+ List<EdmNavigationProperty> navigationPropertyList = new ArrayList<EdmNavigationProperty>();
+ // Mocking a navigation property and its mapping object
+ EdmNavigationProperty navigationProperty = EasyMock.createMock(EdmNavigationProperty.class);
+ EdmMapping edmMapping = EasyMock.createMock(EdmMapping.class);
+ try {
+ EasyMock.expect(edmMapping.getInternalName()).andStubReturn("relatedEntity");
+ EasyMock.replay(edmMapping);
+ EasyMock.expect(navigationProperty.getName()).andStubReturn("RelatedEntities");
+ EasyMock.expect(navigationProperty.getMapping()).andStubReturn(edmMapping);
+ EasyMock.replay(navigationProperty);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ navigationPropertyList.add(navigationProperty);
+ try {
+ HashMap<String, Object> result = resultParser.parse2EdmNavigationValueMap(jpaEntity, navigationPropertyList);
+ assertEquals(relatedEntity, result.get("RelatedEntities"));
+
+ } catch (ODataJPARuntimeException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ @Test
+ public void testparse2EdmPropertyValueMapFromList()
+ {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ demoItem jpaEntity = new demoItem("laptop", 1);
+ DemoRelatedEntity relatedEntity = new DemoRelatedEntity("DemoOrder");
+ jpaEntity.setRelatedEntity(relatedEntity);
+ List<EdmProperty> selectPropertyList = new ArrayList<EdmProperty>();
+ // Mocking EdmProperties
+ EdmProperty edmProperty1 = EasyMock.createMock(EdmProperty.class);
+ EdmProperty edmProperty2 = EasyMock.createMock(EdmProperty.class);
+ EdmType edmType1 = EasyMock.createMock(EdmType.class);
+ EdmType edmType2 = EasyMock.createMock(EdmType.class);
+ EdmMapping mapping1 = EasyMock.createMock(EdmMapping.class);
+ EdmMapping mapping2 = EasyMock.createMock(EdmMapping.class);
+ try {
+ EasyMock.expect(edmType1.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.replay(edmType1);
+ EasyMock.expect(mapping1.getInternalName()).andStubReturn("id");
+ EasyMock.replay(mapping1);
+ EasyMock.expect(edmProperty1.getName()).andStubReturn("Id");
+ EasyMock.expect(edmProperty1.getMapping()).andStubReturn(mapping1);
+ EasyMock.expect(edmProperty1.getType()).andStubReturn(edmType1);
+ EasyMock.replay(edmProperty1);
+ EasyMock.expect(edmType2.getKind()).andStubReturn(EdmTypeKind.COMPLEX);
+ EasyMock.replay(edmType2);
+ EasyMock.expect(mapping2.getInternalName()).andStubReturn("relatedEntity.order");
+ EasyMock.replay(mapping2);
+ EasyMock.expect(edmProperty2.getName()).andStubReturn("Order");
+ EasyMock.expect(edmProperty2.getMapping()).andStubReturn(mapping2);
+ EasyMock.expect(edmProperty2.getType()).andStubReturn(edmType2);
+ EasyMock.replay(edmProperty2);
+
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ selectPropertyList.add(edmProperty1);
+ selectPropertyList.add(edmProperty2);
+ try {
+ Map<String, Object> result = resultParser.parse2EdmPropertyValueMap(jpaEntity, selectPropertyList);
+ assertEquals("DemoOrder", result.get("Order"));
+ } catch (ODataJPARuntimeException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ }
+
+ // This unit tests when there is a complex type in the select list
+ @SuppressWarnings("unchecked")
+ @Test
+ public void testparse2EdmPropertyValueMapFromListComplex()
+ {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ demoItem jpaEntity = new demoItem("laptop", 1);
+ DemoRelatedEntity relatedEntity = new DemoRelatedEntity("DemoOrder");
+ jpaEntity.setRelatedEntity(relatedEntity);
+ List<EdmProperty> selectPropertyList = new ArrayList<EdmProperty>();
+ // Mocking EdmProperties
+ EdmProperty edmProperty1 = EasyMock.createMock(EdmProperty.class);
+ EdmProperty edmProperty2 = EasyMock.createMock(EdmProperty.class);
+ EdmProperty edmComplexProperty = EasyMock.createMock(EdmProperty.class);
+ EdmType edmType1 = EasyMock.createMock(EdmType.class);
+ EdmStructuralType edmType2 = EasyMock.createMock(EdmStructuralType.class);
+ EdmType edmComplexType = EasyMock.createMock(EdmType.class);
+ EdmMapping mapping1 = EasyMock.createMock(EdmMapping.class);
+ EdmMapping mapping2 = EasyMock.createMock(EdmMapping.class);
+ EdmMapping complexMapping = EasyMock.createMock(EdmMapping.class);
+ try {
+ EasyMock.expect(edmType1.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.replay(edmType1);
+ EasyMock.expect(mapping1.getInternalName()).andStubReturn("id");
+ EasyMock.replay(mapping1);
+ EasyMock.expect(edmProperty1.getName()).andStubReturn("Id");
+ EasyMock.expect(edmProperty1.getMapping()).andStubReturn(mapping1);
+ EasyMock.expect(edmProperty1.getType()).andStubReturn(edmType1);
+ EasyMock.replay(edmProperty1);
+ // Mocking the complex properties
+ EasyMock.expect(edmComplexType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.replay(edmComplexType);
+ EasyMock.expect(complexMapping.getInternalName()).andStubReturn("order");
+ EasyMock.replay(complexMapping);
+ EasyMock.expect(edmComplexProperty.getName()).andStubReturn("OrderName");
+ EasyMock.expect(edmComplexProperty.getMapping()).andStubReturn(complexMapping);
+ EasyMock.expect(edmComplexProperty.getType()).andStubReturn(edmComplexType);
+ EasyMock.replay(edmComplexProperty);
+ EasyMock.expect(edmType2.getKind()).andStubReturn(EdmTypeKind.COMPLEX);
+ EasyMock.expect(edmType2.getProperty("OrderName")).andStubReturn(edmComplexProperty);
+ List<String> propertyNames = new ArrayList<String>();
+ propertyNames.add("OrderName");
+ EasyMock.expect(edmType2.getPropertyNames()).andStubReturn(propertyNames);
+ EasyMock.replay(edmType2);
+ EasyMock.expect(mapping2.getInternalName()).andStubReturn("relatedEntity");
+ EasyMock.replay(mapping2);
+ EasyMock.expect(edmProperty2.getName()).andStubReturn("Order");
+ EasyMock.expect(edmProperty2.getMapping()).andStubReturn(mapping2);
+ EasyMock.expect(edmProperty2.getType()).andStubReturn(edmType2);
+ EasyMock.replay(edmProperty2);
+
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ selectPropertyList.add(edmProperty1);
+ selectPropertyList.add(edmProperty2);
+ try {
+ Map<String, Object> result = resultParser.parse2EdmPropertyValueMap(jpaEntity, selectPropertyList);
+ assertEquals(1, ((HashMap<String, Object>) result.get("Order")).size());
+ } catch (ODataJPARuntimeException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ }
+
+ /*
+ * TestCase - getGetterName is a private method in JPAResultParser. The
+ * method is uses reflection to derive the property access methods from
+ * EdmProperty
+ */
+ @Test
+ public void testGetGettersWithOutMapping() {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ try {
+
+ /*
+ * Case 1 - Property having No mapping
+ */
+ Class<?>[] pars = { String.class, EdmMapping.class, String.class };
+ Object[] params = { "Field1", null, "get" };
+ Method getGetterName = resultParser.getClass().getDeclaredMethod(
+ "getAccessModifierName", pars);
+ getGetterName.setAccessible(true);
+ String name = (String) getGetterName.invoke(resultParser,
+ params);
+
+ assertEquals("getField1", name);
+
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalArgumentException 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 (NoSuchMethodException 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 testGetGettersWithNullPropname() {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ try {
+
+ /*
+ * Case 1 - Property having No mapping and no name
+ */
+ Class<?>[] pars = { String.class, EdmMapping.class, String.class };
+ Object[] params = { null, null, null };
+ Method getGetterName = resultParser.getClass().getDeclaredMethod(
+ "getAccessModifierName", pars);
+ getGetterName.setAccessible(true);
+
+ String name = (String) getGetterName.invoke(resultParser,
+ params);
+ assertNull(name);
+
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalArgumentException 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 (NoSuchMethodException 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);
+
+ }
+ }
+
+ /*
+ * TestCase - getGetterName is a private method in JPAResultParser. The
+ * method is uses reflection to derive the property access methods from
+ * EdmProperty
+ *
+ * EdmProperty name could have been modified. Then mapping object of
+ * EdmProperty should be used for deriving the name
+ */
+ @Test
+ public void testGetGettersWithMapping() {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ EdmMapping edmMapping = EasyMock.createMock(EdmMapping.class);
+ EasyMock.expect(edmMapping.getInternalName()).andStubReturn("field1");
+ EasyMock.replay(edmMapping);
+ try {
+
+ Class<?>[] pars = { String.class, EdmMapping.class, String.class };
+ Object[] params = { "myField", edmMapping, "get" };
+ Method getGetterName = resultParser.getClass().getDeclaredMethod(
+ "getAccessModifierName", pars);
+ getGetterName.setAccessible(true);
+
+ String name = (String) getGetterName.invoke(resultParser,
+ params);
+ assertEquals("getField1", name);
+
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalArgumentException 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 (NoSuchMethodException 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 testGetGettersNoSuchMethodException() {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ try {
+
+ Method getGetterName = resultParser.getClass().getDeclaredMethod(
+ "getGetterName1", EdmProperty.class);
+ getGetterName.setAccessible(true);
+
+ } catch (NoSuchMethodException e) {
+ assertEquals(
+ "org.apache.olingo.odata2.processor.core.jpa.access.data.JPAEntityParser.getGetterName1(org.apache.olingo.odata2.api.edm.EdmProperty)",
+ e.getMessage());
+ } catch (SecurityException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+
+ }
+ }
+
+ @Test
+ public void testParse2EdmPropertyValueMap() {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ Object jpaEntity = new DemoItem2("abc");
+ try {
+ resultParser.parse2EdmPropertyValueMap(jpaEntity, getEdmPropertyList());
+ } catch (ODataJPARuntimeException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ @Test
+ public void testGetGetterEdmException() {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ Object jpaEntity = new demoItem("abc", 10);
+ EdmStructuralType structuralType = EasyMock
+ .createMock(EdmStructuralType.class);
+ try {
+ EasyMock.expect(structuralType.getPropertyNames()).andStubThrow(
+ new EdmException(null));
+ EasyMock.replay(structuralType);
+ Method getGetters = resultParser.getClass().getDeclaredMethod(
+ "getGetters", Object.class, EdmStructuralType.class);
+ getGetters.setAccessible(true);
+ try {
+ getGetters.invoke(resultParser, jpaEntity, structuralType);
+ } catch (IllegalAccessException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (IllegalArgumentException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (InvocationTargetException e) {
+ assertTrue(true);
+ }
+ } catch (NoSuchMethodException e) {
+ assertEquals(
+ "org.apache.olingo.odata2.processor.core.jpa.access.data.JPAEntityParser.getGetters(java.lang.Object, org.apache.olingo.odata2.api.edm.EdmStructuralType)",
+ e.getMessage());
+ } catch (SecurityException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ @Test
+ public void testForNullJPAEntity() {
+ JPAEntityParser resultParser = JPAEntityParser.create();
+ EdmStructuralType structuralType = EasyMock
+ .createMock(EdmStructuralType.class);
+ Object map;
+ try {
+ map = resultParser.parse2EdmPropertyValueMap(null,
+ structuralType);
+ assertNull(map);
+ } catch (ODataJPARuntimeException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ class demoItem {
+ private String id;
+ private int value;
+ private DemoRelatedEntity relatedEntity;
+
+ public String getId() {
+ return id;
+ }
+
+ public void setId(final String id) {
+ this.id = id;
+ }
+
+ public DemoRelatedEntity getRelatedEntity() {
+ return relatedEntity;
+ }
+
+ public void setRelatedEntity(final DemoRelatedEntity relatedEntity) {
+ this.relatedEntity = relatedEntity;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public void setValue(final int value) {
+ this.value = value;
+ }
+
+ demoItem(final String id, final int value) {
+ this.id = id;
+ this.value = value;
+ }
+
+ }
+
+ class DemoRelatedEntity
+ {
+ String order;
+
+ public String getOrder() {
+ return order;
+ }
+
+ public void setOrder(final String order) {
+ this.order = order;
+ }
+
+ public DemoRelatedEntity(final String order) {
+ super();
+ this.order = order;
+ }
+
+ }
+
+ private List<EdmProperty> getEdmPropertyList() {
+ List<EdmProperty> properties = new ArrayList<EdmProperty>();
+ properties.add(getEdmProperty());
+ return properties;
+ }
+
+ class DemoItem2
+ {
+ private String field1;
+
+ public String getField1() {
+ return field1;
+ }
+
+ public void setField1(final String field) {
+ field1 = field;
+ }
+
+ public DemoItem2(final String field)
+ {
+ field1 = field;
+ }
+
+ }
+
+ private EdmProperty getEdmProperty() {
+ EdmProperty edmTyped = EasyMock.createMock(EdmProperty.class);
+
+ EdmMapping edmMapping = EasyMock.createMock(EdmMapping.class);
+ EasyMock.expect(edmMapping.getInternalName()).andStubReturn(
+ "Field1");
+ EasyMock.replay(edmMapping);
+
+ EdmType edmType = EasyMock.createMock(EdmType.class);
+
+ try {
+ EasyMock.expect(edmType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.expect(edmType.getName()).andStubReturn("identifier");
+ EasyMock.expect(edmTyped.getName()).andStubReturn("SalesOrderHeader");
+ EasyMock.expect(edmTyped.getMapping())
+ .andStubReturn(edmMapping);
+
+ EasyMock.expect(edmTyped.getType()).andStubReturn(edmType);
+ EasyMock.expect(edmTyped.getMapping()).andStubReturn(edmMapping);
+
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(edmType);
+ EasyMock.replay(edmTyped);
+ return edmTyped;
+ }
+}
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/ODataJPAContextImplTest.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/ODataJPAContextImplTest.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/ODataJPAContextImplTest.java
new file mode 100644
index 0000000..5c2d59e
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/ODataJPAContextImplTest.java
@@ -0,0 +1,101 @@
+/*******************************************************************************
+ * 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;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Locale;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.olingo.odata2.api.edm.provider.EdmProvider;
+import org.apache.olingo.odata2.api.processor.ODataContext;
+import org.apache.olingo.odata2.api.processor.ODataProcessor;
+import org.apache.olingo.odata2.processor.api.jpa.ODataJPAContext;
+import org.apache.olingo.odata2.processor.core.jpa.edm.ODataJPAEdmProvider;
+import org.apache.olingo.odata2.processor.core.jpa.mock.ODataJPAContextMock;
+
+public class ODataJPAContextImplTest {
+
+ private ODataContext odataContext = null;
+ private ODataJPAContext odataJPAContext = null;
+ private EdmProvider edmProvider = null;
+ private EntityManagerFactory emf = null;
+ private EntityManager em = null;
+ private ODataProcessor processor = null;
+
+ @Before
+ public void setup() {
+
+ edmProvider = new ODataJPAEdmProvider();
+ emf = EasyMock.createMock(EntityManagerFactory.class);
+ em = EasyMock.createMock(EntityManager.class);
+ EasyMock.replay(em);
+
+ EasyMock.expect(emf.createEntityManager()).andStubReturn(em);
+ EasyMock.replay(emf);
+
+ odataContext = EasyMock.createMock(ODataContext.class);
+ List<Locale> listLocale = new ArrayList<Locale>();
+ listLocale.add(Locale.ENGLISH);
+ listLocale.add(Locale.GERMAN);
+
+ EasyMock.expect(odataContext.getAcceptableLanguages()).andStubReturn(listLocale);
+ EasyMock.replay(odataContext);
+
+ processor = EasyMock.createMock(ODataProcessor.class);
+ EasyMock.replay(processor);
+
+ odataJPAContext = new ODataJPAContextImpl();
+ odataJPAContext.setEdmProvider(edmProvider);
+ odataJPAContext.setEntityManagerFactory(emf);
+ odataJPAContext.setODataContext(odataContext);
+ odataJPAContext.setODataProcessor(processor);
+ odataJPAContext
+ .setPersistenceUnitName(ODataJPAContextMock.PERSISTENCE_UNIT_NAME);
+ odataJPAContext.setJPAEdmMappingModel(ODataJPAContextMock.MAPPING_MODEL);
+ }
+
+ @Test
+ public void testgetMethodsOfODataJPAContext() {
+
+ assertEquals(odataJPAContext.getEdmProvider().hashCode(), edmProvider.hashCode());
+ assertEquals(odataJPAContext.getEntityManagerFactory().hashCode(), emf.hashCode());
+ assertEquals(odataJPAContext.getODataContext().hashCode(), odataContext.hashCode());
+ assertEquals(odataJPAContext.getODataProcessor().hashCode(), processor.hashCode());
+ assertEquals(odataJPAContext.getPersistenceUnitName(),
+ ODataJPAContextMock.PERSISTENCE_UNIT_NAME);
+ assertEquals(odataJPAContext.getJPAEdmMappingModel(), ODataJPAContextMock.MAPPING_MODEL);
+
+ EntityManager em1 = odataJPAContext.getEntityManager();
+ EntityManager em2 = odataJPAContext.getEntityManager();
+ if (em1 != null && em2 != null) {
+ assertEquals(em1.hashCode(), em2.hashCode());
+ }
+
+ }
+
+}
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/ODataJPAProcessorDefaultTest.java
----------------------------------------------------------------------
diff --git a/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/ODataJPAProcessorDefaultTest.java b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/ODataJPAProcessorDefaultTest.java
new file mode 100644
index 0000000..d9fa5d7
--- /dev/null
+++ b/jpa-core/src/test/java/org/apache/olingo/odata2/processor/core/jpa/ODataJPAProcessorDefaultTest.java
@@ -0,0 +1,569 @@
+/*******************************************************************************
+ * 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;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Query;
+import javax.persistence.metamodel.EntityType;
+import javax.persistence.metamodel.Metamodel;
+
+import org.easymock.EasyMock;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import org.apache.olingo.odata2.api.commons.HttpContentType;
+import org.apache.olingo.odata2.api.commons.InlineCount;
+import org.apache.olingo.odata2.api.edm.EdmConcurrencyMode;
+import org.apache.olingo.odata2.api.edm.EdmEntityContainer;
+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.EdmFacets;
+import org.apache.olingo.odata2.api.edm.EdmMapping;
+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.exception.ODataException;
+import org.apache.olingo.odata2.api.processor.ODataContext;
+import org.apache.olingo.odata2.api.uri.KeyPredicate;
+import org.apache.olingo.odata2.api.uri.NavigationSegment;
+import org.apache.olingo.odata2.api.uri.PathInfo;
+import org.apache.olingo.odata2.api.uri.UriInfo;
+import org.apache.olingo.odata2.api.uri.expression.FilterExpression;
+import org.apache.olingo.odata2.api.uri.expression.OrderByExpression;
+import org.apache.olingo.odata2.api.uri.info.DeleteUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntityCountUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntitySetCountUriInfo;
+import org.apache.olingo.odata2.api.uri.info.GetEntityUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PostUriInfo;
+import org.apache.olingo.odata2.api.uri.info.PutMergePatchUriInfo;
+import org.apache.olingo.odata2.processor.api.jpa.ODataJPAContext;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPAModelException;
+import org.apache.olingo.odata2.processor.api.jpa.exception.ODataJPARuntimeException;
+import org.apache.olingo.odata2.processor.core.jpa.common.ODataJPATestConstants;
+import org.apache.olingo.odata2.processor.core.jpa.model.JPAEdmTestModelView;
+
+public class ODataJPAProcessorDefaultTest extends JPAEdmTestModelView {
+
+ ODataJPAProcessorDefault objODataJPAProcessorDefault;
+ ODataJPAProcessorDefaultTest objODataJPAProcessorDefaultTest;
+
+ private static final String STR_LOCAL_URI = "http://localhost:8080/org.apache.olingo.odata2.processor.ref.web/";
+ private static final String SALESORDERPROCESSING_CONTAINER = "salesorderprocessingContainer";
+ private static final String SO_ID = "SoId";
+ private static final String SALES_ORDER = "SalesOrder";
+ private static final String SALES_ORDER_HEADERS = "SalesOrderHeaders";
+ private static final String TEXT_PLAIN_CHARSET_UTF_8 = "text/plain;charset=utf-8";
+ private static final String STR_CONTENT_TYPE = "Content-Type";
+
+ @Before
+ public void setUp() {
+ objODataJPAProcessorDefaultTest = new ODataJPAProcessorDefaultTest();
+ objODataJPAProcessorDefault = new ODataJPAProcessorDefault(getLocalmockODataJPAContext());
+ }
+
+ @Test
+ public void testReadEntitySetGetEntitySetUriInfoString() {
+ try {
+ GetEntityUriInfo getEntityView = getEntityUriInfo();
+ Assert.assertNotNull(objODataJPAProcessorDefault.readEntity(getEntityView, HttpContentType.APPLICATION_XML));
+ } catch (ODataJPAModelException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (ODataJPARuntimeException e1) {//Expected
+ assertTrue(true);
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ }
+
+ @Test
+ public void testcountEntitySet() {
+ try {
+ Assert.assertNotNull(objODataJPAProcessorDefault.countEntitySet(getEntitySetCountUriInfo(), HttpContentType.APPLICATION_XML));
+ Assert.assertEquals(TEXT_PLAIN_CHARSET_UTF_8,
+ objODataJPAProcessorDefault.countEntitySet(getEntitySetCountUriInfo(), HttpContentType.APPLICATION_XML).getHeader(STR_CONTENT_TYPE));
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (Exception e)
+ {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testExistsEntity() {
+ try {
+ Assert.assertNotNull(objODataJPAProcessorDefault.existsEntity(getEntityCountCountUriInfo(), HttpContentType.APPLICATION_XML));
+ Assert.assertEquals(TEXT_PLAIN_CHARSET_UTF_8,
+ objODataJPAProcessorDefault.existsEntity(getEntityCountCountUriInfo(), HttpContentType.APPLICATION_XML).getHeader(STR_CONTENT_TYPE));
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ } catch (Exception e)
+ {
+ assertTrue(true);
+ }
+ }
+
+ @Test
+ public void testDeleteEntity() {
+ try {
+ Assert.assertNotNull(objODataJPAProcessorDefault.deleteEntity(getDeletetUriInfo(), HttpContentType.APPLICATION_XML));
+ Assert.assertEquals(TEXT_PLAIN_CHARSET_UTF_8,
+ objODataJPAProcessorDefault.countEntitySet(getEntitySetCountUriInfo(), HttpContentType.APPLICATION_XML).getHeader(STR_CONTENT_TYPE));
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ }
+
+ @Test
+ public void testCreateEntity() {
+ try {
+ Assert.assertNotNull(objODataJPAProcessorDefault.createEntity(getPostUriInfo(), getMockedInputStreamContent(), HttpContentType.APPLICATION_XML, HttpContentType.APPLICATION_XML));
+ } catch (ODataException e) {
+ Assert.assertTrue(true); //Expected TODO - need to revisit
+ }
+ }
+
+ @Test
+ public void testUpdateEntity() {
+ try {
+ Assert.assertNotNull(objODataJPAProcessorDefault.updateEntity(getPutUriInfo(), getMockedInputStreamContent(), HttpContentType.APPLICATION_XML, false, HttpContentType.APPLICATION_XML));
+ } catch (ODataException e) {
+ Assert.assertTrue(true); //Expected TODO - need to revisit
+ }
+ }
+
+ private PutMergePatchUriInfo getPutUriInfo() {
+ return (PutMergePatchUriInfo) getDeletetUriInfo();
+ }
+
+ private PostUriInfo getPostUriInfo() {
+ return (PostUriInfo) getDeletetUriInfo();
+ }
+
+ private InputStream getMockedInputStreamContent() {
+ return new ByteArrayInputStream(getEntityBody().getBytes());
+ }
+
+ private String getEntityBody() {
+ return
+ "<entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:m=\"http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\" xmlns:d=\"http://schemas.microsoft.com/ado/2007/08/dataservices\" xml:base=\"http://localhost:8080/org.apache.olingo.odata2.processor.ref.web/SalesOrderProcessing.svc/\">"
+ + "<content type=\"application/xml\">"
+ + "<m:properties>"
+ + "<d:ID>2</d:ID>"
+ + "<d:CreationDate>2013-01-02T00:00:00</d:CreationDate>"
+ + "<d:CurrencyCode>Code_555</d:CurrencyCode>"
+ + "<d:BuyerAddressInfo m:type=\"SalesOrderProcessing.AddressInfo\">"
+ + "<d:Street>Test_Street_Name_055</d:Street>"
+ + "<d:Number>2</d:Number>"
+ + "<d:Country>Test_Country_2</d:Country>"
+ + "<d:City>Test_City_2</d:City>"
+ + "</d:BuyerAddressInfo>"
+ + "<d:GrossAmount>0.0</d:GrossAmount>"
+ + "<d:BuyerId>2</d:BuyerId>"
+ + "<d:DeliveryStatus>true</d:DeliveryStatus>"
+ + "<d:BuyerName>buyerName_2</d:BuyerName>"
+ + "<d:NetAmount>0.0</d:NetAmount>"
+ + "</m:properties>"
+ + "</content>"
+ + "</entry>";
+ }
+
+ private GetEntitySetCountUriInfo getEntitySetCountUriInfo() {
+ return getLocalUriInfo();
+ }
+
+ private GetEntityCountUriInfo getEntityCountCountUriInfo() {
+ return getLocalUriInfo();
+ }
+
+ private DeleteUriInfo getDeletetUriInfo() {
+ UriInfo objUriInfo = EasyMock.createMock(UriInfo.class);
+ EasyMock.expect(objUriInfo.getStartEntitySet()).andStubReturn(getLocalEdmEntitySet());
+ EasyMock.expect(objUriInfo.getTargetEntitySet()).andStubReturn(getLocalEdmEntitySet());
+ EasyMock.expect(objUriInfo.getSelect()).andStubReturn(null);
+ EasyMock.expect(objUriInfo.getOrderBy()).andStubReturn(getOrderByExpression());
+ EasyMock.expect(objUriInfo.getTop()).andStubReturn(getTop());
+ EasyMock.expect(objUriInfo.getSkip()).andStubReturn(getSkip());
+ EasyMock.expect(objUriInfo.getInlineCount()).andStubReturn(getInlineCount());
+ EasyMock.expect(objUriInfo.getFilter()).andStubReturn(getFilter());
+ EasyMock.expect(objUriInfo.getKeyPredicates()).andStubReturn(getKeyPredicates());
+ EasyMock.replay(objUriInfo);
+ return objUriInfo;
+ }
+
+ private List<KeyPredicate> getKeyPredicates() {
+ List<KeyPredicate> keyPredicates = new ArrayList<KeyPredicate>();
+ return keyPredicates;
+ }
+
+ /**
+ * @return
+ */
+ private UriInfo getLocalUriInfo() {
+ UriInfo objUriInfo = EasyMock.createMock(UriInfo.class);
+ EasyMock.expect(objUriInfo.getStartEntitySet()).andStubReturn(getLocalEdmEntitySet());
+ EasyMock.expect(objUriInfo.getTargetEntitySet()).andStubReturn(getLocalEdmEntitySet());
+ EasyMock.expect(objUriInfo.getSelect()).andStubReturn(null);
+ EasyMock.expect(objUriInfo.getOrderBy()).andStubReturn(getOrderByExpression());
+ EasyMock.expect(objUriInfo.getTop()).andStubReturn(getTop());
+ EasyMock.expect(objUriInfo.getSkip()).andStubReturn(getSkip());
+ EasyMock.expect(objUriInfo.getInlineCount()).andStubReturn(getInlineCount());
+ EasyMock.expect(objUriInfo.getFilter()).andStubReturn(getFilter());
+ EasyMock.expect(objUriInfo.getFunctionImport()).andStubReturn(null);
+ EasyMock.replay(objUriInfo);
+ return objUriInfo;
+ }
+
+ /**
+ * @return
+ * @throws EdmException
+ */
+ private EdmEntitySet getLocalEdmEntitySet() {
+ EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
+ try {
+ EasyMock.expect(edmEntitySet.getName()).andStubReturn(SALES_ORDER_HEADERS);
+ EasyMock.expect(edmEntitySet.getEntityContainer()).andStubReturn(getLocalEdmEntityContainer());
+ EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(getLocalEdmEntityType());
+ EasyMock.replay(edmEntitySet);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ return edmEntitySet;
+ }
+
+ /**
+ * @return
+ * @throws EdmException
+ */
+ private EdmEntityType getLocalEdmEntityType() {
+ EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
+ try {
+ EasyMock.expect(edmEntityType.getKeyProperties()).andStubReturn(new ArrayList<EdmProperty>());
+ EasyMock.expect(edmEntityType.getPropertyNames()).andStubReturn(getLocalPropertyNames());
+ EasyMock.expect(edmEntityType.getProperty(SO_ID)).andStubReturn(getEdmTypedMockedObj(SALES_ORDER));
+ EasyMock.expect(edmEntityType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.expect(edmEntityType.getNamespace()).andStubReturn(SALES_ORDER_HEADERS);
+ EasyMock.expect(edmEntityType.getName()).andStubReturn(SALES_ORDER_HEADERS);
+ EasyMock.expect(edmEntityType.hasStream()).andStubReturn(false);
+ EasyMock.expect(edmEntityType.getNavigationPropertyNames()).andStubReturn(new ArrayList<String>());
+ EasyMock.expect(edmEntityType.getKeyPropertyNames()).andStubReturn(new ArrayList<String>());
+ EasyMock.expect(edmEntityType.getMapping()).andStubReturn(getEdmMappingMockedObj(SALES_ORDER));// ID vs Salesorder ID
+ EasyMock.replay(edmEntityType);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ return edmEntityType;
+ }
+
+ private InlineCount getInlineCount() {
+ return InlineCount.NONE;
+ }
+
+ private FilterExpression getFilter() {
+ return null;
+ }
+
+ private Integer getSkip() {
+ return null;
+ }
+
+ private Integer getTop() {
+ return null;
+ }
+
+ private OrderByExpression getOrderByExpression() {
+ return null;
+ }
+
+ private ODataJPAContext getLocalmockODataJPAContext() {
+ ODataJPAContext odataJPAContext = EasyMock.createMock(ODataJPAContext.class);
+ EasyMock.expect(odataJPAContext.getPersistenceUnitName()).andStubReturn("salesorderprocessing");
+ EasyMock.expect(odataJPAContext.getEntityManagerFactory()).andStubReturn(mockEntityManagerFactory());
+ EasyMock.expect(odataJPAContext.getODataContext()).andStubReturn(getLocalODataContext());
+ EasyMock.expect(odataJPAContext.getEntityManager()).andStubReturn(getLocalEntityManager());
+ EasyMock.replay(odataJPAContext);
+ return odataJPAContext;
+ }
+
+ private EntityManagerFactory mockEntityManagerFactory() {
+ EntityManagerFactory emf = EasyMock.createMock(EntityManagerFactory.class);
+ EasyMock.expect(emf.getMetamodel()).andStubReturn(mockMetaModel());
+ EasyMock.expect(emf.createEntityManager()).andStubReturn(getLocalEntityManager());
+ EasyMock.replay(emf);
+ return emf;
+ }
+
+ private EntityManagerFactory mockEntityManagerFactory2() {// For create, to avoid stackoverflow
+ EntityManagerFactory emf = EasyMock.createMock(EntityManagerFactory.class);
+ EasyMock.expect(emf.getMetamodel()).andStubReturn(mockMetaModel());
+ EasyMock.replay(emf);
+ return emf;
+ }
+
+ private EntityManager getLocalEntityManager() {
+ EntityManager em = EasyMock.createMock(EntityManager.class);
+ EasyMock.expect(em.createQuery("SELECT E1 FROM SalesOrderHeaders E1")).andStubReturn(getQuery());
+ EasyMock.expect(em.createQuery("SELECT COUNT ( E1 ) FROM SalesOrderHeaders E1")).andStubReturn(getQueryForSelectCount());
+ EasyMock.expect(em.getEntityManagerFactory()).andStubReturn(mockEntityManagerFactory2());// For create
+ EasyMock.expect(em.getTransaction()).andStubReturn(getLocalTransaction()); //For Delete
+ Address obj = new Address();
+ em.remove(obj);// testing void method
+ em.flush();
+ EasyMock.replay(em);
+ return em;
+ }
+
+ private EntityTransaction getLocalTransaction() {
+ EntityTransaction entityTransaction = EasyMock.createMock(EntityTransaction.class);
+ entityTransaction.begin(); // testing void method
+ entityTransaction.commit();// testing void method
+ entityTransaction.rollback();// testing void method
+ EasyMock.replay(entityTransaction);
+ return entityTransaction;
+ }
+
+ private Query getQuery() {
+ Query query = EasyMock.createMock(Query.class);
+ EasyMock.expect(query.getResultList()).andStubReturn(getResultList());
+ EasyMock.replay(query);
+ return query;
+ }
+
+ private Query getQueryForSelectCount() {
+ Query query = EasyMock.createMock(Query.class);
+ EasyMock.expect(query.getResultList()).andStubReturn(getResultListForSelectCount());
+ EasyMock.replay(query);
+ return query;
+ }
+
+ private List<?> getResultList() {
+ List<Object> list = new ArrayList<Object>();
+ list.add(new Address());
+ return list;
+ }
+
+ private List<?> getResultListForSelectCount() {
+ List<Object> list = new ArrayList<Object>();
+ list.add(new Long(11));
+ return list;
+ }
+
+ class Address {
+ private String soId = "12";
+
+ public String getSoId() {
+ return soId;
+ }
+
+ @Override
+ public boolean equals(final Object obj) {
+ boolean isEqual = false;
+ if (obj instanceof Address)
+ {
+ isEqual = getSoId().equalsIgnoreCase(((Address) obj).getSoId());//
+ }
+ return isEqual;
+ }
+ }
+
+ private Metamodel mockMetaModel() {
+ Metamodel metaModel = EasyMock.createMock(Metamodel.class);
+ EasyMock.expect(metaModel.getEntities()).andStubReturn(getLocalEntities());
+ EasyMock.replay(metaModel);
+ return metaModel;
+ }
+
+ private Set<EntityType<?>> getLocalEntities() {
+ Set<EntityType<?>> entityTypeSet = new HashSet<EntityType<?>>();
+ entityTypeSet.add(getLocalJPAEntityType());
+ return entityTypeSet;
+ }
+
+ @SuppressWarnings("rawtypes")
+ private EntityType<EntityType> getLocalJPAEntityType() {
+ @SuppressWarnings("unchecked")
+ EntityType<EntityType> entityType = EasyMock.createMock(EntityType.class);
+ EasyMock.expect(entityType.getJavaType()).andStubReturn(EntityType.class);
+ EasyMock.replay(entityType);
+ return entityType;
+ }
+
+ private GetEntityUriInfo getEntityUriInfo() {
+ GetEntityUriInfo getEntityView = EasyMock
+ .createMock(GetEntityUriInfo.class);
+ EdmEntitySet edmEntitySet = EasyMock.createMock(EdmEntitySet.class);
+ EdmEntityType edmEntityType = EasyMock.createMock(EdmEntityType.class);
+ try {
+ EasyMock.expect(getEntityView.getExpand()).andStubReturn(null);
+ EasyMock.expect(edmEntityType.getKeyProperties()).andStubReturn(
+ new ArrayList<EdmProperty>());
+ EasyMock.expect(edmEntitySet.getEntityType()).andStubReturn(
+ edmEntityType);
+ EasyMock.expect(edmEntitySet.getName()).andStubReturn(SALES_ORDER_HEADERS);
+
+ EasyMock.expect(getEntityView.getSelect()).andStubReturn(null);
+ EasyMock.expect(getEntityView.getTargetEntitySet()).andStubReturn(
+ edmEntitySet);
+ EasyMock.expect(edmEntityType.getPropertyNames()).andStubReturn(getLocalPropertyNames());
+ EasyMock.expect(edmEntityType.getProperty(SO_ID)).andStubReturn(getEdmTypedMockedObj(SO_ID));
+
+ EasyMock.expect(edmEntityType.getMapping()).andStubReturn(getEdmMappingMockedObj(SALES_ORDER));
+
+ EasyMock.expect(edmEntityType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.expect(edmEntityType.getNamespace()).andStubReturn(SALES_ORDER_HEADERS);
+ EasyMock.expect(edmEntityType.getName()).andStubReturn(SALES_ORDER_HEADERS);
+ EasyMock.expect(edmEntityType.hasStream()).andStubReturn(false);
+ EasyMock.expect(edmEntityType.getNavigationPropertyNames()).andStubReturn(new ArrayList<String>());
+ EasyMock.expect(edmEntityType.getKeyPropertyNames()).andStubReturn(new ArrayList<String>());
+
+ EasyMock.expect(edmEntitySet.getEntityContainer()).andStubReturn(getLocalEdmEntityContainer());
+
+ EasyMock.replay(edmEntityType, edmEntitySet);
+ EasyMock.expect(getEntityView.getKeyPredicates()).andStubReturn(
+ new ArrayList<KeyPredicate>());
+ List<NavigationSegment> navigationSegments = new ArrayList<NavigationSegment>();
+ EasyMock.expect(getEntityView.getNavigationSegments()).andReturn(
+ navigationSegments);
+ EasyMock.expect(getEntityView.getStartEntitySet()).andReturn(edmEntitySet);
+
+ EasyMock.replay(getEntityView);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ return getEntityView;
+ }
+
+ private EdmEntityContainer getLocalEdmEntityContainer() {
+ EdmEntityContainer edmEntityContainer = EasyMock.createMock(EdmEntityContainer.class);
+ EasyMock.expect(edmEntityContainer.isDefaultEntityContainer()).andStubReturn(true);
+ try {
+ EasyMock.expect(edmEntityContainer.getName()).andStubReturn(SALESORDERPROCESSING_CONTAINER);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+
+ EasyMock.replay(edmEntityContainer);
+ return edmEntityContainer;
+ }
+
+ private EdmTyped getEdmTypedMockedObj(final String propertyName) {
+ EdmProperty mockedEdmProperty = EasyMock.createMock(EdmProperty.class);
+ try {
+ EasyMock.expect(mockedEdmProperty.getMapping())
+ .andStubReturn(getEdmMappingMockedObj(propertyName));
+ EdmType edmType = EasyMock.createMock(EdmType.class);
+ EasyMock.expect(edmType.getKind()).andStubReturn(EdmTypeKind.SIMPLE);
+ EasyMock.replay(edmType);
+ EasyMock.expect(mockedEdmProperty.getName()).andStubReturn("identifier");
+ EasyMock.expect(mockedEdmProperty.getType()).andStubReturn(edmType);
+ EasyMock.expect(mockedEdmProperty.getFacets()).andStubReturn(getEdmFacetsMockedObj());
+
+ EasyMock.replay(mockedEdmProperty);
+ } catch (EdmException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ return mockedEdmProperty;
+ }
+
+ private EdmFacets getEdmFacetsMockedObj() {
+ EdmFacets facets = EasyMock.createMock(EdmFacets.class);
+ EasyMock.expect(facets.getConcurrencyMode()).andStubReturn(EdmConcurrencyMode.Fixed);
+
+ EasyMock.replay(facets);
+ return facets;
+ }
+
+ private EdmMapping getEdmMappingMockedObj(final String propertyName) {
+ EdmMapping mockedEdmMapping = EasyMock.createMock(EdmMapping.class);
+ if (propertyName.equalsIgnoreCase(SALES_ORDER)) {
+ EasyMock.expect(mockedEdmMapping.getInternalName())
+ .andStubReturn(SALES_ORDER_HEADERS);
+ } else {
+ EasyMock.expect(mockedEdmMapping.getInternalName())
+ .andStubReturn(propertyName);
+ }
+ EasyMock.replay(mockedEdmMapping);
+ return mockedEdmMapping;
+ }
+
+ private List<String> getLocalPropertyNames() {
+ List<String> list = new ArrayList<String>();
+ list.add(SO_ID);
+ return list;
+ }
+
+ private ODataContext getLocalODataContext() {
+ ODataContext objODataContext = EasyMock.createMock(ODataContext.class);
+ try {
+ EasyMock.expect(objODataContext.getPathInfo()).andStubReturn(getLocalPathInfo());
+ } catch (ODataException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ EasyMock.replay(objODataContext);
+ return objODataContext;
+ }
+
+ private PathInfo getLocalPathInfo() {
+ PathInfo pathInfo = EasyMock.createMock(PathInfo.class);
+ EasyMock.expect(pathInfo.getServiceRoot()).andStubReturn(getLocalURI());
+ EasyMock.replay(pathInfo);
+ return pathInfo;
+ }
+
+ private URI getLocalURI() {
+ URI uri = null;
+ try {
+ uri = new URI(STR_LOCAL_URI);
+ } catch (URISyntaxException e) {
+ fail(ODataJPATestConstants.EXCEPTION_MSG_PART_1 + e.getMessage()
+ + ODataJPATestConstants.EXCEPTION_MSG_PART_2);
+ }
+ return uri;
+ }
+
+}