Skip to content

Commit

Permalink
Code coverage additions for Cloud (apache#17621)
Browse files Browse the repository at this point in the history
Code coverage additions for Cloud: AWSCredentialsConfigTest
  • Loading branch information
ashwintumma23 authored Jan 21, 2025
1 parent 50853ce commit a805f4b
Showing 1 changed file with 33 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,49 +61,58 @@ private void cleanProperties()
properties.clear();
}

private Injector createInjector()
{
return Guice.createInjector(
binder -> {
binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/redis");
binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(-1);
binder.bind(Validator.class).toInstance(Validation.buildDefaultValidatorFactory().getValidator());
binder.bindScope(LazySingleton.class, Scopes.SINGLETON);
binder.bind(JsonConfigurator.class).in(LazySingleton.class);
binder.bind(Properties.class).toInstance(properties);
JsonConfigProvider.bind(binder, PROPERTY_PREFIX, AWSCredentialsConfig.class);
}
);
}

@Test
public void testDefaultValues()
{
final Injector injector = createInjector();
final AWSCredentialsConfig credentialsConfig = injector.getInstance(AWSCredentialsConfig.class);
Assert.assertEquals("", credentialsConfig.getAccessKey().getPassword());
Assert.assertEquals("", credentialsConfig.getSecretKey().getPassword());
}

@Test
public void testStringProperty()
{
final String filePath = "/path/to/credentials";
properties.put(PROPERTY_PREFIX + ".fileSessionCredentials", filePath);
properties.put(PROPERTY_PREFIX + ".accessKey", SOME_SECRET);
properties.put(PROPERTY_PREFIX + ".secretKey", SOME_SECRET);

final Injector injector = Guice.createInjector(
binder -> {
binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/redis");
binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(-1);
binder.bind(Validator.class).toInstance(Validation.buildDefaultValidatorFactory().getValidator());
binder.bindScope(LazySingleton.class, Scopes.SINGLETON);
binder.bind(JsonConfigurator.class).in(LazySingleton.class);
binder.bind(Properties.class).toInstance(properties);
JsonConfigProvider.bind(binder, PROPERTY_PREFIX, AWSCredentialsConfig.class);
}
);
final Injector injector = createInjector();
final AWSCredentialsConfig credentialsConfig = injector.getInstance(AWSCredentialsConfig.class);
Assert.assertEquals(filePath, credentialsConfig.getFileSessionCredentials());
Assert.assertEquals(SOME_SECRET, credentialsConfig.getAccessKey().getPassword());
Assert.assertEquals(SOME_SECRET, credentialsConfig.getSecretKey().getPassword());
}

@Test
public void testJsonProperty() throws Exception
{
final String filePath = "/path/to/credentials";
final String someSecret = new ObjectMapper().writeValueAsString(new DefaultPasswordProvider(SOME_SECRET));
properties.put(PROPERTY_PREFIX + ".fileSessionCredentials", filePath);
properties.put(PROPERTY_PREFIX + ".accessKey", someSecret);
properties.put(PROPERTY_PREFIX + ".secretKey", someSecret);

final Injector injector = Guice.createInjector(
binder -> {
binder.bindConstant().annotatedWith(Names.named("serviceName")).to("druid/test/redis");
binder.bindConstant().annotatedWith(Names.named("servicePort")).to(0);
binder.bindConstant().annotatedWith(Names.named("tlsServicePort")).to(-1);
binder.bind(Validator.class).toInstance(Validation.buildDefaultValidatorFactory().getValidator());
binder.bindScope(LazySingleton.class, Scopes.SINGLETON);
binder.bind(JsonConfigurator.class).in(LazySingleton.class);
binder.bind(Properties.class).toInstance(properties);
JsonConfigProvider.bind(binder, PROPERTY_PREFIX, AWSCredentialsConfig.class);
}
);
final Injector injector = createInjector();
final AWSCredentialsConfig credentialsConfig = injector.getInstance(AWSCredentialsConfig.class);
Assert.assertEquals(filePath, credentialsConfig.getFileSessionCredentials());
Assert.assertEquals(SOME_SECRET, credentialsConfig.getAccessKey().getPassword());
Assert.assertEquals(SOME_SECRET, credentialsConfig.getSecretKey().getPassword());
}
Expand Down

0 comments on commit a805f4b

Please sign in to comment.