Skip to content

Commit

Permalink
Cleanup: extract Lists util class from duplicate code (#382)
Browse files Browse the repository at this point in the history
* 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
sleberknight authored Oct 10, 2024
1 parent b960e28 commit fb6adc2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.consul;

import static java.util.Objects.isNull;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
Expand All @@ -20,14 +19,14 @@ void shouldGetDatacenters() {
@Test
void shouldGetNodes() {
List<Coordinate> nodes = client.coordinateClient().getNodes();
assertThat(isNull(nodes)).isFalse();
assertThat(nodes).isNotNull();
}

@Test
void shouldGetNodesForDatacenter() {
String datacenter = client.coordinateClient().getDatacenters().get(0).getDatacenter();

List<Coordinate> nodes = client.coordinateClient().getNodes(datacenter);
assertThat(isNull(nodes)).isFalse();
assertThat(nodes).isNotNull();
}
}
5 changes: 1 addition & 4 deletions src/main/java/org/kiwiproject/consul/KeyValueClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static com.google.common.base.Preconditions.checkArgument;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static org.kiwiproject.consul.util.Lists.firstValueOrEmpty;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -172,10 +173,6 @@ public void onFailure(Throwable throwable) {
http.extractConsulResponse(api.getValue(Strings.trimLeadingSlash(key), queryOptions.toQuery()), wrapper, NOT_FOUND_404);
}

private static Optional<Value> firstValueOrEmpty(List<Value> values) {
return nonNull(values) && !values.isEmpty() ? Optional.of(values.get(0)) : Optional.empty();
}

private static <T> ConsulResponse<T> newConsulResponse(T value, ConsulResponse<List<Value>> response) {
return new ConsulResponse<>(value,
response.getLastContact(),
Expand Down
8 changes: 3 additions & 5 deletions src/main/java/org/kiwiproject/consul/SessionClient.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.kiwiproject.consul;

import static java.util.Objects.isNull;
import static org.kiwiproject.consul.ConsulClients.dcQuery;
import static org.kiwiproject.consul.util.Lists.firstValueOrEmpty;

import org.kiwiproject.consul.config.ClientConfig;
import org.kiwiproject.consul.model.session.Session;
Expand Down Expand Up @@ -81,8 +81,7 @@ public Optional<SessionInfo> renewSession(final String dc, final String sessionI
List<SessionInfo> sessionInfo = http.extract(api.renewSession(sessionId,
Map.of(), dcQuery(dc)));

return isNull(sessionInfo) || sessionInfo.isEmpty() ? Optional.empty() :
Optional.of(sessionInfo.get(0));
return firstValueOrEmpty(sessionInfo);
}

/**
Expand Down Expand Up @@ -132,8 +131,7 @@ public Optional<SessionInfo> getSessionInfo(final String sessionId) {
public Optional<SessionInfo> getSessionInfo(final String sessionId, final String dc) {
List<SessionInfo> sessionInfo = http.extract(api.getSessionInfo(sessionId, dcQuery(dc)));

return isNull(sessionInfo) || sessionInfo.isEmpty() ? Optional.empty() :
Optional.of(sessionInfo.get(0));
return firstValueOrEmpty(sessionInfo);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/kiwiproject/consul/util/Lists.java
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();
}
}
56 changes: 56 additions & 0 deletions src/test/java/org/kiwiproject/consul/util/ListsTest.java
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();
}
}
}

0 comments on commit fb6adc2

Please sign in to comment.