summaryrefslogtreecommitdiff
path: root/src/cart/tests/CartServiceTests.cs
diff options
context:
space:
mode:
authorSaumit <justsaumit@protonmail.com>2025-09-27 02:14:26 +0530
committerSaumit <justsaumit@protonmail.com>2025-09-27 02:14:26 +0530
commit82e03978b89938219958032efb1448cc76baa181 (patch)
tree626f3e54d52ecd49be0ed3bee30abacc0453d081 /src/cart/tests/CartServiceTests.cs
Initial snapshot - OpenTelemetry demo 2.1.3 -f
Diffstat (limited to 'src/cart/tests/CartServiceTests.cs')
-rw-r--r--src/cart/tests/CartServiceTests.cs146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/cart/tests/CartServiceTests.cs b/src/cart/tests/CartServiceTests.cs
new file mode 100644
index 0000000..45173d1
--- /dev/null
+++ b/src/cart/tests/CartServiceTests.cs
@@ -0,0 +1,146 @@
+// Copyright The OpenTelemetry Authors
+// SPDX-License-Identifier: Apache-2.0
+using System;
+using System.Threading.Tasks;
+using Grpc.Net.Client;
+using Oteldemo;
+using Microsoft.AspNetCore.TestHost;
+using Microsoft.Extensions.Hosting;
+using Xunit;
+using static Oteldemo.CartService;
+
+namespace cart.tests;
+
+public class CartServiceTests
+{
+ private readonly IHostBuilder _host;
+
+ public CartServiceTests()
+ {
+ _host = new HostBuilder().ConfigureWebHost(webBuilder =>
+ {
+ webBuilder
+ // .UseStartup<Startup>()
+ .UseTestServer();
+ });
+ }
+
+ [Fact(Skip = "See https://github.com/open-telemetry/opentelemetry-demo/pull/746#discussion_r1107931240")]
+ public async Task GetItem_NoAddItemBefore_EmptyCartReturned()
+ {
+ // Setup test server and client
+ using var server = await _host.StartAsync();
+ var httpClient = server.GetTestClient();
+
+ string userId = Guid.NewGuid().ToString();
+
+ // Create a GRPC communication channel between the client and the server
+ var channel = GrpcChannel.ForAddress(httpClient.BaseAddress, new GrpcChannelOptions
+ {
+ HttpClient = httpClient
+ });
+
+ var cartClient = new CartServiceClient(channel);
+
+ var request = new GetCartRequest
+ {
+ UserId = userId,
+ };
+
+ var cart = await cartClient.GetCartAsync(request);
+ Assert.NotNull(cart);
+
+ // All grpc objects implement IEquitable, so we can compare equality with by-value semantics
+ Assert.Equal(new Cart(), cart);
+ }
+
+ [Fact(Skip = "See https://github.com/open-telemetry/opentelemetry-demo/pull/746#discussion_r1107931240")]
+ public async Task AddItem_ItemExists_Updated()
+ {
+ // Setup test server and client
+ using var server = await _host.StartAsync();
+ var httpClient = server.GetTestClient();
+
+ string userId = Guid.NewGuid().ToString();
+
+ // Create a GRPC communication channel between the client and the server
+ var channel = GrpcChannel.ForAddress(httpClient.BaseAddress, new GrpcChannelOptions
+ {
+ HttpClient = httpClient
+ });
+
+ var client = new CartServiceClient(channel);
+ var request = new AddItemRequest
+ {
+ UserId = userId,
+ Item = new CartItem
+ {
+ ProductId = "1",
+ Quantity = 1
+ }
+ };
+
+ // First add - nothing should fail
+ await client.AddItemAsync(request);
+
+ // Second add of existing product - quantity should be updated
+ await client.AddItemAsync(request);
+
+ var getCartRequest = new GetCartRequest
+ {
+ UserId = userId
+ };
+ var cart = await client.GetCartAsync(getCartRequest);
+ Assert.NotNull(cart);
+ Assert.Equal(userId, cart.UserId);
+ Assert.Single(cart.Items);
+ Assert.Equal(2, cart.Items[0].Quantity);
+
+ // Cleanup
+ await client.EmptyCartAsync(new EmptyCartRequest { UserId = userId });
+ }
+
+ [Fact(Skip = "See https://github.com/open-telemetry/opentelemetry-demo/pull/746#discussion_r1107931240")]
+ public async Task AddItem_New_Inserted()
+ {
+ // Setup test server and client
+ using var server = await _host.StartAsync();
+ var httpClient = server.GetTestClient();
+
+ string userId = Guid.NewGuid().ToString();
+
+ // Create a GRPC communication channel between the client and the server
+ var channel = GrpcChannel.ForAddress(httpClient.BaseAddress, new GrpcChannelOptions
+ {
+ HttpClient = httpClient
+ });
+
+ // Create a proxy object to work with the server
+ var client = new CartServiceClient(channel);
+
+ var request = new AddItemRequest
+ {
+ UserId = userId,
+ Item = new CartItem
+ {
+ ProductId = "1",
+ Quantity = 1
+ }
+ };
+
+ await client.AddItemAsync(request);
+
+ var getCartRequest = new GetCartRequest
+ {
+ UserId = userId
+ };
+ var cart = await client.GetCartAsync(getCartRequest);
+ Assert.NotNull(cart);
+ Assert.Equal(userId, cart.UserId);
+ Assert.Single(cart.Items);
+
+ await client.EmptyCartAsync(new EmptyCartRequest { UserId = userId });
+ cart = await client.GetCartAsync(getCartRequest);
+ Assert.Empty(cart.Items);
+ }
+}