Search in sources :

Example 1 with ActionProvider

use of com.redhat.service.bridge.actions.ActionProvider in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ActionParamValidatorContainer method isValid.

@Override
public boolean isValid(ProcessorRequest value, ConstraintValidatorContext context) {
    /*
         * Centralised handling of Action parameters. The idea here being that for each Action we support, we
         * provide the ability to check:
         *
         * - The action 'type' is recognised
         * - The parameters supplied to configure the Action are valid.
         */
    BaseAction baseAction = value.getAction();
    if (baseAction == null) {
        return false;
    }
    if (baseAction.getParameters() == null) {
        return false;
    }
    ActionProvider actionProvider;
    try {
        actionProvider = actionProviderFactory.getActionProvider(baseAction.getType());
    } catch (ActionProviderException e) {
        context.disableDefaultConstraintViolation();
        HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);
        hibernateContext.addMessageParameter(TYPE_PARAM, baseAction.getType());
        hibernateContext.buildConstraintViolationWithTemplate(ACTION_TYPE_NOT_RECOGNISED_ERROR).addConstraintViolation();
        return false;
    }
    ValidationResult v = actionProvider.getParameterValidator().isValid(baseAction);
    if (!v.isValid()) {
        String message = v.getMessage();
        context.disableDefaultConstraintViolation();
        if (message == null) {
            message = ACTION_PARAMETERS_NOT_VALID_ERROR;
            HibernateConstraintValidatorContext hibernateContext = context.unwrap(HibernateConstraintValidatorContext.class);
            hibernateContext.addMessageParameter(TYPE_PARAM, baseAction.getType());
        } else {
            message = InterpolationHelper.escapeMessageParameter(message);
        }
        context.buildConstraintViolationWithTemplate(message).addConstraintViolation();
    }
    return v.isValid();
}
Also used : ActionProvider(com.redhat.service.bridge.actions.ActionProvider) ActionProviderException(com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException) HibernateConstraintValidatorContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext) BaseAction(com.redhat.service.bridge.infra.models.actions.BaseAction) ValidationResult(com.redhat.service.bridge.actions.ValidationResult)

Example 2 with ActionProvider

use of com.redhat.service.bridge.actions.ActionProvider in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ActionParamValidatorContainerTest method beforeEach.

@BeforeEach
public void beforeEach() {
    actionValidatorMock = mock(ActionParameterValidator.class);
    when(actionValidatorMock.isValid(any())).thenReturn(ValidationResult.valid());
    ActionProvider actionProviderMock = mock(ActionProvider.class);
    when(actionProviderMock.getParameterValidator()).thenReturn(actionValidatorMock);
    reset(actionProviderFactoryMock);
    when(actionProviderFactoryMock.getActionProvider(TEST_ACTION_TYPE)).thenReturn(actionProviderMock);
    when(actionProviderFactoryMock.getActionProvider(not(eq(TEST_ACTION_TYPE)))).thenThrow(new ActionProviderException("No action provider found"));
    validatorContext = mock(HibernateConstraintValidatorContext.class);
    builderMock = mock(HibernateConstraintViolationBuilder.class);
    when(validatorContext.buildConstraintViolationWithTemplate(any(String.class))).thenReturn(builderMock);
    when(validatorContext.unwrap(HibernateConstraintValidatorContext.class)).thenReturn(validatorContext);
}
Also used : ActionProvider(com.redhat.service.bridge.actions.ActionProvider) ActionProviderException(com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException) HibernateConstraintValidatorContext(org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext) HibernateConstraintViolationBuilder(org.hibernate.validator.constraintvalidation.HibernateConstraintViolationBuilder) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ActionParameterValidator(com.redhat.service.bridge.actions.ActionParameterValidator) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 3 with ActionProvider

use of com.redhat.service.bridge.actions.ActionProvider in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ProcessorServiceImpl method createProcessor.

@Override
public Processor createProcessor(String bridgeId, String customerId, ProcessorRequest processorRequest) {
    /* We cannot deploy Processors to a Bridge that is not Available */
    Bridge bridge = bridgesService.getReadyBridge(bridgeId, customerId);
    if (processorDAO.findByBridgeIdAndName(bridgeId, processorRequest.getName()) != null) {
        throw new AlreadyExistingItemException("Processor with name '" + processorRequest.getName() + "' already exists for bridge with id '" + bridgeId + "' for customer '" + customerId + "'");
    }
    Processor newProcessor = new Processor();
    Set<BaseFilter> requestedFilters = processorRequest.getFilters();
    String requestedTransformationTemplate = processorRequest.getTransformationTemplate();
    BaseAction requestedAction = processorRequest.getAction();
    ActionProvider actionProvider = actionProviderFactory.getActionProvider(requestedAction.getType());
    BaseAction resolvedAction = actionProviderFactory.resolve(requestedAction, bridge.getId(), customerId, newProcessor.getId());
    newProcessor.setName(processorRequest.getName());
    newProcessor.setSubmittedAt(ZonedDateTime.now());
    newProcessor.setStatus(ManagedResourceStatus.ACCEPTED);
    newProcessor.setBridge(bridge);
    newProcessor.setShardId(shardService.getAssignedShardId(newProcessor.getId()));
    ProcessorDefinition definition = new ProcessorDefinition(requestedFilters, requestedTransformationTemplate, requestedAction, resolvedAction);
    newProcessor.setDefinition(definitionToJsonNode(definition));
    LOGGER.info("Processor with id '{}' for customer '{}' on bridge '{}' has been marked for creation", newProcessor.getId(), newProcessor.getBridge().getCustomerId(), newProcessor.getBridge().getId());
    createProcessorConnectorEntity(newProcessor, actionProvider, resolvedAction);
    workManager.schedule(newProcessor);
    return newProcessor;
}
Also used : ActionProvider(com.redhat.service.bridge.actions.ActionProvider) Processor(com.redhat.service.bridge.manager.models.Processor) ProcessorDefinition(com.redhat.service.bridge.infra.models.processors.ProcessorDefinition) BaseAction(com.redhat.service.bridge.infra.models.actions.BaseAction) Bridge(com.redhat.service.bridge.manager.models.Bridge) BaseFilter(com.redhat.service.bridge.infra.models.filters.BaseFilter) AlreadyExistingItemException(com.redhat.service.bridge.infra.exceptions.definitions.user.AlreadyExistingItemException)

Aggregations

ActionProvider (com.redhat.service.bridge.actions.ActionProvider)3 ActionProviderException (com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException)2 BaseAction (com.redhat.service.bridge.infra.models.actions.BaseAction)2 HibernateConstraintValidatorContext (org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext)2 ActionParameterValidator (com.redhat.service.bridge.actions.ActionParameterValidator)1 ValidationResult (com.redhat.service.bridge.actions.ValidationResult)1 AlreadyExistingItemException (com.redhat.service.bridge.infra.exceptions.definitions.user.AlreadyExistingItemException)1 BaseFilter (com.redhat.service.bridge.infra.models.filters.BaseFilter)1 ProcessorDefinition (com.redhat.service.bridge.infra.models.processors.ProcessorDefinition)1 Bridge (com.redhat.service.bridge.manager.models.Bridge)1 Processor (com.redhat.service.bridge.manager.models.Processor)1 HibernateConstraintViolationBuilder (org.hibernate.validator.constraintvalidation.HibernateConstraintViolationBuilder)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1