示例#1
0
        public async Task OutputFiles_HaveProperFieldsOrder()
        {
            // arrange
            var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(OutputFiles_HaveProperFieldsOrder));

            // it doesn't matter what template to check - calling policy extractor as an example
            var extractorConfig = this.GetMockedExtractorConsoleAppConfiguration(
                splitApis: false,
                apiVersionSetName: string.Empty,
                multipleApiNames: string.Empty,
                includeAllRevisions: false);
            var extractorParameters = new ExtractorParameters(extractorConfig);

            var mockedPolicyApiClient = MockPolicyClient.GetMockedApiClientWithDefaultValues();
            var policyExtractor       = new PolicyExtractor(this.GetTestLogger <PolicyExtractor>(), mockedPolicyApiClient, new TemplateBuilder());

            var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
                this.GetTestLogger <ExtractorExecutor>(),
                policyExtractor: policyExtractor);

            extractorExecutor.SetExtractorParameters(extractorParameters);

            // act
            var policyTemplate = await extractorExecutor.GeneratePolicyTemplateAsync(currentTestDirectory);

            // assert
            var outputGlobalServicePolicyFilePath = Path.Combine(currentTestDirectory, extractorParameters.FileNames.GlobalServicePolicy);

            File.Exists(outputGlobalServicePolicyFilePath).Should().BeTrue();

            var policyTemplateText = await File.ReadAllTextAsync(outputGlobalServicePolicyFilePath);

            var policyTemplateJson = JToken.Parse(policyTemplateText);

            var resourcesSection = policyTemplateJson["resources"].First;

            resourcesSection.Should().NotBeNull();

            var resourcesSectionText = resourcesSection.ToString();

            var apiVersionFieldPosition = resourcesSectionText.IndexOf("\"apiVersion\"");
            var typeFieldPosition       = resourcesSectionText.IndexOf("\"type\"");
            var nameFieldPosition       = resourcesSectionText.IndexOf("\"name\"");
            var propertiesFieldPosition = resourcesSectionText.IndexOf("\"properties\"");

            apiVersionFieldPosition.Should().BeLessThan(nameFieldPosition);
            typeFieldPosition.Should().BeLessThan(nameFieldPosition);
            nameFieldPosition.Should().BeLessThan(propertiesFieldPosition);
        }
        public async Task GeneratePolicyTemplates_ProperlyLaysTheInformation()
        {
            // arrange
            var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GeneratePolicyTemplates_ProperlyLaysTheInformation));

            var extractorConfig = this.GetMockedExtractorConsoleAppConfiguration(
                splitApis: false,
                apiVersionSetName: string.Empty,
                multipleApiNames: string.Empty,
                includeAllRevisions: false);
            var extractorParameters = new ExtractorParameters(extractorConfig);

            var mockedPolicyApiClient = MockPolicyClient.GetMockedApiClientWithDefaultValues();
            var policyExtractor       = new PolicyExtractor(this.GetTestLogger <PolicyExtractor>(), mockedPolicyApiClient, new TemplateBuilder());

            var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
                this.GetTestLogger <ExtractorExecutor>(),
                policyExtractor: policyExtractor);

            extractorExecutor.SetExtractorParameters(extractorParameters);

            // act
            var policyTemplate = await extractorExecutor.GeneratePolicyTemplateAsync(currentTestDirectory);

            // policy template files exists
            File.Exists(Path.Combine(currentTestDirectory, extractorParameters.FileNames.GlobalServicePolicy)).Should().BeTrue();
            // global service policy.xml
            File.Exists(Path.Combine(currentTestDirectory, PolicyExtractor.PoliciesDirectoryName, PolicyExtractor.GlobalServicePolicyFileName)).Should().BeTrue();

            policyTemplate.Parameters.Should().ContainKey(ParameterNames.ApimServiceName);
            policyTemplate.Parameters.Should().ContainKey(ParameterNames.PolicyXMLBaseUrl);
            policyTemplate.Parameters.Should().ContainKey(ParameterNames.PolicyXMLSasToken);
            policyTemplate.TypedResources.Should().NotBeNull();
            policyTemplate.Resources.Count().Should().Be(1);

            var policyResource = policyTemplate.TypedResources.GlobalServicePolicy;

            policyResource.ApiVersion.Should().Be(GlobalConstants.ApiVersion);
            policyResource.Name.Should().NotBeNullOrEmpty();
            policyResource.Type.Should().Be(ResourceTypeConstants.GlobalServicePolicy);
            policyResource.Properties.Format.Should().NotBeNullOrEmpty();
            policyResource.Properties.PolicyContent.Should().NotBeNullOrEmpty();
        }
示例#3
0
        public async Task GeneratePolicyTemplates_GetCachedPolicy_FoundAndReturnedCorrectly()
        {
            // arrange
            var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GeneratePolicyTemplates_GetCachedPolicy_FoundAndReturnedCorrectly));

            var extractorConfig = this.GetMockedExtractorConsoleAppConfiguration(
                splitApis: false,
                apiVersionSetName: string.Empty,
                multipleApiNames: string.Empty,
                includeAllRevisions: false);
            var extractorParameters = new ExtractorParameters(extractorConfig);

            var mockedPolicyApiClient = MockPolicyClient.GetMockedApiClientWithDefaultValues();
            var policyExtractor       = new PolicyExtractor(this.GetTestLogger <PolicyExtractor>(), mockedPolicyApiClient, new TemplateBuilder());

            var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
                this.GetTestLogger <ExtractorExecutor>(),
                policyExtractor: policyExtractor);

            extractorExecutor.SetExtractorParameters(extractorParameters);

            var policyTemplate = await extractorExecutor.GeneratePolicyTemplateAsync(currentTestDirectory);

            File.Exists(Path.Combine(currentTestDirectory, PolicyExtractor.PoliciesDirectoryName, PolicyExtractor.GlobalServicePolicyFileName)).Should().BeTrue();

            // try get global service policy from cache using full path of policy stored in object
            var globalServicePolicyContent1 = policyExtractor.GetCachedPolicyContent(policyTemplate.TypedResources.GlobalServicePolicy, currentTestDirectory);

            globalServicePolicyContent1.Should().Be(MockPolicyClient.GlobalPolicyContent);

            // try get global service policy from cache using path retrieving
            policyTemplate.TypedResources.GlobalServicePolicy.Properties.PolicyContentFileFullPath = string.Empty;
            var globalServicePolicyContent2 = policyExtractor.GetCachedPolicyContent(policyTemplate.TypedResources.GlobalServicePolicy, currentTestDirectory);

            globalServicePolicyContent2.Should().Be(MockPolicyClient.GlobalPolicyContent);
        }
示例#4
0
        public async Task GenerateProductsTemplates_ProperlyLaysTheInformation()
        {
            // arrange
            var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateProductsTemplates_ProperlyLaysTheInformation));

            var extractorConfig = this.GetMockedExtractorConsoleAppConfiguration(
                splitApis: false,
                apiVersionSetName: string.Empty,
                multipleApiNames: string.Empty,
                includeAllRevisions: false);
            var extractorParameters = new ExtractorParameters(extractorConfig);

            var mockedPolicyClient    = MockPolicyClient.GetMockedApiClientWithDefaultValues();
            var mockedPolicyExtractor = new PolicyExtractor(this.GetTestLogger <PolicyExtractor>(), mockedPolicyClient, new TemplateBuilder());

            var mockedApisClient     = MockApisClient.GetMockedApiClientWithDefaultValues();
            var mockedProductsClient = MockProductsClient.GetMockedApiClientWithDefaultValues();
            var mockedGroupsClient   = MockGroupsClient.GetMockedApiClientWithDefaultValues();
            var mockedTagClient      = MockTagClient.GetMockedApiClientWithDefaultValues();

            var productExtractor = new ProductExtractor(
                this.GetTestLogger <ProductExtractor>(),
                mockedPolicyExtractor,
                mockedProductsClient,
                mockedGroupsClient,
                mockedTagClient,
                new TemplateBuilder());

            var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
                this.GetTestLogger <ExtractorExecutor>(),
                productExtractor: productExtractor);

            extractorExecutor.SetExtractorParameters(extractorParameters);

            // act
            var productTemplate = await extractorExecutor.GenerateProductsTemplateAsync(
                singleApiName : It.IsAny <string>(),
                currentTestDirectory,
                productApiResources : It.IsAny <List <ProductApiTemplateResource> >());

            // assert

            // generated product policy files
            var policyFileName = string.Format(PolicyExtractor.ProductPolicyFileNameFormat, MockProductsClient.ProductName1);

            File.Exists(Path.Combine(currentTestDirectory, PolicyExtractor.PoliciesDirectoryName, policyFileName)).Should().BeTrue();

            // generated product template files
            File.Exists(Path.Combine(currentTestDirectory, extractorParameters.FileNames.Products)).Should().BeTrue();

            var templateParameters = productTemplate.Parameters;

            templateParameters.Should().ContainKey(ParameterNames.ApimServiceName);
            templateParameters.Should().ContainKey(ParameterNames.PolicyXMLBaseUrl);
            templateParameters.Should().ContainKey(ParameterNames.PolicyXMLSasToken);
            templateParameters.Should().ContainKey(ParameterNames.ServiceUrl);
            templateParameters.Should().ContainKey(ParameterNames.ApiLoggerId);

            var templateResources = productTemplate.Resources;

            // product resource
            var productResource = templateResources.First(x => x.Type == ResourceTypeConstants.Product);

            productResource.Name.Should().Contain(MockProductsClient.ProductName1);

            // group resources
            var groupResources = templateResources.Where(x => x.Type == ResourceTypeConstants.ProductGroup).ToList();

            groupResources.Should().HaveCount(2);
            (groupResources[0].Name.Contains(MockGroupsClient.GroupName1) || groupResources[1].Name.Contains(MockGroupsClient.GroupName1)).Should().BeTrue();
            (groupResources[0].Name.Contains(MockGroupsClient.GroupName2) || groupResources[1].Name.Contains(MockGroupsClient.GroupName2)).Should().BeTrue();

            // policy resources
            var policyResource = templateResources.First(x => x.Type == ResourceTypeConstants.ProductPolicy);

            policyResource.Name.Should().Contain(MockProductsClient.ProductName1);

            // tag resources
            var tagResources = templateResources.Where(x => x.Type == ResourceTypeConstants.ProductTag).ToList();

            tagResources.Should().HaveCount(2);
            (tagResources[0].Name.Contains(MockTagClient.TagName1) || tagResources[1].Name.Contains(MockTagClient.TagName1)).Should().BeTrue();
            (tagResources[0].Name.Contains(MockTagClient.TagName2) || tagResources[1].Name.Contains(MockTagClient.TagName2)).Should().BeTrue();
        }
示例#5
0
        public async Task GenerateApiTemplates_ProperlyLaysTheInformation()
        {
            // arrange
            var currentTestDirectory = Path.Combine(this.OutputDirectory, nameof(GenerateApiTemplates_ProperlyLaysTheInformation));

            var extractorConfig = this.GetMockedExtractorConsoleAppConfiguration(
                splitApis: false,
                apiVersionSetName: string.Empty,
                multipleApiNames: string.Empty,
                includeAllRevisions: false);
            var extractorParameters = new ExtractorParameters(extractorConfig);

            // mocked clients
            var mockedApiClient          = MockApisClient.GetMockedApiClientWithDefaultValues();
            var mockedProductClient      = MockProductsClient.GetMockedApiClientWithDefaultValues();
            var mockedApiSchemaClient    = MockApiSchemaClient.GetMockedApiClientWithDefaultValues();
            var mockedPolicyClient       = MockPolicyClient.GetMockedApiClientWithDefaultValues();
            var mockedTagClient          = MockTagClient.GetMockedApiClientWithDefaultValues();
            var mockedApiOperationClient = MockApiOperationClient.GetMockedApiClientWithDefaultValues();
            var mockedDiagnosticClient   = MockDiagnosticClient.GetMockedClientWithApiDependentValues();

            // mocked extractors
            var mockedDiagnosticExtractor   = new DiagnosticExtractor(this.GetTestLogger <DiagnosticExtractor>(), mockedDiagnosticClient);
            var mockedApiSchemaExtractor    = new ApiSchemaExtractor(this.GetTestLogger <ApiSchemaExtractor>(), mockedApiSchemaClient);
            var mockedPolicyExtractor       = new PolicyExtractor(this.GetTestLogger <PolicyExtractor>(), mockedPolicyClient, new TemplateBuilder());
            var mockedProductApisExtractor  = new ProductApisExtractor(this.GetTestLogger <ProductApisExtractor>(), mockedProductClient, mockedApiClient, new TemplateBuilder());
            var mockedTagExtractor          = new TagExtractor(this.GetTestLogger <TagExtractor>(), mockedTagClient, new TemplateBuilder());
            var mockedApiOperationExtractor = new ApiOperationExtractor(this.GetTestLogger <ApiOperationExtractor>(), mockedApiOperationClient);

            var apiExtractor = new ApiExtractor(
                this.GetTestLogger <ApiExtractor>(),
                new TemplateBuilder(),
                mockedApiClient,
                mockedDiagnosticExtractor,
                mockedApiSchemaExtractor,
                mockedPolicyExtractor,
                mockedProductApisExtractor,
                mockedTagExtractor,
                mockedApiOperationExtractor);

            var extractorExecutor = ExtractorExecutor.BuildExtractorExecutor(
                this.GetTestLogger <ExtractorExecutor>(),
                apiExtractor: apiExtractor);

            extractorExecutor.SetExtractorParameters(extractorParameters);

            // act
            var apiTemplate = await extractorExecutor.GenerateApiTemplateAsync(
                singleApiName : It.IsAny <string>(),
                multipleApiNames : It.IsAny <List <string> >(),
                currentTestDirectory);

            // assert
            File.Exists(Path.Combine(currentTestDirectory, apiTemplate.TypedResources.FileName)).Should().BeTrue();
            Directory.GetFiles(Path.Combine(currentTestDirectory, PolicyExtractor.PoliciesDirectoryName)).Count().Should().Be(4);

            apiTemplate.Parameters.Should().NotBeNull();
            apiTemplate.Parameters.Should().ContainKey(ParameterNames.ApimServiceName);
            apiTemplate.Parameters.Should().ContainKey(ParameterNames.ServiceUrl);
            apiTemplate.Parameters.Should().ContainKey(ParameterNames.ApiLoggerId);
            apiTemplate.Parameters.Should().ContainKey(ParameterNames.PolicyXMLBaseUrl);
            apiTemplate.Parameters.Should().ContainKey(ParameterNames.PolicyXMLSasToken);
            apiTemplate.Resources.Count().Should().Be(23);

            // apis
            apiTemplate.TypedResources.Apis.Count().Should().Be(2);
            apiTemplate.TypedResources.Apis.All(x => x.Type == ResourceTypeConstants.API).Should().BeTrue();
            apiTemplate.TypedResources.Apis.All(x => x.Properties is not null).Should().BeTrue();

            // api schemas
            apiTemplate.TypedResources.ApiSchemas.Count().Should().Be(2);
            apiTemplate.TypedResources.ApiSchemas.All(x => x.Type == ResourceTypeConstants.APISchema).Should().BeTrue();
            apiTemplate.TypedResources.ApiSchemas.All(x => x.Properties is not null).Should().BeTrue();

            // diagnostics
            apiTemplate.TypedResources.Diagnostics.Count().Should().Be(3);
            apiTemplate.TypedResources.Diagnostics.All(x => x.Type == ResourceTypeConstants.APIServiceDiagnostic || x.Type == ResourceTypeConstants.APIDiagnostic).Should().BeTrue();
            apiTemplate.TypedResources.Diagnostics.All(x => x.Properties is not null).Should().BeTrue();

            // tags
            apiTemplate.TypedResources.Tags.Count().Should().Be(4);
            apiTemplate.TypedResources.Tags.All(x => x.Type == ResourceTypeConstants.ProductTag).Should().BeTrue();

            // api products
            apiTemplate.TypedResources.ApiProducts.Count().Should().Be(2);
            apiTemplate.TypedResources.ApiProducts.All(x => x.Type == ResourceTypeConstants.ProductApi).Should().BeTrue();
            apiTemplate.TypedResources.ApiProducts.All(x => x.Properties is not null).Should().BeTrue();

            // api policies
            apiTemplate.TypedResources.ApiPolicies.Count().Should().Be(2);
            apiTemplate.TypedResources.ApiPolicies.All(x => x.Properties is not null).Should().BeTrue();

            // api operations
            apiTemplate.TypedResources.ApiOperations.Count().Should().Be(2);
            apiTemplate.TypedResources.ApiOperations.All(x => x.Type == ResourceTypeConstants.APIOperation).Should().BeTrue();
            apiTemplate.TypedResources.ApiOperations.All(x => x.Properties is not null).Should().BeTrue();
            apiTemplate.TypedResources.ApiOperations.SelectMany(x => x.DependsOn).Any(x => x.Contains($"'{ResourceTypeConstants.API}'")).Should().BeTrue();
            apiTemplate.TypedResources.ApiOperations.SelectMany(x => x.DependsOn).Any(x => x.Contains($"'{ResourceTypeConstants.APIOperation}'")).Should().BeFalse();

            // api operations policies
            apiTemplate.TypedResources.ApiOperationsPolicies.Count().Should().Be(2);
            apiTemplate.TypedResources.ApiOperations.All(x => x.Properties is not null).Should().BeTrue();

            // api operations tags
            apiTemplate.TypedResources.ApiOperationsPolicies.Count().Should().Be(2);
            apiTemplate.TypedResources.ApiOperations.All(x => x.Properties is not null).Should().BeTrue();
        }