Exemplo n.º 1
0
        public async Task SingletonQueryOptionsTest(string model, string singletonName)
        {
            string requestUri = string.Format(this.BaseAddress + "/{0}/{1}", model, singletonName);

            await ResetDataSource(model, singletonName);

            // GET /singleton?$select=Name
            var response = this.Client.GetAsync(requestUri + "?$select=Name").Result;
            var result   = response.Content.ReadAsAsync <JObject>().Result;
            int i        = 0;

            foreach (var pro in result.Properties())
            {
                i++;
            }
            Assert.Equal(2, i);

            // POST /singleton/Partners
            Partner partner = new Partner()
            {
                ID = 100, Name = "NewHire"
            };
            var request = new HttpRequestMessage(HttpMethod.Post, requestUri + "/Partners");

            request.Content = new ObjectContent(partner.GetType(), partner, new JsonMediaTypeFormatter());
            response        = Client.SendAsync(request).Result;
            response.EnsureSuccessStatusCode();

            // POST /singleton/Partners
            partner = new Partner()
            {
                ID = 101, Name = "NewHire2"
            };
            request         = new HttpRequestMessage(HttpMethod.Post, requestUri + "/Partners");
            request.Content = new ObjectContent(partner.GetType(), partner, new JsonMediaTypeFormatter());
            response        = Client.SendAsync(request).Result;
            response.EnsureSuccessStatusCode();

            // GET /singleton?$expand=Partners($select=Name)
            response = this.Client.GetAsync(requestUri + "?$expand=Partners($select=Name)").Result;
            result   = await response.Content.ReadAsAsync <JObject>();

            var json = result.GetValue("Partners") as JArray;

            Assert.Equal(2, json.Count);

            // PUT Partners(1)/Company/$ref
            var    navigationUri = string.Format(this.BaseAddress + "/{0}/Partners(1)/Company/$ref", model);
            string idLinkBase    = string.Format(this.BaseAddress + "/{0}/{1}", model, singletonName);

            request         = new HttpRequestMessage(HttpMethod.Put, navigationUri);
            request.Content = new StringContent("{ \"@odata.id\" : \"" + idLinkBase + "\"}");
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;

            // GET /Partners(1)?$expand=Company($select=Name)
            requestUri = string.Format(this.BaseAddress + "/{0}/Partners(1)", model);
            response   = this.Client.GetAsync(requestUri + "?$expand=Company($select=Name)").Result;
            result     = await response.Content.ReadAsAsync <JObject>();

            var company = result.GetValue("Company") as JObject;

            Assert.Equal(singletonName, company.GetValue("Name"));
        }
Exemplo n.º 2
0
        public async Task SingletonNavigationLinkCRUD(string model, string singletonName)
        {
            string requestUri = string.Format(this.BaseAddress + "/{0}/{1}/Partners", model, singletonName);

            // Reset data source
            await ResetDataSource(model, singletonName);
            await ResetDataSource(model, "Partners");

            // GET singleton/Partners
            HttpResponseMessage response = this.Client.GetAsync(requestUri).Result;
            var json = await response.Content.ReadAsAsync <JObject>();

            var result = json.GetValue("value") as JArray;

            Assert.Equal <int>(0, result.Count);

            string navigationLinkUri = string.Format(requestUri + "/$ref");

            // POST singleton/Partners/$ref
            string             idLinkBase = string.Format(this.BaseAddress + "/{0}/Partners", model);
            HttpRequestMessage request    = new HttpRequestMessage(HttpMethod.Post, navigationLinkUri);

            request.Content = new StringContent("{ \"@odata.id\" : \"" + idLinkBase + "(1)\"}");
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // POST singleton/Partners/$ref
            request         = new HttpRequestMessage(HttpMethod.Post, navigationLinkUri);
            request.Content = new StringContent("{ \"@odata.id\" : \"" + idLinkBase + "(2)\"}");
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // POST singleton/Partners/$ref
            request         = new HttpRequestMessage(HttpMethod.Post, navigationLinkUri);
            request.Content = new StringContent("{ \"@odata.id\" : \"" + idLinkBase + "(3)\"}");
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // GET singleton/Partners
            response = this.Client.GetAsync(requestUri).Result;
            json     = await response.Content.ReadAsAsync <JObject>();

            result = json.GetValue("value") as JArray;
            Assert.Equal <int>(3, result.Count);

            // Add Partner to Company by "Deep Insert"
            // POST singleton/Partners
            Partner partner = new Partner()
            {
                ID = 100, Name = "NewHire"
            };

            request         = new HttpRequestMessage(HttpMethod.Post, requestUri);
            request.Content = new ObjectContent(partner.GetType(), partner, new JsonMediaTypeFormatter());
            response        = Client.SendAsync(request).Result;
            response.EnsureSuccessStatusCode();

            // GET singleton/Partners
            response = this.Client.GetAsync(requestUri).Result;
            json     = await response.Content.ReadAsAsync <JObject>();

            result = json.GetValue("value") as JArray;
            Assert.Equal <int>(4, result.Count);

            // Unrelate Partners(3) from Company
            // DELETE singleton/Partners(3)/$ref
            request  = new HttpRequestMessage(HttpMethod.Delete, requestUri + "(3)/$ref");
            response = Client.SendAsync(request).Result;
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // GET singleton/Partners
            response = this.Client.GetAsync(requestUri).Result;
            json     = await response.Content.ReadAsAsync <JObject>();

            result = json.GetValue("value") as JArray;
            Assert.Equal <int>(3, result.Count);

            // GET singleton/WebStack.QA.Test.OData.Singleton.GetPartnersCount()
            requestUri = string.Format(BaseAddress + "/{0}/{1}/{2}.GetPartnersCount()", model, singletonName, NameSpace);
            response   = this.Client.GetAsync(requestUri).Result;
            json       = await response.Content.ReadAsAsync <JObject>();

            Assert.Equal(3, (int)json["value"]);
        }
Exemplo n.º 3
0
        public async Task SingletonNavigationLinkCRUD(string model, string singletonName)
        {
            string requestUri = string.Format(this.BaseAddress + "/{0}/{1}/Partners", model, singletonName);

            // Reset data source
            await ResetDataSource(model, singletonName);
            await ResetDataSource(model, "Partners");

            // GET singleton/Partners
            HttpResponseMessage response = this.Client.GetAsync(requestUri).Result;
            var json = await response.Content.ReadAsAsync<JObject>();
            var result = json.GetValue("value") as JArray;
            Assert.Equal<int>(0, result.Count);

            string navigationLinkUri = string.Format(requestUri + "/$ref");

            // POST singleton/Partners/$ref
            string idLinkBase = string.Format(this.BaseAddress + "/{0}/Partners", model);
            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, navigationLinkUri);
            request.Content = new StringContent("{ \"@odata.id\" : \"" + idLinkBase + "(1)\"}");
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // POST singleton/Partners/$ref
            request = new HttpRequestMessage(HttpMethod.Post, navigationLinkUri);
            request.Content = new StringContent("{ \"@odata.id\" : \"" + idLinkBase + "(2)\"}");
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // POST singleton/Partners/$ref
            request = new HttpRequestMessage(HttpMethod.Post, navigationLinkUri);
            request.Content = new StringContent("{ \"@odata.id\" : \"" + idLinkBase + "(3)\"}");
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // GET singleton/Partners
            response = this.Client.GetAsync(requestUri).Result;
            json = await response.Content.ReadAsAsync<JObject>();
            result = json.GetValue("value") as JArray;
            Assert.Equal<int>(3, result.Count);

            // Add Partner to Company by "Deep Insert"
            // POST singleton/Partners
            Partner partner = new Partner() { ID = 100, Name = "NewHire" };
            request = new HttpRequestMessage(HttpMethod.Post, requestUri);
            request.Content = new ObjectContent(partner.GetType(), partner, new JsonMediaTypeFormatter());
            response = Client.SendAsync(request).Result;
            response.EnsureSuccessStatusCode();

            // GET singleton/Partners
            response = this.Client.GetAsync(requestUri).Result;
            json = await response.Content.ReadAsAsync<JObject>();
            result = json.GetValue("value") as JArray;
            Assert.Equal<int>(4, result.Count);

            // Unrelate Partners(3) from Company
            // DELETE singleton/Partners(3)/$ref
            request = new HttpRequestMessage(HttpMethod.Delete, requestUri + "(3)/$ref");
            response = Client.SendAsync(request).Result;
            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // GET singleton/Partners
            response = this.Client.GetAsync(requestUri).Result;
            json = await response.Content.ReadAsAsync<JObject>();
            result = json.GetValue("value") as JArray;
            Assert.Equal<int>(3, result.Count);

            // GET singleton/WebStack.QA.Test.OData.Singleton.GetPartnersCount()
            requestUri = string.Format(BaseAddress + "/{0}/{1}/{2}.GetPartnersCount()", model, singletonName, NameSpace);
            response = this.Client.GetAsync(requestUri).Result;
            json = await response.Content.ReadAsAsync<JObject>();
            Assert.Equal(3, (int)json["value"]);
        }
Exemplo n.º 4
0
        public async Task SingletonQueryOptionsTest(string model, string singletonName)
        {
            string requestUri = string.Format(this.BaseAddress + "/{0}/{1}", model, singletonName);

            await ResetDataSource(model, singletonName);

            // GET /singleton?$select=Name
            var response = this.Client.GetAsync(requestUri + "?$select=Name").Result;
            var result = response.Content.ReadAsAsync<JObject>().Result;
            int i = 0;
            foreach (var pro in result.Properties())
            {
                i++;
            }
            Assert.Equal(2, i);

            // POST /singleton/Partners
            Partner partner = new Partner() { ID = 100, Name = "NewHire" };
            var request = new HttpRequestMessage(HttpMethod.Post, requestUri + "/Partners");
            request.Content = new ObjectContent(partner.GetType(), partner, new JsonMediaTypeFormatter());
            response = Client.SendAsync(request).Result;
            response.EnsureSuccessStatusCode();

            // POST /singleton/Partners
            partner = new Partner() { ID = 101, Name = "NewHire2" };
            request = new HttpRequestMessage(HttpMethod.Post, requestUri + "/Partners");
            request.Content = new ObjectContent(partner.GetType(), partner, new JsonMediaTypeFormatter());
            response = Client.SendAsync(request).Result;
            response.EnsureSuccessStatusCode();

            // GET /singleton?$expand=Partners($select=Name)
            response = this.Client.GetAsync(requestUri + "?$expand=Partners($select=Name)").Result;
            result = await response.Content.ReadAsAsync<JObject>();
            var json = result.GetValue("Partners") as JArray;
            Assert.Equal(2, json.Count);

            // PUT Partners(1)/Company/$ref
            var navigationUri = string.Format(this.BaseAddress + "/{0}/Partners(1)/Company/$ref", model);
            string idLinkBase = string.Format(this.BaseAddress + "/{0}/{1}", model, singletonName);
            request = new HttpRequestMessage(HttpMethod.Put, navigationUri);
            request.Content = new StringContent("{ \"@odata.id\" : \"" + idLinkBase + "\"}");
            request.Content.Headers.ContentType = MediaTypeWithQualityHeaderValue.Parse("application/json");
            response = Client.SendAsync(request).Result;

            // GET /Partners(1)?$expand=Company($select=Name)
            requestUri = string.Format(this.BaseAddress + "/{0}/Partners(1)", model);
            response = this.Client.GetAsync(requestUri + "?$expand=Company($select=Name)").Result;
            result = await response.Content.ReadAsAsync<JObject>();
            var company = result.GetValue("Company") as JObject;
            Assert.Equal(singletonName, company.GetValue("Name"));
        }
Exemplo n.º 5
0
 public IHttpActionResult POST([FromBody] Partner partner)
 {
     Partners.Add(partner);
     return(Created(partner));
 }