-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature/#41 ReductClient constructor was changed. ServerProperties ha…
…ve builder initialization
- Loading branch information
1 parent
83a6d41
commit e2fd979
Showing
5 changed files
with
90 additions
and
11 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
44 changes: 42 additions & 2 deletions
44
src/main/java/store/reduct/client/config/ServerProperties.java
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 |
---|---|---|
@@ -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; | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
src/test/java/store/reduct/client/ServerPropertiesTest.java
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,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"); | ||
} | ||
} |