Search in sources :

Example 1 with WorkbasketNotFoundException

use of pro.taskana.exceptions.WorkbasketNotFoundException in project taskana by Taskana.

the class TaskServiceImplTest method testCreateThrowsWorkbasketNotFoundException.

@Test(expected = WorkbasketNotFoundException.class)
public void testCreateThrowsWorkbasketNotFoundException() throws NotAuthorizedException, WorkbasketNotFoundException, ClassificationNotFoundException, InvalidWorkbasketException, TaskAlreadyExistException, TaskNotFoundException, InvalidArgumentException {
    TaskServiceImpl cutSpy = Mockito.spy(cut);
    Classification dummyClassification = createDummyClassification();
    TaskImpl task = createUnitTestTask("", "dumma-task", "1", dummyClassification);
    doThrow(TaskNotFoundException.class).when(cutSpy).getTask(task.getId());
    doThrow(WorkbasketNotFoundException.class).when(workbasketServiceMock).getWorkbasket(any(), any());
    try {
        cutSpy.createTask(task);
    } catch (WorkbasketNotFoundException e) {
        verify(taskanaEngineMock, times(1)).openConnection();
        verify(workbasketServiceMock, times(1)).getWorkbasket(task.getWorkbasketKey(), task.getWorkbasketSummary().getDomain());
        verify(taskanaEngineMock, times(1)).returnConnection();
        verifyNoMoreInteractions(attachmentMapperMock, taskanaEngineConfigurationMock, taskanaEngineMock, taskMapperMock, objectReferenceMapperMock, workbasketServiceMock, sqlSessionMock, classificationQueryImplMock, classificationServiceImplMock);
        throw e;
    }
}
Also used : Classification(pro.taskana.Classification) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with WorkbasketNotFoundException

use of pro.taskana.exceptions.WorkbasketNotFoundException in project taskana by Taskana.

the class DistributionTargetsAccTest method testDistributionTargetCallsWithNonExistingWorkbaskets.

@WithAccessId(userName = "user_1_1", groupNames = { "teamlead_1", "group_1", "group_2", "businessadmin" })
@Test
public void testDistributionTargetCallsWithNonExistingWorkbaskets() throws NotAuthorizedException, WorkbasketNotFoundException {
    WorkbasketService workbasketService = taskanaEngine.getWorkbasketService();
    String existingWb = "WBI:100000000000000000000000000000000001";
    String nonExistingWb = "WBI:100000000000000000000000000000000xx1";
    try {
        workbasketService.getDistributionTargets("WBI:100000000000000000000000000000000xx1");
        assertTrue("This line of code should not be reached", false);
    } catch (WorkbasketNotFoundException ex) {
    // nothing to do
    }
    try {
        List<String> distributionTargets = new ArrayList<>(Arrays.asList(nonExistingWb));
        workbasketService.setDistributionTargets(existingWb, distributionTargets);
        assertTrue("This line of code should not be reached", false);
    } catch (WorkbasketNotFoundException ex) {
    // nothing to do
    }
    try {
        workbasketService.addDistributionTarget(existingWb, nonExistingWb);
        assertTrue("This line of code should not be reached", false);
    } catch (WorkbasketNotFoundException ex) {
    // nothing to do
    }
    int beforeCount = workbasketService.getDistributionTargets(existingWb).size();
    workbasketService.removeDistributionTarget(existingWb, nonExistingWb);
    int afterCount = workbasketService.getDistributionTargets(existingWb).size();
    assertEquals(afterCount, beforeCount);
}
Also used : WorkbasketService(pro.taskana.WorkbasketService) ArrayList(java.util.ArrayList) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) AbstractAccTest(acceptance.AbstractAccTest) Test(org.junit.Test) WithAccessId(pro.taskana.security.WithAccessId)

Example 3 with WorkbasketNotFoundException

use of pro.taskana.exceptions.WorkbasketNotFoundException in project taskana by Taskana.

the class WorkbasketDefinitionController method exportWorkbaskets.

@GetMapping
@Transactional(readOnly = true, rollbackFor = Exception.class)
public ResponseEntity<List<WorkbasketDefinition>> exportWorkbaskets(@RequestParam(required = false) String domain) {
    try {
        WorkbasketQuery workbasketQuery = workbasketService.createWorkbasketQuery();
        List<WorkbasketSummary> workbasketSummaryList = domain != null ? workbasketQuery.domainIn(domain).list() : workbasketQuery.list();
        List<WorkbasketDefinition> basketExports = new ArrayList<>();
        for (WorkbasketSummary summary : workbasketSummaryList) {
            Workbasket workbasket = workbasketService.getWorkbasket(summary.getId());
            basketExports.add(workbasketDefinitionMapper.toResource(workbasket));
        }
        return new ResponseEntity<>(basketExports, HttpStatus.OK);
    } catch (WorkbasketNotFoundException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } catch (NotAuthorizedException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    }
}
Also used : WorkbasketDefinition(pro.taskana.rest.resource.WorkbasketDefinition) ResponseEntity(org.springframework.http.ResponseEntity) WorkbasketQuery(pro.taskana.WorkbasketQuery) ArrayList(java.util.ArrayList) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) Workbasket(pro.taskana.Workbasket) WorkbasketSummary(pro.taskana.WorkbasketSummary) GetMapping(org.springframework.web.bind.annotation.GetMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with WorkbasketNotFoundException

use of pro.taskana.exceptions.WorkbasketNotFoundException in project taskana by Taskana.

the class WorkbasketDefinitionController method importWorkbaskets.

/**
 * This method imports a <b>list of {@link WorkbasketDefinition}</b>. This does not exactly match the REST norm, but
 * we want to have an option to import all settings at once. When a logical equal (key and domain are equal)
 * workbasket already exists an update will be executed. Otherwise a new workbasket will be created.
 *
 * @param definitions the list of workbasket definitions which will be imported to the current system.
 * @return Return answer is determined by the status code: 200 - all good 400 - list state error (referring to non
 * existing id's) 401 - not authorized
 */
@PostMapping(path = "/import")
@Transactional(rollbackFor = Exception.class)
public ResponseEntity<String> importWorkbaskets(@RequestBody List<WorkbasketDefinition> definitions) {
    try {
        // key: logical ID
        // value: system ID (in database)
        Map<String, String> systemIds = workbasketService.createWorkbasketQuery().list().stream().collect(Collectors.toMap(this::logicalId, WorkbasketSummary::getId));
        // key: old system ID
        // value: system ID
        Map<String, String> idConversion = new HashMap<>();
        // STEP 1: update or create workbaskets from the import
        for (WorkbasketDefinition definition : definitions) {
            WorkbasketResource res = definition.workbasketResource;
            Workbasket workbasket;
            String oldId = res.workbasketId;
            if (systemIds.containsKey(logicalId(res))) {
                res.workbasketId = systemIds.get(logicalId(res));
                workbasket = workbasketService.updateWorkbasket(workbasketMapper.toModel(res));
            } else {
                res.workbasketId = null;
                workbasket = workbasketService.createWorkbasket(workbasketMapper.toModel(res));
            }
            res.workbasketId = oldId;
            // simply delete all existing accessItems and create new ones.
            for (WorkbasketAccessItem accessItem : workbasketService.getWorkbasketAccessItems(workbasket.getId())) {
                workbasketService.deleteWorkbasketAccessItem(accessItem.getId());
            }
            for (WorkbasketAccessItemResource authorization : definition.authorizations) {
                workbasketService.createWorkbasketAccessItem(workbasketAccessItemMapper.toModel(authorization));
            }
            idConversion.put(definition.workbasketResource.workbasketId, workbasket.getId());
        }
        // This can not be done in step 1 because the system IDs are only known after step 1
        for (WorkbasketDefinition definition : definitions) {
            List<String> distributionTargets = new ArrayList<>();
            for (String oldId : definition.distributionTargets) {
                if (idConversion.containsKey(oldId)) {
                    distributionTargets.add(idConversion.get(oldId));
                } else {
                    throw new InvalidWorkbasketException(String.format("invalid import state: Workbasket '%s' does not exist in the given import list", oldId));
                }
            }
            workbasketService.setDistributionTargets(// no verification necessary since the workbasket was already imported in step 1.
            idConversion.get(definition.workbasketResource.workbasketId), distributionTargets);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    } catch (WorkbasketNotFoundException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    } catch (InvalidWorkbasketException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    } catch (NotAuthorizedException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.UNAUTHORIZED);
    } catch (InvalidArgumentException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.PRECONDITION_FAILED);
    } catch (WorkbasketAlreadyExistException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.CONFLICT);
    } catch (DomainNotFoundException e) {
        TransactionInterceptor.currentTransactionStatus().setRollbackOnly();
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }
}
Also used : WorkbasketDefinition(pro.taskana.rest.resource.WorkbasketDefinition) WorkbasketAccessItemResource(pro.taskana.rest.resource.WorkbasketAccessItemResource) HashMap(java.util.HashMap) WorkbasketAlreadyExistException(pro.taskana.exceptions.WorkbasketAlreadyExistException) WorkbasketAccessItem(pro.taskana.WorkbasketAccessItem) ArrayList(java.util.ArrayList) InvalidWorkbasketException(pro.taskana.exceptions.InvalidWorkbasketException) DomainNotFoundException(pro.taskana.exceptions.DomainNotFoundException) NotAuthorizedException(pro.taskana.exceptions.NotAuthorizedException) ResponseEntity(org.springframework.http.ResponseEntity) InvalidArgumentException(pro.taskana.exceptions.InvalidArgumentException) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) WorkbasketResource(pro.taskana.rest.resource.WorkbasketResource) Workbasket(pro.taskana.Workbasket) PostMapping(org.springframework.web.bind.annotation.PostMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with WorkbasketNotFoundException

use of pro.taskana.exceptions.WorkbasketNotFoundException in project taskana by Taskana.

the class WorkbasketServiceImplTest method testDeleteWorkbasketIsUsed.

@Test(expected = WorkbasketInUseException.class)
public void testDeleteWorkbasketIsUsed() throws NotAuthorizedException, WorkbasketInUseException, InvalidArgumentException, WorkbasketNotFoundException {
    Workbasket wb = createTestWorkbasket("WBI:0", "wb-key");
    List<TaskSummary> usages = Arrays.asList(new TaskSummaryImpl(), new TaskSummaryImpl());
    doReturn(wb).when(cutSpy).getWorkbasket(wb.getId());
    doReturn(sqlSessionMock).when(taskanaEngineImplMock).getSqlSession();
    doReturn(taskMapperMock).when(sqlSessionMock).getMapper(TaskMapper.class);
    doReturn(new Long(1)).when(taskMapperMock).countTasksInWorkbasket(any());
    try {
        cutSpy.deleteWorkbasket(wb.getId());
    } catch (WorkbasketNotFoundException e) {
        verify(taskanaEngineImplMock, times(1)).openConnection();
        verify(cutSpy, times(1)).getWorkbasket(wb.getId());
        verify(taskanaEngineImplMock, times(1)).getTaskService();
        verify(taskServiceMock, times(1)).createTaskQuery();
        verify(taskQueryMock, times(1)).workbasketIdIn(wb.getId());
        verify(taskQueryMock, times(1)).list();
        verify(taskanaEngineImplMock, times(1)).returnConnection();
        verifyNoMoreInteractions(taskQueryMock, taskServiceMock, workbasketMapperMock, workbasketAccessMapperMock, distributionTargetMapperMock, taskanaEngineImplMock, taskanaEngineConfigurationMock);
        throw e;
    }
}
Also used : TaskSummary(pro.taskana.TaskSummary) WorkbasketNotFoundException(pro.taskana.exceptions.WorkbasketNotFoundException) Workbasket(pro.taskana.Workbasket) Test(org.junit.Test)

Aggregations

WorkbasketNotFoundException (pro.taskana.exceptions.WorkbasketNotFoundException)10 Workbasket (pro.taskana.Workbasket)8 Test (org.junit.Test)7 AbstractAccTest (acceptance.AbstractAccTest)5 WithAccessId (pro.taskana.security.WithAccessId)5 ArrayList (java.util.ArrayList)4 NotAuthorizedException (pro.taskana.exceptions.NotAuthorizedException)4 WorkbasketService (pro.taskana.WorkbasketService)3 InvalidWorkbasketException (pro.taskana.exceptions.InvalidWorkbasketException)3 SQLException (java.sql.SQLException)2 ResponseEntity (org.springframework.http.ResponseEntity)2 Transactional (org.springframework.transaction.annotation.Transactional)2 WorkbasketSummary (pro.taskana.WorkbasketSummary)2 DomainNotFoundException (pro.taskana.exceptions.DomainNotFoundException)2 InvalidArgumentException (pro.taskana.exceptions.InvalidArgumentException)2 WorkbasketAlreadyExistException (pro.taskana.exceptions.WorkbasketAlreadyExistException)2 WorkbasketDefinition (pro.taskana.rest.resource.WorkbasketDefinition)2 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1