Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AB2D-6190] increase worker test coverage #1370

Merged
merged 5 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.util.ReflectionTestUtils;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import static gov.cms.ab2d.common.util.PropertyConstants.MAINTENANCE_MODE;
import static gov.cms.ab2d.common.util.PropertyConstants.PCP_MAX_POOL_SIZE;
import static org.junit.Assert.assertFalse;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

// Set property.change.detection to false, otherwise the values from the database will override the values that are being hardcoded here.
@SpringBootTest(properties = {"pcp.core.pool.size=3", "pcp.max.pool.size=20", "pcp.scaleToMax.time=20", "property.change.detection=false"})
Expand All @@ -51,7 +54,7 @@ public class AutoScalingServiceTest {
@Autowired
private ApplicationContext context;

private AutoScalingService autoScalingService;
private AutoScalingServiceImpl autoScalingService;

@Autowired
private RoundRobinBlockingQueue eobClaimRequestsQueue;
Expand All @@ -73,7 +76,6 @@ public void init() {
eobClaimRequestsQueue, propertiesService, 3, 20, 20);
originalMaxPoolSize = autoScalingService.getMaxPoolSize();
patientProcessorThreadPool.setMaxPoolSize(originalMaxPoolSize);

}

@AfterEach
Expand Down Expand Up @@ -189,6 +191,35 @@ void autoScalingKicksInAndResizes() throws InterruptedException {
assertEquals(0, patientProcessorThreadPool.getThreadPoolExecutor().getActiveCount());
}

@Test
void testGetIntProperty() {
PropertiesService mockPropertiesService = mock(PropertiesService.class);
AutoScalingServiceImpl mockedAutoScalingService = new AutoScalingServiceImpl(
patientProcessorThreadPool, eobClaimRequestsQueue, mockPropertiesService, 3, 20, 20
);
when(mockPropertiesService.getProperty(anyString(), anyString())).thenThrow(NullPointerException.class);
assertEquals(99, mockedAutoScalingService.getIntProperty("property", 99));
}

@Test
void testGetDoubleProperty() {
PropertiesService mockPropertiesService = mock(PropertiesService.class);
AutoScalingServiceImpl mockedAutoScalingService = new AutoScalingServiceImpl(
patientProcessorThreadPool, eobClaimRequestsQueue, mockPropertiesService, 3, 20, 20
);
when(mockPropertiesService.getProperty(anyString(), anyString())).thenThrow(NullPointerException.class);
assertEquals(99, mockedAutoScalingService.getDoubleProperty("property", 99));
}

@Test
void testGetBooleanProperty() {
PropertiesService mockPropertiesService = mock(PropertiesService.class);
AutoScalingServiceImpl mockedAutoScalingService = new AutoScalingServiceImpl(
patientProcessorThreadPool, eobClaimRequestsQueue, mockPropertiesService, 3, 20, 20
);
when(mockPropertiesService.isToggleOn(anyString(), anyBoolean())).thenThrow(NullPointerException.class);
assertFalse(mockedAutoScalingService.getBooleanProperty("property", false));
}

private Runnable sleepyRunnable() {
return () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import gov.cms.ab2d.job.model.Job;
import gov.cms.ab2d.job.model.JobStatus;
import gov.cms.ab2d.common.service.FeatureEngagement;
import gov.cms.ab2d.common.service.ResourceNotFoundException;
import gov.cms.ab2d.worker.service.WorkerService;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.integration.support.locks.LockRegistry;
import org.springframework.messaging.MessagingException;
import org.springframework.messaging.support.GenericMessage;


Expand All @@ -18,6 +20,7 @@
import java.util.concurrent.locks.ReentrantLock;

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

@ExtendWith(MockitoExtension.class)
Expand Down Expand Up @@ -51,6 +54,25 @@ void processingNotTriggeredInNeutral() {
verify(workerService, times(0)).process(anyString());
}

@Test
void testEmptyPayload() {

ReentrantLock lock = new ReentrantLock();
when(workerService.getEngagement()).thenReturn(FeatureEngagement.IN_GEAR);

JobHandler jobHandler = new JobHandler(lockRegistry, workerService);

List<Map<String, Object>> payload = List.of();

jobHandler.handleMessage(new GenericMessage<>(payload));

assertFalse(lock.isLocked());

verify(workerService, times(1)).getEngagement();
verify(workerService, times(0)).process(anyString());
}


@DisplayName("Job is started if worker is set to in gear")
@Test
void processingTriggeredInGear() {
Expand Down Expand Up @@ -78,6 +100,32 @@ void processingTriggeredInGear() {
verify(workerService, times(1)).process(anyString());
}

@Test
void testResourceNotFoundException() {
Job submittedJob = new Job();
submittedJob.setStatus(JobStatus.IN_PROGRESS);

ReentrantLock lock = new ReentrantLock();
when(workerService.getEngagement()).thenReturn(FeatureEngagement.IN_GEAR);
when(lockRegistry.obtain(anyString())).thenReturn(lock);
when(workerService.process(anyString())).thenThrow(ResourceNotFoundException.class);

JobHandler jobHandler = new JobHandler(lockRegistry, workerService);

Map<String, Object> jobMap = new HashMap<>() {{
put("job_uuid", "DoesNotMatter");
}};
List<Map<String, Object>> payload = List.of(jobMap);
GenericMessage<List<Map<String, Object>>> message = new GenericMessage<>(payload);

assertThrows(
MessagingException.class,
() -> {
jobHandler.handleMessage(message);
}
);
}

@DisplayName("Handler attempts to start jobs until it finds one that it can start")
@Test
void processUntilSuccessfulForAJob() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
package gov.cms.ab2d.worker.config;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

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

@Slf4j
public class RoundRobinBlockingQueueTest {
class RoundRobinBlockingQueueTest {


@Test
Expand All @@ -31,7 +28,7 @@ void testUnsupported() {
}

@Test
void add() throws FileNotFoundException {
void add() {
RoundRobinBlockingQueue<Object> queue = new RoundRobinBlockingQueue<>();
String contract1 = "0001";
RoundRobinBlockingQueue.CATEGORY_HOLDER.set(contract1);
Expand Down Expand Up @@ -60,7 +57,7 @@ void add() throws FileNotFoundException {
}

@Test
void drainTo() throws FileNotFoundException {
void drainTo() {
RoundRobinBlockingQueue<Object> queue = new RoundRobinBlockingQueue<>();
String contract1 = "0001";
RoundRobinBlockingQueue.CATEGORY_HOLDER.set(contract1);
Expand All @@ -80,7 +77,7 @@ void drainTo() throws FileNotFoundException {

List<Object> returnedVal = new ArrayList<>();
int count = queue.drainTo(returnedVal);
assertEquals(count, 5);
assertEquals(5, count);
assertEquals(5, returnedVal.size());
assertEquals(future1, returnedVal.get(0));
assertEquals(future2, returnedVal.get(1));
Expand All @@ -90,10 +87,14 @@ void drainTo() throws FileNotFoundException {
assertEquals(0, queue.size());
assertTrue(queue.isEmpty());
RoundRobinBlockingQueue.CATEGORY_HOLDER.remove();

assertThrows(IllegalArgumentException.class, () -> {
queue.drainTo(queue);
});
}

@Test
void remove() throws FileNotFoundException {
void remove() {
RoundRobinBlockingQueue<Object> queue = new RoundRobinBlockingQueue<>();
String contract1 = "0001";
assertThrows(NoSuchElementException.class, () -> queue.remove());
Expand All @@ -115,7 +116,7 @@ void remove() throws FileNotFoundException {
}

@Test
void testOffer() throws FileNotFoundException, InterruptedException {
void testOffer() throws InterruptedException {
RoundRobinBlockingQueue<Object> queue = new RoundRobinBlockingQueue<>();
String contract1 = "0001";
RoundRobinBlockingQueue.CATEGORY_HOLDER.set(contract1);
Expand Down Expand Up @@ -143,7 +144,13 @@ void testOffer() throws FileNotFoundException, InterruptedException {
}

@Test
void peek() throws FileNotFoundException, InterruptedException {
void testToString() {
RoundRobinBlockingQueue<Object> queue = new RoundRobinBlockingQueue<>();
assertEquals("RoundRobinBlockingQueue: ", queue.toString());
}

@Test
void peek() throws InterruptedException {
RoundRobinBlockingQueue<Object> queue = new RoundRobinBlockingQueue<>();
String contract1 = "0001";
RoundRobinBlockingQueue.CATEGORY_HOLDER.set(contract1);
Expand All @@ -162,7 +169,7 @@ void peek() throws FileNotFoundException, InterruptedException {
}

@Test
void peekAgain() throws FileNotFoundException, InterruptedException {
void peekAgain() {
RoundRobinBlockingQueue<Object> queue = new RoundRobinBlockingQueue<>();
String contract1 = "0001";
String contract2 = "0002";
Expand All @@ -185,4 +192,4 @@ void peekAgain() throws FileNotFoundException, InterruptedException {
assertEquals(future2, queue.peek());
RoundRobinBlockingQueue.CATEGORY_HOLDER.remove();
}
}
}
Loading