Skip to content

Test controller provider #969

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 20 commits into from
Mar 18, 2021
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
3 changes: 3 additions & 0 deletions src/JsonApiDotNetCore/Middleware/HeaderConstants.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using JetBrains.Annotations;

#pragma warning disable AV1008 // Class should not be static

namespace JsonApiDotNetCore.Middleware
{
[PublicAPI]
public static class HeaderConstants
{
public const string MediaType = "application/vnd.api+json";
Expand Down
21 changes: 8 additions & 13 deletions src/JsonApiDotNetCore/Middleware/JsonApiRoutingConvention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class JsonApiRoutingConvention : IJsonApiRoutingConvention
{
private readonly IJsonApiOptions _options;
private readonly IResourceContextProvider _resourceContextProvider;
private readonly HashSet<string> _registeredTemplates = new HashSet<string>();
private readonly Dictionary<string, string> _registeredControllerNameByTemplate = new Dictionary<string, string>();
private readonly Dictionary<Type, ResourceContext> _resourceContextPerControllerTypeMap = new Dictionary<Type, ResourceContext>();

public JsonApiRoutingConvention(IJsonApiOptions options, IResourceContextProvider resourceContextProvider)
Expand Down Expand Up @@ -89,11 +89,14 @@ public void Apply(ApplicationModel application)

string template = TemplateFromResource(controller) ?? TemplateFromController(controller);

if (template == null)
if (_registeredControllerNameByTemplate.ContainsKey(template))
{
throw new InvalidConfigurationException($"Controllers with overlapping route templates detected: {controller.ControllerType.FullName}");
throw new InvalidConfigurationException(
$"Cannot register '{controller.ControllerType.FullName}' for template '{template}' because '{_registeredControllerNameByTemplate[template]}' was already registered for this template.");
}

_registeredControllerNameByTemplate.Add(template, controller.ControllerType.FullName);

controller.Selectors[0].AttributeRouteModel = new AttributeRouteModel
{
Template = template
Expand All @@ -116,10 +119,7 @@ private string TemplateFromResource(ControllerModel model)
{
string template = $"{_options.Namespace}/{resourceContext.PublicName}";

if (_registeredTemplates.Add(template))
{
return template;
}
return template;
}

return null;
Expand All @@ -133,12 +133,7 @@ private string TemplateFromController(ControllerModel model)
string controllerName = _options.SerializerNamingStrategy.GetPropertyName(model.ControllerName, false);
string template = $"{_options.Namespace}/{controllerName}";

if (_registeredTemplates.Add(template))
{
return template;
}

return null;
return template;
}

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions src/JsonApiDotNetCore/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
[assembly: InternalsVisibleTo("JsonApiDotNetCoreExampleTests")]
[assembly: InternalsVisibleTo("UnitTests")]
[assembly: InternalsVisibleTo("DiscoveryTests")]
[assembly: InternalsVisibleTo("TestBuildingBlocks")]
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public AtomicConstrainedOperationsControllerTests(ExampleIntegrationTestContext<
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<CreateMusicTrackOperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using FluentAssertions;
using FluentAssertions.Extensions;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.EntityFrameworkCore;
using TestBuildingBlocks;
Expand All @@ -23,7 +24,7 @@ public AtomicCreateResourceTests(ExampleIntegrationTestContext<TestableStartup<O
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
Expand All @@ -23,7 +24,7 @@ public AtomicCreateResourceWithClientGeneratedIdTests(
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();

var options = (JsonApiOptions)testContext.Factory.Services.GetRequiredService<IJsonApiOptions>();
options.AllowClientGeneratedIds = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.EntityFrameworkCore;
using TestBuildingBlocks;
Expand All @@ -23,7 +24,7 @@ public AtomicCreateResourceWithToManyRelationshipTests(
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
Expand All @@ -25,7 +26,7 @@ public AtomicCreateResourceWithToOneRelationshipTests(
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.EntityFrameworkCore;
using TestBuildingBlocks;
Expand All @@ -22,7 +23,7 @@ public AtomicDeleteResourceTests(ExampleIntegrationTestContext<TestableStartup<O
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using FluentAssertions;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
Expand All @@ -22,10 +23,10 @@ public AtomicAbsoluteLinksTests(ExampleIntegrationTestContext<TestableStartup<Op
{
_testContext = testContext;

testContext.UseController<OperationsController>();

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddControllersFromExampleProject();

services.AddScoped(typeof(IResourceChangeTracker<>), typeof(NeverSameResourceChangeTracker<>));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
Expand All @@ -22,10 +23,10 @@ public AtomicRelativeLinksWithNamespaceTests(
{
_testContext = testContext;

testContext.UseController<OperationsController>();

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddControllersFromExampleProject();

services.AddScoped(typeof(IResourceChangeTracker<>), typeof(NeverSameResourceChangeTracker<>));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.EntityFrameworkCore;
using TestBuildingBlocks;
Expand All @@ -21,7 +22,7 @@ public AtomicLocalIdTests(ExampleIntegrationTestContext<TestableStartup<Operatio
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using FluentAssertions.Extensions;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
Expand All @@ -22,10 +23,10 @@ public AtomicResourceMetaTests(ExampleIntegrationTestContext<TestableStartup<Ope
{
_testContext = testContext;

testContext.UseController<OperationsController>();

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddControllersFromExampleProject();

services.AddScoped<IResourceDefinition<MusicTrack, Guid>, MusicTrackMetaDefinition>();
services.AddScoped<IResourceDefinition<TextLanguage, Guid>, TextLanguageMetaDefinition>();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using FluentAssertions;
using JsonApiDotNetCore.Serialization;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json.Linq;
Expand All @@ -23,10 +24,10 @@ public AtomicResponseMetaTests(ExampleIntegrationTestContext<TestableStartup<Ope
{
_testContext = testContext;

testContext.UseController<OperationsController>();

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddControllersFromExampleProject();

services.AddSingleton<IResponseMeta, AtomicResponseMeta>();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.EntityFrameworkCore;
using TestBuildingBlocks;
Expand All @@ -19,7 +20,7 @@ public AtomicRequestBodyTests(ExampleIntegrationTestContext<TestableStartup<Oper
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
Expand All @@ -21,7 +22,7 @@ public MaximumOperationsPerRequestTests(ExampleIntegrationTestContext<TestableSt
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.EntityFrameworkCore;
using TestBuildingBlocks;
Expand All @@ -20,7 +21,7 @@ public AtomicModelStateValidationTests(ExampleIntegrationTestContext<ModelStateV
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.AspNetCore.Authentication;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -27,10 +28,11 @@ public AtomicQueryStringTests(ExampleIntegrationTestContext<TestableStartup<Oper
{
_testContext = testContext;

testContext.UseController<OperationsController>();
testContext.UseController<MusicTracksController>();

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddControllersFromExampleProject();

services.AddSingleton<ISystemClock>(new FrozenSystemClock
{
UtcNow = FrozenTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
Expand All @@ -22,10 +23,10 @@ public AtomicSparseFieldSetResourceDefinitionTests(ExampleIntegrationTestContext
{
_testContext = testContext;

testContext.UseController<OperationsController>();

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddControllersFromExampleProject();

services.AddSingleton<LyricPermissionProvider>();
services.AddScoped<IResourceDefinition<Lyric, long>, LyricTextDefinition>();
services.AddScoped(typeof(IResourceChangeTracker<>), typeof(NeverSameResourceChangeTracker<>));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;
using FluentAssertions;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.EntityFrameworkCore;
using TestBuildingBlocks;
Expand All @@ -21,7 +22,7 @@ public AtomicRollbackTests(ExampleIntegrationTestContext<TestableStartup<Operati
{
_testContext = testContext;

testContext.ConfigureServicesAfterStartup(services => services.AddControllersFromExampleProject());
testContext.UseController<OperationsController>();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Serialization.Objects;
using JsonApiDotNetCoreExample.Controllers;
using JsonApiDotNetCoreExampleTests.Startups;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -22,10 +23,10 @@ public AtomicTransactionConsistencyTests(ExampleIntegrationTestContext<TestableS
{
_testContext = testContext;

testContext.UseController<OperationsController>();

testContext.ConfigureServicesAfterStartup(services =>
{
services.AddControllersFromExampleProject();

services.AddResourceRepository<PerformerRepository>();
services.AddResourceRepository<MusicTrackRepository>();
services.AddResourceRepository<LyricRepository>();
Expand Down
Loading