Пример #1
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 = await this.Client.GetAsync(requestUri);

            var json = await response.Content.ReadAsObject <JObject>();

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

            Assert.Empty(result);

            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 = await Client.SendAsync(request);

            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 = await Client.SendAsync(request);

            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 = await Client.SendAsync(request);

            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // GET singleton/Partners
            response = await this.Client.GetAsync(requestUri);

            json = await response.Content.ReadAsObject <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"
            };

            response = await this.Client.PostAsJsonAsync(requestUri, partner);

            response.EnsureSuccessStatusCode();

            // GET singleton/Partners
            response = await this.Client.GetAsync(requestUri);

            json = await response.Content.ReadAsObject <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 = await Client.SendAsync(request);

            Assert.Equal(HttpStatusCode.NoContent, response.StatusCode);

            // GET singleton/Partners
            response = await this.Client.GetAsync(requestUri);

            json = await response.Content.ReadAsObject <JObject>();

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

            // GET singleton/Microsoft.Test.E2E.AspNet.OData.Singleton.GetPartnersCount()
            requestUri = string.Format(BaseAddress + "/{0}/{1}/{2}.GetPartnersCount()", model, singletonName, NameSpace);
            response   = await this.Client.GetAsync(requestUri);

            json = await response.Content.ReadAsObject <JObject>();

            Assert.Equal(3, (int)json["value"]);
        }
Пример #2
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 = await this.Client.GetAsync(requestUri + "?$select=Name");

            var result = await response.Content.ReadAsObject <JObject>();

            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"
            };

            response = await this.Client.PostAsJsonAsync(requestUri + "/Partners", partner);

            response.EnsureSuccessStatusCode();

            // POST /singleton/Partners
            partner = new Partner()
            {
                ID = 101, Name = "NewHire2"
            };
            response = await this.Client.PostAsJsonAsync(requestUri + "/Partners", partner);

            response.EnsureSuccessStatusCode();

            // GET /singleton?$expand=Partners($select=Name)
            response = await this.Client.GetAsync(requestUri + "?$expand=Partners($select=Name)");

            result = await response.Content.ReadAsObject <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);
            HttpRequestMessage request       = new HttpRequestMessage(HttpMethod.Put, navigationUri);

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

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

            result = await response.Content.ReadAsObject <JObject>();

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

            Assert.Equal(singletonName, company.GetValue("Name"));
        }
 public IActionResult POST([FromBody] Partner partner)
 {
     Partners.Add(partner);
     return(Created(partner));
 }