Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit c2a2cee

Browse files
f-alizadaFarhad Alizada
andauthored
Generate tagTemplate for singleApi provided with operation tags (#765)
Co-authored-by: Farhad Alizada <falizada@microsoft.com>
1 parent a993488 commit c2a2cee

File tree

3 files changed

+170
-8
lines changed

3 files changed

+170
-8
lines changed

src/ArmTemplates/Extractor/EntityExtractors/TagExtractor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public async Task<Template<TagTemplateResources>> GenerateTagsTemplateAsync(
122122
// or if it is found in tags associated with the api
123123
// or if it is found in tags associated with the products associated with the api
124124
if (string.IsNullOrEmpty(singleApiName)
125-
|| apiOperationTagResources.Any(t => t.Name.Contains($"/{tag}'"))
125+
|| apiOperationTagResources.Any(t => t.Name.Contains($"/{tagOriginalName}'"))
126126
|| productAPIResources.Any(t => t.Name.Contains($"/{singleApiName}"))
127127
&& productTagResources.Any(t => t.Name.Contains($"/{tagOriginalName}'")))
128128
{
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
// --------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License.
4+
// --------------------------------------------------------------------------
5+
6+
using System.Collections.Generic;
7+
using System.IO;
8+
using System.Linq;
9+
using System.Threading.Tasks;
10+
using Castle.Core.Internal;
11+
using FluentAssertions;
12+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Commands.Executors;
13+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Constants;
14+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Apis;
15+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Builders;
16+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Products;
17+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Common.Templates.Tags;
18+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.EntityExtractors;
19+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Extractor.Models;
20+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Extractor.Abstractions;
21+
using Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Moqs.ApiClients;
22+
using Xunit;
23+
24+
namespace Microsoft.Azure.Management.ApiManagement.ArmTemplates.Tests.Extractor.Scenarios
25+
{
26+
[Trait("Category", "Tag Extraction")]
27+
public class TagExtractorTests : ExtractorMockerWithOutputTestsBase
28+
{
29+
public TagExtractorTests() : base("tag-tests")
30+
{
31+
}
32+
33+
[Fact]
34+
public async Task GenerateTagTemplates_GetAll_ProperlyLaysTheInformation()
35+
{
36+
// arrange
37+
var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateTagTemplates_GetAll_ProperlyLaysTheInformation));
38+
39+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration();
40+
var extractorParameters = new ExtractorParameters(extractorConfig);
41+
42+
var mockedTagClient = MockTagClient.GetMockedApiClientWithDefaultValues();
43+
var tagExtractor = new TagExtractor(
44+
this.GetTestLogger<TagExtractor>(),
45+
mockedTagClient,
46+
new TemplateBuilder());
47+
48+
var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
49+
this.GetTestLogger<ExtractorExecutor>(),
50+
tagExtractor: tagExtractor);
51+
extractorExecutor.SetExtractorParameters(extractorParameters);
52+
53+
ApiTemplateResources apiTemplateResources = new ApiTemplateResources();
54+
ProductTemplateResources productTemplateResources = new ProductTemplateResources();
55+
56+
var tagTemplate = await extractorExecutor.GenerateTagTemplateAsync(
57+
string.Empty,
58+
apiTemplateResources,
59+
productTemplateResources,
60+
currentTestDirectory);
61+
62+
// assert
63+
File.Exists(Path.Combine(currentTestDirectory, extractorParameters.FileNames.Tags)).Should().BeTrue();
64+
65+
tagTemplate.Parameters.Should().ContainKey(ParameterNames.ApimServiceName);
66+
tagTemplate.TypedResources.Tags.Count().Should().Be(4);
67+
tagTemplate.Resources.Count().Should().Be(4);
68+
69+
var resources = tagTemplate.TypedResources;
70+
71+
resources.Tags.Any(x => x.Name.Contains(MockTagClient.TagName1)).Should().BeTrue();
72+
resources.Tags.Any(x => x.Name.Contains(MockTagClient.TagName2)).Should().BeTrue();
73+
resources.Tags.Any(x => x.Name.Contains(MockTagClient.OperationTagName1)).Should().BeTrue();
74+
resources.Tags.Any(x => x.Name.Contains(MockTagClient.OperationTagName2)).Should().BeTrue();
75+
resources.Tags.All(x => x.DependsOn.IsNullOrEmpty()).Should().BeTrue();
76+
}
77+
78+
[Fact]
79+
public async Task GenerateTagTemplates_GetApiOperationRelated_ProperlyLaysTheInformation()
80+
{
81+
// arrange
82+
var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateTagTemplates_GetApiOperationRelated_ProperlyLaysTheInformation));
83+
84+
var extractorConfig = this.GetDefaultExtractorConsoleAppConfiguration();
85+
var extractorParameters = new ExtractorParameters(extractorConfig);
86+
87+
var mockedTagClient = MockTagClient.GetMockedApiClientWithDefaultValues();
88+
var tagExtractor = new TagExtractor(
89+
this.GetTestLogger<TagExtractor>(),
90+
mockedTagClient,
91+
new TemplateBuilder());
92+
93+
var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
94+
this.GetTestLogger<ExtractorExecutor>(),
95+
tagExtractor: tagExtractor);
96+
extractorExecutor.SetExtractorParameters(extractorParameters);
97+
98+
ApiTemplateResources apiTemplateResources = new ApiTemplateResources()
99+
{
100+
ApiOperationsTags = new List<TagTemplateResource>()
101+
{
102+
new TagTemplateResource()
103+
{
104+
Name = $"parameters'/{MockTagClient.OperationTagName1}'"
105+
},
106+
new TagTemplateResource()
107+
{
108+
Name = $"parameters/{MockTagClient.OperationTagName2}'"
109+
}
110+
}
111+
};
112+
ProductTemplateResources productTemplateResources = new ProductTemplateResources();
113+
114+
var tagTemplate = await extractorExecutor.GenerateTagTemplateAsync(
115+
"apiName1",
116+
apiTemplateResources,
117+
productTemplateResources,
118+
currentTestDirectory);
119+
120+
// assert
121+
File.Exists(Path.Combine(currentTestDirectory, extractorParameters.FileNames.Tags)).Should().BeTrue();
122+
123+
tagTemplate.Parameters.Should().ContainKey(ParameterNames.ApimServiceName);
124+
tagTemplate.TypedResources.Tags.Count().Should().Be(2);
125+
tagTemplate.Resources.Count().Should().Be(2);
126+
127+
var resources = tagTemplate.TypedResources;
128+
129+
resources.Tags.Any(x => x.Name.Contains(MockTagClient.OperationTagName1)).Should().BeTrue();
130+
resources.Tags.Any(x => x.Name.Contains(MockTagClient.OperationTagName2)).Should().BeTrue();
131+
}
132+
}
133+
}

tests/ArmTemplates.Tests/Moqs/ApiClients/MockTagClient.cs

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@ class MockTagClient
1616
{
1717
public const string TagName1 = "tag-1";
1818
public const string TagName2 = "tag-2";
19+
public const string OperationTagName1 = "operation-tag-1";
20+
public const string OperationTagName2 = "operation-tag-2";
1921

2022
public static ITagClient GetMockedApiClientWithDefaultValues()
2123
{
22-
var mockGroupsClient = new Mock<ITagClient>(MockBehavior.Strict);
24+
var mockTagsClient = new Mock<ITagClient>(MockBehavior.Strict);
2325

24-
mockGroupsClient
26+
mockTagsClient
2527
.Setup(x => x.GetTagsLinkedToApiOperationAsync(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<ExtractorParameters>()))
2628
.ReturnsAsync(new List<TagTemplateResource>
2729
{
@@ -38,7 +40,7 @@ public static ITagClient GetMockedApiClientWithDefaultValues()
3840
}
3941
});
4042

41-
mockGroupsClient
43+
mockTagsClient
4244
.Setup(x => x.GetAllTagsLinkedToApiAsync(It.IsAny<string>(), It.IsAny<ExtractorParameters>()))
4345
.ReturnsAsync(new List<TagTemplateResource>
4446
{
@@ -55,24 +57,51 @@ public static ITagClient GetMockedApiClientWithDefaultValues()
5557
}
5658
});
5759

58-
mockGroupsClient
60+
mockTagsClient
5961
.Setup(x => x.GetAllTagsLinkedToProductAsync(It.IsAny<string>(), It.IsAny<ExtractorParameters>()))
6062
.ReturnsAsync(new List<TagTemplateResource>
6163
{
6264
new TagTemplateResource
6365
{
6466
Name = TagName1,
65-
Type = ResourceTypeConstants.ProductTag
67+
Type = ResourceTypeConstants.APITag
6668
},
6769

6870
new TagTemplateResource
6971
{
7072
Name = TagName2,
71-
Type = ResourceTypeConstants.ProductTag
73+
Type = ResourceTypeConstants.APITag
74+
}
75+
});
76+
77+
mockTagsClient
78+
.Setup(x => x.GetAllAsync(It.IsAny<ExtractorParameters>(), It.IsAny<int>()))
79+
.ReturnsAsync(new List<TagTemplateResource>
80+
{
81+
new TagTemplateResource
82+
{
83+
Name = TagName1,
84+
Type = ResourceTypeConstants.APITag
85+
},
86+
87+
new TagTemplateResource
88+
{
89+
Name = TagName2,
90+
Type = ResourceTypeConstants.APITag
91+
},
92+
new TagTemplateResource
93+
{
94+
Name = OperationTagName1,
95+
Type = ResourceTypeConstants.APITag
96+
},
97+
new TagTemplateResource
98+
{
99+
Name = OperationTagName2,
100+
Type = ResourceTypeConstants.APITag
72101
}
73102
});
74103

75-
return mockGroupsClient.Object;
104+
return mockTagsClient.Object;
76105
}
77106
}
78107
}

0 commit comments

Comments
 (0)