Search in sources :

Example 1 with OperationPolicyPointcutParametersFactory

use of org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory in project mule by mulesoft.

the class PolicyPointcutParametersManager method createOperationPointcutParameters.

/**
 * Creates {@link PolicyPointcutParameters} for a specific operation. Stored parameters from the source are included in the
 * newly created parameters to be able to correlate parameters from both source and operation.
 *
 * @param operation the operation component to which policies will be applied
 * @param event the event which will execute the operation policies
 * @param operationParameters a map containing the parameters of the operation
 * @return the created {@link PolicyPointcutParameters}
 */
public PolicyPointcutParameters createOperationPointcutParameters(Component operation, CoreEvent event, Map<String, Object> operationParameters) {
    ComponentIdentifier operationIdentifier = operation.getLocation().getComponentIdentifier().getIdentifier();
    PolicyPointcutParameters sourceParameters = sourceParametersMap.get(event.getContext().getCorrelationId());
    OperationPolicyPointcutParametersParameters parameters = new OperationPolicyPointcutParametersParameters(operation, operationParameters, sourceParameters);
    Function<OperationPolicyPointcutParametersFactory, PolicyPointcutParameters> creationFunction = factory -> {
        try {
            return factory.createPolicyPointcutParameters(parameters);
        } catch (AbstractMethodError error) {
            return factory.createPolicyPointcutParameters(parameters.getOperation(), parameters.getOperationParameters());
        }
    };
    return createPointcutParameters(operation, OperationPolicyPointcutParametersFactory.class, operationPointcutFactories, factory -> factory.supportsOperationIdentifier(operationIdentifier), creationFunction).orElse(new PolicyPointcutParameters(operation, sourceParameters));
}
Also used : Optional.empty(java.util.Optional.empty) SourcePolicyPointcutParametersFactory(org.mule.runtime.policy.api.SourcePolicyPointcutParametersFactory) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) Predicate(java.util.function.Predicate) Optional.of(java.util.Optional.of) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) Collection(java.util.Collection) BaseEventContext(org.mule.runtime.core.privileged.event.BaseEventContext) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Function(java.util.function.Function) String.format(java.lang.String.format) Component(org.mule.runtime.api.component.Component) Map(java.util.Map) OperationPolicyPointcutParametersParameters(org.mule.runtime.policy.api.OperationPolicyPointcutParametersParameters) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Optional(java.util.Optional) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) ComponentIdentifier(org.mule.runtime.api.component.ComponentIdentifier) OperationPolicyPointcutParametersParameters(org.mule.runtime.policy.api.OperationPolicyPointcutParametersParameters) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters)

Example 2 with OperationPolicyPointcutParametersFactory

use of org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory in project mule by mulesoft.

the class PolicyPointcutParametersManagerTestCase method createOperationParametersWhenOneFactorySupportsIdentifier.

@Test
public void createOperationParametersWhenOneFactorySupportsIdentifier() {
    Map<String, Object> operationParameters = new HashMap<>();
    PolicyPointcutParameters sourceParameters = parametersManager.createSourcePointcutParameters(component, event);
    OperationPolicyPointcutParametersFactory factory = mockOperationFactory(true, sourceParameters);
    operationPointcutFactories.add(factory);
    sourcePointcutFactories.add(mockSourceFactory(true));
    PolicyPointcutParameters parameters = parametersManager.createOperationPointcutParameters(component, event, operationParameters);
    assertThat(parameters.getComponent(), is(component));
    assertThat(parameters.getSourceParameters(), is(of(sourceParameters)));
    verify(factory).supportsOperationIdentifier(identifier);
    verify(factory).createPolicyPointcutParameters(any());
}
Also used : HashMap(java.util.HashMap) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Test(org.junit.Test)

Example 3 with OperationPolicyPointcutParametersFactory

use of org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory in project mule by mulesoft.

the class PolicyPointcutParametersManagerTestCase method createOperationParametersFallbacksToDeprecatedMethod.

@Test
public void createOperationParametersFallbacksToDeprecatedMethod() {
    Map<String, Object> operationParameters = new HashMap<>();
    PolicyPointcutParameters sourceParameters = parametersManager.createSourcePointcutParameters(component, event);
    OperationPolicyPointcutParametersFactory factory = mockOperationFactory(true, sourceParameters);
    PolicyPointcutParameters parameters = mock(PolicyPointcutParameters.class);
    when(factory.createPolicyPointcutParameters(any())).thenThrow(new AbstractMethodError());
    when(factory.createPolicyPointcutParameters(component, operationParameters)).thenReturn(parameters);
    operationPointcutFactories.add(factory);
    PolicyPointcutParameters returnedParameters = parametersManager.createOperationPointcutParameters(component, event, operationParameters);
    assertThat(returnedParameters, is(parameters));
    verify(factory).supportsOperationIdentifier(identifier);
    verify(factory).createPolicyPointcutParameters(any());
    verify(factory).createPolicyPointcutParameters(component, operationParameters);
}
Also used : HashMap(java.util.HashMap) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Test(org.junit.Test)

Example 4 with OperationPolicyPointcutParametersFactory

use of org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory in project mule by mulesoft.

the class PolicyPointcutParametersManagerTestCase method createOperationParametersWhenOneFactoryDoesNotSupportsIdentifier.

@Test
public void createOperationParametersWhenOneFactoryDoesNotSupportsIdentifier() {
    Map<String, Object> operationParameters = new HashMap<>();
    PolicyPointcutParameters sourceParameters = parametersManager.createSourcePointcutParameters(component, event);
    OperationPolicyPointcutParametersFactory factory = mockOperationFactory(false, sourceParameters);
    operationPointcutFactories.add(factory);
    sourcePointcutFactories.add(mockSourceFactory(true));
    PolicyPointcutParameters parameters = parametersManager.createOperationPointcutParameters(component, event, operationParameters);
    assertThat(parameters.getComponent(), is(component));
    assertThat(parameters.getSourceParameters(), is(of(sourceParameters)));
    verify(factory).supportsOperationIdentifier(identifier);
    verify(factory, never()).createPolicyPointcutParameters(any());
}
Also used : HashMap(java.util.HashMap) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Test(org.junit.Test)

Example 5 with OperationPolicyPointcutParametersFactory

use of org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory in project mule by mulesoft.

the class PolicyPointcutParametersManagerTestCase method createOperationParametersWhenOneFactorySupportsIdentifierAndOneNot.

@Test
public void createOperationParametersWhenOneFactorySupportsIdentifierAndOneNot() {
    Map<String, Object> operationParameters = new HashMap<>();
    PolicyPointcutParameters sourceParameters = parametersManager.createSourcePointcutParameters(component, event);
    OperationPolicyPointcutParametersFactory factory1 = mockOperationFactory(true, sourceParameters);
    OperationPolicyPointcutParametersFactory factory2 = mockOperationFactory(false, sourceParameters);
    operationPointcutFactories.add(factory1);
    operationPointcutFactories.add(factory2);
    sourcePointcutFactories.add(mockSourceFactory(true));
    PolicyPointcutParameters parameters = parametersManager.createOperationPointcutParameters(component, event, operationParameters);
    assertThat(parameters.getComponent(), is(component));
    assertThat(parameters.getSourceParameters(), is(of(sourceParameters)));
    verify(factory1).supportsOperationIdentifier(identifier);
    verify(factory1).createPolicyPointcutParameters(any());
    verify(factory2).supportsOperationIdentifier(identifier);
    verify(factory2, never()).createPolicyPointcutParameters(any());
}
Also used : HashMap(java.util.HashMap) OperationPolicyPointcutParametersFactory(org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory) PolicyPointcutParameters(org.mule.runtime.policy.api.PolicyPointcutParameters) Test(org.junit.Test)

Aggregations

OperationPolicyPointcutParametersFactory (org.mule.runtime.policy.api.OperationPolicyPointcutParametersFactory)7 PolicyPointcutParameters (org.mule.runtime.policy.api.PolicyPointcutParameters)7 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 String.format (java.lang.String.format)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 Optional.of (java.util.Optional.of)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Function (java.util.function.Function)1 Predicate (java.util.function.Predicate)1 Component (org.mule.runtime.api.component.Component)1 ComponentIdentifier (org.mule.runtime.api.component.ComponentIdentifier)1 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)1 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)1 CoreEvent (org.mule.runtime.core.api.event.CoreEvent)1 BaseEventContext (org.mule.runtime.core.privileged.event.BaseEventContext)1 OperationPolicyPointcutParametersParameters (org.mule.runtime.policy.api.OperationPolicyPointcutParametersParameters)1