Use text blocks for formatting JSON in tests

This commit is contained in:
Scott Frederick
2022-11-29 16:38:07 -06:00
parent 9c04c9299e
commit 90ff373955
17 changed files with 248 additions and 248 deletions

View File

@@ -74,24 +74,20 @@ public class CredHubInterpolationTemplateUnitTests {
}
private ServicesData buildVcapServices(String credHubReferenceName) throws IOException {
// @formatter:off
String vcapServices = "{" +
" \"service-offering\": [" +
" {" +
" \"credentials\": {" +
" \"credhub-ref\": \"((" + credHubReferenceName + "))\"" +
" }," +
" \"label\": \"service-offering\"," +
" \"name\": \"service-instance\"," +
" \"plan\": \"standard\"," +
" \"tags\": [" +
" \"cloud-service\"" +
" ]," +
" \"volume_mounts\": []" +
" }" +
" ]" +
"}";
// @formatter:on
String vcapServices = """
{
"service-offering": [{
"credentials": {
"credhub-ref": "((%s))"
},
"label": "service-offering",
"name": "service-instance",
"plan": "standard",
"tags": [ "cloud-service" ],
"volume_mounts": []
}]
}
""".formatted(credHubReferenceName);
ObjectMapper mapper = JsonUtils.buildObjectMapper();
return mapper.readValue(vcapServices, ServicesData.class);

View File

@@ -26,20 +26,20 @@ public class CertificateSummaryDataTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeWithCertificates() {
// @formatter:off
String json = "{\n" +
" \"certificates\": [\n" +
" {\n" +
" \"id\": \"2993f622-cb1e-4e00-a267-4b23c273bf3d\",\n" +
" \"name\": \"/example-certificate-1\"\n" +
" },\n" +
" {\n" +
" \"id\": \"b40d3d3b-2cf5-4a73-babd-9dceefa9b0db\",\n" +
" \"name\": \"/example-certificate-2\"\n" +
" }\n" +
" ]\n" +
"}";
// @formatter:on
String json = """
{
"certificates": [
{
"id": "2993f622-cb1e-4e00-a267-4b23c273bf3d",
"name": "/example-certificate-1"
},
{
"id": "b40d3d3b-2cf5-4a73-babd-9dceefa9b0db",
"name": "/example-certificate-2"
}
]
}
""";
CertificateSummaryData certificates = parseResponse(json, CertificateSummaryData.class);
@@ -52,7 +52,11 @@ public class CertificateSummaryDataTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeWithNoCertificates() {
String json = "{\n" + " \"certificates\": []\n" + "}";
String json = """
{
"certificates": []
}
""";
CertificateSummaryData certificates = parseResponse(json, CertificateSummaryData.class);

View File

@@ -26,24 +26,24 @@ public class CredentialSummaryDataUnitTests extends JsonParsingUnitTestsBase {
@Test
public void deserializationWithCredentials() {
// @formatter:off
String json = "{\n" +
" \"credentials\": [\n" +
" {\n" +
" \"name\": \"/deploy123/example1\",\n" +
" \"version_created_at\": \"" + TEST_DATE_STRING + "\"\n" +
" },\n" +
" {\n" +
" \"name\": \"/deploy123/example2\",\n" +
" \"version_created_at\": \"" + TEST_DATE_STRING + "\"\n" +
" },\n" +
" {\n" +
" \"name\": \"/deploy123/example3\",\n" +
" \"version_created_at\": \"" + TEST_DATE_STRING + "\"\n" +
" }\n" +
" ]\n" +
"}";
// @formatter:on
String json = """
{
"credentials": [
{
"name": "/deploy123/example1",
"version_created_at": "%s"
},
{
"name": "/deploy123/example2",
"version_created_at": "%s"
},
{
"name": "/deploy123/example3",
"version_created_at": "%s"
}
]
}
""".formatted(TEST_DATE_STRING, TEST_DATE_STRING, TEST_DATE_STRING);
CredentialSummaryData response = parseResponse(json, CredentialSummaryData.class);
@@ -56,13 +56,17 @@ public class CredentialSummaryDataUnitTests extends JsonParsingUnitTestsBase {
assertThat(credentials.get(2).getName().getName()).isEqualTo("/deploy123/example3");
for (CredentialSummary credential : credentials) {
assertThat(credential.getVersionCreatedAt()).isEqualTo(testDate);
assertThat(credential.getVersionCreatedAt()).isEqualTo(this.testDate);
}
}
@Test
public void deserializationWithEmptyCredentials() {
String json = "{\n" + " \"credentials\": [\n" + " ]\n" + "}";
String json = """
{
"credentials": []
}
""";
CredentialSummaryData response = parseResponse(json, CredentialSummaryData.class);

View File

@@ -27,22 +27,22 @@ public abstract class JsonParsingUnitTestsBase {
static final String TEST_DATE_STRING = "2017-01-31T11:22:33Z";
// @formatter:off
private static final String CREDENTIAL_DETAIL_TEMPLATE = "{" +
" \"version_created_at\": \"" + TEST_DATE_STRING + "\"," +
" \"id\": \"80cbb13f-7562-4e72-92de-f3ccf69eaa59\"," +
" \"name\": \"/service-broker-name/service-instance-name/binding-id/credentials-json\"," +
" %s" +
"}";
// @formatter:on
private static final String CREDENTIAL_DETAIL_TEMPLATE = """
{
"version_created_at": "%s",
"id": "80cbb13f-7562-4e72-92de-f3ccf69eaa59",
"name": "/service-broker-name/service-instance-name/binding-id/credentials-json",
%s
}
""".formatted(TEST_DATE_STRING, "%s");
// @formatter:off
private static final String CREDENTIAL_DETAILS_DATA_TEMPLATE = "{" +
" \"data\": [" +
CREDENTIAL_DETAIL_TEMPLATE +
" ]" +
"}";
// @formatter:on
private static final String CREDENTIAL_DETAILS_DATA_TEMPLATE = """
{
"data": [
%s
]
}
""".formatted(CREDENTIAL_DETAIL_TEMPLATE);
Date testDate;

View File

@@ -27,15 +27,14 @@ import static org.assertj.core.api.Assertions.assertThat;
public class CertificateCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
// @formatter:off
private static final String CERT_CREDENTIALS =
" \"type\": \"certificate\"," +
" \"value\": {" +
" \"certificate\": \"cert\"," +
" \"ca\": \"authority\"," +
" \"private_key\": \"private-key\"" +
" }";
// @formatter:on
private static final String CERT_CREDENTIALS = """
"type": "certificate",
"value": {
"certificate": "cert",
"ca": "authority",
"private_key": "private-key"
}
""";
@Test
public void deserializeDetailsWithAllValues() {
@@ -46,13 +45,12 @@ public class CertificateCredentialDetailsUnitTests extends JsonParsingUnitTestsB
@Test
public void deserializeDetailsCertOnly() {
// @formatter:off
final String credentials =
" \"type\": \"certificate\"," +
" \"value\": {" +
" \"certificate\": \"cert\"" +
" }";
// @formatter:on
final String credentials = """
"type": "certificate",
"value": {
"certificate": "cert"
}
""";
CredentialDetails<CertificateCredential> data = parseDetails(credentials);
assertDetails(data, "cert", null, null);
@@ -60,14 +58,13 @@ public class CertificateCredentialDetailsUnitTests extends JsonParsingUnitTestsB
@Test
public void deserializeDetailsWithNoCert() {
// @formatter:off
final String credentials =
" \"type\": \"certificate\"," +
" \"value\": {" +
" \"ca\": \"authority\"," +
" \"private_key\": \"private-key\"" +
" }";
// @formatter:on
final String credentials = """
"type": "certificate",
"value": {
"ca": "authority",
"private_key": "private-key"
}
""";
CredentialDetails<CertificateCredential> data = parseDetails(credentials);
assertDetails(data, null, "authority", "private-key");

View File

@@ -26,7 +26,11 @@ public class VersionInfoTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeWithV1() {
String json = "{\n" + " \"version\": \"1.9.0\"\n" + "}";
String json = """
{
"version": "1.9.0"
}
""";
VersionInfo versionInfo = parseResponse(json, VersionInfo.class);
@@ -39,7 +43,11 @@ public class VersionInfoTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeWithV2_0() {
String json = "{\n" + " \"version\": \"2.0.2\"\n" + "}";
String json = """
{
"version": "2.0.2"
}
""";
VersionInfo versionInfo = parseResponse(json, VersionInfo.class);
@@ -52,7 +60,10 @@ public class VersionInfoTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeWithV2_1() {
String json = "{\n" + " \"version\": \"2.1.2\"\n" + "}";
String json = """
{
"version": "2.1.2"
}""";
VersionInfo versionInfo = parseResponse(json, VersionInfo.class);

View File

@@ -27,15 +27,14 @@ import static org.assertj.core.api.Assertions.assertThat;
public class JsonCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
// @formatter:off
private static final String JSON_CREDENTIALS =
" \"type\": \"json\"," +
" \"value\": {" +
" \"client_id\": \"test-id\"," +
" \"client_secret\": \"test-secret\"," +
" \"uri\": \"https://example.com\"" +
" }";
// @formatter:on
private static final String JSON_CREDENTIALS = """
"type": "json",
"value": {
"client_id": "test-id",
"client_secret": "test-secret",
"uri": "https://example.com"
}
""";
@Test
public void deserializeDetails() {

View File

@@ -27,7 +27,10 @@ import static org.assertj.core.api.Assertions.assertThat;
public class PasswordCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
private static final String PASSWORD_CREDENTIALS = " \"type\": \"password\"," + " \"value\": \"secret\"";
private static final String PASSWORD_CREDENTIALS = """
"type": "password",
"value": "secret"
""";
@Test
public void deserializeDetails() {

View File

@@ -33,14 +33,14 @@ public class CredentialPermissionUnitTests extends JsonParsingUnitTestsBase {
@Test
public void deserializePermission() {
// @formatter:off
String json = "{" +
"\"uuid\": \"uuid\",\n" +
"\"path\": \"/example-directory/example-specific-credential\",\n" +
"\"actor\": \"uaa-user:106f52e2-5d01-4675-8d7a-c05ff9a2c081\",\n" +
"\"operations\": [\"read\",\"write\"]" +
"}";
// @formatter:on
String json = """
{
"uuid": "uuid",
"path": "/example-directory/example-specific-credential",
"actor": "uaa-user:106f52e2-5d01-4675-8d7a-c05ff9a2c081",
"operations": ["read","write"]
}
""";
CredentialPermission credentialPermission = parsePermission(json);

View File

@@ -33,27 +33,27 @@ public class CredentialPermissionsUnitTests extends JsonParsingUnitTestsBase {
@Test
public void deserializePermissions() {
// @formatter:off
String json = "{\"credential_name\": \"/c/example\"," +
"\"permissions\": [" +
" {" +
" \"actor\": \"mtls-app:appid1\"," +
" \"operations\": [" +
" \"read\"," +
" \"write\"," +
" \"delete\"," +
" \"read_acl\"," +
" \"write_acl\"" +
" ]" +
" }," +
" {" +
" \"actor\": \"uaa-user:zone1/userid\"," +
" \"operations\": [" +
" \"read\"" +
" ]" +
" }" +
" ]}";
// @formatter:on
String json = """
{
"credential_name": "/c/example",
"permissions": [
{
"actor": "mtls-app:appid1",
"operations": [
"read",
"write",
"delete",
"read_acl",
"write_acl"
]
},
{
"actor": "uaa-user:zone1/userid",
"operations": [ "read" ]
}
]
}
""";
CredentialPermissions permissions = parsePermissions(json);
@@ -80,7 +80,11 @@ public class CredentialPermissionsUnitTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeWithNoPermissions() {
String json = "{\"permissions\": []}";
String json = """
{
"permissions": []
}
""";
CredentialPermissions permissions = parsePermissions(json);

View File

@@ -27,14 +27,13 @@ import static org.assertj.core.api.Assertions.assertThat;
public class RsaCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
// @formatter:off
private static final String RSA_CREDENTIALS =
" \"type\": \"rsa\"," +
" \"value\": {" +
" \"private_key\": \"private-key\"," +
" \"public_key\": \"public-key\"" +
" }";
// @formatter:on
private static final String RSA_CREDENTIALS = """
"type": "rsa",
"value": {
"private_key": "private-key",
"public_key": "public-key"
}
""";
@Test
public void deserializeDetailsWithPublicAndPrivateKeys() {
@@ -45,13 +44,12 @@ public class RsaCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeDetailsWithPublicKey() {
// @formatter:off
final String credentials =
" \"type\": \"rsa\"," +
" \"value\": {" +
" \"public_key\": \"public-key\"" +
" }";
// @formatter:on
final String credentials = """
"type": "rsa",
"value": {
"public_key": "public-key"
}
""";
CredentialDetails<RsaCredential> data = parseDetails(credentials);
assertDetails(data, "public-key", null);
@@ -59,13 +57,12 @@ public class RsaCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeDetailsWithPrivateKey() {
// @formatter:off
final String credentials =
" \"type\": \"rsa\"," +
" \"value\": {" +
" \"private_key\": \"private-key\"" +
" }";
// @formatter:on
final String credentials = """
"type": "rsa",
"value": {
"private_key": "private-key"
}
""";
CredentialDetails<RsaCredential> data = parseDetails(credentials);
assertDetails(data, null, "private-key");

View File

@@ -27,15 +27,14 @@ import static org.assertj.core.api.Assertions.assertThat;
public class SshCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
// @formatter:off
private static final String SSH_CREDENTIALS =
" \"type\": \"ssh\"," +
" \"value\": {" +
" \"private_key\": \"private-key\"," +
" \"public_key\": \"public-key\"," +
" \"public_key_fingerprint\": \"fingerprint\"" +
" }";
// @formatter:on
private static final String SSH_CREDENTIALS = """
"type": "ssh",
"value": {
"private_key": "private-key",
"public_key": "public-key",
"public_key_fingerprint": "fingerprint"
}
""";
@Test
public void deserializeDetailsWithPublicAndPrivateKeys() {
@@ -46,13 +45,12 @@ public class SshCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeDetailsWithPublicKey() {
// @formatter:off
final String credentials =
" \"type\": \"ssh\"," +
" \"value\": {" +
" \"public_key\": \"public-key\"" +
" }";
// @formatter:on
final String credentials = """
"type": "ssh",
"value": {
"public_key": "public-key"
}
""";
CredentialDetails<SshCredential> data = parseDetails(credentials);
assertDetails(data, "public-key", null, null);
@@ -60,13 +58,12 @@ public class SshCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
@Test
public void deserializeDetailsWithPrivateKey() {
// @formatter:off
final String credentials =
" \"type\": \"ssh\"," +
" \"value\": {" +
" \"private_key\": \"private-key\"" +
" }";
// @formatter:on
final String credentials = """
"type": "ssh",
"value": {
"private_key": "private-key"
}
""";
CredentialDetails<SshCredential> data = parseDetails(credentials);
assertDetails(data, null, "private-key", null);

View File

@@ -27,15 +27,14 @@ import static org.assertj.core.api.Assertions.assertThat;
public class UserCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
// @formatter:off
private static final String USER_CREDENTIALS =
" \"type\": \"user\"," +
" \"value\": {" +
" \"username\": \"myname\"," +
" \"password\": \"secret\"," +
" \"password_hash\": \"secret-hash\"" +
" }";
// @formatter:on
private static final String USER_CREDENTIALS = """
"type": "user",
"value": {
"username": "myname",
"password": "secret",
"password_hash": "secret-hash"
}
""";
@Test
public void deserializeDetails() {

View File

@@ -27,11 +27,10 @@ import static org.assertj.core.api.Assertions.assertThat;
public class ValueCredentialDetailsUnitTests extends JsonParsingUnitTestsBase {
// @formatter:off
private static final String VALUE_CREDENTIALS =
" \"type\": \"value\"," +
" \"value\": \"somevalue\"";
// @formatter:on
private static final String VALUE_CREDENTIALS = """
"type": "value",
"value": "somevalue"
""";
@Test
public void deserializeDetails() {

View File

@@ -196,24 +196,22 @@ public class CredHubDemoController {
}
private ServicesData buildServicesData(String credHubReferenceName) throws IOException {
// @formatter:off
String vcapServices = "{" +
" \"service-offering\": [" +
" {" +
" \"credentials\": {" +
" \"credhub-ref\": \"((" + credHubReferenceName + "))\"" +
" }," +
" \"label\": \"service-offering\"," +
" \"name\": \"service-instance\"," +
" \"plan\": \"standard\"," +
" \"tags\": [" +
" \"cloud-service\"" +
" ]," +
" \"volume_mounts\": []" +
" }" +
" ]" +
"}";
// @formatter:on
String vcapServices = """
{
"service-offering": [
{
"credentials": {
"credhub-ref": "((%s))"
},
"label": "service-offering",
"name": "service-instance",
"plan": "standard",
"tags": [ "cloud-service" ],
"volume_mounts": []
}
]
}
""".formatted(credHubReferenceName);
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(vcapServices, ServicesData.class);

View File

@@ -88,24 +88,20 @@ public class InterpolationIntegrationTests extends CredHubIntegrationTests {
}
private ServicesData buildVcapServices(String credHubReferenceName) throws IOException {
// @formatter:off
String vcapServices = "{" +
" \"service-offering\": [" +
" {" +
" \"credentials\": {" +
" \"credhub-ref\": \"((" + credHubReferenceName + "))\"" +
" }," +
" \"label\": \"service-offering\"," +
" \"name\": \"service-instance\"," +
" \"plan\": \"standard\"," +
" \"tags\": [" +
" \"cloud-service\"" +
" ]," +
" \"volume_mounts\": []" +
" }" +
" ]" +
"}";
// @formatter:on
String vcapServices = """
{
"service-offering": [{
"credentials": {
"credhub-ref": "((%s))"
},
"label": "service-offering",
"name": "service-instance",
"plan": "standard",
"tags": [ "cloud-service" ],
"volume_mounts": []
}]
}
""".formatted(credHubReferenceName);
ObjectMapper mapper = JsonUtils.buildObjectMapper();
return mapper.readValue(vcapServices, ServicesData.class);

View File

@@ -91,24 +91,20 @@ public class ReactiveInterpolationIntegrationTests extends ReactiveCredHubIntegr
}
private ServicesData buildVcapServices(String credHubReferenceName) throws IOException {
// @formatter:off
String vcapServices = "{" +
" \"service-offering\": [" +
" {" +
" \"credentials\": {" +
" \"credhub-ref\": \"((" + credHubReferenceName + "))\"" +
" }," +
" \"label\": \"service-offering\"," +
" \"name\": \"service-instance\"," +
" \"plan\": \"standard\"," +
" \"tags\": [" +
" \"cloud-service\"" +
" ]," +
" \"volume_mounts\": []" +
" }" +
" ]" +
"}";
// @formatter:on
String vcapServices = """
{
"service-offering": [{
"credentials": {
"credhub-ref": "((%s))"
},
"label": "service-offering",
"name": "service-instance",
"plan": "standard",
"tags": [ "cloud-service" ],
"volume_mounts": []
}]
}
""".formatted(credHubReferenceName);
ObjectMapper mapper = JsonUtils.buildObjectMapper();
return mapper.readValue(vcapServices, ServicesData.class);