using System; using Xunit; using Newtonsoft.Json.Linq; using FluentAssertions; namespace raven_integration { public class UnitModelCrud { /// /// Full CRUD for a UnitModel, including concurrency violation. /// [Fact] public async Task CRUD() { var token = await Util.GetTokenAsync("BizAdmin"); // CREATE var name = Util.Uniquify("Test Unit Model"); var payload = $$""" {"id":0,"concurrency":0,"name":"{{name}}","active":true,"notes":null,"wiki":null,"customFields":"{}","tags":[],"vendorId":null,"upc":null,"lifeTimeWarranty":false,"introducedDate":null,"discontinued":false,"discontinuedDate":null,"warrantyLength":null,"warrantyTerms":null} """; ApiResponse a = await Util.PostAsync("unit-model", token, payload); Util.ValidateDataReturnResponseOk(a); long Id = a.ObjectResponse["data"]["id"].Value(); Id.Should().BeGreaterThan(0); a.ObjectResponse["data"]["name"].Value().Should().Be(name); // GET a = await Util.GetAsync($"unit-model/{Id}", token); Util.ValidateDataReturnResponseOk(a); a.ObjectResponse["data"]["name"].Value().Should().Be(name); var concurrency = a.ObjectResponse["data"]["concurrency"].Value(); // PUT (update name) var updatedName = Util.Uniquify("Updated Unit Model"); var putPayload = $$""" {"id":{{Id}},"concurrency":{{concurrency}},"name":"{{updatedName}}","active":true,"notes":null,"wiki":null,"customFields":"{}","tags":[],"vendorId":null,"upc":null,"lifeTimeWarranty":false,"introducedDate":null,"discontinued":false,"discontinuedDate":null,"warrantyLength":null,"warrantyTerms":null} """; a = await Util.PutAsync("unit-model", token, putPayload); Util.ValidateHTTPStatusCode(a, 200); var newConcurrency = a.ObjectResponse["data"]["concurrency"].Value(); newConcurrency.Should().NotBe(concurrency, "concurrency should have been bumped by the update"); // Verify the update was persisted a = await Util.GetAsync($"unit-model/{Id}", token); Util.ValidateDataReturnResponseOk(a); a.ObjectResponse["data"]["name"].Value().Should().Be(updatedName); // CONCURRENCY VIOLATION: PUT with stale concurrency should return 409 a = await Util.PutAsync("unit-model", token, putPayload); // putPayload still has old concurrency Util.ValidateConcurrencyError(a); // DELETE a = await Util.DeleteAsync($"unit-model/{Id}", token); Util.ValidateHTTPStatusCode(a, 204); // Confirm deleted a = await Util.GetAsync($"unit-model/{Id}", token); Util.ValidateResponseNotFound(a); } }//eoc public class UnitCrud { /// /// Full CRUD for a Unit, including concurrency violation. /// Uses customerId=1, the always-safe seeded customer. /// [Fact] public async Task CRUD() { var token = await Util.GetTokenAsync("BizAdmin"); // CREATE var serial = Util.Uniquify("SN").Replace(" ", ""); var payload = $$""" {"id":0,"concurrency":0,"active":true,"notes":null,"wiki":null,"customFields":"{}","tags":[],"serial":"{{serial}}","customerId":1,"parentUnitId":null,"unitModelId":null,"unitHasOwnAddress":false,"boughtHere":false,"purchasedFromVendorId":null,"receipt":null,"purchasedDate":null,"description":null,"replacedByUnitId":null,"overrideModelWarranty":false,"warrantyLength":null,"warrantyTerms":null,"contractId":null,"contractExpires":null,"metered":false,"lifeTimeWarranty":false,"text1":null,"text2":null,"text3":null,"text4":null,"address":null,"city":null,"region":null,"country":null,"addressPostal":null,"latitude":null,"longitude":null} """; ApiResponse a = await Util.PostAsync("unit", token, payload); Util.ValidateDataReturnResponseOk(a); long Id = a.ObjectResponse["data"]["id"].Value(); Id.Should().BeGreaterThan(0); a.ObjectResponse["data"]["serial"].Value().Should().Be(serial); // GET a = await Util.GetAsync($"unit/{Id}", token); Util.ValidateDataReturnResponseOk(a); a.ObjectResponse["data"]["serial"].Value().Should().Be(serial); a.ObjectResponse["data"]["customerId"].Value().Should().Be(1); var concurrency = a.ObjectResponse["data"]["concurrency"].Value(); // PUT (update description) var putPayload = $$""" {"id":{{Id}},"concurrency":{{concurrency}},"active":true,"notes":null,"wiki":null,"customFields":"{}","tags":[],"serial":"{{serial}}","customerId":1,"parentUnitId":null,"unitModelId":null,"unitHasOwnAddress":false,"boughtHere":false,"purchasedFromVendorId":null,"receipt":null,"purchasedDate":null,"description":"Updated description","replacedByUnitId":null,"overrideModelWarranty":false,"warrantyLength":null,"warrantyTerms":null,"contractId":null,"contractExpires":null,"metered":false,"lifeTimeWarranty":false,"text1":null,"text2":null,"text3":null,"text4":null,"address":null,"city":null,"region":null,"country":null,"addressPostal":null,"latitude":null,"longitude":null} """; a = await Util.PutAsync("unit", token, putPayload); Util.ValidateHTTPStatusCode(a, 200); var newConcurrency = a.ObjectResponse["data"]["concurrency"].Value(); newConcurrency.Should().NotBe(concurrency, "concurrency should have been bumped by the update"); // Verify the update was persisted a = await Util.GetAsync($"unit/{Id}", token); Util.ValidateDataReturnResponseOk(a); a.ObjectResponse["data"]["description"].Value().Should().Be("Updated description"); // CONCURRENCY VIOLATION: PUT with stale concurrency should return 409 a = await Util.PutAsync("unit", token, putPayload); // putPayload still has old concurrency Util.ValidateConcurrencyError(a); // DELETE a = await Util.DeleteAsync($"unit/{Id}", token); Util.ValidateHTTPStatusCode(a, 204); // Confirm deleted a = await Util.GetAsync($"unit/{Id}", token); Util.ValidateResponseNotFound(a); } }//eoc }//eons