Skip to content

Commit

Permalink
feat: Add unit tests for services (#254)
Browse files Browse the repository at this point in the history
* 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
o-kopysov authored Oct 23, 2023
1 parent 8e9a3b6 commit 08a9c5d
Show file tree
Hide file tree
Showing 4 changed files with 307 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public void checkForQueue() throws InterruptedException {
}
}

private LPVSQueue getLatestScan(List<LPVSQueue> webhookConfigList) {
public LPVSQueue getLatestScan(List<LPVSQueue> webhookConfigList) {
LPVSQueue latestWebhookConfig = webhookConfigList.get(0);
for (LPVSQueue webhookConfig: webhookConfigList) {
if(latestWebhookConfig.getDate().compareTo(webhookConfig.getDate()) < 0) {
Expand Down
116 changes: 116 additions & 0 deletions src/test/java/com/lpvs/service/LPVSLoginCheckServiceTest.java
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);
}
}
89 changes: 85 additions & 4 deletions src/test/java/com/lpvs/service/LPVSQueueServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,22 @@
* 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.LPVSFile;
import com.lpvs.entity.LPVSLicense;
import com.lpvs.entity.LPVSPullRequest;
import com.lpvs.entity.LPVSQueue;
import com.lpvs.entity.enums.LPVSPullRequestAction;
import com.lpvs.entity.enums.LPVSPullRequestStatus;
import com.lpvs.repository.LPVSPullRequestRepository;
import com.lpvs.repository.LPVSQueueRepository;
import com.lpvs.util.LPVSWebhookUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.mockito.MockitoAnnotations;

import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -680,4 +678,87 @@ public void testProcessWebHook__DeletionAbsentLicenseNull() throws IOException {
verifyNoMoreInteractions(mockLicenseService);
}
}

@Nested
class TestProcessWebHook__queueMethods {

LPVSQueueService queueService;
LPVSGitHubService mockGitHubService;
LPVSDetectService mockDetectService;
LPVSLicenseService mockLicenseService;
LPVSPullRequestRepository mocked_lpvsPullRequestRepository;
LPVSQueueRepository mocked_queueRepository = mock(LPVSQueueRepository.class);

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
queueService = new LPVSQueueService(mockGitHubService,
mockDetectService,
mockLicenseService,
mocked_lpvsPullRequestRepository,
mocked_queueRepository,
4);
}

@Test
public void testCheckForQueue() {
LPVSQueue webhookConfig = new LPVSQueue();
webhookConfig.setAttempts(0);
webhookConfig.setDate(new Date());
when(mocked_queueRepository.getQueueList()).thenReturn(List.of(webhookConfig));
assertDoesNotThrow(() -> queueService.checkForQueue());
verify(mocked_queueRepository).save(webhookConfig);
}

@Test
public void testGetQueueFirstElement() throws InterruptedException {
LPVSQueue queue = new LPVSQueue();
queue.setId(1L);
queue.setAttempts(4);
queue.setAction(LPVSPullRequestAction.OPEN);
queue.setUserId("userId");
queue.setHeadCommitSHA("commitSha");
queue.setPullRequestUrl("url");
queueService.add(queue);
LPVSQueue result = queueService.getQueueFirstElement();
assertNotNull(result);
}

@Test
public void testAddFirst() {
LPVSQueue queue = new LPVSQueue();
assertDoesNotThrow(() -> queueService.addFirst(queue));
}

@Test
public void testAdd() {
LPVSQueue queue = new LPVSQueue();
assertDoesNotThrow(() -> queueService.add(queue));
}

@Test
public void testDelete() {
LPVSQueue queue = new LPVSQueue();
queue.setId(1L);
queue.setAttempts(4);
queue.setAction(LPVSPullRequestAction.OPEN);
queue.setUserId("userId");
queue.setHeadCommitSHA("commitSha");
queue.setPullRequestUrl("url");
assertDoesNotThrow(() -> queueService.delete(queue));
verify(mocked_queueRepository).deleteById(queue.getId());
}

@Test
public void testGetLatestScan() {
LPVSQueue webhookConfig1 = new LPVSQueue();
LPVSQueue webhookConfig2 = new LPVSQueue();
webhookConfig1.setDate(new Date(1));
webhookConfig2.setDate(new Date(2));
List<LPVSQueue> webhookConfigList = List.of(webhookConfig1, webhookConfig2);
LPVSQueue result = queueService.getLatestScan(webhookConfigList);
assertNotNull(result);
assertEquals(webhookConfig2, result);
}
}
}
105 changes: 105 additions & 0 deletions src/test/java/com/lpvs/service/LPVSStatisticsServiceTest.java
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);
}
}

0 comments on commit 08a9c5d

Please sign in to comment.