### Question:
Token { public static List<Token> getTokens(Locator locator) { if (StringUtils.isEmpty(locator.getMetricName()) || StringUtils.isEmpty(locator.getTenantId())) return new ArrayList<>(); String[] tokens = locator.getMetricName().split(Locator.METRIC_TOKEN_SEPARATOR_REGEX); return IntStream.range(0, tokens.length) .mapToObj(index -> new Token(locator, tokens, index)) .collect(toList()); } Token(Locator locator, String[] tokens, int level); @Override boolean equals(Object o); @Override int hashCode(); String getId(); Locator getLocator(); String getToken(); String getParent(); boolean isLeaf(); static List<Token> getTokens(Locator locator); static Stream<Token> getUniqueTokens(Stream<Locator> locators); @Override String toString(); static final String LEAF_NODE_SUFFIX; static final String SEPARATOR; }### Answer:
@Test public void testGetTokensHappyCase() { String tenantID = "111111"; String metricName = "a.b.c.d"; Locator locator = Locator.createLocatorFromPathComponents(tenantID, metricName); String[] expectedTokens = new String[] {"a", "b", "c", "d"}; String[] expectedParents = new String[] { "", "a", "a.b", "a.b.c"}; String[] expectedIds = new String[] { tenantID + ":" + "a", tenantID + ":" + "a.b", tenantID + ":" + "a.b.c", tenantID + ":" + "a.b.c.d:$"}; List<Token> tokens = Token.getTokens(locator); verifyTokenInfos(tenantID, expectedTokens, expectedParents, expectedIds, tokens); }
@Test public void testGetTokensForMetricWithOneToken() { String tenantID = "111111"; String metricName = "a"; Locator locator = Locator.createLocatorFromPathComponents(tenantID, metricName); String[] expectedTokens = new String[] {"a"}; String[] expectedParents = new String[] {""}; String[] expectedIds = new String[] {tenantID + ":" + "a:$"}; List<Token> tokens = Token.getTokens(locator); verifyTokenInfos(tenantID, expectedTokens, expectedParents, expectedIds, tokens); }
@Test public void testGetTokensWithEmptyTokenInBetween() { String tenantID = "111111"; String metricName = "ingest00.HeaderNormalization.header-normalization..*_GET.count"; Locator locator = Locator.createLocatorFromPathComponents(tenantID, metricName); String[] expectedTokens = new String[] {"ingest00", "HeaderNormalization", "header-normalization", "", "*_GET", "count"}; String[] expectedParents = new String[] { "", "ingest00", "ingest00.HeaderNormalization", "ingest00.HeaderNormalization.header-normalization", "ingest00.HeaderNormalization.header-normalization.", "ingest00.HeaderNormalization.header-normalization..*_GET"}; String[] expectedIds = new String[] { tenantID + ":" + "ingest00", tenantID + ":" + "ingest00.HeaderNormalization", tenantID + ":" + "ingest00.HeaderNormalization.header-normalization", tenantID + ":" + "ingest00.HeaderNormalization.header-normalization.", tenantID + ":" + "ingest00.HeaderNormalization.header-normalization..*_GET", tenantID + ":" + "ingest00.HeaderNormalization.header-normalization..*_GET.count:$"}; List<Token> tokens = Token.getTokens(locator); verifyTokenInfos(tenantID, expectedTokens, expectedParents, expectedIds, tokens); }
@Test public void testGetTokensForMetricNoTokens() { String tenantID = "111111"; String metricName = ""; Locator locator = Locator.createLocatorFromPathComponents(tenantID, metricName); List<Token> tokens = Token.getTokens(locator); assertEquals("Total number of tokens invalid", 0, tokens.size()); }