public void TestSimulationProducerContract()
        {
            var app = ApplicationProducerContract.Get("AspenPlus");

            Assert.IsNotNull(app);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("test", "AspenPlus");

            Assert.IsNotNull(simulation_contract);

            byte[] data;
            using (var fstream = File.Open("mea.bkp", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }

            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("aspenfile", data, "plain/text");
            data = null;
            using (var fstream = File.Open("mea-sinter.txt", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }

            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("configuration", data, "plain/text");
        }
        public void TestSimulationProducerConsumerContracts()
        {
            var simulation_name = "TestSimulationProducerConsumerContracts";
            var input_name      = "configuration";
            ISimulationProducerContract contract = AspenSimulationContract.Create(simulation_name, "ACM");

            using (var container = new Turbine.Data.TurbineModelContainer())
            {
                var obj   = container.Simulations.Single <Turbine.Data.Simulation>(s => s.Name == simulation_name);
                var input = obj.SimulationStagedInputs.Single <Turbine.Data.SimulationStagedInput>(i => i.Name == input_name);
                Assert.IsNull(input.Hash);
                Assert.IsNull(input.Content);
                Debug.WriteLine("SimulationStagedInput: " + input.Name, this.GetType().Name);

                input_name = "aclm";
                input      = obj.SimulationStagedInputs.Single <Turbine.Data.SimulationStagedInput>(i => i.Name == input_name);
                Assert.IsNull(input.Hash);
                Assert.IsNull(input.Content);
                Debug.WriteLine("SimulationStagedInput: " + input.Name, this.GetType().Name);
            }

            input_name = "configuration";
            contract   = AspenSimulationContract.Get(simulation_name);
            byte[] data     = Encoding.UTF8.GetBytes("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            var    provider = System.Security.Cryptography.MD5CryptoServiceProvider.Create();

            byte[] hash = provider.ComputeHash(data);
            var    sb   = new StringBuilder();

            foreach (byte b in hash)
            {
                sb.Append(b.ToString("X2"));
            }
            string hval = sb.ToString();

            var  content_type = "plain/text";
            bool success      = contract.UpdateInput(input_name, data, content_type);

            using (var container = new Turbine.Data.TurbineModelContainer())
            {
                var obj   = container.Simulations.OrderByDescending(q => q.Create).First <Turbine.Data.Simulation>(s => s.Name == simulation_name);
                var input = obj.SimulationStagedInputs.Single <Turbine.Data.SimulationStagedInput>(i => i.Name == input_name);
                Debug.WriteLine(String.Format("SimulationStagedInput: {0}, {1}, {2}", input.Name, input.Hash, Encoding.UTF8.GetString(input.Content)),
                                this.GetType().Name);
                Assert.AreEqual(input.Hash, hval);
                Assert.AreEqual(Encoding.UTF8.GetString(input.Content), "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
            }
        }
Пример #3
0
        public void TestSimulationContract()
        {
            var simContract = AspenSimulationContract.Get("testSim1"); //testSim1 is the first in Simulations
            var data        = new Byte[] { 0XAA };                     // Just a BS holder value in place of the aspen.bkp file
            //""abs_ht_wash"":5.0,
            var json       = @"{""P_abs_top"":16.0,""abs_ht_mid"":10.7,""abs_ic_dT"":-11.3,""P_sol_pump"":30.0,""lean_load"":0.178,""P_regen_top"":21.2,""cond_T_regen"":121.0,""ht_regen"":13.0,""slv_cool_01"":130.0,""lr_rich_T"":207.0,""reg_dia_in"":17.37,""input_s[0,0]"":134.4,""input_s[0,1]"":16.0,""input_s[0,2]"":959553.0,""input_s[0,3]"":0.071,""input_s[0,4]"":0.0,""input_s[0,5]"":0.0,""input_s[0,6]"":0.211,""input_s[0,7]"":0.0,""input_s[0,8]"":0.0,""input_s[0,9]"":0.0,""input_s[0,10]"":0.0,""input_s[0,11]"":0.0,""input_s[0,12]"":0.0,""input_s[0,13]"":0.0,""input_s[0,14]"":0.0,""input_s[0,15]"":0.718,""input_s[1,0]"":126.0,""input_s[1,1]"":30.0,""input_s[1,2]"":4319500.4116,""input_s[1,3]"":0.66207726067,""input_s[1,4]"":0.28374739743,""input_s[1,5]"":0.0,""input_s[1,6]"":0.0541753419,""input_s[1,7]"":0.0,""input_s[1,8]"":0.0,""input_s[1,9]"":0.0,""input_s[1,10]"":0.0,""input_s[1,11]"":0.0,""input_s[1,12]"":0.0,""input_s[1,13]"":0.0,""input_s[1,14]"":0.0,""input_s[1,15]"":0.0,""input_s[2,0]"":120.0,""input_s[2,1]"":30.0,""input_s[2,2]"":10000.0,""input_s[2,3]"":1.0,""input_s[2,4]"":0.0,""input_s[2,5]"":0.0,""input_s[2,6]"":0.0,""input_s[2,7]"":0.0,""input_s[2,8]"":0.0,""input_s[2,9]"":0.0,""input_s[2,10]"":0.0,""input_s[2,11]"":0.0,""input_s[2,12]"":0.0,""input_s[2,13]"":0.0,""input_s[2,14]"":0.0,""input_s[2,15]"":0.0,""eq_par[0,0]"":0.7996,""eq_par[0,1]"":-8094.81,""eq_par[0,2]"":0.0,""eq_par[0,3]"":-0.007484,""eq_par[1,0]"":98.566,""eq_par[1,1]"":1353.8,""eq_par[1,2]"":-14.3043,""eq_par[1,3]"":0.0,""eq_par[2,0]"":216.049,""eq_par[2,1]"":-12431.7,""eq_par[2,2]"":-35.4819,""eq_par[2,3]"":0.0,""eq_par[3,0]"":1.282562,""eq_par[3,1]"":-3456.179,""eq_par[3,2]"":0.0,""eq_par[3,3]"":0.0,""eq_par[4,0]"":132.899,""eq_par[4,1]"":-13445.9,""eq_par[4,2]"":-22.4773,""eq_par[4,3]"":0.0}";
            var initialize = false;

            Dictionary <string, Object> inputData = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(json);


            simContract.UpdateAspenBackup(data);  // Set the aspen.bkp file to BS value (really this should've already been set when the sim was created)

            // Producer: Produces jobs to be run from simulations
            IJobProducerContract producer = simContract.NewJob(Guid.NewGuid(), initialize); // Ask the AspenSimulationContract to create a new job based on the selected simulation

            producer.Submit();                                                              //Basically just sets state to submit
            int jobId = producer.Id;

            // Consumer: Pops submitted jobs off the 'queue' and runs them
            IJobConsumerContract consumer = new AspenJobConsumerContract()
            {
                Id = jobId
            };
            IProcess process = consumer.Setup();                                                         //Setup the job (change state, copy setup files)  Returns SinterProcessContract

            process.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); //Sets the dir in the function to MyDocs, but doesn't actually chdir
            //process.Input = inputData.ToArray<string>(); //The varaible inputs are a file?  I actually didn't expect that.

            process.Input = inputData;

            consumer.Running();                         //Does some checking and changes state to running.  Run will actually be done outside contract
            consumer.Success();                         //Changes state to success. (Other option is error)

            Byte[] d2 = consumer.GetSimulationBackup(); //Final checks to make sure that all kinda worked.
            Assert.IsTrue(data.SequenceEqual(d2));
            process = consumer.Process;
            Assert.IsTrue(inputData.SequenceEqual(process.Input));
        }
        public string CreateSimulation(string appname)
        {
            string name = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["simulation"];

            if (string.IsNullOrEmpty(name))
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.QUERYPARAM_ERROR,
                    "Required parameter \"simulation\":  Provide unique name for factory method."
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.BadRequest);
            }

            ISimulationProducerContract contract = null;

            try
            {
                contract = AspenSimulationContract.Create(name, appname);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to Create Simulation {0}: {1} {2}",
                                           name, ex.Message, ex.StackTrace.ToString());
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }

            //Simulation sim = DataMarshal.GetSimulation(name);
            var uri = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri;

            return(new Uri(uri, String.Format("../simulation/{0}", name)).ToString());
        }
        public void TestWrongParameter()
        {
            var app = ApplicationProducerContract.Get("Excel");

            Assert.IsNotNull(app);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("test", "Excel");

            Assert.IsNotNull(simulation_contract);

            byte[] data;
            using (var fstream = File.Open("exceltest.xlsm", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }
            Assert.IsTrue(data.Length > 0);
            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("wrongparameter", data, "plain/text");
        }
        internal static ISimulationProducerContract CreateExcelSimulation(String spreadsheet, String sinter)
        {
            var app = ApplicationProducerContract.Get("Excel");

            Assert.IsNotNull(app);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("test", "Excel");

            Assert.IsNotNull(simulation_contract);

            byte[] data;
            using (var fstream = File.Open(spreadsheet, FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }

            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("spreadsheet", data, "plain/text");
            data = null;
            using (var fstream = File.Open(sinter, FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }
            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("configuration", data, "plain/text");

            return(simulation_contract);
        }
Пример #7
0
        public bool UpdateStagedInputFile(string name, string inputName, Stream data)
        {
            Debug.WriteLine("UpdateStagedInputFile: " + name, this.GetType());
            //Simulation sim = GetSimulation(name);
            OutgoingWebResponseContext ctx = WebOperationContext.Current.OutgoingResponse;
            var content_type = WebOperationContext.Current.IncomingRequest.ContentType;

            byte[] bytes;
            using (var ms = new MemoryStream())
            {
                data.CopyTo(ms);
                bytes = ms.ToArray();
            }

            //StreamReader sr = new StreamReader(data);
            //string s = sr.ReadToEnd();
            //byte[] bytes = System.Text.Encoding.ASCII.GetBytes(s);
            Debug.WriteLine("content_type " + content_type, this.GetType());

            // TODO: Validate Backup File
            var  contract = AspenSimulationContract.Get(name);
            bool success  = false;

            try
            {
                success = contract.UpdateInput(inputName, bytes, content_type);
            }
            catch (ArgumentException ex)
            {
                string msg = String.Format("Failed to Update Simulation {0} input {1}: {2}",
                                           name, inputName, ex.Message);
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.AUTHORIZATION_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.Unauthorized);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to Update Simulation {0} input {1}: {2}, {3}",
                                           name, inputName, ex.Message, ex.StackTrace);
                Debug.WriteLine(msg, this.GetType().Name);
                if (ex.InnerException != null)
                {
                    Debug.WriteLine("InnerException: " + ex.InnerException.Message, this.GetType().Name);
                    Debug.WriteLine(ex.InnerException.StackTrace, this.GetType().Name);
                }
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.InternalServerError);
            }

            //sim = GetSimulation(name);
            ctx.ContentType = content_type;
            ctx.StatusCode  = System.Net.HttpStatusCode.OK;

            //return new MemoryStream(bytes);
            return(success);
        }
Пример #8
0
        /*  AppendJobs
         *
         * Each job request has a "Simulation" and "Input"
         *
         */
        public List <int> AppendJobs(string sessionID, System.IO.Stream data)
        {
            Debug.WriteLine(String.Format("append jobs to session {0}", sessionID), this.GetType().Name);
            Object name       = "";
            Object inputs     = "";
            Object initialize = false;
            Object reset      = true;

            StreamReader sr   = new StreamReader(data);
            string       json = sr.ReadToEnd();
            List <Dictionary <string, Object> > jobsInputList =
                Newtonsoft.Json.JsonConvert.DeserializeObject <List <Dictionary <string, Object> > >(json);
            List <Dictionary <String, Object> > jobDictList = new List <Dictionary <string, object> >();
            List <string> simulationList = new List <string>();
            List <bool>   initializeList = new List <bool>();
            List <bool>   resetList      = new List <bool>();

            foreach (var inputData in jobsInputList)
            {
                if (!inputData.TryGetValue("Simulation", out name))
                {
                    Debug.WriteLine("Missing simulation name", this.GetType().Name);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Simulation is required"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                if (!inputData.TryGetValue("Input", out inputs))
                {
                    Debug.WriteLine("Missing input data", this.GetType().Name);
                    throw new ArgumentException("No Input data name");
                }

                if (!inputData.TryGetValue("Initialize", out initialize))
                {
                    initialize = false;
                }
                if (!inputData.TryGetValue("Reset", out reset))
                {
                    reset = true;
                }

                Newtonsoft.Json.Linq.JObject inputsD = null;
                try
                {
                    inputsD = (Newtonsoft.Json.Linq.JObject)inputs;
                }
                catch (InvalidCastException)
                {
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Input is required to be a dictionary"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                Dictionary <String, Object> dd = new Dictionary <string, object>();
                foreach (var v in inputsD)
                {
                    Debug.WriteLine("VALUE: " + v);
                    dd[v.Key] = v.Value;
                }
                jobDictList.Add(dd);
                simulationList.Add(String.Format("{0}", name));
                try
                {
                    initializeList.Add((bool)initialize);
                }
                catch (InvalidCastException)
                {
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key initialize is required to be a boolean"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }
                try
                {
                    resetList.Add((bool)reset);
                }
                catch (InvalidCastException)
                {
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        "Key Reset is required to be a boolean"
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }
            }

            List <int> jobList = new List <int>();

            // Formatting complete
            for (int i = 0; i < jobDictList.Count; i++)
            {
                ISimulationProducerContract contract = null;
                string simulationName          = simulationList[i];
                Dictionary <String, Object> dd = jobDictList[i];

                try
                {
                    contract = AspenSimulationContract.Get(simulationName);
                }
                catch (Exception ex)
                {
                    string msg = String.Format("Failed to get Simulation {0}: {1}",
                                               name, ex.StackTrace.ToString());
                    Debug.WriteLine(msg, this.GetType().Name);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        msg
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }

                IJobProducerContract jobContract;
                try
                {
                    jobContract = contract.NewJob(new Guid(sessionID), initializeList[i], resetList[i]);
                }
                catch (Exception ex)
                {
                    string msg = String.Format("Failed to create new job {0}: {1}",
                                               i, ex.StackTrace.ToString());
                    Debug.WriteLine(msg, this.GetType().Name);
                    var detail = new ErrorDetail(
                        ErrorDetail.Codes.DATAFORMAT_ERROR,
                        msg
                        );
                    throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
                }
                jobContract.Process.Input = dd;
                jobList.Add(jobContract.Id);
            }

            return(jobList);
        }
Пример #9
0
        public Stream CreateJob(Stream data)
        {
            Debug.WriteLine("create new job", this.GetType().Name);
            StreamReader sr   = new StreamReader(data);
            string       json = sr.ReadToEnd();

            Debug.WriteLine("JSON: " + json, this.GetType().Name);
            Dictionary <string, Object> inputData = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(json);
            Object name = "";
            Guid   sessionID;

            if (!inputData.TryGetValue("Session", out name))
            {
                Debug.WriteLine("Missing Session", this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key Session is required"
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.BadRequest);
            }
            try
            {
                sessionID = Guid.Parse((string)name);
            }
            catch (Exception)
            {
                Debug.WriteLine("Bad Session", this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Bad Session ID, must be GUID"
                    );
                throw new WebFaultException <ErrorDetail>(detail,
                                                          System.Net.HttpStatusCode.BadRequest);
            }

            if (!inputData.TryGetValue("Simulation", out name))
            {
                Debug.WriteLine("Missing simulation name", this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key Simulation is required"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }


            Object tmp = false;

            if (!inputData.TryGetValue("initialize", out tmp))
            {
                tmp = false;
            }
            bool initialize;

            try
            {
                initialize = (bool)tmp;
            }
            catch (InvalidCastException)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key initialize is required to be a boolean"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }

            if (!inputData.TryGetValue("reset", out tmp))
            {
                tmp = true;
            }
            bool reset;

            try
            {
                reset = (bool)tmp;
            }
            catch (InvalidCastException)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key reset is required to be a boolean"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }

            Debug.WriteLine("Retrieve Simulation Contract: " + name, this.GetType().Name);
            ISimulationProducerContract contract = null;

            try
            {
                contract = AspenSimulationContract.Get((string)name);
            }
            catch (Exception ex)
            {
                string msg = String.Format("Failed to get Simulation {0}: {1}",
                                           name, ex.StackTrace.ToString());
                Debug.WriteLine(msg, this.GetType().Name);
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    msg
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }
            Object inputs;

            if (!inputData.TryGetValue("Inputs", out inputs))
            {
                Debug.WriteLine("Missing input data", this.GetType().Name);
                throw new ArgumentException("No Input data name");
            }
            Debug.WriteLine("Found Inputs", this.GetType().Name);
            Newtonsoft.Json.Linq.JObject inputsD = null;
            try
            {
                inputsD = (Newtonsoft.Json.Linq.JObject)inputs;
            }
            catch (InvalidCastException)
            {
                var detail = new ErrorDetail(
                    ErrorDetail.Codes.DATAFORMAT_ERROR,
                    "Key Inputs is required to be a dictionary"
                    );
                throw new WebFaultException <ErrorDetail>(detail, System.Net.HttpStatusCode.BadRequest);
            }
            Debug.WriteLine("Cast Inputs", this.GetType().Name);

            Dictionary <String, Object> dd = new Dictionary <string, object>();

            foreach (var v in inputsD)
            {
                Debug.WriteLine("VALUE: " + v);
                dd[v.Key] = v.Value;
            }
            var jobContract = contract.NewJob(sessionID, initialize, reset);

            jobContract.Process.Input = dd;

            var d = DataMarshal.GetJobDict(jobContract.Id, true);

            json = Newtonsoft.Json.JsonConvert.SerializeObject(d);
            return(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)));
        }
        public void TestSessionProducerConsumerContracts()
        {
            // producers
            ISessionProducerContract producer = new AspenSessionProducerContract();
            Guid session_id = producer.Create();

            Assert.IsNotNull(session_id);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("testX", "AspenPlus");

            var bytes = File.ReadAllBytes("mea-sinter.txt");

            Assert.IsNotNull(bytes);
            Assert.IsTrue(bytes.Length > 0);
            System.Threading.Thread.Sleep(100);
            simulation_contract.UpdateInput("configuration", bytes, "plain/text");

            bytes = File.ReadAllBytes("mea.bkp");
            Assert.IsNotNull(bytes);
            Assert.IsTrue(bytes.Length > 0);
            System.Threading.Thread.Sleep(100);
            simulation_contract.UpdateInput("aspenfile", bytes, "plain/text");

            IJobProducerContract job_producer_contract = simulation_contract.NewJob(session_id, false, true);

            job_producer_contract = simulation_contract.NewJob(session_id, false, true);

            IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract();

            contract.Register();

            IJobConsumerContract job = contract.Queue.GetNext();

            Assert.IsNull(job);
            job_producer_contract.Submit();
            job = contract.Queue.GetNext();
            Assert.IsNotNull(job);
            Assert.AreEqual(job.Id, job_producer_contract.Id);


            job.Setup();

            SimpleFile backup = null;
            SimpleFile config = null;

            foreach (var f in job.GetSimulationInputFiles())
            {
                if (f.name == "configuration")
                {
                    config = f;
                }
                else
                {
                    backup = f;
                }
            }

            Assert.AreEqual(config.name, "configuration");

            String filename = SinterHelperFunctions
                              .getBackupFilename(System.Text.Encoding.ASCII.GetString(config.content));

            Assert.AreEqual(backup.name, "aspenfile");


            // NEED TO SET INPUT Before Setting to Run
            //j
            try
            {
                job.Running();
                Assert.Fail("Job.Process.Input Must be set before setting job to run");
            }
            catch (InvalidStateChangeException) {}
            job.Process.Input = new Dictionary <string, Object>();
            job.Running();
            job.Success();

            job = contract.Queue.GetNext();
            Assert.IsNull(job);
        }
        public void TestJobProducerConsumerContracts()
        {
            // producers
            ISessionProducerContract producer = new AspenSessionProducerContract();
            Guid session_id = producer.Create();

            Assert.IsNotNull(session_id);

            ISimulationProducerContract simulation_contract;

            simulation_contract = AspenSimulationContract.Create("test", "AspenPlus");

            Assert.IsTrue(File.Exists("mea-sinter.txt"));
            Assert.IsTrue(File.Exists("mea.bkp"));

            var configContent    = File.ReadAllBytes("mea-sinter.txt");
            var aspenfileContent = File.ReadAllBytes("mea.bkp");

            Assert.IsTrue(configContent.Length > 0);
            Assert.IsTrue(aspenfileContent.Length > 0);

            System.Threading.Thread.Sleep(100);
            simulation_contract.UpdateInput("configuration",
                                            configContent,
                                            "plain/text");
            System.Threading.Thread.Sleep(1000);
            simulation_contract.UpdateInput("aspenfile",
                                            aspenfileContent,
                                            "plain/text");

            IJobProducerContract job_producer_contract = simulation_contract.NewJob(session_id, false, true);

            // consumers
            //IJobQueue queue = AppUtility.GetJobQueue();
            // NHibernate
            IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract();

            contract.Register();
            IJobConsumerContract job = contract.Queue.GetNext();

            Assert.IsNull(job);
            job_producer_contract.Submit();
            job = contract.Queue.GetNext();
            Assert.IsNotNull(job);

            job.Setup();

            IEnumerable <SimpleFile> files = job.GetSimulationInputFiles();

            Assert.AreEqual <int>(files.Count <SimpleFile>(), 2);
            foreach (var f in files)
            {
                Assert.IsNotNull(f);
                Assert.IsNotNull(f.name);
                Debug.WriteLine("File: " + f.name, this.GetType().Name);
                Assert.IsNotNull(f.content);

                Assert.IsTrue(f.content.Length > 0);

                if (f.name == "configuration")
                {
                    Assert.AreEqual <int>(f.content.Length, configContent.Length);
                }
                else if (f.name == "aspenfile")
                {
                    Assert.AreEqual <int>(f.content.Length, aspenfileContent.Length);
                }
                else
                {
                    Assert.Fail(String.Format("Unknown File {0}", f.name));
                }
            }
            //byte[] backup = job.GetSimulationBackup();
            //byte[] config = job.GetSimulationConfiguration();

            //Assert(,
            //IJobConsumerContract consumer = AppUtility.GetJobConsumerContract(id);
        }
        public void TestJobProducerConsumerContractsSetupFailUnRegisterConsumer()
        {
            // Create test Simulation
            TestSimulationProducerContract();

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Get("test");
            ISessionProducerContract    producer            = new AspenSessionProducerContract();
            Guid session_id = producer.Create();
            IJobProducerContract job_producer_contract = simulation_contract.NewJob(session_id, false, true);

            // consumers

            /*
             * IJobQueue queue = AppUtility.GetJobQueue();
             * IJobConsumerContract job = queue.GetNext();
             * Assert.IsNull(job);
             * job_producer_contract.Submit();
             * job = queue.GetNext();
             * Assert.IsNotNull(job);
             */
            // NHibernate
            IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract();

            Assert.IsNull(contract.Queue);
            contract.Register();
            Assert.IsNotNull(contract.Queue);
            Assert.IsNull(contract.Queue.GetNext());  // nothing on queue

            job_producer_contract.Submit();
            IJobConsumerContract job = contract.Queue.GetNext();

            Assert.IsNotNull(job);
            contract.UnRegister();

            job.Setup();

            SimpleFile backup = null;
            SimpleFile config = null;

            foreach (var f in job.GetSimulationInputFiles())
            {
                if (f.name == "configuration")
                {
                    config = f;
                }
                else
                {
                    backup = f;
                }
            }

            Assert.AreEqual(config.name, "configuration");

            String filename = SinterHelperFunctions
                              .getBackupFilename(System.Text.Encoding.ASCII.GetString(config.content));

            Assert.AreEqual(backup.name, "");

            //Assert(,
            //IJobConsumerContract consumer = AppUtility.GetJobConsumerContract(id);
        }
        public void TestExcelSpreadsheet1()
        {
            var app = ApplicationProducerContract.Get("Excel");

            Assert.IsNotNull(app);

            ISimulationProducerContract simulation_contract = AspenSimulationContract.Create("test", "Excel");

            Assert.IsNotNull(simulation_contract);

            byte[] data;
            using (var fstream = File.Open(@"exceltest.xlsm", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }
            Assert.IsTrue(data.Length > 0);
            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("spreadsheet", data, "plain/text");
            data = null;
            using (var fstream = File.Open("exceltest-sinter.json", FileMode.Open))
            {
                using (var ms = new MemoryStream())
                {
                    fstream.CopyTo(ms);
                    data = ms.ToArray();
                }
            }
            Assert.IsTrue(data.Length > 0);
            System.Threading.Thread.Sleep(10);
            simulation_contract.UpdateInput("configuration", data, "plain/text");
            System.Threading.Thread.Sleep(10);

            var guid = Guid.NewGuid();
            var job_producer_contract = simulation_contract.NewJob(guid, false, false);

            job_producer_contract.Process.Input = new Dictionary <string, Object>()
            {
            };
            job_producer_contract.Submit();
            System.Threading.Thread.Sleep(10);

            IConsumerRegistrationContract contract = Turbine.Consumer.AppUtility.GetConsumerRegistrationContract();

            Assert.IsNull(contract.Queue);
            contract.Register();

            Turbine.Consumer.Contract.SinterConsumer consumer = new Turbine.Consumer.Excel.ExcelSinterConsumer();

            //Utility.CheckBaseDirectory();

            consumer.Run();

            int jobID = job_producer_contract.Id;

            Debug.WriteLine("Job GUID" + guid.ToString(), GetType().Name);
            Dictionary <string, Object> json = null;

            using (Turbine.Data.TurbineModelContainer container = new Turbine.Data.TurbineModelContainer())
            {
                Turbine.Data.Job entity = container.Jobs.Single(s => s.Id == job_producer_contract.Id);
                json = Newtonsoft.Json.JsonConvert.DeserializeObject <Dictionary <string, Object> >(entity.Process.Output);
            }

            string output = Newtonsoft.Json.JsonConvert.SerializeObject(json);

            Debug.WriteLine("OUTPUT: " + output, GetType().Name);
        }