|
| 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 | +} |
0 commit comments