Skip to content
This repository has been archived by the owner on Nov 12, 2023. It is now read-only.

Commit

Permalink
fix: PermSyncの動作不良修正、テストコード追加 (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
book000 authored Aug 9, 2022
1 parent e1a9c79 commit 418f06f
Show file tree
Hide file tree
Showing 3 changed files with 161 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/com/jaoafa/javajaotan2/lib/DiscordMinecraftLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,18 @@ public static DiscordMinecraftLink get(long discordId) throws SQLException {
}
Connection conn = manager.getConnection();
String sql = "SELECT * FROM discordlink WHERE disid = ?";
DiscordMinecraftLink link = null;
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, Long.toString(discordId));
try (ResultSet res = stmt.executeQuery()) {
while (res.next()) {
DiscordMinecraftLink link = new DiscordMinecraftLink(res);
link = new DiscordMinecraftLink(res);

if (link.isLinked()) return link;
}
}
}
return null;
return link;
}

public static DiscordMinecraftLink get(UUID uuid) throws SQLException {
Expand All @@ -63,17 +64,18 @@ public static DiscordMinecraftLink get(UUID uuid) throws SQLException {
}
Connection conn = manager.getConnection();
String sql = "SELECT * FROM discordlink WHERE uuid = ?";
DiscordMinecraftLink link = null;
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setString(1, uuid.toString());
try (ResultSet res = stmt.executeQuery()) {
while (res.next()) {
DiscordMinecraftLink link = new DiscordMinecraftLink(res);
link = new DiscordMinecraftLink(res);

if (link.isLinked()) return link;
}
}
}
return null;
return link;
}

private DiscordMinecraftLink(ResultSet res) throws SQLException {
Expand All @@ -90,7 +92,7 @@ private DiscordMinecraftLink(ResultSet res) throws SQLException {
this.disconnectAt = res.getTimestamp("dead_at");
this.connectedAt = res.getTimestamp("date");

lastLogin = getLastLogin(this.minecraftUUID);
this.lastLogin = getLastLogin(this.minecraftUUID);
}

/**
Expand Down Expand Up @@ -286,6 +288,7 @@ public static Timestamp getLastLogin(UUID uuid) throws SQLException {

/**
* Minecraft アカウントを基準にすべての連携データを取得します。<br>
* {@link #isLinked()} が False なデータはこのリストに追加されません。<br>
* 同じ UUID を持った連携データが複数ある場合は、最後に登録されたものが返されます。
*
* @return 連携データ
Expand All @@ -301,8 +304,9 @@ public static List<DiscordMinecraftLink> getAllForMinecraft() throws SQLExceptio
Connection conn = manager.getConnection();
List<DiscordMinecraftLink> connections = new ArrayList<>();
List<UUID> inserted = new ArrayList<>();
String sql = "SELECT * FROM discordlink ORDER BY id";
String sql = "SELECT * FROM discordlink WHERE disabled = ? ORDER BY id";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setBoolean(1, false);
try (ResultSet res = stmt.executeQuery()) {
while (res.next()) {
UUID uuid = UUID.fromString(res.getString("uuid"));
Expand All @@ -319,6 +323,7 @@ public static List<DiscordMinecraftLink> getAllForMinecraft() throws SQLExceptio

/**
* Discord アカウントを基準にすべての連携データを取得します。<br>
* {@link #isLinked()} が False なデータはこのリストに追加されません。<br>
* 同じ Discord ID を持った連携データが複数ある場合は、最後に登録されたものが返されます。
*
* @return 連携データ
Expand All @@ -334,8 +339,9 @@ public static List<DiscordMinecraftLink> getAllForDiscord() throws SQLException
Connection conn = manager.getConnection();
List<DiscordMinecraftLink> connections = new ArrayList<>();
List<String> inserted = new ArrayList<>();
String sql = "SELECT * FROM discordlink ORDER BY id";
String sql = "SELECT * FROM discordlink WHERE disabled = ? ORDER BY id";
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setBoolean(1, false);
try (ResultSet res = stmt.executeQuery()) {
while (res.next()) {
String discordId = res.getString("disid");
Expand Down
103 changes: 103 additions & 0 deletions src/test/com/jaoafa/javajaotan2/lib/DiscordMinecraftLinkTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.jaoafa.javajaotan2.lib;

import com.jaoafa.javajaotan2.Main;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

@SuppressWarnings("FieldCanBeLocal")
class DiscordMinecraftLinkTest {
private final String minecraftName = "mine_book000";
private final UUID minecraftUUID = UUID.fromString("32ff7cdc-a1b4-450a-aa7e-6af75fe8c37c");
private final String discordName = "tomachi";
private final String discordDiscriminator = "0310";
private final long discordId = 221991565567066112L;

DiscordMinecraftLink linkForMinecraft;
DiscordMinecraftLink linkForDiscord;

@BeforeEach
void setUp() {
try {
Main.setConfig(new JavajaotanConfig());
} catch (RuntimeException e) {
e.printStackTrace();
return; // CI用
}
try {
linkForMinecraft = DiscordMinecraftLink.get(minecraftUUID);
linkForDiscord = DiscordMinecraftLink.get(discordId);
} catch (SQLException e) {
e.printStackTrace();
fail();
}
assertNotNull(linkForMinecraft);
assertNotNull(linkForDiscord);
}

@Test
void isFound() {
assertTrue(linkForMinecraft.isFound());
assertTrue(linkForDiscord.isFound());
}

@Test
void isLinked() {
assertTrue(linkForMinecraft.isLinked());
assertTrue(linkForDiscord.isLinked());
}

@Test
void getMinecraftName() {
assertEquals(minecraftName, linkForMinecraft.getMinecraftName());
assertEquals(minecraftName, linkForDiscord.getMinecraftName());
}

@Test
void getMinecraftUUID() {
assertEquals(minecraftUUID, linkForMinecraft.getMinecraftUUID());
assertEquals(minecraftUUID, linkForDiscord.getMinecraftUUID());
}

@Test
void getDiscordId() {
assertEquals(Long.toUnsignedString(discordId), linkForMinecraft.getDiscordId());
assertEquals(Long.toUnsignedString(discordId), linkForDiscord.getDiscordId());
}

@Test
void getDiscordName() {
assertEquals(discordName, linkForMinecraft.getDiscordName());
assertEquals(discordName, linkForDiscord.getDiscordName());
}

@Test
void getDiscordDiscriminator() {
assertEquals(discordDiscriminator, linkForMinecraft.getDiscordDiscriminator());
assertEquals(discordDiscriminator, linkForDiscord.getDiscordDiscriminator());
}

@Test
void getDisconnectPermGroup() {
assertNull(linkForMinecraft.getDisconnectPermGroup());
assertNull(linkForDiscord.getDisconnectPermGroup());
}

@Test
void getAllForMinecraft() throws SQLException {
List<DiscordMinecraftLink> connections = DiscordMinecraftLink.getAllForMinecraft();
DiscordMinecraftLink dml = connections
.stream()
.filter(c -> c.getDiscordId().equals(Long.toUnsignedString(discordId)))
.findFirst()
.orElse(null);
assertNotNull(dml);
assertTrue(dml.isFound());
assertTrue(dml.isLinked());
}
}
45 changes: 45 additions & 0 deletions src/test/com/jaoafa/javajaotan2/lib/MinecraftPermGroupTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.jaoafa.javajaotan2.lib;

import com.jaoafa.javajaotan2.Main;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.sql.SQLException;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;

class MinecraftPermGroupTest {
MinecraftPermGroup mpg;

@BeforeEach
void setUp() throws SQLException {
try {
Main.setConfig(new JavajaotanConfig());
} catch (RuntimeException e) {
e.printStackTrace();
return; // CI用
}
mpg = new MinecraftPermGroup(UUID.fromString("32ff7cdc-a1b4-450a-aa7e-6af75fe8c37c"));
}

@Test
void isFound() {
assertTrue(mpg.isFound());
}

@Test
void getGroup() {
assertEquals(MinecraftPermGroup.Group.ADMIN, mpg.getGroup());
}

@Test
void isTemporary() {
assertFalse(mpg.isTemporary());
}

@Test
void getExpiredAt() {
assertNull(mpg.getExpiredAt());
}
}

0 comments on commit 418f06f

Please sign in to comment.