From 67c1cc781462eb7556064134e69feebaba6325e4 Mon Sep 17 00:00:00 2001 From: can kurttekin Date: Fri, 22 Nov 2024 15:36:15 +0300 Subject: [PATCH] add test resume create or update --- .../presentation/rest/ResumeController.java | 18 +---- .../rest/ResumeControllerTest.java | 72 +++++++++++++++++++ 2 files changed, 73 insertions(+), 17 deletions(-) create mode 100644 backend/src/test/java/com/kurttekin/can/job_track/presentation/rest/ResumeControllerTest.java diff --git a/backend/src/main/java/com/kurttekin/can/job_track/presentation/rest/ResumeController.java b/backend/src/main/java/com/kurttekin/can/job_track/presentation/rest/ResumeController.java index ed692339..a1e1b8dd 100644 --- a/backend/src/main/java/com/kurttekin/can/job_track/presentation/rest/ResumeController.java +++ b/backend/src/main/java/com/kurttekin/can/job_track/presentation/rest/ResumeController.java @@ -36,23 +36,7 @@ public ResponseEntity createOrUpdateResume( return ResponseEntity.status(HttpStatus.CREATED).body(createdOrUpdatedResume); } - - /* - @PostMapping - public ResponseEntity 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 getResume( @AuthenticationPrincipal UserDetails userDetails) { diff --git a/backend/src/test/java/com/kurttekin/can/job_track/presentation/rest/ResumeControllerTest.java b/backend/src/test/java/com/kurttekin/can/job_track/presentation/rest/ResumeControllerTest.java new file mode 100644 index 00000000..307f7862 --- /dev/null +++ b/backend/src/test/java/com/kurttekin/can/job_track/presentation/rest/ResumeControllerTest.java @@ -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 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); + } +} \ No newline at end of file