* #1448 Create AuthMePlayer to get player data from API with * #1448 Add tests for new API method & AuthMePlayer * #1448 Create AuthMePlayer to get player data from API with - Use Optional for all values that may be null * #1448 Add comment that AuthMePlayer data does not update itself
This commit is contained in:
@@ -28,15 +28,16 @@ import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static fr.xephi.authme.IsEqualByReflectionMatcher.hasEqualValuesOnAllFields;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.nullValue;
|
||||
import static org.hamcrest.Matchers.sameInstance;
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
@@ -512,6 +513,36 @@ public class AuthMeApiTest {
|
||||
assertThat(countryName, equalTo("Syldavia"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnAuthMePlayerInfo() {
|
||||
// given
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name("bobb")
|
||||
.realName("Bobb")
|
||||
.registrationDate(1433166082000L)
|
||||
.build();
|
||||
given(dataSource.getAuth("bobb")).willReturn(auth);
|
||||
|
||||
// when
|
||||
Optional<AuthMePlayer> result = api.getPlayerInfo("bobb");
|
||||
|
||||
// then
|
||||
AuthMePlayer playerInfo = result.get();
|
||||
assertThat(playerInfo.getName(), equalTo("Bobb"));
|
||||
assertThat(playerInfo.getRegistrationDate(), equalTo(Instant.ofEpochMilli(1433166082000L)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldReturnNullForNonExistentAuth() {
|
||||
// given / when
|
||||
Optional<AuthMePlayer> result = api.getPlayerInfo("doesNotExist");
|
||||
|
||||
// then
|
||||
assertThat(result.isPresent(), equalTo(false));
|
||||
verify(playerCache).getAuth("doesNotExist");
|
||||
verify(dataSource).getAuth("doesNotExist");
|
||||
}
|
||||
|
||||
private static Player mockPlayerWithName(String name) {
|
||||
Player player = mock(Player.class);
|
||||
given(player.getName()).willReturn(name);
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package fr.xephi.authme.api.v3;
|
||||
|
||||
import fr.xephi.authme.data.auth.PlayerAuth;
|
||||
import org.hamcrest.Description;
|
||||
import org.hamcrest.Matcher;
|
||||
import org.hamcrest.TypeSafeMatcher;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* Test for {@link AuthMePlayerImpl}.
|
||||
*/
|
||||
public class AuthMePlayerImplTest {
|
||||
|
||||
@Test
|
||||
public void shouldMapNullWithoutError() {
|
||||
// given / when / then
|
||||
assertThat(AuthMePlayerImpl.fromPlayerAuth(null), emptyOptional());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldMapFromPlayerAuth() {
|
||||
// given
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name("victor")
|
||||
.realName("Victor")
|
||||
.email("vic@example.com")
|
||||
.registrationDate(1480075661000L)
|
||||
.registrationIp("124.125.126.127")
|
||||
.lastLogin(1542675632000L)
|
||||
.lastIp("62.63.64.65")
|
||||
.uuid(UUID.fromString("deadbeef-2417-4653-9026-feedbabeface"))
|
||||
.build();
|
||||
|
||||
// when
|
||||
Optional<AuthMePlayer> result = AuthMePlayerImpl.fromPlayerAuth(auth);
|
||||
|
||||
// then
|
||||
AuthMePlayer playerInfo = result.get();
|
||||
assertThat(playerInfo.getName(), equalTo("Victor"));
|
||||
assertThat(playerInfo.getUuid().get(), equalTo(auth.getUuid()));
|
||||
assertThat(playerInfo.getEmail().get(), equalTo(auth.getEmail()));
|
||||
assertThat(playerInfo.getRegistrationDate(), equalTo(Instant.ofEpochMilli(auth.getRegistrationDate())));
|
||||
assertThat(playerInfo.getRegistrationIpAddress().get(), equalTo(auth.getRegistrationIp()));
|
||||
assertThat(playerInfo.getLastLoginDate().get(), equalTo(Instant.ofEpochMilli(auth.getLastLogin())));
|
||||
assertThat(playerInfo.getLastLoginIpAddress().get(), equalTo(auth.getLastIp()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldHandleNullAndDefaultValues() {
|
||||
// given
|
||||
PlayerAuth auth = PlayerAuth.builder()
|
||||
.name("victor")
|
||||
.realName("Victor")
|
||||
.email("your@email.com") // DB default
|
||||
.registrationDate(1480075661000L)
|
||||
.lastLogin(0L) // DB default
|
||||
.lastIp("127.0.0.1") // DB default
|
||||
.build();
|
||||
|
||||
// when
|
||||
Optional<AuthMePlayer> result = AuthMePlayerImpl.fromPlayerAuth(auth);
|
||||
|
||||
// then
|
||||
AuthMePlayer playerInfo = result.get();
|
||||
assertThat(playerInfo.getName(), equalTo("Victor"));
|
||||
assertThat(playerInfo.getUuid(), emptyOptional());
|
||||
assertThat(playerInfo.getEmail(), emptyOptional());
|
||||
assertThat(playerInfo.getRegistrationDate(), equalTo(Instant.ofEpochMilli(auth.getRegistrationDate())));
|
||||
assertThat(playerInfo.getRegistrationIpAddress(), emptyOptional());
|
||||
assertThat(playerInfo.getLastLoginDate(), emptyOptional());
|
||||
assertThat(playerInfo.getLastLoginIpAddress(), emptyOptional());
|
||||
}
|
||||
|
||||
private static <T> Matcher<Optional<T>> emptyOptional() {
|
||||
return new TypeSafeMatcher<Optional<T>>() {
|
||||
|
||||
@Override
|
||||
public void describeTo(Description description) {
|
||||
description.appendText("an empty optional");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean matchesSafely(Optional<T> item) {
|
||||
return !item.isPresent();
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user