public void GetAlertSettingsNegativeTest() { using (UndoContext context = UndoContext.Current) { context.Start(); var client = GetSiteRecoveryClient(CustomHttpHandler); AlertSettingsResponse alertResponse = null; try { alertResponse = client.AlertSettings.Get( "dummyName", RequestHeaders); } catch (Exception ex) { if (ex.Message.ToUpper().Contains("NOTFOUND")) { alertResponse = new AlertSettingsResponse(); alertResponse.Alert = null; alertResponse.StatusCode = HttpStatusCode.NotFound; } } Assert.NotNull(alertResponse); Assert.Equal(alertResponse.StatusCode, HttpStatusCode.NotFound); } }
/// <summary> /// Updates the alert settings for the vault. /// </summary> /// <param name='alertSettingsName'> /// Required. Alert Settings name. /// </param> /// <param name='input'> /// Required. Configure Alerts Request. /// </param> /// <param name='customRequestHeaders'> /// Optional. Request header parameters. /// </param> /// <param name='cancellationToken'> /// Cancellation token. /// </param> /// <returns> /// Model class for alerts response. /// </returns> public async Task <AlertSettingsResponse> ConfigureAsync(string alertSettingsName, ConfigureAlertSettingsRequest input, CustomRequestHeaders customRequestHeaders, CancellationToken cancellationToken) { // Validate if (alertSettingsName == null) { throw new ArgumentNullException("alertSettingsName"); } if (input == null) { throw new ArgumentNullException("input"); } // Tracing bool shouldTrace = TracingAdapter.IsEnabled; string invocationId = null; if (shouldTrace) { invocationId = TracingAdapter.NextInvocationId.ToString(); Dictionary <string, object> tracingParameters = new Dictionary <string, object>(); tracingParameters.Add("alertSettingsName", alertSettingsName); tracingParameters.Add("input", input); tracingParameters.Add("customRequestHeaders", customRequestHeaders); TracingAdapter.Enter(invocationId, this, "ConfigureAsync", tracingParameters); } // Construct URL string url = ""; url = url + "/Subscriptions/"; if (this.Client.Credentials.SubscriptionId != null) { url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId); } url = url + "/resourceGroups/"; url = url + Uri.EscapeDataString(this.Client.ResourceGroupName); url = url + "/providers/"; url = url + Uri.EscapeDataString(this.Client.ResourceNamespace); url = url + "/"; url = url + Uri.EscapeDataString(this.Client.ResourceType); url = url + "/"; url = url + Uri.EscapeDataString(this.Client.ResourceName); url = url + "/replicationAlertSettings/"; url = url + Uri.EscapeDataString(alertSettingsName); List <string> queryParameters = new List <string>(); queryParameters.Add("api-version=2015-11-10"); if (queryParameters.Count > 0) { url = url + "?" + string.Join("&", queryParameters); } string baseUrl = this.Client.BaseUri.AbsoluteUri; // Trim '/' character from the end of baseUrl and beginning of url. if (baseUrl[baseUrl.Length - 1] == '/') { baseUrl = baseUrl.Substring(0, baseUrl.Length - 1); } if (url[0] == '/') { url = url.Substring(1); } url = baseUrl + "/" + url; url = url.Replace(" ", "%20"); // Create HTTP transport objects HttpRequestMessage httpRequest = null; try { httpRequest = new HttpRequestMessage(); httpRequest.Method = HttpMethod.Put; httpRequest.RequestUri = new Uri(url); // Set Headers httpRequest.Headers.Add("Accept-Language", customRequestHeaders.Culture); httpRequest.Headers.Add("Agent-Authentication", customRequestHeaders.AgentAuthenticationHeader); httpRequest.Headers.Add("x-ms-client-request-id", customRequestHeaders.ClientRequestId); httpRequest.Headers.Add("x-ms-version", "2015-01-01"); // Set Credentials cancellationToken.ThrowIfCancellationRequested(); await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false); // Serialize Request string requestContent = null; JToken requestDoc = null; JObject configureAlertSettingsRequestValue = new JObject(); requestDoc = configureAlertSettingsRequestValue; if (input.Properties != null) { JObject propertiesValue = new JObject(); configureAlertSettingsRequestValue["properties"] = propertiesValue; if (input.Properties.SendToOwners != null) { propertiesValue["sendToOwners"] = input.Properties.SendToOwners; } if (input.Properties.CustomEmailAddresses != null) { if (input.Properties.CustomEmailAddresses is ILazyCollection == false || ((ILazyCollection)input.Properties.CustomEmailAddresses).IsInitialized) { JArray customEmailAddressesArray = new JArray(); foreach (string customEmailAddressesItem in input.Properties.CustomEmailAddresses) { customEmailAddressesArray.Add(customEmailAddressesItem); } propertiesValue["customEmailAddresses"] = customEmailAddressesArray; } } if (input.Properties.Locale != null) { propertiesValue["locale"] = input.Properties.Locale; } } requestContent = requestDoc.ToString(Newtonsoft.Json.Formatting.Indented); httpRequest.Content = new StringContent(requestContent, Encoding.UTF8); httpRequest.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); // Send Request HttpResponseMessage httpResponse = null; try { if (shouldTrace) { TracingAdapter.SendRequest(invocationId, httpRequest); } cancellationToken.ThrowIfCancellationRequested(); httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false); if (shouldTrace) { TracingAdapter.ReceiveResponse(invocationId, httpResponse); } HttpStatusCode statusCode = httpResponse.StatusCode; if (statusCode != HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); CloudException ex = CloudException.Create(httpRequest, requestContent, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false)); if (shouldTrace) { TracingAdapter.Error(invocationId, ex); } throw ex; } // Create Result AlertSettingsResponse result = null; // Deserialize Response if (statusCode == HttpStatusCode.OK) { cancellationToken.ThrowIfCancellationRequested(); string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false); result = new AlertSettingsResponse(); JToken responseDoc = null; if (string.IsNullOrEmpty(responseContent) == false) { responseDoc = JToken.Parse(responseContent); } if (responseDoc != null && responseDoc.Type != JTokenType.Null) { AlertSettings alertInstance = new AlertSettings(); result.Alert = alertInstance; JToken propertiesValue2 = responseDoc["properties"]; if (propertiesValue2 != null && propertiesValue2.Type != JTokenType.Null) { AlertSettingsProperties propertiesInstance = new AlertSettingsProperties(); alertInstance.Properties = propertiesInstance; JToken sendToOwnersValue = propertiesValue2["sendToOwners"]; if (sendToOwnersValue != null && sendToOwnersValue.Type != JTokenType.Null) { string sendToOwnersInstance = ((string)sendToOwnersValue); propertiesInstance.SendToOwners = sendToOwnersInstance; } JToken customEmailAddressesArray2 = propertiesValue2["customEmailAddresses"]; if (customEmailAddressesArray2 != null && customEmailAddressesArray2.Type != JTokenType.Null) { foreach (JToken customEmailAddressesValue in ((JArray)customEmailAddressesArray2)) { propertiesInstance.CustomEmailAddresses.Add(((string)customEmailAddressesValue)); } } JToken localeValue = propertiesValue2["locale"]; if (localeValue != null && localeValue.Type != JTokenType.Null) { string localeInstance = ((string)localeValue); propertiesInstance.Locale = localeInstance; } } JToken idValue = responseDoc["id"]; if (idValue != null && idValue.Type != JTokenType.Null) { string idInstance = ((string)idValue); alertInstance.Id = idInstance; } JToken nameValue = responseDoc["name"]; if (nameValue != null && nameValue.Type != JTokenType.Null) { string nameInstance = ((string)nameValue); alertInstance.Name = nameInstance; } JToken typeValue = responseDoc["type"]; if (typeValue != null && typeValue.Type != JTokenType.Null) { string typeInstance = ((string)typeValue); alertInstance.Type = typeInstance; } JToken locationValue = responseDoc["location"]; if (locationValue != null && locationValue.Type != JTokenType.Null) { string locationInstance = ((string)locationValue); alertInstance.Location = locationInstance; } JToken tagsSequenceElement = ((JToken)responseDoc["tags"]); if (tagsSequenceElement != null && tagsSequenceElement.Type != JTokenType.Null) { foreach (JProperty property in tagsSequenceElement) { string tagsKey = ((string)property.Name); string tagsValue = ((string)property.Value); alertInstance.Tags.Add(tagsKey, tagsValue); } } JToken clientRequestIdValue = responseDoc["ClientRequestId"]; if (clientRequestIdValue != null && clientRequestIdValue.Type != JTokenType.Null) { string clientRequestIdInstance = ((string)clientRequestIdValue); result.ClientRequestId = clientRequestIdInstance; } JToken correlationRequestIdValue = responseDoc["CorrelationRequestId"]; if (correlationRequestIdValue != null && correlationRequestIdValue.Type != JTokenType.Null) { string correlationRequestIdInstance = ((string)correlationRequestIdValue); result.CorrelationRequestId = correlationRequestIdInstance; } JToken dateValue = responseDoc["Date"]; if (dateValue != null && dateValue.Type != JTokenType.Null) { string dateInstance = ((string)dateValue); result.Date = dateInstance; } JToken contentTypeValue = responseDoc["ContentType"]; if (contentTypeValue != null && contentTypeValue.Type != JTokenType.Null) { string contentTypeInstance = ((string)contentTypeValue); result.ContentType = contentTypeInstance; } } } result.StatusCode = statusCode; if (httpResponse.Content != null && httpResponse.Content.Headers.Contains("Content-Type")) { result.ContentType = httpResponse.Content.Headers.GetValues("Content-Type").FirstOrDefault(); } if (httpResponse.Headers.Contains("Date")) { result.Date = httpResponse.Headers.GetValues("Date").FirstOrDefault(); } if (httpResponse.Headers.Contains("x-ms-client-request-id")) { result.ClientRequestId = httpResponse.Headers.GetValues("x-ms-client-request-id").FirstOrDefault(); } if (httpResponse.Headers.Contains("x-ms-correlation-request-id")) { result.CorrelationRequestId = httpResponse.Headers.GetValues("x-ms-correlation-request-id").FirstOrDefault(); } if (httpResponse.Headers.Contains("x-ms-request-id")) { result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault(); } if (shouldTrace) { TracingAdapter.Exit(invocationId, result); } return(result); } finally { if (httpResponse != null) { httpResponse.Dispose(); } } } finally { if (httpRequest != null) { httpRequest.Dispose(); } } }