コード例 #1
0
        /// <summary>
        /// Performs a lookup for the givern security id if recursiveLookup is false and referenceLookup is true.
        /// </summary>
        /// <param name="securityIdDetails">The details of the security id for which to perform lookup.</param>
        /// <param name="lookupService">The lookup service to use</param>
        /// <returns>the security data.</returns>
        ///
        /// <exception cref="ServiceNotAvailableException">if the lookup service is not available.</exception>
        /// <exception cref="SecurityLookupException">if any error occurs when looking up the security data.</exception>
        /// <exception cref="SecurityDataCombiningException">if fail to combine the security data.</exception>
        private SecurityData PerformReferenceLookup(SecurityIdDetails securityIdDetails,
                                                    ISecurityLookupService lookupService)
        {
            // check cache to get the securityDataRecord
            SecurityDataRecord securityDataRecord = securityDataCache[securityIdDetails.Id] as SecurityDataRecord;

            if (securityDataRecord != null && securityDataRecord.IsLookedUp)
            {
                return(securityDataRecord.SecurityData);
            }
            else if (securityDataRecord == null)
            {
                return(PerformSimpleLookup(securityIdDetails, lookupService));
            }
            else
            {
                //Get the security data for lookupService
                SecurityData securityData = lookupService.Lookup(securityIdDetails);

                //Combine the record in cache with the one returned.
                securityData = securityDataCombiner.Combine(securityData, securityDataRecord.SecurityData);

                //Add or update cache with the cross-referenced security data
                AddUpdateCache(securityData);

                return(securityData);
            }
        }
コード例 #2
0
        /// <summary>
        /// Performs a lookup for the givern security id if recursiveLookup is true.
        /// </summary>
        /// <param name="securityIdDetails">The details of the security id for which to perform lookup.</param>
        /// <param name="lookupService">The lookup service to use</param>
        /// <returns>the security data.</returns>
        ///
        /// <exception cref="UnknownSecurityIdTypeException">
        /// if the type of the given security id is unknown.
        /// </exception>
        /// <exception cref="InvalidSecurityIdFormatException">if the format of security id is invalid.</exception>
        /// <exception cref="SecurityIdParsingException">if any other error occurs in parsing the id.</exception>
        /// <exception cref="ServiceNotAvailableException">if the lookup service is not available.</exception>
        /// <exception cref="SecurityLookupException">if any error occurs when looking up the security data.</exception>
        /// <exception cref="SecurityDataCombiningException">if fail to combine the security data.</exception>
        private SecurityData PerformRecursiveLookup(SecurityIdDetails securityIdDetails,
                                                    ISecurityLookupService lookupService)
        {
            // check cache to get the securityDataRecord
            SecurityDataRecord securityDataRecord = securityDataCache[securityIdDetails.Id] as SecurityDataRecord;

            if (securityDataRecord != null)
            {
                return(securityDataRecord.SecurityData);
            }
            else
            {
                //Get the security data for lookupService
                SecurityData securityData = lookupService.Lookup(securityIdDetails);

                //Add current id to cache. This prevents from infinite recursion due to cyclic references.
                securityDataCache[securityIdDetails.Id] = new SecurityDataRecord(securityData, true);

                //Create all possible cross-references and store in combinedData
                SecurityData combinedData = securityData;
                foreach (string refId in securityData.ReferenceIds)
                {
                    //Check the reference id in cache
                    SecurityDataRecord refRecord = securityDataCache[refId] as SecurityDataRecord;

                    SecurityData refData;
                    //if not present in cache, the load from lookup
                    if (refRecord == null)
                    {
                        //Get details of the reference id
                        SecurityIdDetails refDetails = securityIdParser.Parse(refId);

                        //get lookup service for the refId
                        ISecurityLookupService refLookupService = null;
                        securityLookupServices.TryGetValue(refDetails.Type, out refLookupService);

                        //No lookup service found for current type.
                        if (refLookupService == null)
                        {
                            throw new NoSuchSecurityLookupServiceException(
                                      "No lookup service exists for the security type: " + refDetails.Type);
                        }

                        refData = PerformRecursiveLookup(securityIdParser.Parse(refId), refLookupService);
                    }
                    else
                    {
                        refData = refRecord.SecurityData;
                    }

                    //Combine the current combined data with
                    combinedData = securityDataCombiner.Combine(combinedData, refData);
                }

                //All cross-refernced ids must now point to the same combinedData instance
                AddUpdateCache(combinedData);

                return(combinedData);
            }
        }
コード例 #3
0
        /// <summary>
        /// Adds all the reference ids of the combinedSecurityData to the cache.
        /// </summary>
        /// <param name="combinedSecurityData">The securityData to add to cache</param>
        private void AddUpdateCache(SecurityData combinedSecurityData)
        {
            //Add/update all combined references to cache.
            foreach (string refId in combinedSecurityData.ReferenceIds)
            {
                //Check the reference id in cache
                SecurityDataRecord refSecurityData = securityDataCache[refId] as SecurityDataRecord;

                //Update the referenceId in combinedSecurityData
                SecurityData newSecurityData = new SecurityData(refId, combinedSecurityData.CompanyName,
                                                                combinedSecurityData.ReferenceIds);

                //update cache for current id. Set looked up to true
                if (refId == combinedSecurityData.Id)
                {
                    securityDataCache[refId] = new SecurityDataRecord(newSecurityData, true);
                }
                //If the reference id is already in cache then update the cache
                else if (refSecurityData != null)
                {
                    securityDataCache[refId] = new SecurityDataRecord(newSecurityData, refSecurityData.IsLookedUp);
                }
                //if not in cache and not looked up yet.
                else if (refId != combinedSecurityData.Id)
                {
                    securityDataCache[refId] = new SecurityDataRecord(newSecurityData, false);
                }
            }
        }
コード例 #4
0
            /// <summary>
            /// <para>Constructor with the SecurityData instance and the looked-up flag.</para>
            /// </summary>
            ///
            /// <param name="securityData">the SecurityData instance to hold.</param>
            /// <param name="lookedUp">
            /// indicating the security data is looked up from the ISecurityLookupService directly or not.
            /// </param>
            /// <exception cref="ArgumentNullException">if securityData is null.</exception>
            public SecurityDataRecord(SecurityData securityData, bool lookedUp)
            {
                //Validate
                Helper.ValidateNotNull(securityData, "securityData");

                //Assign
                this.securityData = securityData;
                this.lookedUp     = lookedUp;
            }
コード例 #5
0
        public void TestConstructor1()
        {
            sd = new SecurityData("A", "Name");

            Assert.AreEqual(UnitTestHelper.GetPrivateFieldValue(sd, "id"), "A", "Wrong constructor implementation.");
            Assert.AreEqual(UnitTestHelper.GetPrivateFieldValue(sd, "companyName"), "Name",
                            "Wrong constructor implementation.");
            Assert.IsNotNull(UnitTestHelper.GetPrivateFieldValue(sd, "referenceIds"),
                             "Wrong constructor implementation.");
        }
コード例 #6
0
        public void TestLookupReference3()
        {
            //Lookup using X
            SecurityData securityData = fsm.Lookup("X");

            //Returned data must have reference ids X,Y
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "X", "Y" }),
                          "Wrong Lookup implementation.");
        }
コード例 #7
0
        public void TestLookupRecursive2()
        {
            fsm = new FinancialSecurityManager(securityLookupServices, combiner, true, false, cache);

            //Lookup using X
            SecurityData securityData = fsm.Lookup("X");

            //Returned data must have reference ids X,Y
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "X", "Y" }),
                          "Wrong Lookup implementation.");
        }
コード例 #8
0
ファイル: UnitTestHelper.cs プロジェクト: kurtrips/tc
        /// <summary>
        /// Checks the expected values of the lookedUp, Id and ReferenceIds for an entry in cache.
        /// </summary>
        /// <param name="cache">The cache instance</param>
        /// <param name="id">The expected id.</param>
        /// <param name="refIds">The expected reference ids</param>
        /// <param name="lookedUp">The expected value of lookedUp</param>
        public static void CheckSecurityDataRecord(ICache cache, string id, string[] refIds, bool lookedUp)
        {
            //Get the SecurityDataRecord object from cache for the given id
            object rec = cache[id];

            //Lookedup variable must be correct
            Assert.AreEqual(lookedUp, (bool)GetPrivateFieldValue(rec, "lookedUp"), "lookedUp variable must be correct");

            SecurityData secData = (SecurityData)GetPrivateFieldValue(rec, "securityData");

            Assert.IsTrue(AreReferenceIdsEqual(secData.ReferenceIds, refIds), "Reference ids differ.");
            Assert.AreEqual(secData.Id, id, "Id must be equal.");
        }
コード例 #9
0
        public void TestLookupReference5()
        {
            //Lookup using A first
            TestLookupReference1();

            //Now lookup using A
            SecurityData securityData = fsm.Lookup("A");

            //Both should be pointing to the same object
            ICache cache = (ICache)UnitTestHelper.GetPrivateFieldValue(fsm, "securityDataCache");

            Assert.AreEqual(UnitTestHelper.GetPrivateFieldValue(cache["A"], "securityData"), securityData,
                            "Wrong Lookup implementation.");
        }
コード例 #10
0
        public void TestLookupReference1()
        {
            //Lookup using A
            SecurityData securityData = fsm.Lookup("A");

            //Returned data must have reference ids A,B,C
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "A", "B", "C" }),
                          "Wrong Lookup implementation.");

            ICache cache = (ICache)UnitTestHelper.GetPrivateFieldValue(fsm, "securityDataCache");

            UnitTestHelper.CheckSecurityDataRecord(cache, "A", new string[] { "A", "B", "C" }, true);
            UnitTestHelper.CheckSecurityDataRecord(cache, "B", new string[] { "A", "B", "C" }, false);
            UnitTestHelper.CheckSecurityDataRecord(cache, "C", new string[] { "A", "B", "C" }, false);
        }
コード例 #11
0
        public void TestLookupSimple1()
        {
            fsm = new FinancialSecurityManager(securityLookupServices, combiner, false, false, cache);

            //Lookup using A
            SecurityData securityData = fsm.Lookup("A");

            //Returned data must have reference ids A,B,C
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "A", "B", "C" }),
                          "Wrong Lookup implementation.");

            ICache cacheField = (ICache)UnitTestHelper.GetPrivateFieldValue(fsm, "securityDataCache");

            UnitTestHelper.CheckSecurityDataRecord(cacheField, "A", new string[] { "A", "B", "C" }, true);
            UnitTestHelper.CheckSecurityDataRecord(cacheField, "B", new string[] { "A", "B", "C" }, false);
            UnitTestHelper.CheckSecurityDataRecord(cacheField, "C", new string[] { "A", "B", "C" }, false);
        }
コード例 #12
0
        public void TestLookupRecursive3()
        {
            fsm = new FinancialSecurityManager(securityLookupServices, combiner, true, false, cache);

            //Lookup using X
            fsm.Lookup("X");

            //Lookup again using X
            SecurityData sdx = fsm.Lookup("X");

            //Lookup using Y
            SecurityData sdy = fsm.Lookup("Y");

            //The returned securityData for X must be the same one as that in cache.
            ICache cacheField = (ICache)UnitTestHelper.GetPrivateFieldValue(fsm, "securityDataCache");

            Assert.AreEqual(UnitTestHelper.GetPrivateFieldValue(cacheField["X"], "securityData"), sdx,
                            "Wrong Lookup implementation.");
            Assert.AreEqual(UnitTestHelper.GetPrivateFieldValue(cacheField["Y"], "securityData"), sdy,
                            "Wrong Lookup implementation.");
        }
コード例 #13
0
        /// <summary>
        /// Performs a lookup for the givern security id if recursiveLookup and referenceLookup are both false.
        /// </summary>
        /// <param name="securityIdDetails">The details of the security id for which to perform lookup.</param>
        /// <param name="lookupService">The lookup service to use</param>
        /// <returns>the security data.</returns>
        ///
        /// <exception cref="ServiceNotAvailableException">if the lookup service is not available.</exception>
        /// <exception cref="SecurityLookupException">if any error occurs when looking up the security data.</exception>
        /// <exception cref="SecurityDataCombiningException">if fail to combine the security data.</exception>
        private SecurityData PerformSimpleLookup(SecurityIdDetails securityIdDetails,
                                                 ISecurityLookupService lookupService)
        {
            // check cache to get the securityDataRecord
            SecurityDataRecord securityDataRecord = securityDataCache[securityIdDetails.Id] as SecurityDataRecord;

            if (securityDataRecord != null)
            {
                return(securityDataRecord.SecurityData);
            }
            else
            {
                //If not in cache then perform lookup
                SecurityData securityData = lookupService.Lookup(securityIdDetails);

                //Combine with itself. Combining a securityData instance with itself has the effect of
                //adding the securityID to the referenceIds
                SecurityData combinedData = securityDataCombiner.Combine(securityData, securityData);

                //Create all possible cross references
                foreach (string refId in securityData.ReferenceIds)
                {
                    //Check the reference id in cache
                    SecurityDataRecord refSecurityData = securityDataCache[refId] as SecurityDataRecord;

                    if (refSecurityData != null)
                    {
                        combinedData = securityDataCombiner.Combine(securityData, refSecurityData.SecurityData);
                    }
                }

                //Add or update cache with the cross-referenced security data
                AddUpdateCache(combinedData);

                return(combinedData);
            }
        }
コード例 #14
0
 public void TestConstructorFail12()
 {
     sd = new SecurityData("  ", "Name");
 }
コード例 #15
0
 public void TestConstructorFail5()
 {
     sd = new SecurityData("A", "as", null);
 }
コード例 #16
0
 public void TestConstructorFail7()
 {
     string[] refIds = new string[] { "a", "      " };
     sd = new SecurityData("A", "as", refIds);
 }
コード例 #17
0
 public void SetUp()
 {
     refIds = new string[] { "C", "B" };
     sd     = new SecurityData("A", "Name", refIds);
 }
コード例 #18
0
 public void TearDown()
 {
     sd     = null;
     refIds = null;
 }
コード例 #19
0
 public void TestConstructorFail11()
 {
     sd = new SecurityData(null, "Name");
 }
コード例 #20
0
 public void SetUp()
 {
     sd  = new SecurityData("A", "B");
     sdr = new SecurityDataRecord(sd, true);
 }
コード例 #21
0
 public void TearDown()
 {
     sd  = null;
     sdr = null;
 }
コード例 #22
0
 public void TestConstructorFail14()
 {
     sd = new SecurityData("A", "  ");
 }
コード例 #23
0
ファイル: Demo.cs プロジェクト: kurtrips/tc
        public void DemoLookup()
        {
            //////////////REFERENCE LOOKUP DEMO
            fsm = new FinancialSecurityManager(securityLookupServices, combiner, false, true, cache);

            //Lookup using A. Returns A,B,C
            SecurityData securityData = fsm.Lookup("A");

            //Returned data must have reference ids A,B,C
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "A", "B", "C" }),
                          "Wrong Lookup implementation.");

            //Now lookup using B
            securityData = fsm.Lookup("B");

            //Returned data must have reference ids A,B,C,D
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "A", "B", "C", "D" }),
                          "Wrong Lookup implementation.");



            //////////////SIMPLE LOOKUP DEMO
            cache = new SimpleCache();
            fsm   = new FinancialSecurityManager(securityLookupServices, combiner, false, false, cache);

            //Lookup using A. Returns A,B,C
            securityData = fsm.Lookup("A");

            //Returned data must have reference ids A,B,C
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "A", "B", "C" }),
                          "Wrong Lookup implementation.");

            //Now lookup using B
            securityData = fsm.Lookup("B");

            //Returned data must have reference ids A,B,C
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "A", "B", "C" }),
                          "Wrong Lookup implementation.");



            //////////////RECURSIVE LOOKUP DEMO
            cache = new SimpleCache();
            fsm   = new FinancialSecurityManager(securityLookupServices, combiner, true, false, cache);

            //Lookup using A. Returns A,B,C,D
            securityData = fsm.Lookup("A");

            //Returned data must have reference ids A,B,C,D
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "A", "B", "C", "D" }),
                          "Wrong Lookup implementation.");

            //Now lookup using B
            securityData = fsm.Lookup("B");

            //Returned data must have reference ids A,B,C,D
            Assert.IsTrue(UnitTestHelper.AreReferenceIdsEqual(
                              securityData.ReferenceIds, new string[] { "A", "B", "C", "D" }),
                          "Wrong Lookup implementation.");
        }