Пример #1
0
        public void GetDependenciesCalledWithCorrectParamsTest()
        {
            //Arrange
            const string location = "TestLocation";
            var container = new Container();
            var stubRestClient = MockRepository.GenerateStub<IRestClientAdapter>();
            var stubJsonConverter = MockRepository.GenerateStub<IJsonConvertAdapter>();

            container.RegisterInstance(stubRestClient);
            container.RegisterInstance(stubJsonConverter);
            container.Register<IWeatherRepository, WeatherRepository>();

            var weatherRepository = container.Get<IWeatherRepository>();
            var restClientResponse = new RestClientResponse()
            {
                Content = "TestContent"
            };
            var weatherServiceDetail = new WeatherServiceDetail()
            {
                Resource = "TestResource",
                ServiceUrl = "TestUrl"
            };
            var urlSegment = new Dictionary<string, string> {{"location", location}};

            stubRestClient.Stub(x => x.Get(Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<Dictionary<string, string>>.Is.Anything))
                .Return(restClientResponse);

            //Act
            weatherRepository.Get(weatherServiceDetail, location);

            //Assert
            stubRestClient.AssertWasCalled(x => x.Get(weatherServiceDetail.ServiceUrl, weatherServiceDetail.Resource, urlSegment));
            stubJsonConverter.AssertWasCalled(x => x.DeserializeObject<WeatherServiceResponse>(restClientResponse.Content));
        }
Пример #2
0
        public void GetServiceFailedExceptionThrownTest()
        {
            //Arrange
            var container = new Container();
            var stubRestClient = MockRepository.GenerateStub<IRestClientAdapter>();
            var stubJsonConverter = MockRepository.GenerateStub<IJsonConvertAdapter>();

            container.RegisterInstance(stubRestClient);
            container.RegisterInstance(stubJsonConverter);
            container.Register<IWeatherRepository, WeatherRepository>();

            var weatherRepository = container.Get<IWeatherRepository>();
            var restClientResponse = new RestClientResponse()
            {
                Content = "TestContent",
                ResponseStatus = RestResponseStatus.Failure
            };
            var weatherServiceDetail = new WeatherServiceDetail()
            {
                Resource = "TestResource",
                ServiceUrl = "TestUrl"
            };
            const string location = "TestLocation";

            stubRestClient.Stub(x => x.Get(Arg<string>.Is.Anything, Arg<string>.Is.Anything, Arg<Dictionary<string, string>>.Is.Anything))
                .Return(restClientResponse);

            //Act
            Action action = () => weatherRepository.Get(weatherServiceDetail, location);

            //Assert
            action.ShouldThrow<WeatherServiceException>()
                .WithMessage("Service failed");
        }
        public void GetWeatherTest()
        {
            //Arrange
            var container = new Container();
            var weatherServices = new WeatherServiceStore();
            weatherServices.RegisterService(new WeatherServiceDetail()
            {
                ServiceUrl = "http://localhost:18888/api",
                Resource = "Weather/{location}"
            });
            weatherServices.RegisterService(new WeatherServiceDetail()
            {
                ServiceUrl = "http://localhost:17855/api",
                Resource = "Weather/{location}"
            });

            container.Register<IWeatherAggregatorService, WeatherAggregatorService>();
            container.Register<IWeatherRepository, WeatherRepository>();
            container.Register<IWeatherUnitConverterService, WeatherUnitConverterService>();
            container.Register<IRestClientAdapter, RestClientAdapter>();
            container.Register<IJsonConvertAdapter, JsonConvertAdapter>();
            container.RegisterInstance<IWeatherServiceStore>(weatherServices);

            var weatherAggregatorService = container.Get<IWeatherAggregatorService>();

            const string location = "TestLocation";

            //Act
            var result = weatherAggregatorService.GetAggregatedWeatherFromRegisteredServices(location);

            //Assert
            result.Should().NotBeNull();
        }
        public void GetWeatherAverageTemperatureValuesAreCorrectTest(double celsius, double fahrenheit, double averageCelsius, double averageFahrenheit)
        {
            //Arrange
            const string location = "TestLocation";
            var container = new Container();
            var stubWeatherRespository = MockRepository.GenerateStub<IWeatherRepository>();
            var servicesStore = new WeatherServiceStore();

            container.RegisterInstance(stubWeatherRespository);
            container.RegisterInstance<IWeatherServiceStore>(servicesStore);
            container.Register<IJsonConvertAdapter, JsonConvertAdapter>();
            container.Register<IRestClientAdapter, RestClientAdapter>();
            container.Register<IWeatherUnitConverterService, WeatherUnitConverterService>();
            container.Register<IWeatherAggregatorService, WeatherAggregatorService>();

            servicesStore.RegisterService(new WeatherServiceDetail()
            {
                ServiceUrl = "Test1",
                Resource = "Weather"
            });

            servicesStore.RegisterService(new WeatherServiceDetail()
            {
                ServiceUrl = "Test2",
                Resource = "Weather"
            });

            stubWeatherRespository.Stub(x => x.Get(Arg<WeatherServiceDetail>.Is.Equal((servicesStore.Services[0])), Arg<string>.Is.Anything))
                .Return(new WeatherServiceResponse()
                {
                    TemperatureCelsius = celsius,
                    Location = location
                });

            stubWeatherRespository.Stub(x => x.Get(Arg<WeatherServiceDetail>.Is.Equal((servicesStore.Services[1])), Arg<string>.Is.Anything))
                .Return(new WeatherServiceResponse()
                {
                    TemperatureFahrenheit = fahrenheit,
                    Location = location
                });

            var weatherAggregatorService = container.Get<IWeatherAggregatorService>();

            //Act
            var result = weatherAggregatorService.GetAggregatedWeatherFromRegisteredServices(location);

            //Assert
            result.AverageTemperatureCelsius.Should().Be(averageCelsius);
            result.AverageTemperatureFahrenheit.Should().Be(averageFahrenheit);
        }
        public void GetWeatherLocationIsEmptyOrNullShouldReturnNullTest(string location)
        {
            //Arrange
            var container = new Container();
            var stubWeatherRespository = MockRepository.GenerateStub<IWeatherRepository>();
            var stubWeatherServiceStore = MockRepository.GenerateStub<IWeatherServiceStore>();
            var stubWeatherUnitConverterService = MockRepository.GenerateStub<IWeatherUnitConverterService>();

            container.RegisterInstance(stubWeatherRespository);
            container.RegisterInstance(stubWeatherServiceStore);
            container.RegisterInstance(stubWeatherUnitConverterService);
            container.Register<IWeatherAggregatorService, WeatherAggregatorService>();

            var weatherAggregatorService = container.Get<IWeatherAggregatorService>();

            //Act
            var result = weatherAggregatorService.GetAggregatedWeatherFromRegisteredServices(location);

            //Assert
            result.Should().BeNull("location is empty of null");
        }