olingo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject [2/2] git commit: [OLINGO-86] Created all positive tests for DS
Date Mon, 23 Dec 2013 12:31:03 GMT
[OLINGO-86] Created all positive tests for DS


Project: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/commit/1ce2c877
Tree: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/tree/1ce2c877
Diff: http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/diff/1ce2c877

Branch: refs/heads/master
Commit: 1ce2c8776a688d056fc35f3096f777041aaa6aa9
Parents: 78da059
Author: Michael Bolz <mibo@apache.org>
Authored: Mon Dec 23 13:29:08 2013 +0100
Committer: Michael Bolz <mibo@apache.org>
Committed: Mon Dec 23 13:29:08 2013 +0100

----------------------------------------------------------------------
 .../processor/core/util/AnnotationHelper.java   |  26 +-
 .../datasource/AnnotationsInMemoryDsTest.java   |  60 ++++-
 .../core/util/AnnotationHelperTest.java         | 270 +++++++++++++++++++
 3 files changed, 348 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1ce2c877/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java
b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java
index 807b6fe..54ffb49 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/main/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelper.java
@@ -63,6 +63,11 @@ public class AnnotationHelper {
 
     Map<String, Object> firstKeyFields = getValueForAnnotatedFields(firstInstance,
EdmKey.class);
     Map<String, Object> secondKeyFields = getValueForAnnotatedFields(secondInstance,
EdmKey.class);
+    if(firstKeyFields.isEmpty() && secondKeyFields.isEmpty()) {
+      throw new ODataRuntimeException("Both object instances does not have EdmKey fields
defined ["
+          + "firstClass=" + firstInstance.getClass().getName()
+          + " secondClass=" + secondInstance.getClass().getName() + "].");
+    }
 
     return keyValuesMatch(firstKeyFields, secondKeyFields);
   }
@@ -82,6 +87,8 @@ public class AnnotationHelper {
   private boolean keyValuesMatch(final Map<String, Object> firstKeyValues, final Map<String,
Object> secondKeyValues) {
     if (firstKeyValues.size() != secondKeyValues.size()) {
       return false;
+    } else if(firstKeyValues.isEmpty()) {
+      throw new ODataRuntimeException("No keys given for key value matching.");
     } else {
       Set<Map.Entry<String, Object>> entries = firstKeyValues.entrySet();
       for (Map.Entry<String, Object> entry : entries) {
@@ -498,8 +505,7 @@ public class AnnotationHelper {
     for (Field field : fields) {
       if (field.getAnnotation(annotation) != null) {
         Object value = getFieldValue(instance, field);
-        final EdmProperty property = field.getAnnotation(EdmProperty.class);
-        final String name = property == null || property.name().isEmpty() ? field.getName()
: property.name();
+        final String name = extractPropertyName(field);
         fieldName2Value.put(name, value);
       }
     }
@@ -513,6 +519,15 @@ public class AnnotationHelper {
     return fieldName2Value;
   }
 
+  private String extractPropertyName(Field field) {
+    final EdmProperty property = field.getAnnotation(EdmProperty.class);
+    if(property == null || property.name().isEmpty()) {
+      return getCanonicalName(field);
+    } else {
+      return property.name();
+    }
+  }
+
   public void setValueForAnnotatedField(final Object instance, final Class<? extends Annotation>
annotation,
       final Object value)
       throws ODataAnnotationException {
@@ -641,7 +656,7 @@ public class AnnotationHelper {
     }
   }
 
-  public Object convert(final Field field, final String propertyValue) {
+  private Object convert(final Field field, final String propertyValue) {
     Class<?> fieldClass = field.getType();
     try {
       EdmProperty property = field.getAnnotation(EdmProperty.class);
@@ -649,8 +664,9 @@ public class AnnotationHelper {
       return type.getEdmSimpleTypeInstance().valueOfString(propertyValue,
           EdmLiteralKind.DEFAULT, null, fieldClass);
     } catch (EdmSimpleTypeException ex) {
-      throw new ODataRuntimeException("Conversion failed for string property with error:
"
-          + ex.getMessage(), ex);
+      throw new ODataRuntimeException("Conversion failed for string property [" 
+          + propertyValue + "] on field ["
+          + field + "] with error: " + ex.getMessage(), ex);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1ce2c877/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java
b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java
index b9ebcb3..cff39b9 100644
--- a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java
+++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/datasource/AnnotationsInMemoryDsTest.java
@@ -25,9 +25,6 @@ import java.util.Map;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
-import org.apache.olingo.odata2.annotation.processor.core.datasource.AnnotationInMemoryDs;
-import org.apache.olingo.odata2.annotation.processor.core.datasource.DataSource;
-import org.apache.olingo.odata2.annotation.processor.core.datasource.DataStore;
 import org.apache.olingo.odata2.annotation.processor.core.datasource.DataSource.BinaryData;
 import org.apache.olingo.odata2.annotation.processor.core.edm.AnnotationEdmProvider;
 import org.apache.olingo.odata2.annotation.processor.core.model.Building;
@@ -307,6 +304,26 @@ public class AnnotationsInMemoryDsTest {
     }
   }
 
+  @Test(expected=ODataRuntimeException.class)
+  public void readUnknownEntity() throws Exception {
+    EdmEntitySet unknownEntitySet = Mockito.mock(EdmEntitySet.class);
+    Mockito.when(unknownEntitySet.getName()).thenReturn("UnknownEntity");
+    Map<String, Object> keys = new HashMap<String, Object>();
+    keys.put("Id", "1");
+
+    // execute
+    datasource.readData(unknownEntitySet, keys);
+  }
+
+  @Test(expected=ODataRuntimeException.class)
+  public void readUnknownEntities() throws Exception {
+    EdmEntitySet unknownEntitySet = Mockito.mock(EdmEntitySet.class);
+    Mockito.when(unknownEntitySet.getName()).thenReturn("UnknownEntity");
+
+    // execute
+    datasource.readData(unknownEntitySet);
+  }
+
   @Test
   public void readEntities() throws Exception {
     EdmEntitySet roomsEntitySet = createMockedEdmEntitySet("Rooms");
@@ -608,6 +625,43 @@ public class AnnotationsInMemoryDsTest {
   }
 
 
+  @Test
+  public void writeRelations() throws Exception {
+    DataStore<Building> buildingStore = DataStore.createInMemory(Building.class, true);
+    DataStore<Room> roomStore = DataStore.createInMemory(Room.class, true);
+
+    EdmEntitySet buildingsEntitySet = createMockedEdmEntitySet("Buildings");
+    EdmEntitySet roomsEntitySet = createMockedEdmEntitySet("Rooms");
+
+    Building building = new Building();
+    building.setName("Common Building");
+    Building created = buildingStore.create(building);
+
+    Room room = new Room(42, "Room with Number");
+    room.setSeats(123);;
+    room.setVersion(4711);
+    roomStore.create(room);
+    
+    Map<String, Object> targetEntityKeyValues = new HashMap<String, Object>();
+    targetEntityKeyValues.put("Id", 42);
+    
+    // execute
+    datasource.writeRelation(buildingsEntitySet, building, roomsEntitySet, targetEntityKeyValues);
+    
+    // validate
+    Building readBuilding = buildingStore.read(created);
+    Room readRoom = roomStore.read(new Room(42, ""));
+    
+    List<Room> readRooms = readBuilding.getRooms();
+    Assert.assertEquals(1, readRooms.size());
+    Assert.assertEquals(readRoom, readRooms.get(0));
+    
+    Assert.assertEquals("42", readRoom.getId());
+    Assert.assertEquals(123, readRoom.getSeats());
+    Assert.assertEquals(4711, readRoom.getVersion());
+    Assert.assertEquals(readBuilding, readRoom.getBuilding());
+  }
+  
   private EdmEntitySet createMockedEdmEntitySet(final String entitySetName) throws ODataException
{
     return createMockedEdmEntitySet(edmProvider, entitySetName);
   }

http://git-wip-us.apache.org/repos/asf/incubator-olingo-odata2/blob/1ce2c877/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelperTest.java
----------------------------------------------------------------------
diff --git a/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelperTest.java
b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelperTest.java
new file mode 100644
index 0000000..2e95f82
--- /dev/null
+++ b/odata2-annotation-processor/annotation-processor-core/src/test/java/org/apache/olingo/odata2/annotation/processor/core/util/AnnotationHelperTest.java
@@ -0,0 +1,270 @@
+/*
+ * Copyright 2013 The Apache Software Foundation.
+ * 
+ * Licensed 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.annotation.processor.core.util;
+
+import java.lang.reflect.Field;
+import java.util.HashMap;
+import java.util.Map;
+
+import junit.framework.Assert;
+
+import org.apache.olingo.odata2.annotation.processor.core.model.Location;
+import org.apache.olingo.odata2.api.annotation.edm.EdmEntityType;
+import org.apache.olingo.odata2.api.annotation.edm.EdmKey;
+import org.apache.olingo.odata2.api.annotation.edm.EdmNavigationProperty;
+import org.apache.olingo.odata2.api.annotation.edm.EdmProperty;
+import org.apache.olingo.odata2.api.annotation.edm.EdmType;
+import org.apache.olingo.odata2.api.edm.FullQualifiedName;
+import org.apache.olingo.odata2.api.exception.ODataException;
+import org.apache.olingo.odata2.core.exception.ODataRuntimeException;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class AnnotationHelperTest {
+
+  private final AnnotationHelper annotationHelper;
+
+  public AnnotationHelperTest() {
+    annotationHelper = new AnnotationHelper();
+  }
+
+  @Test
+  public void keyMatchMapPositive() throws ODataException {
+    SimpleEntity firstInstance = new SimpleEntity(42l, "Another Name");
+    Map<String, Object> keyName2Value = new HashMap<String, Object>();
+    keyName2Value.put("Id", Long.valueOf(42));
+
+    boolean result = annotationHelper.keyMatch(firstInstance, keyName2Value);
+
+    Assert.assertTrue(result);
+  }
+
+  @Test
+  public void keyMatchMapNegativeWrongClass() throws ODataException {
+    SimpleEntity firstInstance = new SimpleEntity(42l, "Another Name");
+    Map<String, Object> keyName2Value = new HashMap<String, Object>();
+    keyName2Value.put("Id", 42);
+
+    boolean result = annotationHelper.keyMatch(firstInstance, keyName2Value);
+
+    Assert.assertFalse(result);
+  }
+
+  @Test
+  public void keyMatchMapNegativeDifferentValues() throws ODataException {
+    SimpleEntity firstInstance = new SimpleEntity(99l, "Another Name");
+    Map<String, Object> keyName2Value = new HashMap<String, Object>();
+    keyName2Value.put("Id", 42);
+
+    boolean result = annotationHelper.keyMatch(firstInstance, keyName2Value);
+
+    Assert.assertFalse(result);
+  }
+
+  @Test
+  public void keyMatchMapNegativeDifferentValueCount() throws ODataException {
+    SimpleEntity firstInstance = new SimpleEntity(99l, "Another Name");
+    Map<String, Object> keyName2Value = new HashMap<String, Object>();
+
+    boolean result = annotationHelper.keyMatch(firstInstance, keyName2Value);
+
+    Assert.assertFalse(result);
+  }
+
+  @Test
+  public void keyMatchPositive() throws ODataException {
+    SimpleEntity firstInstance = new SimpleEntity(42l, "A Name");
+    SimpleEntity secondInstance = new SimpleEntity(42l, "Another Name");
+
+    boolean result = annotationHelper.keyMatch(firstInstance, secondInstance);
+
+    Assert.assertTrue(result);
+  }
+
+  @Test
+  public void keyMatchPositiveWithNull() throws ODataException {
+    SimpleEntity firstInstance = new SimpleEntity();
+    SimpleEntity secondInstance = new SimpleEntity();
+
+    boolean result = annotationHelper.keyMatch(firstInstance, secondInstance);
+
+    Assert.assertTrue(result);
+  }
+
+  @Test
+  public void keyMatchNegative() throws ODataException {
+    SimpleEntity firstInstance = new SimpleEntity(99l, "A Name");
+    SimpleEntity secondInstance = new SimpleEntity(42l, "A Name");
+
+    boolean result = annotationHelper.keyMatch(firstInstance, secondInstance);
+
+    Assert.assertFalse(result);
+  }
+
+  @Test
+  public void keyMatchNegativeWithNull() throws ODataException {
+    SimpleEntity firstInstance = new SimpleEntity();
+    SimpleEntity secondInstance = new SimpleEntity(42l, "A Name");
+
+    boolean result = annotationHelper.keyMatch(firstInstance, secondInstance);
+
+    Assert.assertFalse(result);
+  }
+
+  @Test
+  public void keyMatchNegativeWithNullInstance() throws ODataException {
+    SimpleEntity firstInstance = null;
+    SimpleEntity secondInstance = new SimpleEntity(42l, "A Name");
+
+    boolean result = annotationHelper.keyMatch(firstInstance, secondInstance);
+    Assert.assertFalse(result);
+
+    result = annotationHelper.keyMatch(secondInstance, firstInstance);
+    Assert.assertFalse(result);
+  }
+
+  @Test
+  public void keyMatchNegativeOneNotAnnotated() throws ODataException {
+    NotAnnotatedBean firstInstance = new NotAnnotatedBean();
+    SimpleEntity secondInstance = new SimpleEntity(42l, "A Name");
+
+    boolean result = annotationHelper.keyMatch(firstInstance, secondInstance);
+    Assert.assertFalse(result);
+
+    boolean result2 = annotationHelper.keyMatch(secondInstance, firstInstance);
+    Assert.assertFalse(result2);
+  }
+
+  @Test(expected = ODataRuntimeException.class)
+  public void keyMatchNegativeNotAnnotated() throws ODataException {
+    NotAnnotatedBean firstInstance = new NotAnnotatedBean();
+    NotAnnotatedBean secondInstance = new NotAnnotatedBean();
+
+    boolean result = annotationHelper.keyMatch(firstInstance, secondInstance);
+
+    Assert.assertFalse(result);
+  }
+
+  @Test
+  public void extractEntitTypeNameViaNavigation() throws Exception {
+    Field field = NavigationAnnotated.class.getDeclaredField("navigationPropertySimpleEntity");
+    EdmNavigationProperty enp = field.getAnnotation(EdmNavigationProperty.class);
+
+    String name = annotationHelper.extractEntitTypeName(enp, SimpleEntity.class);
+
+    Assert.assertEquals("SimpleEntity", name);
+  }
+
+  @Test
+  public void extractEntitTypeNameViaNavigationField() throws Exception {
+    Field field = NavigationAnnotated.class.getDeclaredField("navigationPropertyDefault");
+    EdmNavigationProperty enp = field.getAnnotation(EdmNavigationProperty.class);
+
+    String name = annotationHelper.extractEntitTypeName(enp, field);
+
+    Assert.assertEquals("SimpleEntity", name);
+  }
+
+  @Test
+  public void getFieldTypeForPropertyNullInstance() throws Exception {
+    Object result = annotationHelper.getFieldTypeForProperty(null, "");
+    Assert.assertNull(result);
+  }
+
+  @Test
+  public void getValueForPropertyNullInstance() throws Exception {
+    Object result = annotationHelper.getValueForProperty(null, "");
+    Assert.assertNull(result);
+  }
+
+  @Test
+  public void setValueForPropertyNullInstance() throws Exception {
+    annotationHelper.setValueForProperty(null, "", null);
+  }
+  
+  @Test
+  public void extractEntitySetNameObject() {
+    Assert.assertNull(annotationHelper.extractEntitySetName(Object.class));
+  }
+
+  @Test
+  public void extractComplexTypeFqnObject() {
+    Assert.assertNull(annotationHelper.extractComplexTypeFqn(Object.class));
+  }
+
+  @Test
+  public void extractComplexTypeFqn() {
+    FullQualifiedName fqn = annotationHelper.extractComplexTypeFqn(Location.class);
+    Assert.assertEquals("RefScenario", fqn.getNamespace());
+    Assert.assertEquals("c_Location", fqn.getName());
+  }
+  
+  @Test
+  public void convert() throws Exception {
+    ConversionProperty cp = new ConversionProperty();
+    annotationHelper.setValueForProperty(cp, "StringProp", "42");
+    annotationHelper.setValueForProperty(cp, "IntegerProp", "420");
+    annotationHelper.setValueForProperty(cp, "LongProp", "4200");
+    annotationHelper.setValueForProperty(cp, "FloatProp", "43");
+    annotationHelper.setValueForProperty(cp, "DoubleProp", "42.00");
+    annotationHelper.setValueForProperty(cp, "ByteProp", "1");
+    
+    Assert.assertEquals("42", cp.stringProp);
+    Assert.assertEquals(Integer.valueOf(420), cp.integerProp);
+    Assert.assertEquals(Long.valueOf("4200"), cp.longProp);
+    Assert.assertEquals(new Float(43), cp.floatProp);
+    Assert.assertEquals(new Double(42.00), cp.doubleProp);
+    Assert.assertEquals(Byte.valueOf("1"), cp.byteProp);
+  }
+
+  @EdmEntityType
+  private class SimpleEntity {
+    @EdmKey
+    @EdmProperty
+    Long id;
+    @EdmProperty
+    String name;
+
+    public SimpleEntity() {}
+
+    public SimpleEntity(Long id, String name) {
+      this.id = id;
+      this.name = name;
+    }
+  }
+
+  private class NavigationAnnotated {
+    @EdmNavigationProperty(toType = SimpleEntity.class)
+    SimpleEntity navigationPropertySimpleEntity;
+    @EdmNavigationProperty
+    SimpleEntity navigationPropertyDefault;
+  }
+
+  private class ConversionProperty {
+    @EdmProperty(type=EdmType.STRING) String stringProp;
+    @EdmProperty(type=EdmType.INT32) Integer integerProp;
+    @EdmProperty(type=EdmType.INT64) Long longProp;
+    @EdmProperty(type=EdmType.DECIMAL) Float floatProp;
+    @EdmProperty(type=EdmType.DOUBLE) Double doubleProp;
+    @EdmProperty(type=EdmType.BYTE) Byte byteProp;
+  }
+  
+  private class NotAnnotatedBean {
+    private String name;
+  }
+}


Mime
View raw message