Skip to content

Fix/#33 #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}