Skip to content

Commit

Permalink
feature/#41 ReductClient constructor was changed. ServerProperties ha…
Browse files Browse the repository at this point in the history
…ve builder initialization
  • Loading branch information
Rumpelshtinskiy committed Sep 27, 2024
1 parent 83a6d41 commit 16957c8
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- EntryClient.writeRecord(Bucket bucket, Entry<?> body) method to add a record to a
bucket [PR-25](https://github.com/reductstore/reduct-java/issues/25)
- EntryClient.getRecord(Bucket bucket, Entry<?> body) method to get a record from a
bucket [PR-25](https://github.com/reductstore/reduct-java/issues/27)

bucket [PR-27](https://github.com/reductstore/reduct-java/issues/27)
- Improve ReductClient initialization. [PR-41](https://github.com/reductstore/reduct-java/issues/41)
### Infrastructure:

- Added GitHub Actions for CI/CD [PR-35](https://github.com/reductstore/reduct-java/pull/35)
7 changes: 2 additions & 5 deletions src/main/java/store/reduct/client/ReductClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import store.reduct.client.config.ServerProperties;
Expand All @@ -35,14 +34,12 @@ public class ReductClient {
private static final String REDUCT_ERROR_HEADER = "x-reduct-error";
private final ServerProperties serverProperties;
private final HttpClient httpClient;
@Getter(AccessLevel.PRIVATE)
private final String token;
private final ObjectMapper objectMapper = new ObjectMapper();

public <T> HttpResponse<T> send(HttpRequest.Builder builder, HttpResponse.BodyHandler<T> bodyHandler) {
try {
if (isNotBlank(token)) {
builder.headers("Authorization", "Bearer %s".formatted(token));
if (isNotBlank(serverProperties.getApiToken())) {
builder.headers("Authorization", "Bearer %s".formatted(serverProperties.getApiToken()));
}
HttpResponse<T> httpResponse = httpClient.send(builder.build(), bodyHandler);
if (httpResponse.statusCode() == 200) {
Expand Down
44 changes: 42 additions & 2 deletions src/main/java/store/reduct/client/config/ServerProperties.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,51 @@
package store.reduct.client.config;

public record ServerProperties(boolean isSsl, String host, int port) {
import java.util.Objects;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import store.reduct.common.exception.ReductException;
import store.reduct.utils.Strings;

@AllArgsConstructor
@Builder
public class ServerProperties {
private Boolean isSsl;
private String host;
private Integer port;
private String url;
@Getter
private String apiToken;

/**
* Returns a base url for the server. Initialize isSsl, host, port fields before
* use the method.
*
* @return The base url for the server
*/
public String getBaseUrl() {
return (isSsl ? "https://" : "http://") + host + ":" + port;
if (Strings.isNotBlank(url)) {
return url;
}
return (isSsl() ? "https://" : "http://") + getHost() + ":" + getPort();
}
private Boolean isSsl() {
if (Objects.isNull(isSsl)) {
throw new ReductException("Initialize isSsl field");
}
return isSsl;
}

private String getHost() {
if (Strings.isBlank(host)) {
throw new ReductException("Initialize host field first");
}
return host;
}
private Integer getPort() {
if (Objects.isNull(port)) {
throw new ReductException("Initialize port field first");
}
return port;
}
}
6 changes: 3 additions & 3 deletions src/test/java/store/reduct/client/QueriesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import org.junit.jupiter.api.Test;
import store.reduct.utils.http.Queries;

public class QueriesTest {
class QueriesTest {
@Test
public void test1() {
void test1() {
// Init
Queries q = new Queries("name1", 4L).add("key2", "new").add("ttl", 60);
// Act
Expand All @@ -16,7 +16,7 @@ public void test1() {
assertThat("?name1=4&key2=new&ttl=60").isEqualTo(q.buildQuery());
}
@Test
public void test2() {
void test2() {
// Init
Queries q = new Queries("key1", 4L);
// Act
Expand Down
42 changes: 42 additions & 0 deletions src/test/java/store/reduct/client/ServerPropertiesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package store.reduct.client;

import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import store.reduct.client.config.ServerProperties;
import store.reduct.common.exception.ReductException;

class ServerPropertiesTest {
@Test
void test1() {
// Init
ServerProperties sp = ServerProperties.builder().build();
// Act
// Assert
assertThatThrownBy(sp::getBaseUrl).isInstanceOf(ReductException.class).hasMessage("Initialize isSsl field");
}

@ParameterizedTest
@CsvSource({"true", "false"})
void test2(Boolean isSsl) {
// Init
ServerProperties sp = ServerProperties.builder().isSsl(isSsl).build();
// Act
// Assert
assertThatThrownBy(sp::getBaseUrl).isInstanceOf(ReductException.class)
.hasMessage("Initialize host field first");
}

@ParameterizedTest
@CsvSource({"true, host", "false, host"})
void test3(Boolean isSsl, String host) {
// Init
ServerProperties sp = ServerProperties.builder().isSsl(isSsl).host(host).build();
// Act
// Assert
assertThatThrownBy(sp::getBaseUrl).isInstanceOf(ReductException.class)
.hasMessage("Initialize port field first");
}
}

0 comments on commit 16957c8

Please sign in to comment.