func (s *ServiceSuite) TestAddSignificantBirthdays(c *C) {
	bd := time.Date(1950, time.March, 1, 12, 0, 0, 0, time.UTC)
	es := &plugin.EventStream{
		Patient: &models.Patient{
			BirthDate: &models.FHIRDateTime{Time: bd, Precision: models.Precision(models.Timestamp)},
		},
		Events: []plugin.Event{
			{
				Date:  time.Date(1985, time.January, 1, 12, 0, 0, 0, time.UTC),
				Type:  "Condition",
				End:   false,
				Value: new(models.Condition),
			},
			{
				Date:  time.Date(2010, time.February, 1, 12, 0, 0, 0, time.UTC),
				Type:  "MedicationStatement",
				End:   false,
				Value: new(models.MedicationStatement),
			},
		},
	}
	addSignificantBirthdayEvents(es, []int{40, 55, 65, 150})
	c.Assert(es.Events, HasLen, 5) // Note: Age 150 should not generate event since it is in future
	c.Assert(es.Events[0], DeepEquals, plugin.Event{
		Date:  time.Date(1985, time.January, 1, 12, 0, 0, 0, time.UTC),
		Type:  "Condition",
		End:   false,
		Value: new(models.Condition),
	})
	c.Assert(es.Events[1], DeepEquals, plugin.Event{
		Date:  time.Date(1990, time.March, 1, 12, 0, 0, 0, time.UTC),
		Type:  "Age",
		End:   false,
		Value: 40,
	})
	c.Assert(es.Events[2], DeepEquals, plugin.Event{
		Date:  time.Date(2005, time.March, 1, 12, 0, 0, 0, time.UTC),
		Type:  "Age",
		End:   false,
		Value: 55,
	})
	c.Assert(es.Events[3], DeepEquals, plugin.Event{
		Date:  time.Date(2010, time.February, 1, 12, 0, 0, 0, time.UTC),
		Type:  "MedicationStatement",
		End:   false,
		Value: new(models.MedicationStatement),
	})
	c.Assert(es.Events[4], DeepEquals, plugin.Event{
		Date:  time.Date(2015, time.March, 1, 12, 0, 0, 0, time.UTC),
		Type:  "Age",
		End:   false,
		Value: 65,
	})
}
func (s *ServiceSuite) TestBuildNARiskAssessmentBundle(c *C) {
	simplePlugin := assessments.NewSimplePlugin()
	bundle := buildNARiskAssessmentBundle("12345", simplePlugin.Config())

	c.Assert(bundle, NotNil)
	c.Assert(bundle.Type, Equals, "transaction")
	c.Assert(bundle.Entry, HasLen, 2)

	// First check the delete entry
	entry := bundle.Entry[0]
	c.Assert(entry.FullUrl, Equals, "")
	c.Assert(entry.Link, HasLen, 0)
	c.Assert(entry.Resource, IsNil)
	c.Assert(entry.Response, IsNil)
	c.Assert(entry.Search, IsNil)
	c.Assert(entry.Request.Method, Equals, "DELETE")
	delURL := entry.Request.Url
	c.Assert(strings.HasPrefix(delURL, "RiskAssessment?"), Equals, true)
	delValues, err := url.ParseQuery(strings.TrimPrefix(delURL, "RiskAssessment?"))
	util.CheckErr(err)
	c.Assert(delValues, HasLen, 2)
	c.Assert(delValues.Get("patient"), Equals, "12345")
	c.Assert(delValues.Get("method"), Equals, "http://interventionengine.org/risk-assessments|Simple")

	// Now check the NA post entry
	entry = bundle.Entry[1]
	c.Assert(entry.FullUrl, Equals, "")
	c.Assert(entry.Link, HasLen, 0)
	c.Assert(entry.Response, IsNil)
	c.Assert(entry.Search, IsNil)
	c.Assert(entry.Request.Method, Equals, "POST")
	c.Assert(entry.Request.Url, Equals, "RiskAssessment")
	c.Assert(entry.Resource, NotNil)
	ra, ok := entry.Resource.(*models.RiskAssessment)
	c.Assert(ok, Equals, true)
	c.Assert(ra.Id, Equals, "")
	c.Assert(ra.Basis, HasLen, 0)
	c.Assert(time.Now().Sub(ra.Date.Time) < time.Minute, Equals, true)
	c.Assert(ra.Date.Precision, Equals, models.Precision(models.Timestamp))
	c.Assert(*ra.Method, DeepEquals, simplePlugin.Config().Method)
	c.Assert(ra.Prediction, HasLen, 1)
	c.Assert(*ra.Prediction[0].Outcome, DeepEquals, simplePlugin.Config().PredictedOutcome)
	c.Assert(ra.Prediction[0].ProbabilityCodeableConcept.Coding, HasLen, 1)
	c.Assert(ra.Prediction[0].ProbabilityCodeableConcept.Coding[0].System, Equals, "http://snomed.info/sct")
	c.Assert(ra.Prediction[0].ProbabilityCodeableConcept.Coding[0].Code, Equals, "385432009")
	c.Assert(ra.Prediction[0].ProbabilityCodeableConcept.Text, Equals, "Not applicable")
	c.Assert(ra.Subject.Reference, Equals, "Patient/12345")
}
func (s *ServiceSuite) TestBuildRiskAssessmentBundle(c *C) {
	one, two, three, four := 1, 2, 3, 4
	results := []plugin.RiskServiceCalculationResult{
		{
			AsOf:  time.Date(2000, 7, 14, 15, 59, 59, 999, time.UTC),
			Score: &one,
			Pie:   plugin.NewPie(s.Server.URL + "/Patient/12345"),
		}, {
			AsOf:  time.Date(2000, 7, 14, 16, 0, 0, 0, time.UTC),
			Score: &one,
			Pie:   plugin.NewPie(s.Server.URL + "/Patient/12345"),
		}, {
			AsOf:  time.Date(2012, 1, 1, 11, 0, 0, 0, time.UTC),
			Score: &three,
			Pie:   plugin.NewPie(s.Server.URL + "/Patient/12345"),
		}, {
			AsOf:  time.Date(2013, 1, 1, 11, 0, 0, 0, time.UTC),
			Score: &four,
			Pie:   plugin.NewPie(s.Server.URL + "/Patient/12345"),
		}, {
			AsOf:  time.Date(2014, 2, 3, 10, 0, 0, 0, time.UTC),
			Score: &two,
			Pie:   plugin.NewPie(s.Server.URL + "/Patient/12345"),
		},
	}

	pieBasisURL := "http://example.org/Pie"
	simplePlugin := assessments.NewSimplePlugin()
	bundle := buildRiskAssessmentBundle("12345", results, pieBasisURL, simplePlugin.Config())

	c.Assert(bundle, NotNil)
	c.Assert(bundle.Type, Equals, "transaction")
	c.Assert(bundle.Entry, HasLen, 6)

	// First check the delete entry
	c.Assert(bundle.Entry[0].FullUrl, Equals, "")
	c.Assert(bundle.Entry[0].Link, HasLen, 0)
	c.Assert(bundle.Entry[0].Resource, IsNil)
	c.Assert(bundle.Entry[0].Response, IsNil)
	c.Assert(bundle.Entry[0].Search, IsNil)
	c.Assert(bundle.Entry[0].Request.Method, Equals, "DELETE")
	delURL := bundle.Entry[0].Request.Url
	c.Assert(strings.HasPrefix(delURL, "RiskAssessment?"), Equals, true)
	delValues, err := url.ParseQuery(strings.TrimPrefix(delURL, "RiskAssessment?"))
	util.CheckErr(err)
	c.Assert(delValues, HasLen, 2)
	c.Assert(delValues.Get("patient"), Equals, "12345")
	c.Assert(delValues.Get("method"), Equals, "http://interventionengine.org/risk-assessments|Simple")

	// Now check the post entries
	for i := 1; i < len(bundle.Entry); i++ {
		entry := bundle.Entry[i]
		result := results[i-1]
		c.Assert(entry.FullUrl, Equals, "")
		c.Assert(entry.Link, HasLen, 0)
		c.Assert(entry.Response, IsNil)
		c.Assert(entry.Search, IsNil)
		c.Assert(entry.Request.Method, Equals, "POST")
		c.Assert(entry.Request.Url, Equals, "RiskAssessment")
		c.Assert(entry.Resource, NotNil)
		ra, ok := entry.Resource.(*models.RiskAssessment)
		c.Assert(ok, Equals, true)
		c.Assert(ra.Id, Equals, "")
		c.Assert(ra.Basis, HasLen, 1)
		c.Assert(ra.Basis[0].Reference, Equals, pieBasisURL+"/"+result.Pie.Id.Hex())
		c.Assert(ra.Date.Time, DeepEquals, result.AsOf)
		c.Assert(ra.Date.Precision, Equals, models.Precision(models.Timestamp))
		c.Assert(*ra.Method, DeepEquals, simplePlugin.Config().Method)
		c.Assert(ra.Prediction, HasLen, 1)
		c.Assert(*ra.Prediction[0].Outcome, DeepEquals, simplePlugin.Config().PredictedOutcome)
		c.Assert(*ra.Prediction[0].ProbabilityDecimal, Equals, float64(*result.Score))
		c.Assert(ra.Subject.Reference, Equals, "Patient/12345")
	}
}