PodNameFactory { String newName(LanderConfiguration config) { String prefix = TRUCK_PARK + "-"; int partition = Iterables.getFirst(config.getOffsets().keySet(), 0); String suffix = "-" + config.getAcquisitionInstant().toLowerCase(Locale.ROOT) + "-" + partition; String roadName = config.getRoadName().toLowerCase().replaceAll("_", "-"); String hash = hashFunction .hashString(roadName, StandardCharsets.UTF_8) .toString() .substring(0, MAX_HASH_STRING_LENGTH) .toLowerCase(Locale.ROOT); int remainingChars = MAX_POD_NAME_LENGTH - prefix.length() - suffix.length(); checkState(remainingChars > MAX_HASH_STRING_LENGTH, "between the process name and partition column value there is not enough room for the road name"); if (remainingChars < roadName.length()) { roadName = roadName.substring(0, remainingChars - hash.length()) + hash; } return prefix + roadName + suffix; }    }
@Test public void typical() { PodNameFactory factory = new PodNameFactory(); LanderConfiguration config = new LanderConfiguration("road_name", "road.road_name", singletonMap(0, null), "s3KeyPrefix", false, "partitionColumnValue10T28929Z", false); String name = factory.newName(config); assertThat(name, is("truck-park-road-name-partitioncolumnvalue10t28929z-0")); assertThat(namePattern.matcher(name).matches(), is(true)); }
@Test public void conf() { PodNameFactory factory = new PodNameFactory(); LanderConfiguration configuration = new LanderConfiguration("road_name", "topicName", singletonMap(0, null), "s3KeyPrefix", false, "partitionColumnValue102T8929Z", false); String name = factory.newName(configuration); assertThat(name, is("truck-park-road-name-partitioncolumnvalue102t8929z-0")); assertThat(namePattern.matcher(name).matches(), is(true)); }
@Test public void road_name_too_long_gets_truncated() throws Exception { PodNameFactory factory = new PodNameFactory(); LanderConfiguration configuration = new LanderConfiguration("ten-------twenty----thirty----forty-----fifty-----", "topicName", singletonMap(0, null), "s3KeyPrefix", false, "ninechars", false); String name = factory.newName(configuration); assertThat(name.length(), is(63)); assertThat(namePattern.matcher(name).matches(), is(true)); }
@Test(expected = IllegalStateException.class) public void partition_column_value_is_too_long_throws_IllegalStateException() throws Exception { PodNameFactory factory = new PodNameFactory(); LanderConfiguration configuration = new LanderConfiguration("road_name", "topicName", singletonMap(0, null), "s3KeyPrefix", false, "ten-------twenty----thirty----forty-----fifty-----sixty-----", false); factory.newName(configuration); }