public void RenderTemplateAsyncWithResourceTemplateWithSuccess(ITemplateRenderer renderer, AzureIntegrationServicesModel model, MessagingObject messagingObject, TargetResourceTemplate resourceTemplate, string templateContent, string renderedContent, Exception e)
        {
            "Given a template renderer"
            .x(() => renderer = new LiquidTemplateRenderer(_mockLogger.Object));

            "And a model"
            .x(() => model = _model);

            "And a messaging object"
            .x(() => messagingObject = model.FindMessagingObject("ContosoMessageBus:System:FtpReceive").messagingObject);

            "And a resource template object"
            .x(() => resourceTemplate = new TargetResourceTemplate()
            {
                ResourceName = "endpointFtpReceiveLogicAppConsumption"
            });

            "And a template content"
            .x(() => templateContent = _templateResourceTemplateContent);

            "When rendering the template"
            .x(async() => e = await Record.ExceptionAsync(async() => renderedContent = await renderer.RenderTemplateAsync(templateContent, model, null, resourceTemplate)));

            "Then the render should succeed"
            .x(() => e.Should().BeNull());

            "And the rendered content should have expected values from the model"
            .x(() =>
            {
                renderedContent.Should().NotBeNull().And.ContainAny("endpointFtpReceiveLogicAppConsumption").And.NotContainAny("{{").And.NotContainAny("}}");
            });
        }
        public void RenderTemplateAsyncWithIntermediaryWithSuccess(ITemplateRenderer renderer, AzureIntegrationServicesModel model, MessagingObject messagingObject, string templateContent, string renderedContent, Exception e)
        {
            "Given a template renderer"
            .x(() => renderer = new LiquidTemplateRenderer(_mockLogger.Object));

            "And a model"
            .x(() => model = _model);

            "And a messaging object"
            .x(() => messagingObject = model.FindMessagingObject("ContosoMessageBus:System:MessageAgent").messagingObject);

            "And a template content"
            .x(() => templateContent = _templateIntermediaryContent);

            "When rendering the template"
            .x(async() => e = await Record.ExceptionAsync(async() => renderedContent = await renderer.RenderTemplateAsync(templateContent, model, messagingObject)));

            "Then the render should succeed"
            .x(() => e.Should().BeNull());

            "And the rendered content should have expected values from the model"
            .x(() =>
            {
                renderedContent.Should().NotBeNull().And.ContainAny("MessageAgent").And.NotContainAny("{{").And.NotContainAny("}}");
            });
        }
Пример #3
0
        public void RenderSnippetAsyncWithMissingMessagingObjectWithWarning(ISnippetRenderer renderer, AzureIntegrationServicesModel model, ProcessManager processManager, ProcessManager missingProcessManager, TargetResourceTemplate resourceTemplate, TargetResourceSnippet resourceSnippet, WorkflowObject workflowObject, string snippetContent, string renderedContent, Exception e)
        {
            "Given a snippet renderer"
            .x(() => renderer = new LiquidSnippetRenderer(_mockLogger.Object));

            "And a model"
            .x(() => model = _model);

            "And a missing process manager"
            .x(() =>
            {
                missingProcessManager = new ProcessManager("MissingProcessManager")
                {
                    Key = "MissingProcessManager"
                };
            });

            "And a workflow object"
            .x(() =>
            {
                var messagingObject = model.FindMessagingObject("ContosoMessageBus:AppA:FtpTransformWorkflow");
                messagingObject.messagingObject.Should().NotBeNull().And.BeOfType(typeof(ProcessManager));
                processManager   = (ProcessManager)messagingObject.messagingObject;
                resourceTemplate = processManager.Resources.First();
                resourceSnippet  = processManager.Snippets.First();
                workflowObject   = processManager.WorkflowModel.Activities.First();
            });

            "And a snippet content"
            .x(() => snippetContent = _missingProcessManagerSnippet);

            "When rendering the snippet"
            .x(async() => e = await Record.ExceptionAsync(async() => renderedContent = await renderer.RenderSnippetAsync(snippetContent, model, missingProcessManager, resourceTemplate, resourceSnippet, workflowObject)));

            "Then the render should succeed"
            .x(() => e.Should().BeNull());

            "And the rendered content should not have expected values from the model"
            .x(() =>
            {
                renderedContent.Should().NotBeNull().And.Be("").And.NotContainAny("{{").And.NotContainAny("}}");

                // Verify warning was raised
                _mockLogger.Verify(l => l.Log(
                                       It.Is <LogLevel>(l => l == LogLevel.Warning),
                                       It.IsAny <EventId>(),
                                       It.Is <It.IsAnyType>((v, t) => v.ToString().Contains("does not appear in the target model", StringComparison.CurrentCultureIgnoreCase)),
                                       It.IsAny <Exception>(),
                                       It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true)), Times.Once);
            });
        }
Пример #4
0
        public void RenderSnippetAsyncWithSuccess(ISnippetRenderer renderer, AzureIntegrationServicesModel model, ProcessManager processManager, TargetResourceTemplate resourceTemplate, TargetResourceSnippet resourceSnippet, WorkflowObject workflowObject, string snippetContent, string renderedContent, Exception e)
        {
            "Given a snippet renderer"
            .x(() => renderer = new LiquidSnippetRenderer(_mockLogger.Object));

            "And a model"
            .x(() => model = _model);

            "And a process manager"
            .x(() =>
            {
                var messagingObject = model.FindMessagingObject("ContosoMessageBus:AppA:FtpTransformWorkflow");
                messagingObject.messagingObject.Should().NotBeNull().And.BeOfType(typeof(ProcessManager));
                processManager   = (ProcessManager)messagingObject.messagingObject;
                resourceTemplate = processManager.Resources.First();
                resourceSnippet  = processManager.Snippets.First();
            });

            "And a workflow object"
            .x(() =>
            {
                workflowObject = processManager.WorkflowModel.Activities.First();
            });

            "And a snippet content"
            .x(() => snippetContent = _snippetContent);

            "When rendering the snippet"
            .x(async() => e = await Record.ExceptionAsync(async() => renderedContent = await renderer.RenderSnippetAsync(snippetContent, model, processManager, resourceTemplate, resourceSnippet, workflowObject)));

            "Then the render should succeed"
            .x(() => e.Should().BeNull());

            "And the rendered content should have expected values from the model"
            .x(() =>
            {
                renderedContent.Should().NotBeNull().And.ContainAny($"StepName:_{workflowObject.Name.Replace(" ", "_", StringComparison.InvariantCulture)}").And.NotContainAny("{{").And.NotContainAny("}}");
            });
        }
Пример #5
0
        public void RenderTemplateAsyncWithSnippetContentNullError(ISnippetRenderer renderer, AzureIntegrationServicesModel model, ProcessManager processManager, TargetResourceTemplate resourceTemplate, TargetResourceSnippet resourceSnippet, WorkflowObject workflowObject, string snippetContent, string renderedContent, Exception e)
        {
            "Given a snippet renderer"
            .x(() => renderer = new LiquidSnippetRenderer(_mockLogger.Object));

            "And a model"
            .x(() => model = _model);

            "And a process manager"
            .x(() =>
            {
                var messagingObject = model.FindMessagingObject("ContosoMessageBus:AppA:FtpTransformWorkflow");
                messagingObject.messagingObject.Should().NotBeNull().And.BeOfType(typeof(ProcessManager));
                processManager   = (ProcessManager)messagingObject.messagingObject;
                resourceTemplate = processManager.Resources.First();
                resourceSnippet  = processManager.Snippets.First();
            });

            "And a workflow object"
            .x(() =>
            {
                workflowObject = processManager.WorkflowModel.Activities.First();
            });

            "And null snippet content"
            .x(() => snippetContent = null);

            "When rendering the template"
            .x(async() => e = await Record.ExceptionAsync(async() => renderedContent = await renderer.RenderSnippetAsync(snippetContent, model, processManager, resourceTemplate, resourceSnippet, workflowObject)));

            "Then the render should error"
            .x(() => e.Should().NotBeNull().And.Subject.Should().BeOfType <ArgumentNullException>().Which.ParamName.Should().Be("snippetContent"));

            "And the rendered content should not have a value"
            .x(() => renderedContent.Should().BeNull());
        }
Пример #6
0
        /// <summary>
        /// Renders a template using a Liquid template engine.
        /// </summary>
        /// <param name="templateContent">The template content to render.</param>
        /// <param name="model">The model used to provide properties to Liquid templates.</param>
        /// <param name="messagingObject">An optional messaging object to add to the variable context, accessible to templates.</param>
        /// <param name="resourceTemplate">An optional resource template object to add to the variable context, accessible to templates.</param>
        /// <returns>The rendered template content.</returns>
        public async Task <string> RenderTemplateAsync(string templateContent, AzureIntegrationServicesModel model, MessagingObject messagingObject = null, TargetResourceTemplate resourceTemplate = null)
        {
            _ = model ?? throw new ArgumentNullException(nameof(model));
            _ = templateContent ?? throw new ArgumentNullException(nameof(templateContent));

            // Create variables on script object, to be accessible to Liquid templates
            var scriptObject = new ScriptObject
            {
                ["model"] = model
            };

            // Is there a messaging object?
            if (messagingObject != null)
            {
                var messagingObjects = model.FindMessagingObject(messagingObject.Key);
                if (messagingObjects.messageBus != null)
                {
                    scriptObject["message_bus"] = messagingObjects.messageBus;

                    if (messagingObjects.application != null)
                    {
                        scriptObject["application"] = messagingObjects.application;
                    }

                    if (messagingObjects.messagingObject != null)
                    {
                        scriptObject["messaging_object"] = messagingObjects.messagingObject;

                        switch (messagingObjects.messagingObject.Type)
                        {
                        case MessagingObjectType.Message:
                            scriptObject["message"] = (Message)messagingObjects.messagingObject;
                            break;

                        case MessagingObjectType.Channel:
                            scriptObject["channel"] = (Channel)messagingObjects.messagingObject;
                            break;

                        case MessagingObjectType.Intermediary:
                            scriptObject["intermediary"] = (Intermediary)messagingObjects.messagingObject;
                            break;

                        case MessagingObjectType.Endpoint:
                            scriptObject["endpoint"] = (Endpoint)messagingObjects.messagingObject;
                            break;
                        }
                    }
                }
                else
                {
                    // Should never happen, unless the messaging object is not attached to the target model
                    _logger.LogWarning(WarningMessages.MessagingObjectMissingInModel, messagingObject.Key);
                }
            }

            // Is there a resource template?
            if (resourceTemplate != null)
            {
                scriptObject["resource_template"] = resourceTemplate;
            }

            // Push variables onto the context
            _context.PushGlobal(scriptObject);

            try
            {
                // Render template
                var template        = Template.ParseLiquid(templateContent);
                var renderedContent = await template.RenderAsync(_context).ConfigureAwait(false);

                return(renderedContent);
            }
            finally
            {
                // Pop model from context stack
                _context.PopGlobal();
            }
        }