Skip to content

Commit

Permalink
Merge branch 'dotliquid'
Browse files Browse the repository at this point in the history
  • Loading branch information
Yue Fei committed Jul 5, 2022
2 parents 39911bb + 237a81b commit f56e051
Show file tree
Hide file tree
Showing 47 changed files with 816 additions and 110 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace Microsoft.Health.Fhir.Liquid.Converter.FunctionalTests
{
public class FunctionalTests
{
private static readonly ProcessorSettings _processorSettings = new ProcessorSettings();

public static IEnumerable<object[]> GetDataForHl7v2()
{
var data = new List<string[]>
Expand Down Expand Up @@ -284,7 +286,7 @@ public static IEnumerable<object[]> GetDataForStu3ToR4()
public void GivenCcdaMessageForTimezoneTesting_WhenConvert_ExpectedResultShouldBeReturned()
{
var inputFile = Path.Combine("TestData", "TimezoneHandling", "Input", "CcdaTestTimezoneInput.ccda");
var ccdaProcessor = new CcdaProcessor();
var ccdaProcessor = new CcdaProcessor(_processorSettings);
var templateDirectory = Path.Join("TestData", "TimezoneHandling", "Template");

var inputContent = File.ReadAllText(inputFile);
Expand All @@ -304,7 +306,7 @@ public void GivenCcdaMessageForTimezoneTesting_WhenConvert_ExpectedResultShouldB
public void GivenHl7v2MessageForTimeZoneTesting_WhenConvert_ExpectedResultShouldBeReturned()
{
var inputFile = Path.Combine("TestData", "TimezoneHandling", "Input", "Hl7v2TestTimezoneInput.hl7v2");
var hl7v2Processor = new Hl7v2Processor();
var hl7v2Processor = new Hl7v2Processor(_processorSettings);
var templateDirectory = Path.Join("TestData", "TimezoneHandling", "Template");

var inputContent = File.ReadAllText(inputFile);
Expand All @@ -325,7 +327,7 @@ public void GivenHl7v2MessageForTimeZoneTesting_WhenConvert_ExpectedResultShould
[MemberData(nameof(GetDataForHl7v2))]
public void GivenHl7v2Message_WhenConverting_ExpectedFhirResourceShouldBeReturned(string rootTemplate, string inputFile, string expectedFile)
{
var hl7v2Processor = new Hl7v2Processor();
var hl7v2Processor = new Hl7v2Processor(_processorSettings);
var templateDirectory = Path.Join(AppDomain.CurrentDomain.BaseDirectory, Constants.TemplateDirectory, "Hl7v2");

var inputContent = File.ReadAllText(inputFile);
Expand Down Expand Up @@ -354,7 +356,7 @@ public void GivenHl7v2Message_WhenConverting_ExpectedFhirResourceShouldBeReturne
[MemberData(nameof(GetDataForCcda))]
public void GivenCcdaDocument_WhenConverting_ExpectedFhirResourceShouldBeReturned(string rootTemplate, string inputFile, string expectedFile)
{
var ccdaProcessor = new CcdaProcessor();
var ccdaProcessor = new CcdaProcessor(_processorSettings);
var templateDirectory = Path.Join(AppDomain.CurrentDomain.BaseDirectory, Constants.TemplateDirectory, "Ccda");

var inputContent = File.ReadAllText(inputFile);
Expand All @@ -375,7 +377,7 @@ public void GivenCcdaDocument_WhenConverting_ExpectedFhirResourceShouldBeReturne
[MemberData(nameof(GetDataForStu3ToR4))]
public void GivenStu3FhirData_WhenConverting_ExpectedR4FhirResourceShouldBeReturned(string rootTemplate, string inputFile, string expectedFile)
{
var fhirProcessor = new FhirProcessor();
var fhirProcessor = new FhirProcessor(_processorSettings);
var templateDirectory = Path.Join(AppDomain.CurrentDomain.BaseDirectory, Constants.TemplateDirectory, "Stu3ToR4");

var inputContent = File.ReadAllText(inputFile);
Expand All @@ -392,7 +394,7 @@ public void GivenStu3FhirData_WhenConverting_ExpectedR4FhirResourceShouldBeRetur
[MemberData(nameof(GetDataForJson))]
public void GivenJsonData_WhenConverting_ExpectedFhirResourceShouldBeReturned(string rootTemplate, string inputFile, string expectedFile)
{
var jsonProcessor = new JsonProcessor();
var jsonProcessor = new JsonProcessor(_processorSettings);
var templateDirectory = Path.Join(AppDomain.CurrentDomain.BaseDirectory, Constants.TemplateDirectory, "Json");

var inputContent = File.ReadAllText(inputFile);
Expand All @@ -408,7 +410,7 @@ public void GivenJsonData_WhenConverting_ExpectedFhirResourceShouldBeReturned(st
[Fact]
public void GivenAnInvalidTemplate_WhenConverting_ExceptionsShouldBeThrown()
{
var hl7v2Processor = new Hl7v2Processor();
var hl7v2Processor = new Hl7v2Processor(_processorSettings);
var templateCollection = new List<Dictionary<string, Template>>
{
new Dictionary<string, Template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public class RuleBasedTests
private static readonly string _ccdaTemplateFolder = Path.Combine(Constants.TemplateDirectory, "Ccda");
private static readonly string _ccdaDataFolder = Path.Combine(Constants.SampleDataDirectory, "Ccda");

private static readonly ProcessorSettings _processorSettings = new ProcessorSettings();

private static readonly ITemplateProvider _hl7TemplateProvider = new TemplateProvider(_hl7TemplateFolder, DataType.Hl7v2);
private static readonly ITemplateProvider _ccdaTemplateProvider = new TemplateProvider(_ccdaTemplateFolder, DataType.Ccda);

Expand Down Expand Up @@ -450,10 +452,10 @@ private async Task<JObject> ConvertData(string templateName, string samplePath,
switch (dataType)
{
case DataType.Hl7v2:
return JObject.Parse(new Hl7v2Processor()
return JObject.Parse(new Hl7v2Processor(_processorSettings)
.Convert(await File.ReadAllTextAsync(samplePath, Encoding.UTF8), templateName, _hl7TemplateProvider));
case DataType.Ccda:
return JObject.Parse(new CcdaProcessor()
return JObject.Parse(new CcdaProcessor(_processorSettings)
.Convert(await File.ReadAllTextAsync(samplePath, Encoding.UTF8), templateName, _ccdaTemplateProvider));
default:
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ internal static class ConverterLogicHandler
{
private const string MetadataFileName = "metadata.json";
private static readonly List<string> CcdaExtensions = new List<string> { ".ccda", ".xml" };
private static readonly ProcessorSettings DefaultProcessorSettings = new ProcessorSettings();

internal static void Convert(ConverterOptions options)
{
Expand Down Expand Up @@ -96,10 +97,10 @@ private static IFhirConverter CreateDataProcessor(DataType dataType)
{
return dataType switch
{
DataType.Hl7v2 => new Hl7v2Processor(),
DataType.Ccda => new CcdaProcessor(),
DataType.Json => new JsonProcessor(),
DataType.Fhir => new FhirProcessor(),
DataType.Hl7v2 => new Hl7v2Processor(DefaultProcessorSettings),
DataType.Ccda => new CcdaProcessor(DefaultProcessorSettings),
DataType.Json => new JsonProcessor(DefaultProcessorSettings),
DataType.Fhir => new FhirProcessor(DefaultProcessorSettings),
_ => throw new NotImplementedException($"The conversion from data type {dataType} to FHIR is not supported")
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static IEnumerable<object[]> GetInvalidEvaluateTemplateContents()
public void GivenValidEvaluateTemplateContent_WhenParseAndRender_CorrectResultShouldBeReturned(string templateContent)
{
// Template should be parsed correctly
var template = TemplateUtility.ParseTemplate(TemplateName, templateContent);
var template = TemplateUtility.ParseLiquidTemplate(TemplateName, templateContent);
Assert.True(template.Root.NodeList.Count > 0);

// Template should be rendered correctly
Expand All @@ -66,14 +66,14 @@ public void GivenValidEvaluateTemplateContent_WhenParseAndRender_CorrectResultSh
[MemberData(nameof(GetInvalidEvaluateTemplateContents))]
public void GivenInvalidEvaluateTemplateContent_WhenParse_ExceptionsShouldBeThrown(string templateContent)
{
Assert.Throws<TemplateLoadException>(() => TemplateUtility.ParseTemplate(TemplateName, templateContent));
Assert.Throws<TemplateLoadException>(() => TemplateUtility.ParseLiquidTemplate(TemplateName, templateContent));
}

[Fact]
public void GivenInvalidSnippet_WhenRender_ExceptionsShouldBeThrown()
{
// No template file system
var template = TemplateUtility.ParseTemplate(TemplateName, @"{% evaluate bundleId using 'ID/Bundle' Data: hl7v2Data -%}");
var template = TemplateUtility.ParseLiquidTemplate(TemplateName, @"{% evaluate bundleId using 'ID/Bundle' Data: hl7v2Data -%}");
var context = new Context(
environments: new List<Hash>(),
outerScope: new Hash(),
Expand All @@ -85,7 +85,7 @@ public void GivenInvalidSnippet_WhenRender_ExceptionsShouldBeThrown()
Assert.Throws<FileSystemException>(() => template.Render(RenderParameters.FromContext(context, CultureInfo.InvariantCulture)));

// Valid template file system but no such template
template = TemplateUtility.ParseTemplate(TemplateName, @"{% evaluate bundleId using 'ID/Foo' Data: hl7v2Data -%}");
template = TemplateUtility.ParseLiquidTemplate(TemplateName, @"{% evaluate bundleId using 'ID/Foo' Data: hl7v2Data -%}");
var templateProvider = new TemplateProvider(TestConstants.Hl7v2TemplateDirectory, DataType.Hl7v2);
context = new Context(
environments: new List<Hash>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static IEnumerable<object[]> GetInvalidMergeDiffTemplateWithErrorSyntax()
public void GivenValidMergeDiffTemplateContent_WhenParseAndRender_CorrectResultShouldBeReturned(string templateContent, string expectedResult)
{
// Template should be parsed correctly
var template = TemplateUtility.ParseTemplate(TemplateName, templateContent);
var template = TemplateUtility.ParseLiquidTemplate(TemplateName, templateContent);
Assert.True(template.Root.NodeList.Count > 0);

// Template should be rendered correctly
Expand Down Expand Up @@ -77,7 +77,7 @@ public void GivenValidMergeDiffTemplateContent_WhenParseAndRender_CorrectResultS
[MemberData(nameof(GetInvalidMergeDiffBlockContentTypes))]
public void GivenInvalidDiffBlockContent_WhenParseAndRender_ExceptionsShouldBeThrown(string templateContent)
{
var template = TemplateUtility.ParseTemplate(TemplateName, templateContent);
var template = TemplateUtility.ParseLiquidTemplate(TemplateName, templateContent);

var templateFolder = Path.Join(TestConstants.TestTemplateDirectory, @"InvalidMergeDiffTemplates");
var templateProvider = new TemplateProvider(templateFolder, DataType.Json);
Expand All @@ -101,7 +101,7 @@ public void GivenInvalidDiffBlockContent_WhenParseAndRender_ExceptionsShouldBeTh
public void GivenInvalidDiffBlockContentWithTooMuchRecursive_WhenParseAndRender_ExceptionsShouldBeThrown()
{
var templateContent = File.ReadAllText(Path.Join(TestConstants.TestTemplateDirectory, @"InvalidMergeDiffTemplates/RecursiveTooMuchTemplate.liquid"));
var template = TemplateUtility.ParseTemplate(TemplateName, templateContent);
var template = TemplateUtility.ParseLiquidTemplate(TemplateName, templateContent);

var templateFolder = Path.Join(TestConstants.TestTemplateDirectory, @"InvalidMergeDiffTemplates");
var templateProvider = new TemplateProvider(templateFolder, DataType.Json);
Expand All @@ -125,14 +125,14 @@ public void GivenInvalidDiffBlockContentWithTooMuchRecursive_WhenParseAndRender_
[MemberData(nameof(GetInvalidMergeDiffTemplateWithErrorSyntax))]
public void GivenInvalidMergeDiffTemplateWithErrorSyntax_WhenParse_ExceptionsShouldBeThrown(string templateContent)
{
Assert.Throws<TemplateLoadException>(() => TemplateUtility.ParseTemplate(TemplateName, templateContent));
Assert.Throws<TemplateLoadException>(() => TemplateUtility.ParseLiquidTemplate(TemplateName, templateContent));
}

[Fact]
public void GivenValidDiffBlockContent_WhenInputIsInvalidJson_WhenParseAndRender_ExceptionsShouldBeThrown()
{
var templateContent = File.ReadAllText(Path.Join(TestConstants.TestTemplateDirectory, @"ValidMergeDiffTemplates/ValidExample2.liquid"));
var template = TemplateUtility.ParseTemplate(TemplateName, templateContent);
var template = TemplateUtility.ParseLiquidTemplate(TemplateName, templateContent);

var templateFolder = Path.Join(TestConstants.TestTemplateDirectory, @"ValidMergeDiffTemplates");
var templateProvider = new TemplateProvider(templateFolder, DataType.Json);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@

using System;
using System.Globalization;
using System.IO;
using DotLiquid;
using Microsoft.Health.Fhir.Liquid.Converter.DotLiquids;
using Microsoft.Health.Fhir.Liquid.Converter.Exceptions;
using Microsoft.Health.Fhir.Liquid.Converter.Models;
using Microsoft.Health.Fhir.Liquid.Converter.Models.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Schema;
using Xunit;

namespace Microsoft.Health.Fhir.Liquid.Converter.UnitTests.DotLiquids
Expand Down Expand Up @@ -44,6 +48,26 @@ public void GivenAValidTemplateDirectory_WhenGetTemplateWithContext_CorrectResul
Assert.Throws<RenderException>(() => templateLocalFileSystem.GetTemplate(context, "Bar"));
}

[Fact]
public void GivenAValidTemplateDirectory_WhenGetJsonSchemaTemplate_CorrectResultsShouldBeReturned()
{
var templateLocalFileSystem = new TemplateLocalFileSystem(Path.Join(TestConstants.TestTemplateDirectory, @"ValidValidateTemplates"), DataType.Hl7v2);
var testSchemaPath = "Schemas/TestSchema.schema.json";
var context = new Context(CultureInfo.InvariantCulture);

var schemaTemplate = templateLocalFileSystem.GetTemplate(testSchemaPath);

// Template exists
Assert.NotNull(schemaTemplate);

var jSchemaDocument = schemaTemplate.Root as JSchemaDocument;
Assert.NotNull(jSchemaDocument);
Assert.NotNull(jSchemaDocument.Schema);

JSchema expectedJSchema = JSchema.Parse(File.ReadAllText(Path.Join(TestConstants.TestTemplateDirectory, @"ValidValidateTemplates", testSchemaPath)));
Assert.True(JToken.DeepEquals(JToken.Parse(jSchemaDocument.Schema.ToString()), JToken.Parse(expectedJSchema.ToString())));
}

[Fact]
public void GivenAValidTemplateDirectory_WhenReadTemplateWithContext_ExceptionShouldBeThrown()
{
Expand All @@ -52,5 +76,14 @@ public void GivenAValidTemplateDirectory_WhenReadTemplateWithContext_ExceptionSh
context["ADT_A01"] = "ADT_A01";
Assert.Throws<NotImplementedException>(() => templateLocalFileSystem.ReadTemplateFile(context, "hello"));
}

[Fact]
public void GivenAValidTemplateDirectory_WhenGetInvalidJsonSchemaTemplate_CorrectResultsShouldBeReturned()
{
var templateLocalFileSystem = new TemplateLocalFileSystem(Path.Join(TestConstants.TestTemplateDirectory, @"InvalidValidateTemplates"), DataType.Hl7v2);
var testSchemaPath = "Schemas/InvalidTestSchema.schema.json";
var context = new Context(CultureInfo.InvariantCulture);
Assert.Throws<TemplateLoadException>(() => templateLocalFileSystem.GetTemplate(testSchemaPath));
}
}
}
Loading

0 comments on commit f56e051

Please sign in to comment.