Ejemplo n.º 1
0
        public void TestInsertUpdateAndGetWareHouse()
        {
            //Get token
            Token token = TokenHelper.GetToken(baseUrl, "Melvin3", "MelvinPass3");
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            //Test insert
            WareHouse wareHouse  = WareHouseHelper.CreateDummyWareHouse();
            string    jsonTicket = javaScriptSerializer.Serialize(wareHouse);
            //Post add ticket
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token.access_token);
            HttpResponseMessage response = client.PostAsJsonAsync(insertAction, wareHouse).Result;

            Assert.IsTrue(response.IsSuccessStatusCode);
            WareHouseResponse wareHouseResponse = response.Content.ReadAsAsync <WareHouseResponse>().Result;

            Assert.IsTrue(wareHouseResponse.Success);
            Assert.IsTrue(wareHouseResponse != null);
            Assert.IsTrue(wareHouseResponse.WareHouse.Id > 0);
            //get by id
            string getByIdUrl = string.Format("{0}?id={1}", getByIdAction, wareHouseResponse.WareHouse.Id.ToString());

            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            GetWareHouseResponse getWareHouseResponse = response.Content.ReadAsAsync <GetWareHouseResponse>().Result;

            Assert.IsTrue(getWareHouseResponse.Success);
            Assert.IsTrue(getWareHouseResponse.WareHouses.Count == 1);
            Assert.IsTrue(getWareHouseResponse.WareHouses.ElementAt(0).Id == wareHouseResponse.WareHouse.Id);
            //get all
            response = client.GetAsync(getAllAction).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getWareHouseResponse = response.Content.ReadAsAsync <GetWareHouseResponse>().Result;
            Assert.IsTrue(getWareHouseResponse.Success);
            WareHouse wareHouseFound = (from produc in getWareHouseResponse.WareHouses where produc.Id == wareHouseResponse.WareHouse.Id select produc).FirstOrDefault();

            Assert.IsTrue(wareHouseFound != null);
            //test update
            wareHouse.Id      = wareHouseResponse.WareHouse.Id;
            wareHouse.Address = "UpdatedAddress";
            wareHouse.Name    = "UpdatedName";
            response          = client.PostAsJsonAsync(updateAction, wareHouse).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            wareHouseResponse = response.Content.ReadAsAsync <WareHouseResponse>().Result;
            Assert.IsTrue(wareHouseResponse.Success);
            //Get ticket again and check it was updated
            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getWareHouseResponse = response.Content.ReadAsAsync <GetWareHouseResponse>().Result;
            Assert.IsTrue(getWareHouseResponse.Success);
            Assert.IsTrue(getWareHouseResponse.WareHouses.Count == 1);
            Assert.IsTrue(getWareHouseResponse.WareHouses.ElementAt(0).Address == "UpdatedAddress");
            Assert.IsTrue(getWareHouseResponse.WareHouses.ElementAt(0).Name == "UpdatedName");
            //test delete
            response = client.PostAsJsonAsync(deleteAction, new IdModel(wareHouseResponse.WareHouse.Id)).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            wareHouseResponse = response.Content.ReadAsAsync <WareHouseResponse>().Result;
            Assert.IsTrue(wareHouseResponse.Success);
            //Get ticket again and check it is not found
            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getWareHouseResponse = response.Content.ReadAsAsync <GetWareHouseResponse>().Result;
            Assert.IsTrue(getWareHouseResponse.Success);
            Assert.IsTrue(getWareHouseResponse.WareHouses.Count == 0);
        }