Search in sources :

Example 1 with OptionsMap

use of com.datastax.oss.driver.api.core.config.OptionsMap in project java-driver by datastax.

the class MapBasedConfigLoaderIT method should_create_policies_per_profile.

/**
 * Checks that profiles that have specific policy options will get their own policy instance.
 */
@Test
public void should_create_policies_per_profile() {
    // Given
    // a query that throws UNAVAILABLE
    String mockQuery = "mock query";
    SIMULACRON_RULE.cluster().prime(when(mockQuery).then(unavailable(QUORUM, 3, 2)));
    // a default profile that uses the default retry policy, and an alternate profile that uses a
    // policy that ignores all errors
    OptionsMap optionsMap = OptionsMap.driverDefaults();
    String alternateProfile = "profile1";
    optionsMap.put(alternateProfile, TypedDriverOption.RETRY_POLICY_CLASS, IgnoreAllPolicy.class.getName());
    try (CqlSession session = CqlSession.builder().addContactEndPoints(SIMULACRON_RULE.getContactPoints()).withLocalDatacenter("dc1").withConfigLoader(DriverConfigLoader.fromMap(optionsMap)).build()) {
        // When
        // executing the query for the default profile
        SimpleStatement defaultProfileStatement = SimpleStatement.newInstance(mockQuery);
        assertThatThrownBy(() -> session.execute(defaultProfileStatement)).satisfies(t -> {
            // Then
            // the UNAVAILABLE error is surfaced
            assertThat(t).isInstanceOf(AllNodesFailedException.class);
            AllNodesFailedException anfe = (AllNodesFailedException) t;
            assertThat(anfe.getAllErrors()).hasSize(1);
            List<Throwable> nodeErrors = anfe.getAllErrors().values().iterator().next();
            assertThat(nodeErrors).hasSize(1);
            assertThat(nodeErrors.get(0)).isInstanceOf(UnavailableException.class);
        });
        // When
        // executing the query for the alternate profile
        SimpleStatement alternateProfileStatement = SimpleStatement.newInstance(mockQuery).setExecutionProfileName(alternateProfile);
        ResultSet rs = session.execute(alternateProfileStatement);
        // Then
        // the error is ignored
        assertThat(rs.one()).isNull();
    }
}
Also used : OptionsMap(com.datastax.oss.driver.api.core.config.OptionsMap) AllNodesFailedException(com.datastax.oss.driver.api.core.AllNodesFailedException) SimpleStatement(com.datastax.oss.driver.api.core.cql.SimpleStatement) ResultSet(com.datastax.oss.driver.api.core.cql.ResultSet) CqlSession(com.datastax.oss.driver.api.core.CqlSession) Test(org.junit.Test)

Example 2 with OptionsMap

use of com.datastax.oss.driver.api.core.config.OptionsMap in project java-driver by datastax.

the class MapBasedDriverConfigTest method should_override_option_in_profile.

@Test
public void should_override_option_in_profile() {
    OptionsMap source = new OptionsMap();
    source.put(MockTypedOptions.INT1, 42);
    source.put("profile1", MockTypedOptions.INT1, 43);
    DriverConfig config = DriverConfigLoader.fromMap(source).getInitialConfig();
    assertThat(config).hasIntOption(MockOptions.INT1, 42).hasIntOption("profile1", MockOptions.INT1, 43);
}
Also used : OptionsMap(com.datastax.oss.driver.api.core.config.OptionsMap) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) Test(org.junit.Test)

Example 3 with OptionsMap

use of com.datastax.oss.driver.api.core.config.OptionsMap in project java-driver by datastax.

the class MapBasedDriverConfigTest method should_load_minimal_config_with_no_profiles.

@Test
public void should_load_minimal_config_with_no_profiles() {
    OptionsMap source = new OptionsMap();
    source.put(MockTypedOptions.INT1, 42);
    DriverConfig config = DriverConfigLoader.fromMap(source).getInitialConfig();
    assertThat(config).hasIntOption(MockOptions.INT1, 42);
}
Also used : OptionsMap(com.datastax.oss.driver.api.core.config.OptionsMap) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) Test(org.junit.Test)

Example 4 with OptionsMap

use of com.datastax.oss.driver.api.core.config.OptionsMap in project java-driver by datastax.

the class MapBasedDriverConfigTest method should_inherit_option_in_profile.

@Test
public void should_inherit_option_in_profile() {
    OptionsMap source = new OptionsMap();
    source.put(MockTypedOptions.INT1, 42);
    // need to add an unrelated option to create the profile
    source.put("profile1", MockTypedOptions.INT2, 1);
    DriverConfig config = DriverConfigLoader.fromMap(source).getInitialConfig();
    assertThat(config).hasIntOption(MockOptions.INT1, 42).hasIntOption("profile1", MockOptions.INT1, 42);
}
Also used : OptionsMap(com.datastax.oss.driver.api.core.config.OptionsMap) DriverConfig(com.datastax.oss.driver.api.core.config.DriverConfig) Test(org.junit.Test)

Example 5 with OptionsMap

use of com.datastax.oss.driver.api.core.config.OptionsMap in project java-driver by datastax.

the class MapBasedDriverConfigLoaderTest method should_fill_default_profile_like_reference_file.

/**
 * Checks that, if we ask to pre-fill the default profile, then we get the same set of options as
 * the built-in reference.conf.
 */
@Test
public void should_fill_default_profile_like_reference_file() {
    OptionsMap optionsMap = OptionsMap.driverDefaults();
    DriverExecutionProfile mapBasedConfig = DriverConfigLoader.fromMap(optionsMap).getInitialConfig().getDefaultProfile();
    DriverExecutionProfile fileBasedConfig = new DefaultDriverConfigLoader(() -> {
        // Only load reference.conf since we are focusing on driver defaults
        ConfigFactory.invalidateCaches();
        return defaultReference().getConfig(DefaultDriverConfigLoader.DEFAULT_ROOT_PATH);
    }).getInitialConfig().getDefaultProfile();
    // Make sure we're not missing any options. -1 is for CONFIG_RELOAD_INTERVAL, which is not
    // defined by OptionsMap because it is irrelevant for the map-based config.
    assertThat(mapBasedConfig.entrySet()).hasSize(fileBasedConfig.entrySet().size() - 1);
    for (TypedDriverOption<?> option : TypedDriverOption.builtInValues()) {
        if (option.getRawOption() == DefaultDriverOption.CONFIG_RELOAD_INTERVAL) {
            continue;
        }
        Optional<Object> fileBasedValue = get(fileBasedConfig, option);
        Optional<Object> mapBasedValue = get(mapBasedConfig, option);
        assertThat(mapBasedValue).as("Wrong value for %s in OptionsMap", option.getRawOption()).isEqualTo(fileBasedValue);
    }
}
Also used : OptionsMap(com.datastax.oss.driver.api.core.config.OptionsMap) DefaultDriverConfigLoader(com.datastax.oss.driver.internal.core.config.typesafe.DefaultDriverConfigLoader) DriverExecutionProfile(com.datastax.oss.driver.api.core.config.DriverExecutionProfile) Test(org.junit.Test)

Aggregations

OptionsMap (com.datastax.oss.driver.api.core.config.OptionsMap)13 Test (org.junit.Test)10 DriverConfig (com.datastax.oss.driver.api.core.config.DriverConfig)7 DriverExecutionProfile (com.datastax.oss.driver.api.core.config.DriverExecutionProfile)4 CqlSession (com.datastax.oss.driver.api.core.CqlSession)2 DriverConfigLoader (com.datastax.oss.driver.api.core.config.DriverConfigLoader)2 DefaultDriverConfigLoader (com.datastax.oss.driver.internal.core.config.typesafe.DefaultDriverConfigLoader)2 AllNodesFailedException (com.datastax.oss.driver.api.core.AllNodesFailedException)1 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)1 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)1 Node (com.datastax.oss.driver.api.core.metadata.Node)1 Before (org.junit.Before)1 Test (org.junit.jupiter.api.Test)1