Search in sources :

Example 1 with WithOperationsDeclaration

use of org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration in project mule by mulesoft.

the class NotificationsDeclarationEnricher method enrich.

@Override
public void enrich(ExtensionLoadingContext extensionLoadingContext) {
    ExtensionDeclaration declaration = extensionLoadingContext.getExtensionDeclarer().getDeclaration();
    Optional<ExtensionTypeDescriptorModelProperty> extensionType = declaration.getModelProperty(ExtensionTypeDescriptorModelProperty.class);
    String extensionNamespace = getExtensionsNamespace(declaration);
    ClassTypeLoader typeLoader = ExtensionsTypeLoaderFactory.getDefault().createTypeLoader();
    if (extensionType.isPresent() && extensionType.get().getType().getDeclaringClass().isPresent()) {
        Type extensionElement = extensionType.get().getType();
        Optional<NotificationActions> annotation = extensionElement.getAnnotation(NotificationActions.class);
        annotation.ifPresent(actionsAnnotation -> {
            NotificationActionDefinition<?>[] actions = (NotificationActionDefinition<?>[]) actionsAnnotation.value().getEnumConstants();
            Map<NotificationActionDefinition, NotificationModel> notificationModels = new HashMap<>();
            stream(actions).forEach(action -> {
                NotificationModel model = new ImmutableNotificationModel(extensionNamespace, ((Enum) action).name(), typeLoader.load(action.getDataType().getType()));
                declaration.addNotificationModel(model);
                notificationModels.put(action, model);
            });
            new IdempotentDeclarationWalker() {

                @Override
                public void onOperation(WithOperationsDeclaration owner, OperationDeclaration declaration) {
                    Optional<ExtensionOperationDescriptorModelProperty> modelProperty = declaration.getModelProperty(ExtensionOperationDescriptorModelProperty.class);
                    if (modelProperty.isPresent()) {
                        MethodElement method = modelProperty.get().getOperationMethod();
                        Optional<Fires> emitsNotifications = getOperationNotificationDeclaration(method, extensionElement);
                        includeNotificationDeclarationIfNeeded(declaration, emitsNotifications);
                    }
                }

                @Override
                public void onSource(SourceDeclaration declaration) {
                    Optional<ExtensionTypeDescriptorModelProperty> modelProperty = declaration.getModelProperty(ExtensionTypeDescriptorModelProperty.class);
                    if (modelProperty.isPresent()) {
                        Type sourceContainer = modelProperty.get().getType();
                        Optional<Fires> emitsNotifications = getNotificationDeclaration(sourceContainer, extensionElement);
                        includeNotificationDeclarationIfNeeded(declaration, emitsNotifications);
                    }
                }

                private Optional<Fires> getOperationNotificationDeclaration(MethodElement operationMethod, Type extensionElement) {
                    Type operationContainer = operationMethod.getEnclosingType();
                    return ofNullable(operationMethod.getAnnotation(Fires.class)).orElse(getNotificationDeclaration(operationContainer, extensionElement));
                }

                private Optional<Fires> getNotificationDeclaration(Type container, Type extensionElement) {
                    return ofNullable(container.getAnnotation(Fires.class).orElseGet(() -> extensionElement.getAnnotation(Fires.class).orElse(null)));
                }

                private void includeNotificationDeclarationIfNeeded(ExecutableComponentDeclaration declaration, Optional<Fires> emitsNotifications) {
                    emitsNotifications.ifPresent(emits -> {
                        Class<? extends NotificationActionProvider>[] providers = emits.value();
                        stream(providers).forEach(provider -> {
                            try {
                                NotificationActionProvider notificationActionProvider = provider.newInstance();
                                notificationActionProvider.getNotificationActions().stream().map(action -> validateEmits(actions, action)).forEach(action -> declaration.addNotificationModel(notificationModels.get(action)));
                            } catch (InstantiationException | IllegalAccessException e) {
                                throw new MuleRuntimeException(createStaticMessage("Could not create NotificationActionProvider of type " + provider.getName()), e);
                            }
                        });
                    });
                }

                private NotificationActionDefinition validateEmits(NotificationActionDefinition[] actions, NotificationActionDefinition action) {
                    Class<?> extensionAction = actions.getClass().getComponentType();
                    if (!action.getClass().equals(extensionAction) && !action.getClass().getSuperclass().equals(extensionAction)) {
                        throw new IllegalModelDefinitionException(format("Invalid EmitsNotification detected, the extension declared" + " firing notifications of %s type, but a notification of %s type has been detected", extensionAction, action.getClass()));
                    } else {
                        return action;
                    }
                }
            }.walk(declaration);
        });
    }
}
Also used : ImmutableNotificationModel(org.mule.runtime.extension.api.model.notification.ImmutableNotificationModel) MuleExtensionUtils.getExtensionsNamespace(org.mule.runtime.module.extension.internal.util.MuleExtensionUtils.getExtensionsNamespace) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) NotificationActionProvider(org.mule.runtime.extension.api.annotation.notification.NotificationActionProvider) ExtensionsTypeLoaderFactory(org.mule.runtime.extension.api.declaration.type.ExtensionsTypeLoaderFactory) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) OperationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration) NotificationActions(org.mule.runtime.extension.api.annotation.notification.NotificationActions) HashMap(java.util.HashMap) NotificationActionDefinition(org.mule.runtime.extension.api.notification.NotificationActionDefinition) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) SourceModel(org.mule.runtime.api.meta.model.source.SourceModel) WithOperationsDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) Map(java.util.Map) NotificationModel(org.mule.runtime.api.meta.model.notification.NotificationModel) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) DeclarationEnricher(org.mule.runtime.extension.api.loader.DeclarationEnricher) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) DeclarationEnricherPhase(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) ExtensionLoadingContext(org.mule.runtime.extension.api.loader.ExtensionLoadingContext) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) String.format(java.lang.String.format) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) Fires(org.mule.runtime.extension.api.annotation.notification.Fires) Optional(java.util.Optional) ExecutableComponentDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExecutableComponentDeclaration) Arrays.stream(java.util.Arrays.stream) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) INITIALIZE(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase.INITIALIZE) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) NotificationActionProvider(org.mule.runtime.extension.api.annotation.notification.NotificationActionProvider) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) HashMap(java.util.HashMap) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) ImmutableNotificationModel(org.mule.runtime.extension.api.model.notification.ImmutableNotificationModel) NotificationModel(org.mule.runtime.api.meta.model.notification.NotificationModel) OperationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration) NotificationActions(org.mule.runtime.extension.api.annotation.notification.NotificationActions) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) ClassTypeLoader(org.mule.metadata.api.ClassTypeLoader) Fires(org.mule.runtime.extension.api.annotation.notification.Fires) ExecutableComponentDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExecutableComponentDeclaration) ImmutableNotificationModel(org.mule.runtime.extension.api.model.notification.ImmutableNotificationModel) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration) Optional(java.util.Optional) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) NotificationActionDefinition(org.mule.runtime.extension.api.notification.NotificationActionDefinition) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) WithOperationsDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker)

Example 2 with WithOperationsDeclaration

use of org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration in project mule by mulesoft.

the class JavaDeclarationDelegateTestCase method assertTestModuleOperations.

private void assertTestModuleOperations(ExtensionDeclaration extensionDeclaration) throws Exception {
    assertThat(extensionDeclaration.getOperations(), hasSize(48));
    WithOperationsDeclaration withOperationsDeclaration = extensionDeclaration.getConfigurations().get(0);
    assertThat(withOperationsDeclaration.getOperations().size(), is(15));
    assertOperation(withOperationsDeclaration, SAY_MY_NAME_OPERATION, "");
    assertOperation(withOperationsDeclaration, NAME_AS_STREAM, "");
    assertOperation(withOperationsDeclaration, GET_ENEMY_OPERATION, "");
    assertOperation(withOperationsDeclaration, GET_ALL_ENEMIES_OPERATION, "");
    assertOperation(extensionDeclaration, KILL_OPERATION, "");
    assertOperation(extensionDeclaration, KILL_CUSTOM_OPERATION, "");
    assertOperation(extensionDeclaration, KILL_WITH_WEAPON, "");
    assertOperation(extensionDeclaration, KILL_WITH_RICINS, "");
    assertOperation(extensionDeclaration, KILL_WITH_MULTIPLES_WEAPONS, "");
    assertOperation(extensionDeclaration, KILL_WITH_MULTIPLE_WILDCARD_WEAPONS, "");
    assertOperation(withOperationsDeclaration, DIE, "");
    assertOperation(withOperationsDeclaration, LAUNDER_MONEY, "");
    assertOperation(extensionDeclaration, INJECTED_EXTENSION_MANAGER, "");
    assertOperation(extensionDeclaration, ALIAS, "");
    assertOperation(withOperationsDeclaration, CALL_SAUL, "");
    assertOperation(extensionDeclaration, CALL_GUS_FRING, "");
    assertOperation(withOperationsDeclaration, GET_SAUL_PHONE, "");
    assertOperation(extensionDeclaration, GET_MEDICAL_HISTORY, "");
    assertOperation(extensionDeclaration, GET_GRAMS_IN_STORAGE, "");
    assertOperation(extensionDeclaration, APPROVE_INVESTMENT, "");
    assertOperation(withOperationsDeclaration, GET_PAGED_PERSONAL_INFO_OPERATION, "");
    assertOperation(withOperationsDeclaration, EMPTY_PAGED_OPERATION, "");
    assertOperation(withOperationsDeclaration, FAILING_PAGED_OPERATION, "");
    assertOperation(withOperationsDeclaration, CONNECTION_PAGED_OPERATION, "");
    assertOperation(extensionDeclaration, PROCESS_INFO, "");
    assertOperation(extensionDeclaration, PROCESS_WEAPON, "");
    assertOperation(extensionDeclaration, PROCESS_WEAPON_LIST, "");
    assertOperation(extensionDeclaration, PROCESS_WEAPON_WITH_DEFAULT_VALUE, "");
    assertOperation(extensionDeclaration, FAIL_TO_EXECUTE, "");
    assertOperation(extensionDeclaration, THROW_ERROR, "");
    assertOperation(extensionDeclaration, BY_PASS_WEAPON, "");
    assertOperation(extensionDeclaration, ECHO_AN_OPERATION_WITH_ALIAS, "");
    assertOperation(extensionDeclaration, "executeRemoteKill", "");
    OperationDeclaration operation = getOperation(withOperationsDeclaration, SAY_MY_NAME_OPERATION);
    assertThat(operation, is(notNullValue()));
    assertThat(operation.getAllParameters().isEmpty(), is(true));
    operation = getOperation(withOperationsDeclaration, GET_ENEMY_OPERATION);
    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);
    assertTransactional(operation, false);
    operation = getOperation(withOperationsDeclaration, GET_ALL_ENEMIES_OPERATION);
    assertThat(operation, is(notNullValue()));
    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);
    operation = getOperation(extensionDeclaration, KILL_OPERATION);
    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, true, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, KILL_WITH_WEAPON);
    assertThat(operation, is(notNullValue()));
    assertThat(operation.getAllParameters(), hasSize(3));
    assertParameter(operation.getAllParameters(), "weapon", "", WEAPON_TYPE, true, SUPPORTED, null);
    assertParameter(operation.getAllParameters(), "type", "", toMetadataType(WeaponType.class), true, SUPPORTED, null);
    assertParameter(operation.getAllParameters(), "attributesOfWeapon", "", toMetadataType(Weapon.WeaponAttributes.class), true, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, KILL_WITH_RICINS);
    assertThat(operation, is(notNullValue()));
    assertThat(operation.getAllParameters(), hasSize(1));
    assertParameter(operation.getAllParameters(), "ricins", "", arrayOf(List.class, objectTypeBuilder(Ricin.class)), false, SUPPORTED, "#[payload]");
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, KILL_WITH_MULTIPLES_WEAPONS);
    assertThat(operation, is(notNullValue()));
    assertThat(operation.getAllParameters(), hasSize(1));
    assertParameter(operation.getAllParameters(), "weapons", "", arrayOf(List.class, objectTypeBuilder(Weapon.class)), false, SUPPORTED, "#[payload]");
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, KILL_WITH_MULTIPLE_WILDCARD_WEAPONS);
    assertThat(operation, is(notNullValue()));
    assertThat(operation.getAllParameters(), hasSize(1));
    assertParameter(operation.getAllParameters(), "wildCardWeapons", "", arrayOf(List.class, objectTypeBuilder(Weapon.class)), true, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, KILL_CUSTOM_OPERATION);
    assertThat(operation, is(notNullValue()));
    assertThat(operation.getAllParameters(), hasSize(2));
    assertParameter(operation.getAllParameters(), "victim", "", STRING_TYPE, false, SUPPORTED, "#[payload]");
    assertParameter(operation.getAllParameters(), "goodbyeMessage", "", STRING_TYPE, true, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(withOperationsDeclaration, LAUNDER_MONEY);
    assertParameter(operation.getAllParameters(), "amount", "", toMetadataType(long.class), true, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, INJECTED_EXTENSION_MANAGER);
    assertThat(operation, is(notNullValue()));
    assertThat(operation.getAllParameters().isEmpty(), is(true));
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, ALIAS);
    assertParameter(operation.getAllParameters(), "greeting", "", STRING_TYPE, true, SUPPORTED, null);
    assertParameter(operation.getAllParameters(), "myName", "", STRING_TYPE, false, SUPPORTED, HEISENBERG);
    assertParameter(operation.getAllParameters(), "age", "", toMetadataType(Integer.class), false, SUPPORTED, AGE);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, KNOCK);
    assertParameter(operation.getAllParameters(), "knockedDoor", "", toMetadataType(KnockeableDoor.class), true, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, KNOCK_MANY);
    assertParameter(operation.getAllParameters(), "doors", "", arrayOf(List.class, objectTypeBuilder(KnockeableDoor.class)), true, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(withOperationsDeclaration, CALL_SAUL);
    assertThat(operation.getAllParameters(), is(empty()));
    assertConnected(operation, true);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, CURE_CANCER);
    assertThat(operation.getAllParameters(), is(empty()));
    assertConnected(operation, false);
    assertTransactional(operation, false);
    java.util.Optional<ExceptionHandlerFactory> exceptionEnricherFactory = operation.getModelProperty(ExceptionHandlerModelProperty.class).map(ExceptionHandlerModelProperty::getExceptionHandlerFactory);
    assertThat(exceptionEnricherFactory.isPresent(), is(true));
    assertThat(exceptionEnricherFactory.get().createHandler(), instanceOf(CureCancerExceptionEnricher.class));
    operation = getOperation(extensionDeclaration, GET_MEDICAL_HISTORY);
    assertParameter(operation.getAllParameters(), "healthByYear", "", TYPE_BUILDER.objectType().with(new ClassInformationAnnotation(Map.class, asList(String.class, HealthStatus.class))).openWith(TYPE_LOADER.load(HealthStatus.class)).build(), true, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, GET_GRAMS_IN_STORAGE);
    assertThat(operation, is(notNullValue()));
    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);
    operation = getOperation(extensionDeclaration, APPROVE_INVESTMENT);
    assertThat(operation, is(notNullValue()));
    assertParameter(operation.getAllParameters(), "investment", "", TYPE_LOADER.load(Investment.class), true, SUPPORTED, null);
    assertThat(getType(operation.getOutput().getType()), equalTo(getType(TYPE_LOADER.load(Investment.class))));
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, IGNORED_OPERATION);
    assertThat(operation, is(nullValue()));
    operation = getOperation(withOperationsDeclaration, GET_PAGED_PERSONAL_INFO_OPERATION);
    assertThat(operation.getModelProperty(PagedOperationModelProperty.class).isPresent(), is(true));
    assertThat(operation.getOutput().getType(), is(instanceOf(ArrayType.class)));
    ArrayType outputType = (ArrayType) operation.getOutput().getType();
    assertThat(outputType.getType(), is(TYPE_LOADER.load(PersonalInfo.class)));
    assertConnected(operation, true);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, PROCESS_INFO);
    assertThat(operation, is(notNullValue()));
    assertConnected(operation, false);
    assertTransactional(operation, false);
    assertParameter(operation.getAllParameters(), "sales", "", TYPE_LOADER.load(new TypeToken<Map<String, SaleInfo>>() {
    }.getType()), true, SUPPORTED, null);
    operation = getOperation(extensionDeclaration, PROCESS_WEAPON);
    assertThat(operation, is(notNullValue()));
    assertParameter(operation.getAllParameters(), "weapon", "", WEAPON_TYPE, false, SUPPORTED, null);
    assertConnected(operation, false);
    operation = getOperation(extensionDeclaration, PROCESS_WEAPON_LIST);
    assertThat(operation, is(notNullValue()));
    assertParameter(operation.getAllParameters(), "weapons", "", TYPE_LOADER.load(new TypeToken<List<Weapon>>() {
    }.getType()), false, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, PROCESS_WEAPON_WITH_DEFAULT_VALUE);
    assertThat(operation, is(notNullValue()));
    assertParameter(operation.getAllParameters(), "weapon", "", WEAPON_TYPE, false, SUPPORTED, PAYLOAD);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, "processWeaponListWithDefaultValue");
    assertThat(operation, is(notNullValue()));
    assertParameter(operation.getAllParameters(), "weapons", "", TYPE_LOADER.load(new TypeToken<List<Weapon>>() {
    }.getType()), false, SUPPORTED, PAYLOAD);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, "processAddressBook");
    assertThat(operation, is(notNullValue()));
    assertParameter(operation.getAllParameters(), "phoneNumbers", "", TYPE_LOADER.load(new TypeToken<List<String>>() {
    }.getType()), true, SUPPORTED, null);
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, FAIL_TO_EXECUTE);
    assertThat(operation, is(notNullValue()));
    assertThat(operation.getAllParameters(), is(empty()));
    assertThat(operation.getOutput().getType(), is(instanceOf(VoidType.class)));
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, THROW_ERROR);
    assertThat(operation, is(notNullValue()));
    assertThat(operation.getAllParameters(), is(empty()));
    assertThat(operation.getOutput().getType(), is(instanceOf(VoidType.class)));
    assertConnected(operation, false);
    assertTransactional(operation, false);
    operation = getOperation(extensionDeclaration, BY_PASS_WEAPON);
    assertThat(operation, is(notNullValue()));
    assertParameter(operation.getAllParameters(), "awesomeWeapon", "", WEAPON_TYPE, true, SUPPORTED, null);
    assertParameter(operation.getAllParameters(), "awesomeName", "", STRING_TYPE, true, SUPPORTED, null);
    assertThat(operation.getOutput().getType(), is(instanceOf(ObjectType.class)));
    assertConnected(operation, false);
    assertTransactional(operation, false);
}
Also used : ExceptionHandlerModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.ExceptionHandlerModelProperty) HealthStatus(org.mule.test.heisenberg.extension.model.HealthStatus) WithOperationsDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration) OperationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration) ClassInformationAnnotation(org.mule.metadata.java.api.annotation.ClassInformationAnnotation) ArrayType(org.mule.metadata.api.model.ArrayType) ExceptionHandlerFactory(org.mule.runtime.extension.api.runtime.exception.ExceptionHandlerFactory) CureCancerExceptionEnricher(org.mule.test.heisenberg.extension.exception.CureCancerExceptionEnricher) WeaponType(org.mule.test.heisenberg.extension.model.types.WeaponType) KnockeableDoor(org.mule.test.heisenberg.extension.model.KnockeableDoor) IntegerAttributes(org.mule.test.heisenberg.extension.model.types.IntegerAttributes) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Map(java.util.Map) Investment(org.mule.test.heisenberg.extension.model.Investment)

Example 3 with WithOperationsDeclaration

use of org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration in project mule by mulesoft.

the class ErrorsDeclarationEnricher method collectErrorOperations.

private List<Pair<ComponentDeclaration, MethodElement>> collectErrorOperations(ExtensionDeclaration declaration) {
    List<Pair<ComponentDeclaration, MethodElement>> operations = new LinkedList<>();
    new IdempotentDeclarationWalker() {

        @Override
        public void onOperation(WithOperationsDeclaration owner, OperationDeclaration declaration) {
            addComponent(declaration);
        }

        @Override
        protected void onConstruct(ConstructDeclaration declaration) {
            addComponent(declaration);
        }

        private void addComponent(ComponentDeclaration<?> declaration) {
            declaration.getModelProperty(ExtensionOperationDescriptorModelProperty.class).ifPresent(implementingMethodModelProperty -> operations.add(new Pair<>(declaration, implementingMethodModelProperty.getOperationMethod())));
        }
    }.walk(declaration);
    return operations;
}
Also used : MuleExtensionUtils.getExtensionsNamespace(org.mule.runtime.module.extension.internal.util.MuleExtensionUtils.getExtensionsNamespace) OperationModel(org.mule.runtime.api.meta.model.operation.OperationModel) ExtensionTypeDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionTypeDescriptorModelProperty) OperationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration) MuleErrors(org.mule.runtime.extension.api.error.MuleErrors) ConstructDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ConstructDeclaration) Type(org.mule.runtime.module.extension.api.loader.java.type.Type) WithOperationsDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration) MethodElement(org.mule.runtime.module.extension.api.loader.java.type.MethodElement) Pair(org.mule.runtime.api.util.Pair) DeclarationEnricher(org.mule.runtime.extension.api.loader.DeclarationEnricher) LinkedList(java.util.LinkedList) ErrorTypes(org.mule.runtime.extension.api.annotation.error.ErrorTypes) ComponentDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ComponentDeclaration) Optional.ofNullable(java.util.Optional.ofNullable) I18nMessageFactory.createStaticMessage(org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage) DeclarationEnricherPhase(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) ExtensionLoadingContext(org.mule.runtime.extension.api.loader.ExtensionLoadingContext) MuleRuntimeException(org.mule.runtime.api.exception.MuleRuntimeException) Throws(org.mule.runtime.extension.api.annotation.error.Throws) String.format(java.lang.String.format) ExtensionModel(org.mule.runtime.api.meta.model.ExtensionModel) ErrorTypeDefinition(org.mule.runtime.extension.api.error.ErrorTypeDefinition) List(java.util.List) Stream(java.util.stream.Stream) ErrorTypeProvider(org.mule.runtime.extension.api.annotation.error.ErrorTypeProvider) Optional(java.util.Optional) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) ExtensionOperationDescriptorModelProperty(org.mule.runtime.module.extension.internal.loader.java.type.property.ExtensionOperationDescriptorModelProperty) INITIALIZE(org.mule.runtime.extension.api.loader.DeclarationEnricherPhase.INITIALIZE) IllegalModelDefinitionException(org.mule.runtime.extension.api.exception.IllegalModelDefinitionException) ConstructDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ConstructDeclaration) IdempotentDeclarationWalker(org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker) WithOperationsDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration) LinkedList(java.util.LinkedList) OperationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration) Pair(org.mule.runtime.api.util.Pair)

Example 4 with WithOperationsDeclaration

use of org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration in project mule by mulesoft.

the class ExtensionDescriptionsEnricher method document.

/**
 * Fills all the descriptions in the provided {@link ExtensionDeclaration} based on the
 * <strong>extensions-descriptions.xml</strong> file.
 *
 * @param declaration   the declaration to describe.
 * @param documentation the extension documentation with its corresponding description.
 */
private void document(ExtensionDeclaration declaration, XmlExtensionDocumentation documentation) {
    declaration.setDescription(documentation.getExtension().getDescription());
    new DeclarationWalker() {

        @Override
        protected void onConfiguration(ConfigurationDeclaration declaration) {
            document(declaration, documentation.getConfigs());
        }

        @Override
        protected void onOperation(WithOperationsDeclaration owner, OperationDeclaration declaration) {
            document(declaration, documentation.getOperations());
        }

        @Override
        protected void onConnectionProvider(ConnectedDeclaration owner, ConnectionProviderDeclaration declaration) {
            document(declaration, documentation.getConnections());
        }

        @Override
        protected void onSource(WithSourcesDeclaration owner, SourceDeclaration declaration) {
            document(declaration, documentation.getSources());
        }

        private void document(ParameterizedDeclaration<?> declaration, List<XmlExtensionElementDocumentation> elements) {
            elements.stream().filter(e -> e.getName().equals(declaration.getName())).findAny().ifPresent(e -> {
                declaration.setDescription(e.getDescription());
                declaration.getAllParameters().forEach(param -> e.getParameters().stream().filter(p -> p.getName().equals(param.getName())).findAny().ifPresent(p -> param.setDescription(p.getDescription())));
            });
        }
    }.walk(declaration);
}
Also used : XmlExtensionDocumentation(org.mule.runtime.module.extension.internal.resources.documentation.XmlExtensionDocumentation) WithSourcesDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithSourcesDeclaration) URL(java.net.URL) OperationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration) ParameterizedDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ParameterizedDeclaration) ExtensionDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration) ExtensionLoadingContext(org.mule.runtime.extension.api.loader.ExtensionLoadingContext) IOException(java.io.IOException) XmlExtensionElementDocumentation(org.mule.runtime.module.extension.internal.resources.documentation.XmlExtensionElementDocumentation) ConfigurationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ConfigurationDeclaration) ConnectedDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ConnectedDeclaration) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) WithOperationsDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration) DeclarationWalker(org.mule.runtime.api.meta.model.declaration.fluent.util.DeclarationWalker) DeclarationEnricher(org.mule.runtime.extension.api.loader.DeclarationEnricher) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration) ExtensionDescriptionsSerializer(org.mule.runtime.module.extension.internal.resources.documentation.ExtensionDescriptionsSerializer) ConnectionProviderDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ConnectionProviderDeclaration) ConfigurationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ConfigurationDeclaration) ConnectionProviderDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ConnectionProviderDeclaration) WithOperationsDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration) OperationDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration) ConnectedDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.ConnectedDeclaration) DeclarationWalker(org.mule.runtime.api.meta.model.declaration.fluent.util.DeclarationWalker) XmlExtensionElementDocumentation(org.mule.runtime.module.extension.internal.resources.documentation.XmlExtensionElementDocumentation) WithSourcesDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.WithSourcesDeclaration) SourceDeclaration(org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration)

Aggregations

OperationDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.OperationDeclaration)4 WithOperationsDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.WithOperationsDeclaration)4 List (java.util.List)3 ExtensionDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.ExtensionDeclaration)3 DeclarationEnricher (org.mule.runtime.extension.api.loader.DeclarationEnricher)3 ExtensionLoadingContext (org.mule.runtime.extension.api.loader.ExtensionLoadingContext)3 String.format (java.lang.String.format)2 Map (java.util.Map)2 Optional (java.util.Optional)2 Optional.ofNullable (java.util.Optional.ofNullable)2 MuleRuntimeException (org.mule.runtime.api.exception.MuleRuntimeException)2 I18nMessageFactory.createStaticMessage (org.mule.runtime.api.i18n.I18nMessageFactory.createStaticMessage)2 ExtensionModel (org.mule.runtime.api.meta.model.ExtensionModel)2 SourceDeclaration (org.mule.runtime.api.meta.model.declaration.fluent.SourceDeclaration)2 OperationModel (org.mule.runtime.api.meta.model.operation.OperationModel)2 IdempotentDeclarationWalker (org.mule.runtime.extension.api.declaration.fluent.util.IdempotentDeclarationWalker)2 IllegalModelDefinitionException (org.mule.runtime.extension.api.exception.IllegalModelDefinitionException)2 DeclarationEnricherPhase (org.mule.runtime.extension.api.loader.DeclarationEnricherPhase)2 IOException (java.io.IOException)1 URL (java.net.URL)1