-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6552 from ita-social-projects/endpoint-achievements
Endpoint achievements
- Loading branch information
Showing
6 changed files
with
153 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package greencity.controller; | ||
|
||
import greencity.enums.AchievementCategoryType; | ||
import greencity.repository.AchievementRepo; | ||
import greencity.service.AchievementService; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
|
@@ -14,6 +15,10 @@ | |
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
|
||
import java.security.Principal; | ||
|
||
import static greencity.ModelUtils.getPrincipal; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.verify; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
|
@@ -24,6 +29,7 @@ | |
class AchievementControllerTest { | ||
private static final String achievementLink = "/achievements"; | ||
private MockMvc mockMvc; | ||
private final Principal principal = getPrincipal(); | ||
|
||
@InjectMocks | ||
private AchievementController achievementController; | ||
|
@@ -40,8 +46,35 @@ void setup() { | |
|
||
@Test | ||
void findAllTest() throws Exception { | ||
mockMvc.perform(get(achievementLink)).andExpect(status().isOk()); | ||
verify(achievementService).findAll(); | ||
mockMvc.perform(get(achievementLink).principal(principal)).andExpect(status().isOk()); | ||
verify(achievementService).findAllByType("[email protected]", null); | ||
} | ||
|
||
@Test | ||
void findAllAchievedTest() throws Exception { | ||
mockMvc.perform(get(achievementLink).principal(principal).param("achievementStatus", "ACHIEVED")) | ||
.andExpect(status().isOk()); | ||
verify(achievementService).findAllByType("[email protected]", "ACHIEVED"); | ||
} | ||
|
||
@Test | ||
void findAllUnAchievedTest() throws Exception { | ||
mockMvc.perform(get(achievementLink).principal(principal).param("achievementStatus", "UNACHIEVED")) | ||
.andExpect(status().isOk()); | ||
verify(achievementService).findAllByType("[email protected]", "UNACHIEVED"); | ||
} | ||
|
||
@Test | ||
void findAllAchievedIgnoreCaseTest() throws Exception { | ||
mockMvc.perform(get(achievementLink).principal(principal).param("achievementStatus", "AchieVED")) | ||
.andExpect(status().isOk()); | ||
verify(achievementService).findAllByType("[email protected]", "AchieVED"); | ||
} | ||
|
||
@Test | ||
void findAllUnAchievedIgnoreCaseTest() throws Exception { | ||
mockMvc.perform(get(achievementLink).principal(principal).param("achievementStatus", "unAchieVED")) | ||
.andExpect(status().isOk()); | ||
verify(achievementService).findAllByType("[email protected]", "unAchieVED"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,7 @@ | |
import greencity.entity.Achievement; | ||
import greencity.entity.AchievementCategory; | ||
|
||
import greencity.entity.User; | ||
import greencity.enums.AchievementCategoryType; | ||
import greencity.enums.AchievementAction; | ||
import greencity.exception.exceptions.NotDeletedException; | ||
|
@@ -71,19 +72,24 @@ class AchievementServiceImplTest { | |
|
||
@Test | ||
void findAllWithEmptyListTest() { | ||
when(userService.findByEmail("[email protected]")).thenReturn(ModelUtils.getUserVO()); | ||
when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); | ||
when(achievementRepo.findAll()).thenReturn(Collections.emptyList()); | ||
List<AchievementVO> findAllResult = achievementService.findAll(); | ||
List<AchievementVO> findAllResult = achievementService.findAllByType("[email protected]", ""); | ||
assertTrue(findAllResult.isEmpty()); | ||
} | ||
|
||
@Test | ||
void findAllWithOneValueInRepoTest() { | ||
Achievement achievement = ModelUtils.getAchievement(); | ||
when(userService.findByEmail("[email protected]")).thenReturn(ModelUtils.getUserVO()); | ||
when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); | ||
when(achievementRepo.findAll()) | ||
.thenReturn(Collections.singletonList(achievement)); | ||
when(modelMapper.map(achievement, AchievementVO.class)) | ||
.thenReturn(ModelUtils.getAchievementVO()); | ||
List<AchievementVO> findAllResult = achievementService.findAll(); | ||
when(userService.findByEmail("[email protected]")).thenReturn(ModelUtils.getUserVO()); | ||
List<AchievementVO> findAllResult = achievementService.findAllByType("[email protected]", ""); | ||
assertEquals(1L, (long) findAllResult.get(0).getId()); | ||
} | ||
|
||
|
@@ -99,6 +105,38 @@ void findAllByPageableTest() { | |
assertEquals(10, pageableAdvancedDto.getTotalElements()); | ||
} | ||
|
||
@Test | ||
void findAllACHIEVEDInRepoTest() { | ||
when(userService.findByEmail("[email protected]")).thenReturn(ModelUtils.getUserVO()); | ||
when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); | ||
when(userAchievementRepo.getUserAchievementByUserId(anyLong())) | ||
.thenReturn(Arrays.asList(ModelUtils.getUserAchievement())); | ||
when(achievementRepo.findById(anyLong())).thenReturn(Optional.of(ModelUtils.getAchievement())); | ||
when(modelMapper.map(ModelUtils.getAchievement(), AchievementVO.class)) | ||
.thenReturn(ModelUtils.getAchievementVO()); | ||
List<AchievementVO> findAllResult = achievementService.findAllByType("[email protected]", "ACHIEVED"); | ||
assertEquals(1L, (long) findAllResult.get(0).getId()); | ||
verify(userService).findByEmail("[email protected]"); | ||
verify(modelMapper).map(ModelUtils.getUserVO(), User.class); | ||
verify(userAchievementRepo).getUserAchievementByUserId(anyLong()); | ||
verify(modelMapper).map(ModelUtils.getAchievement(), AchievementVO.class); | ||
} | ||
|
||
@Test | ||
void findAllUNACHIEVEDInRepoTest() { | ||
when(userService.findByEmail("[email protected]")).thenReturn(ModelUtils.getUserVO()); | ||
when(modelMapper.map(ModelUtils.getUserVO(), User.class)).thenReturn(ModelUtils.getUser()); | ||
when(achievementRepo.searchAchievementsUnAchieved(anyLong())) | ||
.thenReturn(Arrays.asList(ModelUtils.getAchievement())); | ||
when(modelMapper.map(ModelUtils.getAchievement(), AchievementVO.class)) | ||
.thenReturn(ModelUtils.getAchievementVO()); | ||
List<AchievementVO> findAllResult = achievementService.findAllByType("[email protected]", "UNACHIEVED"); | ||
assertEquals(1L, (long) findAllResult.get(0).getId()); | ||
verify(userService).findByEmail("[email protected]"); | ||
verify(modelMapper).map(ModelUtils.getUserVO(), User.class); | ||
verify(achievementRepo).searchAchievementsUnAchieved(anyLong()); | ||
} | ||
|
||
@Test | ||
void saveTest() { | ||
Achievement achievement = ModelUtils.getAchievement(); | ||
|