-
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.
- Loading branch information
1 parent
f11378a
commit 1d07422
Showing
11 changed files
with
259 additions
and
9 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
60 changes: 60 additions & 0 deletions
60
src/main/java/rs/iggy/clients/blocking/tcp/SystemTcpClient.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,60 @@ | ||
package rs.iggy.clients.blocking.tcp; | ||
|
||
import io.netty.buffer.Unpooled; | ||
import rs.iggy.clients.blocking.SystemClient; | ||
import rs.iggy.system.ClientInfo; | ||
import rs.iggy.system.ClientInfoDetails; | ||
import rs.iggy.system.Stats; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import static rs.iggy.clients.blocking.tcp.BytesDeserializer.*; | ||
|
||
class SystemTcpClient implements SystemClient { | ||
private static final int PING_CODE = 1; | ||
private static final int GET_STATS_CODE = 10; | ||
private static final int GET_ME_CODE = 20; | ||
private static final int GET_CLIENT_CODE = 21; | ||
private static final int GET_CLIENTS_CODE = 22; | ||
|
||
private final TcpConnectionHandler connection; | ||
|
||
SystemTcpClient(TcpConnectionHandler connection) { | ||
this.connection = connection; | ||
} | ||
|
||
@Override | ||
public Stats getStats() { | ||
var response = connection.send(GET_STATS_CODE); | ||
return readStats(response); | ||
} | ||
|
||
@Override | ||
public ClientInfoDetails getMe() { | ||
var response = connection.send(GET_ME_CODE); | ||
return readClientInfoDetails(response); | ||
} | ||
|
||
@Override | ||
public ClientInfoDetails getClient(Long clientId) { | ||
var payload = Unpooled.buffer(4); | ||
payload.writeIntLE(clientId.intValue()); | ||
var response = connection.send(GET_CLIENT_CODE, payload); | ||
return readClientInfoDetails(response); | ||
} | ||
|
||
@Override | ||
public List<ClientInfo> getClients() { | ||
var response = connection.send(GET_CLIENTS_CODE); | ||
List<ClientInfo> clients = new ArrayList<>(); | ||
while (response.isReadable()) { | ||
clients.add(readClientInfo(response)); | ||
} | ||
return clients; | ||
} | ||
|
||
@Override | ||
public String ping() { | ||
connection.send(PING_CODE); | ||
return ""; | ||
} | ||
} |
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
27 changes: 27 additions & 0 deletions
27
src/test/java/rs/iggy/clients/blocking/SystemClientBaseTest.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,27 @@ | ||
package rs.iggy.clients.blocking; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public abstract class SystemClientBaseTest extends IntegrationTest { | ||
|
||
protected SystemClient systemClient; | ||
|
||
@BeforeEach | ||
void beforeEachBase() { | ||
systemClient = client.system(); | ||
|
||
login(); | ||
} | ||
|
||
@Test | ||
void shouldGetStats() { | ||
// when | ||
var stats = systemClient.getStats(); | ||
|
||
// then | ||
assertThat(stats).isNotNull(); | ||
} | ||
|
||
} |
13 changes: 13 additions & 0 deletions
13
src/test/java/rs/iggy/clients/blocking/http/SystemHttpClientTest.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,13 @@ | ||
package rs.iggy.clients.blocking.http; | ||
|
||
import rs.iggy.clients.blocking.IggyClient; | ||
import rs.iggy.clients.blocking.SystemClientBaseTest; | ||
|
||
class SystemHttpClientTest extends SystemClientBaseTest { | ||
|
||
@Override | ||
protected IggyClient getClient() { | ||
return HttpClientFactory.create(iggyServer); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
src/test/java/rs/iggy/clients/blocking/tcp/SystemTcpClientTest.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 rs.iggy.clients.blocking.tcp; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import rs.iggy.clients.blocking.IggyClient; | ||
import rs.iggy.clients.blocking.SystemClientBaseTest; | ||
import rs.iggy.system.ClientInfo; | ||
import rs.iggy.system.ClientInfoDetails; | ||
import java.util.List; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class SystemTcpClientTest extends SystemClientBaseTest { | ||
|
||
@Override | ||
protected IggyClient getClient() { | ||
return TcpClientFactory.create(iggyServer); | ||
} | ||
|
||
@Test | ||
void shouldGetMeAndClient() { | ||
// when | ||
ClientInfoDetails me = systemClient.getMe(); | ||
|
||
// then | ||
assertThat(me).isNotNull(); | ||
|
||
// when | ||
var clientInfo = systemClient.getClient(me.clientId()); | ||
|
||
// then | ||
assertThat(clientInfo).isNotNull(); | ||
} | ||
|
||
@Test | ||
void shouldGetClients() { | ||
// when | ||
List<ClientInfo> clients = systemClient.getClients(); | ||
|
||
// then | ||
assertThat(clients).isNotNull(); | ||
assertThat(clients.size()).isEqualTo(1); | ||
} | ||
} |