cleanup
This commit is contained in:
1 parent
868dbea8df
commit
a507b471b4
13 files changed
+122
-184
No files matched your search
@@ -1,4 +1,4 @@
|
||||
package me.bobert.netneutrality.command.basic;
|
||||
package me.bobert.netneutrality.command.bot;
|
||||
/*
|
||||
* Copyright (c) anthorob
|
||||
*
|
||||
|
||||
@@ -14,8 +14,6 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.function.Predicate.not;
|
||||
|
||||
public class UsageValidator {
|
||||
|
||||
public boolean validate(CommandContext context, CommandContainer container) {
|
||||
|
||||
@@ -100,7 +100,7 @@ public abstract class CommandContext extends AbstractContext {
|
||||
|
||||
public CommandOption getOption(String name) {
|
||||
List<CommandOption> options = getOptionsByName(name);
|
||||
return options.isEmpty() ? null : options.get(0);
|
||||
return options.isEmpty() ? null : options.getFirst();
|
||||
}
|
||||
|
||||
public List<CommandOption> getOptionsByName(String name) {
|
||||
|
||||
@@ -89,7 +89,7 @@ public class ScheduledTaskManager implements Lifecycle {
|
||||
|
||||
public void cancelAll() {
|
||||
logger.info("Cancelling all {} scheduled tasks", scheduledTasks.size());
|
||||
scheduledTasks.forEach((id, future) -> future.cancel(false));
|
||||
scheduledTasks.forEach((_, future) -> future.cancel(false));
|
||||
scheduledTasks.clear();
|
||||
}
|
||||
|
||||
|
||||
@@ -182,7 +182,7 @@ public class TaskManagerImpl implements TaskManager {
|
||||
String lockId = task.getLockId();
|
||||
ReentrantLock lock = (task instanceof PersistableTask || task instanceof BuiltInTask)
|
||||
? null
|
||||
: tasksLock.computeIfAbsent(lockId, k -> new ReentrantLock());
|
||||
: tasksLock.computeIfAbsent(lockId, _ -> new ReentrantLock());
|
||||
|
||||
try {
|
||||
if (lock != null) {
|
||||
|
||||
@@ -105,7 +105,7 @@ public class WebModule {
|
||||
try (Response response = client.newCall(request).execute()) {
|
||||
long elapsedTime = System.currentTimeMillis() - startTime;
|
||||
Sentry.addBreadcrumb(Breadcrumb.http(url, method.name(), response.code()));
|
||||
String body = response.body() != null ? response.body().string() : null;
|
||||
String body = response.body().string();
|
||||
|
||||
this.cachedResponse = new CachedResponse(
|
||||
response.code(),
|
||||
@@ -168,13 +168,13 @@ public class WebModule {
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return cachedResponse != null && cachedResponse.getStatusCode() >= 200 && cachedResponse.getStatusCode() < 400;
|
||||
return cachedResponse != null && cachedResponse.statusCode() >= 200 && cachedResponse.statusCode() < 400;
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private Request request;
|
||||
private final String url;
|
||||
private String version;
|
||||
private final String version;
|
||||
private Method method = Method.GET;
|
||||
private long timeout = 5;
|
||||
private TimeUnit timeUnit = TimeUnit.MINUTES;
|
||||
@@ -328,45 +328,19 @@ public class WebModule {
|
||||
}
|
||||
}
|
||||
|
||||
private static class CachedResponse {
|
||||
private final int statusCode;
|
||||
private final Headers headers;
|
||||
private final long elapsedTime;
|
||||
private final String body;
|
||||
private final long expirationTime;
|
||||
private final String errorMessage;
|
||||
|
||||
public CachedResponse(int statusCode, Headers headers, long elapsedTime, String body, long expirationTime, String errorMessage) {
|
||||
this.statusCode = statusCode;
|
||||
this.headers = headers;
|
||||
this.elapsedTime = elapsedTime;
|
||||
this.body = body;
|
||||
this.expirationTime = expirationTime;
|
||||
this.errorMessage = errorMessage;
|
||||
}
|
||||
private record CachedResponse(int statusCode, Headers headers, long elapsedTime, String body, long expirationTime,
|
||||
String errorMessage) {
|
||||
|
||||
public boolean isExpired() {
|
||||
return System.currentTimeMillis() > expirationTime;
|
||||
}
|
||||
return System.currentTimeMillis() > expirationTime;
|
||||
}
|
||||
|
||||
public int getStatusCode() {
|
||||
return statusCode;
|
||||
}
|
||||
public Optional<String> getBody() {
|
||||
return Optional.ofNullable(body);
|
||||
}
|
||||
|
||||
public Headers getHeaders() {
|
||||
return headers;
|
||||
public Optional<String> getErrorMessage() {
|
||||
return Optional.ofNullable(errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
public long getElapsedTime() {
|
||||
return elapsedTime;
|
||||
}
|
||||
|
||||
public Optional<String> getBody() {
|
||||
return Optional.ofNullable(body);
|
||||
}
|
||||
|
||||
public Optional<String> getErrorMessage() {
|
||||
return Optional.ofNullable(errorMessage);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,35 +16,20 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
public class ApplicationContextTest {
|
||||
|
||||
private static final class RecordingComponent implements Lifecycle {
|
||||
private final String name;
|
||||
private final int priority;
|
||||
private final List<String> startLog;
|
||||
private final List<String> stopLog;
|
||||
|
||||
private RecordingComponent(String name, int priority, List<String> startLog, List<String> stopLog) {
|
||||
this.name = name;
|
||||
this.priority = priority;
|
||||
this.startLog = startLog;
|
||||
this.stopLog = stopLog;
|
||||
}
|
||||
private record RecordingComponent(String name, int priority, List<String> startLog,
|
||||
List<String> stopLog) implements Lifecycle {
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
startLog.add(name);
|
||||
}
|
||||
public void start() {
|
||||
startLog.add(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
stopLog.add(name);
|
||||
@Override
|
||||
public void stop() {
|
||||
stopLog.add(name);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int priority() {
|
||||
return priority;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void startsAscendingAndStopsDescending() throws Exception {
|
||||
List<String> startLog = new ArrayList<>();
|
||||
|
||||
@@ -25,30 +25,16 @@ public class ReflectionInjectionTest {
|
||||
public interface Fixture {
|
||||
}
|
||||
|
||||
/** Dependency registered in the test context. */
|
||||
public static final class SampleDependency {
|
||||
private final String label;
|
||||
|
||||
public SampleDependency(String label) {
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public String label() {
|
||||
return label;
|
||||
}
|
||||
/**
|
||||
* Dependency registered in the test context.
|
||||
*/
|
||||
public record SampleDependency(String label) {
|
||||
}
|
||||
|
||||
/** Fixture that requires a registered dependency via constructor injection. */
|
||||
public static final class InjectedFixture implements Fixture {
|
||||
private final SampleDependency dependency;
|
||||
|
||||
public InjectedFixture(SampleDependency dependency) {
|
||||
this.dependency = dependency;
|
||||
}
|
||||
|
||||
public SampleDependency dependency() {
|
||||
return dependency;
|
||||
}
|
||||
/**
|
||||
* Fixture that requires a registered dependency via constructor injection.
|
||||
*/
|
||||
public record InjectedFixture(SampleDependency dependency) implements Fixture {
|
||||
}
|
||||
|
||||
/** Fixture that only exposes a no-arg constructor (fallback path). */
|
||||
|
||||
@@ -22,19 +22,17 @@ import static org.mockito.Mockito.*;
|
||||
public class QueueManagerTest {
|
||||
|
||||
private AudioPlayer player;
|
||||
private TrackScheduler scheduler;
|
||||
private MusicManager musicManager;
|
||||
private MusicManagerCallback callback;
|
||||
private Guild guild;
|
||||
private QueueManager queueManager;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
player = mock(AudioPlayer.class);
|
||||
guild = mock(Guild.class);
|
||||
Guild guild = mock(Guild.class);
|
||||
when(guild.getId()).thenReturn("guild1");
|
||||
|
||||
scheduler = mock(TrackScheduler.class);
|
||||
TrackScheduler scheduler = mock(TrackScheduler.class);
|
||||
musicManager = mock(MusicManager.class);
|
||||
callback = mock(MusicManagerCallback.class);
|
||||
|
||||
|
||||
@@ -17,8 +17,6 @@ import static org.mockito.Mockito.*;
|
||||
public class TrackSchedulerTest {
|
||||
|
||||
private AudioPlayer player;
|
||||
private MusicManager musicManager;
|
||||
private QueueManager queueManager;
|
||||
private TrackScheduler scheduler;
|
||||
|
||||
@BeforeEach
|
||||
@@ -27,12 +25,11 @@ public class TrackSchedulerTest {
|
||||
Guild guild = mock(Guild.class);
|
||||
when(guild.getId()).thenReturn("guild1");
|
||||
|
||||
musicManager = mock(MusicManager.class);
|
||||
MusicManager musicManager = mock(MusicManager.class);
|
||||
when(musicManager.getPlayer()).thenReturn(player);
|
||||
when(musicManager.getGuild()).thenReturn(guild);
|
||||
|
||||
scheduler = new TrackScheduler(musicManager);
|
||||
queueManager = scheduler.getQueueManager();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user