/// <summary> /// Get Notification Settings. /// </summary> /// <param name="expectedResponseStatusCode">expectedResponseStatusCode</param> /// <param name="expectedRequestResult">expectedRequestResult</param> /// <returns></returns> private Tuple <RequestResult <NotificationSettingsResponse>, RequestResult <NotificationSettingsResponse> > GetNotificationSettings(HttpStatusCode expectedResponseStatusCode, RequestResult <NotificationSettingsResponse> expectedRequestResult, bool throwException = false) { string json = @"{""smsEnabled"": true, ""smsCellNumber"": ""5551231234"", ""smsVerified"": true, ""smsScope"": [""COVID19""], ""emailEnabled"": true, ""emailAddress"": ""*****@*****.**"", ""emailScope"": [""COVID19""]}"; var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, IgnoreNullValues = true, WriteIndented = true, }; expectedRequestResult.ResourcePayload = JsonSerializer.Deserialize <NotificationSettingsResponse>(json, options); var handlerMock = new Mock <HttpMessageHandler>(); handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = expectedResponseStatusCode, Content = throwException ? null : new StringContent(json), }) .Verifiable(); using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); Mock <IHttpClientService> mockHttpClientService = new Mock <IHttpClientService>(); mockHttpClientService.Setup(s => s.CreateDefaultHttpClient()).Returns(() => new HttpClient(handlerMock.Object)); INotificationSettingsDelegate nsDelegate = new RestNotificationSettingsDelegate(loggerFactory.CreateLogger <RestNotificationSettingsDelegate>(), mockHttpClientService.Object, this.configuration); RequestResult <NotificationSettingsResponse> actualResult = Task.Run(async() => await nsDelegate.GetNotificationSettings(string.Empty)).Result; return(new Tuple <RequestResult <NotificationSettingsResponse>, RequestResult <NotificationSettingsResponse> >(actualResult, expectedRequestResult)); }
public void ValidateGetNotificationSettings204() { RequestResult <NotificationSettingsResponse> expected = new RequestResult <NotificationSettingsResponse>() { ResourcePayload = new NotificationSettingsResponse(), ResultStatus = Common.Constants.ResultType.Success, TotalResultCount = 0, }; var handlerMock = new Mock <HttpMessageHandler>(); handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.NoContent, Content = new StringContent(string.Empty), }) .Verifiable(); using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); Mock <IHttpClientService> mockHttpClientService = new Mock <IHttpClientService>(); mockHttpClientService.Setup(s => s.CreateDefaultHttpClient()).Returns(() => new HttpClient(handlerMock.Object)); INotificationSettingsDelegate nsDelegate = new RestNotificationSettingsDelegate(loggerFactory.CreateLogger <RestNotificationSettingsDelegate>(), mockHttpClientService.Object, this.configuration); RequestResult <NotificationSettingsResponse> actualResult = Task.Run(async() => await nsDelegate.GetNotificationSettings(string.Empty)).Result; Assert.True(actualResult.IsDeepEqual(expected)); }
public void ValidateGetNotificationSettings403() { RequestResult <NotificationSettingsResponse> expected = new RequestResult <NotificationSettingsResponse>() { ResultStatus = Common.Constants.ResultType.Error, ResultError = new RequestResultError() { ResultMessage = "DID Claim is missing or can not resolve PHN, HTTP Error Forbidden", ErrorCode = ErrorTranslator.ServiceError(ErrorType.CommunicationExternal, ServiceType.PHSA) }, }; var handlerMock = new Mock <HttpMessageHandler>(); handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.Forbidden, Content = new StringContent(string.Empty), }) .Verifiable(); using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); Mock <IHttpClientService> mockHttpClientService = new Mock <IHttpClientService>(); mockHttpClientService.Setup(s => s.CreateDefaultHttpClient()).Returns(() => new HttpClient(handlerMock.Object)); INotificationSettingsDelegate nsDelegate = new RestNotificationSettingsDelegate(loggerFactory.CreateLogger <RestNotificationSettingsDelegate>(), mockHttpClientService.Object, this.configuration); RequestResult <NotificationSettingsResponse> actualResult = Task.Run(async() => await nsDelegate.GetNotificationSettings(string.Empty)).Result; Assert.True(actualResult.IsDeepEqual(expected)); }
public void ValidateGetNotificationSettings200() { string json = @"{""smsEnabled"": true, ""smsCellNumber"": ""5551231234"", ""smsVerified"": true, ""smsScope"": [""COVID19""], ""emailEnabled"": true, ""emailAddress"": ""*****@*****.**"", ""emailScope"": [""COVID19""]}"; var options = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase, IgnoreNullValues = true, WriteIndented = true, }; RequestResult <NotificationSettingsResponse> expected = new RequestResult <NotificationSettingsResponse>() { ResourcePayload = JsonSerializer.Deserialize <NotificationSettingsResponse>(json, options), ResultStatus = Common.Constants.ResultType.Success, TotalResultCount = 1, }; var handlerMock = new Mock <HttpMessageHandler>(); handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(json), }) .Verifiable(); using var loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); Mock <IHttpClientService> mockHttpClientService = new Mock <IHttpClientService>(); mockHttpClientService.Setup(s => s.CreateDefaultHttpClient()).Returns(() => new HttpClient(handlerMock.Object)); INotificationSettingsDelegate nsDelegate = new RestNotificationSettingsDelegate(loggerFactory.CreateLogger <RestNotificationSettingsDelegate>(), mockHttpClientService.Object, this.configuration); RequestResult <NotificationSettingsResponse> actualResult = Task.Run(async() => await nsDelegate.GetNotificationSettings(string.Empty)).Result; Assert.True(actualResult.IsDeepEqual(expected)); }