You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2016/05/16 03:53:56 UTC

[21/42] metamodel git commit: METAMODEL-235: Fixed

METAMODEL-235: Fixed

Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/6a669d7f
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/6a669d7f
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/6a669d7f

Branch: refs/heads/5.x
Commit: 6a669d7fb2190c6349f386f38c2bb515b7b80bfe
Parents: 324ea22
Author: kaspersorensen <i....@gmail.com>
Authored: Thu Apr 21 09:44:37 2016 -0700
Committer: kaspersorensen <i....@gmail.com>
Committed: Thu Apr 21 09:44:37 2016 -0700

----------------------------------------------------------------------
 .gitignore                                      |  4 +-
 .../rest/JestElasticSearchUtils.java            |  5 +-
 .../rest/JestElasticSearchUtilsTest.java        | 64 +++++++++++++++++---
 3 files changed, 63 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/metamodel/blob/6a669d7f/.gitignore
----------------------------------------------------------------------
diff --git a/.gitignore b/.gitignore
index e1af7b0..960f35f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,6 @@
 .project
-.settings/
-.classpath/
+.settings
+.classpath
 .metadata/
 target/
 /.idea/

http://git-wip-us.apache.org/repos/asf/metamodel/blob/6a669d7f/elasticsearch/rest/src/main/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtils.java
----------------------------------------------------------------------
diff --git a/elasticsearch/rest/src/main/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtils.java b/elasticsearch/rest/src/main/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtils.java
index c37ff80..7448aa6 100644
--- a/elasticsearch/rest/src/main/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtils.java
+++ b/elasticsearch/rest/src/main/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtils.java
@@ -55,12 +55,15 @@ final class JestElasticSearchUtils {
     }
 
     private static Object getDataFromColumnType(JsonElement field, ColumnType type) {
+        if (field == null || field.isJsonNull()) {
+            return null;
+        }
         if (type.isNumber()) {
             // Pretty terrible workaround to avoid LazilyParsedNumber
             // (which is happily output, but not recognized by Jest/GSON).
             return NumberComparator.toNumber(field.getAsString());
         } else if (type.isTimeBased()) {
-            Date valueToDate = ElasticSearchDateConverter.tryToConvert(field.getAsString());
+            final Date valueToDate = ElasticSearchDateConverter.tryToConvert(field.getAsString());
             if (valueToDate == null) {
                 return field.getAsString();
             } else {

http://git-wip-us.apache.org/repos/asf/metamodel/blob/6a669d7f/elasticsearch/rest/src/test/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtilsTest.java
----------------------------------------------------------------------
diff --git a/elasticsearch/rest/src/test/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtilsTest.java b/elasticsearch/rest/src/test/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtilsTest.java
index 0d78d8e..9e2b42f 100644
--- a/elasticsearch/rest/src/test/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtilsTest.java
+++ b/elasticsearch/rest/src/test/java/org/apache/metamodel/elasticsearch/rest/JestElasticSearchUtilsTest.java
@@ -18,22 +18,28 @@
  */
 package org.apache.metamodel.elasticsearch.rest;
 
-import com.google.gson.JsonObject;
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.Date;
+import java.util.List;
+
 import org.apache.metamodel.data.DataSetHeader;
 import org.apache.metamodel.data.Row;
 import org.apache.metamodel.data.SimpleDataSetHeader;
 import org.apache.metamodel.query.SelectItem;
+import org.apache.metamodel.schema.Column;
 import org.apache.metamodel.schema.ColumnType;
 import org.apache.metamodel.schema.MutableColumn;
+import org.junit.Test;
 
-import java.util.Arrays;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
+import com.google.gson.JsonObject;
 
-public class JestElasticSearchUtilsTest extends TestCase {
+public class JestElasticSearchUtilsTest {
 
+    @Test
     public void testAssignDocumentIdForPrimaryKeys() throws Exception {
         MutableColumn primaryKeyColumn = new MutableColumn("value1", ColumnType.STRING).setPrimaryKey(true);
         SelectItem primaryKeyItem = new SelectItem(primaryKeyColumn);
@@ -49,6 +55,50 @@ public class JestElasticSearchUtilsTest extends TestCase {
         assertEquals(primaryKeyValue, documentId);
     }
 
+    @Test
+    public void testCreateRowWithNullValues() throws Exception {
+        final Column col1 = new MutableColumn("col1", ColumnType.STRING);
+        final Column col2 = new MutableColumn("col2", ColumnType.STRING);
+        final DataSetHeader header = new SimpleDataSetHeader(new Column[] { col1, col2 });
+        final JsonObject source = new JsonObject();
+        source.addProperty("col1", "foo");
+        source.addProperty("col2", (String) null);
+        final String documentId = "row1";
+
+        final Row row = JestElasticSearchUtils.createRow(source, documentId, header);
+        assertEquals("Row[values=[foo, null]]", row.toString());
+    }
+
+    @Test
+    public void testCreateRowWithNumberValueAndStringType() throws Exception {
+        final Column col1 = new MutableColumn("col1", ColumnType.STRING);
+        final DataSetHeader header = new SimpleDataSetHeader(new Column[] { col1 });
+        final JsonObject source = new JsonObject();
+        source.addProperty("col1", 42);
+        final String documentId = "row1";
+
+        final Row row = JestElasticSearchUtils.createRow(source, documentId, header);
+        assertEquals("Row[values=[42]]", row.toString());
+    }
+
+    @Test
+    public void testCreateRowWithStringValueAndNumberType() throws Exception {
+        final Column col1 = new MutableColumn("col1", ColumnType.NUMBER);
+        final DataSetHeader header = new SimpleDataSetHeader(new Column[] { col1 });
+        final JsonObject source = new JsonObject();
+        source.addProperty("col1", "hello world");
+        final String documentId = "row1";
+
+        final Row row = JestElasticSearchUtils.createRow(source, documentId, header);
+
+        // whether or not 'null' should be returned (bad value, but preserves
+        // type) or 'hello world' should be returned (correct value, breaks
+        // type) can be debated. For now it is added here as an assertion to
+        // keep track of any regressions.
+        assertEquals("Row[values=[null]]", row.toString());
+    }
+
+    @Test
     public void testCreateRowWithParseableDates() throws Exception {
         SelectItem item1 = new SelectItem(new MutableColumn("value1", ColumnType.STRING));
         SelectItem item2 = new SelectItem(new MutableColumn("value2", ColumnType.DATE));