ConfigurationService { public Map<Integer, String> getShardingItemParameters() { String value = jobNodeStorage.getJobNodeDataDirectly(ConfigurationNode.SHARDING_ITEM_PARAMETERS); if (Strings.isNullOrEmpty(value)) { return Collections.emptyMap(); } String[] shardingItemParameters = value.split(","); Map<Integer, String> result = new HashMap<>(shardingItemParameters.length); for (String each : shardingItemParameters) { String[] pair = each.trim().split("="); if (2 != pair.length) { throw new ShardingItemParametersException("Sharding item parameters '%s' format error, should be int=xx,int=xx", value); } try { result.put(Integer.parseInt(pair[0].trim()), pair[1].trim()); } catch (final NumberFormatException ex) { throw new ShardingItemParametersException("Sharding item parameters key '%s' is not an integer.", pair[0]); } } return result; } ConfigurationService(final CoordinatorRegistryCenter coordinatorRegistryCenter, final JobConfiguration jobConfiguration); void persistJobConfiguration(); int getShardingTotalCount(); Map<Integer, String> getShardingItemParameters(); String getJobParameter(); String getCron(); boolean isMonitorExecution(); int getProcessCountIntervalSeconds(); int getConcurrentDataProcessThreadCount(); int getFetchDataCount(); void checkMaxTimeDiffSecondsTolerable(); boolean isFailover(); boolean isMisfire(); String getJobShardingStrategyClass(); int getMonitorPort(); String getJobName(); String getScriptCommandLine();  }
@Test public void assertGetShardingItemParametersWhenIsEmpty() { when(jobNodeStorage.getJobNodeDataDirectly(ConfigurationNode.SHARDING_ITEM_PARAMETERS)).thenReturn(""); assertThat(configService.getShardingItemParameters(), is(Collections.EMPTY_MAP)); verify(jobNodeStorage).getJobNodeDataDirectly(ConfigurationNode.SHARDING_ITEM_PARAMETERS); }
@Test(expected = ShardingItemParametersException.class) public void assertGetShardingItemParametersWhenPairFormatInvalid() { when(jobNodeStorage.getJobNodeDataDirectly(ConfigurationNode.SHARDING_ITEM_PARAMETERS)).thenReturn("xxx-xxx"); try { configService.getShardingItemParameters(); } finally { verify(jobNodeStorage).getJobNodeDataDirectly(ConfigurationNode.SHARDING_ITEM_PARAMETERS); } }
@Test(expected = ShardingItemParametersException.class) public void assertGetShardingItemParametersWhenItemIsNotNumber() { when(jobNodeStorage.getJobNodeDataDirectly(ConfigurationNode.SHARDING_ITEM_PARAMETERS)).thenReturn("xxx=xxx"); try { configService.getShardingItemParameters(); } finally { verify(jobNodeStorage).getJobNodeDataDirectly(ConfigurationNode.SHARDING_ITEM_PARAMETERS); } }
@Test public void assertGetShardingItemParameters() { when(jobNodeStorage.getJobNodeDataDirectly(ConfigurationNode.SHARDING_ITEM_PARAMETERS)).thenReturn("0=A,1=B,2=C"); Map<Integer, String> expected = new HashMap<>(3); expected.put(0, "A"); expected.put(1, "B"); expected.put(2, "C"); assertThat(configService.getShardingItemParameters(), is(expected)); verify(jobNodeStorage).getJobNodeDataDirectly(ConfigurationNode.SHARDING_ITEM_PARAMETERS); }