ElementHelper { public static void attachProperties(final Element element, final Object... propertyKeyValues) { if (null == element) throw Graph.Exceptions.argumentCanNotBeNull("element"); final boolean allowNullPropertyValues = element instanceof Vertex ? element.graph().features().vertex().supportsNullPropertyValues() : element instanceof Edge ? element.graph().features().edge().supportsNullPropertyValues() : element.graph().features().vertex().properties().supportsNullPropertyValues(); for (int i = 0; i < propertyKeyValues.length; i = i + 2) { if (!propertyKeyValues[i].equals(T.id) && !propertyKeyValues[i].equals(T.label)) if (!allowNullPropertyValues && null == propertyKeyValues[i + 1]) element.properties(((String) propertyKeyValues[i])).forEachRemaining(Property::remove); else element.property((String) propertyKeyValues[i], propertyKeyValues[i + 1]); } } private  ElementHelper(); static void validateLabel(final String label); static void validateMixedElementIds(final Class<? extends Element> clazz, final Object... ids); static void validateProperty(final String key, final Object value); static void legalPropertyKeyValueArray(final Object... propertyKeyValues); static Optional<Object> getIdValue(final Object... keyValues); static Optional<Object[]> remove(final String keyToRemove, final Object... keyValues); static Optional<Object[]> remove(final T accessor, final Object... keyValues); static Object[] upsert(final Object[] keyValues, final Object key, final Object val); static Object[] replaceKey(final Object[] keyValues, final Object oldKey, final Object newKey); static Map<String, Object> asMap(final Object... keyValues); static List<Pair<String, Object>> asPairs(final Object... keyValues); static Set<String> getKeys(final Object... keyValues); static Optional<String> getLabelValue(final Object... keyValues); static void attachProperties(final Element element, final Object... propertyKeyValues); static void attachProperties(final Vertex vertex, final Object... propertyKeyValues); static void attachProperties(final Vertex vertex, final VertexProperty.Cardinality cardinality,
                                        final Object... propertyKeyValues); static Optional<VertexProperty<V>> stageVertexProperty(final Vertex vertex,
                                                                      final VertexProperty.Cardinality cardinality,
                                                                      final String key, final V value, final Object... keyValues); static Object[] getProperties(final Element element, final boolean includeId, final boolean includeLabel, final Set<String> propertiesToCopy); static boolean areEqual(final Element a, final Object b); static boolean areEqual(final Vertex a, final Vertex b); static boolean areEqual(final Edge a, final Edge b); static boolean areEqual(final VertexProperty a, final VertexProperty b); static boolean areEqual(final VertexProperty a, final Object b); static boolean haveEqualIds(final Element a, final Element b); static int hashCode(final Element element); static int hashCode(final Property property); static boolean areEqual(final Property a, final Object b); static Map<String, Object> propertyValueMap(final Element element, final String... propertyKeys); static Map<String, Property> propertyMap(final Element element, final String... propertyKeys); static Map<String, List> vertexPropertyValueMap(final Vertex vertex, final String... propertyKeys); static Map<String, List<VertexProperty>> vertexPropertyMap(final Vertex vertex, final String... propertyKeys); static boolean keyExists(final String key, final String... providedKeys); static boolean idExists(final Object id, final Object... providedIds);  }
@Test public void shouldAttachPropertiesButNotLabelsOrId() { final Element mockElement = mock(Element.class); final Graph mockGraph = mock(Graph.class); final Graph.Features mockGraphFeatures = mock(Graph.Features.class); final Graph.Features.VertexFeatures mockVertexFeatures = mock(Graph.Features.VertexFeatures.class); final Graph.Features.VertexPropertyFeatures mockVertexPropertyFeatures = mock(Graph.Features.VertexPropertyFeatures.class); when(mockElement.graph()).thenReturn(mockGraph); when(mockGraph.features()).thenReturn(mockGraphFeatures); when(mockGraphFeatures.vertex()).thenReturn(mockVertexFeatures); when(mockVertexFeatures.properties()).thenReturn(mockVertexPropertyFeatures); when(mockVertexPropertyFeatures.supportsNullPropertyValues()).thenReturn(true); ElementHelper.attachProperties(mockElement, "test", 123, T.id, 321, T.label, "friends"); verify(mockElement, times(1)).property("test", 123); verify(mockElement, times(0)).property(T.id.getAccessor(), 321); verify(mockElement, times(0)).property(T.label.getAccessor(), "friends"); }
@Test(expected = ClassCastException.class) public void shouldFailTryingToAttachPropertiesNonStringKey() { final Element mockElement = mock(Element.class); final Graph mockGraph = mock(Graph.class); final Graph.Features mockGraphFeatures = mock(Graph.Features.class); final Graph.Features.VertexFeatures mockVertexFeatures = mock(Graph.Features.VertexFeatures.class); final Graph.Features.VertexPropertyFeatures mockVertexPropertyFeatures = mock(Graph.Features.VertexPropertyFeatures.class); when(mockElement.graph()).thenReturn(mockGraph); when(mockGraph.features()).thenReturn(mockGraphFeatures); when(mockGraphFeatures.vertex()).thenReturn(mockVertexFeatures); when(mockVertexFeatures.properties()).thenReturn(mockVertexPropertyFeatures); when(mockVertexPropertyFeatures.supportsNullPropertyValues()).thenReturn(true); ElementHelper.attachProperties(mockElement, "test", 123, 321, "test"); }
@Test public void shouldFailTryingToAttachPropertiesToNullElement() { try { ElementHelper.attachProperties(null, "test", 123, 321, "test"); fail("Should throw exception since the element argument is null"); } catch (IllegalArgumentException iae) { assertTrue(Graph.Exceptions.argumentCanNotBeNull("vertex").getMessage().equals(iae.getMessage()) || Graph.Exceptions.argumentCanNotBeNull("element").getMessage().equals(iae.getMessage())); } }
@Test public void shouldAttachPropertiesWithCardinalityButNotLabelsOrId() { final Vertex mockElement = mock(Vertex.class); final Graph mockGraph = mock(Graph.class); final Graph.Features mockGraphFeatures = mock(Graph.Features.class); final Graph.Features.VertexFeatures mockVertexFeatures = mock(Graph.Features.VertexFeatures.class); final Graph.Features.VertexPropertyFeatures mockVertexPropertyFeatures = mock(Graph.Features.VertexPropertyFeatures.class); when(mockElement.graph()).thenReturn(mockGraph); when(mockGraph.features()).thenReturn(mockGraphFeatures); when(mockGraphFeatures.vertex()).thenReturn(mockVertexFeatures); when(mockVertexFeatures.properties()).thenReturn(mockVertexPropertyFeatures); when(mockVertexPropertyFeatures.supportsNullPropertyValues()).thenReturn(true); ElementHelper.attachProperties(mockElement, VertexProperty.Cardinality.single, "test", 123, T.id, 321, T.label, "friends"); verify(mockElement, times(1)).property(VertexProperty.Cardinality.single, "test", 123); verify(mockElement, times(0)).property(VertexProperty.Cardinality.single, T.id.getAccessor(), 321); verify(mockElement, times(0)).property(VertexProperty.Cardinality.single, T.label.getAccessor(), "friends"); }
@Test(expected = ClassCastException.class) public void shouldFailTryingToAttachPropertiesWithCardinalityNonStringKey() { final Element mockElement = mock(Vertex.class); final Graph mockGraph = mock(Graph.class); final Graph.Features mockGraphFeatures = mock(Graph.Features.class); final Graph.Features.VertexFeatures mockVertexFeatures = mock(Graph.Features.VertexFeatures.class); final Graph.Features.VertexPropertyFeatures mockVertexPropertyFeatures = mock(Graph.Features.VertexPropertyFeatures.class); when(mockElement.graph()).thenReturn(mockGraph); when(mockGraph.features()).thenReturn(mockGraphFeatures); when(mockGraphFeatures.vertex()).thenReturn(mockVertexFeatures); when(mockVertexFeatures.properties()).thenReturn(mockVertexPropertyFeatures); when(mockVertexPropertyFeatures.supportsNullPropertyValues()).thenReturn(true); ElementHelper.attachProperties(mockElement, VertexProperty.Cardinality.single, "test", 123, 321, "test"); }
@Test public void shouldFailTryingToAttachPropertiesWithCardinalityToNullElement() { try { ElementHelper.attachProperties((Vertex) null, VertexProperty.Cardinality.single, "test", 123, 321, "test"); fail("Should throw exception since the element argument is null"); } catch (IllegalArgumentException iae) { assertEquals(Graph.Exceptions.argumentCanNotBeNull("vertex").getMessage(), iae.getMessage()); } }