Refactoring -- chain or responsibility pattern was replaced by if-then block. JUnit for new functionality.
Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/e0a821c3 Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/e0a821c3 Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/e0a821c3 Branch: refs/heads/master Commit: e0a821c3c55c61245097aa22e4fdb6cdc8d1e7b0 Parents: 59ec9cc Author: jakub <[email protected]> Authored: Wed May 23 16:28:43 2018 +0200 Committer: jakub <[email protected]> Committed: Wed May 23 16:28:43 2018 +0200 ---------------------------------------------------------------------- .../metamodel/neo4j/Neo4jDataContext.java | 77 +++++++++--------- .../neo4j/utils/AbstractColumnTypeHandler.java | 33 -------- .../neo4j/utils/ArrayColumnTypeHandler.java | 39 ---------- .../neo4j/utils/BooleanColumnTypeHandler.java | 39 ---------- .../neo4j/utils/ColumnTypeHandler.java | 38 --------- .../neo4j/utils/ColumnTypeResolver.java | 43 ++++++---- .../neo4j/utils/DoubleColumnTypeHandler.java | 39 ---------- .../neo4j/utils/IntegerColumnTypeHandler.java | 39 ---------- .../neo4j/utils/LongColumnTypeHandler.java | 39 ---------- .../neo4j/utils/MapColumnTypeHandler.java | 39 ---------- .../neo4j/utils/StringColumnTypeHandler.java | 39 ---------- .../neo4j/utils/ColumnTypeResolverTest.java | 82 ++++++++++++++++++++ 12 files changed, 150 insertions(+), 396 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java index b23f1c9..524d29c 100644 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java +++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/Neo4jDataContext.java @@ -167,55 +167,59 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat labelsJsonArray = new JSONArray(labelsJsonString); for (int i = 0; i < labelsJsonArray.length(); i++) { - fillTableDefsFromLabel(labelsJsonArray.getString(i), tableDefs); + final SimpleTableDef tableDefFromLabel = createTableDefFromLabel(labelsJsonArray.getString(i)); + + if (tableDefFromLabel != null) { + tableDefs.add(tableDefFromLabel); + } } return tableDefs.toArray(new SimpleTableDef[tableDefs.size()]); } catch (final JSONException e) { - logger.error("Error occured in parsing JSON while detecting the schema: ", e); + logger.error("Error occurred in parsing JSON while detecting the schema: ", e); throw new IllegalStateException(e); } } - private void fillTableDefsFromLabel(final String label, final List<SimpleTableDef> tableDefs) throws JSONException { + private SimpleTableDef createTableDefFromLabel(final String label) throws JSONException { final List<JSONObject> nodesPerLabel = getAllNodesPerLabel(label); final List<String> propertiesPerLabel = getPropertiesFromLabelNodes(nodesPerLabel); final Set<String> relationshipPropertiesPerLabel = new LinkedHashSet<>(); for (final JSONObject node : nodesPerLabel) { final Integer nodeId = (Integer) node.getJSONObject("metadata").get("id"); - fillRelationshipPropertiesPerLabel(nodeId, relationshipPropertiesPerLabel); + final Set<String> relationshipPropertiesForNode = createRelationshipPropertiesForNode(nodeId); + relationshipPropertiesPerLabel.addAll(relationshipPropertiesForNode); } propertiesPerLabel.addAll(relationshipPropertiesPerLabel); - // Do not add a table if label has no nodes (empty tables are considered non-existent) - if (!nodesPerLabel.isEmpty()) { + if (nodesPerLabel.isEmpty()) { + return null; // Do not add a table if label has no nodes (empty tables are considered non-existent) + } else { final String[] columnNames = propertiesPerLabel.toArray(new String[propertiesPerLabel.size()]); final ColumnTypeResolver columnTypeResolver = new ColumnTypeResolver(); - final SimpleTableDef tableDef = new SimpleTableDef(label, columnNames, + return new SimpleTableDef(label, columnNames, columnTypeResolver.getColumnTypes(nodesPerLabel.get(0), columnNames)); - tableDefs.add(tableDef); } } - private void fillRelationshipPropertiesPerLabel(final Integer nodeId, final Set<String> relationshipPropertiesPerLabel) - throws JSONException { + private Set<String> createRelationshipPropertiesForNode(final Integer nodeId) throws JSONException { final List<JSONObject> relationshipsPerNode = getOutgoingRelationshipsPerNode(nodeId); + final Set<String> relationshipProperties = new LinkedHashSet<>(); for (final JSONObject relationship : relationshipsPerNode) { // Add the relationship as a column in the table final String relationshipName = relationship.getString("type"); final String relationshipNameProperty = RELATIONSHIP_PREFIX + relationshipName; + relationshipProperties.add(relationshipNameProperty); - if (!relationshipPropertiesPerLabel.contains(relationshipNameProperty)) { - relationshipPropertiesPerLabel.add(relationshipNameProperty); - } - // Add all the relationship properties as table columns final List<String> propertiesPerRelationship = getAllPropertiesPerRelationship(relationship); - relationshipPropertiesPerLabel.addAll(propertiesPerRelationship); + relationshipProperties.addAll(propertiesPerRelationship); } + + return relationshipProperties; } private List<String> getPropertiesFromLabelNodes(final List<JSONObject> nodesPerLabel) { @@ -235,14 +239,17 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat } private List<String> getAllPropertiesPerRelationship(JSONObject relationship) { - List<String> propertyNames = new ArrayList<String>(); + final List<String> propertyNames = new ArrayList<>(); try { - String relationshipName = RELATIONSHIP_PREFIX + relationship.getJSONObject("metadata").getString("type"); - JSONObject relationshipPropertiesJSONObject = relationship.getJSONObject("data"); + final String relationshipName = + RELATIONSHIP_PREFIX + relationship.getJSONObject("metadata").getString("type"); + final JSONObject relationshipPropertiesJSONObject = relationship.getJSONObject("data"); + if (relationshipPropertiesJSONObject.length() > 0) { - JSONArray relationshipPropertiesNamesJSONArray = relationshipPropertiesJSONObject.names(); + final JSONArray relationshipPropertiesNamesJSONArray = relationshipPropertiesJSONObject.names(); + for (int i = 0; i < relationshipPropertiesNamesJSONArray.length(); i++) { - String propertyName = relationshipName + RELATIONSHIP_COLUMN_SEPARATOR + final String propertyName = relationshipName + RELATIONSHIP_COLUMN_SEPARATOR + relationshipPropertiesNamesJSONArray.getString(i); if (!propertyNames.contains(propertyName)) { propertyNames.add(propertyName); @@ -250,30 +257,30 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat } } return propertyNames; - } catch (JSONException e) { - logger.error("Error occured in parsing JSON while getting relationship properties: ", e); + } catch (final JSONException e) { + logger.error("Error occurred in parsing JSON while getting relationship properties: ", e); throw new IllegalStateException(e); } } private List<JSONObject> getOutgoingRelationshipsPerNode(final Integer nodeId) { - List<JSONObject> outgoingRelationshipsPerNode = new ArrayList<>(); - - String outgoingRelationshipsPerNodeJsonString = _requestWrapper.executeRestRequest(new HttpGet(_serviceRoot - + "/node/" + nodeId + "/relationships/out")); - - JSONArray outgoingRelationshipsPerNodeJsonArray; + final List<JSONObject> outgoingRelationshipsPerNode = new ArrayList<>(); + final String outgoingRelationshipsPerNodeJsonString = _requestWrapper.executeRestRequest( + new HttpGet(_serviceRoot + "/node/" + nodeId + "/relationships/out")); + final JSONArray outgoingRelationshipsPerNodeJsonArray; + try { outgoingRelationshipsPerNodeJsonArray = new JSONArray(outgoingRelationshipsPerNodeJsonString); for (int i = 0; i < outgoingRelationshipsPerNodeJsonArray.length(); i++) { - JSONObject relationship = outgoingRelationshipsPerNodeJsonArray.getJSONObject(i); + final JSONObject relationship = outgoingRelationshipsPerNodeJsonArray.getJSONObject(i); if (!outgoingRelationshipsPerNode.contains(relationship)) { outgoingRelationshipsPerNode.add(relationship); } } return outgoingRelationshipsPerNode; - } catch (JSONException e) { - logger.error("Error occured in parsing JSON while detecting outgoing relationships for node: " + nodeId, e); + } catch (final JSONException e) { + logger.error("Error occurred in parsing JSON while detecting outgoing relationships for node: " + nodeId, + e); throw new IllegalStateException(e); } } @@ -293,7 +300,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat } return allNodesPerLabel; } catch (JSONException e) { - logger.error("Error occured in parsing JSON while detecting the nodes for a label: " + label, e); + logger.error("Error occurred in parsing JSON while detecting the nodes for a label: " + label, e); throw new IllegalStateException(e); } } @@ -318,7 +325,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat } return properties; } catch (JSONException e) { - logger.error("Error occured in parsing JSON while detecting the properties of a node: " + node, e); + logger.error("Error occurred in parsing JSON while detecting the properties of a node: " + node, e); throw new IllegalStateException(e); } } @@ -334,7 +341,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat final List<SelectItem> selectItems = columns.stream().map(SelectItem::new).collect(Collectors.toList()); dataSet = new Neo4jDataSet(selectItems, resultJSONObject); } catch (JSONException e) { - logger.error("Error occured in parsing JSON while materializing the schema: ", e); + logger.error("Error occurred in parsing JSON while materializing the schema: ", e); throw new IllegalStateException(e); } @@ -366,7 +373,7 @@ public class Neo4jDataContext extends QueryPostprocessDataContext implements Dat Number value = (Number) valueJSONArray.get(0); return value; } catch (JSONException e) { - logger.error("Error occured in parsing JSON response: ", e); + logger.error("Error occurred in parsing JSON response: ", e); // Do not throw an exception here. Returning null here will make // MetaModel attempt to count records manually and therefore recover // from the error. http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java deleted file mode 100644 index 1198fe0..0000000 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/AbstractColumnTypeHandler.java +++ /dev/null @@ -1,33 +0,0 @@ -/** - * 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.metamodel.neo4j.utils; - -import org.apache.metamodel.schema.ColumnType; -import org.json.JSONObject; - -public abstract class AbstractColumnTypeHandler implements ColumnTypeHandler { - protected ColumnTypeHandler _successor; - - @Override - public void setSuccessor(final ColumnTypeHandler successor) { - _successor = successor; - } - - public abstract ColumnType getTypeFromValue(final JSONObject data, final String key); -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java deleted file mode 100644 index 3d491f4..0000000 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ArrayColumnTypeHandler.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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.metamodel.neo4j.utils; - -import org.apache.metamodel.schema.ColumnType; -import org.json.JSONException; -import org.json.JSONObject; - -public class ArrayColumnTypeHandler extends AbstractColumnTypeHandler { - @Override - public ColumnType getTypeFromValue(final JSONObject data, final String key) { - try { - data.getJSONArray(key); - return ColumnType.ARRAY; - } catch (final JSONException e) { - if (_successor != null) { - return _successor.getTypeFromValue(data, key); - } else { - return ColumnType.STRING; - } - } - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java deleted file mode 100644 index 5aa4194..0000000 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/BooleanColumnTypeHandler.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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.metamodel.neo4j.utils; - -import org.apache.metamodel.schema.ColumnType; -import org.json.JSONException; -import org.json.JSONObject; - -public class BooleanColumnTypeHandler extends AbstractColumnTypeHandler { - @Override - public ColumnType getTypeFromValue(final JSONObject data, final String key) { - try { - data.getBoolean(key); - return ColumnType.BOOLEAN; - } catch (final JSONException e) { - if (_successor != null) { - return _successor.getTypeFromValue(data, key); - } else { - return ColumnType.STRING; - } - } - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java deleted file mode 100644 index f673f4d..0000000 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeHandler.java +++ /dev/null @@ -1,38 +0,0 @@ -/** - * 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.metamodel.neo4j.utils; - -import org.apache.metamodel.schema.ColumnType; -import org.json.JSONObject; - -public interface ColumnTypeHandler { - /** - * Sets the following node in the chain. - * @param successor - */ - void setSuccessor(final ColumnTypeHandler successor); - - /** - * Returns a column type based on the given value. - * @param data - * @param key - * @return ColumnType - */ - ColumnType getTypeFromValue(final JSONObject data, final String key); -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java index 75677a2..f5a817b 100644 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java +++ b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolver.java @@ -24,10 +24,15 @@ import java.util.Iterator; import java.util.List; import org.apache.metamodel.schema.ColumnType; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ColumnTypeResolver { + private static final Logger logger = LoggerFactory.getLogger(ColumnTypeResolver.class); + public ColumnType[] getColumnTypes(final JSONObject jsonObject, final String[] columnNamesArray) { final List<String> columnNames = new ArrayList<>(Arrays.asList(columnNamesArray)); final List<ColumnType> columnTypes = new ArrayList<>(); @@ -49,7 +54,7 @@ public class ColumnTypeResolver { if (jsonObject.has(dataKey)) { final JSONObject data = jsonObject.getJSONObject(dataKey); - final Iterator keysIterator = data.keys(); + final Iterator<?> keysIterator = data.keys(); while (keysIterator.hasNext()) { final String key = (String) keysIterator.next(); @@ -96,22 +101,26 @@ public class ColumnTypeResolver { } private ColumnType getTypeFromValue(final JSONObject data, final String key) { - final BooleanColumnTypeHandler booleanHandler = new BooleanColumnTypeHandler(); - final IntegerColumnTypeHandler integerHandler = new IntegerColumnTypeHandler(); - final LongColumnTypeHandler longHandler = new LongColumnTypeHandler(); - final DoubleColumnTypeHandler doubleHandler = new DoubleColumnTypeHandler(); - final ArrayColumnTypeHandler arrayHandler = new ArrayColumnTypeHandler(); - final MapColumnTypeHandler mapHandler = new MapColumnTypeHandler(); - final StringColumnTypeHandler stringHandler = new StringColumnTypeHandler(); - - // chain of responsibility - booleanHandler.setSuccessor(integerHandler); - integerHandler.setSuccessor(longHandler); - longHandler.setSuccessor(doubleHandler); - doubleHandler.setSuccessor(arrayHandler); - arrayHandler.setSuccessor(mapHandler); - mapHandler.setSuccessor(stringHandler); + try { + final Class<? extends Object> keyClass = data.get(key).getClass(); + + if (keyClass.equals(Boolean.class)) { + return ColumnType.BOOLEAN; + } else if (keyClass.equals(Integer.class)) { + return ColumnType.INTEGER; + } else if (keyClass.equals(Long.class)) { + return ColumnType.BIGINT; + } else if (keyClass.equals(Double.class)) { + return ColumnType.DOUBLE; + } else if (keyClass.equals(JSONArray.class)) { + return ColumnType.ARRAY; + } else if (keyClass.equals(JSONObject.class)) { + return ColumnType.MAP; + } + } catch (final JSONException e) { + logger.error("JSON object does not contain required key '{}'. {}", key, e.getMessage()); + } - return booleanHandler.getTypeFromValue(data, key); + return ColumnType.STRING; } } http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java deleted file mode 100644 index 35e9ef8..0000000 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/DoubleColumnTypeHandler.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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.metamodel.neo4j.utils; - -import org.apache.metamodel.schema.ColumnType; -import org.json.JSONException; -import org.json.JSONObject; - -public class DoubleColumnTypeHandler extends AbstractColumnTypeHandler { - @Override - public ColumnType getTypeFromValue(final JSONObject data, final String key) { - try { - data.getDouble(key); - return ColumnType.DOUBLE; - } catch (final JSONException e) { - if (_successor != null) { - return _successor.getTypeFromValue(data, key); - } else { - return ColumnType.STRING; - } - } - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java deleted file mode 100644 index 9a48742..0000000 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/IntegerColumnTypeHandler.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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.metamodel.neo4j.utils; - -import org.apache.metamodel.schema.ColumnType; -import org.json.JSONException; -import org.json.JSONObject; - -public class IntegerColumnTypeHandler extends AbstractColumnTypeHandler { - @Override - public ColumnType getTypeFromValue(final JSONObject data, final String key) { - try { - data.getInt(key); - return ColumnType.INTEGER; - } catch (final JSONException e) { - if (_successor != null) { - return _successor.getTypeFromValue(data, key); - } else { - return ColumnType.STRING; - } - } - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java deleted file mode 100644 index 939e355..0000000 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/LongColumnTypeHandler.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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.metamodel.neo4j.utils; - -import org.apache.metamodel.schema.ColumnType; -import org.json.JSONException; -import org.json.JSONObject; - -public class LongColumnTypeHandler extends AbstractColumnTypeHandler { - @Override - public ColumnType getTypeFromValue(final JSONObject data, final String key) { - try { - data.getLong(key); - return ColumnType.BIGINT; - } catch (final JSONException e) { - if (_successor != null) { - return _successor.getTypeFromValue(data, key); - } else { - return ColumnType.STRING; - } - } - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java deleted file mode 100644 index 8151490..0000000 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/MapColumnTypeHandler.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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.metamodel.neo4j.utils; - -import org.apache.metamodel.schema.ColumnType; -import org.json.JSONException; -import org.json.JSONObject; - -public class MapColumnTypeHandler extends AbstractColumnTypeHandler { - @Override - public ColumnType getTypeFromValue(final JSONObject data, final String key) { - try { - data.getJSONObject(key); - return ColumnType.MAP; - } catch (final JSONException e) { - if (_successor != null) { - return _successor.getTypeFromValue(data, key); - } else { - return ColumnType.STRING; - } - } - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java ---------------------------------------------------------------------- diff --git a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java b/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java deleted file mode 100644 index 06afe36..0000000 --- a/neo4j/src/main/java/org/apache/metamodel/neo4j/utils/StringColumnTypeHandler.java +++ /dev/null @@ -1,39 +0,0 @@ -/** - * 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.metamodel.neo4j.utils; - -import org.apache.metamodel.schema.ColumnType; -import org.json.JSONException; -import org.json.JSONObject; - -public class StringColumnTypeHandler extends AbstractColumnTypeHandler { - @Override - public ColumnType getTypeFromValue(final JSONObject data, final String key) { - try { - data.getString(key); - return ColumnType.STRING; - } catch (final JSONException e) { - if (_successor != null) { - return _successor.getTypeFromValue(data, key); - } else { - return ColumnType.STRING; - } - } - } -} http://git-wip-us.apache.org/repos/asf/metamodel/blob/e0a821c3/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java ---------------------------------------------------------------------- diff --git a/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java b/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java new file mode 100644 index 0000000..8e56b66 --- /dev/null +++ b/neo4j/src/test/java/org/apache/metamodel/neo4j/utils/ColumnTypeResolverTest.java @@ -0,0 +1,82 @@ +/** + * 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.metamodel.neo4j.utils; + +import static junit.framework.TestCase.assertEquals; + +import org.apache.metamodel.schema.ColumnType; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; +import org.junit.Test; + +public class ColumnTypeResolverTest { + private static final String COLUMN_ID = "id"; + private static final String COLUMN_ID_NEO4J = "_id"; + private static final String COLUMN_METADATA = "metadata"; + private static final String COLUMN_DATA = "data"; + private static final String COLUMN_BOOLEAN = "boolean"; + private static final String COLUMN_INTEGER = "integer"; + private static final String COLUMN_LONG = "long"; + private static final String COLUMN_DOUBLE = "double"; + private static final String COLUMN_ARRAY = "array"; + private static final String COLUMN_MAP = "map"; + private static final String COLUMN_STRING = "string"; + + @Test + public void testGetColumnTypes() throws Exception { + final ColumnTypeResolver resolver = new ColumnTypeResolver(); + final JSONObject jsonObject = createJSONObject(); + final String[] columnNames = + new String[] { COLUMN_ID_NEO4J, COLUMN_BOOLEAN, COLUMN_INTEGER, COLUMN_LONG, COLUMN_DOUBLE, + COLUMN_ARRAY, COLUMN_MAP, COLUMN_STRING }; + final ColumnType[] columnTypes = resolver.getColumnTypes(jsonObject, columnNames); + assertEquals(columnTypes.length, columnNames.length); + assertEquals(columnTypes[0], ColumnType.BIGINT); // ID + assertEquals(columnTypes[1], ColumnType.BOOLEAN); + assertEquals(columnTypes[2], ColumnType.STRING); + assertEquals(columnTypes[3], ColumnType.ARRAY); + assertEquals(columnTypes[4], ColumnType.DOUBLE); + assertEquals(columnTypes[5], ColumnType.INTEGER); + assertEquals(columnTypes[6], ColumnType.MAP); + assertEquals(columnTypes[7], ColumnType.BIGINT); + } + + private JSONObject createJSONObject() throws JSONException { + final JSONObject json = new JSONObject(); + final JSONObject metadata = new JSONObject(); + metadata.put(COLUMN_ID, 42L); + json.put(COLUMN_METADATA, metadata); + final JSONObject data = new JSONObject(); + data.put(COLUMN_BOOLEAN, true); + data.put(COLUMN_STRING, "forty-two"); + final JSONArray array = new JSONArray(); + array.put(1).put(2).put(3); + data.put(COLUMN_ARRAY, array); + data.put(COLUMN_DOUBLE, 3.141592); + data.put(COLUMN_INTEGER, 42); + final JSONObject map = new JSONObject(); + map.put("1", "one").put("2", "two").put("3", "three"); + data.put(COLUMN_MAP, map); + data.put(COLUMN_LONG, 12345678910L); + json.put(COLUMN_DATA, data); + + return json; + } +}
