public async Task TestCase1() { string token = await IdentityServerSetup.Instance.GetAccessTokenForUser(DataSetupUtility.UserA, DataSetupUtility.IntegrationTestPassword); var clientWithAuth = _factory.CreateClient(); clientWithAuth.SetBearerToken(token); // Step 1. Metadata request var metadata = await this._client.GetAsync("/api/$metadata"); Assert.Equal(HttpStatusCode.OK, metadata.StatusCode); var content = await metadata.Content.ReadAsStringAsync(); if (content.Length > 0) { // How to verify metadata? // TBD. } // Step 2. Read Home Defines - Non authority case var req1 = await this._client.GetAsync("/api/HomeDefines"); Assert.Equal(HttpStatusCode.Unauthorized, req1.StatusCode); // Step 3. Read Home Defines - Authority case var resp2 = await clientWithAuth.GetAsync("/api/HomeDefines"); Assert.True(resp2.IsSuccessStatusCode); string result = resp2.Content.ReadAsStringAsync().Result; if (!String.IsNullOrEmpty(result)) { JToken outer = JToken.Parse(result); // Old way to deserialize the arry JArray inner = outer["value"].Value <JArray>(); var dfs = inner.ToObject <List <HomeDefine> >(); Assert.Equal(2, dfs.Count); // For user A, Home1 Is a must var bHome1Exist = false; foreach (var df in dfs) { Assert.NotNull(df); Assert.True(df.ID > 0); Assert.False(String.IsNullOrEmpty(df.Name)); Assert.NotNull(df.HomeMembers); if (df.ID == DataSetupUtility.Home1ID) { bHome1Exist = true; } } Assert.True(bHome1Exist); } // Step 4. Read home defines - with home members resp2 = await clientWithAuth.GetAsync("/api/HomeDefines?$expand=HomeMembers"); Assert.True(resp2.IsSuccessStatusCode); result = resp2.Content.ReadAsStringAsync().Result; if (!String.IsNullOrEmpty(result)) { JToken outer = JToken.Parse(result); JArray inner = outer["value"].Value <JArray>(); var dfs = inner.ToObject <List <HomeDefine> >(); Assert.Equal(2, dfs.Count); // For user A, Home1 Is a must foreach (var df in dfs) { Assert.NotNull(df); Assert.True(df.ID > 0); Assert.False(String.IsNullOrEmpty(df.Name)); Assert.NotNull(df.HomeMembers); var exist = df.HomeMembers.Single(p => p.User == DataSetupUtility.UserA); Assert.NotNull(exist); } } var hid = DataSetupUtility.Home1ID; var cc1id = 0; var ord1id = 0; var acnt1id = 0; var doc1id = 0; var jsetting = new JsonSerializerSettings(); jsetting.Converters.Add(new StringEnumConverter()); jsetting.ReferenceLoopHandling = ReferenceLoopHandling.Ignore; jsetting.DateFormatString = "yyyy-MM-dd"; // Step 5. Create a control center var cc = new FinanceControlCenter() { HomeID = hid, Name = "Control Center 1", Comment = "Comment 1", Owner = DataSetupUtility.UserA }; var kjson = JsonConvert.SerializeObject(cc, jsetting); var inputContent = new StringContent(kjson, Encoding.UTF8, "application/json"); resp2 = await clientWithAuth.PostAsync("/api/FinanceControlCenters", inputContent); Assert.True(resp2.IsSuccessStatusCode); result = resp2.Content.ReadAsStringAsync().Result; if (!String.IsNullOrEmpty(result)) { var odatarst = JsonConvert.DeserializeObject <FinanceControlCenter>(result); Assert.Equal(odatarst.Name, cc.Name); Assert.Equal(odatarst.HomeID, cc.HomeID); Assert.Equal(odatarst.Owner, cc.Owner); cc1id = odatarst.ID; Assert.True(cc1id > 0); } // Step 6. Create an order var ord = new FinanceOrder() { HomeID = hid, Name = "Order 1", Comment = "Comment 1" }; var srule = new FinanceOrderSRule() { Order = ord, RuleID = 1, ControlCenterID = cc1id, Precent = 100 }; ord.SRule.Add(srule); kjson = JsonConvert.SerializeObject(ord, jsetting); inputContent = new StringContent(kjson, Encoding.UTF8, "application/json"); resp2 = await clientWithAuth.PostAsync("/api/FinanceOrders", inputContent); Assert.True(resp2.IsSuccessStatusCode); result = resp2.Content.ReadAsStringAsync().Result; if (!String.IsNullOrEmpty(result)) { var odatarst = JsonConvert.DeserializeObject <FinanceOrder>(result); Assert.Equal(odatarst.Name, ord.Name); ord1id = odatarst.ID; Assert.True(ord1id > 0); } // Step 7. Create an account var acnt = new FinanceAccount() { HomeID = DataSetupUtility.Home1ID, Name = "Account 1", CategoryID = FinanceAccountCategory.AccountCategory_Cash, Owner = DataSetupUtility.UserA }; kjson = JsonConvert.SerializeObject(acnt, jsetting); inputContent = new StringContent(kjson, Encoding.UTF8, "application/json"); resp2 = await clientWithAuth.PostAsync("/api/FinanceAccounts", inputContent); Assert.True(resp2.IsSuccessStatusCode); result = resp2.Content.ReadAsStringAsync().Result; if (!String.IsNullOrEmpty(result)) { var odatarst = JsonConvert.DeserializeObject <FinanceAccount>(result); Assert.Equal(odatarst.Name, acnt.Name); acnt1id = odatarst.ID; Assert.True(acnt1id > 0); } // Step 7a. Get all accounts resp2 = await clientWithAuth.GetAsync("/api/FinanceAccounts?hid=" + hid.ToString()); Assert.True(resp2.IsSuccessStatusCode); result = resp2.Content.ReadAsStringAsync().Result; if (!String.IsNullOrEmpty(result)) { //var odatarst = JsonConvert.DeserializeObject<FinanceAccount>(result); //Assert.Equal(odatarst.Name, acnt.Name); //acnt1id = odatarst.ID; //Assert.True(acnt1id > 0); } // Step 7b. Read one specified account resp2 = await clientWithAuth.GetAsync("/api/FinanceAccounts(" + acnt1id.ToString() + ")"); // ?hid=" + hid.ToString()); Assert.True(resp2.IsSuccessStatusCode); result = resp2.Content.ReadAsStringAsync().Result; if (!String.IsNullOrEmpty(result)) { } // Step 8. Post a document var doc = new FinanceDocument() { DocType = FinanceDocumentType.DocType_Normal, HomeID = hid, TranDate = DateTime.Today, Desp = "First document", TranCurr = DataSetupUtility.Home1BaseCurrency, }; var item = new FinanceDocumentItem() { DocumentHeader = doc, ItemID = 1, Desp = "Item 1.1", TranType = 2, // Wage TranAmount = 10, AccountID = acnt1id, ControlCenterID = cc1id, }; doc.Items.Add(item); jsetting.NullValueHandling = NullValueHandling.Ignore; kjson = JsonConvert.SerializeObject(doc, jsetting); inputContent = new StringContent(kjson, Encoding.UTF8, "application/json"); resp2 = await clientWithAuth.PostAsync("/api/FinanceDocuments", inputContent); Assert.True(resp2.IsSuccessStatusCode); result = resp2.Content.ReadAsStringAsync().Result; if (!String.IsNullOrEmpty(result)) { var odatarst = JsonConvert.DeserializeObject <FinanceDocument>(result); Assert.Equal(odatarst.Desp, doc.Desp); doc1id = odatarst.ID; Assert.True(doc1id > 0); } // Step 9. Create an ADP document //var adpcontext = new FinanceADPDocumentCreateContext(); //adpcontext.DocumentInfo = new FinanceDocument(); //adpcontext.AccountInfo = new FinanceAccount(); }
public async Task TestCase1(int hid, string user) { var context = this.fixture.GetCurrentDataContext(); if (hid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } if (hid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(context); } if (hid == DataSetupUtility.Home3ID) { fixture.InitHome3TestData(context); } if (hid == DataSetupUtility.Home4ID) { fixture.InitHome4TestData(context); } if (hid == DataSetupUtility.Home5ID) { fixture.InitHome5TestData(context); } // 1. Prepare dta var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); var listCCs = context.FinanceControlCenter.Where(p => p.HomeID == hid).ToList <FinanceControlCenter>(); var curhmemquery = (from homemem in context.HomeMembers where homemem.HomeID == hid && homemem.User == user select homemem).FirstOrDefault(); var curhmem = Assert.IsType <HomeMember>(curhmemquery); var existamt = (from homemem in context.HomeMembers join finord in context.FinanceOrder on new { homemem.HomeID, homemem.User } equals new { finord.HomeID, User = user } select finord.ID).ToList().Count(); var existamt_curhome = context.FinanceOrder.Where(p => p.HomeID == hid).Count(); // 2. Create order var control = new FinanceOrdersController(context); control.ControllerContext = new ControllerContext() { HttpContext = httpctx }; var ord = new FinanceOrder() { HomeID = hid, Name = "Order 1", Comment = "Comment 1" }; var srule = new FinanceOrderSRule() { Order = ord, RuleID = 1, ControlCenterID = listCCs[0].ID, Precent = 100 }; ord.SRule.Add(srule); var rst = await control.Post(ord); Assert.NotNull(rst); var rst2 = Assert.IsType <CreatedODataResult <FinanceOrder> >(rst); Assert.Equal(rst2.Entity.Name, ord.Name); var oid = rst2.Entity.ID; Assert.True(oid > 0); ordersCreated.Add(oid); // 3. Read the order out (without Home ID) var queryUrl = "http://localhost/api/FinanceOrders"; var req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); var odatacontext = UnitTestUtility.GetODataQueryContext <FinanceOrder>(this.model); var options = UnitTestUtility.GetODataQueryOptions <FinanceOrder>(odatacontext, req); var rst3 = control.Get(options); Assert.NotNull(rst3); if (curhmem.IsChild.HasValue && curhmem.IsChild == true) { existamt = context.FinanceOrder.Where(p => p.HomeID == hid).Count(); Assert.Equal(existamt + 1, rst3.Cast <FinanceOrder>().Count()); } else { Assert.Equal(existamt + 1, rst3.Cast <FinanceOrder>().Count()); } // 3a. Read the order out (with Home ID) queryUrl = "http://localhost/api/FinanceOrders?$filter=HomeID eq " + hid.ToString(); req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); //var odatacontext = UnitTestUtility.GetODataQueryContext<FinanceOrder>(this.model); options = UnitTestUtility.GetODataQueryOptions <FinanceOrder>(odatacontext, req); rst3 = control.Get(options); Assert.NotNull(rst3); Assert.Equal(existamt_curhome + 1, rst3.Cast <FinanceOrder>().Count()); // 4a. Change one order - Add new rule var norder = rst2.Entity; norder.Name = "New Order"; norder.SRule.ElementAt(0).Precent = 80; norder.SRule.Add(new FinanceOrderSRule { Order = norder, RuleID = 2, ControlCenterID = listCCs.Count > 1 ? listCCs[1].ID : listCCs[0].ID, Precent = 20 }); rst = await control.Put(norder.ID, norder); Assert.NotNull(rst); var rst4 = Assert.IsType <UpdatedODataResult <FinanceOrder> >(rst); Assert.Equal(norder.Name, rst4.Entity.Name); // Re-read the order //queryUrl = "http://localhost/api/FinanceOrders?$filter=HomeID eq " + hid.ToString() + " and ID eq " + norder.ID.ToString() + "&$expand=SRule"; //req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); //options = UnitTestUtility.GetODataQueryOptions<FinanceOrder>(odatacontext, req); //rst3 = control.Get(options); //var rst3a = rst3.Cast<FinanceOrder>(); //Assert.True(rst3a.Count() == 1); //var rst3a_elem = rst3a.ElementAt(0); //Assert.True(rst3a_elem.SRule.Count == 2); ////var rst3a = Assert.IsType<FinanceOrder>(rst3); ////Assert.True(rst3a.SRule.Count == 2); // 5. Delete an order var rst5 = await control.Delete(oid); Assert.NotNull(rst4); var rst6 = Assert.IsType <StatusCodeResult>(rst5); Assert.Equal(204, rst6.StatusCode); Assert.Equal(0, context.FinanceOrderSRule.Where(p => p.OrderID == oid).Count()); ordersCreated.Clear(); // 6. Read the order again queryUrl = "http://localhost/api/FinanceOrders?$filter=HomeID eq " + hid.ToString(); req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); options = UnitTestUtility.GetODataQueryOptions <FinanceOrder>(odatacontext, req); rst3 = control.Get(options); Assert.NotNull(rst3); Assert.Equal(existamt_curhome, rst3.Cast <FinanceOrder>().Count()); await context.DisposeAsync(); }