Skip to content

Commit

Permalink
Add more tests for services
Browse files Browse the repository at this point in the history
Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov committed Oct 20, 2023
1 parent 6be5b33 commit bf3a7ec
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 18 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
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);
}
}
}
13 changes: 0 additions & 13 deletions src/test/java/com/lpvs/service/LPVSStatisticsServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
import com.lpvs.entity.LPVSMember;
import com.lpvs.entity.LPVSPullRequest;
import com.lpvs.entity.enums.Grade;
import com.lpvs.exception.WrongAccessException;
import com.lpvs.repository.LPVSDetectedLicenseRepository;
import com.lpvs.repository.LPVSLicenseRepository;
import com.lpvs.repository.LPVSMemberRepository;
import com.lpvs.repository.LPVSPullRequestRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -36,18 +32,9 @@ public class LPVSStatisticsServiceTest {
@Mock
private LPVSPullRequestRepository pullRequestRepository;

@Mock
private LPVSDetectedLicenseRepository detectedLicenseRepository;

@Mock
private LPVSLoginCheckService loginCheckService;

@Mock
private LPVSLicenseRepository licenseRepository;

@Mock
private LPVSMemberRepository memberRepository;

@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
Expand Down

0 comments on commit bf3a7ec

Please sign in to comment.