public void SingletonRegistration_Fails_Without_Singleton_Flag() { var readers = new List <ReaderConfiguration>(); readers.Add(CreateReaderWithSingletonDependencyConfig("1", "2")); ComponentRegistration.Reset(); // the collector needs to be registered since it is injected ComponentRegistration.RegisterComponent <ICollector>(ConfigurationTests.TYPE_COLLECTOR); ComponentRegistration.RegisterComponents(readers); ComponentRegistration.RegisterTypesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = COLLECTOR_SDK_ASSEMBLY, Type = "LoggerFacade" }); ComponentRegistration.RegisterModulesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = NLOG_EXTRAS_ASSEMBLY, RegisterAll = true }); ComponentRegistration.Build(); IReader reader = null; Assert.ThrowsException <DependencyResolutionException>(() => reader = ComponentRegistration.CreateInstance <IReader>(ConfigurationTests.TYPE_READER_WITH_SINGLETON_DEPENDENCY)); reader.Should().BeNull(); }
public void ComponentRegistration_Reset_Success() { ComponentRegistration.Reset(); var collector = ComponentRegistration.CreateInstance <ICollector>(ConfigurationTests.TYPE_COLLECTOR); collector.Should().NotBeNull(); }
public void TransformerRegistration_Success() { var transformers = new List <TransformerConfiguration>(); transformers.Add(CreateTransformerConfig("1", CreateMapperConfig("2", "1"), "3")); ComponentRegistration.Reset(); // the collector needs to be resgistered since it is injected ComponentRegistration.RegisterComponent <ICollector>(ConfigurationTests.TYPE_COLLECTOR); ComponentRegistration.RegisterComponents(transformers); ComponentRegistration.RegisterTypesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = COLLECTOR_SDK_ASSEMBLY, Type = "LoggerFacade" }); ComponentRegistration.RegisterModulesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = NLOG_EXTRAS_ASSEMBLY, RegisterAll = true }); ComponentRegistration.Build(); var reader = ComponentRegistration.CreateInstance <ITransformer>(ConfigurationTests.TYPE_TRANSFORMER); reader.Should().NotBeNull(); }
/// <summary> /// Create a copy of a transformer. /// </summary> /// <param name="id">New id of the transformer, must be unique</param> /// <param name="transformer">The transformer to copy.</param> /// <returns>A new transformer</returns> public static ITransformer CloneTransformer(string id, ITransformer transformer) { try { var newConfig = new TransformerConfiguration() { Id = id, ReaderId = transformer.Config.ReaderId, Mappers = transformer.Config.Mappers, Type = transformer.Config.Type, }; var mappers = new List <IMapper>(); foreach (var mapperConfig in newConfig.Mappers) { var mapper = CollectorFactory.CreateMapper(mapperConfig); if (mapper == null) { throw new NullReferenceException("Unable to create Collector. Invalid Mapper defined."); } mappers.Add(mapper); } var newTransformer = ComponentRegistration.CreateInstance <ITransformer>(newConfig.Type); newTransformer.Configure(newConfig, transformer.Handler); return(transformer); } catch (Exception e) { _logger.Error(e); } return(null); }
public void SingletonRegistration_Build_Success() { var readers = new List <ReaderConfiguration>(); readers.Add(CreateReaderWithSingletonDependencyConfig("1", "2")); ComponentRegistration.Reset(); // the collector needs to be registered since it is injected ComponentRegistration.RegisterComponent <ICollector>(ConfigurationTests.TYPE_COLLECTOR); ComponentRegistration.RegisterComponents(readers); ComponentRegistration.RegisterTypesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = COLLECTOR_SDK_ASSEMBLY, Type = "LoggerFacade" }); ComponentRegistration.RegisterTypesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = typeof(MockDisposableSingleton).Assembly.GetName().Name, Type = nameof(MockDisposableSingleton), Singleton = true }); ComponentRegistration.RegisterModulesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = NLOG_EXTRAS_ASSEMBLY, RegisterAll = true }); ComponentRegistration.Build(); var reader = ComponentRegistration.CreateInstance <IReader>(ConfigurationTests.TYPE_READER_WITH_SINGLETON_DEPENDENCY); reader.Should().NotBeNull(); }
public void ReaderRegistration_Success() { var readers = new List <ReaderConfiguration>(); readers.Add(CreateReaderConfig("1", "2")); ComponentRegistration.Reset(); // the collector needs to be resgistered since it is injected ComponentRegistration.RegisterComponent <ICollector>(ConfigurationTests.TYPE_COLLECTOR); ComponentRegistration.RegisterComponents(readers); ComponentRegistration.RegisterTypesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = "Collector.SDK", Type = "LoggerFacade" }); ComponentRegistration.RegisterModulesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = "Autofac.Extras.NLog", RegisterAll = true }); ComponentRegistration.Build(); var reader = ComponentRegistration.CreateInstance <IReader>(ConfigurationTests.TYPE_READER); reader.Should().NotBeNull(); }
public void SampleTransformer_Transform_Success() { var readerId = "2"; var transformer = ComponentRegistration.CreateInstance <ITransformer>(TYPE_SAMPLE_TRANSFORMER); var config = ConfigurationTests.CreateTransformerConfig("4", ConfigurationTests.CreateMapperConfig("3", "4"), readerId); var handler = new MockTransformationHandler(); transformer.Configure(config, handler); var dataPoint = new KeyValuePair <string, object>("ABC", "Some Value"); var dataRow = new EntityCollection(); dataRow.Entities.Add(dataPoint.Key, dataPoint.Value); var data = new List <IEntityCollection>(); data.Add(dataRow); transformer.HandleData(readerId, data, new Dictionary <string, string>()).Wait(); handler.Invoked.Should().BeTrue(); handler.SenderId.Should().Equals(readerId); handler.Data.Count.Should().Equals(1); foreach (var row in handler.Data) { row.GetType().Should().Be(typeof(MockEntity)); } }
public void LogPublisher_Publish_Success() { var currentDirectory = Directory.GetCurrentDirectory(); var fileName = string.Format(CultureInfo.InvariantCulture, "{0}\\test.log", currentDirectory); var outFileName = string.Format(CultureInfo.InvariantCulture, "{0}\\test.log.txt", currentDirectory); File.Delete(outFileName); var publisher = ComponentRegistration.CreateInstance <IPublisher>("Collector.SDK.Samples.Publishers.LogPublisher, Collector.SDK.Samples.Publishers"); var config = ConfigurationTests.CreateEndPointConfig("1", "FileName", fileName); config.Properties.Add(CollectorConstants.KEY_FOLDER, currentDirectory); publisher.Configure("5", config); var entity = new MockEntity(); entity.XYZ = "123"; entity.ABC = "789"; var data = new List <object>(); data.Add(entity); var context = new Dictionary <string, string>(); context.Add(CollectorConstants.KEY_FILENAME, fileName); publisher.PublishData("3", data, context).Wait(); File.Exists(outFileName).Should().BeTrue(); var text = File.ReadAllText(outFileName); text.Should().Contain("\"XYZ\":\"123\""); }
/// <summary> /// Create a Collector, this involved creating all the layers - Readers, Transformers and Publishers. /// </summary> /// <param name="collectorConfig">The configuration that defines all the layers.</param> /// <param name="mapperConfigs">The mappers to use by transformers.</param> /// <returns></returns> public static ICollector CreateCollector(CollectorConfiguration collectorConfig) { ComponentRegistration.RegisterComponents(collectorConfig); ComponentRegistration.Build(); var collector = ComponentRegistration.CreateInstance <ICollector>(collectorConfig.Type); collector.Configure(collectorConfig); return(collector); }
public void ComponentRegistration_Meaningful_Exception() { try { var collector = ComponentRegistration.CreateInstance <ICollector>("Something,Something"); collector.Should().BeNull(); } catch (Exception e) { e.Message.Should().Contain("The requested service 'Something (Collector.SDK.Collectors.ICollector)' has not been registered."); } }
public void LoggerFacade_LogDebug_Success() { ILogger logger = ComponentRegistration.CreateInstance <ILogger>(TYPE_LOGGER); logger.Should().NotBeNull(); logger.Debug("Test_2"); var content = File.ReadAllText(_path); content.Should().Contain("DEBUG"); content.Should().Contain("Test_2"); }
public void LoggerFacade_LogFatalException_Success() { ILogger logger = ComponentRegistration.CreateInstance <ILogger>(TYPE_LOGGER); logger.Should().NotBeNull(); logger.Fatal(new Exception("Test_12")); var content = File.ReadAllText(_path); content.Should().Contain("FATAL"); content.Should().Contain("Test_12"); }
public void LoggerFacade_LogErrorException_Success() { var logger = ComponentRegistration.CreateInstance <Logging.ILogger>(TYPE_LOGGER); logger.Should().NotBeNull(); logger.Error(new Exception("Test_11")); var content = File.ReadAllText(_path); content.Should().Contain("ERROR"); content.Should().Contain("Test_11"); }
public void LoggerFacade_LogWarning_Success() { var logger = ComponentRegistration.CreateInstance <Logging.ILogger>(TYPE_LOGGER); logger.Should().NotBeNull(); logger.Warn("Test_4"); var content = File.ReadAllText(_path); content.Should().Contain("WARN"); content.Should().Contain("Test_4"); }
/// <summary> /// Create a converter. /// </summary> /// <param name="type"></param> /// <returns></returns> public static IConverter CreateConverter(string type) { try { var converter = ComponentRegistration.CreateInstance <IConverter>(type); return(converter); } catch (Exception e) { _logger.Error(e); } return(null); }
/// <summary> /// Create a publisher. /// </summary> /// <param name="type">Fully qualified name of the publisher class</param> /// <param name="id">Id of the publisher</param> /// <param name="config">The config parameters for connecting to the data end point</param> /// <returns>A new publisher</returns> public static IPublisher CreatePublisher(PublisherConfiguration config, EndPointConfiguration endpointConfig) { try { var publisher = ComponentRegistration.CreateInstance <IPublisher>(config.Type); publisher.Configure(config.Id, endpointConfig); return(publisher); } catch (Exception e) { _logger.Error(e); } return(null); }
/// <summary> /// Create a connecter with a state change handler that will get invoked when the connecter's state changes. /// </summary> /// <param name="type">Fully qualified name of the connecter class and assembly, comma seperated.</param> /// <param name="id">The id of the connector.</param> /// <param name="config">The config parameters for connecting to the data source</param> /// <param name="handler">The handler to signal with blocks of data</param> /// <returns>A new connecter</returns> public static IReader CreateReader(string type, string id, EndPointConfiguration config, IDataHandler handler) { try { var connector = ComponentRegistration.CreateInstance <IReader>(type); connector.Configure(id, config, handler); return(connector); } catch (Exception e) { _logger.Error(e); } return(null); }
/// <summary> /// Create an Entity with the given fields /// </summary> /// <param name="dataType"></param> /// <param name="dataPoints"></param> /// <returns></returns> public static IEntity CreateEntity(string dataType, Dictionary <string, object> dataPoints) { var entity = ComponentRegistration.CreateInstance <IEntity>(dataType); if (entity != null) { var type = entity.GetType(); foreach (var point in dataPoints) { if (type.GetProperty(point.Key, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static) != null) { type.GetProperty(point.Key).SetValue(entity, point.Value); } } } return(entity); }
/// <summary> /// Create an array of collectors /// </summary> /// <param name="collectorConfigs">An array of collector configurations</param> /// <returns>a list of collectors</returns> public static List <ICollector> CreateCollectors(CollectorConfiguration[] collectorConfigs) { var result = new List <ICollector>(); foreach (var config in collectorConfigs) { ComponentRegistration.RegisterComponents(config); } ComponentRegistration.Build(); foreach (var config in collectorConfigs) { var collector = ComponentRegistration.CreateInstance <ICollector>(config.Type); collector.Configure(config); result.Add(collector); } return(result); }
/// <summary> /// Create a mapper to use to transform data. /// </summary> /// <param name="type"></param> /// <returns></returns> public static IMapper CreateMapper(MapperConfiguration mapperConfig) { var mapper = ComponentRegistration.CreateInstance <IMapper>(mapperConfig.Type); if (mapper == null) { throw new NullReferenceException("Unable to create Collector. Invalid Mapper defined."); } var converters = new Dictionary <string, IConverter>(); if (mapperConfig.SourceTargetMappings != null && mapperConfig.SourceTargetMappings.Count > 0) { foreach (var converterConfig in mapperConfig.PipedConverters) { var converter = ComponentRegistration.CreateInstance <IConverter>(converterConfig.Type); if (converter == null) { throw new NullReferenceException("Unable to create Collector. Invalid Converter defined."); } converter.Configure(converterConfig); converters.Add(converter.Id, converter); } } else { foreach (var converterConfig in mapperConfig.Converters) { var converter = ComponentRegistration.CreateInstance <IConverter>(converterConfig.Type); if (converter == null) { throw new NullReferenceException("Unable to create Collector. Invalid Converter defined."); } converter.Configure(converterConfig); foreach (var key in converterConfig.LeftSideMap.Keys) { converters.Add(key, converter); } } } mapper.Configure(mapperConfig, converters); return(mapper); }
public void MapperRegistration_Success() { ComponentRegistration.Reset(); ComponentRegistration.RegisterComponent(CreateMapperConfig("1", "2")); ComponentRegistration.RegisterTypesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = "Collector.SDK", Type = "LoggerFacade" }); ComponentRegistration.RegisterModulesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = "Autofac.Extras.NLog", RegisterAll = true }); ComponentRegistration.Build(); var mapper = ComponentRegistration.CreateInstance <IMapper>(ConfigurationTests.TYPE_MAPPER); mapper.Should().NotBeNull(); }
/// <summary> /// Create a Transformer. /// </summary> /// <param name="type">The name of the class to create.</param> /// <param name="id">The id of the transformer.</param> /// <param name="publishers">The publishers to use to publish the JSON data to a repository.</param> /// <param name="mappers">The mappers to use to transform/map the data.</param> /// <returns>A new handler.</returns> public static ITransformer CreateTransformer(TransformerConfiguration config, ITransformedDataHandler handler) { var mappers = new List <IMapper>(); foreach (var mapperConfig in config.Mappers) { var mapper = CollectorFactory.CreateMapper(mapperConfig); if (mapper == null) { throw new NullReferenceException("Unable to create Collector. Invalid Mapper defined."); } mappers.Add(mapper); } var transformer = ComponentRegistration.CreateInstance <ITransformer>(config.Type); transformer.Configure(config, handler); return(transformer); }
public static IPublisher ClonePublisher(string publisherId, IPublisher publisher) { try { var assemblyName = publisher.GetType().Assembly.GetName().Name; var type = string.Format(CultureInfo.InvariantCulture, "{0},{1}", publisher.GetType().FullName, assemblyName); var newPublisher = ComponentRegistration.CreateInstance <IPublisher>(type); newPublisher.Configure(publisherId, publisher.EndPointConfig); return(newPublisher); } catch (Exception e) { _logger.Error(e); } return(null); }
public void CollectorRegistration_Success() { ComponentRegistration.Reset(); ComponentRegistration.RegisterComponents(CreateCollectorConfig("1")); ComponentRegistration.RegisterTypesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = COLLECTOR_SDK_ASSEMBLY, Type = "LoggerFacade" }); ComponentRegistration.RegisterModulesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = NLOG_EXTRAS_ASSEMBLY, RegisterAll = true }); ComponentRegistration.Build(); var collector = ComponentRegistration.CreateInstance <ICollector>(ConfigurationTests.TYPE_COLLECTOR); collector.Should().NotBeNull(); }
public void ComponentRegistration_Build_Success() { ComponentRegistration.Reset(); ComponentRegistration.RegisterComponent <ICollector>(ConfigurationTests.TYPE_COLLECTOR); ComponentRegistration.RegisterTypesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = "Collector.SDK", Type = "LoggerFacade" }); ComponentRegistration.RegisterModulesFromAssembly(new ThirdPartyAutofacConfiguration() { AssemblyName = "Autofac.Extras.NLog", RegisterAll = true }); ComponentRegistration.Build(); var collector = ComponentRegistration.CreateInstance <ICollector>(ConfigurationTests.TYPE_COLLECTOR); collector.Should().NotBeNull(); }
public void LogPublisher_Publish_Success() { var currentDirectory = Directory.GetCurrentDirectory(); //var outFileName = string.Format(CultureInfo.InvariantCulture, "{0}\\publisher-log.txt", currentDirectory); var outFileName = string.Format("{0}\\publisher-log-{1}{2}{3}{4}{5}.txt", currentDirectory, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Year, DateTime.Now.Hour, DateTime.Now.Minute); var publisher = ComponentRegistration.CreateInstance <IPublisher>("Collector.SDK.Samples.Publishers.LogPublisher, Core.Collector.SDK.Samples"); var config = ConfigurationTests.CreateEndPointConfig("1", CollectorConstants.KEY_FOLDER, currentDirectory); publisher.Configure("5", config); var entity = new LogEntry() { DateTime = DateTime.Now, DateTimeUTC = DateTime.Now.ToUniversalTime(), Type = "INFO", Module = "SomeModuleName", Message = "Some log message" }; var data = new List <object>(); data.Add(entity); var context = new Dictionary <string, string>(); publisher.PublishData("3", data, context).Wait(); File.Exists(outFileName).Should().BeTrue(); var text = File.ReadAllText(outFileName); //text.Should().Contain("\"TYPE\":\"INFO\""); text.Should().Contain("\"Module\":\"SomeModuleName\""); text.Should().Contain("\"Message\":\"Some log message\""); }
/// <summary> /// Create an entire stack. /// </summary> /// <param name="readerId"></param> /// <param name="config"></param> /// <returns></returns> public static IStack CreateStack(string readerId, CollectorConfiguration config) { var stack = ComponentRegistration.CreateInstance <IStack>(config.StackType); foreach (var readerConfig in config.Readers) { if (readerConfig.Id == readerId) { var newReaderId = Guid.NewGuid().ToString(); List <IPublisher> publishers = new List <IPublisher>(); List <ITransformer> transformers = new List <ITransformer>(); foreach (var transformerConfig in config.Transformers) { if (transformerConfig.ReaderId == readerId) { var newTransformerId = Guid.NewGuid().ToString(); foreach (var publisherConfig in config.Publishers) { if (publisherConfig.TransformerId == transformerConfig.Id) { foreach (var endpoint in config.EndPoints) { if (endpoint.Id == publisherConfig.EndpointId) { var newEndpoint = new EndPointConfiguration() { Id = Guid.NewGuid().ToString(), Password = endpoint.Password, User = endpoint.User }; foreach (var key in endpoint.Properties.Keys) { newEndpoint.Properties.Add(key, endpoint.Properties[key]); } var newPublisherConfig = new PublisherConfiguration() { Id = Guid.NewGuid().ToString(), EndpointId = newEndpoint.Id, TransformerId = newTransformerId, Type = publisherConfig.Type }; var publisher = CreatePublisher(newPublisherConfig, endpoint); publishers.Add(publisher); } } } } var newTransformerConfig = new TransformerConfiguration() { Id = newTransformerId, Type = transformerConfig.Type, ReaderId = newReaderId, }; foreach (var mapper in transformerConfig.Mappers) { var newMapperConfig = new MapperConfiguration() { Id = mapper.Id, DataType = mapper.DataType, TransformerId = newTransformerId, Type = mapper.Type, SourceTargetMappings = mapper.SourceTargetMappings }; foreach (var converter in mapper.PipedConverters) { var newConverter = CopyConfig(converter); newMapperConfig.PipedConverters.Add(converter); } foreach (var converter in mapper.Converters) { var newConverter = CopyConfig(converter); newMapperConfig.Converters.Add(converter); } newTransformerConfig.Mappers.Add(newMapperConfig); } var transformer = CreateTransformer(newTransformerConfig, stack); transformers.Add(transformer); } } foreach (var endpoint in config.EndPoints) { if (readerConfig.EndpointId.Equals(endpoint.Id)) { var newEndpoint = new EndPointConfiguration() { Id = Guid.NewGuid().ToString(), Password = endpoint.Password, User = endpoint.User }; foreach (var key in endpoint.Properties.Keys) { newEndpoint.Properties.Add(key, endpoint.Properties[key]); } var reader = CreateReader(readerConfig.Type, newReaderId, newEndpoint, stack); stack.Configure(reader, transformers, publishers); break; } } break; } } return(stack); }