-
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
util
classes (#248)
feat: Add unit tests for util classes Signed-off-by: Oleg Kopysov <[email protected]>
- Loading branch information
Showing
6 changed files
with
237 additions
and
109 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
This file was deleted.
Oops, something went wrong.
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,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); | ||
} | ||
} |
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,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)); | ||
} | ||
} | ||
} |
Oops, something went wrong.