Skip to content

Commit

Permalink
add test resume create or update
Browse files Browse the repository at this point in the history
  • Loading branch information
cankurttekin committed Nov 22, 2024
1 parent dd521e5 commit 67c1cc7
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,7 @@ public ResponseEntity<ResumeDTO> createOrUpdateResume(

return ResponseEntity.status(HttpStatus.CREATED).body(createdOrUpdatedResume);
}

/*
@PostMapping
public ResponseEntity<ResumeDTO> createResume(
@RequestBody Resume resume,
@AuthenticationPrincipal UserDetails userDetails) {
String username = userDetails.getUsername();
User user = userService.findUserByUsername(username)
.orElseThrow(() -> new RuntimeException("User not found"));
resume.setUser(user); // Set the user on the resume
ResumeDTO createdResume = resumeService.createResume(resume);
return ResponseEntity.status(HttpStatus.CREATED).body(createdResume);
}
*/

@GetMapping
public ResponseEntity<ResumeDTO> getResume(
@AuthenticationPrincipal UserDetails userDetails) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.kurttekin.can.job_track.presentation.rest;

import com.kurttekin.can.job_track.application.dto.ResumeDTO;
import com.kurttekin.can.job_track.domain.model.resume.Resume;
import com.kurttekin.can.job_track.domain.model.user.User;
import com.kurttekin.can.job_track.domain.service.ResumeService;
import com.kurttekin.can.job_track.domain.service.UserService;
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 org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Optional;

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

class ResumeControllerTest {

@InjectMocks
private ResumeController resumeController;

@Mock
private ResumeService resumeService;

@Mock
private UserService userService;

private User user;
private UserDetails userDetails;
private Resume resume;
private ResumeDTO resumeDTO;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
// Create mock User and Resume
user = new User();
user.setId(1L);
user.setUsername("testuser");

userDetails = mock(UserDetails.class);
when(userDetails.getUsername()).thenReturn(user.getUsername());

resume = new Resume();
resume.setTitle("Software Developer but unemployed");
resumeDTO = new ResumeDTO();
resumeDTO.setTitle("Software Developer but unemployed");
}

@Test
public void testCreateOrUpdateResume_Success() {
// Mock userService behavior
when(userService.findUserByUsername(user.getUsername())).thenReturn(Optional.of(user));

// Mock resumeService behavior
when(resumeService.createOrUpdateResume(resume)).thenReturn(resumeDTO);

// Call the controller method
ResponseEntity<ResumeDTO> response = resumeController.createOrUpdateResume(resume, userDetails);

// Verify and assert
assertEquals(HttpStatus.CREATED, response.getStatusCode());
assertEquals(resumeDTO, response.getBody());
verify(userService).findUserByUsername(user.getUsername());
verify(resumeService).createOrUpdateResume(resume);
}
}

0 comments on commit 67c1cc7

Please sign in to comment.