public async Task Create_A_Customer() { using (var client = new APIClientProvider().Client) { /* * ARRANGE */ Customer newCustomer = new Customer() { FirstName = "Will", LastName = "Wilkinson", CreationDate = new DateTime(2019, 11, 02), LastActiveDate = new DateTime(2019, 11, 06) }; var customerAsJSON = JsonConvert.SerializeObject(newCustomer); /* * ACT */ var response = await client.PostAsync( "/api/customers", new StringContent(customerAsJSON, Encoding.UTF8, "application/json")); string responseBody = await response.Content.ReadAsStringAsync(); var customer = JsonConvert.DeserializeObject <Customer>(responseBody); /* * ASSERT */ Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.True(customer.FirstName == "Will"); } }
public async Task Test_Delete_Computer() { var date = new DateTime(2019, 01, 01); using (var client = new APIClientProvider().Client) { /* * ARRANGE */ Computer Something = new Computer { Id = -1, PurchaseDate = date, DecomissionDate = date, Make = "Something", Manufacturer = "Something" }; // Serialize the C# object into a JSON string var somethingAsJSON = JsonConvert.SerializeObject(Something); /* * ACT */ //Insert object var response = await client.PostAsync( "/api/PaymentType", new StringContent(somethingAsJSON, Encoding.UTF8, "application/json") ); string responseBody = await response.Content.ReadAsStringAsync(); var newType = JsonConvert.DeserializeObject <Computer>(responseBody); //Get object var getSomething = await client.GetAsync("/api/computer/-1" + ""); getSomething.EnsureSuccessStatusCode(); string getSomethingBody = await getSomething.Content.ReadAsStringAsync(); Computer newSomething = JsonConvert.DeserializeObject <Computer>(getSomethingBody); //Delete Object var deleteSomething = await client.DeleteAsync("/api/computer/-1" + ""); getSomething.EnsureSuccessStatusCode(); //Try to Get Object Again var attemptGetSomething = await client.GetAsync("/api/computer/-1" + ""); attemptGetSomething.EnsureSuccessStatusCode(); string attemptGetSomethingBody = await getSomething.Content.ReadAsStringAsync(); Computer newAttemptSomething = JsonConvert.DeserializeObject <Computer>(attemptGetSomethingBody); /* * ASSERT */ Assert.Equal(HttpStatusCode.NoContent, attemptGetSomething.StatusCode); } }
public async Task Test_Delete_PaymentType() { using (var client = new APIClientProvider().Client) { /* * ARRANGE */ PaymentType Venmo = new PaymentType { Id = -1, Name = "Venmo" }; // Serialize the C# object into a JSON string var venmoAsJSON = JsonConvert.SerializeObject(Venmo); /* * ACT */ //Insert object var response = await client.PostAsync( "/api/PaymentType", new StringContent(venmoAsJSON, Encoding.UTF8, "application/json") ); string responseBody = await response.Content.ReadAsStringAsync(); var newType = JsonConvert.DeserializeObject <PaymentType>(responseBody); //Get object var getVenmo = await client.GetAsync("/api/paymenttype/-1" + ""); getVenmo.EnsureSuccessStatusCode(); string getVenmoBody = await getVenmo.Content.ReadAsStringAsync(); PaymentType newVenmo = JsonConvert.DeserializeObject <PaymentType>(getVenmoBody); //Delete Object var deleteVenmo = await client.DeleteAsync("/api/paymenttype/-1" + ""); getVenmo.EnsureSuccessStatusCode(); //Try to Get Object Again var attemptGetVenmo = await client.GetAsync("/api/paymenttype/-1" + ""); attemptGetVenmo.EnsureSuccessStatusCode(); string attemptGetVenmoBody = await getVenmo.Content.ReadAsStringAsync(); PaymentType newAttemptVenmo = JsonConvert.DeserializeObject <PaymentType>(attemptGetVenmoBody); /* * ASSERT */ Assert.Equal(HttpStatusCode.NoContent, attemptGetVenmo.StatusCode); } }
public async Task Test_Delete_ProductType() { using (var client = new APIClientProvider().Client) { /* * ARRANGE */ ProductType toys = new ProductType { Name = "Toys" }; // Serialize the C# object into a JSON string var toysAsJSON = JsonConvert.SerializeObject(toys); /* * ACT */ //Insert object var response = await client.PostAsync( "/api/ProductType", new StringContent(toysAsJSON, Encoding.UTF8, "application/json") ); string responseBody = await response.Content.ReadAsStringAsync(); var newToys = JsonConvert.DeserializeObject <ProductType>(responseBody); //Get object var getFood = await client.GetAsync($"/api/productType/{newToys.Id}" + ""); getFood.EnsureSuccessStatusCode(); string getFoodBody = await getFood.Content.ReadAsStringAsync(); ProductType newFood = JsonConvert.DeserializeObject <ProductType>(getFoodBody); //Delete Object var deleteFood = await client.DeleteAsync($"/api/productType/{newToys.Id}" + ""); getFood.EnsureSuccessStatusCode(); //Try to Get Object Again var attemptGetFood = await client.GetAsync($"/api/productType/{newToys.Id}" + ""); attemptGetFood.EnsureSuccessStatusCode(); string attemptGetFoodBody = await getFood.Content.ReadAsStringAsync(); ProductType newAttemptFood = JsonConvert.DeserializeObject <ProductType>(attemptGetFoodBody); /* * ASSERT */ Assert.Equal(HttpStatusCode.NoContent, attemptGetFood.StatusCode); } }
public async Task Test_Create_Modify_And_Delete_Product() { using (var client = new APIClientProvider().Client) { //POST section Product test = new Product { Title = "test", Description = "testestst", Price = 1431434, Quantity = 2, ProductTypeId = 1, CustomerId = 1 }; var testAsJSON = JsonConvert.SerializeObject(test); var response = await client.PostAsync( "/api/products", new StringContent(testAsJSON, Encoding.UTF8, "application/json") ); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var newTest = JsonConvert.DeserializeObject <Product>(responseBody); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("test", newTest.Title); Assert.Equal("testestst", newTest.Description); Assert.Equal(1431434, newTest.Price); ////////////////////////////// /// string newTitle = "NEWTESTTITLE"; /* * PUT section */ Product modifiedProduct = new Product { Title = "NEWTESTTITLE", Description = "testestst", Price = 1431434, Quantity = 2, ProductTypeId = 1, CustomerId = 1 }; var modifiedProductAsJSON = JsonConvert.SerializeObject(modifiedProduct); var Modifyresponse = await client.PutAsync( $"/api/products/{newTest.Id}", new StringContent(modifiedProductAsJSON, Encoding.UTF8, "application/json") ); response.EnsureSuccessStatusCode(); string ModifyresponseBody = await Modifyresponse.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.NoContent, Modifyresponse.StatusCode); /* * GET section */ var getProduct = await client.GetAsync($"/api/products/{newTest.Id}"); getProduct.EnsureSuccessStatusCode(); string getProductBody = await getProduct.Content.ReadAsStringAsync(); Product newProduct = JsonConvert.DeserializeObject <Product>(getProductBody); Assert.Equal(HttpStatusCode.OK, getProduct.StatusCode); Assert.Equal(newTitle, newProduct.Title); //DELETE TEST var deleteResponse = await client.DeleteAsync($"/api/products/{newProduct.Id}"); deleteResponse.EnsureSuccessStatusCode(); Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode); } }
public async Task Test_Create_And_Modify_And_Delete_Payment_Type() { using (var client = new APIClientProvider().Client) { //POST PaymentType testPaymentType = new PaymentType { AcctNumber = 1111, Name = "Test Payment Name", CustomerId = 1 }; var testPaymentTypeAsJSON = JsonConvert.SerializeObject(testPaymentType); var response = await client.PostAsync( "api/PaymentTypes", new StringContent(testPaymentTypeAsJSON, Encoding.UTF8, "application/json") ); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var newTestPaymentType = JsonConvert.DeserializeObject <PaymentType>(responseBody); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal(1111, newTestPaymentType.AcctNumber); Assert.Equal("Test Payment Name", newTestPaymentType.Name); Assert.Equal(1, newTestPaymentType.CustomerId); //PUT int newAcctNumber = 1234567; PaymentType modifiedPaymentType = new PaymentType { AcctNumber = newAcctNumber, Name = "Johns BigBank", CustomerId = 1 }; var modifiedPaymentTypeJSON = JsonConvert.SerializeObject(modifiedPaymentType); var modifiedResponse = await client.PutAsync( $"api/PaymentTypes/{newTestPaymentType.Id}", new StringContent(modifiedPaymentTypeJSON, Encoding.UTF8, "application/json") ); modifiedResponse.EnsureSuccessStatusCode(); string modifiedResponseBody = await modifiedResponse.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.NoContent, modifiedResponse.StatusCode); var getPaymentType = await client.GetAsync($"api/PaymentTypes/{newTestPaymentType.Id}"); getPaymentType.EnsureSuccessStatusCode(); string getPaymentTypeBody = await getPaymentType.Content.ReadAsStringAsync(); PaymentType newPaymentType = JsonConvert.DeserializeObject <PaymentType>(getPaymentTypeBody); Assert.Equal(HttpStatusCode.OK, getPaymentType.StatusCode); Assert.Equal(newAcctNumber, newPaymentType.AcctNumber); //DELETE var deleteResponse = await client.DeleteAsync($"api/PaymentTypes/{newTestPaymentType.Id}"); deleteResponse.EnsureSuccessStatusCode(); Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode); } }
public async Task Test_Create_Modify_And_Delete_Computer() { using (var client = new APIClientProvider().Client) { DateTime purchasedate = DateTime.Now; DateTime decomissiondate = DateTime.Now; Computer macbookSuperPro = new Computer { Make = "MacBook Super Pro", Manufacturer = "Apple", PurchaseDate = purchasedate, DecomissionDate = decomissiondate }; var macbookSuperProAsJSON = JsonConvert.SerializeObject(macbookSuperPro); var response = await client.PostAsync( "/computers", new StringContent(macbookSuperProAsJSON, Encoding.UTF8, "application/json") ); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var newmacbookSuperPro = JsonConvert.DeserializeObject <Computer>(responseBody); string newMake = "test number two"; /* * PUT section */ Computer modifiedComputer = new Computer { Make = newMake, Manufacturer = "test", PurchaseDate = purchasedate, DecomissionDate = decomissiondate }; var modifiedComputerAsJSON = JsonConvert.SerializeObject(modifiedComputer); int modifiedNewComputerId = newmacbookSuperPro.Id; var Modifyresponse = await client.PutAsync( $"/computers/{modifiedNewComputerId}", new StringContent(modifiedComputerAsJSON, Encoding.UTF8, "application/json") ); Modifyresponse.EnsureSuccessStatusCode(); string ModifyresponseBody = await Modifyresponse.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.NoContent, Modifyresponse.StatusCode); /* * GET section */ var getComputer = await client.GetAsync($"/computers/{modifiedNewComputerId}"); getComputer.EnsureSuccessStatusCode(); string getComputerBody = await getComputer.Content.ReadAsStringAsync(); Computer newComputer = JsonConvert.DeserializeObject <Computer>(getComputerBody); Assert.Equal(HttpStatusCode.OK, getComputer.StatusCode); Assert.Equal(newMake, newComputer.Make); //DELETE TEST var deleteResponse = await client.DeleteAsync($"/computers/{modifiedNewComputerId}"); deleteResponse.EnsureSuccessStatusCode(); Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode); } }
public async Task Test_Create_Modify_department() { using (var client = new APIClientProvider().Client) { //POST section Department test = new Department { Name = "test", Budget = 321 }; var testAsJSON = JsonConvert.SerializeObject(test); var response = await client.PostAsync( "/api/departments", new StringContent(testAsJSON, Encoding.UTF8, "application/json") ); response.EnsureSuccessStatusCode(); string responseBody = await response.Content.ReadAsStringAsync(); var newTest = JsonConvert.DeserializeObject <Department>(responseBody); Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("test", newTest.Name); Assert.Equal(321, newTest.Budget); ////////////////////////////// /// string newName = "testNEW"; /* * PUT section */ Department newNameTest = new Department { Name = "testNEW", Budget = 321 }; var modifiedDepartmentAsJSON = JsonConvert.SerializeObject(newNameTest); var Modifyresponse = await client.PutAsync( $"/api/departments/{newTest.Id}", new StringContent(modifiedDepartmentAsJSON, Encoding.UTF8, "application/json") ); response.EnsureSuccessStatusCode(); string ModifyresponseBody = await Modifyresponse.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.NoContent, Modifyresponse.StatusCode); /* * GET section */ var getDepartment = await client.GetAsync($"/api/departments/{newTest.Id}"); getDepartment.EnsureSuccessStatusCode(); string getDepartmentBody = await getDepartment.Content.ReadAsStringAsync(); Department newDepartment = JsonConvert.DeserializeObject <Department>(getDepartmentBody); Assert.Equal(HttpStatusCode.OK, getDepartment.StatusCode); Assert.Equal(newName, newDepartment.Name); //DELETE TEST // var deleteResponse = await client.DeleteAsync($"/api/departments/{newTest.Id}"); // deleteResponse.EnsureSuccessStatusCode(); // Assert.Equal(HttpStatusCode.NoContent, deleteResponse.StatusCode); } }
//========================= //Test Create Customer //========================== public async Task Test_Create_Customer() { /* * Generate a new instance of an HttpClient that you can * use to generate HTTP requests to your API controllers. * The `using` keyword will automatically dispose of this * instance of HttpClient once your code is done executing. */ try { using (var client = new APIClientProvider().Client) { /* * ARRANGE */ // Construct a new student object to be sent to the API Customer Jack = new Customer { FirstName = "Jack", LastName = "Taylor", CreationDate = new DateTime(), LastActiveDate = new DateTime() }; // Serialize the C# object into a JSON string var jackAsJSON = JsonConvert.SerializeObject(Jack); /* * ACT */ // Use the client to send the request and store the response var response = await client.PostAsync( "/api/customers", new StringContent(jackAsJSON, Encoding.UTF8, "application/json") ); // Store the JSON body of the response string responseBody = await response.Content.ReadAsStringAsync(); // Deserialize the JSON into an instance of Animal var newJack = JsonConvert.DeserializeObject <Customer>(responseBody); /* * ASSERT */ Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("Jack", newJack.FirstName); Assert.Equal("Taylor", newJack.LastName); Assert.Equal(new DateTime(2010, 8, 18), newJack.CreationDate); Assert.Equal(new DateTime(2012, 8, 18), newJack.LastActiveDate); } } catch (Exception e) { } }
public async Task Test_Post_Employee() { /* * Generate a new instance of an HttpClient that you can * use to generate HTTP requests to your API controllers. * The `using` keyword will automatically dispose of this * instance of HttpClient once your code is done executing. */ using (var client = new APIClientProvider().Client) { /* * ARRANGE */ // Construct a new student object to be sent to the API var start = new DateTime(2019, 01, 01); var end = new DateTime(2019, 01, 01); Employee joey = new Employee { FirstName = "Joey", LastName = "Smith", DepartmentId = 2, IsSuperVisor = true, StartDate = start, EndDate = end }; // Serialize the C# object into a JSON string var joeyAsJSON = JsonConvert.SerializeObject(joey); /* * ACT */ // Use the client to send the request and store the response var response = await client.PostAsync( "/api/employee", new StringContent(joeyAsJSON, Encoding.UTF8, "application/json") ); // Store the JSON body of the response string responseBody = await response.Content.ReadAsStringAsync(); // Deserialize the JSON into an instance of Animal var newJoey = JsonConvert.DeserializeObject <Employee>(responseBody); /* * ASSERT */ Assert.Equal(HttpStatusCode.Created, response.StatusCode); Assert.Equal("Joey", newJoey.FirstName); Assert.Equal("Smith", newJoey.LastName); Assert.Equal(2, newJoey.DepartmentId); Assert.True(newJoey.IsSuperVisor); Assert.Equal(start, newJoey.StartDate); Assert.Equal(end, newJoey.EndDate); } }