Search in sources :

Example 1 with LoggableEvent

use of gov.cms.ab2d.eventlogger.LoggableEvent in project ab2d by CMSgov.

the class AuthenticationTests method testClientNoAuthorization.

@Test
public void testClientNoAuthorization() throws Exception {
    PdpClient pdpClient = pdpClientRepository.findByClientId(TEST_PDP_CLIENT);
    pdpClient.setRoles(Collections.emptySet());
    pdpClientRepository.save(pdpClient);
    this.mockMvc.perform(get(API_PREFIX_V1 + FHIR_PREFIX + "/Patient/$export").header("Authorization", "Bearer " + token).contentType(MediaType.APPLICATION_JSON)).andExpect(status().is(403));
    List<LoggableEvent> apiRequestEvents = loggerEventRepository.load(ApiRequestEvent.class);
    assertEquals(1, apiRequestEvents.size());
    ApiRequestEvent requestEvent = (ApiRequestEvent) apiRequestEvents.get(0);
    List<LoggableEvent> apiResponseEvents = loggerEventRepository.load(ApiResponseEvent.class);
    assertEquals(1, apiResponseEvents.size());
    ApiResponseEvent responseEvent = (ApiResponseEvent) apiResponseEvents.get(0);
    assertEquals(HttpStatus.FORBIDDEN.value(), responseEvent.getResponseCode());
    assertEquals(requestEvent.getRequestId(), responseEvent.getRequestId());
}
Also used : LoggableEvent(gov.cms.ab2d.eventlogger.LoggableEvent) PdpClient(gov.cms.ab2d.common.model.PdpClient) ApiRequestEvent(gov.cms.ab2d.eventlogger.events.ApiRequestEvent) ApiResponseEvent(gov.cms.ab2d.eventlogger.events.ApiResponseEvent) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with LoggableEvent

use of gov.cms.ab2d.eventlogger.LoggableEvent in project ab2d by CMSgov.

the class AuthenticationTests method testClientDoesNotExist.

@Test
public void testClientDoesNotExist() throws Exception {
    PdpClient pdpClient = pdpClientRepository.findByClientId(TEST_PDP_CLIENT);
    pdpClientRepository.delete(pdpClient);
    this.mockMvc.perform(get(API_PREFIX_V1 + FHIR_PREFIX + "/Patient/$export").header("Authorization", "Bearer " + token).contentType(MediaType.APPLICATION_JSON)).andExpect(status().is(403));
    List<LoggableEvent> apiRequestEvents = loggerEventRepository.load(ApiRequestEvent.class);
    assertEquals(1, apiRequestEvents.size());
    ApiRequestEvent requestEvent = (ApiRequestEvent) apiRequestEvents.get(0);
    List<LoggableEvent> apiResponseEvents = loggerEventRepository.load(ApiResponseEvent.class);
    assertEquals(1, apiResponseEvents.size());
    ApiResponseEvent responseEvent = (ApiResponseEvent) apiResponseEvents.get(0);
    assertEquals(HttpStatus.FORBIDDEN.value(), responseEvent.getResponseCode());
    assertEquals(requestEvent.getRequestId(), responseEvent.getRequestId());
}
Also used : LoggableEvent(gov.cms.ab2d.eventlogger.LoggableEvent) PdpClient(gov.cms.ab2d.common.model.PdpClient) ApiRequestEvent(gov.cms.ab2d.eventlogger.events.ApiRequestEvent) ApiResponseEvent(gov.cms.ab2d.eventlogger.events.ApiResponseEvent) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with LoggableEvent

use of gov.cms.ab2d.eventlogger.LoggableEvent in project ab2d by CMSgov.

the class BulkDataAccessAPIIntegrationTests method testGetStatusWhileFinished.

@Test
void testGetStatusWhileFinished() throws Exception {
    MvcResult mvcResult = this.mockMvc.perform(get(API_PREFIX_V1 + FHIR_PREFIX + PATIENT_EXPORT_PATH + "?_type=ExplanationOfBenefit").header("Authorization", "Bearer " + token).contentType(MediaType.APPLICATION_JSON)).andReturn();
    String statusUrl = mvcResult.getResponse().getHeader(CONTENT_LOCATION);
    assertNotNull(statusUrl);
    JobOutput jobOutput = new JobOutput();
    jobOutput.setFhirResourceType(EOB);
    jobOutput.setFilePath("file.ndjson");
    jobOutput.setError(false);
    jobOutput.setFileLength(5000L);
    jobOutput.setChecksum("file");
    jobClientMock.addJobOutputForDownload(jobOutput);
    JobOutput errorJobOutput = new JobOutput();
    errorJobOutput.setFhirResourceType(OPERATION_OUTCOME);
    errorJobOutput.setFilePath("error.ndjson");
    errorJobOutput.setFileLength(6000L);
    errorJobOutput.setChecksum("error");
    errorJobOutput.setError(true);
    jobClientMock.addJobOutputForDownload(errorJobOutput);
    String jobUuid = jobClientMock.pickAJob();
    StartJobDTO startJobDTO = jobClientMock.lookupJob(jobUuid);
    this.mockMvc.perform(get(statusUrl).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + token)).andExpect(status().is(200)).andExpect(buildExpiresMatcher()).andExpect(jsonPath("$.transactionTime", Is.is(new org.hl7.fhir.dstu3.model.DateTimeType(OffsetDateTime.now().toString()).toHumanDisplay()))).andExpect(jsonPath("$.request", Is.is(startJobDTO.getUrl()))).andExpect(jsonPath("$.requiresAccessToken", Is.is(true))).andExpect(jsonPath("$.output[0].type", Is.is(EOB))).andExpect(jsonPath("$.output[0].url", Is.is("http://localhost" + API_PREFIX_V1 + FHIR_PREFIX + "/Job/" + jobUuid + "/file/file.ndjson"))).andExpect(jsonPath("$.output[0].extension[0].url", Is.is(CHECKSUM_STRING))).andExpect(jsonPath("$.output[0].extension[0].valueString", Is.is("sha256:file"))).andExpect(jsonPath("$.output[0].extension[1].url", Is.is(CONTENT_LENGTH_STRING))).andExpect(jsonPath("$.output[0].extension[1].valueDecimal", Is.is(5000))).andExpect(jsonPath("$.error[0].type", Is.is(OPERATION_OUTCOME))).andExpect(jsonPath("$.error[0].url", Is.is("http://localhost" + API_PREFIX_V1 + FHIR_PREFIX + "/Job/" + jobUuid + "/file/error.ndjson"))).andExpect(jsonPath("$.error[0].extension[0].url", Is.is(CHECKSUM_STRING))).andExpect(jsonPath("$.error[0].extension[0].valueString", Is.is("sha256:error"))).andExpect(jsonPath("$.error[0].extension[1].url", Is.is(CONTENT_LENGTH_STRING))).andExpect(jsonPath("$.error[0].extension[1].valueDecimal", Is.is(6000)));
    List<LoggableEvent> apiRequestEvents = loggerEventRepository.load(ApiRequestEvent.class);
    assertEquals(2, apiRequestEvents.size());
    ApiRequestEvent requestEvent = (ApiRequestEvent) apiRequestEvents.get(0);
    ApiRequestEvent requestEvent2 = (ApiRequestEvent) apiRequestEvents.get(1);
    if (requestEvent.getUrl().contains("export")) {
        assertNull(requestEvent.getJobId());
    } else {
        assertEquals(jobUuid, requestEvent.getJobId());
    }
    if (requestEvent2.getUrl().contains("export")) {
        assertNull(requestEvent2.getJobId());
    } else {
        assertEquals(jobUuid, requestEvent2.getJobId());
    }
    List<LoggableEvent> apiResponseEvents = loggerEventRepository.load(ApiResponseEvent.class);
    assertEquals(2, apiResponseEvents.size());
    ApiResponseEvent responseEvent = (ApiResponseEvent) apiResponseEvents.get(0);
    ApiResponseEvent responseEvent2 = (ApiResponseEvent) apiResponseEvents.get(1);
    assertEquals(jobUuid, responseEvent.getJobId());
    assertEquals(jobUuid, responseEvent2.getJobId());
    assertEquals(200, responseEvent2.getResponseCode());
    assertTrue(requestEvent.getRequestId().equals(responseEvent.getRequestId()) || requestEvent.getRequestId().equalsIgnoreCase(responseEvent2.getRequestId()));
    assertTrue(requestEvent2.getRequestId().equals(responseEvent.getRequestId()) || requestEvent2.getRequestId().equalsIgnoreCase(responseEvent2.getRequestId()));
    assertEquals(requestEvent2.getRequestId(), responseEvent2.getRequestId());
    // Technically the job status change has 1 entry but should have more because
    // it went through the entire process, but because it is done manually here
    // events weren't created for it.
    assertTrue(UtilMethods.allEmpty(loggerEventRepository.load(ReloadEvent.class), loggerEventRepository.load(ContractSearchEvent.class), loggerEventRepository.load(ErrorEvent.class), loggerEventRepository.load(FileEvent.class)));
}
Also used : StartJobDTO(gov.cms.ab2d.common.dto.StartJobDTO) LoggableEvent(gov.cms.ab2d.eventlogger.LoggableEvent) MvcResult(org.springframework.test.web.servlet.MvcResult) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with LoggableEvent

use of gov.cms.ab2d.eventlogger.LoggableEvent in project ab2d by CMSgov.

the class BulkDataAccessAPIIntegrationTests method testPatientExportDuplicateSubmission.

@Test
void testPatientExportDuplicateSubmission() throws Exception {
    createMaxJobs();
    MvcResult mvcResult = this.mockMvc.perform(get(API_PREFIX_V1 + FHIR_PREFIX + PATIENT_EXPORT_PATH).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + token)).andExpect(status().is(429)).andExpect(header().string("Retry-After", "30")).andExpect(header().doesNotExist(X_PROG)).andReturn();
    List<LoggableEvent> apiRequestEvents = loggerEventRepository.load(ApiRequestEvent.class);
    assertEquals(MAX_JOBS_PER_CLIENT + 1, apiRequestEvents.size());
    List<LoggableEvent> apiResponseEvents = loggerEventRepository.load(ApiResponseEvent.class);
    assertEquals(MAX_JOBS_PER_CLIENT + 1, apiResponseEvents.size());
    ApiResponseEvent responseEvent = (ApiResponseEvent) apiResponseEvents.get(apiResponseEvents.size() - 1);
    assertEquals(HttpStatus.TOO_MANY_REQUESTS.value(), responseEvent.getResponseCode());
    List<LoggableEvent> errorEvents = loggerEventRepository.load(ErrorEvent.class);
    ErrorEvent errorEvent = (ErrorEvent) errorEvents.get(0);
    assertEquals(ErrorEvent.ErrorType.TOO_MANY_STATUS_REQUESTS, errorEvent.getErrorType());
    List<LoggableEvent> jobEvents = loggerEventRepository.load(JobStatusChangeEvent.class);
    assertEquals(MAX_JOBS_PER_CLIENT, jobEvents.size());
    jobEvents.forEach(e -> assertEquals(SUBMITTED.name(), ((JobStatusChangeEvent) e).getNewStatus()));
    JobStatusChangeEvent jobEvent = (JobStatusChangeEvent) jobEvents.get(0);
    assertEquals(SUBMITTED.name(), jobEvent.getNewStatus());
    assertNull(jobEvent.getOldStatus());
    assertTrue(UtilMethods.allEmpty(loggerEventRepository.load(ReloadEvent.class), loggerEventRepository.load(ContractSearchEvent.class), loggerEventRepository.load(FileEvent.class)));
    assertEquals(MAX_JOBS_PER_CLIENT, Objects.requireNonNull(mvcResult.getResponse().getHeader(CONTENT_LOCATION)).split(",").length);
}
Also used : LoggableEvent(gov.cms.ab2d.eventlogger.LoggableEvent) MvcResult(org.springframework.test.web.servlet.MvcResult) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with LoggableEvent

use of gov.cms.ab2d.eventlogger.LoggableEvent in project ab2d by CMSgov.

the class BulkDataAccessAPIIntegrationTests method testBasicPatientExport.

@Test
void testBasicPatientExport() throws Exception {
    ResultActions resultActions = this.mockMvc.perform(get(API_PREFIX_V1 + FHIR_PREFIX + PATIENT_EXPORT_PATH).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + token));
    List<LoggableEvent> apiRequestEvents = loggerEventRepository.load(ApiRequestEvent.class);
    ApiRequestEvent requestEvent = (ApiRequestEvent) apiRequestEvents.get(0);
    assertEquals(1, apiRequestEvents.size());
    List<LoggableEvent> apiResponseEvents = loggerEventRepository.load(ApiResponseEvent.class);
    ApiResponseEvent responseEvent = (ApiResponseEvent) apiResponseEvents.get(0);
    assertEquals(1, apiResponseEvents.size());
    assertEquals(HttpStatus.ACCEPTED.value(), responseEvent.getResponseCode());
    assertEquals(requestEvent.getRequestId(), responseEvent.getRequestId());
    List<LoggableEvent> jobEvents = loggerEventRepository.load(JobStatusChangeEvent.class);
    assertEquals(1, jobEvents.size());
    JobStatusChangeEvent jobEvent = (JobStatusChangeEvent) jobEvents.get(0);
    assertEquals(SUBMITTED.name(), jobEvent.getNewStatus());
    assertNull(jobEvent.getOldStatus());
    assertTrue(UtilMethods.allEmpty(loggerEventRepository.load(ReloadEvent.class), loggerEventRepository.load(ContractSearchEvent.class), loggerEventRepository.load(ErrorEvent.class), loggerEventRepository.load(FileEvent.class)));
    String jobUuid = jobClientMock.pickAJob();
    assertEquals(jobUuid, responseEvent.getJobId());
    String statusUrl = "http://localhost" + API_PREFIX_V1 + FHIR_PREFIX + "/Job/" + jobUuid + "/$status";
    resultActions.andExpect(status().isAccepted()).andExpect(header().string(CONTENT_LOCATION, statusUrl));
    StartJobDTO startJobDTO = jobClientMock.lookupJob(jobUuid);
    assertEquals("http://localhost" + API_PREFIX_V1 + FHIR_PREFIX + PATIENT_EXPORT_PATH, startJobDTO.getUrl());
    assertEquals(EOB, startJobDTO.getResourceTypes());
    assertEquals(pdpClientRepository.findByClientId(TEST_PDP_CLIENT).getOrganization(), startJobDTO.getOrganization());
}
Also used : StartJobDTO(gov.cms.ab2d.common.dto.StartJobDTO) LoggableEvent(gov.cms.ab2d.eventlogger.LoggableEvent) ResultActions(org.springframework.test.web.servlet.ResultActions) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

LoggableEvent (gov.cms.ab2d.eventlogger.LoggableEvent)36 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)33 Test (org.junit.jupiter.api.Test)22 OffsetDateTime (java.time.OffsetDateTime)11 DisplayName (org.junit.jupiter.api.DisplayName)11 File (java.io.File)8 Path (java.nio.file.Path)7 ApiResponseEvent (gov.cms.ab2d.eventlogger.events.ApiResponseEvent)5 FileEvent (gov.cms.ab2d.eventlogger.events.FileEvent)5 JobStatusChangeEvent (gov.cms.ab2d.eventlogger.events.JobStatusChangeEvent)5 ApiRequestEvent (gov.cms.ab2d.eventlogger.events.ApiRequestEvent)4 ArrayList (java.util.ArrayList)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 SpringIntegrationTest (org.springframework.integration.test.context.SpringIntegrationTest)4 MvcResult (org.springframework.test.web.servlet.MvcResult)4 ContractDTO (gov.cms.ab2d.common.dto.ContractDTO)3 Job (gov.cms.ab2d.common.model.Job)3 PdpClient (gov.cms.ab2d.common.model.PdpClient)3 ContractSearchEvent (gov.cms.ab2d.eventlogger.events.ContractSearchEvent)3 IBaseBundle (org.hl7.fhir.instance.model.api.IBaseBundle)3