public async Task ReturnsSuccessForJsonMediaType(string mediaTypeName) { // Arrange var expectedResult = new JobProfileTasksSegmentModel(); var controller = BuildSegmentController(mediaTypeName); var apiModel = GetDummyApiModel(); expectedResult.CanonicalName = ArticleName; A.CallTo(() => FakeJobProfileSegmentService.GetByIdAsync(A <Guid> .Ignored)).Returns(expectedResult); A.CallTo(() => FakeMapper.Map <WhatYouWillDoApiModel>(A <JobProfileTasksDataSegmentModel> .Ignored)).Returns(apiModel); // Act var result = await controller.Body(documentId).ConfigureAwait(false); // Assert A.CallTo(() => FakeJobProfileSegmentService.GetByIdAsync(A <Guid> .Ignored)).MustHaveHappenedOnceExactly(); A.CallTo(() => FakeMapper.Map <WhatYouWillDoApiModel>(A <JobProfileTasksDataSegmentModel> .Ignored)).MustHaveHappenedOnceExactly(); var jsonResult = Assert.IsType <OkObjectResult>(result); Assert.IsAssignableFrom <WhatYouWillDoApiModel>(jsonResult.Value); controller.Dispose(); }
public async Task WhenAddingNewArticleReturnCreated() { // Arrange var url = "/segment"; var documentId = Guid.NewGuid(); var tasksSegmentModel = new JobProfileTasksSegmentModel() { DocumentId = documentId, CanonicalName = documentId.ToString().ToLowerInvariant(), SocLevelTwo = "12PostSoc", Data = new JobProfileTasksDataSegmentModel(), }; var client = factory.CreateClient(); client.DefaultRequestHeaders.Accept.Clear(); // Act var response = await client.PostAsync(url, tasksSegmentModel, new JsonMediaTypeFormatter()).ConfigureAwait(false); // Assert response.EnsureSuccessStatusCode(); response.StatusCode.Should().Be(HttpStatusCode.Created); // Cleanup await client.DeleteAsync(string.Concat(url, "/", documentId)).ConfigureAwait(false); }
public async Task <IActionResult> Post([FromBody] JobProfileTasksSegmentModel jobProfileTasksSegmentModel) { logService.LogInformation($"{PostActionName} has been called"); if (jobProfileTasksSegmentModel == null) { logService.LogInformation($"{PostActionName}. No document was passed"); return(BadRequest()); } if (!ModelState.IsValid) { logService.LogInformation($"{PostActionName}. Model state is invalid"); return(BadRequest(ModelState)); } var existingDocument = await jobProfileTasksSegmentService.GetByIdAsync(jobProfileTasksSegmentModel.DocumentId).ConfigureAwait(false); if (existingDocument != null) { return(new StatusCodeResult((int)HttpStatusCode.AlreadyReported)); } var response = await jobProfileTasksSegmentService.UpsertAsync(jobProfileTasksSegmentModel).ConfigureAwait(false); logService.LogInformation($"{PostActionName} has created content for: {jobProfileTasksSegmentModel.CanonicalName}"); return(new StatusCodeResult((int)response)); }
public async Task ReturnsSuccess(string mediaTypeName) { // Arrange var existingModel = new JobProfileTasksSegmentModel { SequenceNumber = 123 }; var modelToUpsert = new JobProfileTasksSegmentModel { SequenceNumber = 124 }; var controller = BuildSegmentController(mediaTypeName); var expectedUpsertResponse = HttpStatusCode.OK; A.CallTo(() => FakeJobProfileSegmentService.GetByIdAsync(A <Guid> .Ignored)).Returns(existingModel); A.CallTo(() => FakeJobProfileSegmentService.UpsertAsync(A <JobProfileTasksSegmentModel> .Ignored)).Returns(expectedUpsertResponse); // Act var result = await controller.Put(modelToUpsert).ConfigureAwait(false); // Assert A.CallTo(() => FakeJobProfileSegmentService.UpsertAsync(A <JobProfileTasksSegmentModel> .Ignored)).MustHaveHappenedOnceExactly(); var statusCodeResult = Assert.IsType <StatusCodeResult>(result); Assert.Equal((int)HttpStatusCode.OK, statusCodeResult.StatusCode); controller.Dispose(); }
private async Task <HttpStatusCode> UpsertAndRefreshSegmentModel(JobProfileTasksSegmentModel 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); }
public async Task GetByNameReturnsNullWhenMissingInRepository() { // arrange JobProfileTasksSegmentModel expectedResult = null; A.CallTo(() => repository.GetAsync(A <Expression <Func <JobProfileTasksSegmentModel, bool> > > .Ignored)).Returns(expectedResult); // act var result = await jobProfileTasksSegmentService.GetByNameAsync("article-name").ConfigureAwait(false); // assert A.CallTo(() => repository.GetAsync(A <Expression <Func <JobProfileTasksSegmentModel, bool> > > .Ignored)).MustHaveHappenedOnceExactly(); Assert.Equal(expectedResult, result); }
public async Task <HttpStatusCode> UpsertAsync(JobProfileTasksSegmentModel tasksSegmentModel) { if (tasksSegmentModel == null) { throw new ArgumentNullException(nameof(tasksSegmentModel)); } if (tasksSegmentModel.Data == null) { tasksSegmentModel.Data = new JobProfileTasksDataSegmentModel(); } return(await UpsertAndRefreshSegmentModel(tasksSegmentModel).ConfigureAwait(false)); }
public async Task ReturnsBadResultWhenModelIsInvalid(string mediaTypeName) { // Arrange var jobProfileTasksSegmentModel = new JobProfileTasksSegmentModel(); var controller = BuildSegmentController(mediaTypeName); controller.ModelState.AddModelError(string.Empty, "Model is not valid"); // Act var result = await controller.Put(jobProfileTasksSegmentModel).ConfigureAwait(false); // Assert var statusResult = Assert.IsType <BadRequestObjectResult>(result); Assert.Equal((int)HttpStatusCode.BadRequest, statusResult.StatusCode); controller.Dispose(); }
public async Task ReturnsNotFoundWhenDocumentDoesNotAlreadyExist() { // Arrange var jobProfileTasksSegmentModel = new JobProfileTasksSegmentModel(); var controller = BuildSegmentController(); A.CallTo(() => FakeJobProfileSegmentService.GetByIdAsync(A <Guid> .Ignored)).Returns((JobProfileTasksSegmentModel)null); // Act var result = await controller.Put(jobProfileTasksSegmentModel).ConfigureAwait(false); // Assert A.CallTo(() => FakeJobProfileSegmentService.UpsertAsync(A <JobProfileTasksSegmentModel> .Ignored)).MustNotHaveHappened(); var statusCodeResult = Assert.IsType <StatusCodeResult>(result); Assert.Equal((int)HttpStatusCode.NotFound, statusCodeResult.StatusCode); controller.Dispose(); }
public async Task ReturnsNoContentWhenNoData(string mediaTypeName) { // Arrange JobProfileTasksSegmentModel expectedResult = null; var controller = BuildSegmentController(mediaTypeName); A.CallTo(() => FakeJobProfileSegmentService.GetByNameAsync(A <string> .Ignored)).Returns(expectedResult); // Act var result = await controller.Document(Article).ConfigureAwait(false); // Assert A.CallTo(() => FakeJobProfileSegmentService.GetByNameAsync(A <string> .Ignored)).MustHaveHappenedOnceExactly(); var statusResult = Assert.IsType <NoContentResult>(result); Assert.Equal((int)HttpStatusCode.NoContent, statusResult.StatusCode); controller.Dispose(); }
public async Task <HttpStatusCode> PutAsync(JobProfileTasksSegmentModel tasksSegmentModel) { var url = new Uri($"{jobProfileClientOptions?.BaseAddress}segment"); using (var content = new ObjectContent(typeof(JobProfileTasksSegmentModel), tasksSegmentModel, new JsonMediaTypeFormatter(), MediaTypeNames.Application.Json)) { ConfigureHttpClient(); var response = await httpClient.PutAsync(url, content).ConfigureAwait(false); if (!response.IsSuccessStatusCode && response.StatusCode != HttpStatusCode.NotFound) { var responseContent = await response.Content.ReadAsStringAsync().ConfigureAwait(false); logger.LogError($"Failure status code '{response.StatusCode}' received with content '{responseContent}', for Put type {typeof(JobProfileTasksSegmentModel)}, Id: {tasksSegmentModel?.DocumentId}"); response.EnsureSuccessStatusCode(); } return(response.StatusCode); } }
public async Task WhenUpdateExistingArticleReturnsAlreadyReported() { // Arrange const string url = "/segment"; var tasksSegmentModel = new JobProfileTasksSegmentModel() { DocumentId = dataSeeding.Article2Id, CanonicalName = "article2_modified", SocLevelTwo = dataSeeding.Article2SocCode, Data = new JobProfileTasksDataSegmentModel(), }; var client = factory.CreateClient(); client.DefaultRequestHeaders.Accept.Clear(); // Act var response = await client.PostAsync(url, tasksSegmentModel, new JsonMediaTypeFormatter()).ConfigureAwait(false); // Assert response.StatusCode.Should().Be(HttpStatusCode.AlreadyReported); }
public async Task <IActionResult> Put([FromBody] JobProfileTasksSegmentModel jobProfileTasksSegmentModel) { logService.LogInformation($"{PutActionName} has been called"); if (jobProfileTasksSegmentModel == null) { logService.LogInformation($"{PutActionName}. No document was passed"); return(BadRequest()); } if (!ModelState.IsValid) { logService.LogInformation($"{PutActionName}. Model state is invalid"); return(BadRequest(ModelState)); } var existingDocument = await jobProfileTasksSegmentService.GetByIdAsync(jobProfileTasksSegmentModel.DocumentId).ConfigureAwait(false); if (existingDocument == null) { logService.LogInformation($"{PutActionName}. Couldnt find document with Id {jobProfileTasksSegmentModel.DocumentId}"); return(new StatusCodeResult((int)HttpStatusCode.NotFound)); } if (jobProfileTasksSegmentModel.SequenceNumber <= existingDocument.SequenceNumber) { logService.LogInformation($"{PutActionName}. Nothing to update as SequenceNumber of passed document {jobProfileTasksSegmentModel.SequenceNumber} is lower than SequenceNumber of persisted document {existingDocument.SequenceNumber}. "); return(new StatusCodeResult((int)HttpStatusCode.AlreadyReported)); } jobProfileTasksSegmentModel.Etag = existingDocument.Etag; jobProfileTasksSegmentModel.SocLevelTwo = existingDocument.SocLevelTwo; var response = await jobProfileTasksSegmentService.UpsertAsync(jobProfileTasksSegmentModel).ConfigureAwait(false); logService.LogInformation($"{PutActionName} has updated content for: {jobProfileTasksSegmentModel.CanonicalName}"); return(new StatusCodeResult((int)response)); }
public async Task ReturnsAlreadyReportedWhenExistingSequenceNumberIsHigher() { // Arrange var existingModel = new JobProfileTasksSegmentModel { SequenceNumber = 999 }; var modelToUpsert = new JobProfileTasksSegmentModel { SequenceNumber = 124 }; var controller = BuildSegmentController(); A.CallTo(() => FakeJobProfileSegmentService.GetByIdAsync(A <Guid> .Ignored)).Returns(existingModel); // Act var result = await controller.Put(modelToUpsert).ConfigureAwait(false); // Assert A.CallTo(() => FakeJobProfileSegmentService.UpsertAsync(A <JobProfileTasksSegmentModel> .Ignored)).MustNotHaveHappened(); var statusCodeResult = Assert.IsType <StatusCodeResult>(result); Assert.Equal((int)HttpStatusCode.AlreadyReported, statusCodeResult.StatusCode); controller.Dispose(); }