private boolean shouldOverrideMetadataFormat(ExecutableComponentDeclaration declaration) { /** * On top of looking for the CustomDefinedStaticTypeAnnotation to see if there is a Static Resolution involved, you have to * check that the MetadataFormat is not JAVA, since there are other ways of Static Resolved Metadata that do not add this * annotation (none of the set the metadata format as JAVA), for example: @OutputJsonType and @OutputXmlType. */ return !declaration.getOutput().getType().getAnnotation(CustomDefinedStaticTypeAnnotation.class).isPresent() && declaration.getOutput().getType().getMetadataFormat().equals(JAVA); }
private void assertOutputType(OutputDeclaration output, MetadataType type, boolean isDynamic) { assertThat(output.getType(), equalTo(type)); assertThat(output.hasDynamicType(), is(isDynamic)); }
private OutputDeclaration toDeclaration(MetadataType type) { OutputDeclaration declaration = new OutputDeclaration(); declaration.setType(type, false); return declaration; } }
private void replaceOutputType(ExecutableComponentDeclaration<?> declaration, MediaTypeModelProperty property, Function<MetadataFormat, MetadataType> type) { if (!shouldOverrideMetadataFormat(declaration)) { return; } property.getMediaType().ifPresent(mediaType -> { final OutputDeclaration output = declaration.getOutput(); output.setType(type.apply(toMetadataFormat(mediaType)), output.hasDynamicType()); }); }
private void replaceOutputType(ExecutableComponentDeclaration<?> declaration, MediaTypeModelProperty property, Function<MetadataFormat, MetadataType> type) { final MediaType mediaType = property.getMediaType(); if (mediaType.matches(ANY)) { return; } final OutputDeclaration output = declaration.getOutput(); output.setType(type.apply(toMetadataFormat(mediaType)), output.hasDynamicType()); }
private void mockOutput(WithOutputDeclaration declaration, MetadataType type) { OutputDeclaration output = mock(OutputDeclaration.class); when(output.getType()).thenReturn(type); when(declaration.getOutput()).thenReturn(output); }
private void assertOutputType(OutputDeclaration output, MetadataType type, boolean isDynamic) { assertThat(output.getType(), equalTo(type)); assertThat(output.hasDynamicType(), is(isDynamic)); }
private OutputDeclaration toDeclaration(MetadataType type) { OutputDeclaration declaration = new OutputDeclaration(); declaration.setType(type, false); return declaration; } }
private void declareMimeTypeParameters(ExecutableComponentDeclaration<?> declaration, Optional<MetadataType> outputType) { MediaTypeModelProperty property = declaration.getModelProperty(MediaTypeModelProperty.class).orElse(null); outputType.orElse(declaration.getOutput().getType()).accept(new MetadataTypeVisitor() {
@Test public void messageOperationWithoutGenerics() throws Exception { ExtensionDeclarer declarer = loaderFor(HeisenbergWithGenericlessMessageOperation.class) .declare(new DefaultExtensionLoadingContext(HeisenbergWithSameOperationsAndConfigs.class.getClassLoader(), getDefault(emptySet()))); OperationDeclaration operation = getOperation(declarer.getDeclaration(), "noGenerics"); assertThat(operation.getOutput().getType(), is(instanceOf(AnyType.class))); assertThat(operation.getOutputAttributes().getType(), is(instanceOf(AnyType.class))); }
@Override public void visitString(StringType stringType) { if (property == null || stringType.getAnnotation(EnumAnnotation.class).isPresent()) { return; } if (!property.isStrict()) { declareOutputMimeTypeParameter(declaration.getParameterGroup(DEFAULT_GROUP_NAME)); } replaceOutputType(declaration, property, format -> { StringTypeBuilder stringTypeBuilder = BaseTypeBuilder.create(format).stringType(); enrichWithAnnotations(stringTypeBuilder, declaration.getOutput().getType().getAnnotations()); return stringTypeBuilder.build(); }); }
@Test public void listOfResultsOperation() throws Exception { ExtensionDeclarer declarer = loaderFor(HeisenbergWithListOfResultOperations.class) .declare(new DefaultExtensionLoadingContext(getClass().getClassLoader(), getDefault(emptySet()))); OperationDeclaration operation = getOperation(declarer.getDeclaration(), "listOfResults"); MetadataType outputType = operation.getOutput().getType(); assertThat(outputType, is(instanceOf(ArrayType.class))); assertMessageType(((ArrayType) outputType).getType(), TYPE_LOADER.load(Integer.class), TYPE_LOADER.load(IntegerAttributes.class)); assertThat(operation.getOutputAttributes().getType(), is(instanceOf(VoidType.class))); }
@Override public void visitBinaryType(BinaryType binaryType) { if (property == null) { return; } if (!property.isStrict()) { ParameterGroupDeclaration group = declaration.getParameterGroup(DEFAULT_GROUP_NAME); declareOutputMimeTypeParameter(group); declareOutputEncodingParameter(group); } replaceOutputType(declaration, property, format -> { BinaryTypeBuilder builder = BaseTypeBuilder.create(format).binaryType(); enrichWithAnnotations(builder, declaration.getOutput().getType().getAnnotations()); return builder.build(); }); }
private void assertTestModuleMessageSource(ExtensionDeclaration extensionDeclaration) throws Exception { assertThat(extensionDeclaration.getMessageSources(), hasSize(1)); SourceDeclaration source = extensionDeclaration.getMessageSources().get(0); assertThat(source.getName(), is(DEA_SOURCE_NAME)); final MetadataType outputType = source.getOutput().getType(); assertThat(outputType, is(instanceOf(ArrayType.class))); MetadataType outputMetadataType = IntrospectionUtils.getReturnType(new TypeWrapper(Object.class, TYPE_LOADER)); assertMessageType(((ArrayType) outputType).getType(), STRING_TYPE, TYPE_LOADER.load(DEAOfficerAttributes.class)); assertThat(source.getOutputAttributes().getType(), equalTo(outputMetadataType)); ConfigurationDeclaration config = extensionDeclaration.getConfigurations().get(0); assertThat(config.getMessageSources(), hasSize(3)); assertHeisenbergSource(config.getMessageSources().get(0), ASYNC_SOURCE_NAME, AsyncHeisenbergSource.class); assertHeisenbergSource(config.getMessageSources().get(1), SOURCE_NAME, HeisenbergSource.class); }
@Override public void visitAnyType(AnyType anyType) { if (property == null) { return; } if (!property.isStrict()) { boolean shouldAddSinceMuleVersionModelProperty = !isInputStream(anyType); ParameterGroupDeclaration group = declaration.getParameterGroup(DEFAULT_GROUP_NAME); declareOutputMimeTypeParameter(group, shouldAddSinceMuleVersionModelProperty); declareOutputEncodingParameter(group, shouldAddSinceMuleVersionModelProperty); } replaceOutputType(declaration, property, format -> { AnyTypeBuilder anyTypeBuilder = BaseTypeBuilder.create(format).anyType(); enrichWithAnnotations(anyTypeBuilder, declaration.getOutput().getType().getAnnotations()); return anyTypeBuilder.build(); }); }
@Test public void listOfResultsOperationWithoutGenerics() throws Exception { ExtensionDeclarer declarer = loaderFor(HeisenbergWithListOfResultOperations.class) .declare(new DefaultExtensionLoadingContext(getClass().getClassLoader(), getDefault(emptySet()))); OperationDeclaration operation = getOperation(declarer.getDeclaration(), "listOfResultsWithoutGenerics"); MetadataType outputType = operation.getOutput().getType(); assertThat(outputType, is(instanceOf(ArrayType.class))); assertMessageType(((ArrayType) outputType).getType(), TYPE_BUILDER.anyType().build(), TYPE_BUILDER.anyType().build()); }
assertThat(operation, is(notNullValue())); assertThat(operation.getAllParameters(), hasSize(1)); assertThat(operation.getOutput().getType(), equalTo(STRING_TYPE)); assertThat(operation.getOutputAttributes().getType(), equalTo(toMetadataType(IntegerAttributes.class))); assertParameter(operation.getAllParameters(), "index", "", INT_TYPE, false, SUPPORTED, "0"); assertConnected(operation, false); assertFalse(operation.getDeprecation().isPresent()); assertThat(operation.getAllParameters(), hasSize(0)); assertThat(operation.getOutput().getType(), is(instanceOf(ArrayType.class))); assertMessageType(((ArrayType) operation.getOutput().getType()).getType(), STRING_TYPE, TYPE_LOADER.load(IntegerAttributes.class)); assertThat(operation.getOutputAttributes().getType(), is(instanceOf(VoidType.class))); assertConnected(operation, false); assertTransactional(operation, false); assertThat(operation, is(notNullValue())); assertThat(operation.getAllParameters(), hasSize(2)); assertThat(operation.getOutput().getType(), equalTo(STRING_TYPE)); assertThat(operation.getOutputAttributes().getType(), is(instanceOf(VoidType.class))); assertParameter(operation.getAllParameters(), "victim", "", STRING_TYPE, false, SUPPORTED, "#[payload]"); assertParameter(operation.getAllParameters(), "goodbyeMessage", "", STRING_TYPE, false, SUPPORTED, "We are done"); assertParameter(operation.getAllParameters(), "grams", "", TYPE_LOADER.load(int[][].class), false, SUPPORTED, "#[payload]"); assertThat(operation.getOutput().getType(), is(TYPE_LOADER.load(int[][].class))); assertConnected(operation, false); assertTransactional(operation, false);
private void declareMimeTypeParameters(ExecutableComponentDeclaration<?> declaration) { MediaTypeModelProperty property = declaration.getModelProperty(MediaTypeModelProperty.class).orElse(null); declaration.getOutput().getType().accept(new MetadataTypeVisitor() {
@Override public void visitBinaryType(BinaryType binaryType) { if (property == null) { String componentType = NameUtils.getDeclarationTypeName(declaration); throw new IllegalModelDefinitionException(String.format( "%s '%s' has a binary output but doesn't specify a default mime type. Please annotate it with @%s", componentType, declaration.getName(), org.mule.runtime.extension.api.annotation.param.MediaType.class .getSimpleName())); } if (!property.isStrict()) { ParameterGroupDeclaration group = declaration.getParameterGroup(DEFAULT_GROUP_NAME); declareOutputMimeTypeParameter(group); declareOutputEncodingParameter(group); } replaceOutputType(declaration, property, format -> { BinaryTypeBuilder builder = BaseTypeBuilder.create(format).binaryType(); declaration.getOutput().getType().getAnnotation(ClassInformationAnnotation.class).ifPresent(builder::with); return builder.build(); }); } });