Browse Source

add first Feign Client with registrate method and test

feat-loginClient
Steffen Nitschke 4 years ago
parent
commit
2cd85077f3
  1. 6
      fh.fd.ci.client/build.gradle
  2. 11
      fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java
  3. 29
      fh.fd.ci.client/src/main/java/de/fd/fh/network/FeignClients.java
  4. 46
      fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java

6
fh.fd.ci.client/build.gradle

@ -7,6 +7,12 @@ mainClassName = 'de.fd.fh.ClientApp'
dependencies {
implementation project(':fh.fd.ci.shared')
implementation 'io.github.openfeign:feign-okhttp:11.0'
implementation 'io.github.openfeign:feign-gson:11.0'
implementation 'io.github.openfeign:feign-slf4j:11.0'
testImplementation 'io.github.openfeign:feign-mock:11.0'
}
javafx {

11
fh.fd.ci.client/src/main/java/de/fd/fh/network/AccessClient.java

@ -0,0 +1,11 @@
package de.fd.fh.network;
import de.fd.fh.shared.network.messages.RegistrateRequest;
import feign.RequestLine;
import feign.Response;
public interface AccessClient
{
@RequestLine("POST /accounts/registrate")
Response registrate(RegistrateRequest request);
}

29
fh.fd.ci.client/src/main/java/de/fd/fh/network/FeignClients.java

@ -0,0 +1,29 @@
package de.fd.fh.network;
import feign.Feign;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.okhttp.OkHttpClient;
import lombok.Getter;
@Getter
public class FeignClients
{
private static final String BASE_URL = "http://localhost:4567";
private AccessClient accessClient;
public FeignClients()
{
accessClient = buildClient(AccessClient.class);
}
<T> T buildClient(final Class<T> client)
{
return Feign.builder()
.client(new OkHttpClient())
.encoder(new GsonEncoder())
.decoder(new GsonDecoder())
.target(client, BASE_URL);
}
}

46
fh.fd.ci.client/src/test/java/de/fd/fh/network/AccessClientTest.java

@ -0,0 +1,46 @@
package de.fd.fh.network;
import de.fd.fh.shared.network.messages.RegistrateRequest;
import feign.Feign;
import feign.Response;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.mock.HttpMethod;
import feign.mock.MockClient;
import feign.mock.MockTarget;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class AccessClientTest
{
private AccessClient client;
private MockClient mockClient;
@BeforeEach
void before()
{
mockClient = new MockClient();
client = Feign.builder()
.decoder(new GsonDecoder())
.encoder(new GsonEncoder())
.client(mockClient)
.target(new MockTarget<>(AccessClient.class));
}
@Test
void testRegistrate()
{
mockClient.ok(HttpMethod.POST, "/accounts/registrate");
final RegistrateRequest request = RegistrateRequest.of("TestName", "TestPassword");
final Response result = client.registrate(request);
assertEquals(200, result.status(), "Should return status code 200.");
mockClient.verifyStatus();
}
}
Loading…
Cancel
Save