public static void ProcessCommand(string command)
        {
            if (command.IsEmpty())
            {
                return;
            }

            if (!IsTddExecutionMode())
            {
                throw new Exception("Invalid command in non TDD mode.");
            }

            var request  = HttpContext.Current.Request;
            var response = HttpContext.Current.Response;

            var isShared = request["mode"] == "shared";

            if (command == "snap")
            {
                new Snapshot(request["name"], isShared).Create(HttpContext.Current);
            }
            else if (command == "restore")
            {
                new Snapshot(request["name"], isShared).Restore(HttpContext.Current);
            }
            else if (command == "remove_snapshots")
            {
                Snapshot.RemoveSnapshots();
            }
            else if (command == "snapshots_list")
            {
                response.EndWith(JsonConvert.SerializeObject(Snapshot.GetList(isShared)));
            }
            else if (command == "snapExists")
            {
                if (new Snapshot(request["name"], isShared).Exists())
                {
                    response.EndWith("true");
                }
                else
                {
                    response.EndWith("false");
                }
            }
            else if (command.IsAnyOf("start", "run", "ran", "cancel", "restart"))
            {
                InitiateTempDatabase(enforceRestart: true, mustRenew: true);

                if (request.Has("runner"))
                {
                    CurrentRunner = request["runner"];
                }
            }
            else if (command == "testEmail")
            {
                new EmailTestService(request, response).Process();
            }
            else if (command == "tasks")
            {
                DispatchTasksList();
            }
            else if (command == "setLocalDate")
            {
                var time = LocalTime.Now.TimeOfDay;
                if (request.Has("time"))
                {
                    time = TimeSpan.Parse(request["time"]);
                }

                var date = LocalTime.Today;
                if (request.Has("date"))
                {
                    date = request["date"].To <DateTime>();
                }

                date = date.Add(time);

                var trueOrigin = DateTime.Now;

                LocalTime.RedefineNow(() => { return(date.Add(DateTime.Now.Subtract(trueOrigin))); });
                response.Clear();
                response.EndWith(date.ToString("yyyy-MM-dd @ HH:mm:ss"));
            }
            else if (command == "remove_snapshot")
            {
                Snapshot.RemoveSnapshot(request["name"]);
            }
            else if (command == "inject.service.response")
            {
                IntegrationTestInjector.Inject(request["service"], request["request"], request["response"]);
            }
        }
Exemple #2
0
        public static async Task ProcessCommand(string command)
        {
            if (command.IsEmpty())
            {
                return;
            }

            if (!IsTddExecutionMode())
            {
                throw new Exception("Invalid command in non TDD mode.");
            }

            var request  = Context.Http.Request;
            var response = Context.Http.Response;

            var isShared = request.Param("mode") == "shared";

            if (command == "snap")
            {
                await new Snapshot(request.Param("name"), isShared).Create(Context.Http);
            }
            else if (command == "restore")
            {
                await new Snapshot(request.Param("name"), isShared).Restore(Context.Http);
            }
            else if (command == "remove_snapshots")
            {
                Snapshot.RemoveSnapshots();
            }
            else if (command == "snapshots_list")
            {
                response.EndWith(JsonConvert.SerializeObject(Snapshot.GetList(isShared)));
            }
            else if (command == "snapExists")
            {
                if (new Snapshot(request.Param("name"), isShared).Exists())
                {
                    response.EndWith("true");
                }
                else
                {
                    response.EndWith("false");
                }
            }
            else if (command.IsAnyOf("start", "run", "ran", "cancel", "restart"))
            {
                await InitiateTempDatabase(enforceRestart : true, mustRenew : true);

                DatabaseChangeWatcher.Restart();
                if (request.Has("runner"))
                {
                    CurrentRunner = request.Param("runner");
                }
            }
            else if (command == "testEmail")
            {
                await(await new EmailTestService(request, response).Initialize()).Process();
            }
            else if (command == "dbChanges")
            {
                DatabaseChangeWatcher.DispatchChanges();
            }
            else if (command == "tasks")
            {
                await DispatchTasksList();
            }
            else if (command == "setLocalDate")
            {
                if (request.Param("date") == "now")
                {
                    // reset to normal
                    LocalTime.RedefineNow(overriddenNow: null);
                    response.EndWith(LocalTime.Now.ToString("yyyy-MM-dd @ HH:mm:ss"));
                }

                var time = LocalTime.Now.TimeOfDay;
                if (request.Has("time"))
                {
                    time = TimeSpan.Parse(request.Param("time"));
                }

                var date = LocalTime.Today;
                if (request.Has("date"))
                {
                    date = request.Param("date").To <DateTime>();
                }

                date = date.Add(time);

                var trueOrigin = DateTime.Now;

                LocalTime.RedefineNow(() => { return(date.Add(DateTime.Now.Subtract(trueOrigin))); });
                response.EndWith(date.ToString("yyyy-MM-dd @ HH:mm:ss"));
            }
            else if (command == "remove_snapshot")
            {
                Snapshot.RemoveSnapshot(request.Param("name"));
            }
            else if (command == "inject.service.response")
            {
                var serviceType = AppDomain.CurrentDomain.GetAssemblies()
                                  .SelectMany(a => a.GetTypes())
                                  .Where(x => x.InhritsFrom(typeof(IntegrationService)))
                                  .SingleOrDefault(x => x.Name == request.Param("service"));

                if (serviceType == null)
                {
                    throw new Exception("Cannot find a class named " + request.Param("service") + " in the currently loaded assemblies, which inherits from IntegrationService<,>.");
                }

                new Thread(new ThreadStart(async() =>
                                           await IntegrationTestInjector.Inject(serviceType, request.Param("request"), request.Param("response"))))
                {
                    IsBackground = true
                }
                .Start();
            }
        }