Element(final String group, final Properties properties) { this.group = group; if (null == properties) { this.properties = new Properties(); } else { this.properties = properties; } }
Element(final String group) { this(group, new Properties()); }
@Override public Properties clone() { return new Properties((Map<String, Object>) super.clone()); }
@Override public Edge emptyClone() { return new Edge( getGroup(), getSource(), getDestination(), isDirected(), getMatchedVertex(), new Properties() ); }
@Test public void shouldConstructEmptyProperties() { // Given // When final Properties properties = new Properties(); // Then assertTrue(properties.isEmpty()); }
@Test public void shouldConstructPropertiesWithOtherProperties() { // Given final Map<String, Object> otherProperties = new HashMap<>(); otherProperties.put("propertyName", "property value"); // When final Properties properties = new Properties(otherProperties); // Then assertEquals(1, properties.size()); assertEquals("property value", properties.get("propertyName")); }
@Test public void shouldNotAddPropertyIfPropertyNameIsNull() { // When final Properties properties = new Properties(); properties.put(null, "propertyValue1"); // Then assertEquals(0, properties.size()); }
@Test public void shouldReturnHumanReadableToString() { // Given final String property1 = "property 1"; final String property2 = "property 2"; final String propertyValue1 = "property value 1"; final String propertyValue2 = "property value 2"; final Properties properties = new Properties(); properties.put(property1, propertyValue1); properties.put(property2, propertyValue2); // When final String toString = properties.toString(); // Then assertTrue(toString.contains("property 2=" + "<java.lang.String>property value 2")); assertTrue(toString.contains("property 1=" + "<java.lang.String>property value 1")); } }
@Test public void shouldNotLoadPropertyWhenLoaded() { // Given final ElementValueLoader elementLoader = mock(ElementValueLoader.class); final String propertyName = "property name"; final String exceptedPropertyValue = "property value"; final Properties properties = new Properties(propertyName, exceptedPropertyValue); final LazyProperties lazyProperties = new LazyProperties(properties, elementLoader); given(elementLoader.getProperty(propertyName, lazyProperties)).willReturn(exceptedPropertyValue); // When final Object propertyValue = lazyProperties.get(propertyName); // Then assertEquals(exceptedPropertyValue, propertyValue); verify(elementLoader, never()).getProperty(propertyName, lazyProperties); }
@Test public void shouldAddPropertyToMapWhenAddingProperty() { // Given final ElementValueLoader elementLoader = mock(ElementValueLoader.class); final Properties properties = new Properties(); final String propertyName = "property name"; final String propertyValue = "property value"; final LazyProperties lazyProperties = new LazyProperties(properties, elementLoader); given(elementLoader.getProperty(propertyName, lazyProperties)).willReturn(propertyValue); // When lazyProperties.put(propertyName, propertyValue); // Then verify(elementLoader, never()).getProperty(propertyName, lazyProperties); assertEquals(propertyValue, properties.get(propertyName)); }
@Test public void shouldConstructPropertiesWithProperty() { // Given // When final Properties properties = new Properties("propertyName", "property value"); // Then assertEquals(1, properties.size()); assertEquals("property value", properties.get("propertyName")); }
@Test public void shouldRemovePropertyNameFromLoadedPropertiesAndMapWhenRemoveIsCalled() { // Given final ElementValueLoader elementLoader = mock(ElementValueLoader.class); final Properties properties = new Properties(); final String propertyName = "property name"; final String propertyValue = "property value"; final LazyProperties lazyProperties = new LazyProperties(properties, elementLoader); given(elementLoader.getProperty(propertyName, lazyProperties)).willReturn(propertyValue); lazyProperties.get(propertyName); // call it to load the value. // When lazyProperties.remove(propertyName); lazyProperties.get(propertyName); // Then verify(elementLoader, times(2)).getProperty(propertyName, lazyProperties); // should be called twice before and after removeProperty() assertEquals(propertyValue, properties.get(propertyName)); }
@Test public void shouldClearLoadedPropertiesAndMapWhenClearIsCalled() { // Given final ElementValueLoader elementLoader = mock(ElementValueLoader.class); final Properties properties = new Properties(); final String propertyName = "property name"; final String propertyValue = "property value"; final LazyProperties lazyProperties = new LazyProperties(properties, elementLoader); given(elementLoader.getProperty(propertyName, lazyProperties)).willReturn(propertyValue); lazyProperties.get(propertyName); // call it to load the value. // When lazyProperties.clear(); lazyProperties.get(propertyName); // Then verify(elementLoader, times(2)).getProperty(propertyName, lazyProperties); // should be called twice before and after clear() assertEquals(propertyValue, properties.get(propertyName)); }
@Test public void shouldAggregateProperties() { // Given final String reference = "reference1"; final Integer value1 = 1; final Integer value2 = 2; final Integer valueResult = 3; final BinaryOperator<Integer> function = mock(BinaryOperator.class); given(function.apply(value1, value2)).willReturn(valueResult); final ElementAggregator aggregator = new ElementAggregator.Builder() .select(reference) .execute(function) .build(); final Properties properties1 = new Properties(reference, value1); final Properties properties2 = new Properties(reference, value2); // When final Properties result = aggregator.apply(properties1, properties2); // Then assertEquals(valueResult, result.get(reference)); }
@Test public void shouldLoadPropertyWhenNotLoaded() { // Given final ElementValueLoader elementLoader = mock(ElementValueLoader.class); final Properties properties = new Properties(); final String propertyName = "property name"; final String exceptedPropertyValue = "property value"; final LazyProperties lazyProperties = new LazyProperties(properties, elementLoader); given(elementLoader.getProperty(propertyName, lazyProperties)).willReturn(exceptedPropertyValue); // When final Object propertyValue = lazyProperties.get(propertyName); // Then assertEquals(exceptedPropertyValue, propertyValue); assertEquals(propertyValue, properties.get(propertyName)); }
@Test public void shouldSerialiseAndDeserialiseProperties() throws SerialisationException { // Given final Element element = newElement("group"); final Properties properties = new Properties(); properties.put("property1", 1L); properties.put("property2", 2); properties.put("property3", (double) 3); properties.put("property4", "4"); properties.put("property5", new Date(5L)); element.setProperties(properties); // When final byte[] serialisedElement = JSONSerialiser.serialise(element); final Element deserialisedElement = JSONSerialiser.deserialise(serialisedElement, element.getClass()); // Then assertTrue(StringUtil.toString(serialisedElement).contains("{\"java.util.Date\":5}")); assertEquals(element, deserialisedElement); }
@Test public void shouldRemovePropertyIfAddedWithNullValue() { // Given final Properties properties = new Properties(); properties.put("property1", "propertyValue1"); properties.put("property2", "propertyValue2"); // When properties.put("property1", null); // Then assertEquals(1, properties.size()); assertEquals(null, properties.get("property1")); }
@Test public void shouldCopyProperties() { // Given final Element element1 = newElement("group"); final Properties newProperties = new Properties("property1", "propertyValue1"); // When element1.copyProperties(newProperties); // Then assertEquals(1, element1.getProperties().size()); assertEquals("propertyValue1", element1.getProperty("property1")); }
@Test public void shouldSetAndGetFields() { // Given final String group = "group"; final Properties properties = new Properties(); final Element element = newElement(); // When element.setGroup(group); element.setProperties(properties); // Then assertEquals(group, element.getGroup()); assertSame(properties, element.getProperties()); }
@Test public void shouldCloneProperties() { // Given final String property1 = "property 1"; final String property2 = "property 2"; final String propertyValue1 = "property value 1"; final String propertyValue2 = "property value 2"; final Properties properties = new Properties(); properties.put(property1, propertyValue1); properties.put(property2, propertyValue2); // When final Properties clone = properties.clone(); // Then assertEquals(2, clone.size()); assertNotSame(properties, clone); assertEquals(propertyValue1, clone.get(property1)); assertEquals(propertyValue2, clone.get(property2)); }