Skip to content

Commit

Permalink
test: add test for reloading tables from DB for singlescan
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Rudyk <[email protected]>
  • Loading branch information
m-rudyk committed Mar 5, 2024
1 parent b32302c commit 723f53f
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 20 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/lpvs/service/LPVSLicenseService.java
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ private void init() {
}
}

/**
* This method reloads licenses and license conflicts from database tables if the
* licenses list in case of single scan triggered and used in memory database.
*/
public void reloadFromTables() {
if (licenses.isEmpty()) {
licenses = lpvsLicenseRepository.takeAllLicenses();
Expand Down
105 changes: 85 additions & 20 deletions src/test/java/com/lpvs/service/LPVSLicenseServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,44 @@
*/
package com.lpvs.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.when;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

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.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.test.util.ReflectionTestUtils;

import com.lpvs.entity.LPVSFile;
import com.lpvs.entity.LPVSLicense;
import com.lpvs.entity.LPVSLicenseConflict;
import com.lpvs.entity.LPVSQueue;
import com.lpvs.repository.LPVSLicenseConflictRepository;
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.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mockito;
import org.springframework.test.util.ReflectionTestUtils;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;

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

@Slf4j
@ExtendWith(SystemStubsExtension.class)
public class LPVSLicenseServiceTest {
Expand Down Expand Up @@ -229,6 +235,65 @@ public void testInitDB() {
fail();
}
}

@Test
public void testReloadFromTables()
throws NoSuchFieldException,
SecurityException,
IllegalArgumentException,
IllegalAccessException {

List<LPVSLicense> licenseList = new ArrayList<>();

LPVSLicense license1 =
new LPVSLicense() {
{
setChecklistUrl("");
setAccess("unrviewed");
setSpdxId("Apache-2.0");
}
};
LPVSLicense license2 =
new LPVSLicense() {
{
setChecklistUrl("");
setAccess("unrviewed");
setSpdxId("MIT");
}
};

licenseList.add(license1);
licenseList.add(license2);

when(lpvsLicenseRepository.takeAllLicenses()).thenReturn(licenseList);

LPVSLicenseConflict licenseConflict = new LPVSLicenseConflict();
licenseConflict.setConflictId(1L);
licenseConflict.setConflictLicense(license1);
licenseConflict.setRepositoryLicense(license2);
List<LPVSLicenseConflict> licenseConflictList = new ArrayList<>();
licenseConflictList.add(licenseConflict);

when(lpvsLicenseConflictRepository.takeAllLicenseConflicts())
.thenReturn(licenseConflictList);

Field lpvsLicenseRepositoryField =
LPVSLicenseService.class.getDeclaredField("lpvsLicenseRepository");
lpvsLicenseRepositoryField.setAccessible(true);
lpvsLicenseRepositoryField.set(licenseService, lpvsLicenseRepository);

Field lpvsLicenseConflictRepositoryField =
LPVSLicenseService.class.getDeclaredField("lpvsLicenseConflictRepository");
lpvsLicenseConflictRepositoryField.setAccessible(true);
lpvsLicenseConflictRepositoryField.set(licenseService, lpvsLicenseConflictRepository);

try {
licenseService.reloadFromTables();
} catch (Exception e) {
e.printStackTrace();
fail("Exception occurred: " + e.getMessage());
}
}
}

@Nested
Expand Down

0 comments on commit 723f53f

Please sign in to comment.