Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonProcessor interface to convert JObject #416

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Health.Fhir.Liquid.Converter.Exceptions;
using Microsoft.Health.Fhir.Liquid.Converter.Models;
using Microsoft.Health.Fhir.Liquid.Converter.Processors;
using Newtonsoft.Json.Linq;
using Xunit;

namespace Microsoft.Health.Fhir.Liquid.Converter.UnitTests.Processors
Expand All @@ -20,6 +21,7 @@ public class ProcessorTests
private static readonly string _hl7v2TestData;
private static readonly string _ccdaTestData;
private static readonly string _jsonTestData;
private static readonly string _jsonExpectData;
private static readonly string _fhirStu3TestData;
private static readonly ProcessorSettings _processorSettings;

Expand All @@ -28,6 +30,7 @@ static ProcessorTests()
_hl7v2TestData = File.ReadAllText(Path.Join(TestConstants.SampleDataDirectory, "Hl7v2", "LRI_2.0-NG_CBC_Typ_Message.hl7"));
_ccdaTestData = File.ReadAllText(Path.Join(TestConstants.SampleDataDirectory, "Ccda", "CCD.ccda"));
_jsonTestData = File.ReadAllText(Path.Join(TestConstants.SampleDataDirectory, "Json", "ExamplePatient.json"));
_jsonExpectData = File.ReadAllText(Path.Join(TestConstants.ExpectedDirectory, "ExamplePatient.json"));
_fhirStu3TestData = File.ReadAllText(Path.Join(TestConstants.SampleDataDirectory, "Stu3", "Patient.json"));
_processorSettings = new ProcessorSettings();
}
Expand Down Expand Up @@ -242,5 +245,15 @@ public void GivenTemplateWithNestingTooDeep_WhenConvert_ExceptionShouldBeThrown(
exception = Assert.Throws<RenderException>(() => processor.Convert(data, "NestingTooDeepDiffTemplate", templateProvider));
Assert.Contains("Nesting too deep", exception.Message);
}

[Fact]
public void GivenJObjectInput_WhenConvertWithJsonProcessor_CorrectResultShouldBeReturned()
{
var processor = new JsonProcessor(_processorSettings);
var templateProvider = new TemplateProvider(TestConstants.JsonTemplateDirectory, DataType.Json);
var testData = JObject.Parse(_jsonTestData);
var result = processor.Convert(testData, "ExamplePatient", templateProvider);
Assert.True(JToken.DeepEquals(JObject.Parse(_jsonExpectData), JToken.Parse(result)));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"resourceType": "Patient",
"id": "e8b835f7-0c46-4166-22ad-23e6783aaf54",
"identifier": [
{
"use": "usual",
"type": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/v2-0203",
"code": "MR"
}
]
},
"system": "urn:oid:2.16.840.1.113883.19.5",
"value": "M0R1N2"
}
],
"active": true,
"name": [
{
"family": "Smith",
"given": [
"Jerry"
]
}
],
"telecom": [
{
"system": "phone",
"value": "1234-5678"
},
{
"system": "phone",
"value": "1234-5679"
}
],
"gender": "male",
"birthDate": "2001-01-10",
"managingOrganization": {
"reference": "Organization/2.16.840.1.113883.19.5",
"display": "Good Health Clinic"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using Microsoft.Health.Fhir.Liquid.Converter.Exceptions;
using Microsoft.Health.Fhir.Liquid.Converter.Extensions;
using Microsoft.Health.Fhir.Liquid.Converter.Models;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
using System.Collections.Generic;
using System.Globalization;
using DotLiquid;
using Microsoft.Health.Fhir.Liquid.Converter.Extensions;
using Microsoft.Health.Fhir.Liquid.Converter.Models;
using Microsoft.Health.Fhir.Liquid.Converter.Models.Json;
using Microsoft.Health.Fhir.Liquid.Converter.Parsers;
using Newtonsoft.Json.Linq;
using NJsonSchema;

namespace Microsoft.Health.Fhir.Liquid.Converter.Processors
Expand All @@ -28,6 +30,12 @@ public override string Convert(string data, string rootTemplate, ITemplateProvid
return Convert(jsonData, rootTemplate, templateProvider, traceInfo);
}

public string Convert(JObject data, string rootTemplate, ITemplateProvider templateProvider, TraceInfo traceInfo = null)
{
var jsonData = data.ToObject();
return Convert(jsonData, rootTemplate, templateProvider, traceInfo);
}

protected override Context CreateContext(ITemplateProvider templateProvider, IDictionary<string, object> data)
{
// Load data and templates
Expand Down