/// <inheritdoc/> public async Task <string> Create(CloudEvent item) { try { await _conn.OpenAsync(); NpgsqlCommand pgcom = new NpgsqlCommand(insertEventSql, _conn); pgcom.Parameters.AddWithValue("id", item.Id); pgcom.Parameters.AddWithValue("source", item.Source.OriginalString); pgcom.Parameters.AddWithValue("subject", item.Subject); pgcom.Parameters.AddWithValue("type", item.Type); pgcom.Parameters.AddWithValue("cloudevent", item.Serialize()); await pgcom.ExecuteNonQueryAsync(); return(item.Id); } catch (Exception e) { _logger.LogError("PostgresRepository // Create // Exception", e); throw; } finally { await _conn.CloseAsync(); } }
public async void Post_InValidCloudEvent_ReturnsStatusBadRequest() { // Arrange string requestUri = $"{BasePath}/app"; CloudEvent cloudEvent = GetCloudEvent(); cloudEvent.Subject = null; Mock <IEventsService> eventsService = new Mock <IEventsService>(); HttpClient client = GetTestClient(eventsService.Object); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", PrincipalUtil.GetToken(1)); HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(cloudEvent.Serialize(), Encoding.UTF8, "application/json") }; httpRequestMessage.Headers.Add("PlatformAccessToken", PrincipalUtil.GetAccessToken("ttd", "unittest")); // Act HttpResponseMessage response = await client.SendAsync(httpRequestMessage); // Assert Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }
public async void Post_GivenValidCloudEvent_ReturnsStatusCreatedAndCorrectData() { // Arrange string requestUri = $"{BasePath}/app"; string responseId = Guid.NewGuid().ToString(); CloudEvent cloudEvent = GetCloudEvent(); Mock <IEventsService> eventsService = new Mock <IEventsService>(); eventsService.Setup(s => s.StoreCloudEvent(It.IsAny <CloudEvent>())).ReturnsAsync(responseId); HttpClient client = GetTestClient(eventsService.Object); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", PrincipalUtil.GetToken(1)); HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(cloudEvent.Serialize(), Encoding.UTF8, "application/json") }; httpRequestMessage.Headers.Add("PlatformAccessToken", PrincipalUtil.GetAccessToken("ttd", "unittest")); // Act HttpResponseMessage response = await client.SendAsync(httpRequestMessage); // Assert Assert.Equal(HttpStatusCode.Created, response.StatusCode); string content = response.Content.ReadAsStringAsync().Result; Assert.Contains(responseId, content); }
/// <inheritdoc/> public async Task <string> Create(CloudEvent cloudEvent) { using NpgsqlConnection conn = new NpgsqlConnection(_connectionString); await conn.OpenAsync(); NpgsqlCommand pgcom = new NpgsqlCommand(insertEventSql, conn); pgcom.Parameters.AddWithValue("id", cloudEvent.Id); pgcom.Parameters.AddWithValue("source", cloudEvent.Source.OriginalString); pgcom.Parameters.AddWithValue("subject", cloudEvent.Subject); pgcom.Parameters.AddWithValue("type", cloudEvent.Type); pgcom.Parameters.AddWithValue("cloudevent", cloudEvent.Serialize()); await pgcom.ExecuteNonQueryAsync(); return(cloudEvent.Id); }
public async void Post_OneMatchingAndValidSubscriptions_AddedToqueue() { // Arrange string requestUri = $"{BasePath}/push"; CloudEvent cloudEvent = GetCloudEvent(new Uri("https://ttd.apps.altinn.no/ttd/endring-av-navn-v2/instances/1337/123124"), "/party/1337/", "app.instance.process.movedTo.task_1"); HttpClient client = GetTestClient(); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", PrincipalUtil.GetOrgToken("skd")); HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(cloudEvent.Serialize(), Encoding.UTF8, "application/json") }; // Act HttpResponseMessage response = await client.SendAsync(httpRequestMessage); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.True(QueueServiceMock.OutboundQueue.ContainsKey(cloudEvent.Id)); Assert.Single(QueueServiceMock.OutboundQueue[cloudEvent.Id]); }
public async void Post_TwoMatchingAndValidSubscriptions_AddedToqueue() { // Arrange string requestUri = $"{BasePath}/push"; CloudEvent cloudEvent = GetCloudEvent(new Uri("https://ttd.apps.altinn.no/ttd/endring-av-navn-v2/instances/1337/123124"), "/party/1337/", "app.instance.process.completed"); HttpClient client = GetTestClient(); HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(cloudEvent.Serialize(), Encoding.UTF8, "application/json") }; httpRequestMessage.Headers.Add("PlatformAccessToken", PrincipalUtil.GetAccessToken("platform", "events")); // Act HttpResponseMessage response = await client.SendAsync(httpRequestMessage); // Assert Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.True(QueueServiceMock.OutboundQueue.ContainsKey(cloudEvent.Id)); Assert.Equal(2, QueueServiceMock.OutboundQueue[cloudEvent.Id].Count); }
public async void Post_RepositoryThrowsException_ReturnsInternalServerError() { // Arrange string requestUri = $"{BasePath}/app"; CloudEvent cloudEvent = GetCloudEvent(); Mock <IEventsService> eventsService = new Mock <IEventsService>(); eventsService.Setup(er => er.StoreCloudEvent(It.IsAny <CloudEvent>())).Throws(new Exception()); HttpClient client = GetTestClient(eventsService.Object); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", PrincipalUtil.GetToken(1)); HttpRequestMessage httpRequestMessage = new HttpRequestMessage(HttpMethod.Post, requestUri) { Content = new StringContent(cloudEvent.Serialize(), Encoding.UTF8, "application/json") }; httpRequestMessage.Headers.Add("PlatformAccessToken", PrincipalUtil.GetAccessToken("ttd", "unittest")); // Act HttpResponseMessage response = await client.SendAsync(httpRequestMessage); // Assert Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode); }
/// <inheritdoc/> public async Task <string> Create(CloudEvent cloudEvent) { try { using NpgsqlConnection conn = new NpgsqlConnection(_connectionString); await conn.OpenAsync(); NpgsqlCommand pgcom = new NpgsqlCommand(insertEventSql, conn); pgcom.Parameters.AddWithValue("id", cloudEvent.Id); pgcom.Parameters.AddWithValue("source", cloudEvent.Source.OriginalString); pgcom.Parameters.AddWithValue("subject", cloudEvent.Subject); pgcom.Parameters.AddWithValue("type", cloudEvent.Type); pgcom.Parameters.AddWithValue("cloudevent", cloudEvent.Serialize()); await pgcom.ExecuteNonQueryAsync(); return(cloudEvent.Id); } catch (Exception e) { _logger.LogError(e, "PostgresRepository // Create // Exception"); throw; } }