Ejemplo n.º 1
0
        public ActionResult Add(UnitTestAddModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var componentRepository = CurrentAccountDbContext.GetComponentRepository();
            var component           = componentRepository.GetById(model.ComponentId.Value);

            var unitTestTypeRepository = CurrentAccountDbContext.GetUnitTestTypeRepository();
            var unitTestType           = unitTestTypeRepository.GetById(model.UnitTestTypeId);

            var client = GetDispatcherClient();
            var data   = new GetOrCreateUnitTestRequestData()
            {
                ComponentId    = component.Id,
                UnitTestTypeId = unitTestType.Id,
                DisplayName    = model.DisplayName,
                SystemName     = model.DisplayName
            };

            var response = client.GetOrCreateUnitTest(CurrentUser.AccountId, data);
            var unitTest = response.Data;

            this.SetTempMessage(TempMessageType.Success, string.Format("Добавлена проверка <a href='{1}' class='alert-link'>{0}</a>", unitTest.DisplayName, Url.Action("ResultDetails", new { id = unitTest.Id })));
            return(RedirectToAction("Index"));
        }
Ejemplo n.º 2
0
        public GetOrCreateUnitTestResponse GetOrCreateUnitTest(Guid accountId, GetOrCreateUnitTestRequestData data)
        {
            var request = GetRequest <GetOrCreateUnitTestRequest>(accountId);

            request.Data = data;
            var dispatcher = DispatcherHelper.GetDispatcherService();

            return(dispatcher.GetOrCreateUnitTest(request));
        }
Ejemplo n.º 3
0
        public void WrongUnitTestTypeIdTest()
        {
            var account    = TestHelper.GetTestAccount();
            var dispatcher = TestHelper.GetDispatcherClient();
            var component  = TestHelper.GetTestApplicationComponent(account);

            var data = new GetOrCreateUnitTestRequestData()
            {
                UnitTestTypeId = new Guid("00000000-1111-2222-3333-444444444444"),
                ComponentId    = component.Id,
                SystemName     = "UnitTest." + Guid.NewGuid()
            };

            var response = dispatcher.GetOrCreateUnitTest(account.Id, data);

            Assert.False(response.Success);
        }
        public UnitTest SaveSimpleCheck(T model)
        {
            var dispatcher          = GetDispatcherClient();
            var unitTestDisplayName = GetUnitTestDisplayName(model);

            if (model.Id.HasValue)
            {
                var unitTest   = GetUnitTestById(model.Id.Value);
                var updateData = new UpdateUnitTestRequestData()
                {
                    ComponentId   = unitTest.ComponentId,
                    DisplayName   = unitTestDisplayName,
                    PeriodSeconds = TimeSpanHelper.GetSeconds(model.Period),
                    UnitTestId    = unitTest.Id,
                    ErrorColor    = unitTest.ErrorColor,
                    NoSignalColor = ObjectColor.Gray,
                    SystemName    = unitTest.SystemName,
                    SimpleMode    = true
                };
                dispatcher.UpdateUnitTest(CurrentUser.AccountId, updateData).Check();

                // Зем: название компонента решили не менять, чтобы не было неожиданных сюрпризов,
                // меняешь проверку, а меняется компонент, если нужно изменить название компонента,
                // пусть пользователь сделает это явно вручную

                // Обновим параметры
                SetUnitTestParams(unitTest, model);
                CurrentAccountDbContext.SaveChanges();

                this.SetTempMessage(TempMessageType.Success, string.Format("Обновлена проверка <a href='{1}' class='alert-link'>{0}</a>", unitTest.DisplayName, Url.Action("Edit", "Checks", new { id = unitTest.Id })));
                return(unitTest);
            }
            else // создание проверки
            {
                // Создаём компонент только если его ещё нет
                ComponentInfo component;
                if (!model.ComponentId.HasValue)
                {
                    // Создадим папку для компонента
                    var componentRepository = CurrentAccountDbContext.GetComponentRepository();
                    var root = componentRepository.GetRoot();

                    var createFolderResponse = dispatcher.GetOrCreateComponent(CurrentUser.AccountId, new GetOrCreateComponentRequestData()
                    {
                        SystemName        = GetFolderSystemName(model),
                        DisplayName       = GetFolderDisplayName(model),
                        TypeId            = SystemComponentTypes.Folder.Id,
                        ParentComponentId = root.Id
                    });

                    if (!createFolderResponse.Success)
                    {
                        throw new UserFriendlyException("Ошибка создания папки для проверки: " + createFolderResponse.ErrorMessage);
                    }

                    var folder = createFolderResponse.Data.Component;

                    // Создадим тип компонента
                    var createComponentTypeResponse = dispatcher.GetOrCreateComponentType(CurrentUser.AccountId, new GetOrCreateComponentTypeRequestData()
                    {
                        SystemName  = GetTypeSystemName(model),
                        DisplayName = GetTypeDisplayName(model)
                    });

                    if (!createComponentTypeResponse.Success)
                    {
                        throw new UserFriendlyException("Ошибка создания типа компонента для проверки: " + createComponentTypeResponse.ErrorMessage);
                    }

                    var componentType = createComponentTypeResponse.Data;

                    // Создадим компонент
                    var componentId             = Guid.NewGuid();
                    var createComponentResponse = dispatcher.GetOrCreateComponent(CurrentUser.AccountId, new GetOrCreateComponentRequestData()
                    {
                        NewId             = componentId,
                        SystemName        = GetComponentSystemName(componentId),
                        DisplayName       = GetComponentDisplayName(model),
                        TypeId            = componentType.Id,
                        ParentComponentId = folder.Id
                    });

                    if (!createComponentResponse.Success)
                    {
                        throw new UserFriendlyException("Ошибка создания компонента для проверки: " + createComponentResponse.ErrorMessage);
                    }

                    component = createComponentResponse.Data.Component;
                }
                else
                {
                    component = dispatcher.GetComponentById(CurrentUser.AccountId, model.ComponentId.Value).Data;
                }

                // Создадим проверку
                var unitTestId = Guid.NewGuid();

                var createUnitTestData = new GetOrCreateUnitTestRequestData()
                {
                    ActualTimeSecs = null,
                    ComponentId    = component.Id,
                    DisplayName    = unitTestDisplayName,
                    ErrorColor     = UnitTestResult.Alarm,
                    NewId          = unitTestId,
                    SimpleMode     = true,
                    NoSignalColor  = ObjectColor.Gray,
                    PeriodSeconds  = TimeSpanHelper.GetSeconds(model.Period),
                    SystemName     = UnitTestHelper.GetDynamicSystemName(unitTestId),
                    UnitTestTypeId = GetUnitTestTypeId()
                };
                dispatcher.GetOrCreateUnitTest(CurrentUser.AccountId, createUnitTestData).Check();
                var unitTest = GetUnitTestById(unitTestId);
                SetUnitTestParams(unitTest, model);

                CurrentAccountDbContext.SaveChanges();

                this.SetTempMessage(TempMessageType.Success, string.Format("Добавлена проверка <a href='{1}' class='alert-link'>{0}</a>", unitTest.DisplayName, Url.Action("Edit", "Checks", new { id = unitTest.Id })));
                return(unitTest);
            }
        }
Ejemplo n.º 5
0
        public IUnitTestCacheReadObject GetOrCreateUnitTest(Guid accountId, GetOrCreateUnitTestRequestData data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }
            if (string.IsNullOrEmpty(data.SystemName))
            {
                throw new ParameterRequiredException("SystemName");
            }
            if (data.ComponentId == null)
            {
                throw new ParameterRequiredException("ComponentId");
            }
            if (data.UnitTestTypeId == null)
            {
                throw new ParameterRequiredException("UnitTestTypeId");
            }
            var cache       = new AccountCache(accountId);
            var componentId = data.ComponentId.Value;
            var systemName  = data.SystemName;

            // проверим, что тип проверки существует
            var unitTestTypeId = data.UnitTestTypeId.Value;

            if (!SystemUnitTestTypes.IsSystem(unitTestTypeId))
            {
                var unitTestType = AllCaches.UnitTestTypes.Find(new AccountCacheRequest()
                {
                    AccountId = accountId,
                    ObjectId  = unitTestTypeId
                });

                if (unitTestType == null)
                {
                    throw new UnknownUnitTestTypeIdException(unitTestTypeId);
                }
            }

            // получим компонент
            var component = cache.Components.Read(componentId);

            if (component == null)
            {
                throw new UnknownComponentIdException(componentId, accountId);
            }

            // проверим есть ли у него ссылка на проверку
            var unitTestRef = component.UnitTests.FindByName(data.SystemName);

            if (unitTestRef != null)
            {
                // ссылка есть, вернем существующую проверку
                var unitTest = cache.UnitTests.Read(unitTestRef.Id);
                if (unitTest == null)
                {
                    throw new Exception("unitTest == null");
                }
                return(unitTest);
            }

            // ссылки нет
            using (var writeComponent = cache.Components.Write(componentId))
            {
                // проверим ссылку еще раз
                unitTestRef = writeComponent.UnitTests.FindByName(data.SystemName);
                if (unitTestRef != null)
                {
                    var unitTest = cache.UnitTests.Read(unitTestRef.Id);
                    if (unitTest == null)
                    {
                        throw new Exception("unitTest == null");
                    }
                    return(unitTest);
                }

                // создадим проверку
                var accountDbContext = Context.GetAccountDbContext(accountId);

                // Проверим лимиты
                var checker     = AccountLimitsCheckerManager.GetCheckerForAccount(accountId);
                var checkResult = checker.CheckMaxUnitTestsCount(accountDbContext);

                if (!checkResult.Success)
                {
                    throw new OverLimitException(checkResult.Message);
                }

                // проверка лимитов пройдена
                if (string.IsNullOrWhiteSpace(data.DisplayName))
                {
                    data.DisplayName = data.SystemName;
                }

                // создаем юнит-тест
                var unitTestObj = Create(accountId, componentId, unitTestTypeId, systemName, data.DisplayName, data.NewId);
                unitTestRef = new CacheObjectReference(unitTestObj.Id, unitTestObj.SystemName);
                writeComponent.WriteUnitTests.Add(unitTestRef);
                writeComponent.BeginSave();

                checker.RefreshApiChecksCount();

                // обновим свойства
                using (var unitTestCache = cache.UnitTests.Write(unitTestObj.Id))
                {
                    unitTestCache.NoSignalColor = data.NoSignalColor;
                    unitTestCache.ErrorColor    = data.ErrorColor;
                    unitTestCache.ActualTime    = TimeSpanHelper.FromSeconds(data.ActualTimeSecs);
                    if (data.PeriodSeconds.HasValue)
                    {
                        unitTestCache.PeriodSeconds = data.PeriodSeconds;
                    }
                    if (data.SimpleMode.HasValue)
                    {
                        unitTestCache.SimpleMode = data.SimpleMode.Value;
                    }
                    unitTestCache.BeginSave();
                    unitTestCache.WaitSaveChanges();
                    return(unitTestCache);
                }
            }
        }