Ejemplo n.º 1
0
        public void converttojson_collection_with_collectables_and_instances_success()
        {
            int N = 3;
            int M = 2;

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase collection = GetTestCollection("initial", collectionType, N, M);

                string json = HomeCollectionRepository.ConvertCollectionToJson(collection);

                int countCollectionNameTag = CountOccurences(json, "CollectionName");
                int countCollectionTypeTag = CountOccurences(json, "CollectionType");
                int countDisplayNameTag    = CountOccurences(json, "DisplayName");
                int countDescriptionTag    = CountOccurences(json, "Description");
                int countItemInstanceTag   = CountOccurences(json, "ItemInstances");
                int countItemDetailsTag    = CountOccurences(json, "ItemDetails");
                int countIsFavoriteTag     = CountOccurences(json, "IsFavorite");

                Assert.AreEqual(1, countCollectionNameTag);
                Assert.AreEqual(1, countCollectionTypeTag);
                Assert.AreEqual(N, countDisplayNameTag);
                Assert.AreEqual(N, countDescriptionTag);
                Assert.AreEqual(N, countItemInstanceTag);
                Assert.AreEqual(N * M, countItemDetailsTag);
                Assert.AreEqual(N * M, countIsFavoriteTag);
            }
        }
Ejemplo n.º 2
0
        public void convertcollectiontojson_throws_exception_when_collection_is_null()
        {
            ICollectionBase collection = null;

            string json = HomeCollectionRepository.ConvertCollectionToJson(collection);

            Assert.Fail("Should have thrown exception when collection is null");
        }
Ejemplo n.º 3
0
        public void convertcollectiontojson_serializes_new_collection_successfully()
        {
            string          collectionName  = "test";
            Type            collectableType = CollectableBaseFactory.CollectableTypes[0];
            ICollectionBase collection      = new HomeCollection(collectionName, collectableType);
            string          expected        = @"{""CollectionName"":""test"",""CollectionType"":""HomeCollector.Models.BookBase, HomeCollector, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"",""Collectables"":[]}";

            string json = HomeCollectionRepository.ConvertCollectionToJson(collection);

            // This is a very fragile test.  Should look for expected tokens instead.
            Assert.AreEqual(expected, json);
        }
Ejemplo n.º 4
0
        public void savecollection_calls_writefile_with_serialize_json_set()
        {
            Mock <ICollectionBase>    mockCollection = new Mock <ICollectionBase>();
            IHomeCollectionRepository repo           = new HomeCollectionRepository(mockCollection.Object, _mockFileIO.Object);

            string path           = "filepath";
            string filename       = "filename";
            string jsonCollection = HomeCollectionRepository.ConvertCollectionToJson(mockCollection.Object);
            bool   overwrite      = false;

            repo.SaveCollection(path, filename, overwrite);

            _mockFileIO.Verify(r => r.WriteFile(It.IsAny <string>(), jsonCollection, It.IsAny <bool>()), Times.Once);
        }
Ejemplo n.º 5
0
        public void loadcollection_file_valid_json_content_returns_collection()
        {
            IHomeCollectionRepository repo = new HomeCollectionRepository(_mockFileIO.Object);

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase testCollection  = GetTestCollection("test collection", collectionType, 1);
                string          testFileContent = HomeCollectionRepository.ConvertCollectionToJson(testCollection);
                _mockFileIO.Setup(i => i.ReadFile(It.IsAny <string>())).Returns(testFileContent);
                string fullFilePath = "filepath";

                ICollectionBase collection = repo.LoadCollection(fullFilePath);

                Assert.IsNotNull(collection);
            }
        }
Ejemplo n.º 6
0
        public void loadcollection_with_path_filename_calls_getfullfilepath()
        {
            IHomeCollectionRepository repo = new HomeCollectionRepository(_mockFileIO.Object);

            _mockFileIO.Setup(f => f.GetFullFilePath(It.IsAny <string>(), It.IsAny <string>()))
            .Returns((string p, string f) => p + @"\" + f);

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase testCollection  = GetTestCollection("test collection", collectionType, 1);
                string          testFileContent = HomeCollectionRepository.ConvertCollectionToJson(testCollection);
                _mockFileIO.Setup(i => i.ReadFile(It.IsAny <string>())).Returns(testFileContent);
                string path     = "path";
                string filename = "filename";

                ICollectionBase collection = repo.LoadCollection(path, filename);
            }
            _mockFileIO.Verify(r => r.GetFullFilePath(It.IsAny <string>(), It.IsAny <string>()), Times.Exactly(CollectableBaseFactory.CollectableTypes.Count));
        }
Ejemplo n.º 7
0
        public void collection_serialize_deserialize_success()
        {
            string collectionName              = "test";
            int    numberOfCollectables        = 2;
            int    numberOfItemsPerCollectable = 2;

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase testCollection = GetTestCollection(collectionName, collectionType, numberOfCollectables, numberOfItemsPerCollectable);
                string          jsonCollection = HomeCollectionRepository.ConvertCollectionToJson(testCollection);

                ICollectionBase resultCollection = HomeCollectionRepository.ConvertJsonToCollection(jsonCollection);

                Assert.AreEqual(collectionName, resultCollection.CollectionName);
                Assert.AreEqual(collectionType, resultCollection.CollectionType);
                Assert.AreEqual(numberOfCollectables, resultCollection.Collectables.Count);
                Assert.AreEqual(numberOfCollectables * numberOfItemsPerCollectable, resultCollection.Collectables.Count * resultCollection.Collectables[0].ItemInstances.Count);
            }
        }
Ejemplo n.º 8
0
        public void convertjsontocollection_deserialize_format_missing_bracket_throws_exception()
        {
            string collectionName              = "test";
            int    numberOfCollectables        = 2;
            int    numberOfItemsPerCollectable = 2;

            foreach (Type collectionType in CollectableBaseFactory.CollectableTypes)
            {
                ICollectionBase testCollection = GetTestCollection(collectionName, collectionType, numberOfCollectables, numberOfItemsPerCollectable);
                string          jsonCollectionMissingTrailingBracket = HomeCollectionRepository.ConvertCollectionToJson(testCollection);
                string          jsonCollectionMissingLeadingBracket  = jsonCollectionMissingTrailingBracket;
                jsonCollectionMissingTrailingBracket = jsonCollectionMissingTrailingBracket.TrimEnd('}');  // remove bracket
                jsonCollectionMissingLeadingBracket  = jsonCollectionMissingLeadingBracket.TrimStart('{'); // remove bracket

                bool failTrailing = false;
                bool failLeading  = false;
                try
                {
                    ICollectionBase resultCollection = HomeCollectionRepository.ConvertJsonToCollection(jsonCollectionMissingTrailingBracket);
                }
                catch (CollectionParseException)
                {
                    failTrailing = true;
                }
                try
                {
                    ICollectionBase resultCollection = HomeCollectionRepository.ConvertJsonToCollection(jsonCollectionMissingTrailingBracket);
                }
                catch (CollectionParseException)
                {
                    failLeading = true;
                }

                Assert.IsTrue(failTrailing, "Expect exception to be thrown when missing JSON bracket(s)");
                Assert.IsTrue(failLeading, "Expect exception to be thrown when missing JSON bracket(s)");
            }
        }