Search in sources :

Example 6 with ParameterGroupDescriptor

use of org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor in project mule by mulesoft.

the class ImplicitObjectUtils method buildImplicitResolverSet.

/**
 * Creates a {@link ResolverSet} based on the default values for the {@link ParameterModel}s in the {@code parametrizedModel}.
 * <p>
 * If a {@link ParameterModel} returns {@code null} for {@link ParameterModel#getDefaultValue()} then it's ignored
 *
 * @param model a model holding the {@link ParameterModel}s to consider
 * @param muleContext the Mule node.
 * @return a {@link ResolverSet}
 */
public static ResolverSet buildImplicitResolverSet(ParameterizedModel model, ReflectionCache reflectionCache, MuleContext muleContext) {
    ResolverSet resolverSet = new HashedResolverSet(muleContext);
    ParametersResolver parametersResolver = ParametersResolver.fromDefaultValues(model, muleContext, reflectionCache);
    for (ParameterGroupModel groupModel : model.getParameterGroupModels()) {
        Optional<ParameterGroupDescriptor> descriptor = groupModel.getModelProperty(ParameterGroupModelProperty.class).map(ParameterGroupModelProperty::getDescriptor);
        if (descriptor.isPresent() && groupModel.getParameterModels().stream().noneMatch(ParameterModel::isRequired)) {
            String groupKey = getContainerName(descriptor.get().getContainer());
            resolverSet.add(groupKey, NullSafeValueResolverWrapper.of(new StaticValueResolver<>(null), descriptor.get().getMetadataType(), reflectionCache, muleContext, parametersResolver));
        } else {
            groupModel.getParameterModels().forEach(parameterModel -> {
                Object defaultValue = parameterModel.getDefaultValue();
                ValueResolver<?> resolver;
                if (defaultValue instanceof String) {
                    resolver = getExpressionBasedValueResolver((String) defaultValue, parameterModel.getType(), muleContext);
                } else {
                    resolver = new StaticValueResolver<>(null);
                }
                if (parameterModel.getModelProperty(NullSafeModelProperty.class).isPresent()) {
                    MetadataType metadataType = parameterModel.getModelProperty(NullSafeModelProperty.class).get().defaultType();
                    resolver = NullSafeValueResolverWrapper.of(resolver, metadataType, reflectionCache, muleContext, parametersResolver);
                }
                resolverSet.add(parameterModel.getName(), resolver);
            });
        }
    }
    return resolverSet;
}
Also used : HashedResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.HashedResolverSet) ResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet) NullSafeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.NullSafeModelProperty) HashedResolverSet(org.mule.runtime.module.extension.internal.runtime.resolver.HashedResolverSet) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) ParameterGroupModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty) MetadataType(org.mule.metadata.api.model.MetadataType) ParametersResolver(org.mule.runtime.module.extension.internal.runtime.resolver.ParametersResolver) ParameterGroupModel(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel) StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver)

Example 7 with ParameterGroupDescriptor

use of org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor in project mule by mulesoft.

the class GroupValueSetterTestCase method before.

@Before
public void before() throws Exception {
    final String personalInfo = "personalInfo";
    ParameterGroupDescriptor group = new ParameterGroupDescriptor("group", new TypeWrapper(PersonalInfo.class, new JavaTypeLoader(Thread.currentThread().getContextClassLoader())), ExtensionsTypeLoaderFactory.getDefault().createTypeLoader().load(PersonalInfo.class), getField(HeisenbergExtension.class, personalInfo, reflectionCache).get(), null);
    Map<String, Object> resultMap = new HashMap<>();
    resultMap.put("name", NAME);
    resultMap.put("age", AGE);
    resultMap.put("dateOfBirth", DATE);
    when(result.asMap()).thenReturn(resultMap);
    valueSetter = new GroupValueSetter(group, () -> reflectionCache);
}
Also used : TypeWrapper(org.mule.runtime.module.extension.internal.loader.java.type.runtime.TypeWrapper) PersonalInfo(org.mule.test.heisenberg.extension.model.PersonalInfo) HashMap(java.util.HashMap) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) JavaTypeLoader(org.mule.metadata.java.api.JavaTypeLoader) Before(org.junit.Before)

Example 8 with ParameterGroupDescriptor

use of org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor in project mule by mulesoft.

the class ParameterModelsLoaderDelegate method declaredAsGroup.

private List<ParameterDeclarer> declaredAsGroup(HasParametersDeclarer component, ParameterDeclarationContext declarationContext, ExtensionParameter groupParameter) throws IllegalParameterModelDefinitionException {
    ParameterGroup groupAnnotation = groupParameter.getAnnotation(ParameterGroup.class).orElse(null);
    if (groupAnnotation == null) {
        return emptyList();
    }
    final String groupName = groupAnnotation.name();
    if (DEFAULT_GROUP_NAME.equals(groupName)) {
        throw new IllegalParameterModelDefinitionException(format("%s '%s' defines parameter group of name '%s' which is the default one. " + "@%s cannot be used with the default group name", getComponentDeclarationTypeName(((Declarer) component).getDeclaration()), ((NamedDeclaration) ((Declarer) component).getDeclaration()).getName(), groupName, ParameterGroup.class.getSimpleName()));
    }
    final Type type = groupParameter.getType();
    final List<FieldElement> nestedGroups = type.getAnnotatedFields(ParameterGroup.class);
    if (!nestedGroups.isEmpty()) {
        throw new IllegalParameterModelDefinitionException(format("Class '%s' is used as a @%s but contains fields which also hold that annotation. Nesting groups is not allowed. " + "Offending fields are: [%s]", type.getName(), ParameterGroup.class.getSimpleName(), nestedGroups.stream().map(element -> element.getName()).collect(joining(","))));
    }
    if (groupParameter.isAnnotatedWith(org.mule.runtime.extension.api.annotation.param.Optional.class)) {
        throw new IllegalParameterModelDefinitionException(format("@%s can not be applied alongside with @%s. Affected parameter is [%s].", org.mule.runtime.extension.api.annotation.param.Optional.class.getSimpleName(), ParameterGroup.class.getSimpleName(), groupParameter.getName()));
    }
    ParameterGroupDeclarer declarer = component.onParameterGroup(groupName);
    if (declarer.getDeclaration().getModelProperty(ParameterGroupModelProperty.class).isPresent()) {
        throw new IllegalParameterModelDefinitionException(format("Parameter group '%s' has already been declared on %s '%s'", groupName, getComponentDeclarationTypeName(((Declarer) component).getDeclaration()), ((NamedDeclaration) ((Declarer) component).getDeclaration()).getName()));
    } else {
        declarer.withModelProperty(new ParameterGroupModelProperty(new ParameterGroupDescriptor(groupName, type, groupParameter.getType().asMetadataType(), // TODO: Eliminate dependency to Annotated Elements
        groupParameter.getDeclaringElement().orElse(null), groupParameter)));
    }
    final List<FieldElement> annotatedParameters = type.getAnnotatedFields(Parameter.class);
    type.getAnnotation(ExclusiveOptionals.class).ifPresent(annotation -> {
        Set<String> optionalParamNames = annotatedParameters.stream().filter(f -> !f.isRequired()).map(WithAlias::getAlias).collect(toSet());
        declarer.withExclusiveOptionals(optionalParamNames, annotation.isOneRequired());
    });
    declarer.withDslInlineRepresentation(groupAnnotation.showInDsl());
    groupParameter.getAnnotation(DisplayName.class).ifPresent(displayName -> declarer.withDisplayModel(DisplayModel.builder().displayName(displayName.value()).build()));
    parseLayoutAnnotations(groupParameter, LayoutModel.builder()).ifPresent(declarer::withLayout);
    declarer.withModelProperty(new ExtensionParameterDescriptorModelProperty(groupParameter));
    if (!annotatedParameters.isEmpty()) {
        return declare(component, annotatedParameters, declarationContext, declarer);
    } else {
        return declare(component, getFieldsWithGetters(type), declarationContext, declarer);
    }
}
Also used : FieldElement(org.mule.runtime.module.extension.api.loader.java.type.FieldElement) MuleExtensionAnnotationParser.parseLayoutAnnotations(org.mule.runtime.module.extension.internal.loader.java.MuleExtensionAnnotationParser.parseLayoutAnnotations) ImplementingParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ImplementingParameterModelProperty) NameUtils.getComponentDeclarationTypeName(org.mule.runtime.extension.api.util.NameUtils.getComponentDeclarationTypeName) HasParametersDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.HasParametersDeclarer) NullSafeModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.NullSafeModelProperty) DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) ArrayType(org.mule.metadata.api.model.ArrayType) DisplayName(org.mule.runtime.extension.api.annotation.param.display.DisplayName) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) ParameterDsl(org.mule.runtime.extension.api.annotation.dsl.xml.ParameterDsl) ParameterDeclarerContributor(org.mule.runtime.module.extension.internal.loader.java.contributor.ParameterDeclarerContributor) Collectors.toSet(java.util.stream.Collectors.toSet) ParameterDslConfiguration(org.mule.runtime.api.meta.model.ParameterDslConfiguration) IntrospectionUtils.getFieldsWithGetters(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getFieldsWithGetters) ExtensionMetadataTypeUtils.isMap(org.mule.runtime.extension.api.util.ExtensionMetadataTypeUtils.isMap) IllegalParameterModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalParameterModelDefinitionException) ObjectType(org.mule.metadata.api.model.ObjectType) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) ExclusiveParametersModel(org.mule.runtime.api.meta.model.parameter.ExclusiveParametersModel) Collections.emptyList(java.util.Collections.emptyList) WithAlias(org.mule.runtime.module.extension.api.loader.java.type.WithAlias) Connection(org.mule.runtime.extension.api.annotation.param.Connection) ExclusiveOptionals(org.mule.runtime.extension.api.annotation.param.ExclusiveOptionals) Set(java.util.Set) String.format(java.lang.String.format) Collectors.joining(java.util.stream.Collectors.joining) DisplayModel(org.mule.runtime.api.meta.model.display.DisplayModel) List(java.util.List) ParameterGroupDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ParameterGroupDeclarer) NullSafe(org.mule.runtime.extension.api.annotation.param.NullSafe) LayoutModel(org.mule.runtime.api.meta.model.display.LayoutModel) Annotation(java.lang.annotation.Annotation) MetadataType(org.mule.metadata.api.model.MetadataType) ExtensionParameterDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionParameterDescriptorModelProperty) ExtensionParameter(org.mule.runtime.module.extension.api.loader.java.type.ExtensionParameter) Expression(org.mule.runtime.extension.api.annotation.Expression) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) AnnotatedElement(java.lang.reflect.AnnotatedElement) ConfigOverride(org.mule.runtime.extension.api.annotation.param.ConfigOverride) HasNestedComponentsDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.HasNestedComponentsDeclarer) MetadataKeyId(org.mule.runtime.extension.api.annotation.metadata.MetadataKeyId) DefaultImplementingTypeModelProperty(org.mule.runtime.extension.api.property.DefaultImplementingTypeModelProperty) Config(org.mule.runtime.extension.api.annotation.param.Config) ArrayList(java.util.ArrayList) ExtensionModelUtils.roleOf(org.mule.runtime.extension.api.util.ExtensionModelUtils.roleOf) ImmutableExclusiveParametersModel(org.mule.runtime.extension.api.model.parameter.ImmutableExclusiveParametersModel) Content(org.mule.runtime.extension.api.annotation.param.Content) IntrospectionUtils.getExpressionSupport(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getExpressionSupport) DEFAULT_GROUP_NAME(org.mule.runtime.api.meta.model.parameter.ParameterGroupModel.DEFAULT_GROUP_NAME) BasicTypeMetadataVisitor(org.mule.metadata.api.visitor.BasicTypeMetadataVisitor) ParameterGroupModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty) ParameterGroupDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterGroupDeclaration) Parameter(org.mule.runtime.extension.api.annotation.param.Parameter) ParameterGroup(org.mule.runtime.extension.api.annotation.param.ParameterGroup) ExclusiveOptionalModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ExclusiveOptionalModelProperty) ModelLoaderUtils.isProcessorChain(org.mule.runtime.module.extension.internal.loader.utils.ModelLoaderUtils.isProcessorChain) NamedDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.NamedDeclaration) ExclusiveParametersDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExclusiveParametersDeclaration) Field(java.lang.reflect.Field) Collectors.toList(java.util.stream.Collectors.toList) ParameterDeclarationContext(org.mule.runtime.module.extension.internal.loader.utils.ParameterDeclarationContext) ParameterDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ParameterDeclarer) Declarer(org.mule.runtime.api.meta.model.declaration.fluent.Declarer) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) ExclusiveOptionals(org.mule.runtime.extension.api.annotation.param.ExclusiveOptionals) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) FieldElement(org.mule.runtime.module.extension.api.loader.java.type.FieldElement) ParameterGroupModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty) ExtensionParameterDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionParameterDescriptorModelProperty) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) ArrayType(org.mule.metadata.api.model.ArrayType) ObjectType(org.mule.metadata.api.model.ObjectType) MetadataType(org.mule.metadata.api.model.MetadataType) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) ParameterGroup(org.mule.runtime.extension.api.annotation.param.ParameterGroup) DisplayName(org.mule.runtime.extension.api.annotation.param.display.DisplayName) NamedDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.NamedDeclaration) IllegalParameterModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalParameterModelDefinitionException) ParameterGroupDeclarer(org.mule.runtime.api.meta.model.declaration.fluent.ParameterGroupDeclarer)

Example 9 with ParameterGroupDescriptor

use of org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor in project mule by mulesoft.

the class ParameterGroupModelValidatorTestCase method invalidModelDueEmptyParameterGroup.

@Test(expected = IllegalModelDefinitionException.class)
public void invalidModelDueEmptyParameterGroup() {
    ParameterGroupDescriptor descriptor = new ParameterGroupDescriptor("name", new TypeWrapper(EmptyGroupPojo.class, typeLoader), null, mock(AnnotatedElement.class), null);
    when(groupModel.getModelProperty(ParameterGroupModelProperty.class)).thenReturn(of(new ParameterGroupModelProperty(descriptor)));
    validate(extensionModel, validator);
}
Also used : TypeWrapper(org.mule.runtime.module.extension.internal.loader.java.type.runtime.TypeWrapper) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) ParameterGroupModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Example 10 with ParameterGroupDescriptor

use of org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor in project mule by mulesoft.

the class ParameterGroupModelValidatorTestCase method invalidModelDueToNonInstantiableParameterGroup.

@Test(expected = IllegalModelDefinitionException.class)
public void invalidModelDueToNonInstantiableParameterGroup() {
    TypeWrapper type = new TypeWrapper(Serializable.class, typeLoader);
    ParameterGroupDescriptor descriptor = new ParameterGroupDescriptor("name", type, null, mock(AnnotatedElement.class), null);
    when(groupModel.getModelProperty(ParameterGroupModelProperty.class)).thenReturn(of(new ParameterGroupModelProperty(descriptor)));
    when(groupModel.getParameterModels()).thenReturn(asList(parameterModel));
    validate(extensionModel, validator);
}
Also used : TypeWrapper(org.mule.runtime.module.extension.internal.loader.java.type.runtime.TypeWrapper) ParameterGroupDescriptor(org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) ParameterGroupModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty) SmallTest(org.mule.tck.size.SmallTest) Test(org.junit.Test)

Aggregations

ParameterGroupDescriptor (org.mule.runtime.module.extension.internal.loader.ParameterGroupDescriptor)10 ParameterGroupModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.ParameterGroupModelProperty)7 TypeWrapper (org.mule.runtime.module.extension.internal.loader.java.type.runtime.TypeWrapper)5 AnnotatedElement (java.lang.reflect.AnnotatedElement)4 Field (java.lang.reflect.Field)3 Test (org.junit.Test)3 SmallTest (org.mule.tck.size.SmallTest)3 String.format (java.lang.String.format)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Collectors.toList (java.util.stream.Collectors.toList)2 MetadataType (org.mule.metadata.api.model.MetadataType)2 ParameterGroupDeclarer (org.mule.runtime.api.meta.model.declaration.fluent.ParameterGroupDeclarer)2 ParameterGroupModel (org.mule.runtime.api.meta.model.parameter.ParameterGroupModel)2 DEFAULT_GROUP_NAME (org.mule.runtime.api.meta.model.parameter.ParameterGroupModel.DEFAULT_GROUP_NAME)2 DeclaringMemberModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty)2 NullSafeModelProperty (org.mule.runtime.module.extension.internal.loader.java.property.NullSafeModelProperty)2 ResolverSet (org.mule.runtime.module.extension.internal.runtime.resolver.ResolverSet)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1