Exemplo n.º 1
0
        public void Get_Configuration_Reads_ContentLocation()
        {
            IConfigurationProvider provider = new AppConfigConfigurationProvider();

            IConfiguration configuration = provider.Configuration;

            configuration.ContentLocation.Should().Be("MyPath/To/Blog");
        }
    public void GetConfiguration_KomfoSharpNodeIsAbsent_ThrowsException()
    {
      // Arrange
      var xmlSource = Substitute.For<IXmlSource>();
      var configurationFactory = new AppConfigConfigurationProvider(xmlSource);
      xmlSource.GetXml().Returns((XmlNode)null);

      // Act, Assert
      configurationFactory
        .Invoking((x) => x.GetConfiguration())
        .ShouldThrow<InvalidOperationException>()
        .WithMessage("Could not retrieve the 'komfoSharp' configuration node.");
    }
    public void GetConfiguration_ConfigurationIsValid_ReturnsConfiguration()
    {
      // Arrange
      var configuration = XDocument.Parse(@"<komfoSharp>
                                              <services>
                                                <endpoints baseUrl='https://connect.komfo.com/' >
                                                  <tokens path='/oauth20/tokens' />
                                                  <stream path='/v1/twitter/followers/stream' maxTwitterHandlesPerCall='100' maxResultsPerCall='1000'/>
                                                  <metrics path='/v1/twitter/followers/metrics' maxTwitterHandlesPerCall='100' />
                                                  <customAudiences path='/v1/ads/customaudiences' />
                                                  <customAudience path='/v1/ads/customaudiences/{audience_id}' />
                                                  <customAudienceUsers path='/v1/ads/customaudiences/{audience_id}/users' maxEntriesPerCall='5000' />
                                                  <customAudienceStatus path='/v1/ads/customaudiences/{audience_id}/status' />
                                                  <campaigns path='/v1/ads/campaigns' />
                                                  <campaignCustomAudiences path='/v1/ads/campaigns/{campaign_id}/customaudiences' />
                                                </endpoints>
                                              </services>
                                              <polling defaultTimeInterval='00:00:10' defaultAttemptsCount='6'/>
                                            </komfoSharp>");

      var xmlSource = Substitute.For<IXmlSource>();
      var configurationFactory = new AppConfigConfigurationProvider(xmlSource);
      xmlSource.GetXml().Returns(ToXmlDocument(configuration).SelectSingleNode("komfoSharp"));

      // Act
      var result = configurationFactory.GetConfiguration();

      // Assert
      result.PollingConfiguration.DefaultAttemptsCount.Should().Be(6);
      result.PollingConfiguration.DefaultTimeInterval.Should().Be(TimeSpan.FromSeconds(10));

      result.EndpointsConfiguration.Tokens.Path.Should().Be("/oauth20/tokens");

      result.EndpointsConfiguration.Metrics.Path.Should().Be("/v1/twitter/followers/metrics");
      result.EndpointsConfiguration.Metrics.MaxTwitterHandlesPerCall.Should().Be(100);

      result.EndpointsConfiguration.Stream.Path.Should().Be("/v1/twitter/followers/stream");
      result.EndpointsConfiguration.Stream.MaxTwitterHandlesPerCall.Should().Be(100);
      result.EndpointsConfiguration.Stream.MaxResultsPerCall.Should().Be(1000);

      result.EndpointsConfiguration.CustomAudiences.Path.Should().Be("/v1/ads/customaudiences");
      result.EndpointsConfiguration.CustomAudience.Path.Should().Be("/v1/ads/customaudiences/{audience_id}");
      result.EndpointsConfiguration.CustomAudienceUsers.Path.Should().Be("/v1/ads/customaudiences/{audience_id}/users");
      result.EndpointsConfiguration.CustomAudienceUsers.MaxEntriesPerCall.Should().Be(5000);
      result.EndpointsConfiguration.CustomAudienceStatus.Path.Should().Be("/v1/ads/customaudiences/{audience_id}/status");
      result.EndpointsConfiguration.Campaigns.Path.Should().Be("/v1/ads/campaigns");
      result.EndpointsConfiguration.CampaignCustomAudiences.Path.Should().Be("/v1/ads/campaigns/{campaign_id}/customaudiences");
    }
Exemplo n.º 4
0
        public override IConfigurationProvider CreateConfigurationProvider(object globalConfig)
        {
            if (this.RefreshTime <= TimeSpan.Zero)
            {
                this.RefreshTime = new TimeSpan(0, 1, 0);
            }

            if (string.IsNullOrEmpty(this.FileName))
            {
                AppConfigConfigurationProvider output;

                if (HttpContext.Current == null && OperationContext.Current == null)
                {
                    output = new AppConfigConfigurationProvider();
                }
                else
                {
                    output = new WebConfigConfigurationProvider();
                }

                output.RefreshTime = this.RefreshTime;

                return(output);
            }
            else
            {
                AppConfigConfigurationProvider local = new AppConfigConfigurationProvider();
                string baseDirectory = local.ApplicationPath;

                if (globalConfig != null && globalConfig is CompositeConfigurationProviderConfiguration)
                {
                    CompositeConfigurationProviderConfiguration cComp = globalConfig as CompositeConfigurationProviderConfiguration;

                    if (!string.IsNullOrEmpty(cComp.BaseDirectory))
                    {
                        baseDirectory = Path.Combine(baseDirectory, cComp.BaseDirectory);
                        baseDirectory = Path.GetFullPath(baseDirectory);
                    }
                }

                baseDirectory = Path.Combine(baseDirectory, this.FileName);
                local.ConfigurationFileName = baseDirectory;
                local.RefreshTime           = this.RefreshTime;

                return(local);
            }
        }
    public void GetConfiguration_EndpointsNodeIsAbsent_ThrowsException()
    {
      // Arrange
      var configuration = XDocument.Parse(@"<komfoSharp>
                                              <polling defaultTimeInterval='00:00:10' defaultAttemptsCount='6'/>
                                            </komfoSharp>");

      var xmlSource = Substitute.For<IXmlSource>();
      var configurationFactory = new AppConfigConfigurationProvider(xmlSource);
      xmlSource.GetXml().Returns(ToXmlDocument(configuration).SelectSingleNode("komfoSharp"));

      // Act, Assert
      configurationFactory
        .Invoking((x) => x.GetConfiguration())
        .ShouldThrow<InvalidOperationException>()
        .WithMessage("Could not retrieve the 'komfoSharp/services/endpoints' configuration node.");
    }
    public void GetConfiguration_MaxEntriesPerCallIsNotSetForCustomAudienceUsersEndpoint_ThrowsException()
    {
      // Arrange
      var configuration = XDocument.Parse(@"<komfoSharp>
                                              <services>
                                                <endpoints baseUrl='https://connect.komfo.com'>
                                                  <customAudienceUsers path='/v1/ads/customaudiences/{audience_id}/users' />
                                                </endpoints>
                                              </services>
                                              <polling defaultTimeInterval='00:00:10' defaultAttemptsCount='6'/>
                                            </komfoSharp>");

      var xmlSource = Substitute.For<IXmlSource>();
      var configurationFactory = new AppConfigConfigurationProvider(xmlSource);
      xmlSource.GetXml().Returns(ToXmlDocument(configuration).SelectSingleNode("komfoSharp"));

      // Act, Assert
      configurationFactory
        .Invoking((x) => x.GetConfiguration())
        .ShouldThrow<InvalidOperationException>()
        .WithMessage("There were errors during deserializing the 'komfoSharp/services/endpoints/customAudienceUsers' node into the KomfoSharp.Configuration.Endpoints.CustomAudienceUsersEndpoint type. See inner exception for details.");
    }
    public void GetEndpointUrl_AudienceIdIsPassedToCustomAudienceUsersEndpoint_UrlIsFormattedWithAudienceId()
    {
      // arrange
      var configuration = XDocument.Parse(@"<komfoSharp>
                                              <services>
                                                <endpoints baseUrl='https://connect.komfo.com'>
                                                  <customAudience path='/v1/ads/customaudiences/{audience_id}' />
                                                </endpoints>
                                              </services>
                                              <polling defaultTimeInterval='00:00:10' defaultAttemptsCount='6'/>
                                            </komfoSharp>");

      var xmlSource = Substitute.For<IXmlSource>();
      var configurationFactory = new AppConfigConfigurationProvider(xmlSource);
      xmlSource.GetXml().Returns(ToXmlDocument(configuration).SelectSingleNode("komfoSharp"));

      // act
      var endpointConfiguration = configurationFactory.GetConfiguration().EndpointsConfiguration;
      var url = endpointConfiguration.GetEndpointUrl(endpointConfiguration.CustomAudience, new Dictionary<string, string> { { CustomAudienceEndpoint.Parameters.AudienceId, "1" } });

      // assert
      url.ToString().Should().Be("https://connect.komfo.com/v1/ads/customaudiences/1");
    }
    public void GetConfiguration_PollingNodeIsAbsent_ThrowsException()
    {
      // Arrange
      var configuration = XDocument.Parse(@"<komfoSharp>
                                              <services>
                                                <endpoints baseUrl='https://connect.komfo.com'>
                                                  <tokens path='/oauth20/tokens' />
                                                  <stream path='/v1/twitter/followers/stream' maxTwitterHandlesPerCall='100' maxResultsPerCall='1000'/>
                                                  <metrics path='/v1/twitter/followers/metrics' maxTwitterHandlesPerCall='100' />
                                                </endpoints>
                                              </services>
                                            </komfoSharp>");

      var xmlSource = Substitute.For<IXmlSource>();
      var configurationFactory = new AppConfigConfigurationProvider(xmlSource);
      xmlSource.GetXml().Returns(ToXmlDocument(configuration).SelectSingleNode("komfoSharp"));

      // Act, Assert
      configurationFactory
        .Invoking((x) => x.GetConfiguration())
        .ShouldThrow<InvalidOperationException>()
        .WithMessage("Could not retrieve the 'komfoSharp/polling' configuration node.");
    }
    public void GetConfiguration_MaxResultsPerCallIsNotSetForStreamEndpoint_ThrowsException()
    {
      // Arrange
      var configuration = XDocument.Parse(@"<komfoSharp>
                                              <services>
                                                <endpoints baseUrl='https://connect.komfo.com'>
                                                  <tokens path='/oauth20/tokens' />
                                                  <stream path='/v1/twitter/followers/stream' maxTwitterHandlesPerCall='100' />
                                                  <metrics path='/v1/twitter/followers/metrics' maxTwitterHandlesPerCall='100' />
                                                </endpoints>
                                              </services>
                                              <polling defaultTimeInterval='00:00:10' defaultAttemptsCount='6'/>
                                            </komfoSharp>");

      var xmlSource = Substitute.For<IXmlSource>();
      var configurationFactory = new AppConfigConfigurationProvider(xmlSource);
      xmlSource.GetXml().Returns(ToXmlDocument(configuration).SelectSingleNode("komfoSharp"));

      // Act, Assert
      configurationFactory
        .Invoking((x) => x.GetConfiguration())
        .ShouldThrow<InvalidOperationException>()
        .WithMessage("There were errors during deserializing the 'komfoSharp/services/endpoints/stream' node into the KomfoSharp.Configuration.Endpoints.StreamEndpoint type. See inner exception for details.");
    }
    public void GetConfiguration_BaseUrlInvalid_ThrowsException()
    {
      // Arrange
      var configuration = XDocument.Parse(@"<komfoSharp>
                                              <services>
                                                <endpoints baseUrl='invalid'>
                                                  <tokens path='/oauth20/tokens' />
                                                  <stream path='/v1/twitter/followers/stream' maxTwitterHandlesPerCall='100' maxResultsPerCall='1000'/>
                                                  <metrics path='/v1/twitter/followers/metrics' maxTwitterHandlesPerCall='100' />
                                                </endpoints>
                                              </services>
                                              <polling defaultTimeInterval='00:00:10' defaultAttemptsCount='6'/>
                                            </komfoSharp>");

      var xmlSource = Substitute.For<IXmlSource>();
      var configurationFactory = new AppConfigConfigurationProvider(xmlSource);
      xmlSource.GetXml().Returns(ToXmlDocument(configuration).SelectSingleNode("komfoSharp"));

      // Act, Assert
      configurationFactory
        .Invoking((x) => x.GetConfiguration())
        .ShouldThrow<InvalidOperationException>()
        .WithMessage("The 'baseUrl' value should be a valid URL.");
    }