Exemplo n.º 1
0
func (s *ServerSuite) TestNewRecordMatchDedupRequest(c *C) {
	// Insert a record match system interface to the DB
	r := ptm_models.InsertResourceFromFile(database, "RecordMatchSystemInterface", "../fixtures/record-match-sys-if-01.json")
	recMatchSysIface := r.(*ptm_models.RecordMatchSystemInterface)
	c.Assert(*recMatchSysIface, NotNil)

	// Insert a record set to the DB
	r = ptm_models.InsertResourceFromFile(database, "RecordSet", "../fixtures/record-set-01.json")
	masterRecSet := r.(*ptm_models.RecordSet)
	c.Assert(*masterRecSet, NotNil)

	// Insert a corresponding record match run to the DB
	recMatchRun := &ptm_models.RecordMatchRun{}
	ptm_models.LoadResourceFromFile("../fixtures/record-match-run-post-01.json", recMatchRun)
	recMatchRun.MasterRecordSetID = masterRecSet.ID
	recMatchRun.RecordMatchSystemInterfaceID = recMatchSysIface.ID

	// Build a record match run
	// construct a record match request
	req, err := newRecordMatchRequest("http://localhost/fhir", recMatchRun, database)
	c.Assert(err, IsNil)
	buf, _ := req.Message.MarshalJSON()
	logger.Log.WithFields(
		logrus.Fields{"method": "TestNewRecordMatchDedupRequest",
			"request": string(buf)}).Info("")
	c.Assert(req, NotNil)
	c.Assert(req.Message.Type, Equals, "message")
	c.Assert(len(req.Message.Entry), Equals, 2) //MsgHdr + Param`
}
Exemplo n.º 2
0
// SetAnswerKey associates a specified Record Set with a FHIR Bundle that
// contains a set of expected record matches (i.e., answer key for the record set)
// The uploaded file is expected to be a FHIR Bundle  of type, document,
// in JSON representation.
func (rc *ResourceController) SetAnswerKey(ctx *gin.Context) {
	recordSetId, err := toBsonObjectID(ctx.PostForm("recordSetId"))

	// Ensure the referenced Record Set exists
	resource, err := rc.LoadResource("RecordSet", recordSetId)
	if err != nil {
		ctx.AbortWithError(http.StatusNotFound, err)
		return
	}
	recordSet := resource.(*ptm_models.RecordSet)

	// extract the answer key from the posted form
	file, _, err := ctx.Request.FormFile("answerKey")
	if err != nil {
		ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	// write uploaded content to a temp file
	tmpfile, err := ioutil.TempFile(os.TempDir(), "ptmatch-")
	defer os.Remove(tmpfile.Name())
	_, err = io.Copy(tmpfile, file)

	ptm_models.LoadResourceFromFile(tmpfile.Name(), &recordSet.AnswerKey)

	if isValidAnswerKey(recordSet.AnswerKey) {
		c := rc.Database().C(ptm_models.GetCollectionName("RecordSet"))
		err = c.Update(bson.M{"_id": recordSetId}, recordSet)
		if err != nil {
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}

		resource, err = rc.LoadResource("RecordSet", recordSetId)
		if err != nil {
			ctx.AbortWithError(http.StatusInternalServerError, err)
			return
		}
		recordSet = resource.(*ptm_models.RecordSet)

		logger.Log.WithFields(
			logrus.Fields{"updated recordset": recordSet}).Info("SetAnswerKey")

		ctx.JSON(http.StatusOK, recordSet)
	} else {
		ctx.AbortWithStatus(http.StatusBadRequest)
	}
}
Exemplo n.º 3
0
// TestNewMessageHeader tests that a MessageHeader for a record-match
// request is constructed from information in a RecordMatchRun object.
func (s *ServerSuite) TestNewMessageHeader(c *C) {
	// Insert a record match system interface to the DB
	r := ptm_models.InsertResourceFromFile(database, "RecordMatchSystemInterface", "../fixtures/record-match-sys-if-01.json")
	recMatchSysIface := r.(*ptm_models.RecordMatchSystemInterface)
	c.Assert(*recMatchSysIface, NotNil)

	recMatchRun := &ptm_models.RecordMatchRun{}
	ptm_models.LoadResourceFromFile("../fixtures/record-match-run-post-01.json", recMatchRun)
	recMatchRun.RecordMatchSystemInterfaceID = recMatchSysIface.ID

	// Build a record match run
	// construct a record match request
	src := "http://replace.me/with/selurl/global"
	msgHdr, _ := newMessageHeader(src, recMatchRun, database)
	c.Assert(msgHdr, NotNil)
	c.Assert(msgHdr.Source.Endpoint, Equals, src)
	c.Assert(msgHdr.Event.Code, Equals, "record-match")
}
Exemplo n.º 4
0
func (s *ServerSuite) TestRecordMatchRunResponse(c *C) {
	var r interface{}
	// Insert a record match run object to the DB
	//	r := ptm_models.InsertResourceFromFile(Database(), "RecordMatchRun", "../fixtures/record-match-run-01.json")
	recMatchRun := &ptm_models.RecordMatchRun{}
	ptm_models.LoadResourceFromFile("../fixtures/record-match-run-01.json", recMatchRun)
	//	recMatchRun := r.(*ptm_models.RecordMatchRun)
	c.Assert(*recMatchRun, NotNil)
	// confirm initial state - zero responses assoc. w/ run
	c.Assert(len(recMatchRun.Responses), Equals, 0)
	// Assign New Identifier to request message
	//	recMatchRun.Request.Message.Enry[0].Resource.ID = bson.NewObjectId()
	reqMsg := recMatchRun.Request.Message
	reqMsgHdr := reqMsg.Entry[0].Resource.(*fhir_models.MessageHeader)
	recMatchRun.Request.ID = bson.NewObjectId()
	reqMsg.Id = bson.NewObjectId().Hex()
	reqMsgHdr.Id = bson.NewObjectId().Hex()

	ptm_models.PersistResource(Database(), "RecordMatchRun", recMatchRun)

	logger.Log.WithFields(
		logrus.Fields{"func": "TestRecordMatchRunResponse",
			"recMatchRun": recMatchRun}).Info("after insert recMatchRun")

	respMsg := &fhir_models.Bundle{}
	// Load text of a record match ack message
	ptm_models.LoadResourceFromFile("../fixtures/record-match-ack-01.json", respMsg)
	//respMsg := r.(*fhir_models.Bundle)
	c.Assert(*respMsg, NotNil)

	// Ensure the response references the request
	//	reqMsg := recMatchRun.Request.Message
	c.Assert(reqMsg, NotNil)
	c.Assert(reqMsg.Type, Equals, "message")
	c.Assert(len(reqMsg.Entry) > 1, Equals, true)
	c.Assert(reqMsg.Entry[0].Resource, NotNil)
	//	reqMsgHdr := reqMsg.Entry[0].Resource.(*fhir_models.MessageHeader)
	respMsgHdr := respMsg.Entry[0].Resource.(*fhir_models.MessageHeader)
	respMsgHdr.Response.Identifier = reqMsgHdr.Id

	buf, _ := respMsg.MarshalJSON()
	logger.Log.WithFields(
		logrus.Fields{"func": "TestRecordMatchRunResponse",
			"resp msg": string(buf)}).Info("prep to POST")

	e := s.Server.Engine

	code, body := request("POST", "/Bundle",
		bytes.NewReader(buf), "application/json", e)
	c.Assert(code, Equals, http.StatusCreated)
	c.Assert(body, NotNil)
	logger.Log.Info("Post Record Match response: " + body)

	// Load the record match run object -- this time from database
	r, err := ptm_models.LoadResource(Database(), "RecordMatchRun", recMatchRun.ID)
	c.Assert(err, IsNil)
	c.Assert(r, NotNil)
	recMatchRun = r.(*ptm_models.RecordMatchRun)

	logger.Log.WithFields(
		logrus.Fields{"func": "TestRecordMatchRunResponse",
			"recMatchRun": recMatchRun}).Info("after recv response")

	// The response should be attached to the record match run ObjectId
	c.Assert(len(recMatchRun.Responses), Equals, 1)
	c.Assert(recMatchRun.Responses[0].ID, NotNil)
	c.Assert(recMatchRun.Responses[0].Message, NotNil)
	var respMsg1 *fhir_models.Bundle
	respMsg1 = recMatchRun.Responses[0].Message

	// After inserting into database, we've lost knowledge about type of resource in response message
	// so we use hack to decode to and then encode from json to get map to struct
	respMsgHdr1 := &fhir_models.MessageHeader{}
	mapToStruct(respMsg1.Entry[0].Resource.(bson.M), respMsgHdr1)
	c.Assert(respMsgHdr1.Response.Identifier, Equals, respMsgHdr.Response.Identifier)
}