Search in sources :

Example 6 with NotFoundException

use of com.sequenceiq.cloudbreak.common.exception.NotFoundException in project cloudbreak by hortonworks.

the class RequestPropertyAuthorizationFactory method calcAuthorization.

private Optional<AuthorizationRule> calcAuthorization(Object resourceObject, CheckPermissionByRequestProperty methodAnnotation, String userCrn) {
    boolean skipOnNull = methodAnnotation.skipOnNull();
    try {
        Object fieldObject = PropertyUtils.getProperty(resourceObject, methodAnnotation.path());
        AuthorizationVariableType authorizationVariableType = methodAnnotation.type();
        AuthorizationResourceAction action = methodAnnotation.action();
        if (fieldObject != null) {
            return calcAuthorizationFromObject(action, authorizationVariableType, fieldObject, userCrn);
        } else if (!methodAnnotation.skipOnNull()) {
            throw new BadRequestException(String.format("Property [%s] of the request object must not be null.", methodAnnotation.path()));
        }
    } catch (NestedNullException nne) {
        if (!skipOnNull) {
            throw new BadRequestException(String.format("Property [%s] of the request object must not be null.", methodAnnotation.path()));
        }
    } catch (NotFoundException nfe) {
        LOGGER.warn("Resource not found during permission check of resource object, this should be handled by microservice.");
    } catch (Error | RuntimeException unchecked) {
        LOGGER.error("Error happened during authorization of the request object: ", unchecked);
        throw unchecked;
    } catch (Throwable t) {
        LOGGER.error("Error happened during authorization of the request object: ", t);
        throw new AccessDeniedException("Error happened during authorization of the request object, thus access is denied!", t);
    }
    return Optional.empty();
}
Also used : AccessDeniedException(org.springframework.security.access.AccessDeniedException) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) NestedNullException(org.apache.commons.beanutils.NestedNullException) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) RequestObject(com.sequenceiq.authorization.annotation.RequestObject) AuthorizationResourceAction(com.sequenceiq.authorization.resource.AuthorizationResourceAction) AuthorizationVariableType(com.sequenceiq.authorization.resource.AuthorizationVariableType)

Example 7 with NotFoundException

use of com.sequenceiq.cloudbreak.common.exception.NotFoundException in project cloudbreak by hortonworks.

the class StackCreatorServiceRecipeValidationTest method testIfMultipleRecipesDoesNotExistsWhichHasGivenInOneOfTheHostgroupsThenBadRequestExceptionShouldCome.

@Test
void testIfMultipleRecipesDoesNotExistsWhichHasGivenInOneOfTheHostgroupsThenBadRequestExceptionShouldCome() {
    String notExistingRecipeName = "someNotExistingRecipe";
    String someOtherNotExistingRecipeName = "someOtherNotExistingRecipe";
    StackV4Request request = new StackV4Request();
    request.setInstanceGroups(List.of(getInstanceGroupWithRecipe(INSTANCE_GROUP_MASTER, Set.of(notExistingRecipeName, someOtherNotExistingRecipeName))));
    when(recipeService.get(any(NameOrCrn.class), eq(WORKSPACE_ID))).thenThrow(new NotFoundException("Recipe not found"));
    BadRequestException exception = Assertions.assertThrows(BadRequestException.class, () -> underTest.createStack(user, workspace, request, false));
    Assert.assertNotNull(exception);
    Assertions.assertTrue(exception.getMessage().matches(String.format("The given recipes does not exists for the instance group \"%s\": (\\w+), (\\w+)", INSTANCE_GROUP_MASTER)));
    verify(recipeService, times(2)).get(any(NameOrCrn.class), anyLong());
}
Also used : StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NameOrCrn(com.sequenceiq.cloudbreak.api.endpoint.v4.dto.NameOrCrn) Test(org.junit.jupiter.api.Test)

Example 8 with NotFoundException

use of com.sequenceiq.cloudbreak.common.exception.NotFoundException in project cloudbreak by hortonworks.

the class StackCreatorServiceRecipeValidationTest method testIfOneRecipeDoesNotExistsWhichIsGivenInOneOfTheHostgroupsThenBadRequestExceptionShouldCome.

@Test
void testIfOneRecipeDoesNotExistsWhichIsGivenInOneOfTheHostgroupsThenBadRequestExceptionShouldCome() {
    String notExistingRecipeName = "someNotExistingRecipe";
    StackV4Request request = new StackV4Request();
    request.setInstanceGroups(List.of(getInstanceGroupWithRecipe(INSTANCE_GROUP_MASTER, Set.of(notExistingRecipeName))));
    when(recipeService.get(any(NameOrCrn.class), eq(WORKSPACE_ID))).thenThrow(new NotFoundException("Recipe not found"));
    BadRequestException exception = Assertions.assertThrows(BadRequestException.class, () -> underTest.createStack(user, workspace, request, false));
    Assert.assertNotNull(exception);
    Assertions.assertEquals(String.format("The given recipe does not exist for the instance group \"%s\": %s", INSTANCE_GROUP_MASTER, notExistingRecipeName), exception.getMessage());
    verify(recipeService, times(1)).get(any(NameOrCrn.class), anyLong());
}
Also used : StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) NameOrCrn(com.sequenceiq.cloudbreak.api.endpoint.v4.dto.NameOrCrn) Test(org.junit.jupiter.api.Test)

Example 9 with NotFoundException

use of com.sequenceiq.cloudbreak.common.exception.NotFoundException in project cloudbreak by hortonworks.

the class StackCreatorServiceTest method shouldThrowBadRequestWhenRecipeIsMissing.

@Test
public void shouldThrowBadRequestWhenRecipeIsMissing() {
    User user = new User();
    Workspace workspace = new Workspace();
    workspace.setId(WORKSPACE_ID);
    StackV4Request stackRequest = new StackV4Request();
    InstanceGroupV4Request instanceGroupV4Request = new InstanceGroupV4Request();
    instanceGroupV4Request.setName(INSTANCE_GROUP);
    instanceGroupV4Request.setRecipeNames(Set.of(RECIPE_NAME));
    stackRequest.setInstanceGroups(List.of(instanceGroupV4Request));
    doNothing().when(nodeCountLimitValidator).validateProvision(any());
    doThrow(new NotFoundException("missing recipe")).when(recipeService).get(NameOrCrn.ofName(RECIPE_NAME), WORKSPACE_ID);
    BadRequestException badRequestException = assertThrows(BadRequestException.class, () -> underTest.createStack(user, workspace, stackRequest, false));
    assertThat(badRequestException).hasMessage("The given recipe does not exist for the instance group \"INSTANCE_GROUP\": RECIPE_NAME");
}
Also used : User(com.sequenceiq.cloudbreak.workspace.model.User) StackV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) BadRequestException(com.sequenceiq.cloudbreak.common.exception.BadRequestException) InstanceGroupV4Request(com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.instancegroup.InstanceGroupV4Request) Workspace(com.sequenceiq.cloudbreak.workspace.model.Workspace) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with NotFoundException

use of com.sequenceiq.cloudbreak.common.exception.NotFoundException in project cloudbreak by hortonworks.

the class BlueprintServiceTest method testGetByNameForWorkspaceAndLoadDefaultsIfNecessaryWhenNotFound.

@Test
public void testGetByNameForWorkspaceAndLoadDefaultsIfNecessaryWhenNotFound() {
    Blueprint blueprint1 = getBlueprint("One", DEFAULT);
    Blueprint blueprint2 = getBlueprint("Two", DEFAULT);
    when(blueprintRepository.findByNameAndWorkspaceId("Three", 1L)).thenReturn(Optional.empty());
    when(blueprintRepository.findAllByWorkspaceIdAndStatusIn(anyLong(), any())).thenReturn(Set.of(blueprint1));
    when(blueprintLoaderService.isAddingDefaultBlueprintsNecessaryForTheUser(any())).thenReturn(true);
    when(blueprintLoaderService.loadBlueprintsForTheWorkspace(any(), any(), any())).thenReturn(Set.of(blueprint1, blueprint2));
    try {
        underTest.getByNameForWorkspaceAndLoadDefaultsIfNecessary("Three", getWorkspace());
    } catch (NotFoundException e) {
        assertThat(e.getMessage(), containsString("Three"));
    }
    verify(blueprintRepository).findByNameAndWorkspaceId("Three", 1L);
    verify(blueprintRepository).findAllByWorkspaceIdAndStatusIn(anyLong(), any());
    verify(blueprintLoaderService).isAddingDefaultBlueprintsNecessaryForTheUser(any());
    verify(blueprintLoaderService).loadBlueprintsForTheWorkspace(any(), any(), any());
}
Also used : Blueprint(com.sequenceiq.cloudbreak.domain.Blueprint) NotFoundException(com.sequenceiq.cloudbreak.common.exception.NotFoundException) Test(org.junit.jupiter.api.Test)

Aggregations

NotFoundException (com.sequenceiq.cloudbreak.common.exception.NotFoundException)73 Test (org.junit.jupiter.api.Test)33 CloudbreakImageNotFoundException (com.sequenceiq.cloudbreak.core.CloudbreakImageNotFoundException)11 BadRequestException (com.sequenceiq.cloudbreak.common.exception.BadRequestException)10 Stack (com.sequenceiq.cloudbreak.domain.stack.Stack)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 SdxCluster (com.sequenceiq.datalake.entity.SdxCluster)9 StackV4Request (com.sequenceiq.cloudbreak.api.endpoint.v4.stacks.request.StackV4Request)8 Map (java.util.Map)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 NameOrCrn (com.sequenceiq.cloudbreak.api.endpoint.v4.dto.NameOrCrn)6 List (java.util.List)6 TransactionExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionExecutionException)5 TransactionRuntimeExecutionException (com.sequenceiq.cloudbreak.common.service.TransactionService.TransactionRuntimeExecutionException)5 Workspace (com.sequenceiq.cloudbreak.workspace.model.Workspace)5 Optional (java.util.Optional)5 CloudbreakImageCatalogException (com.sequenceiq.cloudbreak.core.CloudbreakImageCatalogException)4 Set (java.util.Set)4 CheckPermissionByResourceName (com.sequenceiq.authorization.annotation.CheckPermissionByResourceName)3 Blueprint (com.sequenceiq.cloudbreak.domain.Blueprint)3