Skip to content

Commit

Permalink
AB2D-6315 address AB2D unit test and integration test issues (#1403)
Browse files Browse the repository at this point in the history
## 🎫 Ticket

https://jira.cms.gov/browse/AB2D-6315

## 🛠 Changes

Mocked PropertyService in unit and integration tests

## ℹ️ Context
In migrating the AB2D unit and integration tests from Jenkins to GitHub
Actions we are still seeing flakiness with the "Cannot access properties
service, using default database value" and "Cannot find the property
maintenance.mode" errors.

## 🧪 Validation
Failed main branch
https://github.com/CMSgov/ab2d/actions/runs/11355072772/job/31583682078#step:8:23490
This branch:
https://github.com/CMSgov/ab2d/actions/runs/11448554170/job/31852335578
  • Loading branch information
smirnovaae authored Oct 22, 2024
1 parent 44d411d commit dd9e9b5
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@
import gov.cms.ab2d.api.SpringBootApp;
import gov.cms.ab2d.api.controller.common.ApiCommon;
import gov.cms.ab2d.api.remote.JobClientMock;
import gov.cms.ab2d.common.properties.PropertiesService;
import gov.cms.ab2d.common.properties.PropertyServiceStub;
import gov.cms.ab2d.common.util.AB2DPostgresqlContainer;
import gov.cms.ab2d.common.util.AB2DSQSMockConfig;
import gov.cms.ab2d.common.util.DataSetup;
import gov.cms.ab2d.eventclient.clients.SQSEventClient;
import gov.cms.ab2d.eventclient.events.ApiResponseEvent;
import gov.cms.ab2d.eventclient.events.LoggableEvent;
import gov.cms.ab2d.job.model.JobOutput;
import java.util.List;
Expand All @@ -20,12 +21,12 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.testcontainers.containers.PostgreSQLContainer;
Expand All @@ -38,6 +39,7 @@
import static gov.cms.ab2d.common.model.Role.SPONSOR_ROLE;
import static gov.cms.ab2d.common.util.Constants.API_PREFIX_V1;
import static gov.cms.ab2d.common.util.Constants.FHIR_PREFIX;
import static gov.cms.ab2d.common.util.PropertyConstants.MAINTENANCE_MODE;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.springframework.http.HttpHeaders.CONTENT_LOCATION;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand Down Expand Up @@ -70,6 +72,11 @@ public class AdminAPIMaintenanceModeTests {
@Autowired
SQSEventClient sqsEventClient;

@Autowired
private ApplicationContext context;

private final PropertiesService propertiesService = new PropertyServiceStub();

@Captor
private ArgumentCaptor<LoggableEvent> captor;

Expand All @@ -78,6 +85,10 @@ public class AdminAPIMaintenanceModeTests {
@BeforeEach
public void setup() throws JwtVerificationException {
token = testUtil.setupToken(List.of(SPONSOR_ROLE, ADMIN_ROLE));
ApiCommon apiCommon = context.getBean(ApiCommon.class);
ReflectionTestUtils.setField(apiCommon, "propertiesService", propertiesService);
propertiesService.createProperty(MAINTENANCE_MODE, "false");
propertiesService.createProperty("ZipSupportOn", "false");
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@
import com.jayway.jsonpath.JsonPath;
import com.okta.jwt.JwtVerificationException;
import gov.cms.ab2d.api.SpringBootApp;
import gov.cms.ab2d.api.controller.common.ApiCommon;
import gov.cms.ab2d.api.controller.v1.CapabilityStatementSTU3;
import gov.cms.ab2d.api.controller.v2.CapabilityStatementR4;
import gov.cms.ab2d.api.remote.JobClientMock;
import gov.cms.ab2d.common.model.PdpClient;
import gov.cms.ab2d.common.properties.PropertiesService;
import gov.cms.ab2d.common.properties.PropertyServiceStub;
import gov.cms.ab2d.common.repository.PdpClientRepository;
import gov.cms.ab2d.common.service.ContractServiceStub;
import gov.cms.ab2d.common.util.AB2DLocalstackContainer;
Expand Down Expand Up @@ -41,10 +44,12 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
Expand All @@ -64,6 +69,7 @@
import static gov.cms.ab2d.common.util.Constants.FHIR_NDJSON_CONTENT_TYPE;
import static gov.cms.ab2d.common.util.DataSetup.TEST_PDP_CLIENT;
import static gov.cms.ab2d.common.util.DataSetup.VALID_CONTRACT_NUMBER;
import static gov.cms.ab2d.common.util.PropertyConstants.MAINTENANCE_MODE;
import static gov.cms.ab2d.fhir.BundleUtils.EOB;
import static gov.cms.ab2d.fhir.FhirVersion.R4;
import static gov.cms.ab2d.fhir.FhirVersion.STU3;
Expand Down Expand Up @@ -123,6 +129,11 @@ class BulkDataAccessAPIIntegrationTests {
@Autowired
SQSEventClient sqsEventClient;

@Autowired
private ApplicationContext context;

private final PropertiesService propertiesService = new PropertyServiceStub();

private String token;

public static final String PATIENT_EXPORT_PATH = "/Patient/$export";
Expand All @@ -133,6 +144,9 @@ class BulkDataAccessAPIIntegrationTests {
public void setup() throws JwtVerificationException {
token = testUtil.setupToken(List.of(SPONSOR_ROLE));
testUtil.turnMaintenanceModeOff();
ApiCommon apiCommon = context.getBean(ApiCommon.class);
ReflectionTestUtils.setField(apiCommon, "propertiesService", propertiesService);
propertiesService.createProperty(MAINTENANCE_MODE, "false");
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.model.PurgeQueueRequest;
import com.okta.jwt.JwtVerificationException;
import gov.cms.ab2d.api.SpringBootApp;
import gov.cms.ab2d.api.controller.common.ApiCommon;
import gov.cms.ab2d.api.remote.JobClientMock;
import gov.cms.ab2d.common.properties.PropertiesService;
import gov.cms.ab2d.common.properties.PropertyServiceStub;
import gov.cms.ab2d.common.service.ContractServiceStub;
import gov.cms.ab2d.common.util.AB2DLocalstackContainer;
import gov.cms.ab2d.eventclient.clients.SQSConfig;
Expand All @@ -22,8 +26,10 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.testcontainers.containers.PostgreSQLContainer;
Expand All @@ -37,6 +43,7 @@
import static gov.cms.ab2d.common.model.Role.SPONSOR_ROLE;
import static gov.cms.ab2d.common.util.Constants.*;
import static gov.cms.ab2d.common.util.DataSetup.*;
import static gov.cms.ab2d.common.util.PropertyConstants.MAINTENANCE_MODE;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.http.HttpHeaders.CONTENT_LOCATION;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
Expand Down Expand Up @@ -79,6 +86,18 @@ public class BulkDataAccessAPIUnusualDataTests {
@Container
private static final AB2DLocalstackContainer localstackContainer = new AB2DLocalstackContainer();

@Autowired
private ApplicationContext context;

private final PropertiesService propertiesService = new PropertyServiceStub();

@BeforeEach
public void setup() throws JwtVerificationException {
ApiCommon apiCommon = context.getBean(ApiCommon.class);
ReflectionTestUtils.setField(apiCommon, "propertiesService", propertiesService);
propertiesService.createProperty(MAINTENANCE_MODE, "false");
}

@AfterEach
public void cleanup() {
dataSetup.cleanup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import com.okta.jwt.JwtVerificationException;
import gov.cms.ab2d.api.SpringBootApp;
import gov.cms.ab2d.api.controller.common.ApiCommon;
import gov.cms.ab2d.api.remote.JobClientMock;
import gov.cms.ab2d.common.properties.PropertiesService;
import gov.cms.ab2d.common.properties.PropertyServiceStub;
import gov.cms.ab2d.common.repository.PdpClientRepository;
import gov.cms.ab2d.common.service.ContractServiceStub;
import gov.cms.ab2d.common.util.AB2DLocalstackContainer;
Expand All @@ -18,7 +21,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.http.MediaType;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.testcontainers.containers.PostgreSQLContainer;
Expand All @@ -31,6 +36,7 @@
import static gov.cms.ab2d.common.util.Constants.FHIR_PREFIX;
import static gov.cms.ab2d.common.util.DataSetup.TEST_PDP_CLIENT;
import static gov.cms.ab2d.common.util.DataSetup.VALID_CONTRACT_NUMBER;
import static gov.cms.ab2d.common.util.PropertyConstants.MAINTENANCE_MODE;
import static gov.cms.ab2d.fhir.BundleUtils.EOB;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -69,14 +75,22 @@ public class BulkDataAccessAPIV2IntegrationTests {
@Autowired
private DataSetup dataSetup;

@Autowired
private ApplicationContext context;

private String token;

private final PropertiesService propertiesService = new PropertyServiceStub();

public static final String PATIENT_EXPORT_PATH = "/Patient/$export";

@BeforeEach
public void setup() throws JwtVerificationException {
testUtil.turnMaintenanceModeOff();
token = testUtil.setupToken(List.of(SPONSOR_ROLE));
ApiCommon apiCommon = context.getBean(ApiCommon.class);
ReflectionTestUtils.setField(apiCommon, "propertiesService", propertiesService);
propertiesService.createProperty(MAINTENANCE_MODE, "false");
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,14 @@
import gov.cms.ab2d.common.util.AB2DPostgresqlContainer;
import gov.cms.ab2d.common.util.AB2DSQSMockConfig;
import org.hamcrest.core.Is;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
Expand All @@ -41,8 +39,18 @@ public class MaintenanceModeAPITests {
@Autowired
private MockMvc mockMvc;

@Autowired
private ApplicationContext context;

private PropertiesService propertiesService = new PropertyServiceStub();

@BeforeEach
public void setup() {
MaintenanceModeAPI maintenanceModeAPI = context.getBean(MaintenanceModeAPI.class);
ReflectionTestUtils.setField(maintenanceModeAPI, "propertiesService", propertiesService);
propertiesService.createProperty(MAINTENANCE_MODE, "false");
}

@AfterEach
void tearDown() {
propertiesService.updateProperty(MAINTENANCE_MODE, "false");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
import com.amazonaws.services.sqs.AmazonSQS;
import gov.cms.ab2d.api.SpringBootApp;
import gov.cms.ab2d.api.controller.TestUtil;
import gov.cms.ab2d.api.controller.common.ApiCommon;
import gov.cms.ab2d.api.remote.JobClientMock;
import gov.cms.ab2d.common.properties.PropertiesService;
import gov.cms.ab2d.common.properties.PropertyServiceStub;
import gov.cms.ab2d.common.util.AB2DLocalstackContainer;
import gov.cms.ab2d.common.util.AB2DPostgresqlContainer;
import gov.cms.ab2d.common.util.DataSetup;
Expand All @@ -14,8 +17,9 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.context.ApplicationContext;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.testcontainers.containers.PostgreSQLContainer;
Expand All @@ -26,6 +30,7 @@

import static gov.cms.ab2d.common.model.Role.SPONSOR_ROLE;
import static gov.cms.ab2d.common.util.Constants.FHIR_NDJSON_CONTENT_TYPE;
import static gov.cms.ab2d.common.util.PropertyConstants.MAINTENANCE_MODE;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down Expand Up @@ -60,12 +65,20 @@ class StatusAPIV2Test {
@Autowired
SQSEventClient sqsEventClient;

@Autowired
private ApplicationContext context;

private final PropertiesService propertiesService = new PropertyServiceStub();

private String token;

@BeforeEach
public void setup() throws Exception {
token = testUtil.setupToken(List.of(SPONSOR_ROLE));
testUtil.turnMaintenanceModeOff();
ApiCommon apiCommon = context.getBean(ApiCommon.class);
ReflectionTestUtils.setField(apiCommon, "propertiesService", propertiesService);
propertiesService.createProperty(MAINTENANCE_MODE, "false");
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,23 @@
import com.okta.jwt.AccessTokenVerifier;
import com.okta.jwt.JwtVerificationException;
import gov.cms.ab2d.api.SpringBootApp;
import gov.cms.ab2d.api.controller.MaintenanceModeAPI;
import gov.cms.ab2d.api.controller.TestUtil;
import gov.cms.ab2d.common.properties.PropertiesService;
import gov.cms.ab2d.common.properties.PropertyServiceStub;
import gov.cms.ab2d.common.service.PdpClientService;
import gov.cms.ab2d.common.util.AB2DLocalstackContainer;
import gov.cms.ab2d.common.util.AB2DPostgresqlContainer;
import gov.cms.ab2d.common.util.AB2DSQSMockConfig;
import gov.cms.ab2d.common.util.DataSetup;
import gov.cms.ab2d.eventclient.clients.SQSEventClient;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletRequest;
Expand All @@ -34,6 +39,7 @@

import static gov.cms.ab2d.common.model.Role.ADMIN_ROLE;
import static gov.cms.ab2d.common.util.Constants.*;
import static gov.cms.ab2d.common.util.PropertyConstants.MAINTENANCE_MODE;
import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.fail;
Expand Down Expand Up @@ -81,6 +87,18 @@ class JwtAuthenticationFilterTest {
@Autowired
SQSEventClient sqsEventClient;

@Autowired
private ApplicationContext context;

private final PropertiesService propertiesService = new PropertyServiceStub();

@BeforeEach
public void setup() throws JwtVerificationException {
MaintenanceModeAPI maintenanceModeAPI = context.getBean(MaintenanceModeAPI.class);
ReflectionTestUtils.setField(maintenanceModeAPI, "propertiesService", propertiesService);
propertiesService.createProperty(MAINTENANCE_MODE, "false");
}

@AfterEach
public void cleanup() {
dataSetup.cleanup();
Expand Down
7 changes: 7 additions & 0 deletions coverage/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>gov.cms.ab2d</groupId>
<artifactId>common</artifactId>
<version>0.0.1-SNAPSHOT</version>
<type>test-jar</type>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Loading

0 comments on commit dd9e9b5

Please sign in to comment.