DefaultNodeIdCreator implements INodeIdCreator, IBuiltInExtensionPoint { public String generateNodeId(Node node, String remoteHost, String remoteAddress) { 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(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."); } } } return nodeId; } DefaultNodeIdCreator(IParameterService parameterService, INodeService nodeService, ISecurityService securityService); String selectNodeId(Node node, String remoteHost, String remoteAddress); String generateNodeId(Node node, String remoteHost, String remoteAddress); String generatePassword(Node node);  }
@Test public void testGenerateNodeIdNoExisting() throws Exception { final String EXPECTED_NODE_ID = "100"; DefaultNodeIdCreator generator = new DefaultNodeIdCreator(new MockParameterService(), new MockNodeService(), SecurityServiceFactory.create()); Node node = new Node(); node.setExternalId("100"); String selectedNodeId = generator.generateNodeId(node, null, null); assertEquals(EXPECTED_NODE_ID, selectedNodeId); }
@Test public void testGenerateNodeIdExisting() throws Exception { final String EXPECTED_NODE_ID = "100"; DefaultNodeIdCreator generator = new DefaultNodeIdCreator(new MockParameterService( ParameterConstants.EXTERNAL_ID_IS_UNIQUE, "true"), new MockNodeService() { @Override public Node findNode(String nodeId) { if (nodeId.equals("100")) { Node node = new Node(); node.setNodeId("100"); return node; } 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 testGenerateNodeIdExistingWithUniqueParam() throws Exception { final String EXPECTED_NODE_ID = "100-0"; IParameterService paramService = mock(IParameterService.class); Mockito.when(paramService.is(ParameterConstants.EXTERNAL_ID_IS_UNIQUE)).thenReturn(false); Mockito.when(paramService.getInt(ParameterConstants.NODE_ID_CREATOR_MAX_NODES, 100)).thenReturn(100); DefaultNodeIdCreator generator = new DefaultNodeIdCreator(paramService, new MockNodeService() { @Override public Node findNode(String nodeId) { if (nodeId.equals("100")) { Node node = new Node(); 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"; IParameterService paramService = mock(IParameterService.class); Mockito.when(paramService.is(ParameterConstants.EXTERNAL_ID_IS_UNIQUE)).thenReturn(false); Mockito.when(paramService.getInt(ParameterConstants.NODE_ID_CREATOR_MAX_NODES, 100)).thenReturn(100); 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); return node; } else { 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"); String selectedNodeId = generator.generateNodeId(node, null, null); assertEquals(EXPECTED_NODE_ID, selectedNodeId); }
@Test public void testGenerateNodeIdExistingAll() throws Exception { IParameterService paramService = mock(IParameterService.class); Mockito.when(paramService.is(ParameterConstants.EXTERNAL_ID_IS_UNIQUE)).thenReturn(false); Mockito.when(paramService.getInt(ParameterConstants.NODE_ID_CREATOR_MAX_NODES, 100)).thenReturn(100); DefaultNodeIdCreator generator = new DefaultNodeIdCreator(paramService, new MockNodeService() { @Override public Node findNode(String nodeId) { Node node = new Node(); 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"); try { generator.generateNodeId(node, null, null); fail("Should have received exception"); } catch (Exception e) { } }
@Test public void testGenerateNodeIdWithNodeIdSet() throws Exception { DefaultNodeIdCreator generator = new DefaultNodeIdCreator(new MockParameterService(), new MockNodeService(), SecurityServiceFactory.create()); Node node = new Node(); final String EXPECTED_NODE_ID = "10001"; node.setExternalId(EXPECTED_NODE_ID); node.setNodeId(EXPECTED_NODE_ID); String selectedNodeId = generator.generateNodeId(node, null, null); assertEquals(EXPECTED_NODE_ID, selectedNodeId); }