Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/week-9-getAll.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/screenshots/week-9-getById.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package me.nickhanson.codeforge.service;

import me.nickhanson.codeforge.entity.Challenge;

import java.util.List;
import java.util.Optional;

public class Week9ChallengeService {
private final ChallengeService svc;

public Week9ChallengeService() {
this.svc = new ChallengeService();
}

public Week9ChallengeService(ChallengeService svc) {
this.svc = svc;
}

public List<Challenge> findAll() {
return svc.listChallenges(null);
}

public Optional<Challenge> findById(Long id) {
return svc.getById(id);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package me.nickhanson.codeforge.persistence;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.sql.Statement;

/**
* Base class for DAO integration tests. It resets the in-memory H2 database tables
* between tests for isolation.
Expand All @@ -15,11 +16,15 @@ public abstract class DaoTestBase {
void resetDatabase() {
// Delete in FK-safe order
try (Session session = SessionFactoryProvider.getSessionFactory().openSession()) {
Transaction tx = session.beginTransaction();
session.createNativeQuery("DELETE FROM SUBMISSIONS").executeUpdate();
session.createNativeQuery("DELETE FROM DRILL_ITEMS").executeUpdate();
session.createNativeQuery("DELETE FROM CHALLENGES").executeUpdate();
tx.commit();
session.doWork(conn -> {
try (Statement st = conn.createStatement()) {
st.execute("SET REFERENTIAL_INTEGRITY TO FALSE");
st.execute("TRUNCATE TABLE SUBMISSIONS RESTART IDENTITY");
st.execute("TRUNCATE TABLE DRILL_ITEMS RESTART IDENTITY");
st.execute("TRUNCATE TABLE CHALLENGES RESTART IDENTITY");
st.execute("SET REFERENTIAL_INTEGRITY TO TRUE");
}
});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.List;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
Expand All @@ -23,6 +23,31 @@ class ChallengeServiceTest {
@Mock ChallengeDao dao;
@InjectMocks ChallengeService svc;

@Test
void findAll() {
List<Challenge> all = List.of(
new Challenge("Two Sum", Difficulty.EASY, "b","p"),
new Challenge("LRU Cache", Difficulty.HARD, "b","p")
);
when(dao.getAll()).thenReturn(all);

List<Challenge> result = svc.listChallenges(null);

assertEquals(2, result.size());
verify(dao).getAll();
verifyNoMoreInteractions(dao);
}

@Test
void findById_returnsOne_whenPresent() {
Challenge c = new Challenge("Two Sum", Difficulty.EASY, "b","p");
when(dao.getById(42L)).thenReturn(c);

assertTrue(svc.getById(42L).isPresent());
assertEquals("Two Sum", svc.getById(42L).get().getTitle());
verify(dao, times(2)).getById(42L); // or call once then store the Optional
Comment on lines +46 to +48
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test calls svc.getById(42L) twice, which results in two unnecessary DAO calls. Store the result in a variable to avoid redundant method invocations. The comment acknowledges this but the code should be fixed.

Suggested change
assertTrue(svc.getById(42L).isPresent());
assertEquals("Two Sum", svc.getById(42L).get().getTitle());
verify(dao, times(2)).getById(42L); // or call once then store the Optional
var result = svc.getById(42L);
assertTrue(result.isPresent());
assertEquals("Two Sum", result.get().getTitle());
verify(dao).getById(42L);

Copilot uses AI. Check for mistakes.
}

@Test
void create_setsFields_and_saves() {
ChallengeForm form = new ChallengeForm();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void getRandomQuote_formatsSingleElementResponse_andCaches() throws Exception {
.thenReturn(httpResponse);

String first = service.getRandomQuote();
assertEquals("\u201cHello\u201d — World", first);
assertEquals("“Hello” — World", first);

// Second call should use cache; http.send still only 1 call
String second = service.getRandomQuote();
Expand Down Expand Up @@ -123,7 +123,7 @@ void getRandomQuote_multiElementArray_returnsOneOfThem() throws Exception {
.thenReturn(httpResponse);

String out = service.getRandomQuote();
assertTrue(out.equals("\u201cAlpha\u201d — A") || out.equals("\u201cBeta\u201d — B"));
assertTrue(out.equals("“Alpha” — A") || out.equals("“Beta” — B"));
}

/**
Expand All @@ -138,7 +138,7 @@ void getRandomQuote_blankAuthor_defaultsToUnknown() throws Exception {
.thenReturn(httpResponse);

String out = service.getRandomQuote();
assertEquals("\u201cMsg\u201d — Unknown", out);
assertEquals("“Msg” — Unknown", out);
}

// --- helpers ---
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package me.nickhanson.codeforge.service;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.*;
import org.mockito.junit.jupiter.MockitoExtension;


import static org.mockito.Mockito.verify;

@ExtendWith(MockitoExtension.class)
public class Week9ChallengeServiceTest {

@Mock ChallengeService svc;
Week9ChallengeService w9;

@BeforeEach
void setUp() { w9 = new Week9ChallengeService(svc); }

@Test
void findAll() {
w9.findAll();
verify(svc).listChallenges(null);
}

@Test
void findById() {
Long id = 42L;
w9.findById(id);
verify(svc).getById(id);
}
}