Esempio n. 1
0
        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);
            GetProducerResponse getProducerResponse = response.Content.ReadAsAsync <GetProducerResponse>().Result;

            Assert.IsFalse(getProducerResponse.Success);
            Assert.IsTrue(getProducerResponse.ErrorCode.HasFlag(ProducerError.InvalidId));
        }
Esempio n. 2
0
 public void UpdateCurrentProducer()
 {
     try
     {
         if (producerView.SelectedId == -1)
         {
             producerView.CurrentProducer = new Producer();
         }
         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, producerView.SelectedId)).Result;
             response.EnsureSuccessStatusCode();
             GetProducerResponse getProducerResponse = response.Content.ReadAsAsync <GetProducerResponse>().Result;
             if (getProducerResponse.Success)
             {
                 PropertyCopier.CopyProperties(getProducerResponse.Producers[0], producerView.CurrentProducer);
                 producerView.CurrentProducer.RaiseUpdateProperties();
             }
             else
             {
                 throw new ProducerException(getProducerResponse.ErrorCode, getProducerResponse.ErrorMessage);
             }
         }
     }
     catch (Exception ex)
     {
         StackTrace st = new StackTrace();
         StackFrame sf = st.GetFrame(0);
         MethodBase currentMethodName = sf.GetMethod();
         Guid       errorId           = Guid.NewGuid();
         //Log error here
         producerView.HandleException(ex, currentMethodName.Name, errorId);
     }
 }
Esempio n. 3
0
        public IHttpActionResult GetById(int id)
        {
            GetProducerResponse response = new GetProducerResponse();

            try
            {
                List <Producer> producers = producerBL.GetProducer(id);
                response.Producers = producers;
                response.Success   = true;
            }
            catch (ProducerException 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));
        }
Esempio n. 4
0
        public void TestInsertUpdateAndGetProducer()
        {
            //Get token
            Token token = TokenHelper.GetToken(baseUrl, "Melvin3", "MelvinPass3");
            JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
            //Test insert
            Producer producer   = ProducerHelper.CreateDummyProducer();
            string   jsonTicket = javaScriptSerializer.Serialize(producer);
            //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, producer).Result;

            Assert.IsTrue(response.IsSuccessStatusCode);
            ProducerResponse producerResponse = response.Content.ReadAsAsync <ProducerResponse>().Result;

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

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

            Assert.IsTrue(getProducerResponse.Success);
            Assert.IsTrue(getProducerResponse.Producers.Count == 1);
            Assert.IsTrue(getProducerResponse.Producers.ElementAt(0).Id == producerResponse.Producer.Id);
            //get all
            response = client.GetAsync(getAllAction).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getProducerResponse = response.Content.ReadAsAsync <GetProducerResponse>().Result;
            Assert.IsTrue(getProducerResponse.Success);
            Producer producerFound = (from produc in getProducerResponse.Producers where produc.Id == producerResponse.Producer.Id select produc).FirstOrDefault();

            Assert.IsTrue(producerFound != null);
            //test update
            producer.Id       = producerResponse.Producer.Id;
            producer.RFC      = "UpdatedProducerRFC";
            producer.GenderId = 2;
            response          = client.PostAsJsonAsync(updateAction, producer).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            producerResponse = response.Content.ReadAsAsync <ProducerResponse>().Result;
            Assert.IsTrue(producerResponse.Success);
            //Get ticket again and check it was updated
            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getProducerResponse = response.Content.ReadAsAsync <GetProducerResponse>().Result;
            Assert.IsTrue(getProducerResponse.Success);
            Assert.IsTrue(getProducerResponse.Producers.Count == 1);
            Assert.IsTrue(getProducerResponse.Producers.ElementAt(0).GenderId == 2);
            Assert.IsTrue(getProducerResponse.Producers.ElementAt(0).RFC == "UpdatedProducerRFC");
            //test delete
            response = client.PostAsJsonAsync(deleteAction, new IdModel(producerResponse.Producer.Id)).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            producerResponse = response.Content.ReadAsAsync <ProducerResponse>().Result;
            Assert.IsTrue(producerResponse.Success);
            //Get ticket again and check it is not found
            response = client.GetAsync(getByIdUrl).Result;
            Assert.IsTrue(response.IsSuccessStatusCode);
            getProducerResponse = response.Content.ReadAsAsync <GetProducerResponse>().Result;
            Assert.IsTrue(getProducerResponse.Success);
            Assert.IsTrue(getProducerResponse.Producers.Count == 0);
        }