Skip to content

Commit

Permalink
chore: surefire and pioneer replaced with systemstubs to be able to c…
Browse files Browse the repository at this point in the history
…hange environmemt vars
  • Loading branch information
rnd4you committed Jan 4, 2024
1 parent 4ff769c commit ef7416b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 41 deletions.
28 changes: 4 additions & 24 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
<version>3.2.1</version>
</parent>


<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>17</maven.compiler.source>
Expand Down Expand Up @@ -69,9 +68,9 @@
<version>1.318</version>
</dependency>
<dependency>
<groupId>org.junit-pioneer</groupId>
<artifactId>junit-pioneer</artifactId>
<version>2.2.0</version>
<groupId>uk.org.webcompere</groupId>
<artifactId>system-stubs-jupiter</artifactId>
<version>2.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -85,14 +84,6 @@
<artifactId>commons-codec</artifactId>
<version>1.15</version>
</dependency>

<!-- Support of static mocks present only in mockito-inline, not mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
Expand Down Expand Up @@ -132,7 +123,6 @@
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!--version>2.7.9</version-->
<dependencies>
<!-- The following enables the "thin jar" deployment option. -->
<dependency>
Expand All @@ -142,16 +132,6 @@
</dependency>
</dependencies>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<argLine>
--add-opens java.base/java.util=ALL-UNNAMED
--add-opens java.base/java.lang=ALL-UNNAMED
</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
Expand All @@ -177,7 +157,7 @@
<artifactId>maven-compiler-plugin</artifactId>
<version>3.12.1</version>
<configuration>
<release>11</release>
<release>17</release>
<showWarnings>true</showWarnings>
<showDeprecation>true</showDeprecation>
<compilerArgs>
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/lpvs/service/LPVSQueueService.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class LPVSQueueService {
* @param queueRepository Repository for storing LPVSQueue entities.
* @param maxAttempts Maximum attempts for processing LPVSQueue elements.
*/
@Autowired
public LPVSQueueService(
LPVSGitHubService gitHubService,
LPVSDetectService detectService,
Expand Down Expand Up @@ -267,7 +266,7 @@ public void processWebHook(LPVSQueue webhookConfig) throws IOException {
gitHubService.commentResults(webhookConfig, null, null, pullRequest);
delete(webhookConfig);
throw new Exception(
"Files are not found. Probably pull request is not exists. Terminating.");
"Files are not found. Probably pull request does not exist. Terminating.");
}
delete(webhookConfig);
} catch (Exception | Error e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@

import com.lpvs.util.LPVSExitHandler;
import lombok.extern.slf4j.Slf4j;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

Expand All @@ -23,8 +27,12 @@
import static org.mockito.Mockito.mock;

@Slf4j
@ExtendWith(SystemStubsExtension.class)
public class GitHubWebhooksControllerTest {

@SystemStub
private EnvironmentVariables environmentVars;

private static final String SIGNATURE = "X-Hub-Signature-256";
private static final String SUCCESS = "Success";
private static final String ERROR = "Error";
Expand Down Expand Up @@ -71,7 +79,6 @@ public void noPayloadTest() {
@Test
public void okTest() {
ResponseEntity<LPVSResponseWrapper> actual;
LPVSQueueRepository queueRepository;

String json_to_test =
"{"
Expand Down Expand Up @@ -113,18 +120,19 @@ public void okTest() {
} catch (Exception e) {
log.error(e.getMessage());
actual = null;
fail();
}
ResponseEntity<LPVSResponseWrapper> expected =
new ResponseEntity<>(new LPVSResponseWrapper(SUCCESS), HttpStatus.OK);
assertEquals(expected.toString().substring(0, 42), actual.toString().substring(0, 42));
}

@Test
@SetEnvironmentVariable(key = "LPVS_GITHUB_SECRET", value = "LPVS")
public void wrongSecretTest() {

String signature =
"sha256=c0ca451d2e2a7ea7d50bb29383996a35f43c7a9df0810bd6ffc45cefc8d1ce42";
environmentVars.set("LPVS_GITHUB_SECRET", "LPVS");

String signature = "sha256=c0ca451d2e2a7ea7d50bb29383996a35f43c7a9df0810bd6ffc45cefc8d1ce42";

String json_to_test =
"{"
Expand Down
21 changes: 17 additions & 4 deletions src/test/java/com/lpvs/service/LPVSGitHubServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import com.lpvs.util.LPVSFileUtil;
import com.lpvs.util.LPVSWebhookUtil;
import lombok.extern.slf4j.Slf4j;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kohsuke.github.*;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
Expand All @@ -44,12 +48,17 @@
import java.util.Set;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verifyNoMoreInteractions;

@Slf4j
@ExtendWith(SystemStubsExtension.class)
public class LPVSGitHubServiceTest {

@SystemStub
private EnvironmentVariables environmentVars;

/**
* Helper class to mock `GHPullRequest`, because we cannot mock it via Mockito.
* There is exception when we try to mock `getUrl()` method:
Expand Down Expand Up @@ -4249,9 +4258,10 @@ public void testCommentResults() throws IOException {
}

@Test
@SetEnvironmentVariable(key = "LPVS_GITHUB_TOKEN", value = "GitHubTokenValue")
public void testSetGithubTokenFromEnv_WhenEnvVariableIsSet()
throws IllegalAccessException, NoSuchFieldException {

environmentVars.set("LPVS_GITHUB_TOKEN", "GitHubTokenValue");
String githubTokenValue = "GitHubTokenValue";
lpvsGitHubConnectionService.setGithubTokenFromEnv();
Field field =
Expand All @@ -4264,6 +4274,7 @@ public void testSetGithubTokenFromEnv_WhenEnvVariableIsSet()
@Test
public void testSetGithubTokenFromEnv_WhenEnvVariableIsNotSet()
throws NoSuchFieldException, IllegalAccessException {
environmentVars.remove("LPVS_GITHUB_TOKEN");
lpvsGitHubConnectionService.setGithubTokenFromEnv();
Field field =
lpvsGitHubConnectionService.getClass().getDeclaredField("GITHUB_AUTH_TOKEN");
Expand All @@ -4282,9 +4293,11 @@ public void setUp() {
}

@Test
@SetEnvironmentVariable(key = "LPVS_GITHUB_TOKEN", value = "")
public void testCheckEmpty()
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {

environmentVars.set("LPVS_GITHUB_TOKEN", "");

LPVSPullRequestRepository mocked_pullRequestRepository =
mock(LPVSPullRequestRepository.class);
LPVSDetectedLicenseRepository mocked_lpvsDetectedLicenseRepository =
Expand Down
14 changes: 11 additions & 3 deletions src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
import com.lpvs.repository.LPVSLicenseRepository;
import com.lpvs.util.LPVSExitHandler;
import lombok.extern.slf4j.Slf4j;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.test.util.ReflectionTestUtils;

Expand All @@ -35,8 +39,12 @@
import static org.junit.jupiter.api.Assertions.*;

@Slf4j
@ExtendWith(SystemStubsExtension.class)
public class LPVSLicenseServiceTest {

@SystemStub
private EnvironmentVariables environmentVars;

private LPVSExitHandler exitHandler;

@Test
Expand Down Expand Up @@ -154,8 +162,8 @@ class TestInit {
Mockito.mock(LPVSLicenseConflictRepository.class);

@Test
@SetEnvironmentVariable(key = "LPVS_LICENSE_CONFLICT", value = "scanner")
public void testInit() {
environmentVars.set("LPVS_LICENSE_CONFLICT", "scanner");
try {
Method init_method = licenseService.getClass().getDeclaredMethod("init");
init_method.setAccessible(true);
Expand All @@ -167,8 +175,8 @@ public void testInit() {
}

@Test
@SetEnvironmentVariable(key = "LPVS_LICENSE_CONFLICT", value = "db")
public void testInitDB() {
environmentVars.set("LPVS_LICENSE_CONFLICT", "db");
try {
LPVSLicense license1 =
new LPVSLicense() {
Expand Down
15 changes: 12 additions & 3 deletions src/test/java/com/lpvs/util/LPVSWebhookUtilTestFuzzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,24 @@
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
import com.code_intelligence.jazzer.junit.FuzzTest;
import com.lpvs.entity.LPVSQueue;
import org.junitpioneer.jupiter.SetEnvironmentVariable;
import org.junit.jupiter.api.extension.ExtendWith;

import static org.junit.jupiter.api.Assertions.*;
import uk.org.webcompere.systemstubs.environment.EnvironmentVariables;
import uk.org.webcompere.systemstubs.jupiter.SystemStub;
import uk.org.webcompere.systemstubs.jupiter.SystemStubsExtension;

import static org.junit.jupiter.api.Assertions.*;
@ExtendWith(SystemStubsExtension.class)
public class LPVSWebhookUtilTestFuzzer {

@SystemStub
private EnvironmentVariables environmentVars;

@FuzzTest(maxDuration = "60s")
@SetEnvironmentVariable(key = "JAZZER_FUZZ", value = "1")
public void fuzzTestGetPullRequestId(FuzzedDataProvider data) {

environmentVars.set("JAZZER_FUZZ", "1");

System.setProperty("JAZZER_FUZZ", "1");

LPVSQueue webhookConfig = new LPVSQueue();
Expand Down

0 comments on commit ef7416b

Please sign in to comment.