-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#213 [FEATURE] Allow usages of the annotation attributes on interfaces (
#214) Co-authored-by: senn.g <[email protected]>
- Loading branch information
Showing
6 changed files
with
220 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
test/Saunter.Tests/Generation/DocumentGeneratorTests/InterfaceAttributeTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Reflection; | ||
using Saunter.AsyncApiSchema.v2; | ||
using Saunter.Attributes; | ||
using Saunter.Generation; | ||
using Shouldly; | ||
using Xunit; | ||
using System.Linq; | ||
|
||
namespace Saunter.Tests.Generation.DocumentGeneratorTests | ||
{ | ||
public class InterfaceAttributeTests | ||
{ | ||
[Theory] | ||
[InlineData(typeof(IServiceEvents))] | ||
[InlineData(typeof(ServiceEventsFromInterface))] | ||
[InlineData(typeof(ServiceEventsFromAnnotatedInterface))] // Check that annotations are not inherited from the interface | ||
public void NonAnnotatedTypesTest(Type type) | ||
{ | ||
// Arrange | ||
var options = new AsyncApiOptions(); | ||
var documentGenerator = new DocumentGenerator(); | ||
|
||
// Act | ||
var document = documentGenerator.GenerateDocument(new[] { type.GetTypeInfo() }, options, options.AsyncApi, ActivatorServiceProvider.Instance); | ||
|
||
// Assert | ||
document.ShouldNotBeNull(); | ||
document.Channels.Count.ShouldBe(0); | ||
} | ||
|
||
[Theory] | ||
[InlineData(typeof(IAnnotatedServiceEvents), "interface")] | ||
[InlineData(typeof(AnnotatedServiceEventsFromInterface), "class")] | ||
[InlineData(typeof(AnnotatedServiceEventsFromAnnotatedInterface), "class")] // Check that the actual type's annotation takes precedence of the inherited interface | ||
public void AnnotatedTypesTest(Type type, string source) | ||
{ | ||
// Arrange | ||
var options = new AsyncApiOptions(); | ||
var documentGenerator = new DocumentGenerator(); | ||
|
||
// Act | ||
var document = documentGenerator.GenerateDocument(new[] { type.GetTypeInfo() }, options, options.AsyncApi, ActivatorServiceProvider.Instance); | ||
|
||
// Assert | ||
document.ShouldNotBeNull(); | ||
document.Channels.Count.ShouldBe(1); | ||
|
||
var channel = document.Channels.First(); | ||
channel.Key.ShouldBe($"{source}.event"); | ||
channel.Value.Description.ShouldBeNull(); | ||
|
||
var publish = channel.Value.Publish; | ||
publish.ShouldNotBeNull(); | ||
publish.OperationId.ShouldBe("PublishEvent"); | ||
publish.Description.ShouldBe($"({source}) Subscribe to domains events about a tenant."); | ||
|
||
var messageRef = publish.Message.ShouldBeOfType<MessageReference>(); | ||
messageRef.Id.ShouldBe("tenantEvent"); | ||
} | ||
|
||
[AsyncApi] | ||
private interface IAnnotatedServiceEvents | ||
{ | ||
[Channel("interface.event")] | ||
[PublishOperation(typeof(TenantEvent), Description = "(interface) Subscribe to domains events about a tenant.")] | ||
void PublishEvent(TenantEvent evt); | ||
} | ||
|
||
private interface IServiceEvents | ||
{ | ||
void PublishEvent(TenantEvent evt); | ||
} | ||
|
||
private class ServiceEventsFromInterface : IServiceEvents | ||
{ | ||
public void PublishEvent(TenantEvent evt) { } | ||
} | ||
|
||
private class ServiceEventsFromAnnotatedInterface : IAnnotatedServiceEvents | ||
{ | ||
public void PublishEvent(TenantEvent evt) { } | ||
} | ||
|
||
[AsyncApi] | ||
private class AnnotatedServiceEventsFromInterface : IAnnotatedServiceEvents | ||
{ | ||
[Channel("class.event")] | ||
[PublishOperation(typeof(TenantEvent), Description = "(class) Subscribe to domains events about a tenant.")] | ||
public void PublishEvent(TenantEvent evt) { } | ||
} | ||
|
||
[AsyncApi] | ||
private class AnnotatedServiceEventsFromAnnotatedInterface : IAnnotatedServiceEvents | ||
{ | ||
[Channel("class.event")] | ||
[PublishOperation(typeof(TenantEvent), Description = "(class) Subscribe to domains events about a tenant.")] | ||
public void PublishEvent(TenantEvent evt) { } | ||
} | ||
|
||
private class TenantEvent { } | ||
} | ||
} |