Beispiel #1
0
        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 });
        }
Beispiel #2
0
        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);
        }
Beispiel #3
0
        public async Task AddItem_New_Inserted()
        {
            string userId = Guid.NewGuid().ToString();

            // Construct server's Uri
            string targetUri = $"{serverHostName}:{port}";

            // Create a GRPC communication channel between the client and the server
            var channel = new Channel(targetUri, ChannelCredentials.Insecure);

            // 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);
        }
Beispiel #4
0
        public async Task AddItem_ItemExists_Updated()
        {
            string userId = Guid.NewGuid().ToString();

            // Construct server's Uri
            string targetUri = $"{serverHostName}:{port}";

            // Create a GRPC communication channel between the client and the server
            var channel = new Channel(targetUri, ChannelCredentials.Insecure);

            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 });
        }
Beispiel #5
0
        public async Task AddItem_New_Inserted()
        {
            string userId = Guid.NewGuid().ToString();

            // Create a GRPC communication channel between the client and the server
            using (var channel = GrpcChannel.ForAddress(TargetUrl))
            {
                // 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);
            }
        }