Merge branch 'client/fix-inject-httpclient' into 'main'

fix(client): use Guice injection pattern for HttpClient

Closes #59

See merge request cse1105/2025-2026/teams/csep-team-76!53
This commit is contained in:
Zhongheng Liu 2026-01-17 14:28:18 +01:00
commit 2823711fcd
4 changed files with 10 additions and 5 deletions

View file

@ -35,6 +35,7 @@ import com.google.inject.TypeLiteral;
import commons.Ingredient;
import commons.Recipe;
import java.net.http.HttpClient;
import java.nio.file.Path;
public class MyModule implements Module {
@ -51,6 +52,7 @@ public class MyModule implements Module {
binder.bind(WebSocketUtils.class).in(Scopes.SINGLETON);
binder.bind(NutritionDetailsCtrl.class).in(Scopes.SINGLETON);
binder.bind(NutritionViewCtrl.class).in(Scopes.SINGLETON);
binder.bind(HttpClient.class).in(Scopes.SINGLETON);
binder.bind(ConfigService.class).toInstance(new ConfigService(Path.of("config.json")));
binder.bind(new TypeLiteral<WebSocketDataService<Long, Recipe>>() {}).toInstance(
new WebSocketDataService<>()
@ -59,4 +61,4 @@ public class MyModule implements Module {
new WebSocketDataService<>()
);
}
}
}

View file

@ -32,8 +32,8 @@ public class ServerUtils {
private final int statusOK = 200;
@Inject
public ServerUtils() {
client = HttpClient.newHttpClient();
public ServerUtils(HttpClient client) {
this.client = client;
}
/**

View file

@ -12,6 +12,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.http.HttpClient;
import java.util.ArrayList;
import java.util.List;
@ -40,7 +41,7 @@ class ServerUtilsMockTest {
@BeforeEach
void setup() {
objectMapper = new ObjectMapper();
serverUtils = new ServerUtils();
serverUtils = new ServerUtils(HttpClient.newHttpClient());
}
/**

View file

@ -11,6 +11,7 @@ import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.http.HttpClient;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
@ -32,7 +33,8 @@ class ServerUtilsTest {
@BeforeEach
void setup() throws IOException, InterruptedException {
dv = new ServerUtils();
// FIXME: Prefer Guice-provided instance via a unit test Module.
dv = new ServerUtils(HttpClient.newHttpClient());
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available");