-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7466d38
commit 1b4c623
Showing
6 changed files
with
197 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...in/java/com/kurttekin/can/job_track/domain/exception/JobApplicationNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.kurttekin.can.job_track.domain.exception; | ||
|
||
public class JobApplicationNotFoundException extends RuntimeException { | ||
public JobApplicationNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
...end/src/main/java/com/kurttekin/can/job_track/domain/exception/UserNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.kurttekin.can.job_track.domain.exception; | ||
|
||
public class UserNotFoundException extends RuntimeException { | ||
public UserNotFoundException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
.../java/com/kurttekin/can/job_track/infrastructure/repository/JobApplicationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
.../src/test/java/com/kurttekin/can/job_track/application/JobApplicationServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package com.kurttekin.can.job_track.application; | ||
|
||
import com.kurttekin.can.job_track.domain.exception.JobApplicationNotFoundException; | ||
import com.kurttekin.can.job_track.domain.model.JobApplication; | ||
import com.kurttekin.can.job_track.domain.model.User; | ||
import com.kurttekin.can.job_track.domain.service.UserService; | ||
import com.kurttekin.can.job_track.infrastructure.repository.JobApplicationRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.time.LocalDate; | ||
import java.util.*; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.*; | ||
|
||
class JobApplicationServiceImplTest { | ||
|
||
@Mock | ||
private JobApplicationRepository jobApplicationRepository; | ||
|
||
@Mock | ||
private UserService userService; | ||
|
||
@InjectMocks | ||
private JobApplicationServiceImpl jobApplicationService; | ||
|
||
private JobApplication jobApplication; | ||
private User user; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
user = new User(); | ||
user.setId(1L); | ||
user.setUsername("testuser"); | ||
|
||
jobApplication = new JobApplication(); | ||
jobApplication.setId(1L); | ||
jobApplication.setCompanyName("Test Company"); | ||
jobApplication.setJobTitle("Test Job"); | ||
jobApplication.setUser(user); | ||
jobApplication.setApplicationDate(LocalDate.now()); | ||
jobApplication.setResponseDate(LocalDate.now()); | ||
jobApplication.setStatus("Applied"); | ||
jobApplication.setComments("No comments"); | ||
} | ||
@Test | ||
void createJobApplication() { | ||
when(jobApplicationRepository.save(any(JobApplication.class))).thenReturn(jobApplication); | ||
|
||
JobApplication createdApplication = jobApplicationService.createJobApplication(jobApplication); | ||
|
||
assertNotNull(createdApplication); | ||
assertEquals("Test Company", createdApplication.getCompanyName()); | ||
verify(jobApplicationRepository, times(1)).save(jobApplication); | ||
} | ||
|
||
@Test | ||
void testFindAllByUserId_NotFound() { | ||
// Mock the repository to return an empty list | ||
when(jobApplicationRepository.findAllByUserId(user.getId())).thenReturn(Collections.emptyList()); | ||
|
||
// Expect the custom exception to be thrown | ||
JobApplicationNotFoundException exception = assertThrows( | ||
JobApplicationNotFoundException.class, | ||
() -> jobApplicationService.findAllByUserId(user.getId()) | ||
); | ||
|
||
// Verify the exception message | ||
assertEquals("Job Applications not found for user ID: " + user.getId(), exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void testFindById_Success() { | ||
when(jobApplicationRepository.findById(1L)).thenReturn(Optional.of(jobApplication)); | ||
|
||
Optional<JobApplication> foundApplication = jobApplicationService.findById(1L); | ||
|
||
assertTrue(foundApplication.isPresent()); | ||
assertEquals("Test Company", foundApplication.get().getCompanyName()); | ||
} | ||
|
||
@Test | ||
void testFindById_NotFound() { | ||
when(jobApplicationRepository.findById(1L)).thenReturn(Optional.empty()); | ||
|
||
Optional<JobApplication> foundApplication = jobApplicationService.findById(1L); | ||
|
||
assertFalse(foundApplication.isPresent()); | ||
} | ||
|
||
@Test | ||
void testUpdateJobApplication() { | ||
when(jobApplicationRepository.findById(1L)).thenReturn(Optional.of(jobApplication)); | ||
when(jobApplicationRepository.save(any(JobApplication.class))).thenReturn(jobApplication); | ||
|
||
jobApplication.setJobTitle("Updated Job Title"); | ||
JobApplication updatedApplication = jobApplicationService.updateJobApplication(jobApplication); | ||
|
||
assertEquals("Updated Job Title", updatedApplication.getJobTitle()); | ||
verify(jobApplicationRepository, times(1)).save(jobApplication); | ||
} | ||
|
||
@Test | ||
void testUpdateJobApplication_NotFound() { | ||
when(jobApplicationRepository.findById(1L)).thenReturn(Optional.empty()); | ||
|
||
Exception exception = assertThrows(RuntimeException.class, () -> { | ||
jobApplicationService.updateJobApplication(jobApplication); | ||
}); | ||
|
||
assertEquals("Job Application not found", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
void testDeleteJobApplication() { | ||
when(jobApplicationRepository.findById(1L)).thenReturn(Optional.of(jobApplication)); | ||
|
||
jobApplicationService.deleteJobApplication(1L); | ||
|
||
verify(jobApplicationRepository, times(1)).deleteById(1L); | ||
} | ||
|
||
@Test | ||
void testDeleteJobApplication_NotFound() { | ||
// Mock the repository to return an empty | ||
// Optional when searching for the JobApplication by ID | ||
when(jobApplicationRepository.findById(1L)).thenReturn(Optional.empty()); | ||
|
||
// Expect the custom exception to be thrown | ||
// when trying to delete a non-existing JobApplication | ||
JobApplicationNotFoundException exception = assertThrows( | ||
JobApplicationNotFoundException.class, | ||
() -> jobApplicationService.deleteJobApplication(1L) | ||
); | ||
|
||
// Verify the exception message | ||
assertEquals("Job Application not found for ID: 1", exception.getMessage()); | ||
|
||
// Verify that the delete method was never called on the repository | ||
verify(jobApplicationRepository, never()).deleteById(1L); | ||
} | ||
|
||
|
||
@Test | ||
void testDeleteAllByUserId() { | ||
jobApplicationService.deleteAllByUserId(user.getId()); | ||
|
||
verify(jobApplicationRepository, times(1)).deleteByUserId(user.getId()); | ||
} | ||
|
||
@Test | ||
void testGetJobApplicationStats() { | ||
when(jobApplicationRepository.findByUserId(user.getId())).thenReturn(Arrays.asList(jobApplication)); | ||
|
||
Map<String, Integer> stats = jobApplicationService.getJobApplicationStats(user.getId()); | ||
|
||
assertEquals(1, stats.get("totalApplications")); | ||
verify(jobApplicationRepository, times(1)).findByUserId(user.getId()); | ||
} | ||
} |