-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add unit tests for services (#254)
* feat: Add unit tests for services Signed-off-by: Oleg Kopysov <[email protected]> * Add more tests for services Signed-off-by: Oleg Kopysov <[email protected]> --------- Signed-off-by: Oleg Kopysov <[email protected]>
- Loading branch information
Showing
4 changed files
with
307 additions
and
5 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
116 changes: 116 additions & 0 deletions
116
src/test/java/com/lpvs/service/LPVSLoginCheckServiceTest.java
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 |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/** | ||
* Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. | ||
* | ||
* Use of this source code is governed by a MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
package com.lpvs.service; | ||
|
||
import com.lpvs.entity.LPVSMember; | ||
import com.lpvs.entity.history.HistoryPageEntity; | ||
import com.lpvs.exception.LoginFailedException; | ||
import com.lpvs.repository.LPVSMemberRepository; | ||
import com.lpvs.repository.LPVSPullRequestRepository; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.Mock; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.oauth2.core.user.OAuth2User; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class LPVSLoginCheckServiceTest { | ||
|
||
@Mock | ||
private LPVSPullRequestRepository lpvsPullRequestRepository; | ||
|
||
@Mock | ||
private LPVSMemberRepository memberRepository; | ||
|
||
@Mock | ||
private Authentication authentication; | ||
|
||
private LPVSLoginCheckService loginCheckService; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
lpvsPullRequestRepository = mock(LPVSPullRequestRepository.class); | ||
memberRepository = mock(LPVSMemberRepository.class); | ||
authentication = mock(Authentication.class); | ||
|
||
loginCheckService = new LPVSLoginCheckService(lpvsPullRequestRepository, memberRepository); | ||
} | ||
|
||
@Test | ||
public void testGetOauthLoginMemberMap() { | ||
OAuth2User oAuth2User = mock(OAuth2User.class); | ||
Map<String, Object> attributes = Collections.singletonMap("email", "[email protected]"); | ||
|
||
when(authentication.getPrincipal()).thenReturn(oAuth2User); | ||
when(oAuth2User.getAttributes()).thenReturn(attributes); | ||
|
||
Map<String, Object> result = loginCheckService.getOauthLoginMemberMap(authentication); | ||
|
||
assertNotNull(result); | ||
assertEquals("[email protected]", result.get("email")); | ||
} | ||
|
||
@Test | ||
public void testGetOauthLoginMemberMapWithNullPrincipal() { | ||
when(authentication.getPrincipal()).thenReturn(null); | ||
|
||
Map<String, Object> result = loginCheckService.getOauthLoginMemberMap(authentication); | ||
|
||
assertNull(result); | ||
} | ||
|
||
@Test | ||
public void testLoginVerificationWithNullPrincipal() { | ||
when(authentication.getPrincipal()).thenReturn(null); | ||
|
||
assertThrows(LoginFailedException.class, () -> loginCheckService.loginVerification(authentication)); | ||
} | ||
|
||
@Test | ||
public void testGetMemberFromMemberMap() { | ||
OAuth2User oAuth2User = mock(OAuth2User.class); | ||
Map<String, Object> attributes = new HashMap<>(); | ||
attributes.put("email", "[email protected]"); | ||
attributes.put("provider", "provider"); | ||
|
||
when(authentication.getPrincipal()).thenReturn(oAuth2User); | ||
when(oAuth2User.getAttributes()).thenReturn(attributes); | ||
|
||
when(memberRepository.findByEmailAndProvider("[email protected]", "provider")).thenReturn(Optional.of(new LPVSMember())); | ||
|
||
LPVSMember result = loginCheckService.getMemberFromMemberMap(authentication); | ||
|
||
assertNotNull(result); | ||
} | ||
|
||
@Test | ||
public void testPathCheckOwnType() { | ||
LPVSMember member = new LPVSMember(); | ||
member.setNickname("testNickname"); | ||
Map<String, Object> attributes = new HashMap<>(); | ||
attributes.put("email", "[email protected]"); | ||
attributes.put("provider", "provider"); | ||
OAuth2User oAuth2User = mock(OAuth2User.class); | ||
when(oAuth2User.getAttributes()).thenReturn(attributes); | ||
when(authentication.getPrincipal()).thenReturn(oAuth2User); | ||
when(lpvsPullRequestRepository.findByPullRequestBase("testNickname", null)).thenReturn(Page.empty()); | ||
when(memberRepository.findByEmailAndProvider("[email protected]", "provider")).thenReturn(Optional.of(member)); | ||
|
||
HistoryPageEntity result = loginCheckService.pathCheck("own", "testNickname", null, authentication); | ||
|
||
assertNotNull(result); | ||
} | ||
} |
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
105 changes: 105 additions & 0 deletions
105
src/test/java/com/lpvs/service/LPVSStatisticsServiceTest.java
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* Copyright (c) 2023, Samsung Electronics Co., Ltd. All rights reserved. | ||
* | ||
* Use of this source code is governed by a MIT license that can be | ||
* found in the LICENSE file. | ||
*/ | ||
package com.lpvs.service; | ||
|
||
import com.lpvs.entity.LPVSMember; | ||
import com.lpvs.entity.LPVSPullRequest; | ||
import com.lpvs.entity.enums.Grade; | ||
import com.lpvs.repository.LPVSPullRequestRepository; | ||
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.security.core.Authentication; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class LPVSStatisticsServiceTest { | ||
|
||
@InjectMocks | ||
private LPVSStatisticsService statisticsService; | ||
|
||
@Mock | ||
private LPVSPullRequestRepository pullRequestRepository; | ||
|
||
@Mock | ||
private LPVSLoginCheckService loginCheckService; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
public void testPathCheckOwnType() { | ||
Authentication authentication = mock(Authentication.class); | ||
LPVSMember member = new LPVSMember(); | ||
member.setNickname("testNickname"); | ||
when(loginCheckService.getMemberFromMemberMap(authentication)).thenReturn(member); | ||
when(pullRequestRepository.findByPullRequestBase("testNickname")).thenReturn(new ArrayList<>()); | ||
List<LPVSPullRequest> result = statisticsService.pathCheck("own", "testNickname", authentication); | ||
|
||
assertNotNull(result); | ||
assertEquals(0, result.size()); | ||
} | ||
|
||
@Test | ||
public void testPathCheckOrgType() { | ||
Authentication authentication = mock(Authentication.class); | ||
LPVSMember member = new LPVSMember(); | ||
member.setOrganization("testOrganization"); | ||
when(loginCheckService.getMemberFromMemberMap(authentication)).thenReturn(member); | ||
when(pullRequestRepository.findByPullRequestBase("testOrganization")).thenReturn(Collections.emptyList()); | ||
List<LPVSPullRequest> result = statisticsService.pathCheck("org", "testOrganization", authentication); | ||
|
||
assertNotNull(result); | ||
assertEquals(0, result.size()); | ||
} | ||
|
||
@Test | ||
public void testPathCheckSendType() { | ||
Authentication authentication = mock(Authentication.class); | ||
LPVSMember member = new LPVSMember(); | ||
member.setNickname("testNickname"); | ||
when(loginCheckService.getMemberFromMemberMap(authentication)).thenReturn(member); | ||
when(pullRequestRepository.findBySenderOrPullRequestHead("testNickname")).thenReturn(Collections.emptyList()); | ||
List<LPVSPullRequest> result = statisticsService.pathCheck("send", "testNickname", authentication); | ||
|
||
assertNotNull(result); | ||
assertEquals(0, result.size()); | ||
} | ||
|
||
@Test | ||
public void testGetGradeHigh() { | ||
Grade grade = statisticsService.getGrade("80%"); | ||
assertEquals(Grade.HIGH, grade); | ||
} | ||
|
||
@Test | ||
public void testGetGradeMiddle() { | ||
Grade grade = statisticsService.getGrade("60%"); | ||
assertEquals(Grade.MIDDLE, grade); | ||
} | ||
|
||
@Test | ||
public void testGetGradeLow() { | ||
Grade grade = statisticsService.getGrade("35%"); | ||
assertEquals(Grade.LOW, grade); | ||
} | ||
|
||
@Test | ||
public void testGetGradeNone() { | ||
Grade grade = statisticsService.getGrade("10%"); | ||
assertEquals(Grade.NONE, grade); | ||
} | ||
} |