示例#1
0
        public IHttpActionResult Update(Cicle cicle)
        {
            CicleResponse response = new CicleResponse();

            try
            {
                Cicle cicleSaved = cicleBL.UpdateCicle(cicle);
                response.Cicle   = cicleSaved;
                response.Success = true;
            }
            catch (CicleException ex)
            {
                response.ErrorCode    = ex.Error;
                response.ErrorMessage = "Error. " + ex.Error.ToString();
                response.Cicle        = null;
                response.Success      = false;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = "Error. " + ex.Message;
                response.Cicle        = null;
                response.Success      = false;
            }
            return(Ok(response));
        }
示例#2
0
        public IHttpActionResult Add(Cicle cicle)
        {
            CicleResponse response = new CicleResponse();

            try
            {
                //cicle.UserId = CurrentUserId;
                Cicle cicleSaved = cicleBL.InsertCicle(cicle);
                response.Cicle   = cicleSaved;
                response.Success = true;
            }
            catch (CicleException ex)
            {
                response.ErrorCode    = ex.Error;
                response.ErrorMessage = "Error. " + ex.Error.ToString();
                response.Cicle        = null;
                response.Success      = false;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = "Error. " + ex.Message;
                response.Cicle        = null;
                response.Success      = false;
            }
            return(Ok(response));
        }
示例#3
0
        public void TestDeleteErrors()
        {
            Token token = TokenHelper.GetToken(baseUrl, "Melvin3", "MelvinPass3");
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            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);
            //Test delete
            HttpResponseMessage response = client.PostAsJsonAsync(deleteAction, new IdModel(0)).Result;

            Assert.IsTrue(response.IsSuccessStatusCode);
            CicleResponse cicleResponse = response.Content.ReadAsAsync <CicleResponse>().Result;

            Assert.IsFalse(cicleResponse.Success);
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidId));
        }
示例#4
0
        public void SaveCicle()
        {
            bool       reLoadList = false;
            HttpClient client     = new HttpClient();

            client.BaseAddress = new Uri(baseUrl);
            client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/bson"));
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", Token.access_token);
            string action = string.Empty;

            if (cicleView.CurrentCicle.Id == 0)
            {
                //insert
                action     = insertAction;
                reLoadList = true;
            }
            else
            {
                action = updateAction;
            }
            //update
            MediaTypeFormatter  bsonFormatter = new BsonMediaTypeFormatter();
            HttpResponseMessage response      = client.PostAsync(action, cicleView.CurrentCicle, bsonFormatter).Result;

            response.EnsureSuccessStatusCode();
            MediaTypeFormatter[] formatters    = new MediaTypeFormatter[] { bsonFormatter };
            CicleResponse        cicleResponse = response.Content.ReadAsAsync <CicleResponse>(formatters).Result;

            if (cicleResponse.Success)
            {
                if (cicleResponse.Cicle != null)
                {
                    if (reLoadList)
                    {
                        LoadCicles();
                        cicleView.SelectedId = cicleResponse.Cicle.Id;
                    }
                }
            }
            else
            {
                throw new CicleException(cicleResponse.ErrorCode, cicleResponse.ErrorMessage);
            }
        }
示例#5
0
        public void TestInsertUpdateErrors()
        {
            Token token = TokenHelper.GetToken(baseUrl, "Melvin3", "MelvinPass3");
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            //Test insert
            Cicle  cicle      = new Cicle();
            string jsonTicket = javaScriptSerializer.Serialize(cicle);
            //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);
            //Test insert
            HttpResponseMessage response = client.PostAsJsonAsync(insertAction, cicle).Result;

            Assert.IsTrue(response.IsSuccessStatusCode);
            CicleResponse cicleResponse = response.Content.ReadAsAsync <CicleResponse>().Result;

            Assert.IsFalse(cicleResponse.Success);
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidAmount));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidDateZone1));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidDateZone2));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidIsClosed));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidStartDate));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidName));


            //Test update
            response = client.PostAsJsonAsync(updateAction, cicle).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            cicleResponse = response.Content.ReadAsAsync <CicleResponse>().Result;
            Assert.IsFalse(cicleResponse.Success);
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidAmount));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidDateZone1));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidDateZone2));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidIsClosed));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidStartDate));
            Assert.IsTrue(cicleResponse.ErrorCode.HasFlag(CicleError.InvalidName));
        }
示例#6
0
        public void DeleteCicle()
        {
            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(deleteAction, new IdModel(cicleView.SelectedId)).Result;

            if (response.IsSuccessStatusCode)
            {
                CicleResponse cicleResponse = response.Content.ReadAsAsync <CicleResponse>().Result;
                if (cicleResponse.Success)
                {
                    //Deleted!
                    LoadCicles();
                    PropertyCopier.CopyProperties(new Cicle(), cicleView.CurrentCicle);
                    cicleView.CurrentCicle.RaiseUpdateProperties();
                    cicleView.SelectedId = -1;
                }
            }
        }
示例#7
0
        public IHttpActionResult Delete(IdModel id)
        {
            CicleResponse response = new CicleResponse();

            try
            {
                bool success = cicleBL.DeleteCicle(id.Id);
                response.Success = success;
            }
            catch (CicleException ex)
            {
                response.ErrorCode    = ex.Error;
                response.ErrorMessage = "Error. " + ex.Error.ToString();
                response.Cicle        = null;
                response.Success      = false;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = "Error. " + ex.Message;
                response.Cicle        = null;
                response.Success      = false;
            }
            return(Ok(response));
        }
示例#8
0
        public void TestInsertUpdateAndGetCicle()
        {
            //Get token
            Token token = TokenHelper.GetToken(baseUrl, "Melvin3", "MelvinPass3");
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            //Test insert
            Cicle  cicle      = CicleHelper.CreateDummyCicle();
            string jsonTicket = javaScriptSerializer.Serialize(cicle);
            //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, cicle).Result;

            Assert.IsTrue(response.IsSuccessStatusCode);
            CicleResponse cicleResponse = response.Content.ReadAsAsync <CicleResponse>().Result;

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

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

            Assert.IsTrue(getCicleResponse.Success);
            Assert.IsTrue(getCicleResponse.Cicles.Count == 1);
            Assert.IsTrue(getCicleResponse.Cicles.ElementAt(0).Id == cicleResponse.Cicle.Id);
            //get all
            response = client.GetAsync(getAllAction).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getCicleResponse = response.Content.ReadAsAsync <GetCicleResponse>().Result;
            Assert.IsTrue(getCicleResponse.Success);
            Cicle cicleFound = (from aCicle in getCicleResponse.Cicles where aCicle.Id == cicleResponse.Cicle.Id select aCicle).FirstOrDefault();

            Assert.IsTrue(cicleFound != null);
            //test update
            cicle.Id   = cicleResponse.Cicle.Id;
            cicle.Name = "CicleNameUpdated";
            cicle.AmountPerHectarea = 2;
            response = client.PostAsJsonAsync(updateAction, cicle).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            cicleResponse = response.Content.ReadAsAsync <CicleResponse>().Result;
            Assert.IsTrue(cicleResponse.Success);
            //Get ticket again and check it was updated
            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getCicleResponse = response.Content.ReadAsAsync <GetCicleResponse>().Result;
            Assert.IsTrue(getCicleResponse.Success);
            Assert.IsTrue(getCicleResponse.Cicles.Count == 1);
            Assert.IsTrue(getCicleResponse.Cicles.ElementAt(0).AmountPerHectarea == 2);
            Assert.IsTrue(getCicleResponse.Cicles.ElementAt(0).Name == "CicleNameUpdated");
            //test delete
            response = client.PostAsJsonAsync(deleteAction, new IdModel(cicleResponse.Cicle.Id)).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            cicleResponse = response.Content.ReadAsAsync <CicleResponse>().Result;
            Assert.IsTrue(cicleResponse.Success);
            //Get ticket again and check it is not found
            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getCicleResponse = response.Content.ReadAsAsync <GetCicleResponse>().Result;
            Assert.IsTrue(getCicleResponse.Success);
            Assert.IsTrue(getCicleResponse.Cicles.Count == 0);
        }