Skip to content

Commit

Permalink
MINOR: Introduce ApiKeyVersionsSource annotation for `Parameterized…
Browse files Browse the repository at this point in the history
…Test` (apache#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 <showuon@gmail.com>, Jason Gustafson <jason@confluent.io>
  • Loading branch information
dajac committed Nov 11, 2021
1 parent 214b59b commit 6f62485
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 14 deletions.
Expand Up @@ -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;

Expand All @@ -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());
}
}
@@ -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);
}
}
@@ -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();
}

0 comments on commit 6f62485

Please sign in to comment.