public async Task ThenItShouldSendProviderToRestProxyTopic(ApprenticeshipProvider provider) { await _receiver.SendDataAsync(provider, _cancellationToken); var expectedUrl = new Uri( new Uri(_configuration.KafkaRestProxyUrl, UriKind.Absolute), new Uri($"topics/{_configuration.RoatpProviderTopic}", UriKind.Relative)); _httpClientMock.Verify(request => request.RequestUri.AbsoluteUri == expectedUrl.AbsoluteUri, Times.Once()); var expectedContent = JsonConvert.SerializeObject(new RestProxyPublishMessage <long, ApprenticeshipProvider> { Records = new[] { new RestProxyPublishMessageRecord <long, ApprenticeshipProvider> { Key = provider.Ukprn, Value = provider, }, }, }); _httpClientMock.Verify(request => request.Content.ReadAsStringAsync().Result == expectedContent, Times.Once()); }
public async Task SendDataAsync(ApprenticeshipProvider provider, CancellationToken cancellationToken) { _logger.LogInformation("Sending {UKPRN} to Kafka topic {TopicName}", provider.Ukprn, _configuration.RoatpProviderTopic); var result = await _producer.ProduceAsync( _configuration.RoatpProviderTopic, provider.Ukprn.ToString(), provider, cancellationToken); _logger.LogInformation("Message for {UKPRN} stored as offset {Offset} in partition {Partition} for {TopicName}", provider.Ukprn, result.Offset, result.Partition, _configuration.RoatpProviderTopic); }
public async Task ThenItShouldSendEachProviderReceivedFromSourceToReceiver() { var apprenticeshipProvider1 = new ApprenticeshipProvider(); var apprenticeshipProvider2 = new ApprenticeshipProvider(); _roatpDataSourceMock.Setup(source => source.GetDataAsync(It.IsAny <CancellationToken>())) .ReturnsAsync(new[] { apprenticeshipProvider1, apprenticeshipProvider2, }); await _changeProcessor.ProcessChangesAsync(_cancellationToken); _roatpDataReceiverMock.Verify(receiver => receiver.SendDataAsync(It.IsAny <ApprenticeshipProvider>(), It.IsAny <CancellationToken>()), Times.Exactly(2)); _roatpDataReceiverMock.Verify(receiver => receiver.SendDataAsync(apprenticeshipProvider1, _cancellationToken), Times.Once); _roatpDataReceiverMock.Verify(receiver => receiver.SendDataAsync(apprenticeshipProvider2, _cancellationToken), Times.Once); }
public async Task SendDataAsync(ApprenticeshipProvider provider, CancellationToken cancellationToken) { _logger.LogInformation("Sending {UKPRN} to Kafka topic {TopicName}", provider.Ukprn, _configuration.RoatpProviderTopic); var message = new RestProxyPublishMessage <long, ApprenticeshipProvider> { Records = new[] { new RestProxyPublishMessageRecord <long, ApprenticeshipProvider> { Key = provider.Ukprn, Value = provider, }, }, }; var messageJson = JsonConvert.SerializeObject(message); var content = new StringContent(messageJson, Encoding.UTF8, "application/vnd.kafka.json.v2+json"); var response = await _httpClient.PostAsync("", content, cancellationToken); if (!response.IsSuccessStatusCode) { throw await RestProxyException.FromFailedHttpResponseAsync($"posting message to {_configuration.RoatpProviderTopic}", response); } var responseJson = await response.Content.ReadAsStringAsync(); var responseMessage = JsonConvert.DeserializeObject <RestProxyPublishMessageResponse>(responseJson); if (!string.IsNullOrEmpty(responseMessage.Offsets[0].Error) || !string.IsNullOrEmpty(responseMessage.Offsets[0].ErrorCode)) { throw RestProxyException.FromErroredOffset(responseMessage.Offsets[0], response.StatusCode); } _logger.LogInformation("Message for {UKPRN} stored as offset {Offset} in partition {Partition} for {TopicName}", provider.Ukprn, responseMessage.Offsets[0].Offset, responseMessage.Offsets[0].Partition, _configuration.RoatpProviderTopic); }