Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for
// license information.
// using ApiManagement.Management.Tests;

using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
using Microsoft.Azure.Management.ApiManagement;
using Microsoft.Azure.Management.ApiManagement.Models;
using Xunit;
using System.Linq;
using System.Threading.Tasks;
using System;
using System.Collections.Generic;
using Microsoft.Rest.Azure;

namespace ApiManagement.Tests.ManagementApiTests
{
public class ApiDiagnosticTests : TestBase
{
[Fact]
public async Task CreateListUpdateDelete()
{
Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
var testBase = new ApiManagementTestBase(context);
testBase.TryCreateApiManagementService();

// list all the APIs
IPage<ApiContract> apiResponse = testBase.client.Api.ListByService(
testBase.rgName,
testBase.serviceName,
null);
Assert.NotNull(apiResponse);
Assert.Single(apiResponse);
Assert.Null(apiResponse.NextPageLink);

//api to use
ApiContract apiToUse = apiResponse.First();

// list diagnostics: there should be none for the Api currently
var apiDiagnostics = testBase.client.ApiDiagnostic.ListByService(
testBase.rgName,
testBase.serviceName,
apiToUse.Name);

Assert.NotNull(apiDiagnostics);
Assert.Empty(apiDiagnostics);

// create new diagnostic, supported Ids are applicationinsights, azuremonitor
string apiDiagnosticId = "applicationinsights";
string loggerId = TestUtilities.GenerateName("appInsights");

try
{
// create a logger
Guid applicationInsightsGuid = TestUtilities.GenerateGuid("appInsights");
var credentials = new Dictionary<string, string>();
credentials.Add("instrumentationKey", applicationInsightsGuid.ToString());

var loggerCreateParameters = new LoggerContract(LoggerType.ApplicationInsights, credentials);
var loggerContract = await testBase.client.Logger.CreateOrUpdateAsync(
testBase.rgName,
testBase.serviceName,
loggerId,
loggerCreateParameters);
Assert.NotNull(loggerContract);
Assert.Equal(loggerId, loggerContract.Name);
Assert.Equal(LoggerType.ApplicationInsights, loggerContract.LoggerType);
Assert.NotNull(loggerContract.Credentials);
Assert.Equal(1, loggerContract.Credentials.Keys.Count);

// create a diagnostic entity with just loggerId
var diagnosticContractParams = new DiagnosticContract()
{
LoggerId = loggerContract.Id
};
var apiDiagnosticContract = await testBase.client.ApiDiagnostic.CreateOrUpdateAsync(
testBase.rgName,
testBase.serviceName,
apiToUse.Name,
apiDiagnosticId,
diagnosticContractParams);
Assert.NotNull(apiDiagnosticContract);
Assert.Equal(apiDiagnosticId, apiDiagnosticContract.Name);

// check the diagnostic entity etag
var apiDiagnosticTag = await testBase.client.ApiDiagnostic.GetEntityTagAsync(
testBase.rgName,
testBase.serviceName,
apiToUse.Name,
apiDiagnosticId);
Assert.NotNull(apiDiagnosticTag);
Assert.NotNull(apiDiagnosticTag.ETag);

// now update the sampling and other settings of the diagnostic
diagnosticContractParams.EnableHttpCorrelationHeaders = true;
diagnosticContractParams.AlwaysLog = "allErrors";
diagnosticContractParams.Sampling = new SamplingSettings("fixed", 50);
var listOfHeaders = new List<string> { "Content-type" };
var bodyDiagnostic = new BodyDiagnosticSettings(512);
diagnosticContractParams.Frontend = new PipelineDiagnosticSettings
{
Request = new HttpMessageDiagnostic()
{
Body = bodyDiagnostic,
Headers = listOfHeaders
},
Response = new HttpMessageDiagnostic()
{
Body = bodyDiagnostic,
Headers = listOfHeaders
}
};
diagnosticContractParams.Backend = new PipelineDiagnosticSettings
{
Request = new HttpMessageDiagnostic()
{
Body = bodyDiagnostic,
Headers = listOfHeaders
},
Response = new HttpMessageDiagnostic()
{
Body = bodyDiagnostic,
Headers = listOfHeaders
}
};

var updatedApiDiagnostic = await testBase.client.ApiDiagnostic.CreateOrUpdateWithHttpMessagesAsync(
testBase.rgName,
testBase.serviceName,
apiToUse.Name,
apiDiagnosticId,
diagnosticContractParams,
apiDiagnosticTag.ETag);
Assert.NotNull(updatedApiDiagnostic);
Assert.True(updatedApiDiagnostic.Body.EnableHttpCorrelationHeaders.Value);
Assert.Equal("allErrors", updatedApiDiagnostic.Body.AlwaysLog);
Assert.NotNull(updatedApiDiagnostic.Body.Sampling);
Assert.NotNull(updatedApiDiagnostic.Body.Frontend);
Assert.NotNull(updatedApiDiagnostic.Body.Backend);

// delete the diagnostic entity
await testBase.client.ApiDiagnostic.DeleteAsync(
testBase.rgName,
testBase.serviceName,
apiToUse.Name,
apiDiagnosticId,
updatedApiDiagnostic.Headers.ETag);

Assert.Throws<ErrorResponseException>(()
=> testBase.client.ApiDiagnostic.GetEntityTag(
testBase.rgName,
testBase.serviceName,
apiToUse.Name,
apiDiagnosticId));

// check the logger entity etag
var loggerTag = await testBase.client.Logger.GetEntityTagAsync(
testBase.rgName,
testBase.serviceName,
loggerId);
Assert.NotNull(loggerTag);
Assert.NotNull(loggerTag.ETag);

// delete the logger entity
await testBase.client.Logger.DeleteAsync(
testBase.rgName,
testBase.serviceName,
loggerId,
loggerTag.ETag);

Assert.Throws<ErrorResponseException>(()
=> testBase.client.Logger.GetEntityTag(testBase.rgName, testBase.serviceName, loggerId));
}
finally
{
testBase.client.ApiDiagnostic.Delete(
testBase.rgName,
testBase.serviceName,
apiToUse.Name,
apiDiagnosticId,
"*");
testBase.client.Logger.Delete(testBase.rgName, testBase.serviceName, loggerId, "*");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Azure.Management.ApiManagement;
using Microsoft.Azure.Management.ApiManagement.Models;
using Microsoft.Rest.ClientRuntime.Azure.TestFramework;
Expand Down Expand Up @@ -40,8 +39,8 @@ public void SwaggerTest()
var apiCreateOrUpdate = new ApiCreateOrUpdateParameter()
{
Path = path,
ContentFormat = ContentFormat.SwaggerJson,
ContentValue = swaggerApiContent
Format = ContentFormat.SwaggerJson,
Value = swaggerApiContent
};

var swaggerApiResponse = testBase.client.Api.CreateOrUpdate(
Expand All @@ -64,7 +63,8 @@ public void SwaggerTest()
ApiExportResult swaggerExport = testBase.client.ApiExport.Get(testBase.rgName, testBase.serviceName, swaggerApi, ExportFormat.Swagger);

Assert.NotNull(swaggerExport);
Assert.NotNull(swaggerExport.Link);
Assert.NotNull(swaggerExport.Value.Link);
Assert.Equal("swagger-link-json", swaggerExport.ExportResultFormat);
}
finally
{
Expand Down Expand Up @@ -100,8 +100,8 @@ public void WadlTest()
var apiCreateOrUpdate = new ApiCreateOrUpdateParameter()
{
Path = path,
ContentFormat = ContentFormat.WadlXml,
ContentValue = wadlApiContent
Format = ContentFormat.WadlXml,
Value = wadlApiContent
};

var wadlApiResponse = testBase.client.Api.CreateOrUpdate(
Expand All @@ -127,7 +127,8 @@ public void WadlTest()
ApiExportResult wadlExport = testBase.client.ApiExport.Get(testBase.rgName, testBase.serviceName, wadlApi, ExportFormat.Wadl);

Assert.NotNull(wadlExport);
Assert.NotNull(wadlExport.Link);
Assert.NotNull(wadlExport.Value.Link);
Assert.Equal("wadl-link-json", wadlExport.ExportResultFormat);
}
finally
{
Expand Down Expand Up @@ -164,8 +165,8 @@ public void WsdlTest()
var apiCreateOrUpdate = new ApiCreateOrUpdateParameter()
{
Path = path,
ContentFormat = ContentFormat.Wsdl,
ContentValue = wsdlApiContent,
Format = ContentFormat.Wsdl,
Value = wsdlApiContent,
SoapApiType = SoapApiType.SoapPassThrough, // create Soap Pass through API
WsdlSelector = new ApiCreateOrUpdatePropertiesWsdlSelector()
{
Expand Down Expand Up @@ -198,14 +199,17 @@ public void WsdlTest()
Assert.True(apiContract.Protocols.Contains(Protocol.Https));
Assert.Equal("1", apiContract.ApiRevision);

/* WSDL Export spits our broken Json
ApiExportResult wsdlExport = testBase.client.ApiExport.Get(
testBase.rgName,
testBase.serviceName,
wsdlApi,
ExportFormat.Wsdl);

Assert.NotNull(wsdlExport);
Assert.NotNull(wsdlExport.Link);
Assert.NotNull(wsdlExport.Value.Link);
Assert.Equal("wsdl-link+xml", wsdlExport.ExportResultFormat);
*/
}
finally
{
Expand All @@ -218,5 +222,66 @@ public void WsdlTest()
}
}
}

[Fact]
public void OpenApiTest()
{
Environment.SetEnvironmentVariable("AZURE_TEST_MODE", "Playback");
using (MockContext context = MockContext.Start(this.GetType().FullName))
{
var testBase = new ApiManagementTestBase(context);
testBase.TryCreateApiManagementService();

const string openapiFilePath = "./Resources/petstore.yaml";
const string path = "openapi3";
string openApiId = TestUtilities.GenerateName("aid");

try
{
// import API
string openApiContent;
using (StreamReader reader = File.OpenText(openapiFilePath))
{
openApiContent = reader.ReadToEnd();
}

var apiCreateOrUpdate = new ApiCreateOrUpdateParameter()
{
Path = path,
Format = ContentFormat.Openapi,
Value = openApiContent
};

var swaggerApiResponse = testBase.client.Api.CreateOrUpdate(
testBase.rgName,
testBase.serviceName,
openApiId,
apiCreateOrUpdate);

Assert.NotNull(swaggerApiResponse);

// get the api to check it was created
var getResponse = testBase.client.Api.Get(testBase.rgName, testBase.serviceName, openApiId);

Assert.NotNull(getResponse);
Assert.Equal(openApiId, getResponse.Name);
Assert.Equal(path, getResponse.Path);
Assert.Equal("Swagger Petstore", getResponse.DisplayName);
Assert.Equal("http://petstore.swagger.io/v1", getResponse.ServiceUrl);

ApiExportResult openApiExport = testBase.client.ApiExport.Get(testBase.rgName, testBase.serviceName, openApiId, ExportFormat.Openapi);

Assert.NotNull(openApiExport);
Assert.NotNull(openApiExport.Value.Link);
Assert.Equal("openapi-link", openApiExport.ExportResultFormat);
}
finally
{
// remove the API
testBase.client.Api.Delete(testBase.rgName, testBase.serviceName, openApiId, "*");
}

}
}
}
}
Loading