Skip to content

Commit d0f00ba

Browse files
committed
Update MainTest and ElpriserApi to handle multiple days.
1 parent caa0290 commit d0f00ba

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

src/main/java/com/example/api/ElpriserAPI.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public enum Prisklass {
5959
* use the String it provides instead of making a real HTTP call.
6060
*/
6161
private static Supplier<String> mockResponseSupplier = null;
62+
63+
// New: map mock responses per date, so tests can provide different JSON per day
64+
private static java.util.Map<LocalDate, String> datedMockResponses = new java.util.HashMap<>();
6265

6366
/**
6467
* FOR TESTS ONLY: Sets a mock JSON response to be returned by the next API call.
@@ -68,13 +71,26 @@ public enum Prisklass {
6871
public static void setMockResponse(String jsonResponse) {
6972
mockResponseSupplier = () -> jsonResponse;
7073
}
74+
75+
/**
76+
* FOR TESTS ONLY: Sets a mock JSON response for a specific date. This allows
77+
* tests to simulate availability for one day but not another.
78+
*/
79+
public static void setMockResponseForDate(LocalDate date, String jsonResponse) {
80+
if (jsonResponse == null) {
81+
datedMockResponses.remove(date);
82+
} else {
83+
datedMockResponses.put(date, jsonResponse);
84+
}
85+
}
7186

7287
/**
7388
* FOR TESTS ONLY: Clears the mock response, causing the API to resume
7489
* making real network requests. This should be called after each test.
7590
*/
7691
public static void clearMockResponse() {
7792
mockResponseSupplier = null;
93+
datedMockResponses.clear();
7894
}
7995
// --- End of test fields ---
8096

@@ -142,10 +158,9 @@ public List<Elpris> getPriser(LocalDate datum, Prisklass prisklass) {
142158
}
143159

144160
// Check for a mock response before making a network call ---
145-
if (mockResponseSupplier != null) {
161+
if (mockResponseSupplier != null || !datedMockResponses.isEmpty()) {
146162
System.out.println("!!! ANVÄNDER MOCK-DATA FÖR TEST !!!");
147-
String mockJson = mockResponseSupplier.get();
148-
// If the mock is null or empty, simulate a "not found" scenario
163+
String mockJson = datedMockResponses.getOrDefault(datum, mockResponseSupplier == null ? null : mockResponseSupplier.get());
149164
if (mockJson == null || mockJson.isBlank()) {
150165
return Collections.emptyList();
151166
}

src/test/java/com/example/MainTest.java

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,21 @@ void findOptimalCharging4Hours() {
216216
assertThat(output).contains("12,5");
217217
}
218218

219+
@Test
220+
void chargingWindowDoesNotUseNextDay_whenNextDayUnavailable() {
221+
// Only today's 3 hours, request 2h window -> should compute within these only
222+
String mockJsonToday = """
223+
[{"SEK_per_kWh":0.20,"EUR_per_kWh":0.02,"EXR":10.0,"time_start":"2025-09-04T00:00:00+02:00","time_end":"2025-09-04T01:00:00+02:00"},
224+
{"SEK_per_kWh":0.10,"EUR_per_kWh":0.01,"EXR":10.0,"time_start":"2025-09-04T01:00:00+02:00","time_end":"2025-09-04T02:00:00+02:00"},
225+
{"SEK_per_kWh":0.15,"EUR_per_kWh":0.015,"EXR":10.0,"time_start":"2025-09-04T02:00:00+02:00","time_end":"2025-09-04T03:00:00+02:00"}]""";
226+
ElpriserAPI.setMockResponse(mockJsonToday);
227+
Main.main(new String[]{"--zone", "SE3", "--date", "2025-09-04", "--charging", "2h"});
228+
String output = bos.toString();
229+
// Best 2h window should be 01-03 (0.10 + 0.15)
230+
assertThat(output).contains("Påbörja laddning");
231+
assertThat(output).contains("01:00");
232+
}
233+
219234
@Test
220235
void findOptimalCharging8Hours() {
221236
// Create mock data with 12 hours to allow for 8-hour window
@@ -298,15 +313,20 @@ void handleNoDataAvailable() {
298313
}
299314

300315
@Test
301-
void handleMultipleDaysData() {
302-
// Mock response with data for two days
303-
String mockJson = """
316+
void handleMultipleDaysData_includesNextDayForCharging() {
317+
// This test ensures charging window can span days when next day data exists
318+
LocalDate today = LocalDate.of(2025, 9, 4);
319+
LocalDate tomorrow = today.plusDays(1);
320+
321+
String mockJsonToday = """
304322
[{"SEK_per_kWh":0.30,"EUR_per_kWh":0.03,"EXR":10.0,"time_start":"2025-09-04T22:00:00+02:00","time_end":"2025-09-04T23:00:00+02:00"},
305-
{"SEK_per_kWh":0.25,"EUR_per_kWh":0.025,"EXR":10.0,"time_start":"2025-09-04T23:00:00+02:00","time_end":"2025-09-05T00:00:00+02:00"},
306-
{"SEK_per_kWh":0.10,"EUR_per_kWh":0.01,"EXR":10.0,"time_start":"2025-09-05T00:00:00+02:00","time_end":"2025-09-05T01:00:00+02:00"},
323+
{"SEK_per_kWh":0.25,"EUR_per_kWh":0.025,"EXR":10.0,"time_start":"2025-09-04T23:00:00+02:00","time_end":"2025-09-05T00:00:00+02:00"}]""";
324+
String mockJsonTomorrow = """
325+
[{"SEK_per_kWh":0.10,"EUR_per_kWh":0.01,"EXR":10.0,"time_start":"2025-09-05T00:00:00+02:00","time_end":"2025-09-05T01:00:00+02:00"},
307326
{"SEK_per_kWh":0.15,"EUR_per_kWh":0.015,"EXR":10.0,"time_start":"2025-09-05T01:00:00+02:00","time_end":"2025-09-05T02:00:00+02:00"}]""";
308327

309-
ElpriserAPI.setMockResponse(mockJson);
328+
ElpriserAPI.setMockResponseForDate(today, mockJsonToday);
329+
ElpriserAPI.setMockResponseForDate(tomorrow, mockJsonTomorrow);
310330

311331
Main.main(new String[]{"--zone", "SE3", "--date", "2025-09-04", "--charging", "4h"});
312332

@@ -315,6 +335,29 @@ void handleMultipleDaysData() {
315335
// Should be able to find optimal charging window across day boundary
316336
}
317337

338+
@Test
339+
void chargingWindowSpansToNextDay_whenCheapestCrossesMidnight() {
340+
// Prices set so best 2h window is 23:00-01:00 (0.50 at 22, 0.20 at 23, 0.05 at 00, 0.40 at 01)
341+
LocalDate today = LocalDate.of(2025, 9, 4);
342+
LocalDate tomorrow = today.plusDays(1);
343+
344+
String mockJsonToday = """
345+
[{"SEK_per_kWh":0.50,"EUR_per_kWh":0.05,"EXR":10.0,"time_start":"2025-09-04T22:00:00+02:00","time_end":"2025-09-04T23:00:00+02:00"},
346+
{"SEK_per_kWh":0.20,"EUR_per_kWh":0.02,"EXR":10.0,"time_start":"2025-09-04T23:00:00+02:00","time_end":"2025-09-05T00:00:00+02:00"}]""";
347+
String mockJsonTomorrow = """
348+
[{"SEK_per_kWh":0.05,"EUR_per_kWh":0.005,"EXR":10.0,"time_start":"2025-09-05T00:00:00+02:00","time_end":"2025-09-05T01:00:00+02:00"},
349+
{"SEK_per_kWh":0.40,"EUR_per_kWh":0.04,"EXR":10.0,"time_start":"2025-09-05T01:00:00+02:00","time_end":"2025-09-05T02:00:00+02:00"}]""";
350+
351+
ElpriserAPI.setMockResponseForDate(today, mockJsonToday);
352+
ElpriserAPI.setMockResponseForDate(tomorrow, mockJsonTomorrow);
353+
354+
Main.main(new String[]{"--zone", "SE3", "--date", "2025-09-04", "--charging", "2h"});
355+
String output = bos.toString();
356+
assertThat(output).contains("Påbörja laddning");
357+
// Expect start at 23:00 (23 + 00 window is cheapest)
358+
assertThat(output).contains("23:00");
359+
}
360+
318361
@Test
319362
public void testHourlyMinMaxPrices() {
320363
List<Double> quarterHourPrices = new ArrayList<>();

0 commit comments

Comments
 (0)