Search in sources :

Example 1 with ActionProviderException

use of com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class ExecutorTest method setup.

@BeforeEach
void setup() {
    actionProviderFactoryMock = mock(ActionProviderFactory.class);
    actionInvokerMock = mock(ActionInvoker.class);
    InvokableActionProvider actionProvider = mock(InvokableActionProvider.class);
    when(actionProvider.getActionInvoker(any(), any())).thenReturn(actionInvokerMock);
    when(actionProviderFactoryMock.getInvokableActionProvider(KafkaTopicAction.TYPE)).thenReturn(actionProvider);
    when(actionProviderFactoryMock.getInvokableActionProvider(WebhookAction.TYPE)).thenReturn(actionProvider);
    when(actionProviderFactoryMock.getInvokableActionProvider(not(or(eq(KafkaTopicAction.TYPE), eq(WebhookAction.TYPE))))).thenThrow(new ActionProviderException("Unknown action type"));
    meterRegistry = new SimpleMeterRegistry();
}
Also used : ActionInvoker(com.redhat.service.bridge.actions.ActionInvoker) ActionProviderException(com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException) InvokableActionProvider(com.redhat.service.bridge.actions.InvokableActionProvider) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) ActionProviderFactory(com.redhat.service.bridge.actions.ActionProviderFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with ActionProviderException

use of com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException 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 3 with ActionProviderException

use of com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException in project sandbox by 5733d9e2be6485d52ffa08870cabdee0.

the class SendToBridgeActionTransformer method transform.

@Override
public BaseAction transform(BaseAction action, String bridgeId, String customerId, String processorId) {
    String destinationBridgeId = action.getParameters().getOrDefault(SendToBridgeAction.BRIDGE_ID_PARAM, bridgeId);
    Bridge destinationBridge = bridgesService.getReadyBridge(destinationBridgeId, customerId);
    Map<String, String> parameters = new HashMap<>();
    try {
        parameters.put(WebhookAction.ENDPOINT_PARAM, getBridgeWebhookUrl(destinationBridge.getEndpoint()));
        parameters.put(WebhookAction.USE_TECHNICAL_BEARER_TOKEN, "true");
    } catch (MalformedURLException e) {
        throw new ActionProviderException("Can't find events webhook for bridge " + destinationBridgeId);
    }
    BaseAction transformedAction = new BaseAction();
    transformedAction.setType(WebhookAction.TYPE);
    transformedAction.setParameters(parameters);
    return transformedAction;
}
Also used : MalformedURLException(java.net.MalformedURLException) ActionProviderException(com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException) HashMap(java.util.HashMap) BaseAction(com.redhat.service.bridge.infra.models.actions.BaseAction) Bridge(com.redhat.service.bridge.manager.models.Bridge)

Example 4 with ActionProviderException

use of com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException 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)

Aggregations

ActionProviderException (com.redhat.service.bridge.infra.exceptions.definitions.user.ActionProviderException)4 ActionProvider (com.redhat.service.bridge.actions.ActionProvider)2 BaseAction (com.redhat.service.bridge.infra.models.actions.BaseAction)2 HibernateConstraintValidatorContext (org.hibernate.validator.constraintvalidation.HibernateConstraintValidatorContext)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 ActionInvoker (com.redhat.service.bridge.actions.ActionInvoker)1 ActionParameterValidator (com.redhat.service.bridge.actions.ActionParameterValidator)1 ActionProviderFactory (com.redhat.service.bridge.actions.ActionProviderFactory)1 InvokableActionProvider (com.redhat.service.bridge.actions.InvokableActionProvider)1 ValidationResult (com.redhat.service.bridge.actions.ValidationResult)1 Bridge (com.redhat.service.bridge.manager.models.Bridge)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 MalformedURLException (java.net.MalformedURLException)1 HashMap (java.util.HashMap)1 HibernateConstraintViolationBuilder (org.hibernate.validator.constraintvalidation.HibernateConstraintViolationBuilder)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1