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 var listResponse = testBase.client.Api.ListByService( testBase.rgName, testBase.serviceName, null); Assert.NotNull(listResponse); Assert.Single(listResponse); Assert.Null(listResponse.NextPageLink); var echoApi = listResponse.First(); Assert.Equal("Echo API", echoApi.DisplayName); Assert.Null(echoApi.Description); Assert.Equal("http://echoapi.cloudapp.net/api", echoApi.ServiceUrl); Assert.Equal("echo", echoApi.Path); Assert.NotNull(echoApi.Protocols); Assert.Equal(1, echoApi.Protocols.Count); Assert.Equal(Protocol.Https, echoApi.Protocols[0]); // get the API by id var getResponse = await testBase.client.Api.GetWithHttpMessagesAsync( testBase.rgName, testBase.serviceName, echoApi.Name); Assert.NotNull(getResponse); Assert.Equal("Echo API", getResponse.Body.DisplayName); Assert.Null(getResponse.Body.Description); Assert.Equal("http://echoapi.cloudapp.net/api", getResponse.Body.ServiceUrl); Assert.Equal("echo", getResponse.Body.Path); Assert.NotNull(getResponse.Body.Protocols); Assert.Equal(1, getResponse.Body.Protocols.Count); Assert.Equal(Protocol.Https, getResponse.Body.Protocols[0]); // add new api // create autorization server string newApiAuthorizationServerId = TestUtilities.GenerateName("authorizationServerId"); string newApiId = TestUtilities.GenerateName("apiid"); string newOpenApiId = TestUtilities.GenerateName("openApiid"); string openIdNoSecret = TestUtilities.GenerateName("openId"); try { var createAuthServerParams = new AuthorizationServerContract { DisplayName = TestUtilities.GenerateName("authName"), DefaultScope = TestUtilities.GenerateName("oauth2scope"), AuthorizationEndpoint = "https://contoso.com/auth", TokenEndpoint = "https://contoso.com/token", ClientRegistrationEndpoint = "https://contoso.com/clients/reg", GrantTypes = new List <string> { GrantType.AuthorizationCode, GrantType.Implicit }, AuthorizationMethods = new List <AuthorizationMethod?> { AuthorizationMethod.POST, AuthorizationMethod.GET }, BearerTokenSendingMethods = new List <string> { BearerTokenSendingMethod.AuthorizationHeader, BearerTokenSendingMethod.Query }, ClientId = TestUtilities.GenerateName("clientid") }; testBase.client.AuthorizationServer.CreateOrUpdate( testBase.rgName, testBase.serviceName, newApiAuthorizationServerId, createAuthServerParams); string newApiName = TestUtilities.GenerateName("apiname"); string newApiDescription = TestUtilities.GenerateName("apidescription"); string newApiPath = "newapiPath"; string newApiServiceUrl = "http://newechoapi.cloudapp.net/api"; string subscriptionKeyParametersHeader = TestUtilities.GenerateName("header"); string subscriptionKeyQueryStringParamName = TestUtilities.GenerateName("query"); string newApiAuthorizationScope = TestUtilities.GenerateName("oauth2scope"); var newApiAuthenticationSettings = new AuthenticationSettingsContract { OAuth2 = new OAuth2AuthenticationSettingsContract { AuthorizationServerId = newApiAuthorizationServerId, Scope = newApiAuthorizationScope } }; var createdApiContract = testBase.client.Api.CreateOrUpdate( testBase.rgName, testBase.serviceName, newApiId, new ApiCreateOrUpdateParameter { DisplayName = newApiName, Description = newApiDescription, Path = newApiPath, ServiceUrl = newApiServiceUrl, Protocols = new List <Protocol?> { Protocol.Https, Protocol.Http }, SubscriptionKeyParameterNames = new SubscriptionKeyParameterNamesContract { Header = subscriptionKeyParametersHeader, Query = subscriptionKeyQueryStringParamName }, AuthenticationSettings = newApiAuthenticationSettings }); // get new api to check it was added var apiGetResponse = testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiGetResponse); Assert.Equal(newApiId, apiGetResponse.Name); Assert.Equal(newApiName, apiGetResponse.DisplayName); Assert.Equal(newApiDescription, apiGetResponse.Description); Assert.Equal(newApiPath, apiGetResponse.Path); Assert.Equal(newApiServiceUrl, apiGetResponse.ServiceUrl); Assert.Equal(subscriptionKeyParametersHeader, apiGetResponse.SubscriptionKeyParameterNames.Header); Assert.Equal(subscriptionKeyQueryStringParamName, apiGetResponse.SubscriptionKeyParameterNames.Query); Assert.Equal(2, apiGetResponse.Protocols.Count); Assert.True(apiGetResponse.Protocols.Contains(Protocol.Http)); Assert.True(apiGetResponse.Protocols.Contains(Protocol.Https)); Assert.NotNull(apiGetResponse.AuthenticationSettings); Assert.NotNull(apiGetResponse.AuthenticationSettings.OAuth2); Assert.Equal(newApiAuthorizationServerId, apiGetResponse.AuthenticationSettings.OAuth2.AuthorizationServerId); // get the API Entity Tag ApiGetEntityTagHeaders apiTag = testBase.client.Api.GetEntityTag( testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiTag); Assert.NotNull(apiTag.ETag); // patch added api string patchedName = TestUtilities.GenerateName("patchedname"); string patchedDescription = TestUtilities.GenerateName("patchedDescription"); string patchedPath = TestUtilities.GenerateName("patchedPath"); testBase.client.Api.Update( testBase.rgName, testBase.serviceName, newApiId, new ApiUpdateContract { DisplayName = patchedName, Description = patchedDescription, Path = patchedPath, AuthenticationSettings = new AuthenticationSettingsContract { OAuth2 = null } }, apiTag.ETag); // get patched api to check it was patched apiGetResponse = testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiGetResponse); Assert.Equal(newApiId, apiGetResponse.Name); Assert.Equal(patchedName, apiGetResponse.DisplayName); Assert.Equal(patchedDescription, apiGetResponse.Description); Assert.Equal(patchedPath, apiGetResponse.Path); Assert.Equal(newApiServiceUrl, apiGetResponse.ServiceUrl); Assert.Equal(subscriptionKeyParametersHeader, apiGetResponse.SubscriptionKeyParameterNames.Header); Assert.Equal(subscriptionKeyQueryStringParamName, apiGetResponse.SubscriptionKeyParameterNames.Query); Assert.Equal(2, apiGetResponse.Protocols.Count); Assert.True(apiGetResponse.Protocols.Contains(Protocol.Http)); Assert.True(apiGetResponse.Protocols.Contains(Protocol.Https)); // add an api with OpenId authentication settings // create a openId connect provider string openIdProviderName = TestUtilities.GenerateName("openIdName"); string metadataEndpoint = testBase.GetOpenIdMetadataEndpointUrl(); string clientId = TestUtilities.GenerateName("clientId"); var openIdConnectCreateParameters = new OpenidConnectProviderContract(openIdProviderName, metadataEndpoint, clientId); var openIdCreateResponse = testBase.client.OpenIdConnectProvider.CreateOrUpdate( testBase.rgName, testBase.serviceName, openIdNoSecret, openIdConnectCreateParameters); Assert.NotNull(openIdCreateResponse); Assert.Equal(openIdProviderName, openIdCreateResponse.DisplayName); Assert.Equal(openIdNoSecret, openIdCreateResponse.Name); string newOpenIdApiName = TestUtilities.GenerateName("apiname"); string newOpenIdApiDescription = TestUtilities.GenerateName("apidescription"); string newOpenIdApiPath = "newOpenapiPath"; string newOpenIdApiServiceUrl = "http://newechoapi2.cloudapp.net/api"; string newOpenIdAuthorizationScope = TestUtilities.GenerateName("oauth2scope"); var newnewOpenIdAuthenticationSettings = new AuthenticationSettingsContract { Openid = new OpenIdAuthenticationSettingsContract { OpenidProviderId = openIdCreateResponse.Name, BearerTokenSendingMethods = new[] { BearerTokenSendingMethods.AuthorizationHeader } } }; var createdOpenApiIdContract = testBase.client.Api.CreateOrUpdate( testBase.rgName, testBase.serviceName, newOpenApiId, new ApiCreateOrUpdateParameter { DisplayName = newOpenIdApiName, Description = newOpenIdApiDescription, Path = newOpenIdApiPath, ServiceUrl = newOpenIdApiServiceUrl, Protocols = new List <Protocol?> { Protocol.Https, Protocol.Http }, SubscriptionKeyParameterNames = new SubscriptionKeyParameterNamesContract { Header = subscriptionKeyParametersHeader, Query = subscriptionKeyQueryStringParamName }, AuthenticationSettings = newnewOpenIdAuthenticationSettings }); // get new api to check it was added var openApiGetResponse = testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newOpenApiId); Assert.NotNull(openApiGetResponse); Assert.Equal(newOpenApiId, openApiGetResponse.Name); Assert.Equal(newOpenIdApiName, openApiGetResponse.DisplayName); Assert.Equal(newOpenIdApiDescription, openApiGetResponse.Description); Assert.Equal(newOpenIdApiPath, openApiGetResponse.Path); Assert.Equal(newOpenIdApiServiceUrl, openApiGetResponse.ServiceUrl); Assert.Equal(subscriptionKeyParametersHeader, openApiGetResponse.SubscriptionKeyParameterNames.Header); Assert.Equal(subscriptionKeyQueryStringParamName, openApiGetResponse.SubscriptionKeyParameterNames.Query); Assert.Equal(2, openApiGetResponse.Protocols.Count); Assert.True(openApiGetResponse.Protocols.Contains(Protocol.Http)); Assert.True(openApiGetResponse.Protocols.Contains(Protocol.Https)); Assert.NotNull(openApiGetResponse.AuthenticationSettings.Openid); Assert.Equal(openIdCreateResponse.Name, openApiGetResponse.AuthenticationSettings.Openid.OpenidProviderId); // list with paging listResponse = testBase.client.Api.ListByService( testBase.rgName, testBase.serviceName, new Microsoft.Rest.Azure.OData.ODataQuery <ApiContract> { Top = 1 }); Assert.NotNull(listResponse); Assert.Single(listResponse); Assert.NotNull(listResponse.NextPageLink); listResponse = testBase.client.Api.ListByServiceNext(listResponse.NextPageLink); Assert.NotNull(listResponse); Assert.Single(listResponse); Assert.NotNull(listResponse.NextPageLink); // delete the api testBase.client.Api.Delete( testBase.rgName, testBase.serviceName, newApiId, "*"); // get the deleted api to make sure it was deleted try { testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId); throw new Exception("This code should not have been executed."); } catch (ErrorResponseException ex) { Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); } // delete the api testBase.client.Api.Delete( testBase.rgName, testBase.serviceName, newOpenApiId, "*"); // get the deleted api to make sure it was deleted try { testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newOpenApiId); throw new Exception("This code should not have been executed."); } catch (ErrorResponseException ex) { Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); } } finally { // delete api server testBase.client.Api.Delete( testBase.rgName, testBase.serviceName, newApiId, "*"); testBase.client.AuthorizationServer.Delete( testBase.rgName, testBase.serviceName, newApiAuthorizationServerId, "*"); // delete api server testBase.client.Api.Delete( testBase.rgName, testBase.serviceName, newOpenApiId, "*"); testBase.client.OpenIdConnectProvider.Delete( testBase.rgName, testBase.serviceName, openIdNoSecret, "*"); } } }
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(); // add new api const string swaggerPath = "./Resources/SwaggerPetStoreV2.json"; const string path = "swaggerApi"; string newApiId = TestUtilities.GenerateName("apiid"); string newReleaseId = TestUtilities.GenerateName("apireleaseid"); try { // import API string swaggerApiContent; using (StreamReader reader = File.OpenText(swaggerPath)) { swaggerApiContent = reader.ReadToEnd(); } var apiCreateOrUpdate = new ApiCreateOrUpdateParameter() { Path = path, Format = ContentFormat.SwaggerJson, Value = swaggerApiContent }; var swaggerApiResponse = testBase.client.Api.CreateOrUpdate( testBase.rgName, testBase.serviceName, newApiId, apiCreateOrUpdate); Assert.NotNull(swaggerApiResponse); // get new api to check it was added var petstoreApiContract = testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(petstoreApiContract); Assert.Equal(path, petstoreApiContract.Path); Assert.Equal("Swagger Petstore Extensive", petstoreApiContract.DisplayName); Assert.Equal("http://petstore.swagger.wordnik.com/api", petstoreApiContract.ServiceUrl); // test the number of operations it has var petstoreApiOperations = testBase.client.ApiOperation.ListByApi( testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(petstoreApiOperations); Assert.NotEmpty(petstoreApiOperations); // get the API Entity Tag ApiGetEntityTagHeaders apiTag = testBase.client.Api.GetEntityTag( testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiTag); Assert.NotNull(apiTag.ETag); // add an api revision string revisionNumber = "2"; // create a revision of Petstore. // Please note we can change the following properties when creating a revision // DisplayName, Description, Path, Protocols, ApiVersion, ApiVersionDescription and ApiVersionSetId var revisionDescription = "Petstore second revision"; var petstoreRevisionContract = new ApiCreateOrUpdateParameter() { Path = petstoreApiContract.Path, DisplayName = petstoreApiContract.DisplayName, ServiceUrl = petstoreApiContract.ServiceUrl + revisionNumber, Protocols = petstoreApiContract.Protocols, SubscriptionKeyParameterNames = petstoreApiContract.SubscriptionKeyParameterNames, AuthenticationSettings = petstoreApiContract.AuthenticationSettings, Description = petstoreApiContract.Description, ApiRevisionDescription = revisionDescription, IsCurrent = false, SourceApiId = petstoreApiContract.Id // with this we ensure to copy all operations, tags, product association. }; var petStoreSecondRevision = await testBase.client.Api.CreateOrUpdateAsync( testBase.rgName, testBase.serviceName, newApiId.ApiRevisionIdentifier(revisionNumber), petstoreRevisionContract); Assert.NotNull(petStoreSecondRevision); Assert.Equal(petstoreApiContract.Path, petStoreSecondRevision.Path); Assert.Equal(petstoreRevisionContract.ServiceUrl, petStoreSecondRevision.ServiceUrl); Assert.Equal(revisionNumber, petStoreSecondRevision.ApiRevision); Assert.Equal(petstoreApiContract.DisplayName, petStoreSecondRevision.DisplayName); Assert.Equal(petstoreApiContract.Description, petStoreSecondRevision.Description); Assert.Equal(revisionDescription, petStoreSecondRevision.ApiRevisionDescription); // add an operation to this revision var newOperationId = TestUtilities.GenerateName("firstOpRev"); var firstOperationContract = testBase.CreateOperationContract("POST"); var firstOperation = await testBase.client.ApiOperation.CreateOrUpdateAsync( testBase.rgName, testBase.serviceName, newApiId.ApiRevisionIdentifier(revisionNumber), newOperationId, firstOperationContract); Assert.NotNull(firstOperation); Assert.Equal("POST", firstOperation.Method); // now test out list operation on the revision api var allRevisionOperations = await testBase.client.ApiOperation.ListByApiAsync( testBase.rgName, testBase.serviceName, newApiId.ApiRevisionIdentifier(revisionNumber)); Assert.NotNull(allRevisionOperations); // we copied the revision and added an extra "POST" operation to second revision Assert.Equal(allRevisionOperations.Count(), petstoreApiOperations.Count() + 1); // now test out list operation on the revision api var firstOperationOfSecondRevision = await testBase.client.ApiOperation.ListByApiAsync( testBase.rgName, testBase.serviceName, newApiId.ApiRevisionIdentifier(revisionNumber), new Microsoft.Rest.Azure.OData.ODataQuery <OperationContract> { Top = 1 }); Assert.NotNull(firstOperationOfSecondRevision); Assert.Single(firstOperationOfSecondRevision); Assert.NotEmpty(firstOperationOfSecondRevision.NextPageLink); // now test whether the next page link works var secondOperationOfSecondRevision = await testBase.client.ApiOperation.ListByApiNextAsync( firstOperationOfSecondRevision.NextPageLink); Assert.NotNull(secondOperationOfSecondRevision); Assert.Single(secondOperationOfSecondRevision); Assert.NotEmpty(secondOperationOfSecondRevision.NextPageLink); // list apiRevision IPage <ApiRevisionContract> apiRevisions = await testBase.client.ApiRevision.ListByServiceAsync( testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiRevisions); Assert.Equal(2, apiRevisions.GetEnumerator().ToIEnumerable <ApiRevisionContract>().Count()); Assert.NotEmpty(apiRevisions.GetEnumerator().ToIEnumerable().Single(d => d.ApiRevision.Equals(revisionNumber)).ApiRevision); Assert.Single(apiRevisions.GetEnumerator().ToIEnumerable().Where(a => a.IsCurrent.HasValue && a.IsCurrent.Value)); // there is only one revision which is current // get the etag of the revision var apiSecondRevisionTag = await testBase.client.Api.GetEntityTagAsync( testBase.rgName, testBase.serviceName, newApiId.ApiRevisionIdentifier(revisionNumber)); var apiOnlineRevisionTag = await testBase.client.Api.GetEntityTagAsync( testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiSecondRevisionTag); Assert.NotNull(apiOnlineRevisionTag); Assert.NotEqual(apiOnlineRevisionTag.ETag, apiSecondRevisionTag.ETag); //there should be no release intially var apiReleases = await testBase.client.ApiRelease.ListByServiceAsync( testBase.rgName, testBase.serviceName, newApiId); Assert.Empty(apiReleases); // lets create a release now and make the second revision as current var apiReleaseContract = new ApiReleaseContract() { ApiId = newApiId.ApiRevisionIdentifierFullPath(revisionNumber), Notes = TestUtilities.GenerateName("revision_description") }; var newapiBackendRelease = await testBase.client.ApiRelease.CreateOrUpdateAsync( testBase.rgName, testBase.serviceName, newApiId, newReleaseId, apiReleaseContract); Assert.NotNull(newapiBackendRelease); Assert.Equal(newReleaseId, newapiBackendRelease.Name); Assert.Equal(apiReleaseContract.Notes, newapiBackendRelease.Notes); // get the release eta var releaseTag = await testBase.client.ApiRelease.GetEntityTagAsync( testBase.rgName, testBase.serviceName, newApiId, newReleaseId); Assert.NotNull(releaseTag); // update the release details apiReleaseContract.Notes = TestUtilities.GenerateName("update_desc"); await testBase.client.ApiRelease.UpdateAsync( testBase.rgName, testBase.serviceName, newApiId, newReleaseId, apiReleaseContract, releaseTag.ETag); // get the release detaild newapiBackendRelease = await testBase.client.ApiRelease.GetAsync( testBase.rgName, testBase.serviceName, newApiId, newReleaseId); Assert.NotNull(newapiBackendRelease); Assert.Equal(newapiBackendRelease.Notes, apiReleaseContract.Notes); // list the revision apiReleases = await testBase.client.ApiRelease.ListByServiceAsync( testBase.rgName, testBase.serviceName, newApiId); Assert.NotNull(apiReleases); Assert.Single(apiReleases); // find the revision which is not online var revisions = await testBase.client.ApiRevision.ListByServiceAsync( testBase.rgName, testBase.serviceName, newApiId, new Microsoft.Rest.Azure.OData.ODataQuery <ApiRevisionContract> { Top = 1 }); Assert.NotNull(revisions); Assert.Single(revisions); // delete the api await testBase.client.Api.DeleteAsync( testBase.rgName, testBase.serviceName, newApiId, "*", deleteRevisions : true); // get the deleted api to make sure it was deleted try { testBase.client.Api.Get(testBase.rgName, testBase.serviceName, newApiId); throw new Exception("This code should not have been executed."); } catch (ErrorResponseException ex) { Assert.Equal(HttpStatusCode.NotFound, ex.Response.StatusCode); } } finally { // delete authorization server testBase.client.Api.Delete( testBase.rgName, testBase.serviceName, newApiId, "*", deleteRevisions: true); testBase.client.ApiRelease.Delete( testBase.rgName, testBase.serviceName, newApiId, newReleaseId, "*"); } } }