From dd9e9b508037f73e04dc24211895cda74d2699ee Mon Sep 17 00:00:00 2001 From: Anna Smirnova <132938234+smirnovaae@users.noreply.github.com> Date: Tue, 22 Oct 2024 11:49:21 -0700 Subject: [PATCH] AB2D-6315 address AB2D unit test and integration test issues (#1403) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 🎫 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 --- .../AdminAPIMaintenanceModeTests.java | 17 ++- .../BulkDataAccessAPIIntegrationTests.java | 14 ++ .../BulkDataAccessAPIUnusualDataTests.java | 19 +++ .../BulkDataAccessAPIV2IntegrationTests.java | 14 ++ .../controller/MaintenanceModeAPITests.java | 18 ++- .../api/controller/v2/StatusAPIV2Test.java | 15 ++- .../security/JwtAuthenticationFilterTest.java | 18 +++ coverage/pom.xml | 7 + .../service/CoverageServiceExceptionTest.java | 120 ++++++++++++++++++ .../service/CoverageServiceImplTest.java | 56 +++----- .../resources/application.coverage.properties | 2 + .../worker/config/AutoScalingServiceTest.java | 12 +- 12 files changed, 259 insertions(+), 53 deletions(-) create mode 100644 coverage/src/test/java/gov/cms/ab2d/coverage/service/CoverageServiceExceptionTest.java diff --git a/api/src/test/java/gov/cms/ab2d/api/controller/AdminAPIMaintenanceModeTests.java b/api/src/test/java/gov/cms/ab2d/api/controller/AdminAPIMaintenanceModeTests.java index 6e01533dee..69430b221d 100644 --- a/api/src/test/java/gov/cms/ab2d/api/controller/AdminAPIMaintenanceModeTests.java +++ b/api/src/test/java/gov/cms/ab2d/api/controller/AdminAPIMaintenanceModeTests.java @@ -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; @@ -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; @@ -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; @@ -70,6 +72,11 @@ public class AdminAPIMaintenanceModeTests { @Autowired SQSEventClient sqsEventClient; + @Autowired + private ApplicationContext context; + + private final PropertiesService propertiesService = new PropertyServiceStub(); + @Captor private ArgumentCaptor captor; @@ -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 diff --git a/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIIntegrationTests.java b/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIIntegrationTests.java index b5743ac4c2..332d33d7cb 100644 --- a/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIIntegrationTests.java +++ b/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIIntegrationTests.java @@ -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; @@ -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; @@ -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; @@ -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"; @@ -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 diff --git a/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIUnusualDataTests.java b/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIUnusualDataTests.java index efd9f7f443..87601489fa 100644 --- a/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIUnusualDataTests.java +++ b/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIUnusualDataTests.java @@ -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; @@ -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; @@ -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; @@ -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(); diff --git a/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIV2IntegrationTests.java b/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIV2IntegrationTests.java index f1131ae924..74501fc49b 100644 --- a/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIV2IntegrationTests.java +++ b/api/src/test/java/gov/cms/ab2d/api/controller/BulkDataAccessAPIV2IntegrationTests.java @@ -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; @@ -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; @@ -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; @@ -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 diff --git a/api/src/test/java/gov/cms/ab2d/api/controller/MaintenanceModeAPITests.java b/api/src/test/java/gov/cms/ab2d/api/controller/MaintenanceModeAPITests.java index c952503671..e434d03827 100644 --- a/api/src/test/java/gov/cms/ab2d/api/controller/MaintenanceModeAPITests.java +++ b/api/src/test/java/gov/cms/ab2d/api/controller/MaintenanceModeAPITests.java @@ -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; @@ -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"); diff --git a/api/src/test/java/gov/cms/ab2d/api/controller/v2/StatusAPIV2Test.java b/api/src/test/java/gov/cms/ab2d/api/controller/v2/StatusAPIV2Test.java index 7ac6aad4de..2af4e827c4 100644 --- a/api/src/test/java/gov/cms/ab2d/api/controller/v2/StatusAPIV2Test.java +++ b/api/src/test/java/gov/cms/ab2d/api/controller/v2/StatusAPIV2Test.java @@ -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; @@ -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; @@ -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; @@ -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 diff --git a/api/src/test/java/gov/cms/ab2d/api/security/JwtAuthenticationFilterTest.java b/api/src/test/java/gov/cms/ab2d/api/security/JwtAuthenticationFilterTest.java index 1339befeed..9b5e527a62 100644 --- a/api/src/test/java/gov/cms/ab2d/api/security/JwtAuthenticationFilterTest.java +++ b/api/src/test/java/gov/cms/ab2d/api/security/JwtAuthenticationFilterTest.java @@ -5,7 +5,10 @@ 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; @@ -13,10 +16,12 @@ 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; @@ -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; @@ -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(); diff --git a/coverage/pom.xml b/coverage/pom.xml index ef253f0ea7..32f3061c7e 100644 --- a/coverage/pom.xml +++ b/coverage/pom.xml @@ -66,6 +66,13 @@ common 0.0.1-SNAPSHOT + + gov.cms.ab2d + common + 0.0.1-SNAPSHOT + test-jar + test + diff --git a/coverage/src/test/java/gov/cms/ab2d/coverage/service/CoverageServiceExceptionTest.java b/coverage/src/test/java/gov/cms/ab2d/coverage/service/CoverageServiceExceptionTest.java new file mode 100644 index 0000000000..6135f65f7e --- /dev/null +++ b/coverage/src/test/java/gov/cms/ab2d/coverage/service/CoverageServiceExceptionTest.java @@ -0,0 +1,120 @@ +package gov.cms.ab2d.coverage.service; + +import gov.cms.ab2d.common.feign.ContractFeignClient; +import gov.cms.ab2d.coverage.model.CoveragePeriod; +import gov.cms.ab2d.coverage.model.CoverageSearch; +import gov.cms.ab2d.coverage.model.CoverageSearchEvent; +import gov.cms.ab2d.coverage.repository.*; +import gov.cms.ab2d.coverage.util.AB2DCoverageLocalstackContainer; +import gov.cms.ab2d.coverage.util.AB2DCoveragePostgressqlContainer; +import gov.cms.ab2d.coverage.util.CoverageDataSetup; +import gov.cms.ab2d.eventclient.clients.SQSEventClient; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentMatchers; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.domain.EntityScan; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.SpyBean; +import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.test.context.TestPropertySource; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +@SpringBootTest +@EntityScan(basePackages = {"gov.cms.ab2d.common.model", "gov.cms.ab2d.coverage.model"}) +@EnableJpaRepositories({"gov.cms.ab2d.common.repository", "gov.cms.ab2d.coverage.repository"}) +@Testcontainers +@TestPropertySource(locations = "/application.coverage.properties") +@EnableFeignClients(clients = {ContractFeignClient.class}) +public class CoverageServiceExceptionTest { + + @Container + private static final PostgreSQLContainer postgreSQLContainer = new AB2DCoveragePostgressqlContainer(); + + @Container + private static final AB2DCoverageLocalstackContainer localstackContainer = new AB2DCoverageLocalstackContainer(); + + @Autowired + CoveragePeriodRepository coveragePeriodRepo; + + @Autowired + CoverageSearchEventRepository coverageSearchEventRepo; + + @Autowired + CoverageSearchRepository coverageSearchRepo; + + @Autowired + CoverageDeltaRepository coverageDeltaRepository; + + @Autowired + CoverageDeltaTestRepository coverageDeltaTestRepository; + + @Autowired + CoverageService coverageService; + + @SpyBean + CoverageServiceRepository coverageServiceRepo; + + @SpyBean + SQSEventClient eventLogger; + + @Autowired + CoverageDataSetup dataSetup; + + private CoveragePeriod period1Jan; + + + @BeforeEach + public void insertContractAndDefaultCoveragePeriod() { + period1Jan = dataSetup.createCoveragePeriod("TST-12", 1, 2020); + } + + @AfterEach + public void cleanUp() { + dataSetup.cleanup(); + } + + @DisplayName("Coverage period searches can be cancelled") + @Test + void failSearchDeleteAlerts() { + + Mockito.doThrow(RuntimeException.class).when(coverageServiceRepo).deleteCurrentSearch(ArgumentMatchers.any()); + + coverageService.submitSearch(period1Jan.getId(), "testing"); + startSearchAndPullEvent(); + + assertThrows(RuntimeException.class, () -> coverageService.failSearch(period1Jan.getId(), "testing")); + + Mockito.verify(eventLogger, Mockito.times(1)).alert(ArgumentMatchers.any(), ArgumentMatchers.any()); + } + + @DisplayName("Delete previous search on completion failure triggers alert") + @Test + void deletePreviousSearchOnCompletionFailure() { + + Mockito.doThrow(new RuntimeException()).when(coverageServiceRepo).deletePreviousSearches(ArgumentMatchers.nullable(CoveragePeriod.class), ArgumentMatchers.nullable(Integer.class)); + + coverageService.submitSearch(period1Jan.getId(), "testing"); + startSearchAndPullEvent(); + + assertThrows(RuntimeException.class, () -> coverageService.completeSearch(period1Jan.getId(), "testing")); + + Mockito.verify(eventLogger, Mockito.times(1)).alert(ArgumentMatchers.any(), ArgumentMatchers.any()); + } + + private CoverageSearchEvent startSearchAndPullEvent() { + Optional search = coverageSearchRepo.findFirstByOrderByCreatedAsc(); + coverageSearchRepo.delete(search.get()); + return coverageService.startSearch(search.get(), "testing").get().getCoverageSearchEvent(); + } +} diff --git a/coverage/src/test/java/gov/cms/ab2d/coverage/service/CoverageServiceImplTest.java b/coverage/src/test/java/gov/cms/ab2d/coverage/service/CoverageServiceImplTest.java index eaa612a203..a72fae93b4 100644 --- a/coverage/src/test/java/gov/cms/ab2d/coverage/service/CoverageServiceImplTest.java +++ b/coverage/src/test/java/gov/cms/ab2d/coverage/service/CoverageServiceImplTest.java @@ -1,6 +1,8 @@ package gov.cms.ab2d.coverage.service; import gov.cms.ab2d.common.feign.ContractFeignClient; +import gov.cms.ab2d.common.properties.PropertiesService; +import gov.cms.ab2d.common.properties.PropertyServiceStub; import gov.cms.ab2d.coverage.model.ContractForCoverageDTO; import gov.cms.ab2d.coverage.model.CoverageCount; import gov.cms.ab2d.coverage.model.CoverageDelta; @@ -39,20 +41,17 @@ import java.util.stream.Collectors; import javax.persistence.EntityNotFoundException; import javax.sql.DataSource; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.mockito.ArgumentMatchers; -import org.mockito.Mockito; + +import org.junit.jupiter.api.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.SpyBean; import org.springframework.cloud.openfeign.EnableFeignClients; +import org.springframework.context.ApplicationContext; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.test.context.TestPropertySource; +import org.springframework.test.util.ReflectionTestUtils; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; @@ -116,9 +115,6 @@ class CoverageServiceImplTest { @Autowired CoverageSearchRepository coverageSearchRepo; - @SpyBean - CoverageServiceRepository coverageServiceRepo; - @Autowired CoverageDeltaRepository coverageDeltaRepository; @@ -137,6 +133,10 @@ class CoverageServiceImplTest { @Autowired DataSource dataSource; + private final PropertiesService propertiesService = new PropertyServiceStub(); + + private CoverageServiceRepository coverageServiceRepo; + private ContractForCoverageDTO contract1; private ContractForCoverageDTO contract2; @@ -147,10 +147,13 @@ class CoverageServiceImplTest { private OffsetDateTime jobStartTime; private CoveragePeriod period2Jan; + @Autowired + private ApplicationContext context; @BeforeEach public void insertContractAndDefaultCoveragePeriod() { - + CoverageServiceRepository c = context.getBean(CoverageServiceRepository.class); + ReflectionTestUtils.setField(c, "propertiesService", propertiesService); contract1 = dataSetup.setupContractDTO("TST-12", AB2D_EPOCH.toOffsetDateTime()); contract2 = dataSetup.setupContractDTO("TST-34", AB2D_EPOCH.toOffsetDateTime()); period1Jan = dataSetup.createCoveragePeriod("TST-12", JANUARY, YEAR); @@ -160,6 +163,9 @@ public void insertContractAndDefaultCoveragePeriod() { jobStartTime = OffsetDateTime.of(YEAR, APRIL, 2, 0, 0, 0, 0, ZoneOffset.UTC); period2Jan = dataSetup.createCoveragePeriod("TST-34", JANUARY, YEAR); + + coverageServiceRepo = new CoverageServiceRepository(dataSource, coveragePeriodRepo, coverageSearchEventRepo, propertiesService); + propertiesService.createProperty("OptOutOn", "false"); } @AfterEach @@ -1029,20 +1035,6 @@ void deletePreviousSearchOnCompletion() { assertTrue(distinctSearchEvents.contains(inProgress3.getId())); } - @DisplayName("Delete previous search on completion failure triggers alert") - @Test - void deletePreviousSearchOnCompletionFailure() { - - Mockito.doThrow(new RuntimeException()).when(coverageServiceRepo).deletePreviousSearches(ArgumentMatchers.nullable(CoveragePeriod.class), ArgumentMatchers.nullable(Integer.class)); - - coverageService.submitSearch(period1Jan.getId(), "testing"); - startSearchAndPullEvent(); - - assertThrows(RuntimeException.class, () -> coverageService.completeSearch(period1Jan.getId(), "testing")); - - Mockito.verify(eventLogger, Mockito.times(1)).alert(ArgumentMatchers.any(), ArgumentMatchers.any()); - } - @DisplayName("Check search status on CoveragePeriod") @Test void getSearchStatus() { @@ -1388,20 +1380,6 @@ void failSearchesDeletesData() { Assertions.assertTrue(dataSetup.findCoverage().isEmpty()); } - @DisplayName("Coverage period searches can be cancelled") - @Test - void failSearchDeleteAlerts() { - - Mockito.doThrow(RuntimeException.class).when(coverageServiceRepo).deleteCurrentSearch(ArgumentMatchers.any()); - - coverageService.submitSearch(period1Jan.getId(), "testing"); - startSearchAndPullEvent(); - - assertThrows(RuntimeException.class, () -> coverageService.failSearch(period1Jan.getId(), "testing")); - - Mockito.verify(eventLogger, Mockito.times(1)).alert(ArgumentMatchers.any(), ArgumentMatchers.any()); - } - @DisplayName("Coverage period month and year are checked correctly") @Test void checkMonthAndYear() { diff --git a/coverage/src/test/resources/application.coverage.properties b/coverage/src/test/resources/application.coverage.properties index a8f51e43a0..2f6c74fc06 100644 --- a/coverage/src/test/resources/application.coverage.properties +++ b/coverage/src/test/resources/application.coverage.properties @@ -8,6 +8,8 @@ spring.jpa.properties.hibernate.temp.use_jdbc_metadata_defaults=false spring.datasource.driver-class-name=org.postgresql.Driver spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect +spring.main.allow-bean-definition-overriding=true + spring.jpa.hibernate.ddl-auto=none spring.liquibase.enabled=true spring.liquibase.contexts=test diff --git a/worker/src/test/java/gov/cms/ab2d/worker/config/AutoScalingServiceTest.java b/worker/src/test/java/gov/cms/ab2d/worker/config/AutoScalingServiceTest.java index bb04ac53cc..642e77e60e 100644 --- a/worker/src/test/java/gov/cms/ab2d/worker/config/AutoScalingServiceTest.java +++ b/worker/src/test/java/gov/cms/ab2d/worker/config/AutoScalingServiceTest.java @@ -27,8 +27,7 @@ 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 gov.cms.ab2d.common.util.PropertyConstants.*; import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -68,14 +67,17 @@ public class AutoScalingServiceTest { @BeforeEach public void init() { - AutoScalingService asservice = context.getBean(AutoScalingServiceImpl.class); - ReflectionTestUtils.setField(asservice, "propertiesService", propertiesService); - patientProcessorThreadPool.getThreadPoolExecutor().getQueue().clear(); autoScalingService = new AutoScalingServiceImpl(patientProcessorThreadPool, eobClaimRequestsQueue, propertiesService, 3, 20, 20); originalMaxPoolSize = autoScalingService.getMaxPoolSize(); patientProcessorThreadPool.setMaxPoolSize(originalMaxPoolSize); + AutoScalingService asservice = context.getBean(AutoScalingServiceImpl.class); + ReflectionTestUtils.setField(asservice, "propertiesService", propertiesService); + propertiesService.createProperty(MAINTENANCE_MODE, "false"); + propertiesService.createProperty(PCP_MAX_POOL_SIZE, "" + originalMaxPoolSize); + propertiesService.createProperty(PCP_SCALE_TO_MAX_TIME, "20"); + propertiesService.createProperty(PCP_CORE_POOL_SIZE, "3"); } @AfterEach