2018-07-26 04:22:00 +00:00
|
|
|
// Copyright 2018 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-06-23 00:17:34 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using Grpc.Core;
|
|
|
|
using Hipstershop;
|
|
|
|
using Xunit;
|
|
|
|
using static Hipstershop.CartService;
|
|
|
|
|
|
|
|
namespace cartservice
|
|
|
|
{
|
|
|
|
public class E2ETests
|
|
|
|
{
|
2018-06-25 16:44:56 +00:00
|
|
|
private static string serverHostName = "localhost";
|
2018-06-23 00:17:34 +00:00
|
|
|
private static int port = 7070;
|
|
|
|
|
2018-06-26 21:41:00 +00:00
|
|
|
[Fact]
|
|
|
|
public async Task GetItem_NoAddItemBefore_EmptyCartReturned()
|
|
|
|
{
|
|
|
|
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 GetCartRequest
|
|
|
|
{
|
|
|
|
UserId = userId,
|
|
|
|
};
|
2018-07-02 20:26:37 +00:00
|
|
|
|
2018-06-26 21:41:00 +00:00
|
|
|
var cart = await client.GetCartAsync(request);
|
|
|
|
Assert.NotNull(cart);
|
2018-07-02 20:26:37 +00:00
|
|
|
|
2018-06-26 21:41:00 +00:00
|
|
|
// All grpc objects implement IEquitable, so we can compare equality with by-value semantics
|
|
|
|
Assert.Equal(new Cart(), cart);
|
|
|
|
}
|
|
|
|
|
2018-06-23 00:17:34 +00:00
|
|
|
[Fact]
|
2018-07-02 20:26:37 +00:00
|
|
|
public async Task AddItem_ItemExists_Updated()
|
2018-06-23 00:17:34 +00:00
|
|
|
{
|
2018-06-25 16:44:56 +00:00
|
|
|
string userId = Guid.NewGuid().ToString();
|
2018-06-23 00:17:34 +00:00
|
|
|
|
|
|
|
// 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);
|
2018-06-25 16:44:56 +00:00
|
|
|
|
2018-06-23 00:17:34 +00:00
|
|
|
var client = new CartServiceClient(channel);
|
|
|
|
var request = new AddItemRequest
|
|
|
|
{
|
|
|
|
UserId = userId,
|
|
|
|
Item = new CartItem
|
|
|
|
{
|
|
|
|
ProductId = "1",
|
|
|
|
Quantity = 1
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-06-27 17:33:18 +00:00
|
|
|
// 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
|
2018-06-23 00:17:34 +00:00
|
|
|
{
|
2018-06-27 17:33:18 +00:00
|
|
|
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]
|
|
|
|
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
|
2018-06-23 00:17:34 +00:00
|
|
|
{
|
2018-06-27 17:33:18 +00:00
|
|
|
ProductId = "1",
|
|
|
|
Quantity = 1
|
2018-06-23 00:17:34 +00:00
|
|
|
}
|
2018-06-27 17:33:18 +00:00
|
|
|
};
|
|
|
|
|
2018-06-25 16:44:56 +00:00
|
|
|
await client.AddItemAsync(request);
|
2018-06-27 17:33:18 +00:00
|
|
|
|
2018-06-25 16:44:56 +00:00
|
|
|
var getCartRequest = new GetCartRequest
|
2018-06-23 00:17:34 +00:00
|
|
|
{
|
|
|
|
UserId = userId
|
|
|
|
};
|
2018-06-25 16:44:56 +00:00
|
|
|
var cart = await client.GetCartAsync(getCartRequest);
|
|
|
|
Assert.NotNull(cart);
|
2018-06-23 00:17:34 +00:00
|
|
|
Assert.Equal(userId, cart.UserId);
|
|
|
|
Assert.Single(cart.Items);
|
2018-06-25 16:44:56 +00:00
|
|
|
|
|
|
|
await client.EmptyCartAsync(new EmptyCartRequest{ UserId = userId });
|
|
|
|
cart = await client.GetCartAsync(getCartRequest);
|
|
|
|
Assert.Empty(cart.Items);
|
2018-06-23 00:17:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|