Ejemplo n.º 1
0
        public void TestCatsListOfMaleOwnerWhenThereAreNoCats()
        {
            // Arrange
            string jsonData     = "[{\"name\":\"Bob\",\"gender\":\"Male\",\"age\":23,\"pets\":[{\"name\":\"Fido\",\"type\":\"Dog\"}]},{\"name\":\"Jennifer\",\"gender\":\"Female\",\"age\":18,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"}]}]";
            var    dependencies = GetTestDependencies();

            dependencies.inputData.ownerAndTheirPets = JsonDeSerializer.FromJson(jsonData);

            var result = dependencies.catStrategy.GetPetsOfMaleOwner();

            Assert.AreEqual(0, result.Count);
        }
Ejemplo n.º 2
0
        public void TestCatsListOfMaleOwnerWhenPetsIsNull()
        {
            // Arrange
            string jsonData     = "[{\"name\":\"Bob\",\"gender\":\"Male\",\"age\":23,\"pets\":null}]";
            var    dependencies = GetTestDependencies();

            dependencies.inputData.ownerAndTheirPets = JsonDeSerializer.FromJson(jsonData);

            var result = dependencies.catStrategy.GetPetsOfMaleOwner();

            Assert.AreEqual(0, result.Count);
        }
Ejemplo n.º 3
0
        public void TestStrategyInstanceForCats()
        {
            // Arrange
            string jsonData     = "[{\"name\":\"Bob\",\"gender\":\"Male\",\"age\":23,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"},{\"name\":\"Fido\",\"type\":\"Dog\"}]},{\"name\":\"Jennifer\",\"gender\":\"Female\",\"age\":18,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"}]},{\"name\":\"Steve\",\"gender\":\"Male\",\"age\":45,\"pets\":null},{\"name\":\"Fred\",\"gender\":\"Male\",\"age\":40,\"pets\":[{\"name\":\"Tom\",\"type\":\"Cat\"},{\"name\":\"Max\",\"type\":\"Cat\"},{\"name\":\"Sam\",\"type\":\"Dog\"},{\"name\":\"Jim\",\"type\":\"Cat\"}]},{\"name\":\"Samantha\",\"gender\":\"Female\",\"age\":40,\"pets\":[{\"name\":\"Tabby\",\"type\":\"Cat\"}]},{\"name\":\"Alice\",\"gender\":\"Female\",\"age\":64,\"pets\":[{\"name\":\"Simba\",\"type\":\"Cat\"},{\"name\":\"Nemo\",\"type\":\"Fish\"}]}]";
            var    dependencies = GetTestDependencies();

            dependencies.inputData.ownerAndTheirPets = JsonDeSerializer.FromJson(jsonData);
            var expectedStrategy = new CatsStrategy(dependencies.petService, dependencies.inputData);

            var result = dependencies.petStrategyFactory.Resolve(PetType.Cat);

            Assert.AreEqual(JsonConvert.SerializeObject(expectedStrategy), JsonConvert.SerializeObject(result));
        }
Ejemplo n.º 4
0
        public void TestCatsCountOfFemaleOwnerWhenThereIsOnlyOneCat()
        {
            // Arrange
            string    jsonData  = "[{\"name\":\"Jennifer\",\"gender\":\"Female\",\"age\":18,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"}]}]";
            InputData inputData = new InputData();

            inputData.ownerAndTheirPets = JsonDeSerializer.FromJson(jsonData);

            IPetService petService = new PetService();

            var result = petService.GetPets(inputData, Gender.Female, PetType.Cat);

            Assert.AreEqual(1, result.Count);
        }
Ejemplo n.º 5
0
        public void TestCatsCountOfMaleOwnerWhenThereIsMoreThanOneCat()
        {
            // Arrange
            string    jsonData  = "[{\"name\":\"Bob\",\"gender\":\"Male\",\"age\":23,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"},{\"name\":\"Fido\",\"type\":\"Dog\"}]},{\"name\":\"Jennifer\",\"gender\":\"Female\",\"age\":18,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"}]},{\"name\":\"Steve\",\"gender\":\"Male\",\"age\":45,\"pets\":null},{\"name\":\"Fred\",\"gender\":\"Male\",\"age\":40,\"pets\":[{\"name\":\"Tom\",\"type\":\"Cat\"},{\"name\":\"Max\",\"type\":\"Cat\"},{\"name\":\"Sam\",\"type\":\"Dog\"},{\"name\":\"Jim\",\"type\":\"Cat\"}]},{\"name\":\"Samantha\",\"gender\":\"Female\",\"age\":40,\"pets\":[{\"name\":\"Tabby\",\"type\":\"Cat\"}]},{\"name\":\"Alice\",\"gender\":\"Female\",\"age\":64,\"pets\":[{\"name\":\"Simba\",\"type\":\"Cat\"},{\"name\":\"Nemo\",\"type\":\"Fish\"}]}]";
            InputData inputData = new InputData();

            inputData.ownerAndTheirPets = JsonDeSerializer.FromJson(jsonData);

            IPetService petService = new PetService();

            var result = petService.GetPets(inputData, Gender.Male, PetType.Cat);

            Assert.AreEqual(4, result.Count);
        }
Ejemplo n.º 6
0
        private TestDependencies GetTestDependencies()
        {
            string    jsonData  = "[{\"name\":\"Bob\",\"gender\":\"Male\",\"age\":23,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"},{\"name\":\"Fido\",\"type\":\"Dog\"}]},{\"name\":\"Jennifer\",\"gender\":\"Female\",\"age\":18,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"}]},{\"name\":\"Steve\",\"gender\":\"Male\",\"age\":45,\"pets\":null}]}]";
            InputData inputData = new InputData();

            inputData.ownerAndTheirPets = JsonDeSerializer.FromJson(jsonData);

            IPetService petService = new PetService();

            return(new TestDependencies
            {
                petService = petService,
                inputData = inputData
            });
        }
Ejemplo n.º 7
0
        public void TestThrowExceptionIfGenderIsInvalid()
        {
            string jsonData = "[{\"name\":\"Bob\",\"gender\":\"FMale\",\"age\":23,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"},{\"name\":\"Fido\",\"type\":\"Dog\"}]}]";

            try
            {
                var result = JsonDeSerializer.FromJson(jsonData);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is GenderIncorrectException);
                Assert.AreEqual(ex.Message, "Invalid Gender: FMale");
            }
        }
Ejemplo n.º 8
0
        public void TestThrowExceptionIfDataIsNull()
        {
            string jsonData = null;

            try
            {
                var result = JsonDeSerializer.FromJson(jsonData);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is InputDataIncorrectException);
                Assert.AreEqual(ex.Message, "Data cannot be null or empty");
            }
        }
Ejemplo n.º 9
0
        public void TestThrowExceptionOnRetrievingFish()
        {
            // Arrange
            string jsonData     = "[{\"name\":\"Bob\",\"gender\":\"Male\",\"age\":23,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"},{\"name\":\"Fido\",\"type\":\"Dog\"}]},{\"name\":\"Jennifer\",\"gender\":\"Female\",\"age\":18,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"}]},{\"name\":\"Steve\",\"gender\":\"Male\",\"age\":45,\"pets\":null},{\"name\":\"Fred\",\"gender\":\"Male\",\"age\":40,\"pets\":[{\"name\":\"Tom\",\"type\":\"Cat\"},{\"name\":\"Max\",\"type\":\"Cat\"},{\"name\":\"Sam\",\"type\":\"Dog\"},{\"name\":\"Jim\",\"type\":\"Cat\"}]},{\"name\":\"Samantha\",\"gender\":\"Female\",\"age\":40,\"pets\":[{\"name\":\"Tabby\",\"type\":\"Cat\"}]},{\"name\":\"Alice\",\"gender\":\"Female\",\"age\":64,\"pets\":[{\"name\":\"Simba\",\"type\":\"Cat\"},{\"name\":\"Nemo\",\"type\":\"Fish\"}]}]";
            var    dependencies = GetTestDependencies();

            dependencies.inputData.ownerAndTheirPets = JsonDeSerializer.FromJson(jsonData);

            try
            {
                var result = dependencies.petStrategyFactory.Resolve(PetType.Fish);
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.IsTrue(ex is NotImplementedException);
            }
        }
Ejemplo n.º 10
0
        public void TestCatsListOfFemaleOwnerWhenThereMoreThanOneCat_SortedAscending()
        {
            // Arrange
            string jsonData     = "[{\"name\":\"Bob\",\"gender\":\"Male\",\"age\":23,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"},{\"name\":\"Fido\",\"type\":\"Dog\"}]},{\"name\":\"Jennifer\",\"gender\":\"Female\",\"age\":18,\"pets\":[{\"name\":\"Garfield\",\"type\":\"Cat\"}]},{\"name\":\"Steve\",\"gender\":\"Male\",\"age\":45,\"pets\":null},{\"name\":\"Fred\",\"gender\":\"Male\",\"age\":40,\"pets\":[{\"name\":\"Tom\",\"type\":\"Cat\"},{\"name\":\"Max\",\"type\":\"Cat\"},{\"name\":\"Sam\",\"type\":\"Dog\"},{\"name\":\"Jim\",\"type\":\"Cat\"}]},{\"name\":\"Samantha\",\"gender\":\"Female\",\"age\":40,\"pets\":[{\"name\":\"Tabby\",\"type\":\"Cat\"}]},{\"name\":\"Alice\",\"gender\":\"Female\",\"age\":64,\"pets\":[{\"name\":\"Simba\",\"type\":\"Cat\"},{\"name\":\"Nemo\",\"type\":\"Fish\"}]}]";
            var    dependencies = GetTestDependencies();

            dependencies.inputData.ownerAndTheirPets = JsonDeSerializer.FromJson(jsonData);

            var expectedOutput = new List <string> {
                "Garfield", "Simba", "Tabby"
            };

            var result = dependencies.catStrategy.GetPetsOfFemaleOwner();

            Assert.AreEqual(expectedOutput.Count, result.Count);
            Assert.AreEqual(expectedOutput[0], result[0]);
            Assert.AreEqual(expectedOutput[1], result[1]);
            Assert.AreEqual(expectedOutput[2], result[2]);
        }
Ejemplo n.º 11
0
        public static void Main(string[] args)
        {
            HttpClientService httpClientService = new HttpClientService();
            // Read data from Http Service
            string jsonData = httpClientService.Get();

            // Deserialize json data
            InputData data = new InputData();

            data.ownerAndTheirPets = JsonDeSerializer.FromJson(jsonData);

            if (data.ownerAndTheirPets != null)
            {
                // Get Pets of Pet types listed
                List <PetType> getPetsOfType = new List <PetType>()
                {
                    PetType.Cat
                };

                IPetService   petService       = new PetService();
                OutputService outPutService    = new OutputService();
                var           stratergyFactory = new PetStrategyFactory(petService, data);

                // Determine the strategy and get pets based on PetType and Owner Gender
                foreach (PetType petType in getPetsOfType)
                {
                    var           Strategy        = stratergyFactory.Resolve(petType);
                    List <string> maleOwnerPets   = Strategy.GetPetsOfMaleOwner();
                    List <string> femaleOwnerCats = Strategy.GetPetsOfFemaleOwner();

                    outPutService.PrintOutput(petType, maleOwnerPets, femaleOwnerCats);
                }
            }
            else
            {
                Console.WriteLine("No data to process");
            }

            Console.ReadKey();
        }