@Override public ValueResolver<Object> parse(String key, MetadataType metadataType, DslElementSyntax elementDsl) { return new StaticValueResolver<>(DataType.builder().mediaType(key).build().getMediaType()); } }
@Test public void ifCharsetItsNotSpecifiedItShouldBeCompatibleWithAny() throws Exception { DataType jsonWithCharset = builder(JSON_PARENT_DATA_TYPE).charset(UTF_8).build(); assertThat(JSON_PARENT_DATA_TYPE, is(not(assignableTo(jsonWithCharset)))); assertThat(jsonWithCharset, is(assignableTo(JSON_PARENT_DATA_TYPE))); }
@Test public void updatesMimeTypeOnTransformation() throws Exception { Message message = Message.builder().value(TEST_PAYLOAD).mediaType(ANY.withCharset(CUSTOM_ENCODING)).build(); Transformer transformer = mock(Transformer.class); when(transformer.isSourceDataTypeSupported(any())).thenReturn(true); DataType outputDataType = DataType.builder().type(Integer.class).mediaType(APPLICATION_XML).build(); when(transformer.getReturnDataType()).thenReturn(outputDataType); when(transformer.transform(any(Message.class))).thenReturn(1); CoreEvent muleEvent = mock(CoreEvent.class); Message result = transformationService.applyTransformers(message, muleEvent, singletonList(transformer)); assertDataType(result, Integer.class, APPLICATION_XML, CUSTOM_ENCODING); }
@Test(expected = IllegalArgumentException.class) public void rejectsValidMimeType() throws Exception { DataType.builder().mediaType("invalidMimeType").build(); } }
protected CoreEvent configureEvent() throws Exception { when(message.getPayload()) .thenReturn(new TypedValue<>(TEST_PAYLOAD, DataType.builder().mediaType(MediaType.create("*", "*", defaultCharset())).build())); when(message.getAttributes()) .thenReturn(new TypedValue<>(null, DataType.builder().fromObject(null).build())); return eventBuilder(muleContext).message(message).build(); }
@Test public void generatesContentTypeWithoutCharset() throws Exception { DataType dataType = DataType.builder().type(Object.class).mediaType(MediaType.APPLICATION_JSON).build(); String contentType = dataType.getMediaType().toRfcString(); assertThat(contentType, equalTo("application/json")); } }
@Test public void transformerWithNoCharsetDataTypeAsSourceFoundEvenWhenSpecified() throws Exception { Converter utf16ToJson = new MockConverterBuilder().from(builder(UTF_16_DATA_TYPE).charset((String) null).build()).to(JSON_DATA_TYPE).build(); graph.addConverter(utf16ToJson); List<Converter> converters = lookupStrategyTransformation.lookupConverters(UTF_16_DATA_TYPE, JSON_DATA_TYPE); assertThat(converters, hasSize(1)); assertThat(converters, contains(utf16ToJson)); }
@Test public void stringUTF16() { TypedValue<String> typedValue = new TypedValue<>(STRING_VALUE, builder(STRING).charset(UTF_16).build()); assertThat(typedValue.getByteLength().isPresent(), is(true)); assertThat(typedValue.getByteLength().getAsLong(), is((long) STRING_VALUE.getBytes(UTF_16).length)); }
@Test public void transformerWithNoCharsetDataTypeAsTargetNotFoundSinceIsMoreSpecific() throws Exception { Converter jsonToUtf16 = new MockConverterBuilder().from(JSON_DATA_TYPE).to(builder(UTF_16_DATA_TYPE).charset((String) null).build()).build(); graph.addConverter(jsonToUtf16); List<Converter> converters = lookupStrategyTransformation.lookupConverters(JSON_DATA_TYPE, UTF_16_DATA_TYPE); assertThat(converters, hasSize(0)); }
@Test public void testAddVariableWithEncoding() throws MuleException { addVariableProcessor.setIdentifier(PLAIN_STRING_KEY); addVariableProcessor.setValue(PLAIN_STRING_VALUE); addVariableProcessor.initialise(); addVariableProcessor.setReturnDataType(DataType.builder().charset(CUSTOM_ENCODING).build()); event = addVariableProcessor.process(event); verifyAdded(event, PLAIN_STRING_KEY, PLAIN_STRING_VALUE); assertThat(getVariableDataType(event, PLAIN_STRING_KEY), like(String.class, ANY, CUSTOM_ENCODING)); }
@Test public void testAddVariableWithMimeType() throws MimeTypeParseException, MuleException { addVariableProcessor.setIdentifier(PLAIN_STRING_KEY); addVariableProcessor.setValue(PLAIN_STRING_VALUE); addVariableProcessor.initialise(); addVariableProcessor.setReturnDataType(DataType.builder().mediaType(APPLICATION_XML).build()); event = addVariableProcessor.process(event); verifyAdded(event, PLAIN_STRING_KEY, PLAIN_STRING_VALUE); assertThat(getVariableDataType(event, PLAIN_STRING_KEY), like(String.class, APPLICATION_XML, getDefaultEncoding(mockMuleContext))); }
@Test public void returnsDataType() throws Exception { DataType dataType = DataType.builder().type(String.class).mediaType(JSON).charset(UTF_16.name()).build(); CoreEvent event = createEvent(TEST_MESSAGE, dataType); TypedValue typedValue = evaluateTyped("payload", event); assertThat((String) typedValue.getValue(), equalTo(TEST_MESSAGE)); assertThat(typedValue.getDataType(), like(String.class, JSON, UTF_16)); }
@Test public void setSessionVariableCustomDataType() throws Exception { DataType dataType = DataType.builder().type(String.class).mediaType(APPLICATION_XML).charset(CUSTOM_ENCODING).build(); muleEvent.getSession().setProperty(PROPERTY_NAME, PROPERTY_VALUE, dataType); DataType actualDataType = muleEvent.getSession().getPropertyDataType(PROPERTY_NAME); assertThat(actualDataType, like(String.class, APPLICATION_XML, CUSTOM_ENCODING)); }
@Test public void setsCustomPropertyDataType() throws Exception { MediaType mediaType = APPLICATION_XML_CUSTOM; InternalMessage message = InternalMessage.builder().value(TEST_PAYLOAD) .addOutboundProperty(TEST_PROPERTY, TEST_PAYLOAD, mediaType).build(); assertOutboundPropertyDataType(message, DataType.builder(STRING).mediaType(mediaType).build()); }
@Test public void setsCustomOutboundPropertyDataType() throws Exception { MediaType mediaType = APPLICATION_XML_CUSTOM; InternalMessage message = InternalMessage.builder().value(TEST_PAYLOAD) .addOutboundProperty(TEST_PROPERTY, TEST_PAYLOAD, mediaType).build(); assertOutboundPropertyDataType(message, DataType.builder().type(String.class).mediaType(mediaType).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 propagatesDataTypeForInlinedInvocationProperty() throws Exception { final DataType expectedDataType = DataType.builder().type(String.class).mediaType(JSON).charset(CUSTOM_ENCODING).build(); MVELExpressionLanguage expressionLanguage = new MVELExpressionLanguage(muleContext); final CompiledExpression compiledExpression = (CompiledExpression) compileExpression("foo = 'unused'", new ParserContext(expressionLanguage.getParserConfiguration())); PrivilegedEvent testEvent = this.<PrivilegedEvent.Builder>getEventBuilder().message(of(TEST_MESSAGE)).addVariable("foo", "bar").build(); final PrivilegedEvent.Builder builder = PrivilegedEvent.builder(testEvent); dataTypePropagator.propagate(testEvent, builder, new TypedValue<>(TEST_MESSAGE, expectedDataType), compiledExpression); assertThat(builder.build().getVariables().get("foo").getDataType(), like(String.class, JSON, CUSTOM_ENCODING)); }
@Test public void setsCustomEncoding() throws MuleException { setPayloadMessageProcessor.setValue(PLAIN_TEXT); setPayloadMessageProcessor.setDataType(DataType.builder().charset(CUSTOM_ENCODING).build()); setPayloadMessageProcessor.initialise(); CoreEvent response = setPayloadMessageProcessor.process(testEvent()); assertThat(response.getMessage().getPayload().getDataType(), like(String.class, MediaType.ANY, CUSTOM_ENCODING)); }
@Test public void setsCustomMimeType() throws MuleException { setPayloadMessageProcessor.setValue(PLAIN_TEXT); setPayloadMessageProcessor.setDataType(DataType.builder().mediaType(MediaType.APPLICATION_XML).build()); setPayloadMessageProcessor.initialise(); CoreEvent response = setPayloadMessageProcessor.process(testEvent()); assertThat(response.getMessage().getPayload().getDataType(), like(String.class, MediaType.APPLICATION_XML, null)); } }