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-6187] increase api test coverage #1381

Merged
merged 7 commits into from
Aug 5, 2024
Merged
Changes from 6 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
@@ -0,0 +1,112 @@
package gov.cms.ab2d.api.controller.v2;

import com.amazonaws.services.sqs.AmazonSQS;
import gov.cms.ab2d.api.SpringBootApp;
import gov.cms.ab2d.api.controller.TestUtil;
import gov.cms.ab2d.api.remote.JobClientMock;
import gov.cms.ab2d.common.util.AB2DLocalstackContainer;
import gov.cms.ab2d.common.util.AB2DPostgresqlContainer;
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.http.MediaType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.util.List;

import static gov.cms.ab2d.common.model.Role.SPONSOR_ROLE;
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;

@SpringBootTest(classes = SpringBootApp.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
@Testcontainers
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_CLASS)
class StatusAPIV2Test {

@Autowired
private MockMvc mockMvc;

@Autowired
JobClientMock jobClientMock;

@Container
private static final PostgreSQLContainer postgreSQLContainer = new AB2DPostgresqlContainer();

@Container
private static final AB2DLocalstackContainer localstackContainer = new AB2DLocalstackContainer();

@Autowired
private TestUtil testUtil;

@Autowired
private DataSetup dataSetup;

@Autowired
AmazonSQS amazonSQS;

@Autowired
SQSEventClient sqsEventClient;

private String token;

@BeforeEach
public void setup() throws Exception {
token = testUtil.setupToken(List.of(SPONSOR_ROLE));
testUtil.turnMaintenanceModeOff();
}

@AfterEach
public void cleanup() {
dataSetup.cleanup();
jobClientMock.cleanupAll();
}

@Test
void testStatus() throws Exception {
// start a job
this.mockMvc.perform(
get("https://localhost:8443/api/v2/fhir/Patient/$export").contentType(MediaType.APPLICATION_JSON)
coilysiren marked this conversation as resolved.
Show resolved Hide resolved
.header("Authorization", "Bearer " + token)
.header("X-Forwarded-Proto", "https"));

// get the job status
String jobUuid = jobClientMock.pickAJob();
ResultActions resultActions = this.mockMvc.perform(
get(String.format("https://localhost:8443/api/v2/fhir/Job/%s/$status", jobUuid)).contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + token));

// assert ok
resultActions.andExpect(status().isOk());
}

@Test
void testDelete() throws Exception {
// start a job
this.mockMvc.perform(
get("https://localhost:8443/api/v2/fhir/Patient/$export").contentType(MediaType.APPLICATION_JSON)
coilysiren marked this conversation as resolved.
Show resolved Hide resolved
.header("Authorization", "Bearer " + token)
.header("X-Forwarded-Proto", "https"));

// try and delete the job
String jobUuid = jobClientMock.pickAJob();
ResultActions resultActions = this.mockMvc.perform(
delete(String.format("https://localhost:8443/api/v2/fhir/Job/%s/$status", jobUuid)).contentType(MediaType.APPLICATION_JSON)
.header("Authorization", "Bearer " + token));

// assert that you can't (because the job is already marked successful)
resultActions.andExpect(status().is4xxClientError());
}

}
Loading