public void InvokeRegistrationForUnregisteredResourceProviders() { // Setup Mock<ResourceManagementClient> mockClient = new Mock<ResourceManagementClient>(); Mock<IProviderOperations> mockProvidersOperations = new Mock<IProviderOperations>(); mockClient.Setup(f => f.Providers).Returns(mockProvidersOperations.Object); mockProvidersOperations.Setup(f => f.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())).Returns( (string rp, CancellationToken token) => { ProviderGetResult r = new ProviderGetResult { Provider = new Provider { RegistrationState = RegistrationState.Registered.ToString() } }; return Task.FromResult(r); } ); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, compatibleUri); Dictionary<HttpRequestMessage, List<HttpResponseMessage>> mapping = new Dictionary<HttpRequestMessage, List<HttpResponseMessage>> { { request, new List<HttpResponseMessage> { new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("registered to use namespace") }, new HttpResponseMessage(HttpStatusCode.Accepted) { Content = new StringContent("Azure works!") } } } }; List<string> msgs = new List<string>(); RPRegistrationDelegatingHandler rpHandler = new RPRegistrationDelegatingHandler(() => mockClient.Object, s => msgs.Add(s)) { InnerHandler = new MockResponseDelegatingHandler(mapping) }; HttpClient httpClient = new HttpClient(rpHandler); // Test HttpResponseMessage response = httpClient.SendAsync(request).Result; // Assert Assert.True(msgs.Any(s => s.Equals("Succeeded to register resource provider 'microsoft.compute'"))); Assert.Equal(response.StatusCode, HttpStatusCode.Accepted); Assert.Equal(response.Content.ReadAsStringAsync().Result, "Azure works!"); }
public void DoesNotInvokeRegistrationForRegisteredResourceProviders() { // Setup Mock<ResourceManagementClient> mockClient = new Mock<ResourceManagementClient>(); Mock<IProviderOperations> mockProvidersOperations = new Mock<IProviderOperations>(); mockClient.Setup(f => f.Providers).Returns(mockProvidersOperations.Object); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, compatibleUri); Dictionary<HttpRequestMessage, List<HttpResponseMessage>> mapping = new Dictionary<HttpRequestMessage, List<HttpResponseMessage>> { { request, new List<HttpResponseMessage> { new HttpResponseMessage(HttpStatusCode.Accepted) { Content = new StringContent("Azure works!") } } } }; List<string> msgs = new List<string>(); RPRegistrationDelegatingHandler rpHandler = new RPRegistrationDelegatingHandler(() => mockClient.Object, s => msgs.Add(s)) { InnerHandler = new MockResponseDelegatingHandler(mapping) }; HttpClient httpClient = new HttpClient(rpHandler); // Test HttpResponseMessage response = httpClient.SendAsync(request).Result; // Assert Assert.Equal(0, msgs.Count); Assert.Equal(response.StatusCode, HttpStatusCode.Accepted); Assert.Equal(response.Content.ReadAsStringAsync().Result, "Azure works!"); }
public void DoesNotThrowForFailedRegistrationCall() { // Setup Mock<ResourceManagementClient> mockClient = new Mock<ResourceManagementClient>(); Mock<IProviderOperations> mockProvidersOperations = new Mock<IProviderOperations>(); mockClient.Setup(f => f.Providers).Returns(mockProvidersOperations.Object); mockProvidersOperations.Setup(f => f.GetAsync(It.IsAny<string>(), It.IsAny<CancellationToken>())) .Throws(new CloudException("PR reg failed")); HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, compatibleUri); Dictionary<HttpRequestMessage, List<HttpResponseMessage>> mapping = new Dictionary<HttpRequestMessage, List<HttpResponseMessage>> { { request, new List<HttpResponseMessage> { new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("registered to use namespace") }, new HttpResponseMessage(HttpStatusCode.Conflict) { Content = new StringContent("registered to use namespace") } } } }; List<string> msgs = new List<string>(); RPRegistrationDelegatingHandler rpHandler = new RPRegistrationDelegatingHandler(() => mockClient.Object, s => msgs.Add(s)) { InnerHandler = new MockResponseDelegatingHandler(mapping) }; HttpClient httpClient = new HttpClient(rpHandler); // Test HttpResponseMessage response = httpClient.SendAsync(request).Result; // Assert Assert.True(msgs.Any(s => s.Equals("Failed to register resource provider 'microsoft.compute'.Details: 'PR reg failed'"))); Assert.Equal(response.StatusCode, HttpStatusCode.Conflict); Assert.Equal(response.Content.ReadAsStringAsync().Result, "registered to use namespace"); mockProvidersOperations.Verify(f => f.RegisterAsync("microsoft.compute", It.IsAny<CancellationToken>()), Times.AtMost(4)); }