Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AgentRepository" /> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public AgentRepository(YellowJacketContext context)
        {
            _context = context;

            //if (_context.Agents.Any())
            //    return;

            // Task.Run(async () =>
            //{
            //    await Add(new AgentEntity { Id = "VM0002", Name = "Agent 2", Status = "Running", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0003", Name = "Agent 3", Status = "Idle", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0004", Name = "Agent 4", Status = "Running", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0005", Name = "Agent 5", Status = "Idle", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0006", Name = "Agent 6", Status = "Idle", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0007", Name = "Agent 7", Status = "Running", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0008", Name = "Agent 8", Status = "Idle", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0009", Name = "Agent 9", Status = "Running", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0010", Name = "Agent 10", Status = "Idle", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0011", Name = "Agent 11", Status = "Idle", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0012", Name = "Agent 12", Status = "Running", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0013", Name = "Agent 13", Status = "Idle", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0014", Name = "Agent 14", Status = "Running", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //    await Add(new AgentEntity { Id = "VM0015", Name = "Agent 15", Status = "Idle", RegisteredOn = DateTime.Now, LastUpdateOn = DateTime.Now });
            //});
        }
Пример #2
0
        public void ValidateJob_EmptyName_ValidationError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("AddJob_EmptyName_ValidationError")
                                                             .Options;

            JobModel model = new JobModel
            {
                Features    = new List <string>(),
                PackageName = "MyPackage.zip",
            };

            // Act
            ValidationResult validationResult;

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IJobRepository jobRepository = new JobRepository(context);

                IJobService service = new JobService(jobRepository, GetMapper());

                validationResult = service.Validate(model);
            }

            // Assert
            const int    expectedValidationErrorCount = 1;
            const string errorMessage = "'Name' should not be empty.";

            Assert.That(validationResult.Errors.Count, Is.EqualTo(expectedValidationErrorCount),
                        $"The validation error count should be {expectedValidationErrorCount}");

            Assert.That(validationResult.Errors.First().ErrorMessage, Is.EqualTo(errorMessage),
                        $"The expected error message '{errorMessage}' doesn't match the actual one {validationResult.Errors.First().ErrorMessage}");
        }
Пример #3
0
        public async Task UpdateAgent_ExistingAgent_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("UpdateAgent_ExistingAgent_NoError")
                                                             .Options;

            const string agentName = "MyAgent";

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                context.Agents.Add(new AgentEntity
                {
                    Name         = agentName,
                    LastUpdateOn = DateTime.Now,
                    RegisteredOn = DateTime.Now,
                    Status       = AgentStatus.Idle.ToString()
                });

                context.SaveChanges();
            }

            List <AgentModel> models;

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                models = await service.GetAll();
            }

            AgentModel model = models.First();

            string expectedStatus = AgentStatus.Running.ToString();

            // Assert
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                model.Status = AgentStatus.Running.ToString();

                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                model.Status = expectedStatus;

                model = await service.Update(model);

                Assert.That(
                    model.Status,
                    Is.EqualTo(expectedStatus),
                    $"The actual agent status {model.Status} should be equal to the expected value {expectedStatus}");
            }
        }
Пример #4
0
        public async Task GetAllAgents_ExistingAgents_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("GetAllAgent_ExistingAgents_NoError")
                                                             .Options;

            List <AgentModel> expectedAgents = new List <AgentModel>
            {
                new AgentModel
                {
                    Name         = "AgentA",
                    LastUpdateOn = DateTime.Now,
                    RegisteredOn = DateTime.Now,
                    Status       = AgentStatus.Idle.ToString()
                },
                new AgentModel
                {
                    Name         = "AgentB",
                    LastUpdateOn = DateTime.Now,
                    RegisteredOn = DateTime.Now,
                    Status       = AgentStatus.Idle.ToString()
                }
            };

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                foreach (AgentModel model in expectedAgents)
                {
                    await service.Add(model);
                }

                context.SaveChanges();
            }

            List <AgentModel> actualAgents;

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                actualAgents = await service.GetAll();
            }

            Assert.That(
                actualAgents.Count,
                Is.EqualTo(2),
                $"The actual agent list count {actualAgents.Count} should be equal to the expected one {expectedAgents.Count}");
        }
Пример #5
0
        public async Task RemoveAgent_ExistingAgent_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("RemoveAgent_ExistingAgent_NoError")
                                                             .Options;

            const string agentName = "MyAgent";

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                context.Agents.Add(new AgentEntity
                {
                    Name         = agentName,
                    LastUpdateOn = DateTime.Now,
                    RegisteredOn = DateTime.Now,
                    Status       = AgentStatus.Idle.ToString()
                });

                context.SaveChanges();
            }

            List <AgentModel> models;

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                models = await service.GetAll();
            }

            AgentModel model = models.First();

            // Assert
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                await service.Remove(model.Id);

                const int expectedCount = 0;

                Assert.That(
                    expectedCount,
                    Is.EqualTo(Convert.ToInt32(context.Agents.Count())),
                    $"The agents count should be {expectedCount}.");
            }
        }
Пример #6
0
        public async Task RemoveJob_ExistingJob_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("RemoveJob_ExistingJob_NoError")
                                                             .Options;

            const string jobName = "MyJob";

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                context.Jobs.Add(new JobEntity
                {
                    Name = jobName
                });

                context.SaveChanges();
            }

            List <JobModel> models;

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IJobRepository jobRepository = new JobRepository(context);

                IJobService service = new JobService(jobRepository, GetMapper());

                models = await service.GetAll();
            }

            JobModel model = models.First();

            // Assert
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IJobRepository jobRepository = new JobRepository(context);

                IJobService service = new JobService(jobRepository, GetMapper());

                await service.Remove(model.Id);

                const int expectedCount = 0;

                Assert.That(
                    expectedCount,
                    Is.EqualTo(Convert.ToInt32(context.Jobs.Count())),
                    $"The jobs count should be {expectedCount}.");
            }
        }
Пример #7
0
        public async Task GetAllJobs_ExistingJobs_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("GetAllJob_ExistingJobs_NoError")
                                                             .Options;

            List <JobModel> expectedJobs = new List <JobModel>
            {
                new JobModel
                {
                    Name = "JobA"
                },
                new JobModel
                {
                    Name = "JobB"
                }
            };

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IJobRepository jobRepository = new JobRepository(context);

                IJobService service = new JobService(jobRepository, GetMapper());

                foreach (JobModel model in expectedJobs)
                {
                    await service.Add(model);
                }

                context.SaveChanges();
            }

            List <JobModel> actualJobs;

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IJobRepository jobRepository = new JobRepository(context);

                IJobService service = new JobService(jobRepository, GetMapper());

                actualJobs = await service.GetAll();
            }

            Assert.That(
                actualJobs.Count,
                Is.EqualTo(2),
                $"The actual job list count {actualJobs.Count} should be equal to the expected one {expectedJobs.Count}");
        }
Пример #8
0
        public async Task FindAgent_ExistingAgent_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("FindAgent_ExistingAgent_NoError")
                                                             .Options;

            const string agentName = "MyAgent";

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                context.Agents.Add(new AgentEntity
                {
                    Name         = agentName,
                    LastUpdateOn = DateTime.Now,
                    RegisteredOn = DateTime.Now,
                    Status       = AgentStatus.Idle.ToString()
                });

                context.SaveChanges();
            }

            List <AgentModel> models;

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                models = await service.GetAll();
            }

            AgentModel model = models.First();

            // Assert
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                model = await service.Find(model.Id);

                Assert.That(model, !Is.Null, "The agent shouldn't be null.");
            }
        }
Пример #9
0
        public async Task FindJob_ExistingJob_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("FindJob_ExistingJob_NoError")
                                                             .Options;

            const string jobName = "MyJob";

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                context.Jobs.Add(new JobEntity
                {
                    Name = jobName
                });

                context.SaveChanges();
            }

            List <JobModel> models;

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IJobRepository jobRepository = new JobRepository(context);

                IJobService service = new JobService(jobRepository, GetMapper());

                models = await service.GetAll();
            }

            JobModel model = models.First();

            // Assert
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IJobRepository jobRepository = new JobRepository(context);

                IJobService service = new JobService(jobRepository, GetMapper());

                model = await service.Find(model.Id);

                Assert.That(model, !Is.Null, "The job shouldn't be null.");
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="ConfigurationRepository" /> class.
        /// </summary>
        /// <param name="context">The context.</param>
        public ConfigurationRepository(YellowJacketContext context)
        {
            _context = context;

            if (_context.Agents.Any())
            {
                return;
            }

            Task.Run(async() =>
            {
                await _context.Configurations.AddAsync(new ConfigurationEntity {
                    Id = "main"
                });

                await _context.SaveChangesAsync();
            });
        }
Пример #11
0
        public async Task AddAgent_ValidAgent_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("AddAgent_ValidAgent_NoError")
                                                             .Options;

            const string agentName = "MyAgent";

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IAgentRepository agentRepository = new AgentRepository(context);

                IAgentService service = new AgentService(agentRepository, GetMapper());

                AgentModel model = new AgentModel
                {
                    Name         = agentName,
                    LastUpdateOn = DateTime.Now,
                    RegisteredOn = DateTime.Now,
                    Status       = AgentStatus.Idle.ToString()
                };

                await service.Add(model);
            }

            // Assert
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                const int expectedCount = 1;

                Assert.That(
                    expectedCount, Is.EqualTo(Convert.ToInt32(context.Agents.Count())),
                    $"The agents count should be {expectedCount}.");

                Assert.That(
                    agentName,
                    Is.EqualTo(context.Agents.Single().Name),
                    $"The expected agent name {agentName} doesn't match the actual value {context.Agents.Single().Name}");
            }
        }
Пример #12
0
        public async Task AddJob_ValidJob_NoError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("AddJob_ValidJob_NoError")
                                                             .Options;

            const string jobName = "MyJob";

            JobModel model = new JobModel
            {
                Name        = jobName,
                Features    = new List <string>(),
                PackageName = "MyPackage.zip",
            };

            // Act
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IJobRepository jobRepository = new JobRepository(context);

                IJobService service = new JobService(jobRepository, GetMapper());

                await service.Add(model);
            }

            // Assert
            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                const int expectedCount = 1;

                Assert.That(
                    expectedCount, Is.EqualTo(Convert.ToInt32(context.Jobs.Count())),
                    $"The job count should be {expectedCount}.");

                Assert.That(
                    jobName,
                    Is.EqualTo(context.Jobs.Single().Name),
                    $"The expected job name {jobName} doesn't match the actual value {context.Jobs.Single().Name}");
            }
        }
Пример #13
0
        public void ValidateJob_NameLongerThan25Characters_ValidationError()
        {
            // Arrange
            DbContextOptions <YellowJacketContext> options = new DbContextOptionsBuilder <YellowJacketContext>()
                                                             .UseInMemoryDatabase("ValidateJob_NameLongerThan25Characters_ValidationError")
                                                             .Options;

            const string jobName = "abcdefghijklmnopqrstuvwxyz";

            JobModel model = new JobModel
            {
                Name        = jobName,
                Features    = new List <string>(),
                PackageName = "MyPackage.zip",
            };

            // Act
            ValidationResult validationResult;

            using (YellowJacketContext context = new YellowJacketContext(options))
            {
                IJobRepository jobRepository = new JobRepository(context);

                IJobService service = new JobService(jobRepository, GetMapper());

                validationResult = service.Validate(model);
            }

            // Assert
            const int    expectedValidationErrorCount = 1;
            const string errorMessage = "'Name' must be between 1 and 25 characters. You entered 26 characters.";

            Assert.That(validationResult.Errors.Count, Is.EqualTo(expectedValidationErrorCount),
                        $"The validation error count should be {expectedValidationErrorCount}");

            Assert.That(validationResult.Errors.First().ErrorMessage, Is.EqualTo(errorMessage),
                        $"The expected error message '{errorMessage}' doesn't match the actual one {validationResult.Errors.First().ErrorMessage}");
        }
Пример #14
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JobRepository" /> class.
 /// </summary>
 /// <param name="context">The context.</param>
 public JobRepository(YellowJacketContext context)
 {
     _context = context;
 }