Skip to content

Commit

Permalink
Well then
Browse files Browse the repository at this point in the history
  • Loading branch information
Tisawesomeness committed Sep 13, 2022
1 parent 0031a8e commit b4e0e7f
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 309 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import javax.annotation.Nullable;
import javax.sql.DataSource;
import java.io.IOException;
import java.nio.file.Path;
import java.sql.*;
import java.time.Instant;
import java.util.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public final class NameHistorian {
Expand Down Expand Up @@ -42,12 +44,8 @@ public final class NameHistorian {
" `detected_time`,\n" +
" `last_seen_time`\n" +
") VALUES (?, ?, ?, ?, ?);";
private static final String DELETE_ALL_HISTORY_SQL = "" +
"DELETE FROM `name_history`\n" +
"WHERE `uuid` = ?;";

private final DataSource source;
private final MojangLookup lookup;

/**
* Initializes NameHistorian by connecting to a SQLite database.
Expand All @@ -58,9 +56,6 @@ public final class NameHistorian {
* @throws IllegalStateException if the database is not at the correct version
*/
public NameHistorian(Path databasePath) throws SQLException {
this(databasePath, new MojangLookupImpl());
}
NameHistorian(Path databasePath, MojangLookup lookup) throws SQLException {
SQLiteDataSource ds = new SQLiteConnectionPoolDataSource();
ds.setUrl("jdbc:sqlite:" + databasePath.toFile().getAbsolutePath());
source = ds;
Expand All @@ -69,8 +64,6 @@ public NameHistorian(Path databasePath) throws SQLException {
if (getVersion() != VERSION) {
throw new IllegalStateException("Database version is not " + VERSION);
}

this.lookup = lookup;
}

private int getVersion() throws SQLException {
Expand Down Expand Up @@ -180,9 +173,6 @@ private void recordNewName(Connection con, NameRecord nr) throws SQLException {
*/
public List<NameRecord> getNameHistory(UUID uuid) throws SQLException {
@Cleanup Connection con = source.getConnection();
return retrieveNameHistory(con, uuid);
}
private List<NameRecord> retrieveNameHistory(Connection con, UUID uuid) throws SQLException {
@Cleanup PreparedStatement st = con.prepareStatement(READ_ALL_HISTORY_SQL);
st.setString(1, uuid.toString());
@Cleanup ResultSet rs = st.executeQuery();
Expand All @@ -193,54 +183,6 @@ private List<NameRecord> retrieveNameHistory(Connection con, UUID uuid) throws S
return list;
}

/**
* Requests the player's name history from the Mojang API and saves it to the database.
* This will overwrite the player's existing name history.
* BEWARE: this may error when Mojang disables the API endpoint on September 13th!
* @param uuid The UUID of the player to look up
* @return True on success, false if history cannot be looked up
* @throws IOException see {@link MojangLookup#fetchNameChanges(UUID)}
* @throws SQLException on database error
*/
public boolean saveMojangHistory(UUID uuid) throws IOException, SQLException {
List<NameChange> history = lookup.fetchNameChanges(uuid);
if (history.isEmpty()) {
return false;
}
List<NameRecord> records = new ArrayList<>();
Instant now = Instant.now();

for (int i = 0; i < history.size(); i++) {
NameChange change = history.get(i);

NameChange next = i + 1 < history.size() ? history.get(i + 1) : null;
Instant firstSeen;
if (!change.isOriginal()) {
firstSeen = change.getChangeTime();
} else if (next != null) {
firstSeen = next.getChangeTime();
} else {
firstSeen = now;
}
Instant lastSeen = next != null ? next.getChangeTime() : now;

records.add(new NameRecord(uuid, change.getName(), firstSeen, now, lastSeen));
}

asTransaction(con -> {
deleteHistory(con, uuid);
for (NameRecord nr : records) {
recordNewName(con, nr);
}
});
return true;
}
private void deleteHistory(Connection con, UUID uuid) throws SQLException {
@Cleanup PreparedStatement st = con.prepareStatement(DELETE_ALL_HISTORY_SQL);
st.setString(1, uuid.toString());
st.executeUpdate();
}

private NameDBRecord readDBRecord(ResultSet rs, UUID uuid) throws SQLException {
return new NameDBRecord(
rs.getInt("id"),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.tisawesomeness.namehistorian;

import com.tisawesomeness.namehistorian.testutil.MojangLookupMock;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -13,20 +11,14 @@
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class NameHistorianTest {

private static final UUID TIS_UUID = UUID.fromString("f6489b79-7a9f-49e2-980e-265a05dbc3af");
private static final Instant TIS_TIME = Instant.ofEpochMilli(1438695830000L);
private static final UUID JEB_UUID = UUID.fromString("853c80ef-3c37-49fd-aa49-938b674adae6");
private static final UUID DUMMY_UUID = UUID.fromString("f6489b79-7a9f-49e2-980e-265a05dbc3a0");
private static final UUID INVALID_UUID = UUID.fromString("f6489b79-7a9f-49e2-980e-265a05dbc3ae");
private static final UUID THROWING_UUID = UUID.fromString("e6489b79-7a9f-49e2-980e-265a05dbc3af");
private static final Duration GRACE_PERIOD = Duration.ofMinutes(1);
private NameHistorian historian;

Expand All @@ -36,7 +28,7 @@ public void setUp() throws SQLException, IOException {
Files.createDirectories(parent);
Path dbPath = parent.resolve("test.db");
Files.deleteIfExists(dbPath);
historian = new NameHistorian(dbPath, new MojangLookupMock());
historian = new NameHistorian(dbPath);
}

@Test
Expand Down Expand Up @@ -94,98 +86,4 @@ public void testBulkRecord() throws SQLException {
.containsExactly("test2");
}

@Test
public void testSaveHistory() throws SQLException, IOException {
assertThat(historian.saveMojangHistory(TIS_UUID)).isTrue();

List<NameRecord> history = historian.getNameHistory(TIS_UUID);
checkHistory(history);
}

@Test
public void testSaveHistoryAlreadyExists() throws SQLException, IOException {
historian.recordName(TIS_UUID, "Tis_awesomeness");
assertThat(historian.saveMojangHistory(TIS_UUID)).isTrue();

List<NameRecord> history = historian.getNameHistory(TIS_UUID);
checkHistory(history);
}

@Test
public void testSaveHistoryPreviouslyExists() throws SQLException, IOException {
NameRecord nr = new NameRecord(TIS_UUID, "tis_awesomeness", TIS_TIME.minusSeconds(10), Instant.now(), TIS_TIME.minusSeconds(5));
historian.recordName(nr);
assertThat(historian.saveMojangHistory(TIS_UUID)).isTrue();

List<NameRecord> history = historian.getNameHistory(TIS_UUID);
checkHistory(history);
}

@Test
public void testSaveHistoryDuplicate() throws SQLException, IOException {
assertThat(historian.saveMojangHistory(TIS_UUID)).isTrue();
assertThat(historian.saveMojangHistory(TIS_UUID)).isTrue();

List<NameRecord> history = historian.getNameHistory(TIS_UUID);
checkHistory(history);
}

private static void checkHistory(List<NameRecord> history) {
assertThat(history).hasSize(2);

NameRecord latest = history.get(0);
assertThat(latest)
.extracting(NameRecord::getUsername, NameRecord::getUuid, NameRecord::getFirstSeenTime)
.containsExactly("Tis_awesomeness", TIS_UUID, TIS_TIME);
assertThat(Arrays.asList(latest.getDetectedTime(), latest.getLastSeenTime()))
.allSatisfy(t -> assertThat(t).isBetween(Instant.now().minus(GRACE_PERIOD), Instant.now()));

NameRecord original = history.get(1);
assertThat(original)
.extracting(NameRecord::getUsername, NameRecord::getUuid, NameRecord::getFirstSeenTime, NameRecord::getLastSeenTime)
.containsExactly("tis_awesomeness", TIS_UUID, NameHistorianTest.TIS_TIME, TIS_TIME);
assertThat(original.getDetectedTime())
.isBetween(Instant.now().minus(GRACE_PERIOD), Instant.now());
}

@Test
public void testSaveHistory1() throws IOException, SQLException {
assertThat(historian.saveMojangHistory(JEB_UUID)).isTrue();
List<NameRecord> history = historian.getNameHistory(JEB_UUID);

assertThat(history).hasSize(1);

NameRecord original = history.get(0);
assertThat(original)
.extracting(NameRecord::getUsername, NameRecord::getUuid)
.containsExactly("jeb_", JEB_UUID);
assertThat(Arrays.asList(original.getFirstSeenTime(), original.getDetectedTime(), original.getLastSeenTime()))
.allSatisfy(t -> assertThat(t).isBetween(Instant.now().minus(GRACE_PERIOD), Instant.now()));
}

@Test
public void testSaveHistory2() throws IOException, SQLException {
assertThat(historian.saveMojangHistory(DUMMY_UUID)).isTrue();
List<NameRecord> history = historian.getNameHistory(DUMMY_UUID);

assertThat(history).hasSize(3);

NameRecord middle = history.get(1);
assertThat(middle)
.extracting(NameRecord::getUsername, NameRecord::getUuid, NameRecord::getFirstSeenTime, NameRecord::getLastSeenTime)
.containsExactly("dummy2", DUMMY_UUID, TIS_TIME.plusSeconds(1), TIS_TIME.plusSeconds(2));
assertThat(middle.getDetectedTime())
.isBetween(Instant.now().minus(GRACE_PERIOD), Instant.now());
}

@Test
public void testSaveHistoryInvalid() throws SQLException, IOException {
assertThat(historian.saveMojangHistory(INVALID_UUID)).isFalse();
}
@Test
public void testSaveHistoryIOE() {
assertThatThrownBy(() -> historian.saveMojangHistory(THROWING_UUID))
.isInstanceOf(IOException.class);
}

}

This file was deleted.

0 comments on commit b4e0e7f

Please sign in to comment.