-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup: extract Lists util class from duplicate code (#382)
* Add Lists class with firstValueOrEmpty and isNullOrEmpty * Update KeyValueClient and SessionClient to use Lists class * Change awkward assertions in CoordinateClientITest to use isNotNull
- Loading branch information
1 parent
b960e28
commit fb6adc2
Showing
5 changed files
with
83 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.kiwiproject.consul.util; | ||
|
||
import static java.util.Objects.isNull; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class Lists { | ||
|
||
private Lists() { | ||
// utility class | ||
} | ||
|
||
public static <T> Optional<T> firstValueOrEmpty(List<T> list) { | ||
return isNullOrEmpty(list) ? Optional.empty() : Optional.of(list.get(0)); | ||
} | ||
|
||
public static <T> boolean isNullOrEmpty(List<T> list) { | ||
return isNull(list) || list.isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.kiwiproject.consul.util; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.NullAndEmptySource; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@DisplayName("Lists") | ||
class ListsTest { | ||
|
||
@Nested | ||
class FirstValueOrEmpty { | ||
|
||
@ParameterizedTest | ||
@NullAndEmptySource | ||
void shouldReturnEmptyOptional_WhenListIsNullOrEmpty(List<String> list) { | ||
assertThat(Lists.firstValueOrEmpty(list)).isEmpty(); | ||
} | ||
|
||
@Test | ||
void shouldReturnOnlyValue_InSingleValuedList() { | ||
var list = List.of(42); | ||
assertThat(Lists.firstValueOrEmpty(list)).contains(42); | ||
} | ||
|
||
@Test | ||
void shouldReturnFirstValue_InMultiValuedList() { | ||
var list = List.of(10, 9, 8, 7); | ||
assertThat(Lists.firstValueOrEmpty(list)).contains(10); | ||
} | ||
} | ||
|
||
@Nested | ||
class IsNullOrEmpty { | ||
|
||
@ParameterizedTest | ||
@NullAndEmptySource | ||
void shouldReturnTrue_WhenListIsNullOrEmpty(List<String> list) { | ||
assertThat(Lists.isNullOrEmpty(list)).isTrue(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(ints = { 1, 2, 3 }) | ||
void shouldReturnFalse_WhenListIsNotEmpty(int size) { | ||
List<Integer> list = Collections.nCopies(size, 42); | ||
assertThat(Lists.isNullOrEmpty(list)).isFalse(); | ||
} | ||
} | ||
} |