@Test public void testHardcodedXsdType1AndExtractFieldsInVarsFlow() throws Exception { final CoreEvent muleEvent = flowRunner("testHardcodedXsdType1AndExtractFieldsInVarsFlow").run(); for (Map.Entry<String, Object> entry : USER_DATA.entrySet()) { assertThat(muleEvent.getVariables().containsKey("extracted-user-" + entry.getKey()), is(true)); assertThat(muleEvent.getVariables().get("extracted-user-" + entry.getKey()).getValue(), is(entry.getValue())); } }
@Test public void testHardcodedType1AndExtractFieldsInVarsFlow() throws Exception { final CoreEvent muleEvent = flowRunner("testHardcodedType1AndExtractFieldsInVarsFlow").run(); for (Map.Entry<String, Object> entry : EXPECTED_TYPE_1.entrySet()) { assertThat(muleEvent.getVariables().get("extracted-" + entry.getKey()).getValue(), is(entry.getValue())); } }
@Test public void testExtractingJsonResponseAndFeedingSimpleType() throws Exception { final CoreEvent muleEvent = flowRunner("testExtractingJsonResponseAndFeedingSimpleType").run(); final Map<String, TypedValue<?>> variables = muleEvent.getVariables(); assertThat(variables.get("checkingNotAvenue").getValue(), is(false)); assertThat(variables.get("checkingFromExpression").getValue(), is(true)); assertThat(variables.get("checkingFromHardcodedType1").getValue(), is(true)); assertThat(variables.get("checkingFromHardcodedType1WithVariables").getValue(), is(true)); }
@Test public void pagedOperationInTx() throws Exception { CoreEvent event = flowRunner("pagedOperationInTx").run(); Collection<Integer> accumulator = (Collection<Integer>) event.getVariables().get("accumulator").getValue(); assertThat(accumulator, is(notNullValue())); assertThat(accumulator, hasSize(2)); Iterator<Integer> it = accumulator.iterator(); Integer id1 = it.next(); Integer id2 = it.next(); assertThat(id1, equalTo(id2)); }
@Test public void munitSpy() throws Exception { CoreEvent internalEvent = flowRunner("munitSpy").run(); assertThat(internalEvent.getVariables().get("before").getValue(), is("true")); assertThat(internalEvent.getVariables().get("after").getValue(), is("true")); }
@Test @Description("Stores an object stream in a variable leaving without modifying the original payload") public void getObjectStreamWithTargetValue() throws Exception { CoreEvent event = flowRunner("getStreamWithTargetValue").withPayload(data).run(); assertThat(event.getVariables().get(MY_STREAM_VAR).getValue(), is(instanceOf(String.class))); assertThat(event.getVariables().get(MY_STREAM_VAR).getValue(), equalTo(data.get(0))); }
@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 munitSpyNoBefore() throws Exception { CoreEvent internalEvent = flowRunner("munitSpyNoBefore").run(); assertThat(internalEvent.getMessage().getPayload().getValue(), is("1")); assertThat(internalEvent.getVariables().get("before"), is(nullValue())); assertThat(internalEvent.getVariables().get("after").getValue(), is("true")); }
@Test public void testSetPayloadHardcodedFlowWithTarget() throws Exception { CoreEvent event = flowRunner("testSetPayloadHardcodedFlowWithTarget").run(); assertThat(event.getMessage().getPayload().getValue(), nullValue()); final TypedValue<?> targetVariable = event.getVariables().get("target-variable"); assertThat(targetVariable, notNullValue()); assertThat(targetVariable.getValue(), instanceOf(Message.class)); Message targetMessage = (Message) targetVariable.getValue(); assertThat(targetMessage.getPayload().getValue(), is("hardcoded value")); }
@Test public void testSetPayloadHardcodedFlowWithTargetOverridingAnExistingVariable() throws Exception { CoreEvent event = flowRunner("testSetPayloadHardcodedFlowWithTargetOverridingAnExistingVariable").run(); assertThat(event.getMessage().getPayload().getValue(), nullValue()); final TypedValue<?> targetVariable = event.getVariables().get("existing-variable"); assertThat(targetVariable, notNullValue()); assertThat(targetVariable.getValue(), instanceOf(Message.class)); Message targetMessage = (Message) targetVariable.getValue(); assertThat(targetMessage.getPayload().getValue(), is("hardcoded value")); }
@Test public void testDoNothingFlow() throws Exception { CoreEvent muleEvent = flowRunner("testDoNothingFlow").run(); assertThat(muleEvent.getMessage().getPayload().getValue(), is("before calling")); assertThat(muleEvent.getVariables().get("variableBeforeCalling").getValue(), is("value of flowvar before calling")); }
@Test public void twoRoutesRouterOther() throws Exception { CoreEvent internalEvent = flowRunner("twoRoutesRouter") .withVariable("executeWhen", false) .withVariable("executeOther", true).run(); assertThat(internalEvent.getMessage().getPayload().getValue(), is("mule:set-payload")); assertThat(internalEvent.getVariables().get("newPayload"), is(nullValue())); assertThat(internalEvent.getVariables().get("newAttributes"), is(nullValue())); }
protected void mockFlowReturningEvent(CoreEvent event) { when(flowProcessor.apply(any())).thenAnswer(inv -> from(inv.getArgument(0)) .map(e -> { final Builder builder = PrivilegedEvent.builder((CoreEvent) e) .message(event.getMessage()) .variables(event.getVariables()); if (event instanceof PrivilegedEvent) { builder.session(((PrivilegedEvent) event).getSession()); } return builder.build(); })); }
@Test @Description("Rewing a stream and consume it twice") public void rewind() throws Exception { CoreEvent result = flowRunner("rewind").withPayload(data).run(); Message firstRead = (Message) result.getVariables().get("firstRead").getValue(); Message secondRead = (Message) result.getVariables().get("secondRead").getValue(); assertThat(firstRead.getPayload().getValue(), equalTo(data)); assertThat(secondRead.getPayload().getValue(), equalTo(data)); }
@Test @Description("Stores an object stream in a variable leaving without modifying the original payload") public void getObjectStreamWithTargetVariable() throws Exception { CoreEvent event = flowRunner("getStreamWithTarget").keepStreamsOpen().withPayload(data).run(); assertThat(event.getVariables().get(MY_STREAM_VAR).getValue(), is(instanceOf(CursorIteratorProvider.class))); assertThat(IteratorUtils.toList(((CursorIteratorProvider) event.getVariables().get(MY_STREAM_VAR).getValue()).openCursor()), equalTo(data)); assertThat(event.getMessage().getPayload().getValue(), is(instanceOf(List.class))); assertThat(event.getMessage().getPayload().getValue(), equalTo(data)); }
@Test public void voidRouter() throws Exception { CoreEvent internalEvent = flowRunner("voidRouter").withPayload("message").withAttributes("other").run(); assertThat(internalEvent.getMessage().getPayload().getValue(), is("message")); assertThat(internalEvent.getMessage().getAttributes().getValue(), is("other")); assertThat(internalEvent.getVariables().get("newAttributes"), is(nullValue())); }
@Test public void twoRoutesRouterWhen() throws Exception { CoreEvent internalEvent = flowRunner("twoRoutesRouter") .withVariable("executeWhen", true) .withVariable("executeOther", false).run(); assertThat(internalEvent.getMessage().getPayload().getValue(), is("mule:set-payload")); assertThat(internalEvent.getVariables().get("newPayload").getValue(), is("mule:set-payload")); }
@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 flowVariable() throws Exception { Message message = of(""); CoreEvent event = InternalEvent.builder(context).message(message).addVariable("foo", "bar").build(); assertEquals(event.getVariables().get("foo").getValue(), evaluate("flowVars['foo']", event)); }
@Test public void assignValueToFlowVariable() throws Exception { Message message = of(""); CoreEvent event = InternalEvent.builder(context).message(message).addVariable("foo", "bar_old").build(); CoreEvent.Builder eventBuilder = CoreEvent.builder(event); evaluate("flowVars['foo']='bar'", event, eventBuilder); assertEquals("bar", eventBuilder.build().getVariables().get("foo").getValue()); }