Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup: extract Lists util class from duplicate code #382

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
}