public async Task <IActionResult> Put([FromBody] JobProfileSkillSegmentModel jobProfileSkillSegmentModel)
        {
            logService.LogInformation($"{PutActionName} has been called");

            if (jobProfileSkillSegmentModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var existingDocument = await skillSegmentService.GetByIdAsync(jobProfileSkillSegmentModel.DocumentId).ConfigureAwait(false);

            if (existingDocument == null)
            {
                return(new StatusCodeResult((int)HttpStatusCode.NotFound));
            }

            if (jobProfileSkillSegmentModel.SequenceNumber <= existingDocument.SequenceNumber)
            {
                return(new StatusCodeResult((int)HttpStatusCode.AlreadyReported));
            }

            jobProfileSkillSegmentModel.Etag        = existingDocument.Etag;
            jobProfileSkillSegmentModel.SocLevelTwo = existingDocument.SocLevelTwo;

            var response = await skillSegmentService.UpsertAsync(jobProfileSkillSegmentModel).ConfigureAwait(false);

            return(new StatusCodeResult((int)response));
        }
示例#2
0
        public async Task ExistingSegmentReturnsOk()
        {
            // Arrange
            var documentId = Guid.NewGuid();

            var deleteUri = new Uri($"{SegmentUrl}/{documentId}", UriKind.Relative);

            var skillSegmentModel = new JobProfileSkillSegmentModel()
            {
                DocumentId    = documentId,
                CanonicalName = documentId.ToString().ToLowerInvariant(),
                SocLevelTwo   = "12PostSoc",
                Data          = new JobProfileSkillSegmentDataModel(),
            };

            var client = factory.CreateClient();

            client.DefaultRequestHeaders.Accept.Clear();

            await client.PostAsync(SegmentUrl, skillSegmentModel, new JsonMediaTypeFormatter()).ConfigureAwait(false);

            // Act
            var response = await client.DeleteAsync(deleteUri).ConfigureAwait(false);

            // Assert
            Assert.Equal(HttpStatusCode.OK, response.StatusCode);
        }
        public async Task SegmentControllerPutReturnsSuccessForUpdate()
        {
            // Arrange
            var existingModel = new JobProfileSkillSegmentModel {
                SequenceNumber = 123
            };
            var modelToUpsert = new JobProfileSkillSegmentModel {
                SequenceNumber = 124
            };

            var controller = BuildSegmentController();

            var expectedUpsertResponse = HttpStatusCode.OK;

            A.CallTo(() => FakeSkillSegmentService.GetByIdAsync(A <Guid> .Ignored)).Returns(existingModel);
            A.CallTo(() => FakeSkillSegmentService.UpsertAsync(A <JobProfileSkillSegmentModel> .Ignored)).Returns(expectedUpsertResponse);

            // Act
            var result = await controller.Put(modelToUpsert).ConfigureAwait(false);

            // Assert
            A.CallTo(() => FakeSkillSegmentService.UpsertAsync(A <JobProfileSkillSegmentModel> .Ignored)).MustHaveHappenedOnceExactly();
            var statusCodeResult = Assert.IsType <StatusCodeResult>(result);

            Assert.Equal((int)HttpStatusCode.OK, statusCodeResult.StatusCode);

            controller.Dispose();
        }
        public async Task <IActionResult> Post([FromBody] JobProfileSkillSegmentModel jobProfileSkillSegmentModel)
        {
            logService.LogInformation($"{PostActionName} has been called");

            if (jobProfileSkillSegmentModel == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var existingDocument = await skillSegmentService.GetByIdAsync(jobProfileSkillSegmentModel.DocumentId).ConfigureAwait(false);

            if (existingDocument != null)
            {
                return(new StatusCodeResult((int)HttpStatusCode.AlreadyReported));
            }

            var response = await skillSegmentService.UpsertAsync(jobProfileSkillSegmentModel).ConfigureAwait(false);

            logService.LogInformation($"{PostActionName} has upserted content for: {jobProfileSkillSegmentModel.CanonicalName}");

            return(new StatusCodeResult((int)response));
        }
        public async Task WhenAddingNewArticleReturnCreated()
        {
            // Arrange
            var url               = "/segment";
            var documentId        = Guid.NewGuid();
            var skillSegmentModel = new JobProfileSkillSegmentModel()
            {
                DocumentId    = documentId,
                CanonicalName = documentId.ToString().ToLowerInvariant(),
                SocLevelTwo   = "12PostSoc",
                Data          = new JobProfileSkillSegmentDataModel(),
            };
            var client = factory.CreateClient();

            client.DefaultRequestHeaders.Accept.Clear();

            // Act
            var response = await client.PostAsync(url, skillSegmentModel, new JsonMediaTypeFormatter()).ConfigureAwait(false);

            // Assert
            response.EnsureSuccessStatusCode();
            response.StatusCode.Should().Be(HttpStatusCode.Created);

            // Cleanup
            await client.DeleteAsync(string.Concat(url, "/", documentId)).ConfigureAwait(false);
        }
示例#6
0
        private async Task <HttpStatusCode> UpsertAndRefreshSegmentModel(JobProfileSkillSegmentModel existingSegmentModel)
        {
            var result = await repository.UpsertAsync(existingSegmentModel).ConfigureAwait(false);

            if (result == HttpStatusCode.OK || result == HttpStatusCode.Created)
            {
                var refreshJobProfileSegmentServiceBusModel = mapper.Map <RefreshJobProfileSegmentServiceBusModel>(existingSegmentModel);

                await jobProfileSegmentRefreshService.SendMessageAsync(refreshJobProfileSegmentServiceBusModel).ConfigureAwait(false);
            }

            return(result);
        }
示例#7
0
        public async Task <HttpStatusCode> UpsertAsync(JobProfileSkillSegmentModel skillSegmentModel)
        {
            if (skillSegmentModel == null)
            {
                throw new ArgumentNullException(nameof(skillSegmentModel));
            }

            if (skillSegmentModel.Data == null)
            {
                skillSegmentModel.Data = new JobProfileSkillSegmentDataModel();
            }

            return(await UpsertAndRefreshSegmentModel(skillSegmentModel).ConfigureAwait(false));
        }
        public async Task GetByIdReturnsNullWhenMissingInRepository()
        {
            // arrange
            var documentId = Guid.NewGuid();
            JobProfileSkillSegmentModel expectedResult = null;

            A.CallTo(() => repository.GetAsync(A <Expression <Func <JobProfileSkillSegmentModel, bool> > > .Ignored)).Returns(expectedResult);

            // act
            var result = await skillSegmentService.GetByIdAsync(documentId).ConfigureAwait(false);

            // assert
            A.CallTo(() => repository.GetAsync(A <Expression <Func <JobProfileSkillSegmentModel, bool> > > .Ignored)).MustHaveHappenedOnceExactly();
            Assert.Equal(expectedResult, result);
        }
示例#9
0
        public async Task SegmentControllerPutReturnsBadResultWhenModelIsInvalid()
        {
            // Arrange
            var jobProfileSkillSegmentModel = new JobProfileSkillSegmentModel();
            var controller = BuildSegmentController();

            controller.ModelState.AddModelError(string.Empty, "Model is not valid");

            // Act
            var result = await controller.Post(jobProfileSkillSegmentModel).ConfigureAwait(false);

            // Assert
            var statusResult = Assert.IsType <BadRequestObjectResult>(result);

            Assert.Equal((int)HttpStatusCode.BadRequest, statusResult.StatusCode);

            controller.Dispose();
        }
示例#10
0
        public async Task SegmentControllerPostReturnsAlreadyReportedIfDocumentAlreadyExists()
        {
            // Arrange
            var jobProfileSkillSegmentModel = new JobProfileSkillSegmentModel();
            var controller = BuildSegmentController();

            A.CallTo(() => FakeSkillSegmentService.GetByIdAsync(A <Guid> .Ignored)).Returns(jobProfileSkillSegmentModel);

            // Act
            var result = await controller.Post(jobProfileSkillSegmentModel).ConfigureAwait(false);

            // Assert
            A.CallTo(() => FakeSkillSegmentService.UpsertAsync(A <JobProfileSkillSegmentModel> .Ignored)).MustNotHaveHappened();
            var statusCodeResult = Assert.IsType <StatusCodeResult>(result);

            Assert.Equal((int)HttpStatusCode.AlreadyReported, statusCodeResult.StatusCode);

            controller.Dispose();
        }
示例#11
0
        public async Task ReturnsNoContentWhenNoData(string mediaTypeName)
        {
            // Arrange
            JobProfileSkillSegmentModel expectedResult = null;
            var controller = BuildSegmentController(mediaTypeName);

            A.CallTo(() => FakeSkillSegmentService.GetByNameAsync(A <string> .Ignored)).Returns(expectedResult);

            // Act
            var result = await controller.Document(Article).ConfigureAwait(false);

            // Assert
            A.CallTo(() => FakeSkillSegmentService.GetByNameAsync(A <string> .Ignored)).MustHaveHappenedOnceExactly();

            var statusResult = Assert.IsType <NoContentResult>(result);

            Assert.Equal((int)HttpStatusCode.NoContent, statusResult.StatusCode);

            controller.Dispose();
        }
示例#12
0
        public async Task <HttpStatusCode> PutAsync(JobProfileSkillSegmentModel skillSegmentModel)
        {
            var url = new Uri($"{segmentClientOptions?.BaseAddress}segment");

            ConfigureHttpClient();

            using (var content = new ObjectContent(typeof(JobProfileSkillSegmentModel), skillSegmentModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json))
            {
                var response = await httpClient.PutAsync(url, content).ConfigureAwait(false);

                if (!response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NotFound)
                {
                    var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                    logService.LogError($"Failure status code '{response.StatusCode}' received with content '{responseContent}', for Put type {typeof(JobProfileSkillSegmentModel)}, Id: {skillSegmentModel?.DocumentId}");
                    response.EnsureSuccessStatusCode();
                }

                return(response.StatusCode);
            }
        }
示例#13
0
        public async Task SegmentControllerPostReturnsSuccessForCreate()
        {
            // Arrange
            var jobProfileSkillSegmentModel = new JobProfileSkillSegmentModel();
            var controller             = BuildSegmentController();
            var expectedUpsertResponse = HttpStatusCode.Created;

            A.CallTo(() => FakeSkillSegmentService.GetByIdAsync(A <Guid> .Ignored)).Returns((JobProfileSkillSegmentModel)null);
            A.CallTo(() => FakeSkillSegmentService.UpsertAsync(A <JobProfileSkillSegmentModel> .Ignored)).Returns(expectedUpsertResponse);

            // Act
            var result = await controller.Post(jobProfileSkillSegmentModel).ConfigureAwait(false);

            // Assert
            A.CallTo(() => FakeSkillSegmentService.UpsertAsync(A <JobProfileSkillSegmentModel> .Ignored)).MustHaveHappenedOnceExactly();
            var statusCodeResult = Assert.IsType <StatusCodeResult>(result);

            Assert.Equal((int)HttpStatusCode.Created, statusCodeResult.StatusCode);

            controller.Dispose();
        }
        public async Task WhenUpdateExistingArticleReturnsAlreadyReported()
        {
            // Arrange
            var url = "/segment";
            var skillSegmentModel = new JobProfileSkillSegmentModel()
            {
                DocumentId    = dataSeeding.Article2Id,
                CanonicalName = "article2_modified",
                SocLevelTwo   = dataSeeding.Article2SocCode,
                Data          = new JobProfileSkillSegmentDataModel(),
            };
            var client = factory.CreateClient();

            client.DefaultRequestHeaders.Accept.Clear();

            // Act
            var response = await client.PostAsync(url, skillSegmentModel, new JsonMediaTypeFormatter()).ConfigureAwait(false);

            // Assert
            response.EnsureSuccessStatusCode();
            response.StatusCode.Should().Be(HttpStatusCode.AlreadyReported);
        }
        public async Task SegmentControllerPutReturnsAlreadyReportedWhenExistingSequenceNumberIsHigher()
        {
            // Arrange
            var existingModel = new JobProfileSkillSegmentModel {
                SequenceNumber = 999
            };
            var modelToUpsert = new JobProfileSkillSegmentModel {
                SequenceNumber = 124
            };
            var controller = BuildSegmentController();

            A.CallTo(() => FakeSkillSegmentService.GetByIdAsync(A <Guid> .Ignored)).Returns(existingModel);

            // Act
            var result = await controller.Put(modelToUpsert).ConfigureAwait(false);

            // Assert
            A.CallTo(() => FakeSkillSegmentService.UpsertAsync(A <JobProfileSkillSegmentModel> .Ignored)).MustNotHaveHappened();
            var statusCodeResult = Assert.IsType <StatusCodeResult>(result);

            Assert.Equal((int)HttpStatusCode.AlreadyReported, statusCodeResult.StatusCode);

            controller.Dispose();
        }