コード例 #1
0
        public void AssemblyClassTest()
        {
            // Arrange
            var classes = new[]
            {
                new Class
                {
                    AssemblyName = "Foo",
                    Name         = "Bar",
                    FullName     = "Foo.Bar"
                }
            };

            var assemblyStorageMock = new Mock <IAssemblyStorage>();

            assemblyStorageMock.Setup(x => x.GetClassesInAssembly(It.IsAny <string>())).Returns(classes);

            var loggerMock         = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();
            var assemblyController = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                               loggerMock.Object);

            // Act
            var results = assemblyController.AssemblyClass("Foo") as ViewResult;

            // Assert
            Assert.NotNull(results);
            Assert.NotNull(results.Model);
            Assert.NotEmpty((IEnumerable <Class>)results.Model);
            Assert.Equal(1, ((IEnumerable <Class>)results.Model).Count());
        }
コード例 #2
0
        public void MethodParametersTest()
        {
            // Arrange
            var methodParameters = new[]
            {
                new MethodParameter
                {
                    Name         = "a",
                    Type         = "int",
                    AssemblyName = "Foo",
                    MethodName   = "Dummy",
                    ClassName    = "Bar"
                }
            };

            var assemblyStorageMock = new Mock <IAssemblyStorage>();

            assemblyStorageMock.Setup(x => x.GetMethodParametersInMethod(It.IsAny <Method>())).Returns(methodParameters);

            var loggerMock         = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();
            var assemblyController = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                               loggerMock.Object);

            // Act
            var results = assemblyController.MethodParameters(new Method()) as ViewResult;

            // Assert
            Assert.NotNull(results);
            Assert.NotNull(results.Model);
            Assert.NotEmpty((IEnumerable <MethodParameter>)results.Model);
            Assert.Equal(1, ((IEnumerable <MethodParameter>)results.Model).Count());
        }
コード例 #3
0
        public void ConstructorTest()
        {
            // Arrange
            var assemblyStorageMock = new Mock <IAssemblyStorage>();
            var loggerMock          = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();

            // Act
            var assemblyController = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                               loggerMock.Object);

            // Assert
            Assert.True(true);
        }
コード例 #4
0
 public void ConstructorNullAssemblyStorageTest()
 {
     try
     {
         // Arrange
         // Act
         var assemblyController = new WebApp.Controllers.AssemblyController(null, null);
     }
     catch (ArgumentNullException argumentNullException)
     {
         // Assert
         Assert.NotNull(argumentNullException);
         Assert.Equal("assemblyStorage", argumentNullException.ParamName);
     }
 }
コード例 #5
0
        public void UploadTest()
        {
            // Arrange
            var assemblyStorageMock = new Mock <IAssemblyStorage>();
            var loggerMock          = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();
            var assemblyController  = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                                loggerMock.Object);

            // Act
            var result = assemblyController.Upload() as ViewResult;

            // Assert
            Assert.NotNull(result);
            Assert.Null(result.Model);
        }
コード例 #6
0
        public void UploadAssemblyDataSourceAttributeNotFoundExceptionTest()
        {
            // Arrange
            var assemblyStorageMock = new Mock <IAssemblyStorage>();

            assemblyStorageMock.Setup(x => x.SaveAssembly(It.IsAny <Assembly>()))
            .Throws <DataSourceAttributeNotFoundException>();

            var loggerMock         = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();
            var assemblyController = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                               loggerMock.Object);

            var formFile = new Mock <IFormFile>();

            formFile.Setup(x => x.FileName).Returns("Test");

            var formFileCollection = new Mock <IFormFileCollection>();

            formFileCollection.Setup(x => x.GetFile("assemblyFile")).Returns(formFile.Object);
            formFileCollection.Setup(x => x.Count).Returns(1);

            var formCollection = new Mock <IFormCollection>();

            formCollection.Setup(x => x.Files).Returns(formFileCollection.Object);

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.Setup(x => x.Form).Returns(formCollection.Object);

            var httpContext = new Mock <HttpContext>();

            httpContext.Setup(x => x.Request).Returns(httpRequest.Object);

            var controllerContext = new Mock <ControllerContext>();

            controllerContext.Object.HttpContext = httpContext.Object;
            assemblyController.ControllerContext = controllerContext.Object;

            // Act
            var result = assemblyController.UploadAssemblyAsync().Result as JsonResult;

            // Assert
            Assert.NotNull(result);
            Assert.NotNull((AssemblyUpload)result.Value);
            Assert.Equal(false, ((AssemblyUpload)result.Value).Success);
            Assert.Equal("The uploaded file is not valid.", ((AssemblyUpload)result.Value).ErrorHeading);
            Assert.Equal("Please upload a valid assembly file.", ((AssemblyUpload)result.Value).ErrorMessage);
        }
コード例 #7
0
        public void ConstructorNullLoggerTest()
        {
            try
            {
                // Arrange
                var assemblyStorageMock = new Mock <IAssemblyStorage>();

                // Act
                var assemblyController = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object, null);
            }
            catch (ArgumentNullException argumentNullException)
            {
                // Assert
                Assert.NotNull(argumentNullException);
                Assert.Equal("logger", argumentNullException.ParamName);
            }
        }
コード例 #8
0
        public async Task UploadAssemblyTestAsync()
        {
            // Arrange
            var assemblyStorageMock = new Mock <IAssemblyStorage>();
            var loggerMock          = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();
            var assemblyController  = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                                loggerMock.Object);

            var formFile = new Mock <IFormFile>();

            formFile.Setup(x => x.FileName).Returns("Test");

            var formFileCollection = new Mock <IFormFileCollection>();

            formFileCollection.Setup(x => x.GetFile("assemblyFile")).Returns(formFile.Object);
            formFileCollection.Setup(x => x.Count).Returns(1);

            var formCollection = new Mock <IFormCollection>();

            formCollection.Setup(x => x.Files).Returns(formFileCollection.Object);

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.Setup(x => x.Form).Returns(formCollection.Object);

            var httpContext = new Mock <HttpContext>();

            httpContext.Setup(x => x.Request).Returns(httpRequest.Object);

            var controllerContext = new Mock <ControllerContext>();

            controllerContext.Object.HttpContext = httpContext.Object;
            assemblyController.ControllerContext = controllerContext.Object;

            // Act
            var result = await assemblyController.UploadAssemblyAsync() as JsonResult;

            // Assert
            Assert.NotNull(result);
            Assert.NotNull((AssemblyUpload)result.Value);
            Assert.Equal(true, ((AssemblyUpload)result.Value).Success);
            Assert.Equal("/Assembly", ((AssemblyUpload)result.Value).RedirectTo);
        }
コード例 #9
0
        public void IndexTest()
        {
            // Arrange
            var assemblyStorageMock = new Mock <IAssemblyStorage>();

            assemblyStorageMock.Setup(x => x.GetAssemblies()).Returns(new[]
            {
                new Assembly
                {
                    Path    = @"C:\Temp\Dummy.dll",
                    Name    = "Dummy.dll",
                    Version = "1.0.0.0"
                },
            });

            var loggerMock = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();

            var assemblyController = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                               loggerMock.Object);

            // Act
            var result = assemblyController.Index() as ViewResult;

            // Assert
            Assert.NotNull(result);
            Assert.NotNull(result.Model);
            Assert.IsType(typeof(Assembly[]), result.Model);
            Assert.NotEmpty((IEnumerable <Assembly>)result.Model);
            Assert.Equal(1, ((IEnumerable <Assembly>)result.Model).Count());

            var assembly = ((IEnumerable <Assembly>)result.Model).Single();

            Assert.Equal(@"C:\Temp\Dummy.dll", assembly.Path);
            Assert.Equal("Dummy.dll", assembly.Name);
            Assert.Equal("1.0.0.0", assembly.Version);

            Assert.Null(result.ContentType);
            Assert.Null(result.TempData);
            Assert.Null(result.ViewEngine);
            Assert.Null(result.ViewName);
            Assert.NotNull(result.ViewData);
            Assert.Empty(result.ViewData);
        }
コード例 #10
0
        public void IndexExceptionTest()
        {
            Mock <ILogger <WebApp.Controllers.AssemblyController> > loggerMock = null;

            try
            {
                // Arrange
                var assemblyStorageMock = new Mock <IAssemblyStorage>();
                assemblyStorageMock.Setup(x => x.GetAssemblies()).Throws(new Exception("Error test!"));

                loggerMock = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();
                loggerMock.Setup(x => x.Log(
                                     It.Is <LogLevel>(y => y == LogLevel.Error),
                                     It.Is <Microsoft.Extensions.Logging.EventId>(y => Equals(y, EventId.GenericException)),
                                     It.IsAny <FormattedLogValues>(),
                                     It.IsAny <Exception>(),
                                     It.IsAny <Func <object, Exception, string> >()));

                var assemblyController = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                                   loggerMock.Object);

                // Act
                assemblyController.Index();
            }
            catch (Exception exception)
            {
                // Assert
                Assert.NotNull(exception);
                Assert.IsType(typeof(Exception), exception);
                Assert.Equal("Error test!", exception.Message);
                Assert.NotNull(loggerMock);
                loggerMock.Verify(x => x.Log(
                                      It.Is <LogLevel>(y => y == LogLevel.Error),
                                      It.Is <Microsoft.Extensions.Logging.EventId>(y => Equals(y, EventId.GenericException)),
                                      It.IsAny <FormattedLogValues>(),
                                      It.IsAny <Exception>(),
                                      It.IsAny <Func <object, Exception, string> >()));
            }
        }
コード例 #11
0
        public void MethodParametersExceptionTest()
        {
            // Arrange
            var assemblyStorageMock = new Mock <IAssemblyStorage>();

            assemblyStorageMock.Setup(x => x.GetMethodParametersInMethod(It.IsAny <Method>())).Throws <Exception>();

            var loggerMock         = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();
            var assemblyController = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                               loggerMock.Object);

            try
            {
                // Act
                assemblyController.MethodParameters(new Method());
            }
            catch (Exception)
            {
                // Assert
                Assert.True(true);
            }
        }
コード例 #12
0
        public void AssemblyClassExceptionTest()
        {
            // Arrange
            var assemblyStorageMock = new Mock <IAssemblyStorage>();

            assemblyStorageMock.Setup(x => x.GetClassesInAssembly(It.IsAny <string>())).Throws <Exception>();

            var loggerMock         = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();
            var assemblyController = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                               loggerMock.Object);

            try
            {
                // Act
                assemblyController.AssemblyClass(string.Empty);
            }
            catch (Exception)
            {
                // Assert
                Assert.True(true);
            }
        }
コード例 #13
0
        public void UploadAssemblyNoFileTest()
        {
            // Arrange
            var assemblyStorageMock = new Mock <IAssemblyStorage>();
            var loggerMock          = new Mock <ILogger <WebApp.Controllers.AssemblyController> >();
            var assemblyController  = new WebApp.Controllers.AssemblyController(assemblyStorageMock.Object,
                                                                                loggerMock.Object);

            var formFileCollection = new Mock <IFormFileCollection>();

            formFileCollection.Setup(x => x.Count).Returns(0);

            var formCollection = new Mock <IFormCollection>();

            formCollection.Setup(x => x.Files).Returns(formFileCollection.Object);

            var httpRequest = new Mock <HttpRequest>();

            httpRequest.Setup(x => x.Form).Returns(formCollection.Object);

            var httpContext = new Mock <HttpContext>();

            httpContext.Setup(x => x.Request).Returns(httpRequest.Object);

            var controllerContext = new Mock <ControllerContext>();

            controllerContext.Object.HttpContext = httpContext.Object;
            assemblyController.ControllerContext = controllerContext.Object;

            // Act
            var result = assemblyController.UploadAssemblyAsync().Result as JsonResult;

            // Assert
            Assert.NotNull(result);
            Assert.NotNull((AssemblyUpload)result.Value);
            Assert.Equal(false, ((AssemblyUpload)result.Value).Success);
            Assert.Equal("No assembly uploaded.", ((AssemblyUpload)result.Value).ErrorHeading);
            Assert.Equal("Please upload a valid assembly file.", ((AssemblyUpload)result.Value).ErrorMessage);
        }