Skip to content

Commit

Permalink
0004073: External ID unique parameter not working
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Aug 21, 2019
1 parent 8950e7c commit 4c38402
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.jumpmind.symmetric.model.NodeSecurity;
import org.jumpmind.symmetric.service.INodeService;
import org.jumpmind.symmetric.service.IParameterService;
import org.jumpmind.symmetric.service.RegistrationFailedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -52,60 +53,66 @@ public DefaultNodeIdCreator(IParameterService parameterService, INodeService nod
this.securityService = securityService;
}

/**
* Determine the node ID to use, given the node that is requesting to register, by trying to find its
* record with an open registration.
*/
public String selectNodeId(Node node, String remoteHost, String remoteAddress) {
final int maxTries = parameterService.getInt(ParameterConstants.NODE_ID_CREATOR_MAX_NODES, 100);
final boolean autoRegisterEnabled = parameterService.is(ParameterConstants.AUTO_REGISTER_ENABLED);
if (StringUtils.isBlank(node.getNodeId())) {
String nodeId = evaluateScript(node, remoteHost, remoteAddress);
if (StringUtils.isBlank(nodeId)) {
nodeId = buildNodeId(nodeService, node);
String nodeId = node.getNodeId();
if (StringUtils.isBlank(nodeId)) {
nodeId = evaluateScript(node, remoteHost, remoteAddress);
}
if (StringUtils.isBlank(nodeId)) {
String defaultNodeId = buildNodeId(nodeService, node);
if (parameterService.is(ParameterConstants.EXTERNAL_ID_IS_UNIQUE)) {
nodeId = defaultNodeId;
} else {
final int maxTries = parameterService.getInt(ParameterConstants.NODE_ID_CREATOR_MAX_NODES, 100);
String checkNodeId = defaultNodeId;
for (int sequence = 0; sequence < maxTries; sequence++) {
NodeSecurity security = nodeService.findNodeSecurity(nodeId);
if ((security != null && security.isRegistrationEnabled()) || autoRegisterEnabled) {
return nodeId;
NodeSecurity security = nodeService.findNodeSecurity(checkNodeId);
if (security != null && security.isRegistrationEnabled()) {
nodeId = checkNodeId;
break;
}
nodeId = buildNodeId(nodeService, node) + "-" + sequence;
checkNodeId = defaultNodeId + "-" + sequence;
}
}
return nodeId;

}
return node.getNodeId();
return nodeId;
}

/**
* Determine node ID for the node that is about to be created and opened for registration.
* Return an existing node to re-open its registration.
*/
public String generateNodeId(Node node, String remoteHost, String remoteAddress) {
final boolean autoRegisterEnabled = parameterService.is(ParameterConstants.AUTO_REGISTER_ENABLED);
final int maxTries = parameterService.getInt(ParameterConstants.NODE_ID_CREATOR_MAX_NODES, 100);
String nodeId = node.getNodeId();
if (StringUtils.isBlank(nodeId)) {
nodeId = evaluateScript(node, remoteHost, remoteAddress);
if (StringUtils.isBlank(nodeId)) {
nodeId = buildNodeId(nodeService, node);
if (!parameterService.is(ParameterConstants.EXTERNAL_ID_IS_UNIQUE)) {
for (int sequence = 0; sequence < maxTries; sequence++) {
if (nodeService.findNode(nodeId) == null || autoRegisterEnabled) {
break;
}
nodeId = buildNodeId(nodeService, node) + "-" + sequence;
}

if (nodeService.findNode(nodeId) != null && !autoRegisterEnabled) {
nodeId = null;
}
if (StringUtils.isBlank(nodeId)) {
String defaultNodeId = buildNodeId(nodeService, node);
if (parameterService.is(ParameterConstants.EXTERNAL_ID_IS_UNIQUE)) {
nodeId = defaultNodeId;
} else {
final int maxTries = parameterService.getInt(ParameterConstants.NODE_ID_CREATOR_MAX_NODES, 100);
String checkNodeId = defaultNodeId;
for (int sequence = 0; sequence < maxTries; sequence++) {
NodeSecurity security = nodeService.findNodeSecurity(checkNodeId);
if (security == null || security.isRegistrationEnabled()) {
nodeId = checkNodeId;
break;
}

checkNodeId = defaultNodeId + "-" + sequence;
}
if (StringUtils.isBlank(nodeId)) {
throw new RegistrationFailedException("Could not find an available nodeId to use for externalId of "
+ node.getExternalId() + " after " + maxTries + " tries.");
}


}
}

if (StringUtils.isNotBlank(nodeId)) {
return nodeId;
} else {
throw new RuntimeException("Could not find nodeId for externalId of "
+ node.getExternalId() + " after " + maxTries + " tries.");
}
return nodeId;
}

protected String buildNodeId(INodeService nodeService, Node node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,31 @@ public void testGenerateNodeIdExistingWithUniqueParam() throws Exception {
DefaultNodeIdCreator generator = new DefaultNodeIdCreator(paramService, new MockNodeService() {
@Override
public Node findNode(String nodeId) {

if (nodeId.equals("100")) {
Node node = new Node();
node.setNodeId("100");
node.setNodeId(nodeId);
return node;
} else {
return null;
}
}
public NodeSecurity findNodeSecurity(String nodeId) {
if (nodeId.equals("100")) {
NodeSecurity nodeSecurity = new NodeSecurity();
nodeSecurity.setNodeId(nodeId);
nodeSecurity.setRegistrationEnabled(false);
return nodeSecurity;
} else {
return null;
}
}
}, SecurityServiceFactory.create());
Node node = new Node();
node.setExternalId("100");
String selectedNodeId = generator.generateNodeId(node, null, null);
assertEquals(EXPECTED_NODE_ID, selectedNodeId);
}



@Test
public void testGenerateNodeIdWithMultipleExistingWithUniqueParam() throws Exception {
final String EXPECTED_NODE_ID = "100-5";
Expand All @@ -141,7 +149,6 @@ public void testGenerateNodeIdWithMultipleExistingWithUniqueParam() throws Excep
DefaultNodeIdCreator generator = new DefaultNodeIdCreator(paramService, new MockNodeService() {
@Override
public Node findNode(String nodeId) {

if (nodeId != null && !nodeId.equals("100-5")) {
Node node = new Node();
node.setNodeId(nodeId);
Expand All @@ -150,6 +157,16 @@ public Node findNode(String nodeId) {
return null;
}
}
public NodeSecurity findNodeSecurity(String nodeId) {
if (nodeId != null && !nodeId.equals("100-5")) {
NodeSecurity nodeSecurity = new NodeSecurity();
nodeSecurity.setNodeId(nodeId);
nodeSecurity.setRegistrationEnabled(false);
return nodeSecurity;
} else {
return null;
}
}
}, SecurityServiceFactory.create());
Node node = new Node();
node.setExternalId("100");
Expand All @@ -170,6 +187,13 @@ public Node findNode(String nodeId) {
node.setNodeId(nodeId);
return node;
}
public NodeSecurity findNodeSecurity(String nodeId) {
NodeSecurity nodeSecurity = new NodeSecurity();
nodeSecurity.setNodeId(nodeId);
nodeSecurity.setRegistrationEnabled(false);
return nodeSecurity;
}

}, SecurityServiceFactory.create());
Node node = new Node();
node.setExternalId("100");
Expand Down

0 comments on commit 4c38402

Please sign in to comment.