public void Null_Project_Templates_Configuration_Should_Log_An_Error() { // Arrange var config = new InitializrConfig { ProjectMetadata = new ProjectMetadata(), }; config.ProjectMetadata = new ProjectMetadata(); var cfgServerConfig = new Mock <IOptions <InitializrConfig> >(); cfgServerConfig.Setup(o => o.Value).Returns(config); var settings = new ConfigServerClientSettingsOptions(); var cfgServerSettings = new Mock <IOptions <ConfigServerClientSettingsOptions> >(); cfgServerSettings.Setup(o => o.Value).Returns(settings); var logger = new Mock <ILogger <InitializrConfigService> >(); var service = new InitializrConfigService(cfgServerConfig.Object, cfgServerSettings.Object, logger.Object); // Act service.Initialize(); // Assert logger.VerifyLog(LogLevel.Error, "Project templates configuration missing."); }
public void Initialize_Should_Reset_State() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri("pt:///izr"), }, }, }; var registry = new TemplateRegistryBuilder().WithConfig(config).Build(); // Act var template = registry.Lookup(new ProjectSpec()); // Assert template.Should().NotBeNull(); // Arrange config.ProjectTemplates = new ProjectTemplateConfiguration[0]; // Act registry.Initialize(); template = registry.Lookup(new ProjectSpec()); // Assert template.Should().BeNull(); }
public void Files_In_Project_Template_Should_Match_Manifest() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri("pt:///izr"), }, }, }; var registry = new TemplateRegistryBuilder().WithConfig(config).Build(); // Act var template = registry.Lookup(new ProjectSpec()); // Assert Assert.NotNull(template); var fileEntries = template.Manifest.ToList(); fileEntries.Count().Should().Be(3); fileEntries[0].Path.Should().Be("f1"); fileEntries[0].Text.Should().Be("my file f1"); fileEntries[0].Rename.Should().BeNull(); fileEntries[1].Path.Should().Be("d1/"); fileEntries[1].Text.Should().BeNull(); fileEntries[1].Rename.Should().BeNull(); fileEntries[2].Path.Should().Be("r1"); fileEntries[2].Text.Should().Be("my file r1->n1"); fileEntries[2].Rename.Should().Be("n1"); }
public void Invalid_Version_Should_Log_An_Error() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri("pt:///izr?steeltoeVersionRange=1.0"), }, }, }; var logger = new Mock <ILogger <ProjectTemplateRegistry> >(); var registry = new TemplateRegistryBuilder().WithConfig(config).WithLogger(logger.Object).Build(); var spec = new ProjectSpec { SteeltoeVersion = "1..1" }; // Act var template = registry.Lookup(spec); // Assert template.Should().BeNull(); logger.VerifyLog(LogLevel.Error, "Error looking up project template: Version not in correct format: '1..1'"); }
/* ----------------------------------------------------------------- * * constructors * * ----------------------------------------------------------------- */ /// <summary> /// Initializes a new instance of the <see cref="InitializrConfigService"/> class. /// </summary> /// <param name="configuration">Injected configuration from Config Server.</param> /// <param name="settings">Injected settings from Config Server.</param> /// <param name="logger">Injected logger.</param> public InitializrConfigService( IOptions <InitializrConfig> configuration, IOptions <ConfigServerClientSettingsOptions> settings, ILogger <InitializrConfigService> logger) : base(logger) { _config = configuration.Value; Logger.LogInformation($"Config Server: uri={settings.Value.Uri},env={settings.Value.Env},label={settings.Value.Label}"); }
public void Configuration_Should_Specify_Defaults() { // Arrange var config = new InitializrConfig { ProjectMetadata = new ProjectMetadata { Name = new ProjectMetadata.Text { Default = "my project name" }, Description = new ProjectMetadata.Text { Default = "my description" }, Namespace = new ProjectMetadata.Text { Default = "my namespace" }, SteeltoeVersion = new ProjectMetadata.SingleSelectList { Default = "my steeltoe version" }, DotNetFramework = new ProjectMetadata.SingleSelectList { Default = "my dotnet framework" }, DotNetTemplate = new ProjectMetadata.SingleSelectList { Default = "my dotnet template" }, Language = new ProjectMetadata.SingleSelectList { Default = "my language" }, Packaging = new ProjectMetadata.SingleSelectList { Default = "myarchive" }, }, }; var controller = new ProjectControllerBuilder() .WithInitializrConfiguration(config) .Build(); // Act var unknown = controller.GetProjectArchive(new ProjectSpec()); // Assert var result = Assert.IsType <FileContentResult>(unknown); using var reader = new StreamReader(new MemoryStream(result.FileContents)); reader.ReadLine().Should().Be("project name=my project name"); reader.ReadLine().Should().Be("description=my description"); reader.ReadLine().Should().Be("namespace=my namespace"); reader.ReadLine().Should().Be("steeltoe version=my steeltoe version"); reader.ReadLine().Should().Be("dotnet framework=my dotnet framework"); reader.ReadLine().Should().Be("dotnet template=my dotnet template"); reader.ReadLine().Should().Be("language=my language"); reader.ReadLine().Should().Be("packaging=myarchive"); reader.ReadLine().Should().Be("dependencies=<na>"); reader.ReadLine().Should().BeNull(); }
public void Properties_Should_Be_Defined() { // Arrange var config = new InitializrConfig(); // Act // Assert config.ProjectMetadata.Should().BeNull(); config.ProjectTemplates.Should().BeNull(); }
public void Project_Templates_Not_Configured_Should_Log_An_Error() { // Arrange var config = new InitializrConfig(); var logger = new Mock <ILogger <ProjectTemplateRegistry> >(); // Act new TemplateRegistryBuilder().WithConfig(config).WithLogger(logger.Object).Build(); // Assert logger.VerifyLog(LogLevel.Error, "Templates not configured."); }
public void GetInitializrApiConfiguration_Should_Return_InitializrApiConfiguration() { // Arrange var config = new InitializrConfig(); var configService = new Mock <IInitializrConfigService>(); configService.Setup(repo => repo.GetInitializrConfig()).Returns(config); var controller = new ConfigController(configService.Object, new NullLogger <ConfigController>()); // Act var result = controller.GetInitializrConfiguration(); // Assert var indexResult = Assert.IsType <OkObjectResult>(result); indexResult.Value.Should().BeSameAs(config); }
public void Dependencies_Should_Be_Case_Corrected() { // Arrange var config = new InitializrConfig { ProjectMetadata = new ProjectMetadata { Dependencies = new ProjectMetadata.GroupList { Values = new[] { new ProjectMetadata.Group { Values = new[] { new ProjectMetadata.GroupItem { Id = "CamelCaseDep", }, }, }, }, }, }, }; var spec = new ProjectSpec { Packaging = "myarchive", Dependencies = "camelcasedep", }; var controller = new ProjectControllerBuilder() .WithInitializrConfiguration(config) .Build(); // Act var unknown = controller.GetProjectArchive(spec); // Assert var result = Assert.IsType <FileContentResult>(unknown); using var reader = new StreamReader(new MemoryStream(result.FileContents)); var body = reader.ReadToEnd(); body.Should().Contain("dependencies=CamelCaseDep"); }
internal ProjectController Build() { if (_config is null) { _config = new InitializrConfig { ProjectMetadata = new ProjectMetadata { Packaging = new ProjectMetadata.SingleSelectList { Default = "myarchive", }, }, ProjectTemplates = new ProjectTemplateConfiguration[0], }; } if (_generator is null) { _generator = new TestProjectGenerator(); } if (_registry is null) { var mock = new Mock <IArchiverRegistry>(); mock.Setup(reg => reg.Lookup(It.Is <string>(s => s.Equals("myarchive")))) .Returns(new TestArchiver()); _registry = mock.Object; } var configurationService = new Mock <IInitializrConfigService>(); configurationService.Setup(svc => svc.GetInitializrConfig()).Returns(_config); var logger = new NullLogger <ProjectController>(); var projectController = new ProjectController(configurationService.Object, _generator, _registry, logger) { ControllerContext = { HttpContext = new DefaultHttpContext() } }; return(projectController); }
public void Null_Archive_Format_Should_Return_500_Internal_Server_Error() { // Arrange var spec = new ProjectSpec(); var config = new InitializrConfig { ProjectTemplates = new ProjectTemplateConfiguration[0] }; var controller = new ProjectControllerBuilder().WithInitializrConfiguration(config).Build(); // Act var unknown = controller.GetProjectArchive(spec); // Assert var result = Assert.IsType <ObjectResult>(unknown); result.StatusCode.Should().Be(StatusCodes.Status500InternalServerError); result.Value.Should().Be("Default packaging not configured."); }
/* ----------------------------------------------------------------- * * methods * * ----------------------------------------------------------------- */ /// <inheritdoc /> public void Initialize() { Logger.LogInformation("loading configuration: {Path}", _options.ConfigurationPath); try { var configJson = File.ReadAllText(_options.Configuration["Path"]); _config = Serializer.DeserializeJson <InitializrConfig>(configJson); } catch (FileNotFoundException) { throw new ArgumentException($"Configuration file path does not exist: {_options.ConfigurationPath}"); } catch (UnauthorizedAccessException) { throw new ArgumentException( $"Configuration file path is not a file or cannot be read: {_options.ConfigurationPath}"); } }
public void Lookup_Should_Find_Match() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri( "pt:///izr?description=pt1&steeltoeVersionRange=st2.0&dotNetFrameworkRange=df1.0&dotNetTemplate=dt1&language=l1"), }, new ProjectTemplateConfiguration { Uri = new Uri( "pt:///izr?description=pt2&steeltoeVersionRange=st1.0&dotNetFrameworkRange=df1.0&dotNetTemplate=dt1&language=l1"), }, }, }; var registry = new TemplateRegistryBuilder().WithConfig(config).Build(); // Act var template = registry.Lookup(new ProjectSpec { SteeltoeVersion = "st2.0", DotNetFramework = "df1.0", DotNetTemplate = "dt1", Language = "l1" }); // Assert Assert.NotNull(template); template.Description.Should().Be("pt1"); // Act template = registry.Lookup(new ProjectSpec { SteeltoeVersion = "st1.0", DotNetFramework = "df1.0", DotNetTemplate = "dt1", Language = "l1" }); // Assert Assert.NotNull(template); template.Description.Should().Be("pt2"); }
public void Uri_Unexpected_Error_Should_Log_An_Error() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri("pt:///error"), } }, }; var logger = new Mock <ILogger <ProjectTemplateRegistry> >(); // Act new TemplateRegistryBuilder().WithConfig(config).WithLogger(logger.Object).Build(); // Assert logger.VerifyLog(LogLevel.Error, "Unexpected error [System.Exception[some error message]]: pt:///error"); }
public void Uri_Null_Stream_Should_Log_An_Error() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri("pt:///stream/null"), } }, }; var logger = new Mock <ILogger <ProjectTemplateRegistry> >(); // Act new TemplateRegistryBuilder().WithConfig(config).WithLogger(logger.Object).Build(); // Assert logger.VerifyLog(LogLevel.Error, "URI returned null stream: pt:///stream/null"); }
public void Missing_File_Should_Log_An_Error() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri("pt:///izr?missingfile=yes"), } }, }; var logger = new Mock <ILogger <ProjectTemplateRegistry> >(); // Act new TemplateRegistryBuilder().WithConfig(config).WithLogger(logger.Object).Build(); // Assert logger.VerifyLog(LogLevel.Error, "Project template missing file: 'f1' pt:///izr?missingfile=yes"); }
public void Uri_Directory_Not_Found_Should_Log_An_Error() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri("pt:///no/such/dir/"), } }, }; var logger = new Mock <ILogger <ProjectTemplateRegistry> >(); // Act new TemplateRegistryBuilder().WithConfig(config).WithLogger(logger.Object).Build(); // Assert logger.VerifyLog(LogLevel.Error, "URI not found: pt:///no/such/dir/"); }
public void Malformed_Metadata_Should_Log_An_Error() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri("pt:///izr?metadata=malformed"), } }, }; var logger = new Mock <ILogger <ProjectTemplateRegistry> >(); // Act new TemplateRegistryBuilder().WithConfig(config).WithLogger(logger.Object).Build(); // Assert logger.VerifyLog(LogLevel.Error, "Project template metadata malformed ('.IZR/metadata.yaml'): pt:///izr?metadata=malformed"); }
public void Uri_Non_Zip_Stream_Should_Log_An_Error() { // Arrange var config = new InitializrConfig { ProjectTemplates = new[] { new ProjectTemplateConfiguration { Uri = new Uri("pt:///stream/empty"), } }, }; var configService = new Mock <IInitializrConfigService>(); configService.Setup(svc => svc.GetInitializrConfig()).Returns(config); var logger = new Mock <ILogger <ProjectTemplateRegistry> >(); // Act new TemplateRegistryBuilder().WithConfig(config).WithLogger(logger.Object).Build(); // Assert logger.VerifyLog(LogLevel.Error, "URI not a Zip archive: pt:///stream/empty"); }
public void Get_Should_Return_Help() { // Arrange var initializrCfg = new Mock <IOptions <InitializrOptions> >(); var initializrOpts = new InitializrOptions { Logo = "/no/such/logo" }; initializrCfg.Setup(cfg => cfg.Value).Returns(initializrOpts); var config = new InitializrConfig { ProjectMetadata = new ProjectMetadata { Name = new ProjectMetadata.Text { Default = "MyProject" }, ApplicationName = new ProjectMetadata.Text { Default = "MyApplication" }, Namespace = new ProjectMetadata.Text { Default = "MyNamespace" }, Description = new ProjectMetadata.Text { Default = "my description" }, SteeltoeVersion = new ProjectMetadata.SingleSelectList { Default = "1.2.3" }, DotNetFramework = new ProjectMetadata.SingleSelectList { Default = "1.2" }, DotNetTemplate = new ProjectMetadata.SingleSelectList { Default = "mytemplate" }, Language = new ProjectMetadata.SingleSelectList { Default = "mylang" }, Packaging = new ProjectMetadata.SingleSelectList { Default = "mypkg" }, Dependencies = new ProjectMetadata.GroupList { Values = new[] { new ProjectMetadata.Group() { Name = "AGroup", Values = new[] { new ProjectMetadata.GroupItem() { Id = "dep1", Description = "DependencyOne" }, new ProjectMetadata.GroupItem() { Id = "dep2", Description = "DependencyTwo" } } }, new ProjectMetadata.Group() { Name = "AnotherGroup", Values = new[] { new ProjectMetadata.GroupItem { Id = "anotherdep", Description = "AnotherDependency", SteeltoeVersionRange = "[1.0,1.9)", } } } } } } }; var configService = new Mock <IInitializrConfigService>(); configService.Setup(repo => repo.GetInitializrConfig()).Returns(config); var controller = new RootController(initializrCfg.Object, configService.Object, new NullLogger <RootController>()); // Act var result = controller.GetHelp(); // Assert var indexResult = Assert.IsType <OkObjectResult>(result); indexResult.Value.Should().BeOfType <string>(); var help = Assert.IsType <string>(indexResult.Value); help.Should().MatchRegex("!!! failed to load logo:"); help.Should().MatchRegex(":: Steeltoe Initializr :: https://start.steeltoe.io"); help.Should().MatchRegex("Examples:"); help.Should().MatchRegex(@"\|\s+Parameter\s+\|\s+Description\s+\|\s+Default value\s+\|"); help.Should().MatchRegex(@"\|\s+name\s+\|\s+project name\s+\|\s+MyProject\s+\|"); help.Should().MatchRegex(@"\|\s+applicationName\s+\|\s+application name\s+\|\s+MyApplication\s+\|"); help.Should().MatchRegex(@"\|\s+namespace\s+\|\s+namespace\s+\|\s+MyNamespace\s+\|"); help.Should().MatchRegex(@"\|\s+description\s+\|\s+project description\s+\|\s+my description\s+\|"); help.Should().MatchRegex(@"\|\s+steeltoeVersion\s+\|\s+Steeltoe version\s+\|\s+1.2.3\s+\|"); help.Should().MatchRegex(@"\|\s+dotNetFramework\s+\|\s+target .NET framework\s+\|\s+1.2\s+\|"); help.Should().MatchRegex(@"\|\s+dotNetTemplate\s+\|\s+.NET template\s+\|\s+mytemplate\s+\|"); help.Should().MatchRegex(@"\|\s+language\s+\|\s+programming language\s+\|\s+mylang\s+\|"); help.Should().MatchRegex(@"\|\s+packaging\s+\|\s+project packaging\s+\|\s+mypkg\s+\|"); help.Should().MatchRegex(@"\|\s+Id\s+\|\s+Description\s+\|\s+Required Steeltoe version\s+\|"); help.Should().MatchRegex(@"\|\s+dep1\s+\|\s+DependencyOne\s+\|\s+\|"); help.Should().MatchRegex(@"\|\s+dep2\s+\|\s+DependencyTwo\s+\|\s+\|"); help.Should().MatchRegex(@"\|\s+anotherdep\s+\|\s+AnotherDependency\s+\|\s+>=1.0 and <1.9\s+\|"); help.Should().MatchRegex(@"\|\s+anotherdep\s+\|\s+AnotherDependency\s+\|\s+>=1.0 and <1.9\s+\|"); }
internal ProjectControllerBuilder WithInitializrConfiguration(InitializrConfig config) { _config = config; return(this); }
internal TemplateRegistryBuilder WithConfig(InitializrConfig config) { _config = config; return(this); }