|
@ -0,0 +1,110 @@ |
|
|
|
|
|
package de.fd.fh.network; |
|
|
|
|
|
|
|
|
|
|
|
import de.fd.fh.shared.Utils; |
|
|
|
|
|
import de.fd.fh.shared.network.messages.ChangeUserRequest; |
|
|
|
|
|
import de.fd.fh.shared.network.messages.PlayerResponse; |
|
|
|
|
|
import de.fd.fh.shared.network.messages.UserRequest; |
|
|
|
|
|
import feign.Feign; |
|
|
|
|
|
import feign.Request; |
|
|
|
|
|
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.*; |
|
|
|
|
|
|
|
|
|
|
|
class UserClientTest |
|
|
|
|
|
{ |
|
|
|
|
|
private UserClient client; |
|
|
|
|
|
private MockClient mockClient; |
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
|
|
void before() |
|
|
|
|
|
{ |
|
|
|
|
|
mockClient = new MockClient(); |
|
|
|
|
|
|
|
|
|
|
|
client = Feign.builder() |
|
|
|
|
|
.decoder(new GsonDecoder()) |
|
|
|
|
|
.encoder(new GsonEncoder()) |
|
|
|
|
|
.client(mockClient) |
|
|
|
|
|
.target(new MockTarget<>(UserClient.class)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void updatePlayer() |
|
|
|
|
|
{ |
|
|
|
|
|
mockClient.ok(HttpMethod.POST, "/users", |
|
|
|
|
|
"{\n" + |
|
|
|
|
|
" \"id\": {\n" + |
|
|
|
|
|
" \"identifier\":\"12345\"\n" + |
|
|
|
|
|
" },\n" + |
|
|
|
|
|
" \"name\":\"TestName\"\n" + |
|
|
|
|
|
"}"); |
|
|
|
|
|
|
|
|
|
|
|
final ChangeUserRequest request = new ChangeUserRequest( |
|
|
|
|
|
"TestName", |
|
|
|
|
|
"TestPassword"); |
|
|
|
|
|
|
|
|
|
|
|
final PlayerResponse response = client.updatePlayer("TestAuthHeader", request); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(response); |
|
|
|
|
|
assertEquals("TestName", response.getName()); |
|
|
|
|
|
assertEquals("12345", response.getId().getIdentifier()); |
|
|
|
|
|
|
|
|
|
|
|
final Request sended = mockClient.verifyOne(HttpMethod.POST, "/users"); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(sended.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().isPresent()); |
|
|
|
|
|
assertEquals("Bearer TestAuthHeader", |
|
|
|
|
|
sended.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().get()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void getShortPlayerTest() |
|
|
|
|
|
{ |
|
|
|
|
|
mockClient.ok(HttpMethod.GET, "/users/12345", |
|
|
|
|
|
"{\n" + |
|
|
|
|
|
" \"id\":\"12345\",\n" + |
|
|
|
|
|
" \"name\":\"TestName\"\n" + |
|
|
|
|
|
"}"); |
|
|
|
|
|
|
|
|
|
|
|
final UserRequest response = client.getShortPlayer("TestAuthHeader", "12345"); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(response); |
|
|
|
|
|
assertEquals("TestName", response.getName()); |
|
|
|
|
|
assertEquals("12345", response.getId()); |
|
|
|
|
|
|
|
|
|
|
|
final Request sended = mockClient.verifyOne(HttpMethod.GET, "/users/12345"); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(sended.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().isPresent()); |
|
|
|
|
|
assertEquals("Bearer TestAuthHeader", |
|
|
|
|
|
sended.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().get()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void getCurrentPlayerTest() |
|
|
|
|
|
{ |
|
|
|
|
|
mockClient.ok(HttpMethod.GET, "/users", |
|
|
|
|
|
"{\n" + |
|
|
|
|
|
" \"id\":\n" + |
|
|
|
|
|
" {\n" + |
|
|
|
|
|
" \"identifier\":\"12345\"\n" + |
|
|
|
|
|
" },\n" + |
|
|
|
|
|
" \"name\":\"TestName\"\n" + |
|
|
|
|
|
"}"); |
|
|
|
|
|
|
|
|
|
|
|
final PlayerResponse response = client.getCurrentPlayer("TestAuthHeader"); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(response); |
|
|
|
|
|
assertEquals("TestName", response.getName()); |
|
|
|
|
|
assertEquals("12345", response.getId().getIdentifier()); |
|
|
|
|
|
|
|
|
|
|
|
final Request sended = mockClient.verifyOne(HttpMethod.GET, "/users"); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(sended.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().isPresent()); |
|
|
|
|
|
assertEquals("Bearer TestAuthHeader", |
|
|
|
|
|
sended.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().get()); |
|
|
|
|
|
} |
|
|
|
|
|
} |