Skip to content

Commit

Permalink
Merge pull request #34 from Research-Institute/fix/#33
Browse files Browse the repository at this point in the history
Fix/#33
  • Loading branch information
jaredcnance authored Feb 21, 2017
2 parents ec1a1a7 + d8dfa47 commit abc7de0
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/JsonApiDotNetCore/Builders/LinkBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,26 @@ private string GetNamespaceFromPath(string path, string entityName)
{
var nSpace = string.Empty;
var segments = path.Split('/');

for(var i = 1; i < segments.Length; i++)
{
if(segments[i].ToLower() == entityName.ToLower())
if(segments[i].ToLower() == entityName.Dasherize())
break;

nSpace += $"/{segments[i].Dasherize()}";
}

return nSpace;
}

public string GetSelfRelationLink(string parent, string parentId, string child)
{
return $"{_context.BasePath}/relationships/{child.Dasherize()}";
return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/relationships/{child.Dasherize()}";
}

public string GetRelatedRelationLink(string parent, string parentId, string child)
{
return $"{_context.BasePath}/{child.Dasherize()}";
return $"{_context.BasePath}/{parent.Dasherize()}/{parentId}/{child.Dasherize()}";
}
}
}
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.2.9",
"version": "0.2.10",

"dependencies": {
"Microsoft.NETCore.App": {
Expand Down
141 changes: 141 additions & 0 deletions test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using DotNetCoreDocs;
using DotNetCoreDocs.Writers;
using JsonApiDotNetCoreExample;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Newtonsoft.Json;
using Xunit;
using JsonApiDotNetCore.Models;
using JsonApiDotNetCoreExample.Data;
using System.Linq;
using System;

namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
{
[Collection("WebHostCollection")]
public class Relationships
{
private DocsFixture<Startup, JsonDocWriter> _fixture;
private AppDbContext _context;
public Relationships(DocsFixture<Startup, JsonDocWriter> fixture)
{
_fixture = fixture;
_context = fixture.GetService<AppDbContext>();
}

[Fact]
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
{
// arrange
var builder = new WebHostBuilder()
.UseStartup<Startup>();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync());
var data = documents.Data[0];
var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{data.Id}/relationships/owner";
var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{data.Id}/owner";

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links.Self);
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related);
}

[Fact]
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById()
{
// arrange
var todoItemId = _context.TodoItems.Last().Id;

var builder = new WebHostBuilder()
.UseStartup<Startup>();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/todo-items/{todoItemId}";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<Document>(responseString).Data;
var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{todoItemId}/relationships/owner";
var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{todoItemId}/owner";

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links?.Self);
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related);
}

[Fact]
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships()
{
// arrange
var builder = new WebHostBuilder()
.UseStartup<Startup>();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/people";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync());
var data = documents.Data[0];
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{data.Id}/relationships/todo-items";
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{data.Id}/todo-items";

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links.Self);
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related);
}

[Fact]
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById()
{
// arrange
var personId = _context.People.Last().Id;

var builder = new WebHostBuilder()
.UseStartup<Startup>();

var httpMethod = new HttpMethod("GET");
var route = $"/api/v1/people/{personId}";

var server = new TestServer(builder);
var client = server.CreateClient();
var request = new HttpRequestMessage(httpMethod, route);

// act
var response = await client.SendAsync(request);
var responseString = await response.Content.ReadAsStringAsync();
var data = JsonConvert.DeserializeObject<Document>(responseString).Data;
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{personId}/relationships/todo-items";
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{personId}/todo-items";

// assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links?.Self);
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related);
}
}
}

0 comments on commit abc7de0

Please sign in to comment.