private static void CloneEnvironment()
        {
            using (new SampleMethodScope())
            {
                var originalEnvironment =
                    ExportEnvironment(
                        EnvironmentConstants.AdventureWorksShops); // Export an Environment to use as a template
                var serializer = new JsonSerializer
                {
                    TypeNameHandling  = TypeNameHandling.All,
                    NullValueHandling = NullValueHandling.Ignore
                };
                var reader = new StringReader(originalEnvironment);
                CommerceEnvironment newEnvironment = null;
                using (var jsonReader = new JsonTextReader(reader)
                {
                    DateParseHandling = DateParseHandling.DateTimeOffset
                })
                {
                    newEnvironment = serializer.Deserialize <CommerceEnvironment>(jsonReader);
                }

                // Change the Id of the environment in order to import as a new Environment
                var newEnvironmentId = Guid.NewGuid();
                newEnvironment.ArtifactStoreId = newEnvironmentId;
                newEnvironment.Name            = "ConsoleSample." + newEnvironmentId.ToString("N");
                var    sw = new StringWriter(new StringBuilder());
                string newSerializedEnvironment = null;
                using (var writer = new JsonTextWriter(sw))
                {
                    serializer.Serialize(writer, newSerializedEnvironment);
                }

                newSerializedEnvironment = sw.ToString();

                // imports the environment into Sitecore Commerce
                var importedEnvironment = ImportEnvironment(newSerializedEnvironment);
                importedEnvironment.EntityId.Should()
                .Be($"Entity-CommerceEnvironment-ConsoleSample.{newEnvironmentId}");
                importedEnvironment.Name.Should().Be($"ConsoleSample.{newEnvironmentId}");

                // Adds a policy
                var policyResult = Proxy.DoOpsCommand(
                    OpsContainer.AddPolicy(
                        importedEnvironment.EntityId,
                        "Sitecore.Commerce.Plugin.Availability.GlobalAvailabilityPolicy, Sitecore.Commerce.Plugin.Availability",
                        new GlobalAvailabilityPolicy
                {
                    AvailabilityExpires = 0
                },
                        "GlobalEnvironment"));
                policyResult.Messages.Any(m => m.Code.Equals("error", StringComparison.OrdinalIgnoreCase))
                .Should()
                .BeFalse();
                policyResult.Models.OfType <PolicyAddedModel>().Any().Should().BeTrue();

                // Initialize the Environment with default artifacts
                Bootstrapping.InitializeEnvironment(OpsContainer, $"ConsoleSample.{newEnvironmentId}");

                // Get a SellableItem from the environment to assure that we have set it up correctly
                var shopperInNewEnvironmentContainer = new RegisteredCustomerDana
                {
                    Context = { Environment = $"ConsoleSample.{newEnvironmentId}" }
                }.Context.ShopsContainer();
                var result = Proxy.GetValue(
                    shopperInNewEnvironmentContainer.SellableItems.ByKey("Adventure Works Catalog,AW055 01,")
                    .Expand(
                        "Components($expand=ChildComponents($expand=ChildComponents($expand=ChildComponents)))"));
                result.Should().NotBeNull();
                result.Name.Should().Be("Unisex hiking pants");

                // Get the environment to validate change was made
                var updatedEnvironment =
                    Proxy.GetValue(OpsContainer.Environments.ByKey(importedEnvironment.EntityUniqueId.ToString()));
                var globalAvailabilityPolicy =
                    updatedEnvironment.Policies.OfType <GlobalAvailabilityPolicy>().FirstOrDefault();
                globalAvailabilityPolicy.Should().NotBeNull();
                globalAvailabilityPolicy.AvailabilityExpires.Should().Be(0);
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            try
            {
                OpsServiceUri       = Properties.Settings.Default.OpsServiceUri;
                ShopsServiceUri     = Properties.Settings.Default.ShopsServiceUri;
                MinionsServiceUri   = Properties.Settings.Default.MinionsServiceUri;
                AuthoringServiceUri = Properties.Settings.Default.AuthoringServiceUri;
                SitecoreIdServerUri = Properties.Settings.Default.SitecoreIdServerUri;

                UserName = Properties.Settings.Default.UserName;
                Password = Properties.Settings.Default.Password;

                SitecoreTokenRaw = SitecoreIdServerAuth.GetToken();
                SitecoreToken    = $"Bearer {SitecoreTokenRaw}";

                var stopwatch = new Stopwatch();
                stopwatch.Start();

                Console.ForegroundColor = ConsoleColor.Cyan;

                if (ShouldBootstrapOnLoad)
                {
                    Bootstrapping.RunScenarios();
                    Content.RunScenarios();
                }

                if (ShouldDevOpsScenarios)
                {
                    Environments.RunScenarios();

                    Plugins.RunScenarios();

                    Entities.RunScenarios();

                    Policies.RunScenarios();

                    Caching.RunScenarios();
                }

                if (ShouldRunCatalogScenarios)
                {
                    Catalogs.RunScenarios();
                    CatalogsUX.RunScenarios();

                    Categories.RunScenarios();
                    CategoriesUX.RunScenarios();

                    // TODO: contains failing tests
                    SellableItems.RunScenarios();

                    SellableItemsUX.RunScenarios();
                }

                if (ShouldRunPricingScenarios)
                {
                    Pricing.RunScenarios();
                    PricingUX.RunScenarios();
                }

                if (ShouldRunPromotionsScenarios)
                {
                    Promotions.RunScenarios();
                    PromotionsUX.RunScenarios();
                    PromotionsRuntime.RunScenarios();

                    Rules.RunScenarios();

                    Coupons.RunScenarios();
                    CouponsUX.RunScenarios();
                }

                if (ShouldRunInventoryScenarios)
                {
                    Inventory.RunScenarios();
                    InventoryUX.RunScenarios();
                }

                if (ShouldRunOrdersScenarios)
                {
                    Fulfillments.RunScenarios();

                    Payments.RunScenarios();
                    PaymentsFederated.RunScenarios();

                    Carts.RunScenarios();

                    Returns.RunScenarios();

                    OrdersUX.RunScenarios();
                    Orders.RunScenarios();

                    Shipments.RunScenarios(); // ORDERS HAVE TO BE RELEASED FOR SHIPMENTS TO GET GENERATED
                }

                if (ShouldRunCustomersScenarios)
                {
                    CustomersUX.RunScenarios();
                }

                if (ShouldRunEntitlementsScenarios)
                {
                    Entitlements.RunScenarios();
                }

                if (ShouldRunSearchScenarios)
                {
                    Search.RunScenarios();
                }

                if (ShouldRunBusinessUsersScenarios)
                {
                    ComposerUX.RunScenarios();
                    // Composer.RunScenarios();
                }

                if (ShouldRunVersionScenarios)
                {
                    Versions.RunScenarios();
                }

                stopwatch.Stop();

                Console.WriteLine($"Test Runs Complete - {stopwatch.ElapsedMilliseconds} ms -  (Hit any key to continue)");

                if (DemoStops)
                {
                    Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                ConsoleExtensions.WriteErrorLine("An unexpected exception occurred.");
                ConsoleExtensions.WriteErrorLine(ex.ToString());
            }

            Console.WriteLine("done.");
        }
        public static void RunScenarios()
        {
            var watch = new Stopwatch();

            watch.Start();

            NewEnvironment();

            var habitat = Proxy.GetValue(OpsContainer.Environments.ByKey("Entity-CommerceEnvironment-HabitatAuthoring").Expand("Components"));

            habitat.Should().NotBeNull();

            Console.WriteLine("Begin Clone Environment");

            // Export an Environment to use as a template
            var originalEnvironment = ExportEnvironment("AdventureWorksShops");

            var environmentObject = JsonConvert.DeserializeObject(originalEnvironment);

            // Get a dynamic view of the JSON object
            var dynamicEnvironment = (dynamic)environmentObject;

            // Change the Id of the environment in order to import as a new Environment
            var newEnvironmentId = Guid.NewGuid().ToString("N");

            dynamicEnvironment.ArtifactStoreId = newEnvironmentId;
            dynamicEnvironment.Name            = "ConsoleSample." + newEnvironmentId;

            var newSerializedEnvironment = JsonConvert.SerializeObject(environmentObject);

            // imports the environment into Sitecore Commerce
            var importedEnvironment = ImportEnvironment(newSerializedEnvironment);

            importedEnvironment.EnvironmentId.Should().Be($"Entity-CommerceEnvironment-ConsoleSample.{newEnvironmentId}");
            importedEnvironment.Name.Should().Be($"ConsoleSample.{newEnvironmentId}");

            // Adds a policy
            var policyResult = Proxy.DoOpsCommand(
                OpsContainer.AddPolicy(
                    importedEnvironment.EnvironmentId,
                    "Sitecore.Commerce.Plugin.Availability.GlobalAvailabilityPolicy, Sitecore.Commerce.Plugin.Availability",
                    new GlobalAvailabilityPolicy
            {
                AvailabilityExpires = 0
            },
                    "GlobalEnvironment"));

            policyResult.Messages.Any(m => m.Code.Equals("error", StringComparison.OrdinalIgnoreCase)).Should().BeFalse();
            policyResult.Models.OfType <PolicyAddedModel>().Any().Should().BeTrue();

            // Initialize the Environment with default artifacts
            Bootstrapping.InitializeEnvironment(OpsContainer, $"ConsoleSample.{newEnvironmentId}");

            var shopperInNewEnvironmentContainer = new RegisteredCustomerDana
            {
                Context = { Environment = $"ConsoleSample.{newEnvironmentId}" }
            }.Context.ShopsContainer();

            // Get a SellableItem from the environment to assure that we have set it up correctly
            var result = Proxy.GetValue(shopperInNewEnvironmentContainer.SellableItems.ByKey("Adventure Works Catalog,AW055 01,")
                                        .Expand("Components($expand=ChildComponents($expand=ChildComponents($expand=ChildComponents)))"));

            result.Should().NotBeNull();
            result.Name.Should().Be("Unisex hiking pants");

            // Get the environment to validate change was made
            var updatedEnvironment       = Proxy.GetValue(OpsContainer.Environments.ByKey(importedEnvironment.EnvironmentId));
            var globalAvailabilityPolicy = updatedEnvironment.Policies.OfType <GlobalAvailabilityPolicy>().FirstOrDefault();

            globalAvailabilityPolicy.Should().NotBeNull();
            globalAvailabilityPolicy.AvailabilityExpires.Should().Be(0);

            var orderListsPolicy = updatedEnvironment.Policies.OfType <KnownOrderListsPolicy>().FirstOrDefault();

            orderListsPolicy.Should().NotBeNull();
            orderListsPolicy.OrderDashboardLists = new ObservableCollection <KeyValueString>
            {
                new KeyValueString {
                    Key = "listName", Value = "icon"
                }
            };
            policyResult = Proxy.DoOpsCommand(
                OpsContainer.AddPolicy(
                    importedEnvironment.EnvironmentId,
                    "Sitecore.Commerce.Plugin.Orders.KnownOrderListsPolicy, Sitecore.Commerce.Plugin.Orders",
                    orderListsPolicy,
                    "GlobalEnvironment"));
            policyResult.Messages.Any(m => m.Code.Equals("error", StringComparison.OrdinalIgnoreCase)).Should().BeFalse();
            policyResult.Models.OfType <PolicyAddedModel>().Any().Should().BeTrue();
            updatedEnvironment = Proxy.GetValue(OpsContainer.Environments.ByKey(importedEnvironment.EnvironmentId));
            orderListsPolicy   = updatedEnvironment.Policies.OfType <KnownOrderListsPolicy>().FirstOrDefault();
            orderListsPolicy.Should().NotBeNull();
            orderListsPolicy.OrderDashboardLists.Should().OnlyContain(l => l.Key.Equals("listName"));

            watch.Stop();

            Console.WriteLine($"End Clone Environments: {watch.ElapsedMilliseconds} ms");
        }