Search in sources :

Example 6 with MetadataKeyIdModelProperty

use of org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty in project mule by mulesoft.

the class AbstractExtensionMessageSourceTestCase method before.

@Before
public void before() throws Exception {
    initMocks(this);
    spyInjector(muleContext);
    reset(muleContext.getSchedulerService());
    when(result.getMediaType()).thenReturn(of(ANY));
    when(result.getAttributes()).thenReturn(empty());
    ((MuleContextWithRegistries) muleContext).getRegistry().registerObject(OBJECT_STREAMING_MANAGER, streamingManager);
    when(extensionModel.getXmlDslModel()).thenReturn(XmlDslModel.builder().setPrefix("test-extension").build());
    cursorStreamProviderFactory = getDefaultCursorStreamProviderFactory(streamingManager);
    sourceAdapter = createSourceAdapter();
    when(sourceAdapterFactory.createAdapter(any(), any(), any(), any(), any())).thenReturn(sourceAdapter);
    mockExceptionEnricher(sourceModel, null);
    when(sourceModel.requiresConnection()).thenReturn(true);
    when(sourceModel.getName()).thenReturn(SOURCE_NAME);
    when(sourceModel.getModelProperty(MetadataResolverFactoryModelProperty.class)).thenReturn(empty());
    when(sourceModel.getModelProperty(SourceCallbackModelProperty.class)).thenReturn(empty());
    when(sourceModel.getModelProperty(MediaTypeModelProperty.class)).thenReturn(empty());
    setRequires(sourceModel, true, true);
    when(sourceModel.getOutput().getType()).thenReturn(TYPE_LOADER.load(String.class));
    when(sourceModel.getNotificationModels()).thenReturn(emptySet());
    mockExceptionEnricher(extensionModel, null);
    mockClassLoaderModelProperty(extensionModel, getClass().getClassLoader());
    retryPolicyTemplate.setNotificationFirer(((MuleContextWithRegistries) muleContext).getRegistry().lookupObject(NotificationDispatcher.class));
    initialiseIfNeeded(retryPolicyTemplate, muleContext);
    ((MuleContextWithRegistries) muleContext).getRegistry().registerObject(OBJECT_EXTENSION_MANAGER, extensionManager);
    when(flowConstruct.getMuleContext()).thenReturn(muleContext);
    mockSubTypes(extensionModel);
    when(configurationModel.getSourceModel(SOURCE_NAME)).thenReturn(of(sourceModel));
    when(extensionManager.getConfigurationProvider(CONFIG_NAME)).thenReturn(of(configurationProvider));
    when(configurationProvider.get(any())).thenReturn(configurationInstance);
    when(configurationProvider.getConfigurationModel()).thenReturn(configurationModel);
    when(configurationProvider.getName()).thenReturn(CONFIG_NAME);
    mockMetadataResolverFactory(sourceModel, metadataResolverFactory);
    when(metadataResolverFactory.getKeyResolver()).thenReturn(new TestNoConfigMetadataResolver());
    when(metadataResolverFactory.getInputResolver("content")).thenReturn(new TestNoConfigMetadataResolver());
    when(metadataResolverFactory.getInputResolver("type")).thenReturn(new NullMetadataResolver());
    when(metadataResolverFactory.getOutputResolver()).thenReturn(new TestNoConfigMetadataResolver());
    when(metadataResolverFactory.getOutputAttributesResolver()).thenReturn(new TestNoConfigMetadataResolver());
    when(sourceModel.getOutput()).thenReturn(new ImmutableOutputModel("Output", BaseTypeBuilder.create(JAVA).stringType().build(), true, emptySet()));
    when(sourceModel.getOutputAttributes()).thenReturn(new ImmutableOutputModel("Output", BaseTypeBuilder.create(JAVA).stringType().build(), false, emptySet()));
    when(sourceModel.getModelProperty(MetadataKeyIdModelProperty.class)).thenReturn(of(new MetadataKeyIdModelProperty(typeLoader.load(String.class), METADATA_KEY)));
    when(sourceModel.getAllParameterModels()).thenReturn(emptyList());
    when(messageProcessContext.getTransactionConfig()).thenReturn(empty());
    messageSource = getNewExtensionMessageSourceInstance();
    sourceCallback = spy(DefaultSourceCallback.builder().setSourceModel(sourceModel).setProcessingManager(messageProcessingManager).setListener(messageProcessor).setSource(messageSource).setMuleContext(muleContext).setProcessContextSupplier(() -> messageProcessContext).setCompletionHandlerFactory(completionHandlerFactory).setExceptionCallback(exceptionCallback).setCursorStreamProviderFactory(cursorStreamProviderFactory).build());
    when(sourceCallbackFactory.createSourceCallback(any())).thenReturn(sourceCallback);
}
Also used : NullMetadataResolver(org.mule.runtime.extension.api.metadata.NullMetadataResolver) ImmutableOutputModel(org.mule.runtime.extension.api.model.ImmutableOutputModel) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) MuleContextWithRegistries(org.mule.runtime.core.internal.context.MuleContextWithRegistries) NotificationDispatcher(org.mule.runtime.api.notification.NotificationDispatcher) TestNoConfigMetadataResolver(org.mule.test.metadata.extension.resolver.TestNoConfigMetadataResolver) Before(org.junit.Before)

Example 7 with MetadataKeyIdModelProperty

use of org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty in project mule by mulesoft.

the class MetadataKeyIdObjectResolver method resolve.

/**
 * Returns the populated key in the Type that the component parameter requires by looking for default values, if no
 * {@link MetadataKeyId} is present an empty value is returned since is a key less component.
 * <p>
 * If a key should be built and there is at least one default value missing an {@link IllegalArgumentException} is thrown.
 *
 * @return a new instance of the {@link MetadataKeyId} parameter {@code type}.
 * @throws MetadataResolvingException if the Parameter type is not instantiable.
 * @throws IllegalArgumentException if cannot found the required default values for an specified key.
 */
public Object resolve() throws MetadataResolvingException {
    if (isKeyLess()) {
        return NullMetadataKey.ID;
    }
    if (!keyParts.stream().allMatch(p -> p.getDefaultValue() != null)) {
        throw new IllegalArgumentException("Could not build metadata key from an object that does" + " not have a default value for all it's components.");
    }
    String id = keyParts.get(0).getDefaultValue().toString();
    final MetadataKeyIdModelProperty keyIdModelProperty = findMetadataKeyIdModelProperty(component);
    MetadataType type = keyIdModelProperty.getType();
    KeyMetadataTypeVisitor visitor = new KeyMetadataTypeVisitor(id, getType(type)) {

        @Override
        protected Map<Field, String> getFieldValuesMap() {
            return keyParts.stream().filter(p -> p.getModelProperty(DeclaringMemberModelProperty.class).isPresent()).collect(toMap(p -> p.getModelProperty(DeclaringMemberModelProperty.class).get().getDeclaringField(), p -> p.getDefaultValue().toString()));
        }
    };
    type.accept(visitor);
    return visitor.getResultId();
}
Also used : StaticValueResolver(org.mule.runtime.module.extension.internal.runtime.resolver.StaticValueResolver) ParameterModel(org.mule.runtime.api.meta.model.parameter.ParameterModel) ComponentModel(org.mule.runtime.api.meta.model.ComponentModel) IntrospectionUtils.getFieldValue(org.mule.runtime.module.extension.internal.util.IntrospectionUtils.getFieldValue) Preconditions.checkArgument(org.mule.runtime.api.util.Preconditions.checkArgument) MetadataKeyId(org.mule.runtime.extension.api.annotation.metadata.MetadataKeyId) HashMap(java.util.HashMap) DsqlParser.isDsqlQuery(org.mule.runtime.extension.api.dsql.DsqlParser.isDsqlQuery) MetadataKeyBuilder(org.mule.runtime.api.metadata.MetadataKeyBuilder) QueryParameterModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.QueryParameterModelProperty) MetadataKey(org.mule.runtime.api.metadata.MetadataKey) DsqlParser(org.mule.runtime.extension.api.dsql.DsqlParser) DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) Collectors.toMap(java.util.stream.Collectors.toMap) MetadataKeyPartModelProperty(org.mule.runtime.extension.api.property.MetadataKeyPartModelProperty) DefaultObjectBuilder(org.mule.runtime.module.extension.internal.runtime.objectbuilder.DefaultObjectBuilder) Map(java.util.Map) MetadataKeyBuilder.newKey(org.mule.runtime.api.metadata.MetadataKeyBuilder.newKey) INVALID_METADATA_KEY(org.mule.runtime.api.metadata.resolving.FailureCode.INVALID_METADATA_KEY) DsqlQuery(org.mule.runtime.extension.api.dsql.DsqlQuery) NullMetadataKey(org.mule.runtime.extension.api.metadata.NullMetadataKey) FailureCode(org.mule.runtime.api.metadata.resolving.FailureCode) ObjectType(org.mule.metadata.api.model.ObjectType) ReflectionCache(org.mule.runtime.module.extension.internal.util.ReflectionCache) Field(java.lang.reflect.Field) String.format(java.lang.String.format) MetadataTypeVisitor(org.mule.metadata.api.visitor.MetadataTypeVisitor) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) String.valueOf(java.lang.String.valueOf) StringType(org.mule.metadata.api.model.StringType) BooleanType(org.mule.metadata.api.model.BooleanType) Reference(org.mule.runtime.api.util.Reference) Function.identity(java.util.function.Function.identity) MetadataType(org.mule.metadata.api.model.MetadataType) Optional(java.util.Optional) MetadataResolvingException(org.mule.runtime.api.metadata.MetadataResolvingException) JavaTypeUtils.getType(org.mule.metadata.java.api.utils.JavaTypeUtils.getType) DeclaringMemberModelProperty(org.mule.runtime.module.extension.internal.loader.java.property.DeclaringMemberModelProperty) Field(java.lang.reflect.Field) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) MetadataType(org.mule.metadata.api.model.MetadataType)

Example 8 with MetadataKeyIdModelProperty

use of org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty in project mule by mulesoft.

the class MetadataKeyIdObjectResolver method resolve.

/**
 * Given {@link MetadataKey}, return the populated key in the Type that the component parameter requires.
 *
 * @param key the {@link MetadataKey} associated to the {@link MetadataKeyId}
 * @return a new instance of the {@link MetadataKeyId} parameter {@code type} with the values of the passed {@link MetadataKey}
 * @throws MetadataResolvingException if:
 *         <ul>
 *         <li>Parameter types is not instantiable</li>
 *         <li>{@param key} does not provide the required levels</li>
 *         </ul>
 */
public Object resolve(MetadataKey key) throws MetadataResolvingException {
    if (isKeyLess()) {
        return NullMetadataKey.ID;
    }
    final MetadataKeyIdModelProperty keyIdModelProperty = findMetadataKeyIdModelProperty(component);
    MetadataType type = keyIdModelProperty.getType();
    KeyMetadataTypeVisitor visitor = new KeyMetadataTypeVisitor(key.getId(), getType(type)) {

        @Override
        protected Map<Field, String> getFieldValuesMap() throws MetadataResolvingException {
            return keyToFieldValueMap(key);
        }
    };
    type.accept(visitor);
    return visitor.getResultId();
}
Also used : Field(java.lang.reflect.Field) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) MetadataType(org.mule.metadata.api.model.MetadataType)

Example 9 with MetadataKeyIdModelProperty

use of org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty in project mule by mulesoft.

the class AbstractOperationMessageProcessorTestCase method before.

@Before
public void before() throws Exception {
    ((MuleContextWithRegistries) muleContext).getRegistry().registerObject(OBJECT_STREAMING_MANAGER, streamingManager);
    cursorStreamProviderFactory = spy(getDefaultCursorStreamProviderFactory(streamingManager));
    event = configureEvent();
    when(context.getInjector().inject(any())).thenAnswer(invocationOnMock -> {
        final Object subject = invocationOnMock.getArguments()[0];
        muleContext.getInjector().inject(subject);
        return subject;
    });
    when(extensionModel.getName()).thenReturn(EXTENSION_NAMESPACE);
    when(extensionModel.getConfigurationModels()).thenReturn(asList(configurationModel));
    when(operationModel.getName()).thenReturn(getClass().getName());
    when(operationModel.isBlocking()).thenReturn(true);
    when(extensionModel.getXmlDslModel()).thenReturn(XmlDslModel.builder().setPrefix("test-extension").build());
    when(operationModel.getOutput()).thenReturn(new ImmutableOutputModel("Message.Payload", toMetadataType(String.class), false, emptySet()));
    mockExecutorFactory(operationModel, operationExecutorFactory);
    visitableMock(operationModel);
    when(operationModel.getModelProperty(MetadataKeyIdModelProperty.class)).thenReturn(of(new MetadataKeyIdModelProperty(ExtensionsTypeLoaderFactory.getDefault().createTypeLoader().load(String.class), "someParam")));
    setRequires(operationModel, true, true);
    when(operationExecutorFactory.createExecutor(same(operationModel), anyMap())).thenReturn(operationExecutor);
    when(operationModel.getName()).thenReturn(OPERATION_NAME);
    when(operationModel.getDisplayModel()).thenReturn(empty());
    when(operationModel.getModelProperty(MediaTypeModelProperty.class)).thenReturn(empty());
    mockExceptionEnricher(operationModel, exceptionHandlerFactory);
    when(exceptionHandlerFactory.createHandler()).thenReturn(new NullExceptionHandler());
    mockMetadataResolverFactory(operationModel, metadataResolverFactory);
    when(metadataResolverFactory.getKeyResolver()).thenReturn(new TestNoConfigMetadataResolver());
    when(metadataResolverFactory.getInputResolver("content")).thenReturn(new TestNoConfigMetadataResolver());
    when(metadataResolverFactory.getInputResolver("type")).thenReturn(new NullMetadataResolver());
    when(metadataResolverFactory.getOutputResolver()).thenReturn(new TestNoConfigMetadataResolver());
    when(metadataResolverFactory.getOutputAttributesResolver()).thenReturn(new TestNoConfigMetadataResolver());
    when(metadataResolverFactory.getQueryEntityResolver()).thenReturn(new TestNoConfigMetadataResolver());
    when(keyParamMock.getName()).thenReturn("type");
    when(keyParamMock.getType()).thenReturn(stringType);
    when(keyParamMock.getModelProperty(MetadataKeyPartModelProperty.class)).thenReturn(of(new MetadataKeyPartModelProperty(1)));
    when(keyParamMock.getRole()).thenReturn(BEHAVIOUR);
    when(keyParamMock.getDisplayModel()).thenReturn(empty());
    when(keyParamMock.getLayoutModel()).thenReturn(empty());
    when(keyParamMock.getModelProperty(QueryParameterModelProperty.class)).thenReturn(empty());
    when(keyParamMock.getModelProperty(FieldOperationParameterModelProperty.class)).thenReturn(empty());
    when(contentMock.getName()).thenReturn("content");
    when(contentMock.hasDynamicType()).thenReturn(true);
    when(contentMock.getType()).thenReturn(stringType);
    when(contentMock.getRole()).thenReturn(CONTENT);
    when(contentMock.getDisplayModel()).thenReturn(empty());
    when(contentMock.getLayoutModel()).thenReturn(empty());
    when(contentMock.getModelProperty(MetadataKeyPartModelProperty.class)).thenReturn(empty());
    when(contentMock.getModelProperty(QueryParameterModelProperty.class)).thenReturn(empty());
    when(contentMock.getModelProperty(FieldOperationParameterModelProperty.class)).thenReturn(empty());
    parameterGroupModel = mockParameters(operationModel, keyParamMock, contentMock);
    when(parameterGroupModel.getDisplayModel()).thenReturn(empty());
    when(parameterGroupModel.getLayoutModel()).thenReturn(empty());
    when(parameterGroupModel.getModelProperty(MetadataKeyIdModelProperty.class)).thenReturn(empty());
    when(outputMock.getType()).thenReturn(stringType);
    when(outputMock.hasDynamicType()).thenReturn(true);
    when(operationModel.getOutput()).thenReturn(outputMock);
    when(operationModel.getOutputAttributes()).thenReturn(outputMock);
    when(operationModel.getModelProperty(InterceptorsModelProperty.class)).thenReturn(empty());
    when(operationExecutorFactory.createExecutor(same(operationModel), anyMap())).thenReturn(operationExecutor);
    when(operationExecutor.execute(any())).thenReturn(just(""));
    when(resolverSet.resolve(from(event))).thenReturn(parameters);
    when(configurationInstance.getName()).thenReturn(CONFIG_NAME);
    when(configurationInstance.getModel()).thenReturn(configurationModel);
    when(configurationInstance.getValue()).thenReturn(configuration);
    when(configurationInstance.getConnectionProvider()).thenReturn(of(connectionProviderWrapper));
    when(configurationProvider.get(event)).thenReturn(configurationInstance);
    when(configurationProvider.getConfigurationModel()).thenReturn(configurationModel);
    when(configurationProvider.getName()).thenReturn(configurationName);
    when(configurationModel.getOperationModels()).thenReturn(asList(operationModel));
    when(configurationModel.getOperationModel(OPERATION_NAME)).thenReturn(of(operationModel));
    when(connectionProviderWrapper.getReconnectionConfig()).thenReturn(of(ReconnectionConfig.getDefault()));
    when(connectionProviderWrapper.getRetryPolicyTemplate()).thenReturn(new NoRetryPolicyTemplate());
    mockSubTypes(extensionModel);
    mockClassLoaderModelProperty(extensionModel, getClass().getClassLoader());
    when(extensionManager.getConfiguration(anyString(), anyObject())).thenReturn(configurationInstance);
    when(extensionManager.getConfiguration(extensionModel, operationModel, event)).thenReturn(of(configurationInstance));
    when(configurationProvider.get(anyObject())).thenReturn(configurationInstance);
    when(extensionManager.getConfigurationProvider(extensionModel, operationModel)).thenReturn(of(configurationProvider));
    when(extensionManager.getConfigurationProvider(CONFIG_NAME)).thenReturn(of(configurationProvider));
    when(stringType.getAnnotation(anyObject())).thenReturn(empty());
    when(mockPolicyManager.createOperationPolicy(any(), any(), any(), any())).thenAnswer(invocationOnMock -> {
        if (mockOperationPolicy == null) {
            mockOperationPolicy = mock(OperationPolicy.class);
            when(mockOperationPolicy.process(any())).thenAnswer(operationPolicyInvocationMock -> ((OperationExecutionFunction) invocationOnMock.getArguments()[3]).execute((Map<String, Object>) invocationOnMock.getArguments()[2], (CoreEvent) invocationOnMock.getArguments()[1]));
        }
        return mockOperationPolicy;
    });
    when(executionContext.getRetryPolicyTemplate()).thenReturn(empty());
    when(connectionManagerAdapter.getConnection(anyString())).thenReturn(null);
    messageProcessor = setUpOperationMessageProcessor();
}
Also used : NullMetadataResolver(org.mule.runtime.extension.api.metadata.NullMetadataResolver) ImmutableOutputModel(org.mule.runtime.extension.api.model.ImmutableOutputModel) MetadataKeyIdModelProperty(org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty) CoreEvent(org.mule.runtime.core.api.event.CoreEvent) OperationPolicy(org.mule.runtime.core.internal.policy.OperationPolicy) Matchers.anyObject(org.mockito.Matchers.anyObject) MetadataKeyPartModelProperty(org.mule.runtime.extension.api.property.MetadataKeyPartModelProperty) Matchers.anyString(org.mockito.Matchers.anyString) NullExceptionHandler(org.mule.runtime.module.extension.internal.runtime.exception.NullExceptionHandler) TestNoConfigMetadataResolver(org.mule.test.metadata.extension.resolver.TestNoConfigMetadataResolver) Map(java.util.Map) Matchers.anyMap(org.mockito.Matchers.anyMap) NoRetryPolicyTemplate(org.mule.runtime.core.api.retry.policy.NoRetryPolicyTemplate) Before(org.junit.Before)

Aggregations

MetadataKeyIdModelProperty (org.mule.runtime.extension.api.property.MetadataKeyIdModelProperty)9 ParameterModel (org.mule.runtime.api.meta.model.parameter.ParameterModel)4 Before (org.junit.Before)3 Test (org.junit.Test)3 MetadataType (org.mule.metadata.api.model.MetadataType)3 SmallTest (org.mule.tck.size.SmallTest)3 Field (java.lang.reflect.Field)2 Map (java.util.Map)2 ObjectType (org.mule.metadata.api.model.ObjectType)2 NullMetadataResolver (org.mule.runtime.extension.api.metadata.NullMetadataResolver)2 ImmutableOutputModel (org.mule.runtime.extension.api.model.ImmutableOutputModel)2 MetadataKeyPartModelProperty (org.mule.runtime.extension.api.property.MetadataKeyPartModelProperty)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 String.format (java.lang.String.format)1 String.valueOf (java.lang.String.valueOf)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 Function.identity (java.util.function.Function.identity)1 Supplier (java.util.function.Supplier)1