private PowerShellScriptPluginConfiguration UpdateConfiguration(DictionaryParameters parameters)
        {
            Contract.Requires(parameters.IsValid());

            // check for endpoint
            var configurationBase = SchedulerPluginConfigurationBase.Convert<SchedulerPluginConfigurationBase>(parameters);
            Contract.Assert(configurationBase.IsValid());
            
            var configurationManager = new PowerShellScriptPluginConfigurationManager(configurationBase.Endpoints);
            parameters.Add("ComputerName", configurationManager.GetComputerName());
            parameters.Add("ConfigurationName", configurationManager.GetConfigurationNameTemplate());
            parameters.Add("ScriptBase", configurationManager.GetScriptBase());
            parameters.Add("Credential", configurationManager.GetCredentials());

            var message = new StringBuilder();
            message.AppendLine("PowerShellScriptPlugin.UpdatingConfiguration ...");
            message.AppendLine();

            foreach(KeyValuePair<string, object> item in parameters)
            {
                message.AppendFormat("{0}: '{1}'", item.Key, item.Value ?? item.Value.ToString());
                message.AppendLine();
            }
            message.AppendLine();
            message.AppendLine("PowerShellScriptPlugin.UpdatingConfiguration COMPLETED.");
            
            Logger.WriteLine(message.ToString());

            configuration = PowerShellScriptPluginConfiguration.Convert<PowerShellScriptPluginConfiguration>(parameters, true);
            Contract.Assert(configuration.IsValid());

            return configuration;
        }
        public void InvokeWithMissingWorkingDirectoryThrowsContractException()
        {
            // Arrange
            var parameters = new DictionaryParameters();
            parameters.Add("CommandLine", "arbitrary-string");
            parameters.Add("missing-parameter-WorkingDirectory", "arbitrary-string");
            parameters.Add("Credential", CredentialCache.DefaultCredentials);
            var jobResult = new JobResult();

            // Act
            var sut = new ProgrammePlugin();
            var result = sut.Invoke(parameters, jobResult);

            // Assert
            // N/A
        }
        public void AddThrowsArgumentException()
        {
            // Arrange
            var sut = new DictionaryParameters()
            {
                { "Key1.1", "arbitrary-value1-1" }
                ,
                { "Key1.2", 11L }
                ,
                { "DuplicateKey", "arbitrary-value" }
            };

            var objectToMerge = new DictionaryParameters()
            {
                { "Key2.1", "arbitrary-value-2.1" }
                ,
                { "Key2.2", 22L }
                ,
                { "DuplicateKey", "arbitrary-value" }
            };

            // Act
            var result = sut.Add(objectToMerge);

            // Assert
            // N/A
        }
Esempio n. 4
0
        public DictionaryParameters Convert <TAttribute>(ConvertibleBaseDto convertibleDto, bool includeAllProperties)
            where TAttribute : ConversionKeyBaseAttribute
        {
            // create Dto to be returned
            var dictionaryParameters = new DictionaryParameters();

            // get all defined properties in Dto
            var propertyInfos = convertibleDto
                                .GetType()
                                .GetProperties
                                (
                BindingFlags.Static |
                BindingFlags.Instance |
                BindingFlags.Public |
                BindingFlags.FlattenHierarchy
                                );

            Contract.Assert(null != propertyInfos);

            foreach (var propertyInfo in propertyInfos)
            {
                // get annotation of property
                var attribute = (TAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(TAttribute));

                // skip if no attribute found ...
                if (null == attribute)
                {
                    // but add property if we should include all properties
                    if (includeAllProperties)
                    {
                        dictionaryParameters.Add(propertyInfo.Name, propertyInfo.GetValue(convertibleDto, null));
                    }
                    continue;
                }

                // assert that we do not have the annotated property in our dictionary
                Contract.Assert(!dictionaryParameters.ContainsKey(attribute.Name));

                // add value to dictionary
                dictionaryParameters.Add(attribute.Name, propertyInfo.GetValue(convertibleDto, null));
            }

            return(dictionaryParameters);
        }
        public void GetAndSetConfigurationSucceeds()
        {
            // Arrange
            var configuration = new DictionaryParameters();
            var key1 = "arbitrary-key1";
            var value1 = "arbitrary-value";
            configuration.Add(key1, value1);
            var key2 = "arbitrary-key2";
            var value2 = 42;
            configuration.Add(key2, value2);
            var key3 = "arbitrary-key3";
            var value3 = new object();
            configuration.Add(key3, value3);

            // Act
            var sut = new DefaultPlugin();
            sut.Configuration = configuration;

            // Assert
            Assert.AreEqual(configuration, sut.Configuration);
        }
        public void InvokeConfigurationSucceeds()
        {
            // Arrange
            var parameters = new DictionaryParameters();
            var key1 = "arbitrary-key1";
            var value1 = "arbitrary-value";
            parameters.Add(key1, value1);
            var key2 = "arbitrary-key2";
            var value2 = 42;
            parameters.Add(key2, value2);
            var key3 = "arbitrary-key3";
            var value3 = new object();
            parameters.Add(key3, value3);

            var jobResult = new JobResult();

            var schedulerPluginBase = Mock.Create<SchedulerPluginBase>();
            Mock.Arrange(() => schedulerPluginBase.Invoke(Arg.IsAny<DictionaryParameters>(), Arg.IsAny<IInvocationResult>()))
                .IgnoreInstance()
                .CallOriginal()
                .MustBeCalled();

            // Act
            var sut = new DefaultPlugin();
            sut.Initialise(new DictionaryParameters(), new Logger(), true);
            var result = sut.Invoke(parameters, jobResult);

            // Assert
            Assert.IsTrue(result);
            Assert.IsNotNull(jobResult);
            Assert.IsTrue(jobResult.Description.Contains(key1));
            Assert.IsTrue(jobResult.Description.Contains(value1.ToString()));
            Assert.IsTrue(jobResult.Description.Contains(key2));
            Assert.IsTrue(jobResult.Description.Contains(value2.ToString()));
            Assert.IsTrue(jobResult.Description.Contains(key3));
            Assert.IsTrue(jobResult.Description.Contains(value3.ToString()));

            Mock.Assert(schedulerPluginBase);
        }
        public void ConvertFromDictionaryParametersToSchedulerPluginConfigurationBaseSucceeds()
        {
            var uri = new Uri("http://www.example.com/");
            var credentials = CredentialCache.DefaultNetworkCredentials;
            var appclusiveEndpoints = new AppclusiveEndpoints(uri, credentials);

            var sut = new DictionaryParameters();
            sut.Add(typeof(AppclusiveEndpoints).ToString(), appclusiveEndpoints);

            // Act
            var result = SchedulerPluginConfigurationBase.Convert<SchedulerPluginConfigurationBase>(sut);

            // Assert
            Assert.IsTrue(result.IsValid());
            Assert.IsNotNull(result.Endpoints);
        }
Esempio n. 8
0
        // this method is provided to facilitate calling from PowerShell
        public static DictionaryParameters Merge(DictionaryParameters left, DictionaryParameters right, MergeOptions mergeOption)
        {
            Contract.Requires(null != left);
            Contract.Requires(null != right);

            var mergedDictionary = Merge <string, object>(left, right, mergeOption);

            if (null == mergedDictionary)
            {
                return(null);
            }

            var result = new DictionaryParameters();

            foreach (var kvp in mergedDictionary)
            {
                result.Add(kvp.Key, kvp.Value);
            }

            return(result);
        }
        public void DictionaryParametersReplaceExistingKeysFalseSucceeds()
        {
            var source = new DictionaryParameters()
            {
                { "key1", "value1" }
                ,
                { "key2", "value2" }
            };

            var objectToMerge = new DictionaryParameters()
            {
                { "key1", "value1-new" }
                ,
                { "key3", "value3" }
            };

            var result = source.Add(objectToMerge, false);

            Assert.IsTrue(result.ContainsKey("key1"));
            Assert.AreEqual("value1", result["key1"]);
            Assert.IsTrue(result.ContainsKey("key2"));
            Assert.IsTrue(result.ContainsKey("key3"));
        }
        public void AddSucceeds()
        {
            // Arrange
            var sut = new DictionaryParameters()
            {
                { "Key1.1", "arbitrary-value1-1" }
                ,
                { "Key1.2", 11L }
            };

            var objectToMerge = new DictionaryParameters()
            {
                { "Key2.1", "arbitrary-value-2.1" }
                ,
                { "Key2.2", 22L }
            };

            // Act
            var result = sut.Add(objectToMerge);

            // Assert
            Assert.AreEqual(4, result.Keys.Count);
        }
        private ActivitiPluginConfiguration UpdateConfiguration(DictionaryParameters parameters)
        {
            Contract.Requires(parameters.IsValid());

            // check for endpoint
            var configurationBase = SchedulerPluginConfigurationBase.Convert<SchedulerPluginConfigurationBase>(parameters);
            Contract.Assert(configurationBase.IsValid());
            var endpoints = configurationBase.Endpoints;

            // ManagementUri
            //Type                   : json
            //Value                  : {"ServerUri":"http://www.example.com:9080/activiti-rest/service/"}
            //ManagementCredentialId : 6
            //Id                     : 9
            //Tid                    : ad8f50df-2a5d-4ea5-9fcc-05882f16a9fe
            //Name                   : biz.dfch.PS.Activiti.Client.Setting
            //Description            :
            //CreatedById            : 1
            //ModifiedById           : 1
            //Created                : 12/15/2015 11:46:45 AM +01:00
            //Modified               : 12/15/2015 11:46:45 AM +01:00
            //RowVersion             : {0, 0, 0, 0...}
            //ManagementCredential   :
            //Tenant                 :
            //CreatedBy              :
            //ModifiedBy             :

            var configurationManager = new ActivitiPluginConfigurationManager();

            // get Activiti server
            var managementUri = configurationManager
                .GetManagementUri(endpoints.Core.ManagementUris, configurationManager.GetManagementUriName());
            
            var activitiPluginConfigurationManagementUri = BaseDto
                .DeserializeObject<ActivitiPluginConfigurationManagementUri>(managementUri.Value);
            parameters.Add("ServerBaseUri", activitiPluginConfigurationManagementUri.ServerUri);

            // get Activiti credentials
            var managementCredential = configurationManager
            .GetManagementCredential(endpoints.Core.ManagementCredentials, managementUri.ManagementCredentialId.Value);

            var networkCredential = configurationManager.GetCredential(managementCredential);
            parameters.Add("Credential", networkCredential);

            configuration = ActivitiPluginConfiguration.Convert<ActivitiPluginConfiguration>(parameters, true);
            Contract.Assert(configuration.IsValid());

            // log in to server
            client = new ActivitiClient(configuration.ServerBaseUri, applicationName);
            client.Login(configuration.Credential);

            return configuration;
        }
        public ScheduledJobsWorkerConfiguration(IConfigurationLoader loader, string[] args)
        {
            var parameters = new DictionaryParameters();
            if(null != args)
            {
                for(var c = 0; c < args.Count(); c++)
                {
                    var arg = args[c];
                    parameters.Add(string.Format("args{0}", c), arg);
                }
            }

            loader.Initialise(this, parameters);

            return;
        }
        public void InitialiseSucceeds()
        {
            // Arrange
            var client = Mock.Create<ActivitiClient>();
            Mock.Arrange(() => client.Login(Arg.IsAny<NetworkCredential>()))
                .IgnoreInstance()
                .Returns(true)
                .MustBeCalled();

            var serverBaseUri = new Uri("http://www.example.com:9080/activiti-rest/service/");
            var managementCredentialId = 5;
            var managementUriName = SchedulerAppSettings.Keys.EXTERNAL_WORKFLOW_MANAGEMENT_URI_NAME;
            var managementUri = new ManagementUri()
            {
                Id = 42
                ,
                Name = managementUriName
                ,
                ManagementCredentialId = managementCredentialId
                ,
                Value = string.Format("{{\"ServerUri\":\"{0}\"}}", serverBaseUri)
            };
            var managementUris = new List<ManagementUri>()
            {
                managementUri
            };

            var dataServiceQueryManagementUris = Mock.Create<DataServiceQuery<ManagementUri>>();
            Mock.Arrange(() => dataServiceQueryManagementUris.Where(e => e.Name == managementUriName))
                .IgnoreInstance()
                .Returns(managementUris.AsQueryable())
                .MustBeCalled();

            var encryptedPassword = "******";
            var managementCredential = new ManagementCredential()
            {
                Id = managementCredentialId
                ,
                Name = managementUriName
                ,
                Username = Username
                ,
                Password = Password
                ,
                EncryptedPassword = encryptedPassword
            };
            var managementCredentials = new List<ManagementCredential>()
            {
                managementCredential
            };

            var dataServiceQueryManagementCredentials = Mock.Create<DataServiceQuery<ManagementCredential>>();
            Mock.Arrange(() => dataServiceQueryManagementCredentials.Where(e => e.Id == managementCredentialId))
                .IgnoreInstance()
                .Returns(managementCredentials.AsQueryable())
                .MustBeCalled();

            var endpoints = Mock.Create<AppclusiveEndpoints>(Constructor.Mocked);
            var apiCore = Mock.Create<Api.Core.Core>(Constructor.Mocked);
            endpoints.Core = apiCore;
            var parameters = new DictionaryParameters();
            parameters.Add(typeof(AppclusiveEndpoints).ToString(), endpoints);
            
            Mock.SetupStatic(typeof(ConfigurationManager));
            Mock.Arrange(() => ConfigurationManager.AppSettings[Arg.IsAny<string>()])
                .Returns(managementUriName)
                .MustBeCalled();

            var logger = new Logger();

            var sut = new ActivitiPlugin();

            // Act
            var result = sut.Initialise(parameters, logger, true);

            // Assert
            Mock.Assert(() => ConfigurationManager.AppSettings[Arg.IsAny<string>()]);
            Mock.Assert(client);

            Assert.IsTrue(result);
            Assert.IsTrue(sut.IsInitialised);
            Assert.IsTrue(sut.IsActive);
        }
        public void UpdateConfigurationSucceeds()
        {
            // Arrange
            Mock.SetupStatic(typeof(ConfigurationManager));
            Mock.Arrange(() => ConfigurationManager.AppSettings[Arg.IsAny<string>()])
                .Returns(SchedulerAppSettings.Keys.EXTERNAL_WORKFLOW_MANAGEMENT_URI_NAME)
                .MustBeCalled();

            var client = Mock.Create<ActivitiClient>();
            Mock.Arrange(() => client.Login(Arg.IsAny<NetworkCredential>()))
                .IgnoreInstance()
                .Returns(true)
                .MustBeCalled();

            var serverBaseUri = new Uri("http://www.example.com:9080/activiti-rest/service/");
            var managementCredentialId = 5;
            var managementUriName = SchedulerAppSettings.Keys.EXTERNAL_WORKFLOW_MANAGEMENT_URI_NAME;
            var managementUri = new ManagementUri()
            {
                Id = 42
                ,
                Name = managementUriName
                ,
                ManagementCredentialId = managementCredentialId
                ,
                Value = string.Format("{{\"ServerUri\":\"{0}\"}}", serverBaseUri)
            };
            var managementUris = new List<ManagementUri>()
            {
                managementUri
            };

            var dataServiceQueryManagementUris = Mock.Create<DataServiceQuery<ManagementUri>>();
            Mock.Arrange(() => dataServiceQueryManagementUris.Where(e => e.Name == managementUriName))
                .IgnoreInstance()
                .Returns(managementUris.AsQueryable())
                .MustBeCalled();

            var encryptedPassword = "******";
            var managementCredential = new ManagementCredential()
            {
                Id = managementCredentialId
                ,
                Name = managementUriName
                ,
                Username = Username
                ,
                Password = Password
                ,
                EncryptedPassword = encryptedPassword
            };
            var managementCredentials = new List<ManagementCredential>()
            {
                managementCredential
            };

            var dataServiceQueryManagementCredentials = Mock.Create<DataServiceQuery<ManagementCredential>>();
            Mock.Arrange(() => dataServiceQueryManagementCredentials.Where(e => e.Id == managementCredentialId))
                .IgnoreInstance()
                .Returns(managementCredentials.AsQueryable())
                .MustBeCalled();

            var endpoints = Mock.Create<AppclusiveEndpoints>(Constructor.Mocked);
            var apiCore = Mock.Create<Api.Core.Core>(Constructor.Mocked);
            endpoints.Core = apiCore;
            var parameters = new DictionaryParameters();
            parameters.Add(typeof(AppclusiveEndpoints).ToString(), endpoints);

            var sut = new ActivitiPlugin();
            Mock.NonPublic.Arrange<string>(sut, "managementUriName")
                .IgnoreInstance()
                .Returns(managementUriName);

            // Act
            sut.Configuration = parameters;

            // Assert
            Mock.Arrange(() => ConfigurationManager.AppSettings[Arg.IsAny<string>()]);
            Mock.Assert(client);
            Mock.Assert(endpoints);
            Mock.Assert(dataServiceQueryManagementUris);
            Mock.Assert(dataServiceQueryManagementCredentials);

            Assert.IsTrue(sut.Configuration.ContainsKey("ServerBaseUri"));
            var actualServerBaseUri = sut.Configuration["ServerBaseUri"] as Uri;
            Assert.IsNotNull(actualServerBaseUri);
            Assert.AreEqual(serverBaseUri, actualServerBaseUri);

            Assert.IsTrue(sut.Configuration.ContainsKey("Credential"));
            var actualCredential = sut.Configuration["Credential"] as NetworkCredential;
            Assert.IsNotNull(actualCredential);
            Assert.AreEqual(Username, actualCredential.UserName);
            Assert.AreEqual(Password, actualCredential.Password);
        }
        public void LogSucceeds()
        {
            // Arrange
            var client = Mock.Create<ActivitiClient>();
            Mock.Arrange(() => client.Login(Arg.IsAny<NetworkCredential>()))
                .IgnoreInstance()
                .Returns(true)
                .MustBeCalled();

            var managementUriName = SchedulerAppSettings.Keys.EXTERNAL_WORKFLOW_MANAGEMENT_URI_NAME;
            var serverBaseUri = new Uri("http://www.example.com:9080/activiti-rest/service/");
            var managementCredentialId = 5;
            var managementUri = new ManagementUri()
            {
                Id = 42
                ,
                Name = managementUriName
                ,
                ManagementCredentialId = managementCredentialId
                ,
                Value = string.Format("{{\"ServerUri\":\"{0}\"}}", serverBaseUri)
            };

            var encryptedPassword = "******";
            var managementCredential = new ManagementCredential()
            {
                Id = managementCredentialId
                ,
                Name = managementUriName
                ,
                Username = Username
                ,
                Password = Password
                ,
                EncryptedPassword = encryptedPassword
            };

            var activitiPluginConfigurationManager = Mock.Create<ActivitiPluginConfigurationManager>();
            Mock.Arrange(() => activitiPluginConfigurationManager.GetManagementUriName())
                .IgnoreInstance()
                .Returns(managementUriName)
                .MustBeCalled();
            Mock.Arrange(() => activitiPluginConfigurationManager.GetManagementUri(Arg.IsAny<DataServiceQuery<ManagementUri>>(), Arg.IsAny<string>()))
                .IgnoreInstance()
                .Returns(managementUri)
                .MustBeCalled();
            Mock.Arrange(() => activitiPluginConfigurationManager.GetManagementCredential(Arg.IsAny<DataServiceQuery<ManagementCredential>>(), Arg.IsAny<long>()))
                .IgnoreInstance()
                .Returns(managementCredential)
                .MustBeCalled();
            
            var endpoints = Mock.Create<AppclusiveEndpoints>(Constructor.Mocked);
            var apiCore = Mock.Create<Api.Core.Core>(Constructor.Mocked);
            endpoints.Core = apiCore;
            var parameters = new DictionaryParameters();
            parameters.Add(typeof(AppclusiveEndpoints).ToString(), endpoints);

            var message = "arbitrary-message";
            var logger = new Logger();
            var sut = new ActivitiPlugin();

            sut.Initialise(parameters, logger, true);

            // Act
            sut.Logger.WriteLine(message);

            // Assert
            Mock.Assert(client);
            Mock.Assert(activitiPluginConfigurationManager);
        }
        public void InvokeWithInvalidTypeCredentialThrowsContractException()
        {
            // Arrange
            var parameters = new DictionaryParameters();
            parameters.Add("CommandLine", "arbitrary-string");
            parameters.Add("WorkingDirectory", "arbitrary-string");
            parameters.Add("Credential", new object());
            var jobResult = new JobResult();

            // Act
            var sut = new ProgrammePlugin();
            var result = sut.Invoke(parameters, jobResult);

            // Assert
            // N/A
        }
        public void InvokeSucceeds()
        {
            // Arrange
            var commandLine = "arbitrary-string";
            var workingDirectory = "arbitrary-string";
            var credential = new NetworkCredential("arbitrary-user", "arbitrary-password", "arbitrary-domain");
            var parameters = new DictionaryParameters();
            parameters.Add("JobId", 42);
            parameters.Add("CommandLine", commandLine);
            parameters.Add("WorkingDirectory", workingDirectory);
            parameters.Add("Credential", credential);
            var jobResult = new JobResult();

            Mock.SetupStatic(typeof(biz.dfch.CS.Utilities.Process));

            Mock.Arrange(
                () => biz.dfch.CS.Utilities.Process.StartProcess(
                    Arg.Is<string>(commandLine), 
                    Arg.Is<string>(workingDirectory), 
                    Arg.Is<NetworkCredential>(credential))
                )
                .Returns(default(Dictionary<string, string>))
                .OccursOnce();

            // Act
            var sut = new ProgrammePlugin();
            sut.Initialise(new DictionaryParameters(), new Logger(), true);
            var result = sut.Invoke(parameters, jobResult);

            // Assert
            Mock.Assert(
                () => biz.dfch.CS.Utilities.Process.StartProcess(
                    Arg.Is<string>(commandLine), 
                    Arg.Is<string>(workingDirectory), 
                    Arg.Is<NetworkCredential>(credential))
                );
            Assert.IsTrue(result);
            Assert.IsTrue(jobResult.Succeeded);
            Assert.AreEqual(0, jobResult.Code);
        }
        public void InvokeFails()
        {
            // Arrange
            var commandLine = "arbitrary-string";
            var workingDirectory = "arbitrary-string";
            var credential = new NetworkCredential("arbitrary-user", "arbitrary-password", "arbitrary-domain");
            var parameters = new DictionaryParameters();
            parameters.Add("JobId", 42);
            parameters.Add("CommandLine", commandLine);
            parameters.Add("WorkingDirectory", workingDirectory);
            parameters.Add("Credential", credential);
            var jobResult = new JobResult();

            Mock.SetupStatic(typeof(biz.dfch.CS.Utilities.Process));

            Mock.Arrange(
                () => biz.dfch.CS.Utilities.Process.StartProcess(
                    Arg.Is<string>(commandLine),
                    Arg.Is<string>(workingDirectory),
                    Arg.Is<NetworkCredential>(credential))
                )
                .Throws<InvalidOperationException>()
                .OccursOnce();

            // Act
            var sut = new ProgrammePlugin();
            sut.Initialise(new DictionaryParameters(), new Logger(), true);
            var result = sut.Invoke(parameters, jobResult);

            // Assert
            Mock.Assert(
                () => biz.dfch.CS.Utilities.Process.StartProcess(
                    Arg.Is<string>(commandLine), 
                    Arg.Is<string>(workingDirectory), 
                    Arg.Is<NetworkCredential>(credential))
                );
            Assert.IsFalse(result);
            Assert.IsFalse(jobResult.Succeeded);
            Assert.AreNotEqual(0, jobResult.Code);
            Assert.IsFalse(string.IsNullOrWhiteSpace(jobResult.Message));
            Assert.IsFalse(string.IsNullOrWhiteSpace(jobResult.Description));
        }