using AutoMapper; using DamageAssesment.Api.Attachments.Controllers; using DamageAssesment.Api.Attachments.Db; using DamageAssesment.Api.Attachments.Interfaces; using DamageAssesment.Api.Attachments.Models; using DamageAssesment.Api.Attachments.Profiles; using DamageAssesment.Api.Attachments.Providers; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Moq; using System.Collections.Generic; using Xunit; namespace DamageAssesment.Api.Attachments.Test { public class AttachmentServiceTest { [Fact(DisplayName = "Get Attachments - Ok case")] public async Task GetAttachmentsAsync_ShouldReturnStatusCode200() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getOkResponse(); mockAttachmentService.Setup(service => service.GetAttachmentsAsync()).ReturnsAsync(mockResponse); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); var result = (OkObjectResult)await AttachmentProvider.GetAttachmentsAsync(); Assert.Equal(200, result.StatusCode); } [Fact(DisplayName = "Get Attachments - NoContent Case")] public async Task GetAttachmentsAsync_ShouldReturnStatusCode204() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getNoContentResponse(); mockAttachmentService.Setup(service => service.GetAttachmentsAsync()).ReturnsAsync(mockResponse); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); var result = (NoContentResult)await AttachmentProvider.GetAttachmentsAsync(); Assert.Equal(204, result.StatusCode); } [Fact(DisplayName = "Get Attachment by Id - Ok case")] public async Task GetAttachmentAsync_ShouldReturnStatusCode200() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getOkResponse(1); mockAttachmentService.Setup(service => service.GetAttachmentByIdAsync(1)).ReturnsAsync(mockResponse); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); var result = (OkObjectResult)await AttachmentProvider.GetAttachmentbyIdAsync(1); Assert.Equal(200, result.StatusCode); } [Fact(DisplayName = "Get Attachment by Id - NotFound case")] public async Task GetAttachmentAsync_ShouldReturnStatusCode404() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getNotFoundResponse(); mockAttachmentService.Setup(service => service.GetAttachmentByIdAsync(99)).ReturnsAsync(mockResponse); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); var result = (NotFoundResult)await AttachmentProvider.GetAttachmentbyIdAsync(99); Assert.Equal(404, result.StatusCode); } [Fact(DisplayName = "Post Attachment - Ok case")] public async Task PostAttachmentAsync_ShouldReturnStatusCode200() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getOkResponse(); var AttachmentResponse = await MockData.GetAttachmentInfo(0); var mockInputAttachment = await MockData.getInputAttachmentData(); mockAttachmentService.Setup(service => service.PostAttachmentAsync(mockInputAttachment)).ReturnsAsync(mockResponse); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); var result = (NoContentResult)await AttachmentProvider.UploadAttachmentAsync(AttachmentResponse); Assert.Equal(204, result.StatusCode); } [Fact(DisplayName = "Post Attachment - BadRequest case")] public async Task PostAttachmentAsync_ShouldReturnStatusCode400() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockInputAttachment = await MockData.getInputAttachmentData(); var mockResponse = await MockData.getBadRequestResponse(); mockAttachmentService.Setup(service => service.PostAttachmentAsync(mockInputAttachment)).ReturnsAsync(mockResponse); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); AttachmentInfo attachmentInfo = new AttachmentInfo(); var result = (BadRequestObjectResult)await AttachmentProvider.UploadAttachmentAsync(attachmentInfo); Assert.Equal(400, result.StatusCode); } [Fact(DisplayName = "Put Attachment - Ok case")] public async Task PutAttachmentAsync_ShouldReturnStatusCode200() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getOkResponse(); var AttachmentResponse = await MockData.GetAttachmentInfo(1); var mockInputAttachment = await MockData.getInputAttachmentData(); mockAttachmentService.Setup(service => service.PostAttachmentAsync(mockInputAttachment)).ReturnsAsync(mockResponse); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); var result = (NoContentResult)await AttachmentProvider.UpdateAttachmentAsync(AttachmentResponse); Assert.Equal(204, result.StatusCode); } [Fact(DisplayName = "Put Attachment - BadRequest case")] public async Task PutAttachmentAsync_ShouldReturnStatusCode400() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockInputAttachment = await MockData.getInputAttachmentData(); var mockResponse = await MockData.getBadRequestResponse(); mockAttachmentService.Setup(service => service.PostAttachmentAsync(mockInputAttachment)).ReturnsAsync(mockResponse); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); AttachmentInfo attachmentInfo = new AttachmentInfo(); var result = (BadRequestObjectResult)await AttachmentProvider.UpdateAttachmentAsync(attachmentInfo); Assert.Equal(400, result.StatusCode); } [Fact(DisplayName = "Delete Attachment - Ok case")] public async Task DeleteAttachmentAsync_ShouldReturnStatusCode200() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getOkResponse(1); mockAttachmentService.Setup(service => service.DeleteAttachmentAsync(1)).ReturnsAsync(mockResponse); mockUploadService.Setup(service => service.Deletefile("")); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); var result = (OkObjectResult)await AttachmentProvider.DeleteAttachment(1); Assert.Equal(200, result.StatusCode); } [Fact(DisplayName = "Delete Attachment - NotFound case")] public async Task DeleteAttachmentAsync_ShouldReturnStatusCode404() { var mockAttachmentService = new Mock(); var mockUploadService = new Mock(); var mockResponse = await MockData.getNotFoundResponse(); mockAttachmentService.Setup(service => service.DeleteAttachmentAsync(1)).ReturnsAsync(mockResponse); var AttachmentProvider = new AttachmentsController(mockAttachmentService.Object, mockUploadService.Object); var result = (NotFoundResult)await AttachmentProvider.DeleteAttachment(1); Assert.Equal(404, result.StatusCode); } } }