public void WhenBasicTransactionSubmitted_AllActionsExecuted()
        {
            // Create observation so that we can do the update and create in a transaction
            var updateResource = _fhirClient.Create(_resource);

            updateResource.Comment = "This is an updated resource";

            var transaction = new TransactionBuilder(_fhirClient.Endpoint)
                              .Create(_resource)
                              .Update(updateResource.Id, updateResource);

            var bundle = transaction.ToBundle();

            bundle.Type = Bundle.BundleType.Transaction;

            BundleHelper.CleanEntryRequestUrls(bundle, _fhirClient.Endpoint);

            var transactionResult = _fhirClient.Transaction(bundle);

            Assert.NotNull(transactionResult);
            Assert.Equal(2, transactionResult.Entry.Count);

            var createResult = transactionResult.Entry[0];

            AssertHelper.CheckStatusCode(HttpStatusCode.Created, createResult.Response.Status);

            var updateResult = transactionResult.Entry[1];

            AssertHelper.CheckStatusCode(HttpStatusCode.OK, updateResult.Response.Status);

            AssertHelper.CheckStatusCode(HttpStatusCode.OK, _fhirClient.LastResult.Status);
        }
Example #2
0
        public void WhenBatchSubmittedWithInvalidItem_AllActionsExecuted()
        {
            var updateResource = (Observation)_resource.DeepCopy();

            updateResource.Id = Guid.NewGuid().ToString();

            // Per the spec, a create is handled via a post and update is handled via a put.
            // In a put "The request body SHALL be a Resource with an id element that has an identical value to the [id] in the URL. If no id element is provided, or the value is wrong, the server SHALL respond with an HTTP 400 error code..."
            var transaction = new TransactionBuilder(_fhirClient.Endpoint)
                              .Create(_resource)
                              .Update(Guid.NewGuid().ToString(), updateResource); // Pass a different id than the update resource id.

            var bundle = transaction.ToBundle();

            bundle.Type = Bundle.BundleType.Batch;

            BundleHelper.CleanEntryRequestUrls(bundle, _fhirClient.Endpoint);

            var batchResult = _fhirClient.Transaction(bundle);

            Assert.NotNull(batchResult);
            Assert.Equal(2, batchResult.Entry.Count);

            var createResult = batchResult.Entry[0];

            AssertHelper.CheckStatusCode(HttpStatusCode.Created, createResult.Response.Status);

            var updateResult = batchResult.Entry[1];

            AssertHelper.CheckStatusCode(HttpStatusCode.BadRequest, updateResult.Response.Status);

            AssertHelper.CheckStatusCode(HttpStatusCode.OK, _fhirClient.LastResult.Status);
        }
Example #3
0
        public void WhenBatchSubmitted_AllActionsExecuted()
        {
            // Create observations so that we can do the update, read, and delete in the batch
            var updateResource = _fhirClient.Create(_resource);
            var readResource   = _fhirClient.Create(_resource);
            var deleteResource = _fhirClient.Create(_resource);

            updateResource.Comment = "This is an updated resource";

            var transaction = new TransactionBuilder(_fhirClient.Endpoint)
                              .Create(_resource)
                              .Read("Observation", readResource.Id)
                              .Delete("Observation", deleteResource.Id)
                              .Update(updateResource.Id, updateResource);

            var bundle = transaction.ToBundle();

            bundle.Type = Bundle.BundleType.Batch;

            BundleHelper.CleanEntryRequestUrls(bundle, _fhirClient.Endpoint);

            var batchResult = _fhirClient.Transaction(bundle);

            Assert.NotNull(batchResult);

            Assert.NotNull(batchResult.Entry);
            Assert.Equal(4, batchResult.Entry.Count);

            var createResult = batchResult.Entry[0];

            AssertHelper.CheckStatusCode(HttpStatusCode.Created, createResult.Response.Status);

            var readResult = batchResult.Entry[1];

            AssertHelper.CheckStatusCode(HttpStatusCode.OK, readResult.Response.Status);

            var deleteResult = batchResult.Entry[2];

            AssertHelper.CheckDeleteStatusCode(deleteResult.Response.Status);

            var updateResult = batchResult.Entry[3];

            AssertHelper.CheckStatusCode(HttpStatusCode.OK, updateResult.Response.Status);

            AssertHelper.CheckStatusCode(HttpStatusCode.OK, _fhirClient.LastResult.Status);
        }
        public void WhenTransactionSubmittedWithInvalidPart_AllActionsFail()
        {
            // Create an observation so that we can do the update
            var         updateResource1 = _fhirClient.Create(_resource);
            Observation updateResource2 = new Observation();

            updateResource1.CopyTo(updateResource2);
            updateResource1.Comment = "This is an updated resource";
            updateResource2.Comment = "Other update to the same resourceId";

            // Attempting to update the same resource twice in a single transaction is supposed to fail
            // "If any resource identities (including resolved identities from conditional update/delete) overlap in steps 1-3, then the transaction SHALL fail."
            // See https://www.hl7.org/fhir/http.html#transaction for details
            var transaction = new TransactionBuilder(_fhirClient.Endpoint)
                              .Update(updateResource1.Id, updateResource1)
                              .Update(updateResource1.Id, updateResource2);
            var bundle = transaction.ToBundle();

            bundle.Type = Bundle.BundleType.Transaction;

            BundleHelper.CleanEntryRequestUrls(bundle, _fhirClient.Endpoint);

            var acceptedValues = new HashSet <string>
            {
                ((int)HttpStatusCode.InternalServerError).ToString(),
                ((int)HttpStatusCode.BadRequest).ToString(),
                "500 Internal Server Error",
                "400 Bad Request",
            };

            try
            {
                _fhirClient.Transaction(bundle);
            }
            catch (FhirOperationException)
            {
                AssertHelper.CheckSubsetStatusCode(acceptedValues, _fhirClient.LastResult.Status);
            }

            // Make sure that if the server actually returned something outside of 400 or 500, that we fail the test
            AssertHelper.CheckSubsetStatusCode(acceptedValues, _fhirClient.LastResult.Status);
        }