Commit 78c504c0 authored by Georg Mittendorfer's avatar Georg Mittendorfer

Allow registration of pow node with same url as iri node

This is necessary in certain cases where a POW node is at the same
address as an IRI node (e.g. load balanced to a POW fpga).
parent a082aee2
Pipeline #70473531 passed with stage
in 3 minutes and 33 seconds
......@@ -112,8 +112,9 @@ public class NodeRegistry {
throw new CannotRegisterNode(CONFLICTING, msg);
}
Collection<? extends Node> nodes = this.nodes.values();
if (nodes.stream().anyMatch(n -> urlValidator.areEqual(url, n.getUrl()))
|| nodes.stream().anyMatch(n -> urlValidator.resolveToSameAddress(url, n.getUrl()))) {
if (nodes.stream()
.filter(n -> !(n instanceof PowNode)) // do not compare pow nodes (they might have the same url as iri nodes)
.anyMatch(n -> urlValidator.areEqual(url, n.getUrl()) || urlValidator.resolveToSameAddress(url, n.getUrl()))) {
String msg = String.format("Node with url [%s] is already registered.", url);
logger.debug("Cannot register node. {}", msg);
throw new CannotRegisterNode(CONFLICTING, msg);
......
......@@ -181,6 +181,40 @@ public class NodeRegistryTest {
assertThat(result.getCause().getMessage()).containsIgnoringCase("invalid key");
}
@Test
public void givenDuplicateUrlWhenRegisterThenThrow() {
when(urlValidator.areEqual("http://bar", "http://foo")).thenReturn(true);
registry.registerIriNode("foo", "http://foo", "some-key", false);
Try result = Try.ofSupplier(() -> registry.registerIriNode("bar", "http://bar", "some-key", false));
assertThat(result.isFailure()).isTrue();
assertThat(result.getCause()).isInstanceOf(CannotRegisterNode.class);
assertThat(result.getCause().getMessage()).containsIgnoringCase("already registered");
}
@Test
public void givenDuplicateIpWhenRegisterThenThrow() {
when(urlValidator.resolveToSameAddress("http://bar", "http://foo")).thenReturn(true);
registry.registerIriNode("foo", "http://foo", "some-key", false);
Try result = Try.ofSupplier(() -> registry.registerIriNode("bar", "http://bar", "some-key", false));
assertThat(result.isFailure()).isTrue();
assertThat(result.getCause()).isInstanceOf(CannotRegisterNode.class);
assertThat(result.getCause().getMessage()).containsIgnoringCase("already registered");
}
/**
* It is allowed to add pow nodes (even multiple times) that have the same url as IRI nodes.
*/
@Test
public void givenDuplicatePowUrlWhenRegisterThenIgnore() {
when(urlValidator.resolveToSameAddress(anyString(), anyString())).thenReturn(true);
registry.registerPowNode("foo", "http://foo", "some-key");
Try result = Try.ofSupplier(() -> registry.registerIriNode("bar", "http://bar", "some-key", false));
assertThat(result.isSuccess()).isTrue();
Try result2 = Try.ofSupplier(() -> registry.registerIriNode("bar2", "http://bar", "some-key", false));
assertThat(result2.isSuccess()).isFalse(); // registration of second IRI node should not be possible
}
private HashMap<String, IriNode> nodeMapWithTwoNodes() {
HashMap<String, IriNode> nodes = new HashMap<>();
nodes.put("a-node", mock(IriNode.class));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment