diff --git a/skill-tree /src/main/java/com/RDS/skilltree/Exceptions/GlobalExceptionHandler.java b/skill-tree /src/main/java/com/RDS/skilltree/Exceptions/GlobalExceptionHandler.java deleted file mode 100644 index ce73773f..00000000 --- a/skill-tree /src/main/java/com/RDS/skilltree/Exceptions/GlobalExceptionHandler.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.RDS.skilltree.Exceptions; - -import com.RDS.skilltree.utils.ApiResponse; -import org.springframework.context.support.DefaultMessageSourceResolvable; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.MethodArgumentNotValidException; -import org.springframework.web.bind.annotation.ExceptionHandler; -import org.springframework.web.bind.annotation.RestControllerAdvice; -import org.springframework.web.context.request.WebRequest; - -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -@RestControllerAdvice -public class GlobalExceptionHandler { - @ExceptionHandler(MethodArgumentNotValidException.class) - public ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, - HttpStatus status, WebRequest request) { - Map> body = new HashMap<>(); - - List errors = ex.getBindingResult() - .getFieldErrors() - .stream() - .map(DefaultMessageSourceResolvable::getDefaultMessage) - .collect(Collectors.toList()); - - body.put("errors", errors); - - return new ResponseEntity<>(body, HttpStatus.BAD_REQUEST); - } - - @ExceptionHandler(value = {NoEntityException.class}) - public ResponseEntity resourceNotFoundException(NoEntityException ex) { - String message = ex.getMessage(); - ApiResponse apiResponse = new ApiResponse(message, false, 400); - return new ResponseEntity<>(apiResponse, HttpStatus.NOT_FOUND); - } -} \ No newline at end of file diff --git a/skill-tree /src/main/java/com/RDS/skilltree/Exceptions/NoEntityException.java b/skill-tree /src/main/java/com/RDS/skilltree/Exceptions/NoEntityException.java deleted file mode 100644 index ce4c6cd6..00000000 --- a/skill-tree /src/main/java/com/RDS/skilltree/Exceptions/NoEntityException.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.RDS.skilltree.Exceptions; - -public class NoEntityException extends RuntimeException { - - public NoEntityException(String message) { - super(message); - } - - public NoEntityException(String message, Throwable cause) { - super(message, cause); - } -} \ No newline at end of file diff --git a/skill-tree /src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java b/skill-tree /src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java deleted file mode 100644 index f26fa47f..00000000 --- a/skill-tree /src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.RDS.skilltree.unit; - -import com.RDS.skilltree.Endorsement.EndorsementDRO; -import com.RDS.skilltree.Endorsement.EndorsementModel; -import com.RDS.skilltree.Endorsement.EndorsementRepository; -import com.RDS.skilltree.Endorsement.EndorsementService; -import com.RDS.skilltree.Exceptions.NoEntityException; -import com.RDS.skilltree.Skill.SkillModel; -import com.RDS.skilltree.Skill.SkillRepository; -import com.RDS.skilltree.User.UserModel; -import com.RDS.skilltree.User.UserRepository; -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.util.Optional; -import java.util.UUID; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -class EndorsementServiceTest { - - @Mock - private EndorsementRepository endorsementRepository; - - @Mock - private UserRepository userRepository; - - @Mock - private SkillRepository skillRepository; - - @InjectMocks - private EndorsementService endorsementService; - - @BeforeEach - void setUp() { - MockitoAnnotations.openMocks(this); - } - - @Test - void testCreateEndorsement() { - // Mock data - UUID userId = UUID.randomUUID(); - UUID skillId = UUID.randomUUID(); - EndorsementDRO endorsementDRO = new EndorsementDRO(); - endorsementDRO.setUserId(userId); - endorsementDRO.setSkillId(skillId); - - UserModel mockUser = new UserModel(); - mockUser.setId(userId); - - SkillModel mockSkill = new SkillModel(); - mockSkill.setId(skillId); - - // Mock the repository behavior - when(userRepository.findById(userId)).thenReturn(Optional.of(mockUser)); - when(skillRepository.findById(skillId)).thenReturn(Optional.of(mockSkill)); - - // Call the service method - EndorsementModel result = endorsementService.createEndorsement(endorsementDRO); - - // Verify the interactions - verify(endorsementRepository, times(1)).save(any(EndorsementModel.class)); - - // Assertions - assertNotNull(result); - assertEquals(userId, result.getUser().getId()); - assertEquals(skillId, result.getSkill().getId()); - } - - @Test - void testCreateEndorsementWithInvalidUser() { - UUID userId = UUID.randomUUID(); - UUID skillId = UUID.randomUUID(); - EndorsementDRO endorsementDRO = new EndorsementDRO(); - endorsementDRO.setUserId(userId); - endorsementDRO.setSkillId(skillId); - - // Mock the repository behavior for an invalid user - when(userRepository.findById(userId)).thenReturn(Optional.empty()); - - // Assert that a NoEntityException is thrown - NoEntityException exception = assertThrows(NoEntityException.class, - () -> endorsementService.createEndorsement(endorsementDRO)); - assertEquals("User with id:" + userId + " not found", exception.getMessage()); - - // Verify that save method is not called - verify(endorsementRepository, never()).save(any(EndorsementModel.class)); - } - - @Test - void testCreateEndorsementWithInvalidSkill() { - UUID userId = UUID.randomUUID(); - UUID skillId = UUID.randomUUID(); - EndorsementDRO endorsementDRO = new EndorsementDRO(); - endorsementDRO.setUserId(userId); - endorsementDRO.setSkillId(skillId); - - UserModel mockUser = new UserModel(); - mockUser.setId(userId); - - // Mock the repository behavior for an invalid skill - when(userRepository.findById(userId)).thenReturn(Optional.of(mockUser)); - when(skillRepository.findById(skillId)).thenReturn(Optional.empty()); - - // Assert that a NoEntityException is thrown - NoEntityException exception = assertThrows(NoEntityException.class, - () -> endorsementService.createEndorsement(endorsementDRO)); - assertEquals("Skill with id:" + skillId + " not found", exception.getMessage()); - - // Verify that save method is not called - verify(endorsementRepository, never()).save(any(EndorsementModel.class)); - } -} diff --git a/skill-tree /src/test/java/com/RDS/skilltree/unit/EndorsementListServiceTest.java b/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementListServiceTest.java similarity index 100% rename from skill-tree /src/test/java/com/RDS/skilltree/unit/EndorsementListServiceTest.java rename to skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementListServiceTest.java diff --git a/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java b/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java index 777654d1..7a0d7d96 100644 --- a/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java +++ b/skill-tree/src/test/java/com/RDS/skilltree/unit/EndorsementServiceTest.java @@ -1,5 +1,6 @@ package com.RDS.skilltree.unit; +import com.RDS.skilltree.Endorsement.EndorsementDRO; import com.RDS.skilltree.Endorsement.EndorsementDTO; import com.RDS.skilltree.Endorsement.EndorsementModel; import com.RDS.skilltree.Endorsement.EndorsementRepository; @@ -26,6 +27,12 @@ public class EndorsementServiceTest { @Mock private EndorsementRepository endorsementRepository; + @Mock + private UserRepository userRepository; + + @Mock + private SkillRepository skillRepository; + @InjectMocks @Autowired private EndorsementServiceImpl endorsementService; @@ -67,4 +74,80 @@ public void itShouldHandleEndorsementNotFound() { // Verify the exception message assertEquals("No endorsement with the id " + nonExistentEndorsementId + " found", exception.getMessage()); } + + @Test + void testCreateEndorsement() { + // Mock data + UUID userId = UUID.randomUUID(); + UUID skillId = UUID.randomUUID(); + EndorsementDRO endorsementDRO = new EndorsementDRO(); + endorsementDRO.setUserId(userId); + endorsementDRO.setSkillId(skillId); + + UserModel mockUser = new UserModel(); + mockUser.setId(userId); + + SkillModel mockSkill = new SkillModel(); + mockSkill.setId(skillId); + + // Mock the repository behavior + when(userRepository.findById(userId)).thenReturn(Optional.of(mockUser)); + when(skillRepository.findById(skillId)).thenReturn(Optional.of(mockSkill)); + + // Call the service method + EndorsementModel result = endorsementService.createEndorsement(endorsementDRO); + + // Verify the interactions + verify(endorsementRepository, times(1)).save(any(EndorsementModel.class)); + + // Assertions + assertNotNull(result); + assertEquals(userId, result.getUser().getId()); + assertEquals(skillId, result.getSkill().getId()); + } + + @Test + void testCreateEndorsementWithInvalidUser() { + UUID userId = UUID.randomUUID(); + UUID skillId = UUID.randomUUID(); + EndorsementDRO endorsementDRO = new EndorsementDRO(); + endorsementDRO.setUserId(userId); + endorsementDRO.setSkillId(skillId); + + // Mock the repository behavior for an invalid user + when(userRepository.findById(userId)).thenReturn(Optional.empty()); + + // Assert that a NoEntityException is thrown + NoEntityException exception = assertThrows(NoEntityException.class, + () -> endorsementService.createEndorsement(endorsementDRO)); + assertEquals("User with id:" + userId + " not found", exception.getMessage()); + + // Verify that save method is not called + verify(endorsementRepository, never()).save(any(EndorsementModel.class)); + } + + @Test + void testCreateEndorsementWithInvalidSkill() { + UUID userId = UUID.randomUUID(); + UUID skillId = UUID.randomUUID(); + EndorsementDRO endorsementDRO = new EndorsementDRO(); + endorsementDRO.setUserId(userId); + endorsementDRO.setSkillId(skillId); + + UserModel mockUser = new UserModel(); + mockUser.setId(userId); + + // Mock the repository behavior for an invalid skill + when(userRepository.findById(userId)).thenReturn(Optional.of(mockUser)); + when(skillRepository.findById(skillId)).thenReturn(Optional.empty()); + + // Assert that a NoEntityException is thrown + NoEntityException exception = assertThrows(NoEntityException.class, + () -> endorsementService.createEndorsement(endorsementDRO)); + assertEquals("Skill with id:" + skillId + " not found", exception.getMessage()); + + // Verify that save method is not called + verify(endorsementRepository, never()).save(any(EndorsementModel.class)); + } + } \ No newline at end of file