コード例 #1
0
        internal static ICollectionBase ConvertJsonToCollection(string jsonCollection)
        {
            string collectionTypeName = null;

            try
            {
                dynamic collection = JsonConvert.DeserializeObject(jsonCollection);

                // This is not working as expected when it has child members
                //ICollectionBase collection = JsonConvert.DeserializeObject<HomeCollection>(jsonCollection);

                string collectionName = collection.CollectionName;
                collectionTypeName = collection.CollectionType;
                Type            collectionType = CollectableBaseFactory.GetTypeFromFullName(collectionTypeName);
                ICollectionBase newCollection  = new HomeCollection(collectionName, collectionType);

                var collectables = collection.Collectables;
                foreach (var c in collectables)
                {
                    string jsonCollectable = c.ToString();
                    // add custom try/catch??
                    ICollectableBase collectable = GetCollectableFromJson(jsonCollectable, collectionType);

                    newCollection.AddToCollection(collectable);
                }
                return(newCollection);
            }
            catch (Exception ex)
            {
                throw new CollectionParseException($"Unable to parse Json into a collection object.  Type={collectionTypeName}, Json={jsonCollection}", ex);
            }
        }
コード例 #2
0
        private ICollectionBase GetTestCollection(string collectionName, Type collectableType, int numberOfCollectables, int numberOfItemsPerCollectable = 0)
        {
            ICollectableBase collectable    = null;
            ICollectableItem item           = null;
            Type             collectionType = collectableType;
            ICollectionBase  testCollection = new HomeCollection(collectionName, collectionType);

            for (int i = 0; i < numberOfCollectables; i++)
            {
                if (collectionType == CollectableBaseFactory.BookType)
                {
                    collectable = GetTestBookBase(i);
                }
                else if (collectionType == CollectableBaseFactory.StampType)
                {
                    collectable = GetTestStampBase(i);
                }
                testCollection.AddToCollection(collectable);

                for (int j = 0; j < numberOfItemsPerCollectable; j++)
                {
                    if (collectionType == CollectableBaseFactory.BookType)
                    {
                        item = GetTestBookItem(i);
                    }
                    else if (collectionType == CollectableBaseFactory.StampType)
                    {
                        item = GetTestStampItem(i);
                    }
                    collectable.AddItem(item);
                }
            }
            return(testCollection);
        }
コード例 #3
0
        public bool IsSame(ICollectableBase itemToCompare, bool useTitleAuthor)
        {
            if (itemToCompare == null)
            {
                throw new CollectableException("Cannot compare to a null item");
            }
            Type itemType = itemToCompare.CollectableType;

            if (itemType != this.CollectableType)
            {
                throw new CollectableException($"Invalid type {itemType}, expected type {this.CollectableType}");
            }
            BookBase bookDef = (BookBase)itemToCompare;

            if (!useTitleAuthor)
            {
                if (ISBN != bookDef.ISBN)
                {
                    return(false);
                }
                return(true);
            }
            else
            {
                if (Title != bookDef.Title)
                {
                    return(false);
                }
                if (Author != bookDef.Author)
                {
                    return(false);
                }
                return(true);
            }
        }
コード例 #4
0
        public void controller_initialized_with_null_collectable_base_object_fails()
        {
            ICollectableBase nullBase = null;

            controller = new CollectableBaseController(nullBase);

            Assert.IsFalse(true, "Expected the test to fail when initialized with a null object");
        }
コード例 #5
0
 public CollectableBaseController(ICollectableBase collectableBase)
 {
     if (collectableBase == null)
     {
         throw new CollectableException("Controller must be initialized with a collectable base object");
     }
     _collectableBase = collectableBase;
 }
コード例 #6
0
        public void create_new_collectable_from_interface_type_throws_exception()
        {
            Type invalidType = typeof(IStampBase);

            ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(invalidType);

            Assert.IsFalse(true, "Expected test to fail if passed an interface instead of valid collectable base type");
        }
コード例 #7
0
        public void create_new_collectable_from_empty_string_throws_exception()
        {
            string invalidTypeName = "";

            ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(invalidTypeName);

            Assert.IsFalse(true, "Expected test to fail if passed a null type");
        }
コード例 #8
0
        public void create_new_stamp_from_factory_returns_stampbase_type()
        {
            Type stampType = CollectableBaseFactory.StampType;

            ICollectableBase newStamp = CollectableBaseFactory.CreateCollectableBase(stampType);

            Assert.IsTrue(stampType == newStamp.CollectableType);
        }
コード例 #9
0
        public void createandaddcollectableitem_null_collectable_throws_exception()
        {
            ICollectableBase nullCollectable = null;

            CollectableBaseFactory.CreateAndAddCollectableItem(nullCollectable);

            Assert.Fail("Expected CreateAndAddCollectableItem to fail when passed a null collectable");
        }
コード例 #10
0
        public void create_new_factory_instance_from_valid_collectable_base_name_case_insensitive_succeeds()
        {
            string validTypeName = "BOOkBaSe";   // implements ICollectableBase

            ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(validTypeName);

            Assert.AreEqual(validTypeName.ToUpper(), newItem.CollectableType.Name.ToUpper(), "Expected to get instance of a book base type");
        }
コード例 #11
0
        public void create_new_collectable_from_null_type_throws_exception()
        {
            Type invalidType = null;

            ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(invalidType);

            Assert.IsFalse(true, "Expected test to fail if passed a null type");
        }
コード例 #12
0
        public void create_new_collectable_from_interface_type_string_throws_exception()
        {
            string validTypeName = "ICollectableBase";

            ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(validTypeName);

            Assert.IsFalse(true, "Expected test to fail if passed an interface type name");
        }
コード例 #13
0
        public void create_new_factory_instance_from_valid_collectable_base_succeeds()
        {
            foreach (Type validType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectableBase newCollectable = CollectableBaseFactory.CreateCollectableBase(validType);

                Assert.AreEqual(validType, newCollectable.CollectableType, $"Expected to get instance of a {validType.Name} base type");
            }
        }
コード例 #14
0
        public void createandaddcollectableitem_valid_type_returns_new_item_with_type_set()
        {
            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectableBase collectable = CollectableBaseFactory.CreateCollectableBase(collectableType);
                ICollectableItem newItem     = CollectableBaseFactory.CreateAndAddCollectableItem(collectable);

                Assert.AreEqual(collectableType, newItem.CollectableType);
            }
        }
コード例 #15
0
        public void add_null_collectable_to_collection_throws_exception()
        {
            ICollectionBase testCollection = new HomeCollection("initial", CollectableBaseFactory.CollectableTypes[0]);

            ICollectableBase nullCollectable = null;

            testCollection.AddToCollection(nullCollectable);

            Assert.IsFalse(true, "Expected that an exception is thrown when a null value is added to the collection");
        }
コード例 #16
0
 public void RemoveFromCollection(ICollectableBase collectableToRemove)
 {
     try
     {
         _collection.Remove(collectableToRemove);
     } catch (Exception ex)
     {
         throw new CollectionException("Error removing item from collection", ex);
     }
 }
コード例 #17
0
        public void create_new_factory_instance_from_valid_collectable_base_name_succeeds()
        {
            foreach (Type validType in CollectableBaseFactory.CollectableTypes)
            {
                string validTypeName = validType.Name;   // implements ICollectableBase

                ICollectableBase newItem = CollectableBaseFactory.CreateCollectableBase(validTypeName);

                Assert.AreEqual(validTypeName, newItem.CollectableType.Name, $"Expected to get instance of a {validTypeName} base type");
            }
        }
コード例 #18
0
 public void AddToCollection(ICollectableBase collectableToAdd)
 {
     try
     {
         _homeCollection.AddToCollection(collectableToAdd);
     }
     catch (Exception ex)
     {
         throw new CollectionException("Error adding item to collection", ex);
     }
 }
コード例 #19
0
        public void createandaddcollectableitem_valid_type_adds_new_item_to_collectable()
        {
            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectableBase collectable     = CollectableBaseFactory.CreateCollectableBase(collectableType);
                ICollectableItem newItem         = CollectableBaseFactory.CreateAndAddCollectableItem(collectable);
                ICollectableItem fromCollectable = collectable.ItemInstances[0];

                Assert.AreEqual(newItem, fromCollectable);
            }
        }
コード例 #20
0
        public void getitems_returns_empty_list_when_there_are_no_members()
        {
            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectableBase collectable = GetTestBase(collectableType, 0);

                IList <ICollectableItem> items = collectable.ItemInstances;

                Assert.AreEqual(0, items.Count);
            }
        }
コード例 #21
0
        public static ICollectableItem CreateAndAddCollectableItem(ICollectableBase collectable)
        {
            if (collectable == null)
            {
                throw new CollectableException("Collectable cannot be null when adding an instance of it");
            }
            ICollectableItem newItem = CreateCollectableItem(collectable.CollectableType);

            collectable.AddItem(newItem);
            return(newItem);
        }
コード例 #22
0
        public void controller_collectabletype_returns_initial_collectable_base_type()
        {
            Type             objType         = typeof(ICollectableBase);
            ICollectableBase collectableBase = mockCollectableBase.Object;

            mockCollectableBase.Setup(b => b.CollectableType).Returns(objType);

            controller = new CollectableBaseController(collectableBase);
            Type objTestType = controller.CollectableType;

            Assert.AreEqual(objType, objTestType);
        }
コード例 #23
0
        public void add_valid_type_to_collection_succeeds()
        {
            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase  collection  = new HomeCollection("initial", collectableType);
                ICollectableBase collectable = GetMockCollectableObject(collectableType);

                collection.AddToCollection(collectable);

                Assert.AreEqual(1, collection.Collectables.Count);
            }
        }
コード例 #24
0
        public void additem_inserts_new_item_into_empty_list()
        {
            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectableBase        collectable = GetTestBase(collectableType, 0);
                Mock <ICollectableItem> mockItem    = GetMockItem(collectableType);

                collectable.AddItem(mockItem.Object);

                IList <ICollectableItem> list = collectable.ItemInstances;
                Assert.AreEqual(1, list.Count);
            }
        }
コード例 #25
0
        private ICollectionBase GetMockCollection(int N, Type collectableType)
        {
            Type            collectionType = collectableType;
            ICollectionBase testCollection = new HomeCollection("initial", collectionType);

            for (int i = 0; i < N; i++)
            {
                ICollectableBase collectable = GetMockCollectableObject(collectableType);
                testCollection.AddToCollection(collectable);
            }

            return(testCollection);
        }
コード例 #26
0
        public void clearitems_from_empty_list_success()
        {
            int N = 0;

            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectableBase collectable = GetTestBase(collectableType, N);

                collectable.ClearItems();

                IList <ICollectableItem> list = collectable.ItemInstances;
                Assert.AreEqual(0, list.Count);
            }
        }
コード例 #27
0
        public void controller_initialized_with_collectable_base_object_returns_controller_instance()
        {
            try
            {
                ICollectableBase collectableBase = mockCollectableBase.Object;

                controller = new CollectableBaseController(collectableBase);

                Assert.IsNotNull(controller);
            }
            catch
            {
                Assert.IsFalse(true, "Test should not fail when initialized with an object");
            }
        }
コード例 #28
0
        public void removeitem_from_existing_list_success()
        {
            int N = 3;

            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectableBase collectable = GetTestBase(collectableType, N);
                ICollectableItem mockItem    = collectable.ItemInstances[N - 1];

                collectable.RemoveItem(mockItem);

                IList <ICollectableItem> list = collectable.ItemInstances;
                Assert.AreEqual(N - 1, list.Count);
            }
        }
コード例 #29
0
 public void additem_insert_into_empty_list_fails_for_null()
 {
     foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
     {
         ICollectableBase collectable = GetTestBase(collectableType, 0);
         try
         {
             collectable.AddItem(null);
             Assert.IsFalse(true, "Expected to fail adding a null member");
         }
         catch (CollectableException)
         {
             Assert.IsTrue(true);
         }
     }
 }
コード例 #30
0
        public void getcollectablefromjson_deserialize_bad_format_throws_exception()
        {
            string jsonCollectable = "invalid JSON";

            foreach (Type collectableType in CollectableBaseFactory.CollectableTypes)
            {
                bool fail = false;
                try
                {
                    ICollectableBase newCollectable = HomeCollectionRepository.GetCollectableFromJson(jsonCollectable, collectableType);
                }
                catch (CollectableParseException)
                {
                    fail = true;
                    Assert.IsTrue(fail, "Expected exception to be thrown when JSON is invalid and cannot be parsed to an object");
                }
            }
        }