Repository: apache/kafka
File: group-coordinator/src/test/java/org/apache/kafka/coordinator/group/modern/consumer/TopicRegexResolverTest.java (Java)

Code before:
        assertTrue(resolved.topics().isEmpty());
        assertEquals(image.version(), resolved.version());
        assertEquals(5L, resolved.timestamp());
    }

    @Test
    public void testAuthorizationFiltering() {
        CoordinatorMetadataImage image = imageWithTopics("allow1", "deny1", "allow2");
        Time time = new MockTime(10L, 0L, 0L);

        Authorizer authorizer = mock(Authorizer.class);
        when(authorizer.authorize(any(), any())).thenAnswer(invocation -> {
            List<Action> actions = invocation.getArgument(1);
            List<AuthorizationResult> results = new ArrayList<>(actions.size());
            for (Action action : actions) {
                String topic = action.resourcePattern().name();
                results.add("deny1".equals(topic) ? AuthorizationResult.DENIED : AuthorizationResult.ALLOWED);
            }
            return results;
        });

        Plugin<Authorizer> plugin = Plugin.wrapInstance(authorizer, null, "authorizer.class.name");

        TopicRegexResolver resolver = new TopicRegexResolver(() -> Optional.of(plugin), time);

        Map<String, ResolvedRegularExpression> result = resolver.resolveRegularExpressions(
            null,
            "group-3",
            log,
            image,
            Set.of("a.*", "d.*")
        );

        ResolvedRegularExpression resolved = result.get("a.*");
        assertEquals(Set.of("allow1", "allow2"), resolved.topics());
        assertEquals(image.version(), resolved.version());
        assertEquals(10L, resolved.timestamp());

        resolved = result.get("d.*");
        assertTrue(resolved.topics().isEmpty());
        assertEquals(image.version(), resolved.version());
        assertEquals(10L, resolved.timestamp());
    }
}


Review comment (refactor):
nit: We could use `var` in many place to simplify the code.

Code after:

    @Test
    public void testAuthorizationFiltering() {
        CoordinatorMetadataImage image = new MetadataImageBuilder()
            .addTopic(Uuid.randomUuid(), "allow1", 10)
            .addTopic(Uuid.randomUuid(), "deny1", 10)
            .addTopic(Uuid.randomUuid(), "allow2", 10)
            .buildCoordinatorMetadataImage();

        Time time = new MockTime(10L, 0L, 0L);

        Authorizer authorizer = mock(Authorizer.class);
        when(authorizer.authorize(any(), any())).thenAnswer(invocation -> {
            List<Action> actions = invocation.getArgument(1);
            var results = new ArrayList<>(actions.size());
            for (Action action : actions) {
                String topic = action.resourcePattern().name();
                results.add("deny1".equals(topic) ? AuthorizationResult.DENIED : AuthorizationResult.ALLOWED);
            }
            return results;
        });

        var plugin = Plugin.wrapInstance(authorizer, null, "authorizer.class.name");

        TopicRegexResolver resolver = new TopicRegexResolver(() -> Optional.of(plugin), time);

        var result = resolver.resolveRegularExpressions(
            null,
            "group-3",
            log,
            image,
            Set.of("a.*", "d.*")
        );

        var resolved = result.get("a.*");

        assertEquals(Set.of("allow1", "allow2"), resolved.topics());
        assertEquals(image.version(), resolved.version());
        assertEquals(10L, resolved.timestamp());

        resolved = result.get("d.*");
        assertTrue(resolved.topics().isEmpty());
        assertEquals(image.version(), resolved.version());
        assertEquals(10L, resolved.timestamp());
    }
}