Skip to content

Commit

Permalink
feat: Add unit tests for util classes (#248)
Browse files Browse the repository at this point in the history
feat: Add unit tests for util classes

Signed-off-by: Oleg Kopysov <[email protected]>
  • Loading branch information
o-kopysov authored Oct 16, 2023
1 parent 1a6e70a commit c77cc8b
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 109 deletions.
69 changes: 1 addition & 68 deletions src/main/java/com/lpvs/util/LPVSFileUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
* found in the LICENSE file.
*/

package com.lpvs.util;
package com.lpvs.util;

import com.lpvs.entity.LPVSDiffFile;
import com.lpvs.entity.LPVSQueue;
import lombok.extern.slf4j.Slf4j;
import org.kohsuke.github.GHPullRequestFileDetail;
Expand All @@ -22,7 +21,6 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

@Slf4j
Expand Down Expand Up @@ -114,71 +112,6 @@ public static void deleteIfExists (String path) {
}
}

public static List<LPVSDiffFile> parseDiff(String diffString){
LinkedList<LPVSDiffFile> resultFiles = new LinkedList<>();
String[] lines = diffString.split("\n");
log.debug("# of lines: " + lines.length);
for (String line: lines){
if (line.startsWith("+++") || line.startsWith("---")){
if (line.startsWith("---")){
log.debug("line.startsWith(\"---\")");
LPVSDiffFile diffFile = new LPVSDiffFile();
diffFile.setOldFileName(line.replace("--- ", "")
.replace("---", "")
.replace("a/", "")
.replace("b/", ""));
resultFiles.add(diffFile);
}
if (line.startsWith("+++")){
log.debug("line.startsWith(\"+++\")");
resultFiles.getLast().setNewFileName(line.replace("+++ ", "")
.replace("+++", "")
.replace("a/", "")
.replace("b/", ""));
}
} else if (line.startsWith("+")){
log.debug("line.startsWith(\"+\")");
resultFiles.getLast().appendPatchedLine(line.replace("+", ""));
} else {
log.debug("Skip the line.");
}
}
return resultFiles;
}

public static LPVSDiffFile checkFilePath(LPVSDiffFile diffFile){
if (null == diffFile) {
log.error("DiffFile is absent");
return null;
}
if (!diffFile.getNewFileName().equals(diffFile.getOldFileName())){
if (diffFile.getNewFileName().contains("/dev/null") && !diffFile.getOldFileName().contains("/dev/null"))
diffFile.setNewFileName(diffFile.getOldFileName());
if (diffFile.getOldFileName().contains("/dev/null") && !diffFile.getNewFileName().contains("/dev/null"))
diffFile.setOldFileName(diffFile.getNewFileName());
}
return diffFile;
}

public static String saveReviewBotDiffs(String patch, LPVSQueue webhookConfig) {
String directoryPath = LPVSFileUtil.getLocalDirectoryPath(webhookConfig);
deleteIfExists(directoryPath);
boolean result = new File(directoryPath).mkdirs();
if (result) {
List<LPVSDiffFile> parsedDiffs = parseDiff(patch);
for (LPVSDiffFile diffFile: parsedDiffs) {
if (null == diffFile) {
log.error("DiffFile is absent");
continue;
}
log.info(checkFilePath(diffFile).getNewFileName());
log.info(diffFile.getChangedLines().toString());
saveFile(checkFilePath(diffFile).getNewFileName(), directoryPath, diffFile.getChangedLines());
}
}
return directoryPath;
}

public static String getLocalDirectoryPath(LPVSQueue webhookConfig){
if (webhookConfig.getHeadCommitSHA() == null || webhookConfig.getHeadCommitSHA().equals("")){
return System.getProperty("user.home") + "/" + "Projects/" + LPVSWebhookUtil.getRepositoryName(webhookConfig) + "/" + LPVSWebhookUtil.getPullRequestId(webhookConfig);
Expand Down
17 changes: 11 additions & 6 deletions src/main/java/com/lpvs/util/LPVSWebhookUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public static String getRepositoryOrganization(LPVSQueue webhookConfig) {
}

if (null == webhookConfig.getRepositoryUrl()) {
log.error("No repository url info in webhook config");
return "No repository url info in webhook config";
log.error("No repository URL info in webhook config");
return "No repository URL info in webhook config";
}

List<String> url = Arrays.asList(webhookConfig.getRepositoryUrl().split("/"));
Expand All @@ -83,8 +83,8 @@ public static String getRepositoryName(LPVSQueue webhookConfig) {
}

if (null == webhookConfig.getRepositoryUrl()) {
log.error("No repository url info in webhook config");
return "No repository url info in webhook config";
log.error("No repository URL info in webhook config");
return "No repository URL info in webhook config";
}

List<String> url = Arrays.asList(webhookConfig.getRepositoryUrl().split("/"));
Expand All @@ -102,8 +102,13 @@ public static String getPullRequestId(LPVSQueue webhookConfig) {
}

if (null == webhookConfig.getRepositoryUrl()) {
log.error("No repository url info in webhook config");
return "No repository url info in webhook config";
log.error("No repository URL info in webhook config");
return "No repository URL info in webhook config";
}

if (null == webhookConfig.getPullRequestUrl()) {
log.error("Pull Request URL is absent in webhook config");
return "Pull Request URL is absent in webhook config";
}

List<String> url = Arrays.asList(webhookConfig.getPullRequestUrl().split("/"));
Expand Down
34 changes: 0 additions & 34 deletions src/test/java/com/lpvs/util/FileUtilTest.java

This file was deleted.

66 changes: 66 additions & 0 deletions src/test/java/com/lpvs/util/LPVSCommentUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* 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.util;

import com.lpvs.entity.LPVSFile;
import com.lpvs.entity.LPVSQueue;
import com.lpvs.entity.enums.LPVSVcs;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class LPVSCommentUtilTest {

@Mock
private LPVSQueue webhookConfig;

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

@Test
public void testGetMatchedLinesAsLinkAll() {
LPVSFile file = new LPVSFile();
file.setFilePath("exampleFile.txt");
file.setMatchedLines("all");
Mockito.when(LPVSWebhookUtil.getRepositoryUrl(webhookConfig)).thenReturn("https://github.com/repo");
Mockito.when(webhookConfig.getHeadCommitSHA()).thenReturn("headCommitSHA");
String result = LPVSCommentUtil.getMatchedLinesAsLink(webhookConfig, file, LPVSVcs.GITHUB);
assertEquals("<a target=\"_blank\" href=\"https://github.com/repo/blob/headCommitSHA/exampleFile.txt\">all</a>",
result);
}

@Test
public void testGetMatchedLinesAsLinkMultipleLines() {
LPVSFile file = new LPVSFile();
file.setFilePath("exampleFile.txt");
file.setMatchedLines("1-5,7,9-12");
Mockito.when(LPVSWebhookUtil.getRepositoryUrl(webhookConfig)).thenReturn("https://github.com/repo");
Mockito.when(webhookConfig.getHeadCommitSHA()).thenReturn("headCommitSHA");
String result = LPVSCommentUtil.getMatchedLinesAsLink(webhookConfig, file, LPVSVcs.GITHUB);
assertEquals("<a target=\"_blank\" href=\"https://github.com/repo/blob/headCommitSHA/exampleFile.txt#L1L5\">1-5</a>" +
"<a target=\"_blank\" href=\"https://github.com/repo/blob/headCommitSHA/exampleFile.txt#L7\">7</a>" +
"<a target=\"_blank\" href=\"https://github.com/repo/blob/headCommitSHA/exampleFile.txt#L9L12\">9-12</a>",
result);
}

@Test
public void testGetMatchedLinesAsLinkWithNonGitHubVcs() {
LPVSFile file = new LPVSFile();
file.setFilePath("exampleFile.txt");
file.setMatchedLines("all");
Mockito.when(LPVSWebhookUtil.getRepositoryUrl(webhookConfig)).thenReturn("https://gerrit.org/repo");
Mockito.when(webhookConfig.getHeadCommitSHA()).thenReturn("headCommitSHA");
String result = LPVSCommentUtil.getMatchedLinesAsLink(webhookConfig, file, LPVSVcs.GERRIT);
assertEquals("all (https://gerrit.org/repo/blob/headCommitSHA/exampleFile.txt)", result);
}
}
117 changes: 117 additions & 0 deletions src/test/java/com/lpvs/util/LPVSFileUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/**
* Copyright (c) 2022, 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.util;

import com.lpvs.entity.LPVSQueue;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.kohsuke.github.GHPullRequestFileDetail;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.springframework.test.util.ReflectionTestUtils;
import java.util.ArrayList;
import static org.mockito.Mockito.*;

public class LPVSFileUtilTest {

@Test
public void testSaveGithubDiffs() {
GHPullRequestFileDetail detail = new GHPullRequestFileDetail();
LPVSQueue webhookConfig = new LPVSQueue();
webhookConfig.setHeadCommitSHA("aaaa");
webhookConfig.setRepositoryUrl("http://test.com/test/test");
ReflectionTestUtils.setField(detail, "filename", "I_am_a_file");
LPVSFileUtil.saveGithubDiffs(new ArrayList<GHPullRequestFileDetail>(){{
add(detail);
}}, webhookConfig);
ReflectionTestUtils.setField(detail, "patch", "+ a\n- b\n@@ -8,7 +8,6 @@\n c");
Assertions.assertFalse(LPVSFileUtil.saveGithubDiffs(new ArrayList<GHPullRequestFileDetail>(){{
add(detail);
}}, webhookConfig).contains("Projects//aaaa"));
}

@Test
public void testGetLocalDirectoryPathWithHeadCommitSHA() {
LPVSQueue mockWebhookConfig = Mockito.mock(LPVSQueue.class);
Mockito.when(mockWebhookConfig.getHeadCommitSHA()).thenReturn("abcdef123");

try (MockedStatic<LPVSWebhookUtil> mocked_static_file_util =
mockStatic(LPVSWebhookUtil.class)) {
mocked_static_file_util.when(() -> LPVSWebhookUtil.getRepositoryName(Mockito.any()))
.thenReturn("repoName");

String result = LPVSFileUtil.getLocalDirectoryPath(mockWebhookConfig);
String expectedPath = System.getProperty("user.home") + "/Projects/repoName/abcdef123";
assert (result.equals(expectedPath));
}
}

@Test
public void testGetLocalDirectoryPathWithoutHeadCommitSHA() {
LPVSQueue mockWebhookConfig = Mockito.mock(LPVSQueue.class);
Mockito.when(mockWebhookConfig.getHeadCommitSHA()).thenReturn(null);

try (MockedStatic<LPVSWebhookUtil> mocked_static_file_util =
mockStatic(LPVSWebhookUtil.class)) {
mocked_static_file_util.when(() -> LPVSWebhookUtil.getRepositoryName(Mockito.any()))
.thenReturn("repoName");
mocked_static_file_util.when(() -> LPVSWebhookUtil.getPullRequestId(Mockito.any()))
.thenReturn("pullRequestId");

String result = LPVSFileUtil.getLocalDirectoryPath(mockWebhookConfig);
String expectedPath = System.getProperty("user.home") + "/Projects/repoName/pullRequestId";
assert (result.equals(expectedPath));
}
}

@Test
public void testGetScanResultsJsonFilePathWithHeadCommitSHA() {
LPVSQueue mockWebhookConfig = Mockito.mock(LPVSQueue.class);
Mockito.when(mockWebhookConfig.getHeadCommitSHA()).thenReturn("abcdef123");

try (MockedStatic<LPVSWebhookUtil> mocked_static_file_util =
mockStatic(LPVSWebhookUtil.class)) {
mocked_static_file_util.when(() -> LPVSWebhookUtil.getRepositoryName(Mockito.any()))
.thenReturn("repoName");

String result = LPVSFileUtil.getScanResultsJsonFilePath(mockWebhookConfig);
String expectedPath = System.getProperty("user.home") + "/Results/repoName/abcdef123.json";
assert (result.equals(expectedPath));
}
}

@Test
public void testGetScanResultsJsonFilePathWithoutHeadCommitSHA() {
LPVSQueue mockWebhookConfig = Mockito.mock(LPVSQueue.class);
Mockito.when(mockWebhookConfig.getHeadCommitSHA()).thenReturn(null);

try (MockedStatic<LPVSWebhookUtil> mocked_static_file_util =
mockStatic(LPVSWebhookUtil.class)) {
mocked_static_file_util.when(() -> LPVSWebhookUtil.getRepositoryName(Mockito.any()))
.thenReturn("repoName");
mocked_static_file_util.when(() -> LPVSWebhookUtil.getPullRequestId(Mockito.any()))
.thenReturn("pullRequestId");

String result = LPVSFileUtil.getScanResultsJsonFilePath(mockWebhookConfig);
String expectedPath = System.getProperty("user.home") + "/Results/repoName/pullRequestId.json";
assert (result.equals(expectedPath));
}
}

@Test
public void testGetScanResultsDirectoryPath() {
LPVSQueue mockWebhookConfig = Mockito.mock(LPVSQueue.class);
try (MockedStatic<LPVSWebhookUtil> mocked_static_file_util =
mockStatic(LPVSWebhookUtil.class)) {
mocked_static_file_util.when(() -> LPVSWebhookUtil.getRepositoryName(Mockito.any()))
.thenReturn("repoName");
String result = LPVSFileUtil.getScanResultsDirectoryPath(mockWebhookConfig);
String expectedPath = System.getProperty("user.home") + "/Results/repoName";
assert (result.equals(expectedPath));
}
}
}
Loading

0 comments on commit c77cc8b

Please sign in to comment.