public void TestExportChannels()
        {           
            string policyCatalogueFilename = m_testPath + "SamplePolicyCatalogue.xml";
            string policyLanguageFilename = m_testPath + "SampleLanguageBase.xml";
            string policyFilename = m_testPath + "SamplePolicy.xml";

            XmlPolicyLanguageStore languageStore = XmlPolicyLanguageStore.Instance;
            languageStore.AddLanguage(System.IO.File.ReadAllText(policyLanguageFilename));
            XMLPolicyCatalogueStore catalogueStore = XMLPolicyCatalogueStore.Instance;
            catalogueStore.Reset();
            catalogueStore.AddPolicyCatalogue(System.IO.File.ReadAllText(policyCatalogueFilename));

            XmlStore store = new XmlStore(System.IO.File.ReadAllText(policyFilename));
            IPolicyObjectCollection<IPolicySet> policySet = store.Reader.PolicySets;
            IPolicyObjectCollection<IPolicy> policies = store.Reader.Policies(policySet[0]);
            IPolicy policy = policies[0];
            IPolicySet ps = policySet[0];

            NxPolicyStore policyStore = new NxPolicyStore();
            policyStore.ResourceManager = ResourcesCache.GetResources();
            ps.Export(policyStore);

            string nxstore = policyStore.XMLRepresentation;
            CompiledPolicySetExtractor pse = new CompiledPolicySetExtractor(nxstore);
            List<string> channelNames = pse.GetChannelNames();
            foreach (string channel in channelNames)
            {
                Assert.AreNotEqual("", pse.GetRules(channel));
                Assert.AreNotEqual("", pse.GetObjects(channel));
            }
        }
        private void CompileNxBrePolicySet(IPolicySetVersionCache version, IPolicySet policySet, string target)
        {
            if ((null == version) || (null == policySet) || (string.IsNullOrEmpty(target)))
                return;

            NxPolicyStore nxBreStore = new NxPolicyStore();
            nxBreStore.ResourceManager = ResourcesCache.GetResources();
            policySet.Export(nxBreStore);

            CompiledPolicySetExtractor cpse = new CompiledPolicySetExtractor(nxBreStore.XMLRepresentation);
            List<string> channels = cpse.GetChannelNames();

            foreach (string channel in channels)
            {
                string objects = cpse.GetObjects(channel);
                string rules = cpse.GetRules(channel);

                ICompiledPolicySetCache cps = version.GetCompiledPolicySet(channel, target);
                if (cps == null)
                {
                    cps = version.NewCompiledPolicySet(channel, target, rules, objects);
                }
                else
                {
                    cps.Content = rules;
                    cps.ObjectReferences = objects;
                }
                    
                SaveLanguages(cpse.GetLanguages(), cps);
            }
        }
        public void TestExportChannel()
        {
            string policyCatalogueFilename = m_testPath + "SamplePolicyCatalogueWithSingleChannel.xml";
            string policyLanguageFilename = m_testPath + "SampleLanguageBase.xml";
            string policyFilename = m_testPath + "SamplePolicyWithSingleChannel.xml";

            XmlPolicyLanguageStore languageStore = XmlPolicyLanguageStore.Instance;
            Guid languageId = languageStore.AddLanguage(System.IO.File.ReadAllText(policyLanguageFilename));
            XMLPolicyCatalogueStore catalogueStore = XMLPolicyCatalogueStore.Instance;
            catalogueStore.Reset();

            catalogueStore.AddPolicyCatalogue(System.IO.File.ReadAllText(policyCatalogueFilename));

            XmlStore store = new XmlStore(System.IO.File.ReadAllText(policyFilename));
            IPolicyObjectCollection<IPolicySet> policySet = store.Reader.PolicySets;
            IPolicyObjectCollection<IPolicy> policies = store.Reader.Policies(policySet[0]);
            IPolicy policy = policies[0];
            IPolicySet ps = policySet[0];

            NxPolicyStore policyStore = new NxPolicyStore();
            policyStore.ResourceManager = ResourcesCache.GetResources();
            ps.Export(policyStore);

            string nxstore = policyStore.XMLRepresentation;
            string emailrules = System.IO.Path.GetTempFileName();
            string emailobjects = System.IO.Path.GetTempFileName();

            CompiledPolicySetExtractor pse = new CompiledPolicySetExtractor(nxstore);
            System.IO.File.WriteAllText(emailrules, pse.GetRules("SMTP"));
            System.IO.File.WriteAllText(emailobjects, pse.GetObjects("SMTP"));

            pse.GetLanguages();
      
            XmlDocument rulesXml = new XmlDocument();
            rulesXml.Load(emailrules);

            string xpath = string.Format("/xBusinessRules/Set");
            XmlNodeList nodes = rulesXml.SelectNodes(xpath);

            Assert.AreEqual("Policies", nodes[0].Attributes["id"].Value);
            Assert.AreEqual("w-C1C0D5EA-5B82-4607-AE41-D52739AA6AB1", nodes[1].Attributes["id"].Value);
            Assert.AreEqual("w-c7d62e10-d889-4ef5-af3d-823c82c230ca", nodes[2].Attributes["id"].Value);
            Assert.AreEqual("w-af6e5d89-0c6f-4b10-9a6c-658d13cd3ea8", nodes[4].Attributes["id"].Value);

            System.IO.File.Delete(emailrules);
            System.IO.File.Delete(emailobjects);
        }
        public void TestWriteActionExceptionHandlerObjects()
        {
            string path = Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\Projects\Hygiene\src\NxBrePolicyStore.Tests\TestFiles\");
            string catalogue = path + "TestActionExceptionHandler.Catalogue.xml";
            string language = path + "TestActionExceptionHandler.Language.xml";
            string policyset = path + "TestActionExceptionHandler.PolicySet.xml";

            XmlPolicyLanguageStore languageStore = XmlPolicyLanguageStore.Instance;
            Guid languageId = languageStore.AddLanguage(System.IO.File.ReadAllText(language));
            XMLPolicyCatalogueStore catalogueStore = XMLPolicyCatalogueStore.Instance;
            catalogueStore.Reset();

            catalogueStore.AddPolicyCatalogue(System.IO.File.ReadAllText(catalogue));

            XmlStore store = new XmlStore(System.IO.File.ReadAllText(policyset));
            IPolicyObjectCollection<IPolicySet> policySet = store.Reader.PolicySets;
            IPolicyObjectCollection<IPolicy> policies = store.Reader.Policies(policySet[0]);
            IPolicy policy = policies[0];
            IPolicySet ps = policySet[0];

            NxPolicyStore policyStore = new NxPolicyStore();
            policyStore.ResourceManager = ResourcesCache.GetResources();
            List<Guid> policiesToExport = new List<Guid>();

            policiesToExport.Add(new Guid("{C0F6D4BB-CBF1-41FC-8A28-616D6FC1DC73}"));
            ps.Export(policyStore, policiesToExport);

            string nxstore = policyStore.XMLRepresentation;
            CompiledPolicySetExtractor pse = new CompiledPolicySetExtractor(nxstore);
            string rules = pse.GetRules("SMTP");
            string objects = pse.GetObjects("SMTP");

            XmlDocument objectsxml = new XmlDocument();
            objectsxml.LoadXml(objects);

            XmlNodeList actions = objectsxml.SelectNodes("/PolicyObjects/Actions/*");
            Assert.AreEqual(2, actions.Count, "Hint - Check the actions are registered with the policy designer?");

            XmlNode object1 = actions[0];
            Assert.AreEqual("CustomAction2.dll", object1.Attributes["assembly"].Value);
            Assert.AreEqual("Workshare.Policy.Actions.PDFUserAction", object1.Attributes["class"].Value);
            Assert.AreEqual("1", object1.Attributes["precedence"].Value);

            XmlNode object2 = actions[1];
            Assert.AreEqual("CustomAction.dll", object2.Attributes["assembly"].Value);
            Assert.AreEqual("Workshare.Policy.Actions.CleanUserAction", object2.Attributes["class"].Value);
            Assert.AreEqual("1", object2.Attributes["precedence"].Value);
            Assert.AreEqual("False", object2.Attributes["allowoverride"].Value);
        }
        public void TestWriteActionExceptionHandlerRules()
        {
            string path = Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\Projects\Hygiene\src\NxBrePolicyStore.Tests\TestFiles\");
            string catalogue = path + "TestActionExceptionHandler.Catalogue.xml";
            string language = path + "TestActionExceptionHandler.Language.xml";
            string policyset = path + "TestActionExceptionHandler.PolicySet.xml";

            XmlPolicyLanguageStore languageStore = XmlPolicyLanguageStore.Instance;
            Guid languageId = languageStore.AddLanguage(System.IO.File.ReadAllText(language));
            XMLPolicyCatalogueStore catalogueStore = XMLPolicyCatalogueStore.Instance;
            catalogueStore.Reset();

            catalogueStore.AddPolicyCatalogue(System.IO.File.ReadAllText(catalogue));

            XmlStore store = new XmlStore(System.IO.File.ReadAllText(policyset));
            IPolicyObjectCollection<IPolicySet> policySet = store.Reader.PolicySets;
            IPolicyObjectCollection<IPolicy> policies = store.Reader.Policies(policySet[0]);
            IPolicy policy = policies[0];
            IPolicySet ps = policySet[0];

            NxPolicyStore policyStore = new NxPolicyStore();
            policyStore.ResourceManager = ResourcesCache.GetResources();
            List<Guid> policiesToExport = new List<Guid>();

            policiesToExport.Add(new Guid("{C0F6D4BB-CBF1-41FC-8A28-616D6FC1DC73}"));
            ps.Export(policyStore, policiesToExport);

            string nxstore = policyStore.XMLRepresentation;
            CompiledPolicySetExtractor pse = new CompiledPolicySetExtractor(nxstore);
            string rules = pse.GetRules("SMTP");
            string objects = pse.GetObjects("SMTP");

            XmlDocument rulesxml = new XmlDocument();
            rulesxml.LoadXml(rules);

            XmlNode exceptionHandler = rulesxml.SelectSingleNode("/xBusinessRules/Set[@id='ExceptionHandler']");
            XmlNodeList evaluateNodes = exceptionHandler.SelectNodes("Evaluate");

            Assert.AreEqual(2, evaluateNodes.Count, "There should have been two evaluate nodes");

            // Check the values of the object ref and override nodes (the 4th and 5th respectively).
            // The 1st and third nodes are the name and supported file types.
            XmlNode objectRef1 = evaluateNodes[0].ChildNodes[3];
            Assert.AreEqual("72551fd1-d46d-4af6-8da3-76b5bce01fd8", objectRef1.Attributes["value"].Value);
            XmlNode override1 = evaluateNodes[0].ChildNodes[4];
            Assert.AreEqual("True", override1.Attributes["value"].Value);
            
            XmlNode objectRef2 = evaluateNodes[1].ChildNodes[3];
            Assert.AreEqual("26bcfa7e-3605-4ee7-acc6-0afb4d5ebb71", objectRef2.Attributes["value"].Value);
            XmlNode override2 = evaluateNodes[1].ChildNodes[4];
            Assert.AreEqual("False", override2.Attributes["value"].Value);
        }
        public void TestExportMultiplePolicies()
        {
            string policyCatalogueFilename = m_testPath + "SamplePolicyCatalogue.xml";
            string policyLanguageFilename = m_testPath + "SampleLanguageBase.xml";
            string policyFilename = m_testPath + "SamplePolicy.xml";

            XmlPolicyLanguageStore languageStore = XmlPolicyLanguageStore.Instance;
            Guid languageId = languageStore.AddLanguage(System.IO.File.ReadAllText(policyLanguageFilename));
            XMLPolicyCatalogueStore catalogueStore = XMLPolicyCatalogueStore.Instance;
            catalogueStore.Reset();

            catalogueStore.AddPolicyCatalogue(System.IO.File.ReadAllText(policyCatalogueFilename));

            XmlStore store = new XmlStore(System.IO.File.ReadAllText(policyFilename));
            IPolicyObjectCollection<IPolicySet> policySet = store.Reader.PolicySets;
            IPolicyObjectCollection<IPolicy> policies = store.Reader.Policies(policySet[0]);
            IPolicy policy = policies[0];
            IPolicySet ps = policySet[0];

            NxPolicyStore policyStore = new NxPolicyStore();
            policyStore.ResourceManager = ResourcesCache.GetResources();
            List<Guid> policiesToExport = new List<Guid>();
            policiesToExport.Add(new Guid("{C7D62E10-D889-4EF5-AF3D-823C82C230CA}"));
            policiesToExport.Add(new Guid("{56C991B8-953D-46AD-B9E9-0CFB08F52C23}"));
            ps.Export(policyStore, policiesToExport);

            string nxstore = policyStore.XMLRepresentation;

            string emailrules = System.IO.Path.GetTempFileName();
            string emailobjects = System.IO.Path.GetTempFileName();
            string language = System.IO.Path.GetTempFileName();

            CompiledPolicySetExtractor pse = new CompiledPolicySetExtractor(nxstore);
            System.IO.File.WriteAllText(emailrules, pse.GetRules("SMTP"));
            System.IO.File.WriteAllText(emailobjects, pse.GetObjects("SMTP"));

            pse.GetLanguages();

   
            XmlDocument rulesXml = new XmlDocument();
            rulesXml.Load(emailrules);

            string xpath = string.Format("/xBusinessRules/Set[@id='Policies']/InvokeSet");
            XmlNodeList nodes = rulesXml.SelectNodes(xpath);

            Assert.AreEqual(1, nodes.Count);
            Assert.AreEqual("w-C1C0D5EA-5B82-4607-AE41-D52739AA6AB1", nodes[0].Attributes["id"].Value);

            xpath = string.Format("/xBusinessRules/Set[@id='w-C1C0D5EA-5B82-4607-AE41-D52739AA6AB1']");
            nodes = rulesXml.SelectNodes(xpath);

            Assert.AreEqual("w-C1C0D5EA-5B82-4607-AE41-D52739AA6AB1", nodes[0].Attributes["id"].Value);

            System.IO.File.Delete(emailrules);
            System.IO.File.Delete(emailobjects);
            System.IO.File.Delete(language);
        }