Azure / Azure/azure-sdk-for-java
[FEATURE REQ] please make it easier to instantiate objects
- Dominant language
- Java
- Stars
- 2.6k
- Forks
- 2.2k
- Avg merge
- 2d 8h
- Merged PRs (30d)
- 178
Description
**Is your feature request related to a problem? Please describe.**
I writing unit tests for some code which uses the `SecretClient`.
Imagine something like:
```java
SecretClient client = mock(SecretClient.class);
when(client.getSecret("foo")).thenReturn(new KeyVaultSecret("foo", "secret"));
```
But the `KeyVaultSecret` has many properties my application uses, which I need to set, but there are no setters!
I have had to scrap together all sorts of workaround to get to this — here's how it looks:
```java
// 1. create a bundle, some properties have no setter, so you need to serialize from JSON
SecretBundle bundle = SecretBundle.fromJson(createReader(
"""
{
"attributes": {
"created": %d,
"recoveryLevel": "%s",
"enabled": true
}
}
""".formatted(
Instant.now().getLong(MILLI_OF_SECOND),
RECOVERABLE
)));
// 2. set the ID this way also sets the vault, name, and version
bundle.setId("%s/secrets/%s/%s".formatted(vault.replaceAll("/$",""), name, version));
bundle.setValue(new String(Hex.encodeHex(value)));
// 3. use this helper to make the secret from the bundle
KeyVaultSecret secret = createKeyVaultSecret(bundle);
// 4. finally, set some other stuff which are required
secret.getProperties().setExpiresOn(Instant.now().plusSeconds(600).atOffset(ZoneOffset.UTC));
return secret;
```
This is just one example, even harder is generic iterator classes like `PagedIterable` 🥵
**Describe the solution you'd like**
Builder patter would be ideal — eg:
```java
KeyVaultSecret secret = new KeyVaultSecret.Builder()
.vault("https://example.vault.azure.net/")
.name("example")
.version("aaaaa")
.value(v)
.build()
```
Additionally, maybe default `createdOn`, enabled, etc
**Describe alternatives you've considered**
more setters?
**Additional context**
NA
**Information Checklist**
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report
- [x] Description Added
- [x] Expected solution specified
Contributor guide
Assessment
This issue has not been assessed yet.