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
2 changes: 2 additions & 0 deletions src/main/java/org/example/LoanServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

public class LoanServices {



// Kolla om en bok är utlånad
public boolean isBookLoaned(Long bookId, EntityManager em) {
List<Loan> loans = em.createQuery(
Expand Down
1 change: 1 addition & 0 deletions src/test/java/org/example/BookSearchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
@Disabled("Integration test – runs locally only")
class BookSearchTest {


private static EntityManagerFactory emf;
private EntityManager em;

Expand Down
75 changes: 75 additions & 0 deletions src/test/java/org/example/LoanServicesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.example;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;
import org.junit.jupiter.api.*;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertNotNull;
@Disabled("Integration test – runs locally only")
public class LoanServicesTest {

private static EntityManagerFactory emf;
private EntityManager em;

@BeforeAll
static void init() {
emf = Persistence.createEntityManagerFactory("library_system");
}

@AfterAll
static void shutdown() {
emf.close();
}
Comment on lines +22 to +25

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Add null check before closing emf for consistency and safety.

If init() fails, emf will be null and shutdown() will throw a NullPointerException. The BookSearchTest class guards against this.

Suggested fix
     `@AfterAll`
     static void shutdown() {
-        emf.close();
+        if (emf != null) emf.close();
     }
🤖 Prompt for AI Agents
In `@src/test/java/org/example/LoanServicesTest.java` around lines 22 - 25,
shutdown() blindly calls emf.close() which can throw NullPointerException if
init() failed and emf is null; add a null check before closing emf (e.g., guard
in shutdown() to only call emf.close() when emf != null) following the same
pattern used in BookSearchTest so shutdown() safely no-ops when emf was never
initialized.


@BeforeEach
void setUp() {
em = emf.createEntityManager();
}

@AfterEach
void tearDown() {
em.close();
}

// test - isBookLoaned funkar utan error
@Test
void isBookLoaned_executes_without_error() {
LoanServices service = new LoanServices();
boolean result = service.isBookLoaned(1L, em);
assertNotNull(result);
}
Comment on lines +39 to +43

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

assertNotNull on primitive boolean is always true.

result is a primitive boolean, which cannot be null. This assertion will always pass regardless of the actual value. Use assertTrue/assertFalse or assertEquals to verify the expected outcome.

Suggested fix
     void isBookLoaned_executes_without_error() {
         LoanServices service = new LoanServices();
-        boolean result = service.isBookLoaned(1L, em);
-        assertNotNull(result);
+        Book book = em.find(Book.class, 1L);
+        Assertions.assertDoesNotThrow(() -> service.isBookLoaned(book != null ? book.getId() : 1L, em));
     }

Or if you want to verify the actual value:

boolean result = service.isBookLoaned(1L, em);
assertFalse(result); // or assertTrue, depending on expected state
🤖 Prompt for AI Agents
In `@src/test/java/org/example/LoanServicesTest.java` around lines 39 - 43, The
test is asserting not-null on a primitive boolean which is meaningless; in the
test method isBookLoaned_executes_without_error, replace the
assertNotNull(result) check with an assertion that verifies the expected boolean
outcome from LoanServices.isBookLoaned(1L, em) (use assertTrue(result) or
assertFalse(result) or assertEquals(expectedBoolean, result) depending on the
expected state) so the test actually verifies the method behavior.


// test - loanBook funkar utan error
@Test
void loanBook_executes_without_error() {
LoanServices service = new LoanServices();
User user = em.find(User.class, 1L);
Book book = em.find(Book.class, 1L);
boolean result = service.loanBook(user, book, em);
assertNotNull(result);
}
Comment on lines +47 to +53

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Handle potential null from em.find and fix assertion.

em.find() returns null if no entity with the given ID exists. Passing null to loanBook will likely cause issues. Additionally, assertNotNull on a primitive boolean is ineffective.

Suggested fix
     `@Test`
     void loanBook_executes_without_error() {
         LoanServices service = new LoanServices();
         User user = em.find(User.class, 1L);
         Book book = em.find(Book.class, 1L);
-        boolean result = service.loanBook(user, book, em);
-        assertNotNull(result);
+        assertNotNull(user, "Test requires User with id=1 in database");
+        assertNotNull(book, "Test requires Book with id=1 in database");
+        Assertions.assertDoesNotThrow(() -> service.loanBook(user, book, em));
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
void loanBook_executes_without_error() {
LoanServices service = new LoanServices();
User user = em.find(User.class, 1L);
Book book = em.find(Book.class, 1L);
boolean result = service.loanBook(user, book, em);
assertNotNull(result);
}
`@Test`
void loanBook_executes_without_error() {
LoanServices service = new LoanServices();
User user = em.find(User.class, 1L);
Book book = em.find(Book.class, 1L);
assertNotNull(user, "Test requires User with id=1 in database");
assertNotNull(book, "Test requires Book with id=1 in database");
Assertions.assertDoesNotThrow(() -> service.loanBook(user, book, em));
}
🤖 Prompt for AI Agents
In `@src/test/java/org/example/LoanServicesTest.java` around lines 47 - 53, The
test loanBook_executes_without_error calls em.find which can return null and
then passes those values into LoanServices.loanBook; guard against null by
asserting fetched entities exist (or create/persist test User and Book fixtures)
before calling loanBook, and replace the ineffective assertNotNull(result)
(primitive boolean) with an appropriate assertion such as assertTrue(result) or
assertFalse(result) depending on expected behavior; reference the test method
loanBook_executes_without_error, the em.find calls, and the
LoanServices.loanBook invocation when making these changes.


// test - returnBook funkar utan error
@Test
void returnBook_executes_without_error() {
LoanServices service = new LoanServices();

User user = em.find(User.class, 1L);
Book book = em.find(Book.class, 1L);

boolean result = service.returnBook(user, book, em);

assertNotNull(result);
}
Comment on lines +57 to +66

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Same issues: potential null entities and ineffective assertion.

Apply the same fixes as loanBook_executes_without_error.

Suggested fix
     `@Test`
     void returnBook_executes_without_error() {
         LoanServices service = new LoanServices();

         User user = em.find(User.class, 1L);
         Book book = em.find(Book.class, 1L);

-        boolean result = service.returnBook(user, book, em);
-
-        assertNotNull(result);
+        assertNotNull(user, "Test requires User with id=1 in database");
+        assertNotNull(book, "Test requires Book with id=1 in database");
+        Assertions.assertDoesNotThrow(() -> service.returnBook(user, book, em));
     }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
void returnBook_executes_without_error() {
LoanServices service = new LoanServices();
User user = em.find(User.class, 1L);
Book book = em.find(Book.class, 1L);
boolean result = service.returnBook(user, book, em);
assertNotNull(result);
}
void returnBook_executes_without_error() {
LoanServices service = new LoanServices();
User user = em.find(User.class, 1L);
Book book = em.find(Book.class, 1L);
assertNotNull(user, "Test requires User with id=1 in database");
assertNotNull(book, "Test requires Book with id=1 in database");
Assertions.assertDoesNotThrow(() -> service.returnBook(user, book, em));
}
🤖 Prompt for AI Agents
In `@src/test/java/org/example/LoanServicesTest.java` around lines 57 - 66, The
test returnBook_executes_without_error should mirror
loanBook_executes_without_error: ensure em.find(...) for User and Book are not
null before calling service.returnBook, and replace the ineffective
assertNotNull(result) with an assertion that validates the expected boolean
outcome (e.g., assertTrue(result) or assertEquals(true, result)). Update
references to the LoanServices.returnBook invocation and the local variables
User user and Book book to add assertions immediately after retrieval so the
test fails clearly if entities are missing.


// test - activeLoans funkar utan error
@Test
void activeLoans_executes_without_error() {
LoanServices service = new LoanServices();
List<Loan> result = service.activeLoans(em.find(User.class, 1L), em);
assertNotNull(result);
}
Comment on lines +68 to +74

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

Handle potential null user from em.find.

em.find(User.class, 1L) may return null if no user exists. Passing null to activeLoans() could cause unexpected behavior or exceptions.

Suggested fix
     `@Test`
     void activeLoans_executes_without_error() {
         LoanServices service = new LoanServices();
-        List<Loan> result = service.activeLoans(em.find(User.class, 1L), em);
+        User user = em.find(User.class, 1L);
+        assertNotNull(user, "Test requires User with id=1 in database");
+        List<Loan> result = service.activeLoans(user, em);
         assertNotNull(result);
     }
🤖 Prompt for AI Agents
In `@src/test/java/org/example/LoanServicesTest.java` around lines 68 - 74, The
test calls em.find(User.class, 1L) directly and may pass null into
LoanServices.activeLoans; first retrieve the user into a variable (e.g., User
user = em.find(...)) and assertNotNull(user) or create/persist a test User with
id 1L before calling LoanServices.activeLoans to guarantee a non-null User;
update the test to call service.activeLoans(user, em) only after ensuring the
user is present so activeLoans and related code cannot receive null input.

}