CassandraAtlasDbFactory implements AtlasDbFactory { @VisibleForTesting static CassandraKeyValueServiceConfig preprocessKvsConfig( KeyValueServiceConfig config, Supplier<Optional<KeyValueServiceRuntimeConfig>> runtimeConfig, Optional<String> namespace) { Preconditions.checkArgument(config instanceof CassandraKeyValueServiceConfig, "Invalid KeyValueServiceConfig. Expected a KeyValueServiceConfig of type" + " CassandraKeyValueServiceConfig, found %s.", config.getClass()); CassandraKeyValueServiceConfig cassandraConfig = (CassandraKeyValueServiceConfig) config; String desiredKeyspace = OptionalResolver.resolve(namespace, cassandraConfig.keyspace()); CassandraKeyValueServiceConfig configWithNamespace = CassandraKeyValueServiceConfigs .copyWithKeyspace(cassandraConfig, desiredKeyspace); return new CassandraReloadableKvsConfig(configWithNamespace, runtimeConfig); }  @Override KeyValueService createRawKeyValueService(
            MetricsManager metricsManager,
            KeyValueServiceConfig config,
            Supplier<Optional<KeyValueServiceRuntimeConfig>> runtimeConfig,
            Optional<LeaderConfig> unused,
            Optional<String> namespace,
            LongSupplier freshTimestampSource,
            boolean initializeAsync); @Override ManagedTimestampService createManagedTimestampService(
            KeyValueService rawKvs,
            Optional<TableReference> timestampTable,
            boolean initializeAsync); @Override String getType(); @Override TimestampStoreInvalidator createTimestampStoreInvalidator(KeyValueService rawKvs);  }
@Test public void throwsWhenPreprocessingNonCassandraKvsConfig() { assertThatThrownBy(() -> { KeyValueServiceConfigHelper keyValueServiceConfig = () -> "Fake KVS"; CassandraAtlasDbFactory.preprocessKvsConfig(keyValueServiceConfig, Optional::empty, Optional.empty()); }) .isInstanceOf(IllegalArgumentException.class); }
@Test public void throwsWhenPreprocessingConfigWithNoKeyspaceAndNoNamespace() { assertThatThrownBy(() -> CassandraAtlasDbFactory.preprocessKvsConfig(CONFIG_WITHOUT_KEYSPACE, Optional::empty, Optional.empty())) .isInstanceOf(IllegalArgumentException.class); }
@Test public void throwsWhenPreprocessingConfigWithKeyspaceAndDifferentNamespace() { assertThatThrownBy( () -> CassandraAtlasDbFactory.preprocessKvsConfig(CONFIG_WITH_KEYSPACE, Optional::empty, Optional.of(KEYSPACE_2))) .isInstanceOf(IllegalArgumentException.class); }
@Test public void resolvesConfigWithOriginalKeyspaceIfNoNamespaceProvided() { CassandraKeyValueServiceConfig newConfig = CassandraAtlasDbFactory.preprocessKvsConfig(CONFIG_WITH_KEYSPACE, Optional::empty, Optional.empty()); assertThat(newConfig.getKeyspaceOrThrow()).isEqualTo(CONFIG_WITH_KEYSPACE.getKeyspaceOrThrow()); }
@Test public void resolvesConfigWithNamespaceIfNoKeyspaceProvided() { CassandraKeyValueServiceConfig newConfig = CassandraAtlasDbFactory.preprocessKvsConfig(CONFIG_WITHOUT_KEYSPACE, Optional::empty, Optional.of(KEYSPACE)); assertThat(newConfig.getKeyspaceOrThrow()).isEqualTo(KEYSPACE); }
@Test public void preservesOtherPropertiesOnResolvedConfigWithNamespace() { CassandraKeyValueServiceConfig newConfig = CassandraAtlasDbFactory.preprocessKvsConfig(CONFIG_WITHOUT_KEYSPACE, Optional::empty, Optional.of(KEYSPACE)); assertThat(newConfig.servers()) .isEqualTo(ImmutableDefaultConfig .builder().addAllThriftHosts(SERVERS).build()); assertThat(newConfig.replicationFactor()).isEqualTo(1); }
@Test public void resolvesConfigIfKeyspaceAndNamespaceProvidedAndMatch() { CassandraKeyValueServiceConfig newConfig = CassandraAtlasDbFactory.preprocessKvsConfig(CONFIG_WITH_KEYSPACE, Optional::empty, Optional.of(KEYSPACE)); assertThat(newConfig.getKeyspaceOrThrow()).isEqualTo(KEYSPACE); }