You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by da...@apache.org on 2021/11/11 15:45:54 UTC

[kafka] branch 3.1 updated: MINOR: Introduce `ApiKeyVersionsSource` annotation for `ParameterizedTest` (#11468)

This is an automated email from the ASF dual-hosted git repository.

dajac pushed a commit to branch 3.1
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/3.1 by this push:
     new 05636d1  MINOR: Introduce `ApiKeyVersionsSource` annotation for `ParameterizedTest` (#11468)
05636d1 is described below

commit 05636d134e06cff4ca1efddc3bd3a65964162f38
Author: David Jacot <dj...@confluent.io>
AuthorDate: Thu Nov 11 16:39:08 2021 +0100

    MINOR: Introduce `ApiKeyVersionsSource` annotation for `ParameterizedTest` (#11468)
    
    It is common in our code base to have unit tests which must be run for all the versions of a given request. Most of the time, we do so by iterating over all the versions in the test itself which is error prone.
    
    With JUnit5 and ParameterizedTest, we can now use a custom arguments source for this case, which is way more convenient. It looks likes this:
    
    ```
    @ParameterizedTest
    @ApiKeyVersionsSource(apiKey = ApiKeys.ADD_PARTITIONS_TO_TXN)
    public void mytest(short version) {
      // do smth based on version...
    }
    ```
    
    This patch introduces the new annotation and updates `AddPartitionsToTxnRequestTest` test as a first example. I will migrate all the other cases in subsequent PRs.
    
    Reviewers: Luke Chen <sh...@gmail.com>, Jason Gustafson <ja...@confluent.io>
---
 .../requests/AddPartitionsToTxnRequestTest.java    | 27 ++++++++--------
 .../utils/annotation/ApiKeyVersionsProvider.java   | 36 ++++++++++++++++++++++
 .../utils/annotation/ApiKeyVersionsSource.java     | 31 +++++++++++++++++++
 3 files changed, 80 insertions(+), 14 deletions(-)

diff --git a/clients/src/test/java/org/apache/kafka/common/requests/AddPartitionsToTxnRequestTest.java b/clients/src/test/java/org/apache/kafka/common/requests/AddPartitionsToTxnRequestTest.java
index acba8be..04bde4a 100644
--- a/clients/src/test/java/org/apache/kafka/common/requests/AddPartitionsToTxnRequestTest.java
+++ b/clients/src/test/java/org/apache/kafka/common/requests/AddPartitionsToTxnRequestTest.java
@@ -17,14 +17,15 @@
 package org.apache.kafka.common.requests;
 
 import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.utils.annotation.ApiKeyVersionsSource;
 import org.apache.kafka.common.protocol.ApiKeys;
 import org.apache.kafka.common.protocol.Errors;
-import org.junit.jupiter.api.Test;
 
 import java.util.ArrayList;
 
 import java.util.Collections;
 import java.util.List;
+import org.junit.jupiter.params.ParameterizedTest;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
@@ -35,26 +36,24 @@ public class AddPartitionsToTxnRequestTest {
     private static short producerEpoch = 1;
     private static int throttleTimeMs = 10;
 
-    @Test
-    public void testConstructor() {
+    @ParameterizedTest
+    @ApiKeyVersionsSource(apiKey = ApiKeys.ADD_PARTITIONS_TO_TXN)
+    public void testConstructor(short version) {
         List<TopicPartition> partitions = new ArrayList<>();
         partitions.add(new TopicPartition("topic", 0));
         partitions.add(new TopicPartition("topic", 1));
 
         AddPartitionsToTxnRequest.Builder builder = new AddPartitionsToTxnRequest.Builder(transactionalId, producerId, producerEpoch, partitions);
+        AddPartitionsToTxnRequest request = builder.build(version);
 
-        for (short version : ApiKeys.ADD_PARTITIONS_TO_TXN.allVersions()) {
-            AddPartitionsToTxnRequest request = builder.build(version);
+        assertEquals(transactionalId, request.data().transactionalId());
+        assertEquals(producerId, request.data().producerId());
+        assertEquals(producerEpoch, request.data().producerEpoch());
+        assertEquals(partitions, request.partitions());
 
-            assertEquals(transactionalId, request.data().transactionalId());
-            assertEquals(producerId, request.data().producerId());
-            assertEquals(producerEpoch, request.data().producerEpoch());
-            assertEquals(partitions, request.partitions());
+        AddPartitionsToTxnResponse response = request.getErrorResponse(throttleTimeMs, Errors.UNKNOWN_TOPIC_OR_PARTITION.exception());
 
-            AddPartitionsToTxnResponse response = request.getErrorResponse(throttleTimeMs, Errors.UNKNOWN_TOPIC_OR_PARTITION.exception());
-
-            assertEquals(Collections.singletonMap(Errors.UNKNOWN_TOPIC_OR_PARTITION, 2), response.errorCounts());
-            assertEquals(throttleTimeMs, response.throttleTimeMs());
-        }
+        assertEquals(Collections.singletonMap(Errors.UNKNOWN_TOPIC_OR_PARTITION, 2), response.errorCounts());
+        assertEquals(throttleTimeMs, response.throttleTimeMs());
     }
 }
diff --git a/clients/src/test/java/org/apache/kafka/common/utils/annotation/ApiKeyVersionsProvider.java b/clients/src/test/java/org/apache/kafka/common/utils/annotation/ApiKeyVersionsProvider.java
new file mode 100644
index 0000000..2a1f6e4
--- /dev/null
+++ b/clients/src/test/java/org/apache/kafka/common/utils/annotation/ApiKeyVersionsProvider.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.utils.annotation;
+
+import java.util.stream.Stream;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.ArgumentsProvider;
+import org.junit.jupiter.params.support.AnnotationConsumer;
+
+public class ApiKeyVersionsProvider implements ArgumentsProvider, AnnotationConsumer<ApiKeyVersionsSource> {
+    private ApiKeys apiKey;
+
+    public void accept(ApiKeyVersionsSource source) {
+        apiKey = source.apiKey();
+    }
+
+    public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
+        return apiKey.allVersions().stream().map(Arguments::of);
+    }
+}
diff --git a/clients/src/test/java/org/apache/kafka/common/utils/annotation/ApiKeyVersionsSource.java b/clients/src/test/java/org/apache/kafka/common/utils/annotation/ApiKeyVersionsSource.java
new file mode 100644
index 0000000..9f169b3
--- /dev/null
+++ b/clients/src/test/java/org/apache/kafka/common/utils/annotation/ApiKeyVersionsSource.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.utils.annotation;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import org.apache.kafka.common.protocol.ApiKeys;
+import org.junit.jupiter.params.provider.ArgumentsSource;
+
+@Target({ElementType.METHOD})
+@Retention(RetentionPolicy.RUNTIME)
+@ArgumentsSource(ApiKeyVersionsProvider.class)
+public @interface ApiKeyVersionsSource {
+    ApiKeys apiKey();
+}