@Test public void operationWithListResultStreamOutputOverridesContentType() throws Exception { runFlow("listStreamMediaType"); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getCharset().get().displayName().equals("UTF-16"))); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getPrimaryType().equals("application"))); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getSubType().equals("java"))); }
@Test public void operationWithPagingProviderResultCursorProviderOutputOverridesContentType() throws Exception { runFlow("pagedCursorProviderMediaType"); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getCharset().get().displayName().equals("UTF-16"))); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getPrimaryType().equals("application"))); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getSubType().equals("java"))); }
@Test public void operationWithListResultStringOutputOverridesContentType() throws Exception { runFlow("listStringMediaType"); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getPrimaryType().equals("application"))); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getSubType().equals("java"))); }
@Test public void createsDataTypeForNonCollection() { final DataType dataType = DataType.builder().collectionType(List.class).itemType(type).itemMediaType(mimeType).build(); assertThat(dataType.getType(), equalTo(List.class)); assertThat(dataType, instanceOf(DefaultCollectionDataType.class)); final DataType itemDataType = ((DefaultCollectionDataType) dataType).getItemDataType(); assertThat(itemDataType.getType(), equalTo(type)); assertThat(itemDataType.getMediaType().getPrimaryType(), is(mimeType.split("/")[0])); assertThat(itemDataType.getMediaType().getSubType(), is(mimeType.split("/")[1])); } }
@Test public void sourceWithAListAsResultOverridesContentType() throws Exception { startFlow("sourceListMimeType"); check(PROBER_TIMEOUT, PROBER_FREQUENCY, () -> MediaTypeCollectorProcessor.getMediaTypes().size() == 3); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getPrimaryType().equals("pet"))); assertTrue(MediaTypeCollectorProcessor.getMediaTypes().stream() .allMatch(mediaType -> mediaType.getSubType().equals("plain"))); }
@Test public void mimeTypeWithEncodingInformation() throws Exception { DataType dataType = DataType.builder().type(type).mediaType(format("%s; charset=UTF-8", mimeType)).charset(encoding).build(); assertThat(dataType.getType(), equalTo(type)); assertThat(dataType.getMediaType().getPrimaryType(), is(mimeType.split("/")[0])); assertThat(dataType.getMediaType().getSubType(), is(mimeType.split("/")[1])); assertThat(dataType.getMediaType().getCharset().get(), is(encoding)); }
@Test public void maintainsDataTypeOnGetPayloadTransformation() throws Exception { InputStream payload = mock(InputStream.class); Message message = Message.builder().value(payload).mediaType(APPLICATION_XML_CUSTOM).build(); MuleRegistry muleRegistry = mock(MuleRegistry.class); when(muleContext.getRegistry()).thenReturn(muleRegistry); Transformer transformer = mock(Transformer.class); when(transformer.transform(anyObject(), anyObject())).thenReturn(TEST_PAYLOAD); when(muleRegistry.lookupTransformer(any(), any())).thenReturn(transformer); assertThat(message.getPayload().getDataType().getMediaType().getPrimaryType(), equalTo(APPLICATION_XML.getPrimaryType())); assertThat(message.getPayload().getDataType().getMediaType().getSubType(), equalTo(APPLICATION_XML.getSubType())); assertThat(message.getPayload().getDataType().getMediaType().getCharset().get(), equalTo(CUSTOM_ENCODING)); }
@Test public void validMediaTypeWithCharset() throws TransformerException { final StringToMediaTypeTransformer transformer = new StringToMediaTypeTransformer(); final MediaType transformed = (MediaType) transformer.transform("text/plain;charset=" + US_ASCII.name()); assertThat(transformed.getPrimaryType(), is("text")); assertThat(transformed.getSubType(), is("plain")); assertThat(transformed.getCharset().get(), is(US_ASCII)); }
@Test public void validMediaType() throws TransformerException { final StringToMediaTypeTransformer transformer = new StringToMediaTypeTransformer(); final MediaType transformed = (MediaType) transformer.transform("text/plain"); assertThat(transformed.getPrimaryType(), is("text")); assertThat(transformed.getSubType(), is("plain")); assertThat(transformed.getCharset().isPresent(), is(false)); }
private void assertCustomMimeType(DataType dataType) { assertThat(dataType.getMediaType().getPrimaryType(), is("dead")); assertThat(dataType.getMediaType().getSubType(), is("dead")); }
@Test public void sourceOverridesContentType() throws Exception { startFlow("sourceMimeType"); check(PROBER_TIMEOUT, PROBER_FREQUENCY, () -> { if (MediaTypeCollectorProcessor.getMediaTypes().size() == 2) { return true; } return false; }); assertThat(MediaTypeCollectorProcessor.getMediaTypes().get(0).getCharset().get().displayName(), is("UTF-16")); assertThat(MediaTypeCollectorProcessor.getMediaTypes().get(0).getPrimaryType(), is("pet")); assertThat(MediaTypeCollectorProcessor.getMediaTypes().get(0).getSubType(), is("plain")); assertThat(MediaTypeCollectorProcessor.getMediaTypes().get(0).getParameter("header"), is("false")); assertThat(MediaTypeCollectorProcessor.getMediaTypes().get(1).getPrimaryType(), is("dead")); assertThat(MediaTypeCollectorProcessor.getMediaTypes().get(1).getSubType(), is("json")); }
private void assertDefaultMimeType(DataType dataType) throws Exception { assertThat(dataType.getMediaType().getPrimaryType(), is(getDefaultDataType().getMediaType().getPrimaryType())); assertThat(dataType.getMediaType().getSubType(), is(getDefaultDataType().getMediaType().getSubType())); }
@Test public void parseTemplateWithOverriddenDataTypeAsExpression() throws Exception { String customEncoding = "UTF-16"; MediaType customMediaType = create("application", "lrmextension"); parseTemplateProcessor.setLocation(UNKNOWN_MEDIATYPE_LOCATION); parseTemplateProcessor.setOutputMimeType(customMediaType.toRfcString()); parseTemplateProcessor.setOutputEncoding(customEncoding); parseTemplateProcessor.initialise(); String expectedExpression = IOUtils.getResourceAsString(UNKNOWN_MEDIATYPE_LOCATION, this.getClass()); when(mockExpressionManager.parseLogTemplate(eq(expectedExpression), eq(event), any(), any())).thenReturn("Parsed"); CoreEvent response = parseTemplateProcessor.process(event); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getPrimaryType(), is(equalTo(customMediaType.getPrimaryType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getSubType(), is(equalTo(customMediaType.getSubType()))); assertThat(response.getMessage().getPayload().getDataType().getMediaType().getCharset().get().toString(), is(equalTo(customEncoding))); }
@Test public void setsContentTypeOnXml() throws Exception { CoreEvent response = runFlow("setsContentTypeOnXml"); DataType dataType = response.getMessage().getPayload().getDataType(); assertCustomEncoding(dataType); assertThat(dataType.getMediaType().getPrimaryType(), is(MediaType.TEXT.getPrimaryType())); assertThat(dataType.getMediaType().getSubType(), is(MediaType.TEXT.getSubType())); }
@Test public void mimeTypeWithEncoding() { final DataType dataType = DataType.builder().mediaType("text/plain;charset=ASCII").build(); assertThat(dataType.getMediaType().getPrimaryType(), is("text")); assertThat(dataType.getMediaType().getSubType(), is("plain")); assertThat(dataType.getMediaType().getCharset().get(), is(US_ASCII)); }
@Test public void templateSimple() { final DataType template = DataType.builder().type(String.class).mediaType("text/plain;charset=ASCII").build(); final DataType dataType = DataType.builder(template).build(); assertThat(dataType, instanceOf(SimpleDataType.class)); assertThat(dataType.getType(), is(equalTo(String.class))); assertThat(dataType.getMediaType().getPrimaryType(), is("text")); assertThat(dataType.getMediaType().getSubType(), is("plain")); assertThat(dataType.getMediaType().getCharset().get(), is(US_ASCII)); }
@Test public void templateCollection() { final DataType template = DataType.builder().type(Set.class).mediaType("text/plain;charset=ASCII").build(); final DataType dataType = DataType.builder(template).build(); assertThat(dataType, instanceOf(DefaultCollectionDataType.class)); assertThat(dataType.getType(), is(equalTo(Set.class))); assertThat(((DefaultCollectionDataType) dataType).getItemDataType(), is(OBJECT)); assertThat(dataType.getMediaType().getPrimaryType(), is("text")); assertThat(dataType.getMediaType().getSubType(), is("plain")); assertThat(dataType.getMediaType().getCharset().get(), is(US_ASCII)); }
@Test public void templateMap() { final DataType template = DataType.builder().type(HashMap.class).mediaType("text/plain;charset=ASCII").build(); final DataType dataType = DataType.builder(template).build(); assertThat(dataType, instanceOf(DefaultMapDataType.class)); assertThat(dataType.getType(), is(equalTo(HashMap.class))); assertThat(((DefaultMapDataType) dataType).getKeyDataType(), is(OBJECT)); assertThat(((DefaultMapDataType) dataType).getValueDataType(), is(OBJECT)); assertThat(dataType.getMediaType().getPrimaryType(), is("text")); assertThat(dataType.getMediaType().getSubType(), is("plain")); assertThat(dataType.getMediaType().getCharset().get(), is(US_ASCII)); }
@Test public void setEncodingInMimeTypeAndParam() throws Exception { CoreEvent response = runFlow("setEncodingInMimeTypeAndParam"); DataType dataType = response.getMessage().getPayload().getDataType(); assertThat(dataType.getMediaType().getPrimaryType(), is("application")); assertThat(dataType.getMediaType().getSubType(), is("json")); assertThat(dataType.getMediaType().getCharset().get(), is(StandardCharsets.UTF_16)); }
@Test public void templateTypedMap() { final DataType template = DataType.builder() .mapType(HashMap.class) .keyType(String.class) .keyMediaType("text/plain;charset=UTF-8") .valueType(Number.class) .valueMediaType("application/json;charset=ISO-8859-1") .mediaType("text/plain;charset=ASCII") .build(); final DataType dataType = DataType.builder(template).build(); assertThat(dataType, instanceOf(DefaultMapDataType.class)); assertThat(dataType.getType(), is(equalTo(HashMap.class))); assertThat(((DefaultMapDataType) dataType).getKeyDataType(), is(assignableTo(TEXT_STRING))); assertThat(((DefaultMapDataType) dataType).getKeyDataType().getMediaType().getPrimaryType(), is("text")); assertThat(((DefaultMapDataType) dataType).getKeyDataType().getMediaType().getSubType(), is("plain")); assertThat(((DefaultMapDataType) dataType).getKeyDataType().getMediaType().getCharset().get(), is(UTF_8)); assertThat(((DefaultMapDataType) dataType).getValueDataType(), is(assignableTo((NUMBER)))); assertThat(((DefaultMapDataType) dataType).getValueDataType().getMediaType().getPrimaryType(), is("application")); assertThat(((DefaultMapDataType) dataType).getValueDataType().getMediaType().getSubType(), is("json")); assertThat(((DefaultMapDataType) dataType).getValueDataType().getMediaType().getCharset().get(), is(ISO_8859_1)); }