Пример #1
0
        public async void ShouldCreateAPITemplateResourceFromCreatorConfigWithoutServiceUrlParameter()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            CreatorConfig      creatorConfig      = new CreatorConfig()
            {
                apis = new List <APIConfig>()
            };
            APIConfig api = new APIConfig()
            {
                name        = "name",
                openApiSpec = "https://petstore.swagger.io/v2/swagger.json",
                serviceUrl  = "https://petstore.swagger.io"
            };

            creatorConfig.apis.Add(api);

            // act
            // the above api config will create a unified api template with a single resource
            List <Template> apiTemplates = await apiTemplateCreator.CreateAPITemplatesAsync(api);

            APITemplateResource apiTemplateResource = apiTemplates.FirstOrDefault().resources[0] as APITemplateResource;

            // assert
            Assert.Single(apiTemplates.First().parameters);
            Assert.False(apiTemplates.First().parameters.ContainsKey(ParameterNames.ServiceUrl));
            Assert.Equal("https://petstore.swagger.io", apiTemplateResource.properties.serviceUrl);
        }
Пример #2
0
        public async void ShouldAppendRevisionToAPIName()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            CreatorConfig      creatorConfig      = new CreatorConfig()
            {
                apis = new List <APIConfig>()
            };
            APIConfig api = new APIConfig()
            {
                name                 = "name",
                apiRevision          = "2",
                isCurrent            = false,
                suffix               = "suffix",
                subscriptionRequired = true,
                openApiSpec          = "https://petstore.swagger.io/v2/swagger.json",
            };

            creatorConfig.apis.Add(api);

            // act
            // the above api config will create a unified api template with a single resource
            List <Template> apiTemplates = await apiTemplateCreator.CreateAPITemplatesAsync(api);

            APITemplateResource apiTemplateResource = apiTemplates.FirstOrDefault().resources[0] as APITemplateResource;

            // assert
            Assert.Contains(";rev", apiTemplateResource.name);
        }
Пример #3
0
        public async void ShouldCreateSubsequentlAPITemplateResourceFromCreatorConfigWithAlternateTitle()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            CreatorConfig      creatorConfig      = new CreatorConfig()
            {
                apis = new List <APIConfig>()
            };
            APIConfig api = new APIConfig()
            {
                name        = "name",
                displayName = "Swagger Petstore (alternate title)",
                openApiSpec = "https://petstore.swagger.io/v2/swagger.json",
            };

            creatorConfig.apis.Add(api);

            // act
            APITemplateResource apiTemplateResource = await apiTemplateCreator.CreateAPITemplateResourceAsync(api, true, true);

            // assert
            Assert.Equal($"[concat(parameters('ApimServiceName'), '/{api.name}')]", apiTemplateResource.name);
            Assert.Equal("swagger-json", apiTemplateResource.properties.format);

            // check alternate title has been specified in the embedded YAML or JSON definition

            var yaml         = apiTemplateResource.properties.value;
            var deserializer = new YamlDotNet.Serialization.Deserializer();
            var definition   = deserializer.Deserialize <Dictionary <string, object> >(yaml);
            var info         = (Dictionary <object, object>)definition["info"];

            Assert.Equal("Swagger Petstore (alternate title)", info["title"]);
        }
        public async void ShouldCreateUnifiedAPITemplateResourceFromCreatorConfig()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            CreatorConfig      creatorConfig      = new CreatorConfig()
            {
                apis = new List <APIConfig>()
            };
            APIConfig api = new APIConfig()
            {
                name                   = "name",
                apiVersion             = "apiVersion",
                apiVersionDescription  = "apiVersionDescription",
                apiVersionSetId        = "apiVersionSetId",
                revision               = "revision",
                revisionDescription    = "revisionDescription",
                suffix                 = "suffix",
                subscriptionRequired   = true,
                authenticationSettings = new APITemplateAuthenticationSettings()
                {
                    oAuth2 = new APITemplateOAuth2()
                    {
                        authorizationServerId = "",
                        scope = ""
                    },
                    openid = new APITemplateOpenID()
                    {
                        openidProviderId          = "",
                        bearerTokenSendingMethods = new string[] { }
                    },
                    subscriptionKeyRequired = true
                },
                openApiSpec = "https://petstore.swagger.io/v2/swagger.json",
            };

            creatorConfig.apis.Add(api);

            // act
            APITemplateResource apiTemplateResource = await apiTemplateCreator.CreateAPITemplateResourceAsync(api, false, true);

            // assert
            Assert.Equal($"[concat(parameters('ApimServiceName'), '/{api.name}')]", apiTemplateResource.name);
            Assert.Equal(api.name, apiTemplateResource.properties.displayName);
            Assert.Equal(api.apiVersion, apiTemplateResource.properties.apiVersion);
            Assert.Equal(api.apiVersionDescription, apiTemplateResource.properties.apiVersionDescription);
            Assert.Equal($"[resourceId('Microsoft.ApiManagement/service/apiVersionSets', parameters('ApimServiceName'), '{api.apiVersionSetId}')]", apiTemplateResource.properties.apiVersionSetId);
            Assert.Equal(api.revision, apiTemplateResource.properties.apiRevision);
            Assert.Equal(api.revisionDescription, apiTemplateResource.properties.apiRevisionDescription);
            Assert.Equal(api.suffix, apiTemplateResource.properties.path);
            Assert.Equal(api.subscriptionRequired, apiTemplateResource.properties.subscriptionRequired);
            Assert.Equal(api.authenticationSettings.oAuth2.authorizationServerId, apiTemplateResource.properties.authenticationSettings.oAuth2.authorizationServerId);
            Assert.Equal(api.authenticationSettings.oAuth2.scope, apiTemplateResource.properties.authenticationSettings.oAuth2.scope);
            Assert.Equal(api.authenticationSettings.openid.openidProviderId, apiTemplateResource.properties.authenticationSettings.openid.openidProviderId);
            Assert.Equal(api.authenticationSettings.openid.bearerTokenSendingMethods, apiTemplateResource.properties.authenticationSettings.openid.bearerTokenSendingMethods);
            Assert.Equal(api.authenticationSettings.subscriptionKeyRequired, apiTemplateResource.properties.authenticationSettings.subscriptionKeyRequired);
            Assert.Equal("swagger-link-json", apiTemplateResource.properties.format);
            Assert.Equal(api.openApiSpec, apiTemplateResource.properties.value);
        }
Пример #5
0
        public async void ShouldCreateInitialAPITemplateResourceFromCreatorConfig()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            CreatorConfig      creatorConfig      = new CreatorConfig()
            {
                api = new APIConfig()
                {
                    name                   = "name",
                    apiVersion             = "apiVersion",
                    apiVersionDescription  = "apiVersionDescription",
                    apiVersionSetId        = "apiVersionSetId",
                    revision               = "revision",
                    revisionDescription    = "revisionDescription",
                    suffix                 = "suffix",
                    subscriptionRequired   = true,
                    authenticationSettings = new APITemplateAuthenticationSettings()
                    {
                        oAuth2 = new APITemplateOAuth2()
                        {
                            authorizationServerId = "",
                            scope = ""
                        },
                        openid = new APITemplateOpenID()
                        {
                            openidProviderId          = "",
                            bearerTokenSendingMethods = new string[] { }
                        },
                        subscriptionKeyRequired = true
                    },
                    openApiSpec = "https://petstore.swagger.io/v2/swagger.json"
                }
            };

            // act
            APITemplateResource apiTemplateResource = await apiTemplateCreator.CreateInitialAPITemplateResourceAsync(creatorConfig);

            // assert
            Assert.Equal($"[concat(parameters('ApimServiceName'), '/{creatorConfig.api.name}')]", apiTemplateResource.name);
            Assert.Equal(creatorConfig.api.name, apiTemplateResource.properties.displayName);
            Assert.Equal(creatorConfig.api.apiVersion, apiTemplateResource.properties.apiVersion);
            Assert.Equal(creatorConfig.api.apiVersionDescription, apiTemplateResource.properties.apiVersionDescription);
            Assert.Equal(creatorConfig.api.apiVersionSetId, apiTemplateResource.properties.apiVersionSetId);
            Assert.Equal(creatorConfig.api.revision, apiTemplateResource.properties.apiRevision);
            Assert.Equal(creatorConfig.api.revisionDescription, apiTemplateResource.properties.apiRevisionDescription);
            Assert.Equal(creatorConfig.api.suffix, apiTemplateResource.properties.path);
            Assert.Equal(creatorConfig.api.subscriptionRequired, apiTemplateResource.properties.subscriptionRequired);
            Assert.Equal(creatorConfig.api.authenticationSettings.oAuth2.authorizationServerId, apiTemplateResource.properties.authenticationSettings.oAuth2.authorizationServerId);
            Assert.Equal(creatorConfig.api.authenticationSettings.oAuth2.scope, apiTemplateResource.properties.authenticationSettings.oAuth2.scope);
            Assert.Equal(creatorConfig.api.authenticationSettings.openid.openidProviderId, apiTemplateResource.properties.authenticationSettings.openid.openidProviderId);
            Assert.Equal(creatorConfig.api.authenticationSettings.openid.bearerTokenSendingMethods, apiTemplateResource.properties.authenticationSettings.openid.bearerTokenSendingMethods);
            Assert.Equal(creatorConfig.api.authenticationSettings.subscriptionKeyRequired, apiTemplateResource.properties.authenticationSettings.subscriptionKeyRequired);
        }
Пример #6
0
        public async void ShouldCreateSubsequentlAPITemplateResourceFromCreatorConfigWithAlternateTitleInOpenApi()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            CreatorConfig      creatorConfig      = new CreatorConfig()
            {
                apis = new List <APIConfig>()
            };

            // extract swagger as a local file

            var openapiPath = Path.GetTempFileName();

            using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("apimtemplate.test.Resources.swaggerPetstore.yml"))
                using (var reader = new StreamReader(stream))
                    File.WriteAllText(openapiPath, reader.ReadToEnd());

            // create API config with local swagger definition

            APIConfig api = new APIConfig()
            {
                name        = "name",
                displayName = "Swagger Petstore (alternate title)",
                openApiSpec = openapiPath,
            };

            creatorConfig.apis.Add(api);

            // act
            APITemplateResource apiTemplateResource = await apiTemplateCreator.CreateAPITemplateResourceAsync(api, true, true);

            // assert
            Assert.Equal($"[concat(parameters('ApimServiceName'), '/{api.name}')]", apiTemplateResource.name);
            Assert.Equal("openapi", apiTemplateResource.properties.format);

            // check alternate title has been specified in the embedded YAML or JSON definition

            var yaml         = apiTemplateResource.properties.value;
            var deserializer = new YamlDotNet.Serialization.Deserializer();
            var definition   = deserializer.Deserialize <Dictionary <string, object> >(yaml);
            var info         = (Dictionary <object, object>)definition["info"];

            Assert.Equal("Swagger Petstore (alternate title)", info["title"]);
        }
        public void ShouldCreateProtocolsFromOpenApiDocument()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            OpenApiDocument    openApiDocument    = new OpenApiDocument();
            int count = 2;

            for (int i = 0; i < count; i++)
            {
                openApiDocument.Servers.Add(new OpenApiServer()
                {
                    Url = $"{i}:{i}"
                });
            }

            // act
            string[] protocols = apiTemplateCreator.CreateProtocols(openApiDocument);

            // assert
            Assert.Equal(count, protocols.Length);
        }
Пример #8
0
        public void ShouldCreateSubsequentlAPITemplateResourceFromCreatorConfigWithCorrectContent()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            CreatorConfig      creatorConfig      = new CreatorConfig()
            {
                api = new APIConfig()
                {
                    name        = "name",
                    openApiSpec = "https://petstore.swagger.io/v2/swagger.json"
                }
            };

            // act
            APITemplateResource apiTemplateResource = apiTemplateCreator.CreateSubsequentAPITemplateResource(creatorConfig);

            // assert
            Assert.Equal($"[concat(parameters('ApimServiceName'), '/{creatorConfig.api.name}')]", apiTemplateResource.name);
            Assert.Equal("swagger-link-json", apiTemplateResource.properties.contentFormat);
            Assert.Equal(creatorConfig.api.openApiSpec, apiTemplateResource.properties.contentValue);
        }
Пример #9
0
        public async void ShouldUseDefaultVersionSetIdWithoutProvidedVersionSet()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            CreatorConfig      creatorConfig      = new CreatorConfig()
            {
                apiVersionSet = new APIVersionSetConfig()
                {
                },
                api = new APIConfig()
                {
                    openApiSpec = "https://petstore.swagger.io/v2/swagger.json"
                }
            };

            // act
            APITemplateResource apiTemplateResource = await apiTemplateCreator.CreateInitialAPITemplateResourceAsync(creatorConfig);

            // assert
            Assert.Equal("[resourceId('Microsoft.ApiManagement/service/api-version-sets', parameters('ApimServiceName'), 'versionset')]", apiTemplateResource.properties.apiVersionSetId);
        }
Пример #10
0
        public async void ShouldCreateSubsequentlAPITemplateResourceFromCreatorConfigWithCorrectContent()
        {
            // arrange
            APITemplateCreator apiTemplateCreator = APITemplateCreatorFactory.GenerateAPITemplateCreator();
            CreatorConfig      creatorConfig      = new CreatorConfig()
            {
                apis = new List <APIConfig>()
            };
            APIConfig api = new APIConfig()
            {
                name        = "name",
                openApiSpec = "https://petstore.swagger.io/v2/swagger.json"
            };

            creatorConfig.apis.Add(api);

            // act
            APITemplateResource apiTemplateResource = await apiTemplateCreator.CreateAPITemplateResourceAsync(api, true, true);

            // assert
            Assert.Equal($"[concat(parameters('ApimServiceName'), '/{api.name}')]", apiTemplateResource.name);
            Assert.Equal("swagger-link-json", apiTemplateResource.properties.format);
            Assert.Equal(api.openApiSpec, apiTemplateResource.properties.value);
        }