use of com.buschmais.xo.api.CompositeObject in project jqa-core-framework by buschmais.
the class ReportHelperTest method label.
@Test
void label() {
TestDescriptorWithLanguageElement descriptorWithLabel = mock(TestDescriptorWithLanguageElement.class);
when(descriptorWithLabel.getValue()).thenReturn("value");
assertThat(ReportHelper.getLabel(descriptorWithLabel), equalTo("value"));
assertThat(ReportHelper.getLabel(singletonList(descriptorWithLabel)), equalTo("value"));
assertThat(ReportHelper.getLabel(new String[] { "value1", "value2" }), equalTo("value1, value2"));
Map<String, Object> singleProperty = new HashMap<>();
singleProperty.put("key", "Value");
assertThat(ReportHelper.getLabel(singleProperty), equalTo("Value"));
Map<String, Object> multipleProperties = new HashMap<>();
multipleProperties.put("key1", descriptorWithLabel);
multipleProperties.put("key2", "simpleValue");
assertThat(ReportHelper.getLabel(multipleProperties), equalTo("key1:value, key2:simpleValue"));
TestDescriptorWithLanguageElement descriptorWithEmptyLanguageLabel = mock(TestDescriptorWithLanguageElement.class);
assertThat(ReportHelper.getLabel(descriptorWithEmptyLanguageLabel), notNullValue());
// Composite object without supported label
Neo4jNode neo4jNode = mock(Neo4jNode.class);
doReturn(multipleProperties).when(neo4jNode).getProperties();
CompositeObject compositeObject = mock(CompositeObject.class);
doReturn(neo4jNode).when(compositeObject).getDelegate();
assertThat(ReportHelper.getLabel(compositeObject), equalTo("key1:value, key2:simpleValue"));
}
use of com.buschmais.xo.api.CompositeObject in project jqa-core-framework by buschmais.
the class SubGraphFactory method toIdentifiable.
/**
* Convert a given value to an {@link Identifiable} element of a
* {@link SubGraph}.
*
* @param value
* The value.
* @return The {@link Identifiable}.
* @throws ReportException
* If value that cannot be converted to an {@link Identifiable}.
*/
public <I extends Identifiable> I toIdentifiable(Object value) throws ReportException {
if (value == null) {
return null;
} else if (value instanceof Map) {
Map<String, Object> virtualObject = (Map) value;
Object role = virtualObject.get(ROLE);
if (role != null) {
Map<String, Object> properties = (Map<String, Object>) virtualObject.get(PROPERTIES);
switch(role.toString().toLowerCase()) {
case NODE:
Node node = new Node();
node.setId(nodeId--);
Collection<String> labels = (Collection<String>) virtualObject.get(LABELS);
node.getLabels().addAll(labels);
node.getProperties().putAll(properties);
node.setLabel((String) virtualObject.get(LABEL));
return (I) node;
case RELATIONSHIP:
Relationship relationship = new Relationship();
relationship.setId(relationshipId--);
Node startNode = toIdentifiable(virtualObject.get(START_NODE));
Node endNode = toIdentifiable(virtualObject.get(END_NODE));
String type = (String) virtualObject.get(TYPE);
relationship.setType(type);
relationship.setStartNode(startNode);
relationship.setEndNode(endNode);
relationship.getProperties().putAll(properties);
relationship.setLabel((String) virtualObject.get(LABEL));
if (startNode == null || endNode == null || type == null) {
throw new ReportException("The virtual relationship does not contain either start node, end node or type: " + relationship);
}
return (I) relationship;
case GRAPH:
SubGraph subgraph = new SubGraph();
subgraph.setId(subgraphId--);
Node parent = toIdentifiable(virtualObject.get(PARENT));
subgraph.setParent(parent);
subgraph.setLabel((String) virtualObject.get(LABEL));
addToValueGraph(subgraph, virtualObject.get(NODES));
addToValueGraph(subgraph, virtualObject.get(RELATIONSHIPS));
return (I) subgraph;
}
}
} else if (value instanceof CompositeObject) {
CompositeObject compositeObject = (CompositeObject) value;
Identifiable identifiable = toIdentifiable(compositeObject.getDelegate());
identifiable.setLabel(ReportHelper.getLabel(value));
return (I) identifiable;
} else if (value instanceof Neo4jNode) {
Neo4jNode<Neo4jLabel, ?, ?, ?> neo4jNode = (Neo4jNode<Neo4jLabel, ?, ?, ?>) value;
Node node = new Node();
node.setId(neo4jNode.getId());
for (Neo4jLabel label : neo4jNode.getLabels()) {
node.getLabels().add(label.getName());
}
node.getProperties().putAll(neo4jNode.getProperties());
return (I) node;
} else if (value instanceof Neo4jRelationship) {
Neo4jRelationship<?, ?, ?, ?, ?> neo4jRelationship = (Neo4jRelationship) value;
Relationship relationship = new Relationship();
relationship.setId(neo4jRelationship.getId());
relationship.setType(neo4jRelationship.getType().getName());
relationship.setStartNode(toIdentifiable(neo4jRelationship.getStartNode()));
relationship.setEndNode(toIdentifiable(neo4jRelationship.getEndNode()));
relationship.getProperties().putAll(neo4jRelationship.getProperties());
return (I) relationship;
}
throw new ReportException("Element type not supported: " + value);
}
Aggregations