Skip to content

Commit a3328c8

Browse files
feat(java): Increase test coverage (#2939)
Closes #2938
1 parent 658eb89 commit a3328c8

38 files changed

+2078
-12
lines changed

foreign/java/java-sdk/src/main/java/org/apache/iggy/exception/IggyException.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,4 @@ protected IggyException(String message) {
4646
protected IggyException(String message, Throwable cause) {
4747
super(message, cause);
4848
}
49-
50-
/**
51-
* Constructs a new IggyException with the specified cause.
52-
*
53-
* @param cause the cause of the exception
54-
*/
55-
protected IggyException(Throwable cause) {
56-
super(cause);
57-
}
5849
}

foreign/java/java-sdk/src/main/java/org/apache/iggy/system/ClientInfoDetails.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
package org.apache.iggy.system;
2121

22-
import java.util.ArrayList;
2322
import java.util.List;
2423
import java.util.Optional;
2524

@@ -30,7 +29,7 @@ public record ClientInfoDetails(
3029
String transport,
3130
Long consumerGroupsCount,
3231
List<ConsumerGroupInfo> consumerGroups) {
33-
public ClientInfoDetails(ClientInfo clientInfo, ArrayList<ConsumerGroupInfo> consumerGroups) {
32+
public ClientInfoDetails(ClientInfo clientInfo, List<ConsumerGroupInfo> consumerGroups) {
3433
this(
3534
clientInfo.clientId(),
3635
clientInfo.userId(),

foreign/java/java-sdk/src/main/java/org/apache/iggy/topic/CompressionAlgorithm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public enum CompressionAlgorithm {
3131
this.code = code;
3232
}
3333

34-
public static CompressionAlgorithm fromCode(byte code) {
34+
public static CompressionAlgorithm fromCode(int code) {
3535
for (CompressionAlgorithm algorithm : values()) {
3636
if (algorithm.code == code) {
3737
return algorithm;
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iggy.config;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.time.Duration;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
class RetryPolicyTest {
29+
@Test
30+
void exponentialBackoffWithNoArgsReturnsExpectedRetryPolicy() {
31+
var retryPolicy = RetryPolicy.exponentialBackoff();
32+
33+
assertThat(retryPolicy.getMaxRetries()).isEqualTo(3);
34+
assertThat(retryPolicy.getInitialDelay()).isEqualTo(Duration.ofMillis(100));
35+
assertThat(retryPolicy.getMaxDelay()).isEqualTo(Duration.ofSeconds(5));
36+
assertThat(retryPolicy.getMultiplier()).isEqualTo(2.0);
37+
}
38+
39+
@Test
40+
void exponentialBackoffWithAllArgsReturnsExpectedRetryPolicy() {
41+
var retryPolicy = RetryPolicy.exponentialBackoff(30, Duration.ofMillis(50), Duration.ofMinutes(60), 1.5);
42+
43+
assertThat(retryPolicy.getMaxRetries()).isEqualTo(30);
44+
assertThat(retryPolicy.getInitialDelay()).isEqualTo(Duration.ofMillis(50));
45+
assertThat(retryPolicy.getMaxDelay()).isEqualTo(Duration.ofMinutes(60));
46+
assertThat(retryPolicy.getMultiplier()).isEqualTo(1.5);
47+
}
48+
49+
@Test
50+
void fixedDelayReturnsExpectedRetryPolicy() {
51+
var retryPolicy = RetryPolicy.fixedDelay(50, Duration.ofMillis(500));
52+
53+
assertThat(retryPolicy.getMaxRetries()).isEqualTo(50);
54+
assertThat(retryPolicy.getInitialDelay()).isEqualTo(Duration.ofMillis(500));
55+
assertThat(retryPolicy.getMaxDelay()).isEqualTo(Duration.ofMillis(500));
56+
assertThat(retryPolicy.getMultiplier()).isEqualTo(1.0);
57+
}
58+
59+
@Test
60+
void noRetryReturnsExpectedRetryPolicy() {
61+
var retryPolicy = RetryPolicy.noRetry();
62+
63+
assertThat(retryPolicy.getMaxRetries()).isEqualTo(0);
64+
assertThat(retryPolicy.getInitialDelay()).isEqualTo(Duration.ZERO);
65+
assertThat(retryPolicy.getMaxDelay()).isEqualTo(Duration.ZERO);
66+
assertThat(retryPolicy.getMultiplier()).isEqualTo(1.0);
67+
}
68+
69+
@Test
70+
void getMaxRetriesReturnsMaxRetries() {
71+
var retryPolicy = RetryPolicy.exponentialBackoff(2, Duration.ofMillis(200), Duration.ofMillis(1000), 1.0);
72+
73+
assertThat(retryPolicy.getMaxRetries()).isEqualTo(2);
74+
}
75+
76+
@Test
77+
void getInitialDelayReturnsInitialDelay() {
78+
var retryPolicy = RetryPolicy.exponentialBackoff(2, Duration.ofMillis(200), Duration.ofMillis(1000), 1.0);
79+
80+
assertThat(retryPolicy.getInitialDelay()).isEqualTo(Duration.ofMillis(200));
81+
}
82+
83+
@Test
84+
void getMaxDelayReturnsMaxDelay() {
85+
var retryPolicy = RetryPolicy.exponentialBackoff(2, Duration.ofMillis(200), Duration.ofMillis(1000), 1.0);
86+
87+
assertThat(retryPolicy.getMaxDelay()).isEqualTo(Duration.ofMillis(1000));
88+
}
89+
90+
@Test
91+
void getMultiplierReturnsMultiplier() {
92+
var retryPolicy = RetryPolicy.exponentialBackoff(2, Duration.ofMillis(200), Duration.ofMillis(1000), 1.0);
93+
94+
assertThat(retryPolicy.getMultiplier()).isEqualTo(1.0);
95+
}
96+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iggy.consumergroup;
21+
22+
import org.junit.jupiter.api.Test;
23+
24+
import java.util.List;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
class ConsumerGroupDetailsTest {
29+
@Test
30+
void constructorWithConsumerGroupCreatesExpectedConsumerGroupDetails() {
31+
var consumerGroup = new ConsumerGroup(2L, "group", 3L, 1L);
32+
var members = List.of(new ConsumerGroupMember(1L, 3L, List.of()));
33+
34+
var consumerGroupDetails = new ConsumerGroupDetails(consumerGroup, members);
35+
36+
assertThat(consumerGroupDetails.id()).isEqualTo(2L);
37+
assertThat(consumerGroupDetails.name()).isEqualTo("group");
38+
assertThat(consumerGroupDetails.partitionsCount()).isEqualTo(3);
39+
assertThat(consumerGroupDetails.membersCount()).isEqualTo(1L);
40+
assertThat(consumerGroupDetails.members()).isEqualTo(members);
41+
}
42+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iggy.consumergroup;
21+
22+
import org.apache.iggy.consumergroup.Consumer.Kind;
23+
import org.apache.iggy.identifier.ConsumerId;
24+
import org.junit.jupiter.api.Nested;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.CsvSource;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
class ConsumerTest {
32+
@Test
33+
void ofWithNumericIdCreatesExpectedConsumerWithConsumerKind() {
34+
var consumer = Consumer.of(123L);
35+
36+
assertThat(consumer.id().getId()).isEqualTo(123L);
37+
assertThat(consumer.kind()).isEqualTo(Kind.Consumer);
38+
}
39+
40+
@Test
41+
void ofWithConsumerIdCreatesExpectedConsumerWithConsumerKind() {
42+
var consumer = Consumer.of(ConsumerId.of(321L));
43+
44+
assertThat(consumer.id().getId()).isEqualTo(321L);
45+
assertThat(consumer.kind()).isEqualTo(Consumer.Kind.Consumer);
46+
}
47+
48+
@Test
49+
void groupWithNumericIdCreatesExpectedConsumerWithConsumerGroupKind() {
50+
var consumer = Consumer.group(456L);
51+
52+
assertThat(consumer.id().getId()).isEqualTo(456L);
53+
assertThat(consumer.kind()).isEqualTo(Consumer.Kind.ConsumerGroup);
54+
}
55+
56+
@Test
57+
void groupWithConsumerIdCreatesExpectedConsumerWithConsumerGroupKind() {
58+
var consumer = Consumer.group(ConsumerId.of(654L));
59+
60+
assertThat(consumer.id().getId()).isEqualTo(654L);
61+
assertThat(consumer.kind()).isEqualTo(Consumer.Kind.ConsumerGroup);
62+
}
63+
64+
@Nested
65+
class ConsumerKindTest {
66+
@ParameterizedTest
67+
@CsvSource({"Consumer, 1", "ConsumerGroup, 2"})
68+
void asCodeReturnsExpectedCode(Consumer.Kind kind, int expectedCode) {
69+
var code = kind.asCode();
70+
71+
assertThat(code).isEqualTo(expectedCode);
72+
}
73+
}
74+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iggy.exception;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.EnumSource;
25+
import org.junit.jupiter.params.provider.EnumSource.Mode;
26+
27+
import java.util.Optional;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
class IggyAuthenticationExceptionTest {
32+
@Test
33+
void constructorCreatesExpectedIggyAuthenticationException() {
34+
var exception = new IggyAuthenticationException(
35+
IggyErrorCode.UNAUTHENTICATED, 40, "unauthenticated", Optional.of("foo"), Optional.of("bar"));
36+
37+
assertThat(exception).isInstanceOf(IggyAuthenticationException.class);
38+
assertThat(exception.getErrorCode()).isEqualTo(IggyErrorCode.UNAUTHENTICATED);
39+
assertThat(exception.getRawErrorCode()).isEqualTo(40);
40+
assertThat(exception.getReason()).isEqualTo("unauthenticated");
41+
assertThat(exception.getField()).isEqualTo(Optional.of("foo"));
42+
assertThat(exception.getErrorId()).isEqualTo(Optional.of("bar"));
43+
}
44+
45+
@ParameterizedTest
46+
@EnumSource(
47+
value = IggyErrorCode.class,
48+
names = {
49+
"UNAUTHENTICATED",
50+
"INVALID_CREDENTIALS",
51+
"INVALID_USERNAME",
52+
"INVALID_PASSWORD",
53+
"INVALID_PAT_TOKEN",
54+
"PASSWORD_DOES_NOT_MATCH",
55+
"PASSWORD_HASH_INTERNAL_ERROR"
56+
})
57+
void matchesReturnsTrueForAuthenticationRelatedCodes(IggyErrorCode code) {
58+
assertThat(IggyAuthenticationException.matches(code)).isTrue();
59+
}
60+
61+
@ParameterizedTest
62+
@EnumSource(
63+
value = IggyErrorCode.class,
64+
names = {
65+
"UNAUTHENTICATED",
66+
"INVALID_CREDENTIALS",
67+
"INVALID_USERNAME",
68+
"INVALID_PASSWORD",
69+
"INVALID_PAT_TOKEN",
70+
"PASSWORD_DOES_NOT_MATCH",
71+
"PASSWORD_HASH_INTERNAL_ERROR"
72+
},
73+
mode = Mode.EXCLUDE)
74+
void matchesReturnsFalseForNonAuthenticationRelatedCodes(IggyErrorCode code) {
75+
assertThat(IggyAuthenticationException.matches(code)).isFalse();
76+
}
77+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.apache.iggy.exception;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.EnumSource;
25+
import org.junit.jupiter.params.provider.EnumSource.Mode;
26+
27+
import java.util.Optional;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
class IggyAuthorizationExceptionTest {
32+
@Test
33+
void constructorCreatesExpectedIggyAuthorizationException() {
34+
var exception = new IggyAuthorizationException(
35+
IggyErrorCode.UNAUTHORIZED, 41, "unauthorized", Optional.of("bar"), Optional.of("foo"));
36+
37+
assertThat(exception).isInstanceOf(IggyAuthorizationException.class);
38+
assertThat(exception.getErrorCode()).isEqualTo(IggyErrorCode.UNAUTHORIZED);
39+
assertThat(exception.getRawErrorCode()).isEqualTo(41);
40+
assertThat(exception.getReason()).isEqualTo("unauthorized");
41+
assertThat(exception.getField()).isEqualTo(Optional.of("bar"));
42+
assertThat(exception.getErrorId()).isEqualTo(Optional.of("foo"));
43+
}
44+
45+
@ParameterizedTest
46+
@EnumSource(
47+
value = IggyErrorCode.class,
48+
names = {"UNAUTHORIZED"})
49+
void matchesReturnsTrueForAuthorizationRelatedCodes(IggyErrorCode code) {
50+
assertThat(IggyAuthorizationException.matches(code)).isTrue();
51+
}
52+
53+
@ParameterizedTest
54+
@EnumSource(
55+
value = IggyErrorCode.class,
56+
names = {"UNAUTHORIZED"},
57+
mode = Mode.EXCLUDE)
58+
void matchesReturnsFalseForNonAuthorizationRelatedCodes(IggyErrorCode code) {
59+
assertThat(IggyAuthorizationException.matches(code)).isFalse();
60+
}
61+
}

0 commit comments

Comments
 (0)