@Override public int calculateId(String name, Set<Integer> usedIds) { Integer reservedId = reservedIds.get(name); if (reservedId == null || usedIds.contains(reservedId)) { return fallback.calculateId(name, Sets.union(usedIds, new HashSet<Integer>(reservedIds .values()))); } return reservedId; }
public static String getDefaultCluster(String name) { String cluster = System.getProperty(PARFAIT_CLUSTER); if (cluster == null) { IdentifierSource clusterSource = new HashingIdentifierSource(1 << 12); Integer id = clusterSource.calculateId(name, Collections.<Integer>emptySet()); return id.toString(); } return cluster; }
@Override protected PcpMetricInfo newInstance(String name, Set<Integer> usedIds) { return new PcpMetricInfoV1(name, identifierSource.calculateId(name, usedIds)); } }
@Override protected Instance newInstance(String name, Set<Integer> usedIds) { return new InstanceV1(instanceDomain, name, identifierSource.calculateId(name, usedIds)); }
@Override protected InstanceDomain newInstance(String name, Set<Integer> usedIds) { return new InstanceDomain(name, identifierSource.calculateId(name, usedIds), instanceStoreFactory); }
@Override protected Instance newInstance(String name, Set<Integer> usedIds) { return new InstanceV2(instanceDomain, name, identifierSource.calculateId(name, usedIds), stringStore.createPcpString(name)); } }
@Override protected PcpMetricInfo newInstance(String name, Set<Integer> usedIds) { return new PcpMetricInfoV2(name, identifierSource.calculateId(name, usedIds), stringStore.createPcpString(name)); } }
private void assertDomainResultMatches(List<String> input) { IdentifierSourceSet sources = new StringParsingIdentifierSourceSet(input, Collections .<String> emptyList(), FALLBACK_SOURCES); assertEquals(DOMAIN_ID, sources.instanceDomainSource().calculateId(DOMAIN_NAME, Collections.<Integer> emptySet())); }
private void assertMetricResultMatches(List<String> input) { IdentifierSourceSet sources = new StringParsingIdentifierSourceSet(Collections .<String> emptyList(), input, FALLBACK_SOURCES); assertEquals(METRIC_ID, sources.metricSource().calculateId(METRIC_NAME, Collections.<Integer> emptySet())); }
private void assertInstanceResultMatches(List<String> input) { IdentifierSourceSet sources = new StringParsingIdentifierSourceSet(input, Collections .<String> emptyList(), FALLBACK_SOURCES); assertEquals(INSTANCE_ID, sources.instanceSource(DOMAIN_NAME).calculateId(INSTANCE_NAME, Collections.<Integer> emptySet())); } }
@Test public void instanceStoreShouldCreateANewInstance() { IdentifierSourceSet identifierSourceSet = mock(IdentifierSourceSet.class); IdentifierSource identifierSource = mock(IdentifierSource.class); InstanceDomain instanceDomain = mock(InstanceDomain.class); PcpStringStore stringStore = mock(PcpStringStore.class); PcpString pcpString = mock(PcpString.class); when(identifierSourceSet.instanceSource(INSTANCE_NAME)).thenReturn(identifierSource); when(identifierSource.calculateId(eq(INSTANCE_NAME), ArgumentMatchers.<Integer>anySet())).thenReturn(123); when(stringStore.createPcpString(INSTANCE_NAME)).thenReturn(pcpString); InstanceV2.InstanceStoreV2 instanceStore = new InstanceV2.InstanceStoreV2(identifierSourceSet, INSTANCE_NAME, instanceDomain, stringStore); Instance actual = instanceStore.byName(INSTANCE_NAME); InstanceV2 expected = new InstanceV2(instanceDomain, INSTANCE_NAME, 123, pcpString); assertReflectionEquals(expected, actual); }
@Test public void metricInfoStoreShouldCreateANewPcpMetricInfoV2() { IdentifierSourceSet identifierSourceSet = mock(IdentifierSourceSet.class); PcpStringStore stringStore = mock(PcpStringStore.class); PcpString pcpString = mock(PcpString.class); IdentifierSource identifierSource = mock(IdentifierSource.class); when(identifierSourceSet.metricSource()).thenReturn(identifierSource); when(identifierSource.calculateId(eq("my.metric"), ArgumentMatchers.<Integer>anySet())).thenReturn(123); when(stringStore.createPcpString("my.metric")).thenReturn(pcpString); MetricInfoStoreV2 metricInfoStoreV2 = new MetricInfoStoreV2(identifierSourceSet, stringStore); PcpMetricInfo actual = metricInfoStoreV2.byName("my.metric"); PcpMetricInfoV2 expected = new PcpMetricInfoV2("my.metric", 123, pcpString); assertReflectionEquals(expected, actual); } }
@Test public void nullFileShouldUseDefaultMetricSource() { assertEquals(FIXED_FALLBACK, buildFileSet(null, null).metricSource().calculateId( METRIC_NAME, NO_IDS)); }
@Test public void nullFileShouldUseDefaultInstanceSource() { assertEquals(FIXED_FALLBACK, buildFileSet(null, null).instanceDomainSource().calculateId( INSTANCE_NAME, NO_IDS)); }
@Test public void shouldFallBackWhenNoValueProvided() { assertEquals(FIXED_FALLBACK, emptySource().calculateId(PREDEFINED_STRING, noExistingIdentifiers())); }
@Test public void shouldReturnFixedValueWhenProvided() { assertEquals(PREDEFINED_IDENTIFIER, populatedSource().calculateId(PREDEFINED_STRING, noExistingIdentifiers())); }
@Test public void shouldReturnFallBackWhenValueCollides() { assertEquals(FIXED_FALLBACK, populatedSource().calculateId(PREDEFINED_STRING, collidingIdentifiers())); }
@Test public void metricValuesShouldBeParsedFromFile() { assertEquals(METRIC_ID, buildFileSet(null, tempFile(METRIC_FILE)).metricSource() .calculateId(METRIC_NAME, NO_IDS)); }
@Test public void instanceDomainValuesShouldBeParsedFromFile() { assertEquals(DOMAIN_ID, buildFileSet(tempFile(INSTANCE_FILE), null).instanceDomainSource() .calculateId(DOMAIN_NAME, NO_IDS)); }
@Test public void instanceValuesShouldBeParsedFromReader() { assertEquals(INSTANCE_ID, buildReaderSet(tempReader(INSTANCE_FILE), null).instanceSource( DOMAIN_NAME).calculateId(INSTANCE_NAME, NO_IDS)); }