Skip to content

Commit d3aec2d

Browse files
committed
test: integration tests
1 parent 86e7055 commit d3aec2d

File tree

10 files changed

+31
-145
lines changed

10 files changed

+31
-145
lines changed

src/Examples/JsonApiDotNetCoreExample/Resources/ArticleResource.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ namespace JsonApiDotNetCoreExample.Resources
1010
{
1111
public class ArticleResource : ResourceDefinition<Article>
1212
{
13-
//public override IEnumerable<Article> AfterRead(IEnumerable<Article> entities, ResourceAction pipeline, bool nestedHook = false)
14-
//{
15-
// if (pipeline == ResourceAction.GetSingle && entities.Single().Name == "Classified")
16-
// {
17-
// throw new JsonApiException(403, "You are not allowed to see this article!", new UnauthorizedAccessException());
18-
// }
19-
// return entities.Where(t => t.Name != "This should be not be included");
20-
//}
13+
public override IEnumerable<Article> OnReturn(IEnumerable<Article> entities, ResourceAction pipeline)
14+
{
15+
if (pipeline == ResourceAction.GetSingle && entities.Single().Name == "Classified")
16+
{
17+
throw new JsonApiException(403, "You are not allowed to see this article!", new UnauthorizedAccessException());
18+
}
19+
return entities.Where(t => t.Name != "This should be not be included");
20+
}
2121
}
2222
}
2323

src/Examples/JsonApiDotNetCoreExample/Resources/PersonResource.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using JsonApiDotNetCore.Hooks.Discovery;
54
using JsonApiDotNetCore.Internal;
65
using JsonApiDotNetCore.Models;
76
using JsonApiDotNetCore.Services;

src/Examples/JsonApiDotNetCoreExample/Resources/TagResource.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ namespace JsonApiDotNetCoreExample.Resources
88
{
99
public class TagResource : ResourceDefinition<Tag>
1010
{
11-
//public override IEnumerable<Tag> AfterRead(IEnumerable<Tag> entities, ResourceAction pipeline, bool nestedHook = false)
12-
//{
13-
// return entities.Where(t => t.Name != "This should be not be included");
14-
//}
11+
public override IEnumerable<Tag> OnReturn(IEnumerable<Tag> entities, ResourceAction pipeline)
12+
{
13+
return entities.Where(t => t.Name != "This should be not be included");
14+
}
1515
}
1616
}

src/JsonApiDotNetCore/Hooks/ResourceHookExecutor.cs

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public class ResourceHookExecutor : IResourceHookExecutor
3434
protected readonly IHookExecutorHelper _meta;
3535
protected readonly IJsonApiContext _context;
3636
private readonly IResourceGraph _graph;
37-
protected Dictionary<Type, HashSet<IIdentifiable>> _processedEntities;
38-
3937

4038
public ResourceHookExecutor(
4139
IHookExecutorHelper meta,
@@ -46,8 +44,7 @@ IResourceGraph graph
4644
_meta = meta;
4745
_context = context;
4846
_graph = graph;
49-
_processedEntities = new Dictionary<Type, HashSet<IIdentifiable>>();
50-
_layerFactory = new EntityTreeLayerFactory(meta, graph, _context, _processedEntities);
47+
_layerFactory = new EntityTreeLayerFactory(meta, graph, _context);
5148
}
5249

5350

@@ -64,8 +61,6 @@ public virtual void BeforeRead<TEntity>(ResourceAction pipeline, string stringId
6461
// TODO: Get rid of nested boolean and calledContainers, add BeforeReadRelation hook
6562
RecursiveBeforeRead(contextEntity, relationshipPath.Split('.').ToList(), pipeline, calledContainers);
6663
}
67-
68-
6964
}
7065

7166
void RecursiveBeforeRead(ContextEntity contextEntity, List<string> relationshipChain, ResourceAction pipeline, List<Type> calledContainers)
@@ -111,7 +106,6 @@ public virtual IEnumerable<TEntity> BeforeUpdate<TEntity>(IEnumerable<TEntity> e
111106
}
112107
EntityTreeLayer nextLayer = _layerFactory.CreateLayer(layer);
113108
BeforeUpdateRelationship(pipeline, nextLayer);
114-
FlushRegister();
115109
return entities;
116110
}
117111

@@ -128,7 +122,6 @@ public virtual IEnumerable<TEntity> BeforeCreate<TEntity>(IEnumerable<TEntity> e
128122
EntityTreeLayer nextLayer = _layerFactory.CreateLayer(layer);
129123

130124
BeforeUpdateRelationship(pipeline, nextLayer);
131-
FlushRegister();
132125
return entities;
133126
}
134127

@@ -219,26 +212,24 @@ public virtual IEnumerable<TEntity> BeforeDelete<TEntity>(IEnumerable<TEntity> e
219212
}
220213
}
221214

222-
FlushRegister();
223215
return entities;
224216

225217
}
226218

227219
public virtual IEnumerable<TEntity> OnReturn<TEntity>(IEnumerable<TEntity> entities, ResourceAction pipeline) where TEntity : class, IIdentifiable
228220
{
229-
var hookContainer = _meta.GetResourceHookContainer<TEntity>(ResourceHook.AfterRead);
221+
var hookContainer = _meta.GetResourceHookContainer<TEntity>(ResourceHook.OnReturn);
230222
var layer = _layerFactory.CreateLayer(entities);
231223
if (hookContainer != null)
232224
{
233225
var uniqueEntities = layer.GetAllUniqueEntities().Cast<TEntity>();
234-
var filteredUniqueEntities = hookContainer?.OnReturn(uniqueEntities, pipeline);
226+
var filteredUniqueEntities = hookContainer.OnReturn(uniqueEntities, pipeline);
235227
/// this is not updating unique entities internally!!!! say [a_1, a_2] => [a_1],
236228
/// then nested hooks for relations of a_2 are still being fired
237229
entities = entities.Intersect(filteredUniqueEntities, Comparer).Cast<TEntity>();
238230
}
239231
var nextLayer = _layerFactory.CreateLayer(layer);
240232
RecursiveOnReturn(nextLayer, pipeline);
241-
FlushRegister();
242233
return entities;
243234
}
244235

@@ -247,7 +238,7 @@ void RecursiveOnReturn(EntityTreeLayer currentLayer, ResourceAction pipeline)
247238
foreach (NodeInLayer node in currentLayer)
248239
{
249240
var entityType = node.EntityType;
250-
var hookContainer = _meta.GetResourceHookContainer(entityType, ResourceHook.AfterRead);
241+
var hookContainer = _meta.GetResourceHookContainer(entityType, ResourceHook.OnReturn);
251242
if (hookContainer == null) continue;
252243

253244
var filteredUniqueSet = CallHook(hookContainer, ResourceHook.OnReturn, new object[] { node.UniqueSet, pipeline }).Cast<IIdentifiable>();
@@ -286,19 +277,17 @@ void Reassign(NodeInLayer node)
286277
}
287278
}
288279

289-
290280
public virtual void AfterRead<TEntity>(IEnumerable<TEntity> entities, ResourceAction pipeline) where TEntity : class, IIdentifiable
291281
{
292282
var hookContainer = _meta.GetResourceHookContainer<TEntity>(ResourceHook.AfterCreate);
293283
var layer = _layerFactory.CreateLayer(entities);
294284
if (hookContainer != null)
295285
{
296286
var uniqueEntities = layer.GetAllUniqueEntities().Cast<TEntity>();
297-
hookContainer?.AfterRead(uniqueEntities, pipeline);
287+
hookContainer.AfterRead(uniqueEntities, pipeline);
298288
}
299289
EntityTreeLayer nextLayer = _layerFactory.CreateLayer(layer);
300290
RecursiveAfterRead(nextLayer, pipeline);
301-
FlushRegister();
302291
}
303292

304293
void RecursiveAfterRead(EntityTreeLayer currentLayer, ResourceAction pipeline)
@@ -326,10 +315,8 @@ public virtual void AfterCreate<TEntity>(IEnumerable<TEntity> entities, Resource
326315
}
327316
EntityTreeLayer nextLayer = _layerFactory.CreateLayer(layer);
328317

329-
FlushRegister();
330318
}
331319

332-
333320
public virtual void AfterUpdate<TEntity>(IEnumerable<TEntity> entities, ResourceAction pipeline) where TEntity : class, IIdentifiable
334321
{
335322
var hookContainer = _meta.GetResourceHookContainer<TEntity>(ResourceHook.AfterUpdate);
@@ -339,7 +326,6 @@ public virtual void AfterUpdate<TEntity>(IEnumerable<TEntity> entities, Resource
339326
var uniqueEntities = layer.GetAllUniqueEntities().Cast<TEntity>();
340327
hookContainer?.AfterUpdate(uniqueEntities, pipeline);
341328
}
342-
FlushRegister();
343329
}
344330

345331
void AfterUpdateRelationship(EntityTreeLayer currentLayer, ResourceAction pipeline)
@@ -363,9 +349,8 @@ public virtual void AfterDelete<TEntity>(IEnumerable<TEntity> entities, Resource
363349
if (hookContainer != null)
364350
{
365351
var uniqueEntities = layer.GetAllUniqueEntities().Cast<TEntity>();
366-
hookContainer?.AfterDelete(uniqueEntities, pipeline, succeeded);
352+
hookContainer.AfterDelete(uniqueEntities, pipeline, succeeded);
367353
}
368-
FlushRegister();
369354
}
370355

371356

@@ -429,11 +414,6 @@ IList LoadDbValues(IList entities, List<RelationshipProxy> relationships, Type e
429414
}
430415

431416

432-
433-
434-
435-
436-
437417
/// <summary>
438418
/// checks that the collection does not contain more than one item when
439419
/// relevant (eg AfterRead from GetSingle pipeline).
@@ -469,16 +449,6 @@ IEnumerable CallHook(IResourceHookContainer container, ResourceHook hook, object
469449
return (IEnumerable)ThrowJsonApiExceptionOnError(() => method.Invoke(container, arguments));
470450
}
471451

472-
/// <summary>
473-
/// We need to flush the list of processed entities because typically
474-
/// the hook executor will be caled twice per service pipeline (eg BeforeCreate
475-
/// and AfterCreate).
476-
/// </summary>
477-
void FlushRegister()
478-
{
479-
_processedEntities = new Dictionary<Type, HashSet<IIdentifiable>>();
480-
}
481-
482452
object ThrowJsonApiExceptionOnError(Func<object> action)
483453
{
484454
try

src/JsonApiDotNetCore/Hooks/TreeTraversal/EntityTreeLayer.cs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,30 @@ public class EntityTreeLayerFactory
1414
private readonly IHookExecutorHelper _meta;
1515
private readonly IResourceGraph _graph;
1616
private readonly IJsonApiContext _context;
17-
private readonly Dictionary<Type, HashSet<IIdentifiable>> _processedEntities;
17+
private Dictionary<Type, HashSet<IIdentifiable>> _processedEntities;
1818

1919

2020
public EntityTreeLayerFactory(
2121
IHookExecutorHelper meta,
2222
IResourceGraph graph,
23-
IJsonApiContext context,
24-
Dictionary<Type, HashSet<IIdentifiable>> processedEntities)
23+
IJsonApiContext context)
2524
{
2625
_context = context;
2726
_meta = meta;
2827
_graph = graph;
29-
_processedEntities = processedEntities;
3028
}
3129

30+
// nested
3231
public EntityTreeLayer CreateLayer(EntityTreeLayer currentLayer)
3332
{
3433
var nextLayer = new EntityTreeLayer(currentLayer, _meta, _graph, _context, _processedEntities);
3534
return nextLayer;
3635
}
3736

37+
// root
3838
public EntityTreeLayer CreateLayer(IEnumerable<IIdentifiable> currentLayerEntities)
3939
{
40+
_processedEntities = new Dictionary<Type, HashSet<IIdentifiable>>();
4041
var layer = new EntityTreeLayer(currentLayerEntities, _meta, _graph, _context, _processedEntities);
4142
return layer;
4243
}
@@ -60,17 +61,13 @@ public class EntityTreeLayer : IEnumerable<NodeInLayer>
6061
private readonly Dictionary<RelationshipProxy, List<IIdentifiable>> _previousEntitiesByRelationship;
6162

6263

63-
64-
public bool IsRootLayer { get; private set; }
65-
6664
public EntityTreeLayer(
6765
IEnumerable<IIdentifiable> currentLayerEntities,
6866
IHookExecutorHelper meta,
6967
IResourceGraph graph,
7068
IJsonApiContext context,
7169
Dictionary<Type, HashSet<IIdentifiable>> processedEntities)
7270
{
73-
IsRootLayer = true;
7471
_meta = meta;
7572
_graph = graph;
7673
_context = context;
@@ -88,7 +85,6 @@ public EntityTreeLayer(
8885
IJsonApiContext context,
8986
Dictionary<Type, HashSet<IIdentifiable>> processedEntities)
9087
{
91-
IsRootLayer = false;
9288
_meta = meta;
9389
_graph = graph;
9490
_context = context;
@@ -278,7 +274,7 @@ public IEnumerator<NodeInLayer> GetEnumerator()
278274
}
279275
var currentLayerByRelationship = _currentEntitiesByRelationship?.Where(p => p.Key.DependentType == dependent).ToDictionary(p => p.Key, p => p.Value);
280276
var previousLayerByRelationship = _previousEntitiesByRelationship?.Where(p => p.Key.DependentType == dependent).ToDictionary(p => p.Key, p => p.Value);
281-
yield return new NodeInLayer(dependent, uniqueEntities, currentLayerByRelationship, previousLayerByRelationship, relationships, IsRootLayer);
277+
yield return new NodeInLayer(dependent, uniqueEntities, currentLayerByRelationship, previousLayerByRelationship, relationships);
282278
}
283279
}
284280

src/JsonApiDotNetCore/Hooks/TreeTraversal/NodeInLayer.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ public class NodeInLayer
1515
{
1616
private HashSet<IIdentifiable> _uniqueSet;
1717

18-
public bool IsRootLayerNode { get; private set; }
1918
public Dictionary<RelationshipProxy, List<IIdentifiable>> EntitiesByRelationships { get; private set; }
2019
public Dictionary<RelationshipProxy, List<IIdentifiable>> PrincipalEntitiesByRelationships { get; private set; }
2120
public List<RelationshipProxy> Relationships { get; private set; }
@@ -27,16 +26,14 @@ public NodeInLayer(
2726
HashSet<IIdentifiable> uniqueSet,
2827
Dictionary<RelationshipProxy, List<IIdentifiable>> entitiesByRelationships,
2928
Dictionary<RelationshipProxy, List<IIdentifiable>> principalEntitiesByRelationships,
30-
List<RelationshipProxy> relationships,
31-
bool isRootLayerNode
29+
List<RelationshipProxy> relationships
3230
)
3331
{
3432
_uniqueSet = uniqueSet;
3533
EntityType = principalType;
3634
EntitiesByRelationships = entitiesByRelationships;
3735
PrincipalEntitiesByRelationships = principalEntitiesByRelationships;
3836
Relationships = relationships;
39-
IsRootLayerNode = isRootLayerNode;
4037
}
4138

4239

src/JsonApiDotNetCore/Hooks/TreeTraversal/RelationshipGroupEntry.cs

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

src/JsonApiDotNetCore/Hooks/TreeTraversal/RelationshipGroups.cs

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

0 commit comments

Comments
 (0)