@Override public CoreEvent process(CoreEvent event) throws MuleException { event = CoreEvent.builder(event) .addVariable(LIFECYCLE_TRACKER_PROCESSOR_PROPERTY, getTracker().toString()) .addVariable(FLOW_CONSRUCT_PROPERTY, registry.lookupByName(getRootContainerLocation().toString()).orElse(null)).build(); return event; } }
@Test public void variablesAddedBeforeNextProcessorNotPropagatedToIt() throws MuleException { CoreEvent initialEventWithVars = CoreEvent.builder(initialEvent).addVariable(INIT_VAR_NAME, INIT_VAR_VALUE).build(); when(policy.getPolicyChain().apply(any())).thenAnswer(invocation -> subscriberContext() .flatMap(ctx -> Mono.<CoreEvent>from(invocation.getArgument(0)).transform(ctx.get(POLICY_NEXT_OPERATION)))); just(initialEventWithVars).transform(policyProcessor).block(); verify(flowProcessor).apply(eventCaptor.capture()); assertEquals(((CoreEvent) from(eventCaptor.getValue()).block()).getVariables().keySet(), initialEventWithVars.getVariables().keySet()); }
@Test public void variablesAddedInNextProcessorNotPropagated() throws MuleException { CoreEvent initialEventWithVars = CoreEvent.builder(initialEvent).addVariable(INIT_VAR_NAME, INIT_VAR_VALUE).build(); CoreEvent modifiedVarsEvent = CoreEvent.builder(initialEvent).addVariable(ADDED_VAR_NAME, ADDED_VAR_VALUE).build(); mockFlowReturningEvent(modifiedVarsEvent); when(policy.getPolicyChain().apply(any())).thenAnswer(invocation -> Flux.<CoreEvent>from(invocation.getArgument(0))); CoreEvent resultEvent = just(initialEventWithVars).transform(policyProcessor).block(); assertEquals(resultEvent.getVariables().keySet(), initialEventWithVars.getVariables().keySet()); }
@Test public void setsDefaultFlowVariableDataType() throws Exception { CoreEvent muleEvent = CoreEvent.builder(testEvent()).addVariable(TEST_PROPERTY, TEST_PAYLOAD).build(); assertVariableDataType(muleEvent, STRING); }
@Test public void testFlowVarNamesAddImmutable() throws Exception { CoreEvent event = getEventBuilder() .message(of("whatever")) .addVariable("test", "val") .build(); expectedException.expect(UnsupportedOperationException.class); event.getVariables().keySet().add("other"); }
@Test public void setFlowVariableCustomDataType() throws Exception { DataType dataType = DataType.builder().type(String.class).mediaType(APPLICATION_XML).charset(CUSTOM_ENCODING).build(); muleEvent = (PrivilegedEvent) CoreEvent.builder(muleEvent).addVariable(PROPERTY_NAME, PROPERTY_VALUE, dataType).build(); DataType actualDataType = muleEvent.getVariables().get(PROPERTY_NAME).getDataType(); assertThat(actualDataType, like(String.class, APPLICATION_XML, CUSTOM_ENCODING)); }
@Benchmark public CoreEvent addEventVariable() { return CoreEvent.builder(event).addVariable(KEY, VALUE).build(); }
@Benchmark public CoreEvent addEventVariableEventWith50VariablesProperties() { return CoreEvent.builder(eventWith50VariablesProperties).addVariable(KEY, VALUE).build(); }
@Test public void batchSizeWithCollectionAttributes() throws Exception { foreach = createForeach(); List<Processor> processors = getSimpleMessageProcessors(new TestMessageProcessor("zas")); foreach.setMessageProcessors(processors); foreach.setBatchSize(2); foreach.setCollectionExpression("vars.collection"); initialiseIfNeeded(foreach, muleContext); foreach.process(eventBuilder(muleContext).addVariable("collection", asList(1, 2, 3)).message(of(null)).build()); assertThat(processedEvents, hasSize(2)); assertThat(((PrivilegedEvent) processedEvents.get(0)).getMessageAsString(muleContext), is("[1, 2]:foo:zas")); assertThat(((PrivilegedEvent) processedEvents.get(1)).getMessageAsString(muleContext), is("[3]:foo:zas")); }
private void assertRemainingMoney(String configName, String name, long expectedAmount) throws Exception { CoreEvent event = CoreEvent.builder(testEvent()).message(of("")).addVariable("myName", name).build(); HeisenbergExtension heisenbergExtension = ExtensionsTestUtils.getConfigurationFromRegistry(configName, event, muleContext); assertThat(heisenbergExtension.getMoney(), equalTo(BigDecimal.valueOf(expectedAmount))); }
private CoreEvent createMuleEvent(Message message, int numProperties) { final Builder builder; try { builder = CoreEvent.builder(create(flow, CONNECTOR_LOCATION)).message(message); } catch (Exception e) { throw new RuntimeException(e); } for (int i = 1; i <= numProperties; i++) { builder.addVariable("FlOwVaRiAbLeKeY" + i, "val"); } return builder.build(); }
@Test public void setsCustomFlowVariableDataType() throws Exception { DataType dataType = DataType.builder().type(String.class).mediaType(APPLICATION_XML).charset(CUSTOM_ENCODING).build(); CoreEvent muleEvent = CoreEvent.builder(testEvent()).addVariable(TEST_PROPERTY, TEST_PAYLOAD, dataType).build(); assertVariableDataType(muleEvent, dataType); }
@Test public void setFlowVariableDefaultDataType() throws Exception { muleEvent = (PrivilegedEvent) CoreEvent.builder(muleEvent).addVariable(PROPERTY_NAME, PROPERTY_VALUE).build(); DataType dataType = muleEvent.getVariables().get(PROPERTY_NAME).getDataType(); assertThat(dataType, like(String.class, MediaType.ANY, null)); }
@Setup public void setup() throws MuleException { muleContext = createMuleContextWithServices(); expressionManager = muleContext.getExpressionManager(); context = create(createFlow(muleContext), CONNECTOR_LOCATION); event = CoreEvent.builder(context).message(of(PAYLOAD)).addVariable("foo", "bar").build(); }
@Override public CoreEvent asReturnValue(Object value, ExecutionContextAdapter operationContext) { TypedValue result = expressionManager .evaluate(targetValue, getTargetBindingContext(toMessage(value, operationContext))); return CoreEvent.builder(operationContext.getEvent()) .securityContext(operationContext.getSecurityContext()) .addVariable(this.target, result.getValue(), result.getDataType()) .build(); } }
@Test public void dynamicHeisenbergWasInjected() throws Exception { assertCorrectProviderInjected(DYNAMIC_AGE_HEISENBERG, dependent.getDynamicAgeHeisenberg()); final int age = 52; CoreEvent event = CoreEvent.builder(testEvent()).message(of("")).addVariable("age", age).build(); HeisenbergExtension heisenberg = ExtensionsTestUtils.getConfigurationFromRegistry(DYNAMIC_AGE_HEISENBERG, event, muleContext); assertThat(heisenberg.getPersonalInfo().getAge(), is(age)); }
@Test public void testFlowVarsNotShared() throws Exception { CoreEvent event = getEventBuilder() .message(of("whatever")) .addVariable("foo", "bar") .build(); event = CoreEvent.builder(event).addVariable("foo", "bar").build(); CoreEvent copy = CoreEvent.builder(event).build(); copy = CoreEvent.builder(copy).addVariable("foo", "bar2").build(); assertEquals("bar", event.getVariables().get("foo").getValue()); assertEquals("bar2", copy.getVariables().get("foo").getValue()); }
@Test @Description("Verifies that flowVars work, returning null for non existent ones and it's value for those that do.") public void flowVars() throws MuleException { CoreEvent.Builder eventBuilder = CoreEvent.builder(testEvent()); String flowVars = "flowVars.myVar"; assertThat(expressionManager.evaluate(flowVars, eventBuilder.build()).getValue(), nullValue()); String value = "Leda"; eventBuilder.addVariable(MY_VAR, value); assertThat(expressionManager.evaluate(flowVars, eventBuilder.build()).getValue(), is(value)); }
@Test public void testFlowVarNamesRemoveImmutable() throws Exception { CoreEvent event = getEventBuilder() .message(of("whatever")) .addVariable("test", "val") .build(); event = CoreEvent.builder(event).addVariable("test", "val").build(); expectedException.expect(UnsupportedOperationException.class); event.getVariables().keySet().remove("test"); }
@Test public void flowVariablesMap() throws Exception { event = CoreEvent.builder(event).addVariable("foo", "bar").build(); assertTrue(evaluate("flowVars", event) instanceof Map); }