|
@ -0,0 +1,96 @@ |
|
|
|
|
|
package de.fd.fh.network; |
|
|
|
|
|
|
|
|
|
|
|
import de.fd.fh.shared.Utils; |
|
|
|
|
|
import de.fd.fh.shared.network.messages.LoginResponse; |
|
|
|
|
|
import de.fd.fh.shared.network.messages.RegistrateRequest; |
|
|
|
|
|
import feign.Feign; |
|
|
|
|
|
import feign.Request; |
|
|
|
|
|
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.*; |
|
|
|
|
|
|
|
|
|
|
|
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(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testLogin() |
|
|
|
|
|
{ |
|
|
|
|
|
mockClient |
|
|
|
|
|
.ok(HttpMethod.POST, "/accounts/login", |
|
|
|
|
|
"{\n" + |
|
|
|
|
|
" \"name\":\"testName\",\n" + |
|
|
|
|
|
" \"userId\": \"12345\",\n" + |
|
|
|
|
|
" \"token\": \"testToken\"\n" + |
|
|
|
|
|
"}" |
|
|
|
|
|
); |
|
|
|
|
|
final LoginResponse result = client.login("testToken"); |
|
|
|
|
|
|
|
|
|
|
|
final Request request = mockClient.verifyOne(HttpMethod.POST, "/accounts/login"); |
|
|
|
|
|
|
|
|
|
|
|
assertNotNull(result); |
|
|
|
|
|
assertEquals("testName", result.getName()); |
|
|
|
|
|
assertEquals("12345", result.getUserId()); |
|
|
|
|
|
assertEquals("testToken", result.getToken()); |
|
|
|
|
|
|
|
|
|
|
|
assertTrue(request.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().isPresent(), |
|
|
|
|
|
"Should have auth header"); |
|
|
|
|
|
final String authHeader = request.headers().get(Utils.AUTHENTICATION_HEADER).stream().findFirst().get(); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals("Basic testToken", authHeader, "Should use given token."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testLogout() |
|
|
|
|
|
{ |
|
|
|
|
|
mockClient.ok(HttpMethod.POST, "/accounts/logout"); |
|
|
|
|
|
|
|
|
|
|
|
final Response result = client.logout("testToken"); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(200, result.status(), "Should return status code 200."); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
void testDeletePlayer() |
|
|
|
|
|
{ |
|
|
|
|
|
mockClient.ok(HttpMethod.DELETE, "/accounts/12345"); |
|
|
|
|
|
|
|
|
|
|
|
final Response result = client.deletePlayer("12345"); |
|
|
|
|
|
|
|
|
|
|
|
assertEquals(200, result.status(), "Should return status code 200."); |
|
|
|
|
|
} |
|
|
|
|
|
} |