Skip to content

Commit

Permalink
job application service tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cankurttekin committed Oct 15, 2024
1 parent 7466d38 commit 1b4c623
Show file tree
Hide file tree
Showing 6 changed files with 197 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
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.service.JobApplicationService;
import com.kurttekin.can.job_track.infrastructure.repository.JobApplicationRepository;
Expand All @@ -24,7 +25,13 @@ public JobApplication createJobApplication(JobApplication jobApplication) {

@Override
public List<JobApplication> findAllByUserId(Long userId) {
return jobApplicationRepository.findAllByUserId(userId);
List<JobApplication> applications = jobApplicationRepository.findAllByUserId(userId);

if (applications.isEmpty()) {
throw new JobApplicationNotFoundException("Job Applications not found for user ID: " + userId);
}

return applications;
}

@Override
Expand All @@ -39,7 +46,12 @@ public JobApplication updateJobApplication(JobApplication jobApplication) {

@Override
public void deleteJobApplication(Long id) {
jobApplicationRepository.deleteById(id);
// Check if the job application exists
JobApplication jobApplication = jobApplicationRepository.findById(id)
.orElseThrow(() -> new JobApplicationNotFoundException("Job Application not found for ID: " + id));

// If found, proceed with deletion
jobApplicationRepository.delete(jobApplication);
}

@Override
Expand Down
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);
}
}
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Optional;

public interface JobApplicationService {
// TO DO custom exceptions
JobApplication createJobApplication(JobApplication jobApplication);
List<JobApplication> findAllByUserId(Long userId);
Optional<JobApplication> findById(Long id);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.kurttekin.can.job_track.infrastructure.repository;

import com.kurttekin.can.job_track.domain.exception.JobApplicationNotFoundException;
import com.kurttekin.can.job_track.domain.model.JobApplication;
import org.springframework.data.jpa.repository.JpaRepository;

import java.time.LocalDate;
import java.util.List;

public interface JobApplicationRepository extends JpaRepository<JobApplication, Long> {
List<JobApplication> findAllByUserId(Long userId);
List<JobApplication> findAllByUserId(Long userId) throws JobApplicationNotFoundException;

void deleteByUserId(Long userId);
//List<JobApplication> findByUserId(Long userId);
Expand Down
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());
}
}

0 comments on commit 1b4c623

Please sign in to comment.