Skip to content

Commit

Permalink
merge the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bhtibrewal committed Dec 29, 2023
1 parent ba72dd8 commit 08bf445
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 171 deletions.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,6 +27,12 @@ public class EndorsementServiceTest {
@Mock
private EndorsementRepository endorsementRepository;

@Mock
private UserRepository userRepository;

@Mock
private SkillRepository skillRepository;

@InjectMocks
@Autowired
private EndorsementServiceImpl endorsementService;
Expand Down Expand Up @@ -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));
}

}

0 comments on commit 08bf445

Please sign in to comment.