示例#1
0
        public async Task Test_CreateJob_Should_SendMessage_And_ReturnOk()
        {
            // arrange
            var faker = new Faker();
            var form  = new CreateJobForm()
            {
                JobId     = NewId.NextGuid(),
                Type      = faker.Lorem.Slug(),
                Namespace = faker.Internet.DomainName()
            };

            _mockSendEndpointProvider
            .SetupMessage <CreateJob>(new
            {
                JobId     = form.JobId,
                Type      = form.Type,
                Namespace = form.Namespace,
            });

            // act
            var result = await _service.CreateJob(form);

            // assert
            result.Should().NotBeNull();
            result.IsOk().Should().BeTrue();

            _mocks.Verify();
        }
示例#2
0
        public async Task <IActionResult> CreateJob([FromBody] CreateJobForm createJob)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            _logger.LogInformation("Creating job = [{Job}]", createJob);
            var result = await _jobService.CreateJob(createJob);

            if (result.IsConflict())
            {
                _logger.LogWarning("Job already exists with Id = {JobId}", createJob.JobId);
                return(Conflict(result.ErrorMessage));
            }

            var url = Url.RouteUrl(nameof(GetJob), new { Id = createJob.JobId });

            return(Accepted(JsonLinks.Self(url)));
        }
示例#3
0
        public async Task Test_CreateJob_DuplicateGuid_Should_ReturnConflict()
        {
            // arrange
            var job = new JobFaker().Generate();

            await using var context = DbContextFactory.Instance.CreateDbContext <PortAuthorityDbContext>();
            await context.Setup(x => x.Jobs, job);

            var form = new CreateJobForm()
            {
                JobId     = job.JobId,
                Type      = "foo-bar-baz",
                Namespace = "com.portauthority"
            };

            // act
            var result = await _service.CreateJob(form);

            // assert
            result.Should().NotBeNull();
            result.IsConflict().Should().BeTrue();
            result.ErrorMessage.Message.Should().StartWith("Job already exists");
        }
示例#4
0
        public async Task <IResult> CreateJob(CreateJobForm form)
        {
            var exists = await _dbContext.Jobs.AnyAsync(x => x.JobId == form.JobId);

            if (exists)
            {
                _logger.LogWarning("Job already exists with ID = {JobId}", form.JobId);
                return(Result.Conflict($"Job already exists with ID {form.JobId}"));
            }

            await _sendEndpointProvider.Send <CreateJob>(new
            {
                JobId         = form.JobId,
                CorrelationId = form.CorrelationId,
                Type          = form.Type,
                Namespace     = form.Namespace,
                Meta          = form.Meta
            });

            _logger.LogInformation("Sent <{MessageType}> command", nameof(CreateJob));

            return(Result.Ok());
        }