### Question:
BaseBucketLoader implements BucketLoader { @Override public Mono<ProposedBucketConfigContext> load(final NodeIdentifier seed, final int port, final String bucket, final Optional<String> alternateAddress) { return core .ensureServiceAt(seed, serviceType, port, Optional.of(bucket), alternateAddress) .then(ensureServiceConnected(seed, serviceType, Optional.of(bucket))) .then(discoverConfig(seed, bucket)) .map(config -> new String(config, UTF_8)) .map(config -> config.replace("$HOST", seed.address())) .map(config -> new ProposedBucketConfigContext(bucket, config, seed.address())) .onErrorResume(ex -> Mono.error(ex instanceof ConfigException ? ex : new ConfigException("Caught exception while loading config.", ex) )); } BaseBucketLoader(final Core core, final ServiceType serviceType); @Override Mono<ProposedBucketConfigContext> load(final NodeIdentifier seed, final int port, final String bucket,
                                                final Optional<String> alternateAddress);  }### Answer:
@Test void loadsAndParsesConfig() { BucketLoader loader = new BaseBucketLoader(core, SERVICE) { @Override protected Mono<byte[]> discoverConfig(NodeIdentifier seed, String bucket) { return Mono.just(readResource( "../config_with_external.json", BaseBucketLoaderTest.class ).getBytes(UTF_8)); } }; when(core.ensureServiceAt(eq(SEED), eq(SERVICE), eq(PORT), eq(Optional.of(BUCKET)), eq(Optional.empty()))) .thenReturn(Mono.empty()); when(core.serviceState(eq(SEED), eq(SERVICE), eq(Optional.of(BUCKET)))).thenReturn(Optional.of(Flux.empty())); ProposedBucketConfigContext ctx = loader.load(SEED, PORT, BUCKET, Optional.empty()).block(); BucketConfig config = BucketConfigParser.parse(ctx.config(), core.context().environment(), ctx.origin()); assertEquals("default", config.name()); assertEquals(1073, config.rev()); }
@Test void failsWhenServiceCannotBeEnabled() { BucketLoader loader = new BaseBucketLoader(core, SERVICE) { @Override protected Mono<byte[]> discoverConfig(NodeIdentifier seed, String bucket) { return Mono.error(new IllegalStateException("Not expected to be called!")); } }; when(core.ensureServiceAt(eq(SEED), eq(SERVICE), eq(PORT), eq(Optional.of(BUCKET)), eq(Optional.empty()))) .thenReturn(Mono.error(new CouchbaseException("Some error during service ensure"))); assertThrows(ConfigException.class, () -> loader.load(SEED, PORT, BUCKET, Optional.empty()).block()); }
@Test void failsWhenChildDiscoverFails() { BucketLoader loader = new BaseBucketLoader(core, SERVICE) { @Override protected Mono<byte[]> discoverConfig(NodeIdentifier seed, String bucket) { return Mono.error(new CouchbaseException("Failed discovering for some reason")); } }; when(core.ensureServiceAt(eq(SEED), eq(SERVICE), eq(PORT), eq(Optional.of(BUCKET)), eq(Optional.empty()))) .thenReturn(Mono.empty()); assertThrows(ConfigException.class, () -> loader.load(SEED, PORT, BUCKET, Optional.empty()).block()); }