Skip to content

Commit 756dab9

Browse files
author
Bart Koelman
committed
Refactored existing tests to use friendlier models
1 parent 60628e4 commit 756dab9

21 files changed

+802
-735
lines changed

src/JsonApiDotNetCore/QueryStrings/Internal/ResourceDefinitionQueryableParameterReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public virtual void Read(string parameterName, StringValues parameterValue)
5656

5757
private object GetQueryableHandler(string parameterName)
5858
{
59-
Type resourceType = _request.PrimaryResource.ResourceType;
59+
Type resourceType = (_request.SecondaryResource ?? _request.PrimaryResource).ResourceType;
6060
object handler = _resourceDefinitionAccessor.GetQueryableHandlerForQueryStringParameter(resourceType, parameterName);
6161

6262
if (handler != null && _request.Kind != EndpointKind.Primary)

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ResourceDefinitions/Reading/CallableResource.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ResourceDefinitions/Reading/CallableResourceDefinition.cs

Lines changed: 0 additions & 120 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceDefinitions.Reading
2+
{
3+
public interface IClientSettingsProvider
4+
{
5+
bool IsIncludePlanetMoonsBlocked { get; }
6+
bool ArePlanetsWithPrivateNameHidden { get; }
7+
}
8+
}

test/JsonApiDotNetCoreExampleTests/IntegrationTests/ResourceDefinitions/Reading/IUserRolesService.cs

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using JetBrains.Annotations;
2+
using JsonApiDotNetCore.Resources;
3+
using JsonApiDotNetCore.Resources.Annotations;
4+
5+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceDefinitions.Reading
6+
{
7+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
8+
public sealed class Moon : Identifiable
9+
{
10+
[Attr]
11+
public string Name { get; set; }
12+
13+
[Attr]
14+
public decimal SolarRadius { get; set; }
15+
16+
[HasOne]
17+
public Planet OrbitsAround { get; set; }
18+
}
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Linq;
2+
using JetBrains.Annotations;
3+
using JsonApiDotNetCore.Configuration;
4+
using JsonApiDotNetCore.Resources;
5+
using Microsoft.Extensions.Primitives;
6+
7+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceDefinitions.Reading
8+
{
9+
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
10+
public sealed class MoonDefinition : JsonApiResourceDefinition<Moon>
11+
{
12+
public MoonDefinition(IResourceGraph resourceGraph)
13+
: base(resourceGraph)
14+
{
15+
// This constructor will be resolved from the container, which means
16+
// you can take on any dependency that is also defined in the container.
17+
}
18+
19+
public override QueryStringParameterHandlers<Moon> OnRegisterQueryableHandlersForQueryStringParameters()
20+
{
21+
return new QueryStringParameterHandlers<Moon>
22+
{
23+
["isLargerThanTheSun"] = FilterByRadius
24+
};
25+
}
26+
27+
private static IQueryable<Moon> FilterByRadius(IQueryable<Moon> source, StringValues parameterValue)
28+
{
29+
bool isFilterOnLargerThan = bool.Parse(parameterValue);
30+
return isFilterOnLargerThan ? source.Where(moon => moon.SolarRadius > 1m) : source.Where(moon => moon.SolarRadius <= 1m);
31+
}
32+
}
33+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55

66
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceDefinitions.Reading
77
{
8-
public sealed class CallableResourcesController : JsonApiController<CallableResource>
8+
public sealed class MoonsController : JsonApiController<Moon>
99
{
10-
public CallableResourcesController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<CallableResource> resourceService)
10+
public MoonsController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Moon> resourceService)
1111
: base(options, loggerFactory, resourceService)
1212
{
1313
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
using JetBrains.Annotations;
3+
using JsonApiDotNetCore.Resources;
4+
using JsonApiDotNetCore.Resources.Annotations;
5+
6+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceDefinitions.Reading
7+
{
8+
[UsedImplicitly(ImplicitUseTargetFlags.Members)]
9+
public sealed class Planet : Identifiable
10+
{
11+
[Attr]
12+
public string PublicName { get; set; }
13+
14+
[Attr]
15+
public string PrivateName { get; set; }
16+
17+
[Attr]
18+
public bool HasRingSystem { get; set; }
19+
20+
[Attr]
21+
public decimal SolarMass { get; set; }
22+
23+
[HasMany]
24+
public ISet<Moon> Moons { get; set; }
25+
26+
[HasOne]
27+
public Star BelongsTo { get; set; }
28+
}
29+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Net;
4+
using JetBrains.Annotations;
5+
using JsonApiDotNetCore;
6+
using JsonApiDotNetCore.Configuration;
7+
using JsonApiDotNetCore.Errors;
8+
using JsonApiDotNetCore.Queries.Expressions;
9+
using JsonApiDotNetCore.Resources;
10+
using JsonApiDotNetCore.Resources.Annotations;
11+
using JsonApiDotNetCore.Serialization.Objects;
12+
13+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceDefinitions.Reading
14+
{
15+
[UsedImplicitly(ImplicitUseKindFlags.InstantiatedNoFixedConstructorSignature)]
16+
public sealed class PlanetDefinition : JsonApiResourceDefinition<Planet>
17+
{
18+
private readonly IClientSettingsProvider _clientSettingsProvider;
19+
20+
public PlanetDefinition(IResourceGraph resourceGraph, IClientSettingsProvider clientSettingsProvider)
21+
: base(resourceGraph)
22+
{
23+
// This constructor will be resolved from the container, which means
24+
// you can take on any dependency that is also defined in the container.
25+
26+
_clientSettingsProvider = clientSettingsProvider;
27+
}
28+
29+
public override IReadOnlyCollection<IncludeElementExpression> OnApplyIncludes(IReadOnlyCollection<IncludeElementExpression> existingIncludes)
30+
{
31+
if (_clientSettingsProvider.IsIncludePlanetMoonsBlocked &&
32+
existingIncludes.Any(include => include.Relationship.Property.Name == nameof(Planet.Moons)))
33+
{
34+
throw new JsonApiException(new Error(HttpStatusCode.BadRequest)
35+
{
36+
Title = "Including moons is not permitted."
37+
});
38+
}
39+
40+
return existingIncludes;
41+
}
42+
43+
public override FilterExpression OnApplyFilter(FilterExpression existingFilter)
44+
{
45+
if (_clientSettingsProvider.ArePlanetsWithPrivateNameHidden)
46+
{
47+
ResourceContext resourceContext = ResourceGraph.GetResourceContext<Planet>();
48+
AttrAttribute privateNameAttribute = resourceContext.Attributes.Single(attribute => attribute.Property.Name == nameof(Planet.PrivateName));
49+
50+
FilterExpression hasNoPrivateName = new ComparisonExpression(ComparisonOperator.Equals, new ResourceFieldChainExpression(privateNameAttribute),
51+
new NullConstantExpression());
52+
53+
return existingFilter == null
54+
? hasNoPrivateName
55+
: new LogicalExpression(LogicalOperator.And, ArrayFactory.Create(hasNoPrivateName, existingFilter));
56+
}
57+
58+
return existingFilter;
59+
}
60+
}
61+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using JsonApiDotNetCore.Configuration;
2+
using JsonApiDotNetCore.Controllers;
3+
using JsonApiDotNetCore.Services;
4+
using Microsoft.Extensions.Logging;
5+
6+
namespace JsonApiDotNetCoreExampleTests.IntegrationTests.ResourceDefinitions.Reading
7+
{
8+
public sealed class PlanetsController : JsonApiController<Planet>
9+
{
10+
public PlanetsController(IJsonApiOptions options, ILoggerFactory loggerFactory, IResourceService<Planet> resourceService)
11+
: base(options, loggerFactory, resourceService)
12+
{
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)