Skip to content

Commit

Permalink
fix: restored possibility to update XMl configurations properties wit…
Browse files Browse the repository at this point in the history
…h null or empty values
  • Loading branch information
angelo.andreussi authored and Coduz committed Nov 4, 2024
1 parent 1fe8a81 commit 22e62f9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.HashMap;

public class XmlPropertiesAdapter<T extends Enum<T>, V extends XmlPropertyAdapted<T>> extends XmlAdapter<V[], Map<String, Object>> {
private final Class<V> propertyClass;
Expand All @@ -47,16 +48,18 @@ public Map<String, Object> unmarshal(V[] properties) {
}
})
.filter(adaptedProp -> xmlPropertyAdapters.containsKey((adaptedProp.getType())))
.collect(Collectors.toMap(
XmlPropertyAdapted::getName,
adaptedProp -> {
final XmlPropertyAdapter xmlPropertyAdapter = xmlPropertyAdapters.get(adaptedProp.getType());
return xmlPropertyAdapter.unmarshallValues(adaptedProp);
}));
.collect(HashMap::new,
(adaptedPropMap, adaptedProp) -> adaptedPropMap.put(adaptedProp.getName(), extractValueFromXmlProperty(adaptedProp)),
HashMap::putAll); // This is a work-around for Collectors.toMap limitation on null values

return unmarshalledProperties;
}

private Object extractValueFromXmlProperty(V adaptedProp) {
final XmlPropertyAdapter xmlPropertyAdapter = xmlPropertyAdapters.get(adaptedProp.getType());
return xmlPropertyAdapter.unmarshallValues(adaptedProp);
}

@Override
public V[] marshal(Map<String, Object> props) {
final List<V> adaptedProperties = Optional.ofNullable(props)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,33 @@ public void testUnmarshallingMissingType() {
new TestPropertyAdapted("anotherValue", null, "42")
}));
}

//Scenario in which, for example, I update a device configuration with some properties set to the default value (null or "") and some to a defined valued
@Test
public void testUnmarshallingEmptyNullValuesFields() {
final StringPropertyAdapter stringAdapter = Mockito.spy(new StringPropertyAdapter());
final BooleanPropertyAdapter booleanAdapter = Mockito.spy(new BooleanPropertyAdapter());
final LongPropertyAdapter longAdapter = Mockito.spy(new LongPropertyAdapter());
final HashMap<TestTypes, XmlPropertyAdapter> adapters = new HashMap<TestTypes, XmlPropertyAdapter>() {
{
put(TestTypes.First, stringAdapter);
put(TestTypes.Second, booleanAdapter);
put(TestTypes.Fourth, longAdapter);
}
};
final XmlPropertiesAdapter instance = new TestPropertiesAdapter(adapters);
final Map<String, Object> got = instance.unmarshal(new TestPropertyAdapted[]{
new TestPropertyAdapted("aString", TestTypes.First, "TheString"),
new TestPropertyAdapted("emptyValues", TestTypes.Second, "", ""),
new TestPropertyAdapted("emptyValue", TestTypes.Fourth, ""),
new TestPropertyAdapted("nullValue", TestTypes.Fourth, (String) null)
});
Assert.assertNotNull(got);
Assert.assertEquals(4, got.keySet().size());
Assert.assertNotNull(got.get("aString"));
Assert.assertArrayEquals(new Boolean[]{null, null}, (Boolean[]) got.get("emptyValues"));
Assert.assertNull(got.get("emptyValue"));
Assert.assertNull(got.get("nullValue"));
}

}

0 comments on commit 22e62f9

Please sign in to comment.