public async Task ProductCategoriesWorkflow()
        {
            using var client = await TestBootstrapper.CreateTestClientWithInMemoryDb();

            await TestRecordsCreator.CreateCategoryAsync(client, "stocks/Germany/Dax 30");

            var getCategoriesResponse = await client.GetAsync("/api/product-categories");

            var categories = (await getCategoriesResponse.Content.ReadAsStringAsync())
                             .DeserializeJson <GetProductCategoriesResponse>().ProductCategories;

            Assert.Equal(3, categories.Count);
            Assert.True(categories.First(c => c.Id == "stocks.germany.dax_30").IsLeaf);
            Assert.False(categories.First(c => c.Id == "stocks.germany").IsLeaf);
            Assert.False(categories.First(c => c.Id == "stocks").IsLeaf);

            var deleteRequest = new DeleteProductCategoryRequest()
            {
                UserName = "******",
            };

            var leafCategoryId = "stocks.germany.dax_30";

            await client.DeleteAsJsonAsync($"/api/product-categories/{leafCategoryId}", deleteRequest);

            getCategoriesResponse = await client.GetAsync("/api/product-categories");

            categories = (await getCategoriesResponse.Content.ReadAsStringAsync())
                         .DeserializeJson <GetProductCategoriesResponse>().ProductCategories;

            Assert.Equal(2, categories.Count);
            Assert.True(categories.First(c => c.Id == "stocks.germany").IsLeaf);
            Assert.False(categories.First(c => c.Id == "stocks").IsLeaf);
        }
        public async Task ProductCategories_CannotDeleteCategoryWithAttachedProducts_Workflow()
        {
            _underlyingsCacheMock.Setup(x => x.GetByMdsCode(It.IsAny <string>()))
            .Returns(new UnderlyingsCacheModel()
            {
                MdsCode         = "mds-code",
                TradingCurrency = "EUR",
            });

            _assetTypesRepositoryMock.Setup(x => x.ExistsAsync(It.IsAny <string>()))
            .ReturnsAsync(true);

            using var client = await TestBootstrapper.CreateTestClientWithInMemoryDb(builder =>
            {
                builder.RegisterInstance(_underlyingsCacheMock.Object).As <IUnderlyingsCache>().SingleInstance();
                builder.RegisterInstance(_assetTypesRepositoryMock.Object).As <IAssetTypesRepository>().SingleInstance();
            });

            var category = "stocks";

            await TestRecordsCreator.CreateCurrencyAsync(client, "EUR");

            await TestRecordsCreator.CreateMarketSettings(client, "market");

            await TestRecordsCreator.CreateTickFormula(client, "tick_formula");

            // asset type is mocked
            await TestRecordsCreator.CreateProductAsync(client, "product1", category);

            var getCategoriesResponse = await client.GetAsync("/api/product-categories");

            var categories = (await getCategoriesResponse.Content.ReadAsStringAsync())
                             .DeserializeJson <GetProductCategoriesResponse>().ProductCategories;

            Assert.Single(categories);

            var deleteRequest = new DeleteProductCategoryRequest()
            {
                UserName = "******",
            };

            var deleteCategoryResponse =
                (await client.DeleteAsJsonAsync($"/api/product-categories/{category}", deleteRequest));
            var errorCode = (await deleteCategoryResponse.Content.ReadAsStringAsync())
                            .DeserializeJson <ErrorCodeResponse <ProductCategoriesErrorCodesContract> >().ErrorCode;

            Assert.Equal(ProductCategoriesErrorCodesContract.CannotDeleteCategoryWithAttachedProducts, errorCode);

            getCategoriesResponse = await client.GetAsync("/api/product-categories");

            categories = (await getCategoriesResponse.Content.ReadAsStringAsync())
                         .DeserializeJson <GetProductCategoriesResponse>().ProductCategories;

            Assert.Single(categories);
        }
        public async Task ProductCategories_CannotDeleteNonLeafCategory_Workflow()
        {
            using var client = await TestBootstrapper.CreateTestClientWithInMemoryDb();

            var notLeafCategory = "stocks";
            var category        = "stocks/germany";

            await TestRecordsCreator.CreateCategoryAsync(client, category);

            var getCategoriesResponse = await client.GetAsync("/api/product-categories");

            var categories = (await getCategoriesResponse.Content.ReadAsStringAsync())
                             .DeserializeJson <GetProductCategoriesResponse>().ProductCategories;

            Assert.Collection(categories,
                              x => { Assert.Equal("stocks", x.Id); },
                              x1 => { Assert.Equal("stocks.germany", x1.Id); });

            var deleteRequest = new DeleteProductCategoryRequest()
            {
                UserName = "******",
            };

            var deleteCategoryResponse =
                (await client.DeleteAsJsonAsync($"/api/product-categories/{notLeafCategory}", deleteRequest));
            var errorCode = (await deleteCategoryResponse.Content.ReadAsStringAsync())
                            .DeserializeJson <ErrorCodeResponse <ProductCategoriesErrorCodesContract> >().ErrorCode;

            Assert.Equal(ProductCategoriesErrorCodesContract.CannotDeleteNonLeafCategory, errorCode);

            getCategoriesResponse = await client.GetAsync("/api/product-categories");

            categories = (await getCategoriesResponse.Content.ReadAsStringAsync())
                         .DeserializeJson <GetProductCategoriesResponse>().ProductCategories;

            Assert.Collection(categories,
                              x => { Assert.Equal("stocks", x.Id); },
                              x1 => { Assert.Equal("stocks.germany", x1.Id); });
        }
        public async Task <ErrorCodeResponse <ProductCategoriesErrorCodesContract> > DeleteAsync(string id,
                                                                                                 [FromBody] DeleteProductCategoryRequest request)
        {
            var result =
                await _productCategoriesService.DeleteAsync(id, request.UserName,
                                                            this.TryGetCorrelationId());

            var response = new ErrorCodeResponse <ProductCategoriesErrorCodesContract>();

            if (result.IsFailed)
            {
                response.ErrorCode =
                    _convertService.Convert <ProductCategoriesErrorCodes, ProductCategoriesErrorCodesContract>(
                        result.Error.GetValueOrDefault());
            }

            return(response);
        }