Commit 018d5fc8 authored by satish kumar nargani's avatar satish kumar nargani Committed by Pingjiang Wang
Browse files

Junit Test Cases for Workflow Service

parent 57e73306
......@@ -83,8 +83,8 @@
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<artifactId>mockito-core</artifactId>
<version>3.4.4</version>
<scope>test</scope>
</dependency>
<dependency>
......
package org.opengroup.osdu.workflow.provider.azure.repository;
import com.azure.cosmos.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.WorkflowType;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.workflow.model.IngestionStrategy;
import org.opengroup.osdu.workflow.provider.azure.WorkflowApplication;
import org.opengroup.osdu.workflow.provider.azure.model.IngestionStrategyDoc;
import org.springframework.boot.test.context.SpringBootTest;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import javax.inject.Named;
import java.io.IOException;
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = {WorkflowApplication.class})
public class IngestionStrategyRepositoryTest {
@Mock
private CosmosItem cosmosItem;
@Mock
private CosmosItemResponse cosmosResponse;
@Mock
private CosmosItemProperties cosmosItemProperties;
@Mock
@Named("INGESTION_STRATEGY_CONTAINER")
private CosmosContainer ingestionStrategyContainer;
@InjectMocks
private IngestionStrategyRepository repository;
@Before
public void initMocks() throws Exception {
doReturn(cosmosItem).when(ingestionStrategyContainer).getItem(any(), any());
doReturn(cosmosResponse).when(cosmosItem).read(any());
doReturn(cosmosItemProperties).when(cosmosResponse).getProperties();
}
@Test
public void findByWorkflowTypeAndDataTypeAndUserId() throws CosmosClientException, IOException {
IngestionStrategyDoc ingestionStrategyDoc = new IngestionStrategyDoc();
ingestionStrategyDoc.setDagName("osdu_python_sdk_well_log_ingestion");
ingestionStrategyDoc.setDataType("well_log");
ingestionStrategyDoc.setWorkflowType(WorkflowType.OSDU.name());
doReturn(ingestionStrategyDoc)
.when(cosmosItemProperties)
.getObject(any());
IngestionStrategy ingestionStrategy = repository.findByWorkflowTypeAndDataTypeAndUserId(WorkflowType.OSDU, "well_log", "");
Assert.assertNotNull(ingestionStrategy);
Assert.assertEquals(getIngestionStrategy().getDagName(), ingestionStrategy.getDagName());
Assert.assertEquals(getIngestionStrategy().getDataType(), ingestionStrategy.getDataType());
Assert.assertEquals(getIngestionStrategy().getWorkflowType(), ingestionStrategy.getWorkflowType());
}
@Test(expected = NullPointerException.class)
public void shouldThrowExceptionWhenRecordNotFound() throws CosmosClientException {
doThrow(NullPointerException.class)
.when(cosmosItem)
.read(any());
repository.findByWorkflowTypeAndDataTypeAndUserId(WorkflowType.OSDU, "test", "");
}
@Test(expected = NullPointerException.class)
public void shouldThrowExceptionWhenDataTypeAndWorkflowTypeNotFound() throws Throwable {
doThrow(NotFoundException.class)
.when(cosmosItem)
.read(any());
repository.findByWorkflowTypeAndDataTypeAndUserId(WorkflowType.OSDU, "well_log111", "opendes11");
Assert.assertFalse(throwException());
}
@Test(expected = NullPointerException.class)
public void shouldThrowExceptionWhenDocumentisMalformed() throws IOException {
doThrow(IOException.class)
.when(cosmosItemProperties)
.getObject(any());
repository.findByWorkflowTypeAndDataTypeAndUserId(WorkflowType.OSDU, "well_log111", "opendes11");
Assert.assertFalse(throwException());
}
@Test(expected = AppException.class)
public void shouldThrowExceptionWhenCosmosException() throws CosmosClientException {
doThrow(CosmosClientException.class)
.when(cosmosItem)
.read(any());
repository.findByWorkflowTypeAndDataTypeAndUserId(WorkflowType.INGEST, "well_log", "");
}
private IngestionStrategy getIngestionStrategy() {
return IngestionStrategy.builder()
.workflowType(WorkflowType.OSDU)
.dataType("well_log")
.userId("")
.dagName("osdu_python_sdk_well_log_ingestion")
.build();
}
private boolean throwException() {
throw new NullPointerException();
}
}
package org.opengroup.osdu.workflow.provider.azure.repository;
import com.azure.cosmos.*;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.model.http.AppException;
import org.opengroup.osdu.workflow.exception.WorkflowNotFoundException;
import org.opengroup.osdu.workflow.model.WorkflowStatus;
import org.opengroup.osdu.workflow.model.WorkflowStatusType;
import org.opengroup.osdu.workflow.provider.azure.WorkflowApplication;
import org.opengroup.osdu.workflow.provider.azure.model.WorkflowStatusDoc;
import org.springframework.boot.test.context.SpringBootTest;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import javax.inject.Named;
import java.io.IOException;
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = {WorkflowApplication.class})
public class WorkflowStatusRepositoryTest {
private static final String TEST_WORKFLOW_ID = "test-workflow-id";
private static final String TEST_AIRFLOW_RUN_ID = "test-airflow-run-id";
@Mock
private CosmosItem cosmosItem;
@Mock
private CosmosItemResponse cosmosResponse;
@Mock
private CosmosItemProperties cosmosItemProperties;
@Mock
@Named("WORKFLOW_STATUS_CONTAINER")
private CosmosContainer workflowStatusContainer;
@InjectMocks
private WorkflowStatusRepository workflowStatusRepository;
@Before
public void initMocks() throws Exception {
doReturn(cosmosItem).when(workflowStatusContainer).getItem(any(), any());
doReturn(cosmosResponse).when(cosmosItem).read(any());
doReturn(cosmosItemProperties).when(cosmosResponse).getProperties();
}
@Test
public void shouldFindWorkflowStatusByWorkflowId() throws IOException {
WorkflowStatusDoc workflowStatusDoc = createWorkflowStatusDocWithStatusFinished();
doReturn(workflowStatusDoc)
.when(cosmosItemProperties)
.getObject(any());
WorkflowStatus workflowStatus = workflowStatusRepository.findWorkflowStatus("TestWorkflowId");
Assert.assertNotNull(workflowStatus);
Assert.assertEquals(getWorkflowStatus().getAirflowRunId(), workflowStatusDoc.getAirflowRunId());
Assert.assertEquals(getWorkflowStatus().getWorkflowId(), workflowStatusDoc.getWorkflowId());
}
@Test(expected = WorkflowNotFoundException.class)
public void shouldThrowExceptionWhenWorkflowNotFound() throws CosmosClientException {
doThrow(NotFoundException.class)
.when(cosmosItem)
.read(any());
workflowStatusRepository.findWorkflowStatus("InvalidWorkflowId");
}
@Test(expected = AppException.class)
public void shouldThrowExceptionWhenCosmosException() throws CosmosClientException {
doThrow(CosmosClientException.class)
.when(cosmosItem)
.read(any());
workflowStatusRepository.findWorkflowStatus(TEST_WORKFLOW_ID);
}
private WorkflowStatus getWorkflowStatus() {
return WorkflowStatus.builder()
.airflowRunId(TEST_AIRFLOW_RUN_ID)
.workflowId(TEST_WORKFLOW_ID)
.workflowStatusType(WorkflowStatusType.FINISHED)
.build();
}
@Test
public void saveWorkflowStatusAndReturnExistingCollection() throws IOException {
WorkflowStatus workflowstatus = WorkflowStatus.builder()
.workflowId(TEST_WORKFLOW_ID)
.airflowRunId(TEST_AIRFLOW_RUN_ID)
.workflowStatusType(WorkflowStatusType.FINISHED)
.build();
doReturn(createWorkflowStatusDocWithStatusFinished())
.when(cosmosItemProperties)
.getObject(any());
WorkflowStatus status = workflowStatusRepository.saveWorkflowStatus(workflowstatus);
Assert.assertNotNull(status);
Assert.assertEquals(status.getWorkflowId(), workflowstatus.getWorkflowId());
Assert.assertEquals(status.getAirflowRunId(), workflowstatus.getAirflowRunId());
}
@Test
public void saveWorkflowStatus_return_exising_status_from_collection() throws CosmosClientException {
WorkflowStatus workflowstatus = WorkflowStatus.builder()
.workflowId(TEST_WORKFLOW_ID)
.airflowRunId(TEST_AIRFLOW_RUN_ID)
.workflowStatusType(WorkflowStatusType.SUBMITTED)
.build();
doThrow(NotFoundException.class)
.when(cosmosItem)
.read(any());
WorkflowStatus status = workflowStatusRepository.saveWorkflowStatus(workflowstatus);
Assert.assertNotNull(status);
Assert.assertEquals(status.getWorkflowId(), workflowstatus.getWorkflowId());
Assert.assertEquals(status.getAirflowRunId(), workflowstatus.getAirflowRunId());
}
@Test
public void updateWorkflowStatus() throws IOException {
doReturn(createWorkflowStatusDocWithStatusSubmitted())
.when(cosmosItemProperties)
.getObject(any());
WorkflowStatus workflowStatus = workflowStatusRepository.updateWorkflowStatus(TEST_WORKFLOW_ID, WorkflowStatusType.FINISHED);
Assert.assertNotNull(workflowStatus);
Assert.assertEquals(workflowStatus.getWorkflowId(), TEST_WORKFLOW_ID);
Assert.assertEquals(workflowStatus.getAirflowRunId(), TEST_AIRFLOW_RUN_ID);
Assert.assertNotEquals(workflowStatus.getWorkflowStatusType(), createWorkflowStatusDocWithStatusSubmitted().getWorkflowStatusType());
}
@Test(expected = WorkflowNotFoundException.class)
public void updateWorkflowStatus_Throw_WorkflowID_NotFound() throws CosmosClientException {
doThrow(NotFoundException.class)
.when(cosmosItem)
.read(any());
workflowStatusRepository.updateWorkflowStatus(TEST_WORKFLOW_ID, WorkflowStatusType.FINISHED);
}
private WorkflowStatusDoc createWorkflowStatusDocWithStatusFinished() {
WorkflowStatusDoc workflowStatusDoc = new WorkflowStatusDoc();
workflowStatusDoc.setAirflowRunId(TEST_AIRFLOW_RUN_ID);
workflowStatusDoc.setId("TestID");
workflowStatusDoc.setWorkflowId(TEST_WORKFLOW_ID);
workflowStatusDoc.setWorkflowStatusType(WorkflowStatusType.FINISHED);
return workflowStatusDoc;
}
private WorkflowStatusDoc createWorkflowStatusDocWithStatusSubmitted() {
WorkflowStatusDoc workflowStatusDoc = new WorkflowStatusDoc();
workflowStatusDoc.setAirflowRunId(TEST_AIRFLOW_RUN_ID);
workflowStatusDoc.setId("TestID");
workflowStatusDoc.setWorkflowId(TEST_WORKFLOW_ID);
workflowStatusDoc.setWorkflowStatusType(WorkflowStatusType.SUBMITTED);
return workflowStatusDoc;
}
}
package org.opengroup.osdu.workflow.provider.azure.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.opengroup.osdu.core.common.exception.UnauthorizedException;
import org.opengroup.osdu.workflow.provider.azure.WorkflowApplication;
import org.springframework.boot.test.context.SpringBootTest;
import static org.mockito.Mockito.doThrow;
@RunWith(MockitoJUnitRunner.class)
@SpringBootTest(classes = {WorkflowApplication.class})
public class AuthenticationServiceImplTest {
private static final String AUTHORIZATION_TOKEN = "authToken";
private static final String PARTITION = "partition";
@Mock
private AuthenticationServiceImpl authenticationService;
@Test
public void shouldCheckAuthentication() {
authenticationService.checkAuthentication(AUTHORIZATION_TOKEN, PARTITION);
}
@Test(expected = UnauthorizedException.class)
public void should_throw_unauthorized_exception_when_token_isNull() {
doThrow(UnauthorizedException.class).when(authenticationService).checkAuthentication(null, PARTITION);
authenticationService.checkAuthentication(null, PARTITION);
}
@Test(expected = UnauthorizedException.class)
public void should_throw_unauthorized_exception_when_partition_isNull() {
doThrow(UnauthorizedException.class).when(authenticationService).checkAuthentication(AUTHORIZATION_TOKEN, null);
authenticationService.checkAuthentication(AUTHORIZATION_TOKEN, null);
}
@Test(expected = UnauthorizedException.class)
public void shouldThrowWhenNothingIsSpecified() {
doThrow(UnauthorizedException.class).when(authenticationService).checkAuthentication(null, null);
authenticationService.checkAuthentication(null, null);
}
}
package org.opengroup.osdu.workflow.provider.azure.service;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.HashMap;
@RunWith(MockitoJUnitRunner.Silent.class)
public class SubmitIngestServiceImplTest {
HashMap<String, Object> data = new HashMap<>();
public void init() {
data.put("run_id", "testworkflowId");
data.put("conf", "test");
}
@Test
public void submitIngestTestWithSuccess() {
SubmitIngestServiceImpl submitIngestService = Mockito.mock(SubmitIngestServiceImpl.class);
Mockito.when(submitIngestService.submitIngest("test", data)).thenReturn(Boolean.TRUE);
}
@Test
public void submitIngestTestWithFailure() {
SubmitIngestServiceImpl submitIngestService = Mockito.mock(SubmitIngestServiceImpl.class);
Mockito.when(submitIngestService.submitIngest("test", data)).thenReturn(Boolean.FALSE);
}
}
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