Commit 7aa38622 authored by Aalekh Jain's avatar Aalekh Jain
Browse files

Updated workflow manager mvc tests

parent 8acbb35a
......@@ -11,12 +11,13 @@ import org.opengroup.osdu.core.common.provider.interfaces.IAuthorizationService;
import org.opengroup.osdu.workflow.exception.ResourceConflictException;
import org.opengroup.osdu.workflow.exception.WorkflowNotFoundException;
import org.opengroup.osdu.workflow.exception.handler.ConflictApiError;
import org.opengroup.osdu.workflow.security.AuthorizationFilter;
import org.opengroup.osdu.workflow.model.CreateWorkflowRequest;
import org.opengroup.osdu.workflow.model.WorkflowMetadata;
import org.opengroup.osdu.workflow.model.WorkflowRole;
import org.opengroup.osdu.workflow.provider.interfaces.IAuthorizationServiceSP;
import org.opengroup.osdu.workflow.provider.interfaces.IWorkflowManagerService;
import org.opengroup.osdu.workflow.security.AuthorizationFilter;
import org.opengroup.osdu.workflow.security.AuthorizationFilterSP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
......@@ -39,8 +40,14 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
/**
......@@ -48,7 +55,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
*/
@WebMvcTest(WorkflowManagerApi.class)
@AutoConfigureMockMvc
@Import({AuthorizationFilter.class, DpsHeaders.class})
@Import({AuthorizationFilter.class, AuthorizationFilterSP.class, DpsHeaders.class})
class WorkflowManagerMvcTest {
private static final String TEST_AUTH = "Bearer bla";
private static final String PARTITION = "partition";
......@@ -80,6 +87,7 @@ class WorkflowManagerMvcTest {
" \"description\": \"This is a test workflow\"\n" +
"}";
private static final String WORKFLOW_ENDPOINT = "/v1/workflow";
private static final String SYSTEM_WORKFLOW_ENDPOINT = "/v1/workflow/system";
private static final String EXISTING_WORKFLOW_ID = "existing-id";
private static final String WORKFLOW_NAME = "test-dag-name";
private static final String WORKFLOW_ID = "2afccfb8-1351-41c6-9127-61f2d7f22ff8";
......@@ -98,6 +106,9 @@ class WorkflowManagerMvcTest {
@MockBean
private IAuthorizationService authorizationService;
@MockBean
private IAuthorizationServiceSP authorizationServiceSP;
@MockBean
private JaxRsDpsLog log;
......@@ -161,6 +172,62 @@ class WorkflowManagerMvcTest {
Assertions.assertEquals(EXISTING_WORKFLOW_ID, response.getConflictId());
}
@Test
void testCreateSystemApiWithSuccess() throws Exception {
final CreateWorkflowRequest request = mapper
.readValue(WORKFLOW_REQUEST, CreateWorkflowRequest.class);
final WorkflowMetadata metadata = mapper.readValue(WORKFLOW_RESPONSE, WorkflowMetadata.class);
when(workflowManagerService.createSystemWorkflow(eq(request))).thenReturn(metadata);
when(authorizationServiceSP.isDomainAdminServiceAccount())
.thenReturn(true);
when(dpsHeaders.getAuthorization()).thenReturn(TEST_AUTH);
when(dpsHeaders.getPartitionId()).thenReturn("");
when(dpsHeaders.getCorrelationId()).thenReturn(CORRELATION_ID);
final MvcResult mvcResult = mockMvc.perform(
post(SYSTEM_WORKFLOW_ENDPOINT)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHttpHeadersWithoutDataPartitionId())
.with(SecurityMockMvcRequestPostProcessors.csrf())
.content(WORKFLOW_REQUEST))
.andExpect(status().isOk())
.andReturn();
verify(workflowManagerService).createSystemWorkflow(eq(request));
verify(authorizationServiceSP).isDomainAdminServiceAccount();
verify(dpsHeaders).getAuthorization();
verify(dpsHeaders).getPartitionId();
final WorkflowMetadata responseMetadata =
mapper.readValue(mvcResult.getResponse().getContentAsByteArray(), WorkflowMetadata.class);
assertThat(metadata, equalTo(responseMetadata));
}
@Test
public void testCreateSystemApiWithConflict() throws Exception {
final CreateWorkflowRequest request = mapper.readValue(WORKFLOW_REQUEST, CreateWorkflowRequest.class);
when(workflowManagerService.createSystemWorkflow(eq(request)))
.thenThrow(new ResourceConflictException(EXISTING_WORKFLOW_ID, "conflict"));
when(authorizationServiceSP.isDomainAdminServiceAccount())
.thenReturn(true);
when(dpsHeaders.getAuthorization()).thenReturn(TEST_AUTH);
when(dpsHeaders.getPartitionId()).thenReturn("");
when(dpsHeaders.getCorrelationId()).thenReturn(CORRELATION_ID);
final MvcResult mvcResult = mockMvc.perform(
post(SYSTEM_WORKFLOW_ENDPOINT)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHttpHeadersWithoutDataPartitionId())
.with(SecurityMockMvcRequestPostProcessors.csrf())
.content(WORKFLOW_REQUEST))
.andExpect(status().isConflict())
.andReturn();
verify(workflowManagerService).createSystemWorkflow(eq(request));
when(authorizationServiceSP.isDomainAdminServiceAccount())
.thenReturn(true);
verify(dpsHeaders).getAuthorization();
verify(dpsHeaders).getPartitionId();
final ConflictApiError response =
mapper.readValue(mvcResult.getResponse().getContentAsByteArray(), ConflictApiError.class);
Assertions.assertEquals(EXISTING_WORKFLOW_ID, response.getConflictId());
}
@Test
void testGetApiWithSuccess() throws Exception {
final WorkflowMetadata metadata = mapper.readValue(WORKFLOW_RESPONSE, WorkflowMetadata.class);
......@@ -227,6 +294,47 @@ class WorkflowManagerMvcTest {
verify(dpsHeaders).getPartitionId();
}
@Test
void testDeleteSystemApiWithSuccess() throws Exception {
doNothing().when(workflowManagerService).deleteSystemWorkflow(eq(WORKFLOW_NAME));
when(authorizationServiceSP.isDomainAdminServiceAccount())
.thenReturn(true);
when(dpsHeaders.getAuthorization()).thenReturn(TEST_AUTH);
when(dpsHeaders.getCorrelationId()).thenReturn(CORRELATION_ID);
mockMvc.perform(
delete("/v1/workflow/system/{workflow_name}", WORKFLOW_NAME)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHttpHeadersWithoutDataPartitionId())
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().is(204))
.andReturn();
verify(workflowManagerService).deleteSystemWorkflow(eq(WORKFLOW_NAME));
verify(authorizationServiceSP).isDomainAdminServiceAccount();
verify(dpsHeaders).getAuthorization();
}
@Test
void testDeleteSystemApiWithError() throws Exception {
doThrow(new WorkflowNotFoundException("not found")).when(workflowManagerService)
.deleteSystemWorkflow(eq(WORKFLOW_NAME));
when(authorizationServiceSP.isDomainAdminServiceAccount())
.thenReturn(true);
when(dpsHeaders.getAuthorization()).thenReturn(TEST_AUTH);
when(dpsHeaders.getPartitionId()).thenReturn("");
when(dpsHeaders.getCorrelationId()).thenReturn(CORRELATION_ID);
mockMvc.perform(
delete("/v1/workflow/system/{workflow_name}", WORKFLOW_NAME)
.contentType(MediaType.APPLICATION_JSON)
.headers(getHttpHeadersWithoutDataPartitionId())
.with(SecurityMockMvcRequestPostProcessors.csrf()))
.andExpect(status().isNotFound())
.andReturn();
verify(workflowManagerService).deleteSystemWorkflow(eq(WORKFLOW_NAME));
verify(authorizationServiceSP).isDomainAdminServiceAccount();
verify(dpsHeaders).getAuthorization();
verify(dpsHeaders).getPartitionId();
}
@Test
public void testGetAllWorkflowsWithSuccess() throws Exception {
final List<WorkflowMetadata> workflowMetadataList =
......@@ -256,9 +364,14 @@ class WorkflowManagerMvcTest {
}
private HttpHeaders getHttpHeaders() {
HttpHeaders headers = getHttpHeadersWithoutDataPartitionId();
headers.add(DpsHeaders.DATA_PARTITION_ID, PARTITION);
return headers;
}
private HttpHeaders getHttpHeadersWithoutDataPartitionId() {
HttpHeaders headers = new HttpHeaders();
headers.add(DpsHeaders.AUTHORIZATION, TEST_AUTH);
headers.add(DpsHeaders.DATA_PARTITION_ID, PARTITION);
return headers;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment