コード例 #1
0
ファイル: CicleApiTest.cs プロジェクト: windygu/CCSys
        public void TestGetErrors()
        {
            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 get by id
            string getByIdUrl            = string.Format("{0}?id={1}", getByIdAction, 0);
            HttpResponseMessage response = client.GetAsync(getByIdUrl).Result;

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

            Assert.IsFalse(getCicleResponse.Success);
            Assert.IsTrue(getCicleResponse.ErrorCode.HasFlag(CicleError.InvalidId));
        }
コード例 #2
0
ファイル: CicleController.cs プロジェクト: windygu/CCSys
        public IHttpActionResult GetById(int id)
        {
            GetCicleResponse response = new GetCicleResponse();

            try
            {
                List <Cicle> cicles = cicleBL.GetCicle(id);
                response.Cicles  = cicles;
                response.Success = true;
            }
            catch (CicleException ex)
            {
                response.ErrorCode    = ex.Error;
                response.ErrorMessage = "Error. " + ex.Error.ToString();
                response.Success      = false;
            }
            catch (Exception ex)
            {
                response.ErrorMessage = "Error. " + ex.Message;
                response.Success      = false;
            }
            return(Ok(response));
        }
コード例 #3
0
ファイル: CiclePresenter.cs プロジェクト: windygu/CCSys
 public void UpdateCurrentCicle()
 {
     if (cicleView.SelectedId == -1)
     {
         cicleView.CurrentCicle = new Cicle();
     }
     else
     {
         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.GetAsync(string.Format("{0}?id={1}", getByIdAction, cicleView.SelectedId)).Result;
         if (response.IsSuccessStatusCode)
         {
             GetCicleResponse getCicleResponse = response.Content.ReadAsAsync <GetCicleResponse>().Result;
             if (getCicleResponse.Success)
             {
                 PropertyCopier.CopyProperties(getCicleResponse.Cicles[0], cicleView.CurrentCicle);
                 cicleView.CurrentCicle.RaiseUpdateProperties();
             }
         }
     }
 }
コード例 #4
0
ファイル: CicleApiTest.cs プロジェクト: windygu/CCSys
        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);
        }