<commit_before>﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using System.Net;
using System.IO;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Model;
using Hl7.Fhir.Support;
using Hl7.Fhir.Search;
using System.Threading.Tasks;

namespace Hl7.Fhir.Tests
{
    [TestClass]
#if PORTABLE45
	public class PortableFhirClientTests
#else
	public class FhirClientTests
#endif
    {    
        Uri testEndpoint = new Uri("http://spark.furore.com/fhir");
        // Uri testEndpoint = new Uri("http://localhost.fiddler:1396/fhir");
        // Uri testEndpoint = new Uri("http://localhost:1396/fhir");
        //Uri testEndpoint = new Uri("http://fhir.healthintersections.com.au/open");
        // Uri testEndpoint = new Uri("https://api.fhir.me");

		[TestInitialize]
		public void TestInitialize()
		{
			System.Diagnostics.Trace.WriteLine("Testing against fhir server: " + testEndpoint.OriginalString);
		}

        [TestMethod, TestCategory("FhirClient")]
        public void FetchConformance()
        {
            FhirClient client = new FhirClient(testEndpoint);

            var entry = client.Conformance();
            var c = entry.Resource;

            Assert.IsNotNull(c);
            // Assert.AreEqual("Spark.Service", c.Software.Name); // This is only for ewout's server
            Assert.AreEqual(Conformance.RestfulConformanceMode.Server, c.Rest[0].Mode.Value);
            Assert.AreEqual(HttpStatusCode.OK, client.LastResponseDetails.Result);

            // Does't currently work on Grahame's server
            Assert.AreEqual(ContentType.XML_CONTENT_HEADER, client.LastResponseDetails.ContentType.ToLower());
        }


        [TestMethod, TestCategory("FhirClient")]
        public void ReadWithFormat()
        {
            FhirClient client = new FhirClient(testEndpoint);

            client.UseFormatParam = true;
            client.PreferredFormat = ResourceFormat.Json;

            var loc = client.Read("Patient/1");

            Assert.AreEqual(ResourceFormat.Json, ContentType.GetResourceFormatFromContentType(client.LastResponseDetails.ContentType));
        }


        [TestMethod, TestCategory("FhirClient")]
        public void Read()
        {
            FhirClient client = new FhirClient(testEndpoint);

            var loc = client.Read<Location>("Location/1");
            Assert.IsNotNull(loc);
            Assert.AreEqual("Den Burg", loc.Resource.Address.City);

            string version = new ResourceIdentity(loc.SelfLink).VersionId;
            Assert.IsNotNull(version);
            string id = new ResourceIdentity(loc.Id).Id;
            Assert.AreEqual("1", id);

            try
            {
                var random = client.Read(new Uri("Location/45qq54", UriKind.Relative));
                Assert.Fail();
            }
            catch (FhirOperationException)
            {
                Assert.IsTrue(client.LastResponseDetails.Result == HttpStatusCode.NotFound);
            }

            var loc2 = client.Read<Location>(ResourceIdentity.Build("Location","1", version));
            Assert.IsNotNull(loc2);
            Assert.AreEqual(FhirSerializer.SerializeBundleEntryToJson(loc),
                            FhirSerializer.SerializeBundleEntryToJson(loc2));

            var loc3 = client.Read<Location>(loc.SelfLink);
            Assert.IsNotNull(loc3);
            Assert.AreEqual(FhirSerializer.SerializeBundleEntryToJson(loc),
                            FhirSerializer.SerializeBundleEntryToJson(loc3));        
        }


        [TestMethod, TestCategory("FhirClient")]
        public void ReadRelative()
        {
            FhirClient client = new FhirClient(testEndpoint);

            var loc = client.Read<Location>(new Uri("Location/1", UriKind.Relative));
            Assert.IsNotNull(loc);
            Assert.AreEqual("Den Burg", loc.Resource.Address.City);            

            var ri = ResourceIdentity.Build(testEndpoint, "Location", "1");
            loc = client.Read<Location>(ri);
            Assert.IsNotNull(loc);
            Assert.AreEqual("Den Burg", loc.Resource.Address.City);            
        }

#if PORTABLE45
		[TestMethod, TestCategory("FhirClient")]
		public void ReadRelativeAsync()
		{
			FhirClient client = new FhirClient(testEndpoint);

			var loc = client.ReadAsync<Location>(new Uri("Location/1", UriKind.Relative)).Result;
			Assert.IsNotNull(loc);
			Assert.AreEqual("<NAME>", loc.Resource.Address.City);

			var ri = ResourceIdentity.Build(testEndpoint, "Location", "1");
			loc = client.ReadAsync<Location>(ri).Result;
			Assert.IsNotNull(loc);
			Assert.AreEqual("<NAME>", loc.Resource.Address.City);
		}
#endif
        [TestMethod, TestCategory("FhirClient")]
        public void Search()
        {
            FhirClient client = new FhirClient(testEndpoint);
            Bundle result;

            result = client.Search<DiagnosticReport>();
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count() > 10, "Test should use testdata with more than 10 reports");

            result = client.Search<DiagnosticReport>(pageSize: 10);
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count <= 10);

            var withSubject = 
                result.Entries.ByResourceType<DiagnosticReport>().FirstOrDefault(dr => dr.Resource.Subject != null);
            Assert.IsNotNull(withSubject, "Test should use testdata with a report with a subject");

            ResourceIdentity ri = new ResourceIdentity(withSubject.Id);

            result = client.SearchById<DiagnosticReport>(ri.Id, 
                        includes: new string[] { "DiagnosticReport.subject" });
            Assert.IsNotNull(result);

            Assert.AreEqual(2, result.Entries.Count);  // should have subject too

            Assert.IsNotNull(result.Entries.Single(entry => new ResourceIdentity(entry.Id).Collection ==
                        typeof(DiagnosticReport).GetCollectionName()));
            Assert.IsNotNull(result.Entries.Single(entry => new ResourceIdentity(entry.Id).Collection ==
                        typeof(Patient).GetCollectionName()));

            result = client.Search<Patient>(new string[] { "name=Everywoman", "name=Eve" });

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count > 0);
        }


        [TestMethod, TestCategory("FhirClient")]
        public void Paging()
        {
            FhirClient client = new FhirClient(testEndpoint);

            var result = client.Search<DiagnosticReport>(pageSize: 10);
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count <= 10);

            var firstId = result.Entries.First().Id;

            // Browse forward
            result = client.Continue(result);
            Assert.IsNotNull(result);
            var nextId = result.Entries.First().Id;
            Assert.AreNotEqual(firstId, nextId);

            // Browse to first
            result = client.Continue(result, PageDirection.First);<commit_msg>Include test case for SearchAsync routines<commit_after>﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
using System.Net;
using System.IO;
using Hl7.Fhir.Rest;
using Hl7.Fhir.Serialization;
using Hl7.Fhir.Model;
using Hl7.Fhir.Support;
using Hl7.Fhir.Search;
using System.Threading.Tasks;

namespace Hl7.Fhir.Tests
{
    [TestClass]
#if PORTABLE45
	public class PortableFhirClientTests
#else
	public class FhirClientTests
#endif
    {    
        Uri testEndpoint = new Uri("http://spark.furore.com/fhir");
        // Uri testEndpoint = new Uri("http://localhost.fiddler:1396/fhir");
        // Uri testEndpoint = new Uri("http://localhost:1396/fhir");
        //Uri testEndpoint = new Uri("http://fhir.healthintersections.com.au/open");
        // Uri testEndpoint = new Uri("https://api.fhir.me");

		[TestInitialize]
		public void TestInitialize()
		{
			System.Diagnostics.Trace.WriteLine("Testing against fhir server: " + testEndpoint.OriginalString);
		}

        [TestMethod, TestCategory("FhirClient")]
        public void FetchConformance()
        {
            FhirClient client = new FhirClient(testEndpoint);

            var entry = client.Conformance();
            var c = entry.Resource;

            Assert.IsNotNull(c);
            // Assert.AreEqual("Spark.Service", c.Software.Name); // This is only for ewout's server
            Assert.AreEqual(Conformance.RestfulConformanceMode.Server, c.Rest[0].Mode.Value);
            Assert.AreEqual(HttpStatusCode.OK, client.LastResponseDetails.Result);

            // Does't currently work on Grahame's server
            Assert.AreEqual(ContentType.XML_CONTENT_HEADER, client.LastResponseDetails.ContentType.ToLower());
        }


        [TestMethod, TestCategory("FhirClient")]
        public void ReadWithFormat()
        {
            FhirClient client = new FhirClient(testEndpoint);

            client.UseFormatParam = true;
            client.PreferredFormat = ResourceFormat.Json;

            var loc = client.Read("Patient/1");

            Assert.AreEqual(ResourceFormat.Json, ContentType.GetResourceFormatFromContentType(client.LastResponseDetails.ContentType));
        }


        [TestMethod, TestCategory("FhirClient")]
        public void Read()
        {
            FhirClient client = new FhirClient(testEndpoint);

            var loc = client.Read<Location>("Location/1");
            Assert.IsNotNull(loc);
            Assert.AreEqual("<NAME>", loc.Resource.Address.City);

            string version = new ResourceIdentity(loc.SelfLink).VersionId;
            Assert.IsNotNull(version);
            string id = new ResourceIdentity(loc.Id).Id;
            Assert.AreEqual("1", id);

            try
            {
                var random = client.Read(new Uri("Location/45qq54", UriKind.Relative));
                Assert.Fail();
            }
            catch (FhirOperationException)
            {
                Assert.IsTrue(client.LastResponseDetails.Result == HttpStatusCode.NotFound);
            }

            var loc2 = client.Read<Location>(ResourceIdentity.Build("Location","1", version));
            Assert.IsNotNull(loc2);
            Assert.AreEqual(FhirSerializer.SerializeBundleEntryToJson(loc),
                            FhirSerializer.SerializeBundleEntryToJson(loc2));

            var loc3 = client.Read<Location>(loc.SelfLink);
            Assert.IsNotNull(loc3);
            Assert.AreEqual(FhirSerializer.SerializeBundleEntryToJson(loc),
                            FhirSerializer.SerializeBundleEntryToJson(loc3));        
        }


        [TestMethod, TestCategory("FhirClient")]
        public void ReadRelative()
        {
            FhirClient client = new FhirClient(testEndpoint);

            var loc = client.Read<Location>(new Uri("Location/1", UriKind.Relative));
            Assert.IsNotNull(loc);
            Assert.AreEqual("Den Burg", loc.Resource.Address.City);            

            var ri = ResourceIdentity.Build(testEndpoint, "Location", "1");
            loc = client.Read<Location>(ri);
            Assert.IsNotNull(loc);
            Assert.AreEqual("Den Burg", loc.Resource.Address.City);            
        }

#if PORTABLE45
		[TestMethod, TestCategory("FhirClient")]
		public void ReadRelativeAsync()
		{
			FhirClient client = new FhirClient(testEndpoint);

			var loc = client.ReadAsync<Location>(new Uri("Location/1", UriKind.Relative)).Result;
			Assert.IsNotNull(loc);
			Assert.AreEqual("Den Burg", loc.Resource.Address.City);

			var ri = ResourceIdentity.Build(testEndpoint, "Location", "1");
			loc = client.ReadAsync<Location>(ri).Result;
			Assert.IsNotNull(loc);
			Assert.AreEqual("Den Burg", loc.Resource.Address.City);
		}
#endif
        [TestMethod, TestCategory("FhirClient")]
        public void Search()
        {
            FhirClient client = new FhirClient(testEndpoint);
            Bundle result;

            result = client.Search<DiagnosticReport>();
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count() > 10, "Test should use testdata with more than 10 reports");

            result = client.Search<DiagnosticReport>(pageSize: 10);
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count <= 10);

            var withSubject = 
                result.Entries.ByResourceType<DiagnosticReport>().FirstOrDefault(dr => dr.Resource.Subject != null);
            Assert.IsNotNull(withSubject, "Test should use testdata with a report with a subject");

            ResourceIdentity ri = new ResourceIdentity(withSubject.Id);

            result = client.SearchById<DiagnosticReport>(ri.Id, 
                        includes: new string[] { "DiagnosticReport.subject" });
            Assert.IsNotNull(result);

            Assert.AreEqual(2, result.Entries.Count);  // should have subject too

            Assert.IsNotNull(result.Entries.Single(entry => new ResourceIdentity(entry.Id).Collection ==
                        typeof(DiagnosticReport).GetCollectionName()));
            Assert.IsNotNull(result.Entries.Single(entry => new ResourceIdentity(entry.Id).Collection ==
                        typeof(Patient).GetCollectionName()));

            result = client.Search<Patient>(new string[] { "name=Everywoman", "name=Eve" });

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count > 0);
        }

#if PORTABLE45
        [TestMethod, TestCategory("FhirClient")]
        public void SearchAsync()
        {
            FhirClient client = new FhirClient(testEndpoint);
            Bundle result;

            result = client.SearchAsync<DiagnosticReport>().Result;
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count() > 10, "Test should use testdata with more than 10 reports");

            result = client.SearchAsync<DiagnosticReport>(pageSize: 10).Result;
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count <= 10);

            var withSubject = 
                result.Entries.ByResourceType<DiagnosticReport>().FirstOrDefault(dr => dr.Resource.Subject != null);
            Assert.IsNotNull(withSubject, "Test should use testdata with a report with a subject");

            ResourceIdentity ri = new ResourceIdentity(withSubject.Id);

            result = client.SearchByIdAsync<DiagnosticReport>(ri.Id, 
                        includes: new string[] { "DiagnosticReport.subject" }).Result;
            Assert.IsNotNull(result);

            Assert.AreEqual(2, result.Entries.Count);  // should have subject too

            Assert.IsNotNull(result.Entries.Single(entry => new ResourceIdentity(entry.Id).Collection ==
                        typeof(DiagnosticReport).GetCollectionName()));
            Assert.IsNotNull(result.Entries.Single(entry => new ResourceIdentity(entry.Id).Collection ==
                        typeof(Patient).GetCollectionName()));

            result = client.SearchAsync<Patient>(new string[] { "name=Everywoman", "name=Eve" }).Result;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count > 0);
        }
#endif

        [TestMethod, TestCategory("FhirClient")]
        public void Paging()
        {
            FhirClient client = new FhirClient(testEndpoint);

            var result = client.Search<DiagnosticReport>(pageSize: 10);
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Entries.Count <= 10);

            var firstId = result.Entries.First().Id;

            // Browse forward
            result = client.Continue(result);
            Assert.IsNotNull(result);
            var nextId = result.Entries.First().Id;
            Assert.AreNotEqual(firstId, nextId);

            // Browse to first
            result = client.Continue(result, PageDirection.First);
{'fraction_non_alphanumeric': 0.11387833883319844, 'fraction_numerical': 0.007097939107153975, 'mean_word_length': 2.4276568501920615, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 52, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5208735', 'n_tokens_mistral': 4390, 'n_tokens_neox': 4145, 'n_words': 788}