diff --git a/src/cartservice/cartstore/RedisCartStore.cs b/src/cartservice/cartstore/RedisCartStore.cs index 381a8ce..ea87568 100644 --- a/src/cartservice/cartstore/RedisCartStore.cs +++ b/src/cartservice/cartstore/RedisCartStore.cs @@ -69,13 +69,13 @@ namespace cartservice.cartstore // Access the cart from the cache var value = await db.HashGetAsync(userId, CART_FIELD_NAME); - Hipstershop.Cart cart = null; if (!value.IsNull) { - cart = Hipstershop.Cart.Parser.ParseFrom(value); + return Hipstershop.Cart.Parser.ParseFrom(value); } - return cart; + // We decided to return empty cart in cases when user wasn't in the cache before + return new Hipstershop.Cart(); } } } \ No newline at end of file diff --git a/src/cartservice/scripts/docker_setup.bat b/src/cartservice/scripts/docker_setup.bat index 4bf0a71..2e4e371 100644 --- a/src/cartservice/scripts/docker_setup.bat +++ b/src/cartservice/scripts/docker_setup.bat @@ -1,16 +1,45 @@ @echo off -set REDIS_PORT=6379 -set REDIS_ADDR=172.30.147.193 -set CART_SERVICE_ADDR=127.0.0.1 -set CART_SERVICE_PORT=7070 +set ENV=%1 -rem run docker container with redis -rem docker run -d --name=redis -p %REDIS_PORT%:%REDIS_PORT% redis:alpine +IF %ENV%==local GOTO local +IF %ENV%==docker GOTO docker_local +GOTO End1 + +:local + set REDIS_PORT=6379 + set REDIS_ADDR=localhost:%REDIS_PORT% + set CART_SERVICE_ADDR=127.0.0.1 + set CART_SERVICE_PORT=7070 + + echo running redis emulator locally on a separate window + taskkill /f /im "redis-server.exe" + start redis-server + + echo running the cart service locally + dotnet build ..\. + dotnet run --project ../cartservice.csproj start +GOTO End1 + +:docker_local + set REDIS_PORT=6379 + set REDIS_ADDR=redis:%REDIS_PORT% + set CART_SERVICE_ADDR=127.0.0.1 + set CART_SERVICE_PORT=7070 + + echo run docker container with redis + docker run -d --name=redis -p %REDIS_PORT%:%REDIS_PORT% redis + + echo building container image for cart service + docker build -t cartservice ..\. + + echo run container image for cart service + docker run -it --rm -e REDIS_ADDR=%REDIS_ADDR% -e CART_SERVICE_ADDR=%CART_SERVICE_ADDR% -e CART_SERVICE_PORT=%CART_SERVICE_PORT% -p %CART_SERVICE_PORT%:%CART_SERVICE_PORT% cartservice + +GOTO End1 + +:End1 -rem running locally -dotnet build ..\. -dotnet run --project ../cartservice.csproj start rem run docker container with cart service rem docker run -it --rm -e REDIS_ADDR=%REDIS_ADDR%:%REDIS_PORT% -e CART_SERVICE_ADDR=%CART_SERVICE_ADDR% -e CART_SERVICE_PORT=%CART_SERVICE_PORT% -p %CART_SERVICE_PORT%:%CART_SERVICE_PORT% cartservice rem -e GRPC_TRACE=all -e GRPC_VERBOSITY=debug diff --git a/tests/cartservice/CartServiceTests.cs b/tests/cartservice/CartServiceTests.cs index 1b98bc6..b82ace3 100644 --- a/tests/cartservice/CartServiceTests.cs +++ b/tests/cartservice/CartServiceTests.cs @@ -12,6 +12,29 @@ namespace cartservice private static string serverHostName = "localhost"; private static int port = 7070; + [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, + }; + var cart = await client.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] public async Task AddItem_ItemInserted() {