diff --git a/build.gradle b/build.gradle index af24bbe..d11c857 100644 --- a/build.gradle +++ b/build.gradle @@ -33,6 +33,7 @@ ext { junitVersion = "4.12" mockitoVersion = "2.7.22" + assertJVersion = "3.6.1" javadocLinks = [ 'http://docs.oracle.com/javase/7/docs/api/', diff --git a/spring-credhub-cloud-connector/build.gradle b/spring-credhub-cloud-connector/build.gradle index aa08ed0..512cdfd 100644 --- a/spring-credhub-cloud-connector/build.gradle +++ b/spring-credhub-cloud-connector/build.gradle @@ -25,4 +25,5 @@ dependencies { testCompile group: 'org.springframework', name: 'spring-test', version: "${springVersion}" testCompile group: 'junit', name: 'junit', version: "${junitVersion}" testCompile group: 'org.mockito', name: 'mockito-core', version: "${mockitoVersion}" + testCompile group: 'org.assertj', name: 'assertj-core', version: "${assertJVersion}" } diff --git a/spring-credhub-cloud-connector/src/test/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessorTests.java b/spring-credhub-cloud-connector/src/test/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessorTests.java index df11683..8ccf0bc 100644 --- a/spring-credhub-cloud-connector/src/test/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessorTests.java +++ b/spring-credhub-cloud-connector/src/test/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessorTests.java @@ -21,9 +21,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.hamcrest.Matcher; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentMatcher; @@ -36,11 +33,10 @@ import org.springframework.credhub.core.CredHubInterpolationOperations; import org.springframework.credhub.support.ServicesData; import org.springframework.http.HttpStatus; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertThat; import static org.mockito.ArgumentMatchers.argThat; import static org.mockito.Mockito.verifyZeroInteractions; import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.assertThat; @RunWith(MockitoJUnitRunner.class) public class CredHubInterpolationServiceDataPostProcessorTests { @@ -59,7 +55,7 @@ public class CredHubInterpolationServiceDataPostProcessorTests { new CredHubInterpolationServiceDataPostProcessor(credHubOperations); CloudFoundryRawServiceData actual = processor.process(rawServiceData); - assertThat(actual, matchesContent(interpolatedServiceData)); + assertThat(actual).isEqualTo(interpolatedServiceData); } @Test @@ -73,7 +69,7 @@ public class CredHubInterpolationServiceDataPostProcessorTests { new CredHubInterpolationServiceDataPostProcessor(credHubOperations); CloudFoundryRawServiceData actual = processor.process(rawServiceData); - assertThat(actual, equalTo(rawServiceData)); + assertThat(actual).isEqualTo(rawServiceData); } @Test @@ -95,24 +91,6 @@ public class CredHubInterpolationServiceDataPostProcessorTests { }; } - private Matcher matchesContent(final ServicesData expected) { - return new BaseMatcher() { - @Override - @SuppressWarnings("unchecked") - public boolean matches(Object actual) { - return mapsAreEquivalent((Map) actual, expected); - } - - @Override - public void describeMismatch(Object item, Description mismatchDescription) { - } - - @Override - public void describeTo(Description description) { - } - }; - } - private boolean mapsAreEquivalent(Map actual, Map expected) { return expected.equals(actual); } diff --git a/spring-credhub-core/build.gradle b/spring-credhub-core/build.gradle index 48fef17..41f3119 100644 --- a/spring-credhub-core/build.gradle +++ b/spring-credhub-core/build.gradle @@ -32,6 +32,6 @@ dependencies { testCompile group: 'org.springframework', name: 'spring-test', version: "${springVersion}" testCompile group: 'junit', name: 'junit', version: "${junitVersion}" testCompile group: 'org.mockito', name: 'mockito-core', version: "${mockitoVersion}" - testCompile group: 'org.assertj', name: 'assertj-core', version: '3.6.1' - testCompile group: 'org.valid4j', name: 'json-path-matchers', version: '1.1' + testCompile group: 'org.assertj', name: 'assertj-core', version: "${assertJVersion}" + testCompile group: 'com.jayway.jsonpath', name: 'json-path', version: '2.4.0' } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/configuration/ClientHttpRequestFactoryFactoryTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/configuration/ClientHttpRequestFactoryFactoryTests.java index c1ff58f..e73b7ff 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/configuration/ClientHttpRequestFactoryFactoryTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/configuration/ClientHttpRequestFactoryFactoryTests.java @@ -28,8 +28,7 @@ import org.springframework.http.client.Netty4ClientHttpRequestFactory; import org.springframework.http.client.OkHttp3ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; -import static org.hamcrest.Matchers.instanceOf; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.credhub.configuration.ClientHttpRequestFactoryFactory.HttpComponents.usingHttpComponents; import static org.springframework.credhub.configuration.ClientHttpRequestFactoryFactory.HttpURLConnection.usingJdk; import static org.springframework.credhub.configuration.ClientHttpRequestFactoryFactory.Netty.usingNetty; @@ -37,21 +36,21 @@ import static org.springframework.credhub.configuration.ClientHttpRequestFactory public class ClientHttpRequestFactoryFactoryTests { @Test - public void jdkDefaultClientCreated() throws Exception { + public void jdkDefaultClientCreated() { ClientHttpRequestFactory factory = usingJdk(new ClientOptions()); - assertThat(factory, instanceOf(SimpleClientHttpRequestFactory.class)); + assertThat(factory).isInstanceOf(SimpleClientHttpRequestFactory.class); } @Test public void httpComponentsClientCreated() throws Exception { ClientHttpRequestFactory factory = usingHttpComponents(new ClientOptions()); - assertThat(factory, instanceOf(HttpComponentsClientHttpRequestFactory.class)); + assertThat(factory).isInstanceOf(HttpComponentsClientHttpRequestFactory.class); HttpClient httpClient = ((HttpComponentsClientHttpRequestFactory) factory).getHttpClient(); - assertThat(httpClient, instanceOf(CloseableHttpClient.class)); + assertThat(httpClient).isInstanceOf(CloseableHttpClient.class); ((DisposableBean) factory).destroy(); } @@ -60,7 +59,7 @@ public class ClientHttpRequestFactoryFactoryTests { public void okHttp3ClientCreated() throws Exception { ClientHttpRequestFactory factory = usingOkHttp3(new ClientOptions()); - assertThat(factory, instanceOf(OkHttp3ClientHttpRequestFactory.class)); + assertThat(factory).isInstanceOf(OkHttp3ClientHttpRequestFactory.class); ((DisposableBean) factory).destroy(); } @@ -69,7 +68,7 @@ public class ClientHttpRequestFactoryFactoryTests { public void nettyClientCreated() throws Exception { ClientHttpRequestFactory factory = usingNetty(new ClientOptions()); - assertThat(factory, instanceOf(Netty4ClientHttpRequestFactory.class)); + assertThat(factory).isInstanceOf(Netty4ClientHttpRequestFactory.class); ((DisposableBean) factory).destroy(); } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubClientUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubClientUnitTests.java index aab49a3..ec14c04 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubClientUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubClientUnitTests.java @@ -27,9 +27,7 @@ import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; import org.springframework.web.util.AbstractUriTemplateHandler; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.instanceOf; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; @RunWith(MockitoJUnitRunner.class) public class CredHubClientUnitTests { @@ -43,11 +41,11 @@ public class CredHubClientUnitTests { RestTemplate restTemplate = CredHubClient.createRestTemplate(CREDHUB_URI, clientHttpRequestFactory); - assertThat(restTemplate.getUriTemplateHandler(), - instanceOf(AbstractUriTemplateHandler.class)); + assertThat(restTemplate.getUriTemplateHandler()) + .isInstanceOf(AbstractUriTemplateHandler.class); AbstractUriTemplateHandler uriTemplateHandler = (AbstractUriTemplateHandler) restTemplate .getUriTemplateHandler(); - assertThat(uriTemplateHandler.getBaseUrl(), equalTo(CREDHUB_URI)); + assertThat(uriTemplateHandler.getBaseUrl()).isEqualTo(CREDHUB_URI); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateSummaryUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateSummaryUnitTests.java index 8fa5127..a16a7dc 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateSummaryUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateSummaryUnitTests.java @@ -28,11 +28,8 @@ import org.springframework.credhub.support.CredentialSummary; import org.springframework.credhub.support.CredentialSummaryData; import org.springframework.http.ResponseEntity; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; import static org.mockito.Mockito.when; import static org.springframework.credhub.core.CredHubCredentialsTemplate.NAME_LIKE_URL_QUERY; import static org.springframework.credhub.core.CredHubCredentialsTemplate.PATH_URL_QUERY; @@ -61,7 +58,7 @@ public class CredHubCredentialsTemplateSummaryUnitTests extends CredHubCredentia fail("Exception should have been thrown"); } catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); + assertThat(e.getMessage()).contains(expectedResponse.getStatusCode().toString()); } } else { @@ -83,7 +80,7 @@ public class CredHubCredentialsTemplateSummaryUnitTests extends CredHubCredentia fail("Exception should have been thrown"); } catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); + assertThat(e.getMessage()).contains(expectedResponse.getStatusCode().toString()); } } else { @@ -96,8 +93,8 @@ public class CredHubCredentialsTemplateSummaryUnitTests extends CredHubCredentia private void assertResponseContainsExpectedCredentials( ResponseEntity expectedResponse, List response) { - assertThat(response, notNullValue()); - assertThat(response.size(), equalTo(expectedResponse.getBody().getCredentials().size())); - assertThat(response.get(0), equalTo(expectedResponse.getBody().getCredentials().get(0))); + assertThat(response).isNotNull(); + assertThat(response.size()).isEqualTo(expectedResponse.getBody().getCredentials().size()); + assertThat(response).contains(expectedResponse.getBody().getCredentials().get(0)); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubInterpolationTemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubInterpolationTemplateUnitTests.java index 0ade9fa..b29883e 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubInterpolationTemplateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubInterpolationTemplateUnitTests.java @@ -35,8 +35,7 @@ import org.springframework.web.client.RestTemplate; import java.io.IOException; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; import static org.springframework.credhub.core.CredHubInterpolationTemplate.INTERPOLATE_URL_PATH; import static org.springframework.http.HttpMethod.POST; @@ -76,7 +75,7 @@ public class CredHubInterpolationTemplateUnitTests { ServicesData response = credHubTemplate.interpolateServiceData(vcapServices); - assertThat(response, equalTo(expectedResponse)); + assertThat(response).isEqualTo(expectedResponse); } private ServicesData buildVcapServices(String credHubReferenceName) throws IOException { diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubPermissionsTemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubPermissionsTemplateUnitTests.java index bb8c7d6..6bb7493 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubPermissionsTemplateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubPermissionsTemplateUnitTests.java @@ -33,9 +33,7 @@ import org.springframework.web.client.RestTemplate; import java.util.List; -import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static org.springframework.credhub.core.CredHubPermissionsTemplate.PERMISSIONS_ACTOR_URL_QUERY; @@ -80,9 +78,9 @@ public class CredHubPermissionsTemplateUnitTests { List response = credHubTemplate.getPermissions(NAME); - assertNotNull(response); - assertThat(response.size(), equalTo(expectedResponse.getPermissions().size())); - assertThat(response, equalTo(expectedResponse.getPermissions())); + assertThat(response).isNotNull(); + assertThat(response).hasSize(expectedResponse.getPermissions().size()); + assertThat(response).isEqualTo(expectedResponse.getPermissions()); } @Test @@ -108,9 +106,9 @@ public class CredHubPermissionsTemplateUnitTests { List response = credHubTemplate.addPermissions(NAME, permission1, permission2); - assertNotNull(response); - assertThat(response.size(), equalTo(expectedResponse.getPermissions().size())); - assertThat(response, equalTo(expectedResponse.getPermissions())); + assertThat(response).isNotNull(); + assertThat(response).hasSize(expectedResponse.getPermissions().size()); + assertThat(response).isEqualTo(expectedResponse.getPermissions()); } @Test diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java index ef67b84..6ce537b 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java @@ -31,11 +31,8 @@ import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.fail; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.isNull; @@ -97,7 +94,7 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCr fail("Exception should have been thrown"); } catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); + assertThat(e.getMessage()).contains(expectedResponse.getStatusCode().toString()); } } else { @@ -120,7 +117,7 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCr fail("Exception should have been thrown"); } catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); + assertThat(e.getMessage()).contains(expectedResponse.getStatusCode().toString()); } } else { @@ -145,7 +142,7 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCr fail("Exception should have been thrown"); } catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); + assertThat(e.getMessage()).contains(expectedResponse.getStatusCode().toString()); } } else { @@ -167,7 +164,7 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCr fail("Exception should have been thrown"); } catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); + assertThat(e.getMessage()).contains(expectedResponse.getStatusCode().toString()); } } else { @@ -190,8 +187,7 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCr fail("Exception should have been thrown"); } catch (CredHubException e) { - assertThat(e.getMessage(), - containsString(expectedResponse.getStatusCode().toString())); + assertThat(e.getMessage()).contains(expectedResponse.getStatusCode().toString()); } } else { @@ -213,8 +209,7 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCr fail("Exception should have been thrown"); } catch (CredHubException e) { - assertThat(e.getMessage(), - containsString(expectedResponse.getStatusCode().toString())); + assertThat(e.getMessage()).contains(expectedResponse.getStatusCode().toString()); } } else { @@ -236,8 +231,7 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCr fail("Exception should have been thrown"); } catch (CredHubException e) { - assertThat(e.getMessage(), - containsString(expectedResponse.getStatusCode().toString())); + assertThat(e.getMessage()).contains(expectedResponse.getStatusCode().toString()); } } else { @@ -250,23 +244,22 @@ public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCr private void assertDataResponseContainsExpectedCredentials( ResponseEntity> expectedResponse, List> response) { - assertThat(response, notNullValue()); - assertThat(response.size(), equalTo(expectedResponse.getBody().getData().size())); - assertThat(response.get(0), equalTo(expectedResponse.getBody().getData().get(0))); + assertThat(response).isNotNull(); + assertThat(response).hasSize(expectedResponse.getBody().getData().size()); + assertThat(response).contains(expectedResponse.getBody().getData().get(0)); } private void assertDataResponseContainsExpectedCredential( ResponseEntity> expectedResponse, CredentialDetails response) { - assertThat(response, notNullValue()); - assertThat(1, equalTo(expectedResponse.getBody().getData().size())); - assertThat(response, equalTo(expectedResponse.getBody().getData().get(0))); + assertThat(response).isNotNull(); + assertThat(response).isEqualTo(expectedResponse.getBody().getData().get(0)); } private void assertDetailsResponseContainsExpectedCredential( ResponseEntity> expectedResponse, CredentialDetails response) { - assertThat(response, notNullValue()); - assertThat(response, equalTo(expectedResponse.getBody())); + assertThat(response).isNotNull(); + assertThat(response).isEqualTo(expectedResponse.getBody()); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CertificateSummaryDataTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CertificateSummaryDataTests.java index 11a808a..21ee3dd 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CertificateSummaryDataTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CertificateSummaryDataTests.java @@ -18,13 +18,11 @@ package org.springframework.credhub.support; import org.junit.Test; -import java.io.IOException; - import static org.assertj.core.api.Assertions.assertThat; public class CertificateSummaryDataTests extends JsonParsingUnitTestsBase { @Test - public void deserializeWithCertificates() throws IOException { + public void deserializeWithCertificates() { String json = "{\n" + " \"certificates\": [\n" + " {\n" + @@ -38,7 +36,7 @@ public class CertificateSummaryDataTests extends JsonParsingUnitTestsBase { " ]\n" + "}"; - CertificateSummaryData certificates = parseResponse(json); + CertificateSummaryData certificates = parseResponse(json, CertificateSummaryData.class); assertThat(certificates.getCertificates().size()).isEqualTo(2); assertThat(certificates.getCertificates()).extracting("id") @@ -48,17 +46,13 @@ public class CertificateSummaryDataTests extends JsonParsingUnitTestsBase { } @Test - public void deserializeWithNoCertificates() throws IOException { + public void deserializeWithNoCertificates() { String json = "{\n" + " \"certificates\": []\n" + "}"; - CertificateSummaryData certificates = parseResponse(json); + CertificateSummaryData certificates = parseResponse(json, CertificateSummaryData.class); assertThat(certificates.getCertificates().size()).isEqualTo(0); } - - private CertificateSummaryData parseResponse(String json) throws IOException { - return objectMapper.readValue(json, CertificateSummaryData.class); - } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredHubRequestUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredHubRequestUnitTestsBase.java index 70ea46c..f8b569b 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredHubRequestUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredHubRequestUnitTestsBase.java @@ -16,56 +16,42 @@ package org.springframework.credhub.support; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Before; +import com.jayway.jsonpath.DocumentContext; import org.junit.Test; import org.springframework.credhub.support.CredHubRequest.CredHubRequestBuilder; import org.springframework.credhub.support.permissions.Actor; import org.springframework.credhub.support.permissions.CredentialPermission; -import org.springframework.credhub.support.utils.JsonUtils; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; + import static org.springframework.credhub.support.permissions.Operation.READ; import static org.springframework.credhub.support.permissions.Operation.READ_ACL; import static org.springframework.credhub.support.permissions.Operation.WRITE; import static org.springframework.credhub.support.permissions.Operation.WRITE_ACL; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.isJson; public abstract class CredHubRequestUnitTestsBase { - private ObjectMapper mapper; protected CredHubRequestBuilder requestBuilder; - @Before - public void setUpCredentialRequestUnitTests() { - mapper = JsonUtils.buildObjectMapper(); - } - @Test - public void serializationWithOnePermission() throws Exception { + public void serializationWithOnePermission() { requestBuilder .permission(CredentialPermission.builder() .app("app-id") .operation(READ) .build()); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = JsonTestUtils.toJsonPath(requestBuilder.build()); - assertThat(jsonValue, - allOf(hasJsonPath("$.additional_permissions[0].actor", - equalTo(Actor.app("app-id").getIdentity())), - hasJsonPath("$.additional_permissions[0].operations[0]", - equalTo("read")))); + assertThat(json).hasPath("$.additional_permissions[0].actor") + .isEqualTo(Actor.app("app-id").getIdentity()); + assertThat(json).hasPath("$.additional_permissions[0].operations[0]") + .isEqualTo("read"); } @Test @SuppressWarnings("unchecked") - public void serializationWithThreePermissions() throws Exception { + public void serializationWithThreePermissions() { requestBuilder .permission(CredentialPermission.builder() .app("app-id") @@ -80,45 +66,47 @@ public abstract class CredHubRequestUnitTestsBase { .operations(READ, WRITE, READ_ACL, WRITE_ACL) .build()); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = JsonTestUtils.toJsonPath(requestBuilder.build()); - assertThat(jsonValue, allOf( - hasJsonPath("$.additional_permissions[0].actor", - equalTo(Actor.app("app-id").getIdentity())), - hasJsonPath("$.additional_permissions[0].operations[0]", equalTo("read")), - hasJsonPath("$.additional_permissions[0].operations[1]", equalTo("write")), + assertThat(json).hasPath("$.additional_permissions[0].actor") + .isEqualTo(Actor.app("app-id").getIdentity()); + assertThat(json).hasPath("$.additional_permissions[0].operations[0]") + .isEqualTo("read"); + assertThat(json).hasPath("$.additional_permissions[0].operations[1]") + .isEqualTo("write"); - hasJsonPath("$.additional_permissions[1].actor", - equalTo(Actor.user("zone1", "user-id").getIdentity())), - hasJsonPath("$.additional_permissions[1].operations[0]", equalTo("read_acl")), - hasJsonPath("$.additional_permissions[1].operations[1]", equalTo("write_acl")), + assertThat(json).hasPath("$.additional_permissions[1].actor") + .isEqualTo(Actor.user("zone1", "user-id").getIdentity()); + assertThat(json).hasPath("$.additional_permissions[1].operations[0]") + .isEqualTo("read_acl"); + assertThat(json).hasPath("$.additional_permissions[1].operations[1]") + .isEqualTo("write_acl"); - hasJsonPath("$.additional_permissions[2].actor", - equalTo(Actor.client("client-id").getIdentity())), - hasJsonPath("$.additional_permissions[2].operations[0]", equalTo("read")), - hasJsonPath("$.additional_permissions[2].operations[1]", equalTo("write")), - hasJsonPath("$.additional_permissions[2].operations[2]", equalTo("read_acl")), - hasJsonPath("$.additional_permissions[2].operations[3]", equalTo("write_acl")) - ) - ); + assertThat(json).hasPath("$.additional_permissions[2].actor") + .isEqualTo(Actor.client("client-id").getIdentity()); + assertThat(json).hasPath("$.additional_permissions[2].operations[0]") + .isEqualTo("read"); + assertThat(json).hasPath("$.additional_permissions[2].operations[1]") + .isEqualTo("write"); + assertThat(json).hasPath("$.additional_permissions[2].operations[2]") + .isEqualTo("read_acl"); + assertThat(json).hasPath("$.additional_permissions[2].operations[3]") + .isEqualTo("write_acl"); } - protected String serializeToJson(T requestBuilder) - throws JsonProcessingException { - String jsonValue = mapper.writeValueAsString(requestBuilder.build()); - assertThat(jsonValue, isJson()); - return jsonValue; + protected DocumentContext toJsonPath(CredHubRequestBuilder requestBuilder) { + return JsonTestUtils.toJsonPath(requestBuilder.build()); } - protected void assertCommonRequestFields(String jsonValue, boolean overwrite, WriteMode writeMode, String name, String type) { - assertThat(jsonValue, - allOf(hasJsonPath("$.overwrite", equalTo(overwrite)), - hasJsonPath("$.mode", equalTo(writeMode.getMode())), - hasJsonPath("$.name", equalTo(name)), - hasJsonPath("$.type", equalTo(type)))); + protected void assertCommonRequestFields(DocumentContext json, boolean overwrite, WriteMode writeMode, + String name, String type) { + assertThat(json).hasPath("$.overwrite").isEqualTo(overwrite); + assertThat(json).hasPath("$.mode").isEqualTo(writeMode.getMode()); + assertThat(json).hasPath("$.name").isEqualTo(name); + assertThat(json).hasPath("$.type").isEqualTo(type); } - protected void assertNoPermissions(String jsonValue) { - assertThat(jsonValue, hasNoJsonPath("$.additional_permissions")); + protected void assertNoPermissions(DocumentContext json) { + assertThat(json).hasNoPath("$.additional_permissions"); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialPathDataTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialPathDataTests.java index d951d76..fb02d67 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialPathDataTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialPathDataTests.java @@ -2,13 +2,11 @@ package org.springframework.credhub.support; import org.junit.Test; -import java.io.IOException; - import static org.assertj.core.api.Assertions.assertThat; public class CredentialPathDataTests extends JsonParsingUnitTestsBase { @Test - public void deserializeWithPaths() throws IOException { + public void deserializeWithPaths() { String json = "{\n" + " \"paths\": [\n" + " {\n" + @@ -29,7 +27,7 @@ public class CredentialPathDataTests extends JsonParsingUnitTestsBase { " ]\n" + "}"; - CredentialPathData paths = parseResponse(json); + CredentialPathData paths = parseResponse(json, CredentialPathData.class); assertThat(paths.getPaths().size()).isEqualTo(5); assertThat(paths.getPaths()).extracting("path") @@ -37,17 +35,13 @@ public class CredentialPathDataTests extends JsonParsingUnitTestsBase { } @Test - public void deserializeWithNoPaths() throws IOException { + public void deserializeWithNoPaths() { String json = "{\n" + " \"paths\": []" + "}"; - CredentialPathData paths = parseResponse(json); + CredentialPathData paths = parseResponse(json, CredentialPathData.class); assertThat(paths.getPaths().size()).isEqualTo(0); } - - private CredentialPathData parseResponse(String json) throws java.io.IOException { - return objectMapper.readValue(json, CredentialPathData.class); - } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialSummaryDataUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialSummaryDataUnitTests.java index 329d515..f399c9a 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialSummaryDataUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialSummaryDataUnitTests.java @@ -20,13 +20,11 @@ import java.util.List; import org.junit.Test; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.hamcrest.CoreMatchers.notNullValue; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class CredentialSummaryDataUnitTests extends JsonParsingUnitTestsBase { @Test - public void deserializationWithCredentials() throws Exception { + public void deserializationWithCredentials() { String json = "{\n" + " \"credentials\": [\n" + " {\n" + @@ -44,38 +42,34 @@ public class CredentialSummaryDataUnitTests extends JsonParsingUnitTestsBase { " ]\n" + "}"; - CredentialSummaryData response = parseResponse(json); + CredentialSummaryData response = parseResponse(json, CredentialSummaryData.class); - assertThat(response.getCredentials().size(), equalTo(3)); + assertThat(response.getCredentials().size()).isEqualTo(3); List credentials = response.getCredentials(); - assertThat(credentials.get(0).getName().getName(), - equalTo("/deploy123/example1")); - assertThat(credentials.get(1).getName().getName(), - equalTo("/deploy123/example2")); - assertThat(credentials.get(2).getName().getName(), - equalTo("/deploy123/example3")); + assertThat(credentials.get(0).getName().getName()) + .isEqualTo("/deploy123/example1"); + assertThat(credentials.get(1).getName().getName()) + .isEqualTo("/deploy123/example2"); + assertThat(credentials.get(2).getName().getName()) + .isEqualTo("/deploy123/example3"); for (CredentialSummary credential : credentials) { - assertThat(credential.getVersionCreatedAt(), equalTo(testDate)); + assertThat(credential.getVersionCreatedAt()).isEqualTo(testDate); } } @Test - public void deserializationWithEmptyCredentials() throws Exception { + public void deserializationWithEmptyCredentials() { String json = "{\n" + " \"credentials\": [\n" + " ]\n" + "}"; - CredentialSummaryData response = parseResponse(json); + CredentialSummaryData response = parseResponse(json, CredentialSummaryData.class); - assertThat(response.getCredentials(), notNullValue()); - assertThat(response.getCredentials().size(), equalTo(0)); - } - - private CredentialSummaryData parseResponse(String json) throws java.io.IOException { - return objectMapper.readValue(json, CredentialSummaryData.class); + assertThat(response.getCredentials()).isNotNull(); + assertThat(response.getCredentials()).hasSize(0); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonParsingUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonParsingUnitTestsBase.java index 0e871f7..34835f8 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonParsingUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonParsingUnitTestsBase.java @@ -18,51 +18,48 @@ package org.springframework.credhub.support; import java.util.Date; -import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.util.ISO8601DateFormat; import org.junit.Before; -import org.springframework.credhub.support.utils.JsonUtils; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public abstract class JsonParsingUnitTestsBase { - protected static final String TEST_DATE_STRING = "2017-01-31T11:22:33Z"; + static final String TEST_DATE_STRING = "2017-01-31T11:22:33Z"; - protected final String CREDENTIAL_DETAIL_TEMPLATE = "{" + + private 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" + "}"; - protected final String CREDENTIAL_DETAILS_DATA_TEMPLATE = "{" + + private final String CREDENTIAL_DETAILS_DATA_TEMPLATE = "{" + " \"data\": [" + CREDENTIAL_DETAIL_TEMPLATE + " ]" + "}"; - protected ObjectMapper objectMapper; - protected Date testDate; + Date testDate; @Before public void setUpJsonParsing() throws Exception { - objectMapper = JsonUtils.buildObjectMapper(); - testDate = new ISO8601DateFormat().parse(TEST_DATE_STRING); } - - @SuppressWarnings("unchecked") - protected CredentialDetails parseDetails(String credentials) throws java.io.IOException { - String json = buildDetails(credentials); - return (CredentialDetails) objectMapper.readValue(json, CredentialDetails.class); + protected T parseResponse(String json, Class type) { + return JsonTestUtils.fromJson(json, type); } @SuppressWarnings("unchecked") - protected CredentialDetailsData parseDetailsData(String credentials) throws java.io.IOException { + protected CredentialDetails parseDetails(String credentials) { + String json = buildDetails(credentials); + return (CredentialDetails) JsonTestUtils.fromJson(json, CredentialDetails.class); + } + + @SuppressWarnings("unchecked") + protected CredentialDetailsData parseDetailsData(String credentials) { String json = buildDetailsData(credentials); - return (CredentialDetailsData) objectMapper.readValue(json, CredentialDetailsData.class); + return (CredentialDetailsData) JsonTestUtils.fromJson(json, CredentialDetailsData.class); } private String buildDetails(String credentials) { @@ -74,9 +71,9 @@ public abstract class JsonParsingUnitTestsBase { } protected void assertCommonDetails(CredentialDetails data) { - assertThat(data.getVersionCreatedAt(), equalTo(testDate)); - assertThat(data.getId(), equalTo("80cbb13f-7562-4e72-92de-f3ccf69eaa59")); - assertThat(data.getName().getName(), equalTo( - "/service-broker-name/service-instance-name/binding-id/credentials-json")); + assertThat(data.getVersionCreatedAt()).isEqualTo(testDate); + assertThat(data.getId()).isEqualTo("80cbb13f-7562-4e72-92de-f3ccf69eaa59"); + assertThat(data.getName().getName()).isEqualTo( + "/service-broker-name/service-instance-name/binding-id/credentials-json"); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonPathAssert.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonPathAssert.java new file mode 100644 index 0000000..c90aa89 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonPathAssert.java @@ -0,0 +1,53 @@ +/* + * Copyright 2002-2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.credhub.support; + +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPathException; +import com.jayway.jsonpath.PathNotFoundException; +import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.AbstractObjectAssert; +import org.assertj.core.api.Assertions; + +public class JsonPathAssert extends AbstractAssert { + public JsonPathAssert(DocumentContext actual) { + super(actual, JsonPathAssert.class); + } + + public static JsonPathAssert assertThat(DocumentContext jsonPathDocument) { + return new JsonPathAssert(jsonPathDocument); + } + + public JsonPathAssert hasNoPath(String jsonPath) { + try { + Object value = actual.read(jsonPath); + failWithMessage("The path '" + jsonPath + "' was not expected but evaluated to " + value); + return null; + } catch (JsonPathException e) { + return this; + } + } + + public AbstractObjectAssert hasPath(String path) { + try { + return Assertions.assertThat(actual.read(path, Object.class)); + } catch (PathNotFoundException e) { + failWithMessage("The JSON " + actual.jsonString() + " does not contain the path '" + path + "'"); + return null; + } + } +} diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonTestUtils.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonTestUtils.java new file mode 100644 index 0000000..5fc5cd7 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonTestUtils.java @@ -0,0 +1,64 @@ +/* + * Copyright 2016-2017 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.credhub.support; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.jayway.jsonpath.Configuration; +import com.jayway.jsonpath.DocumentContext; +import com.jayway.jsonpath.JsonPath; +import com.jayway.jsonpath.spi.json.JacksonJsonProvider; +import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; +import org.springframework.credhub.support.utils.JsonUtils; + +import java.io.IOException; + +import static org.assertj.core.api.Assertions.fail; + +public final class JsonTestUtils { + private JsonTestUtils() { + } + + public static String toJson(Object object) { + try { + ObjectMapper mapper = JsonUtils.buildObjectMapper(); + return mapper.writeValueAsString(object); + } catch (JsonProcessingException e) { + fail("Error creating JSON string from object: " + e); + throw new IllegalStateException(e); + } + } + + public static T fromJson(String json, Class type) { + try { + ObjectMapper mapper = JsonUtils.buildObjectMapper(); + return mapper.readValue(json, type); + } catch (IOException e) { + fail("Error parsing JSON string to object: " + e); + throw new IllegalStateException(e); + } + } + + public static DocumentContext toJsonPath(Object object) { + Configuration configuration = Configuration.builder() + .jsonProvider(new JacksonJsonProvider()) + .mappingProvider(new JacksonMappingProvider()) + .build(); + + return JsonPath.parse(toJson(object), configuration); + } +} diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/ServiceInstanceCredentialNameUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ServiceInstanceCredentialNameUnitTests.java index 227ae7f..e1ff495 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/ServiceInstanceCredentialNameUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ServiceInstanceCredentialNameUnitTests.java @@ -18,8 +18,7 @@ package org.springframework.credhub.support; import org.junit.Test; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class ServiceInstanceCredentialNameUnitTests { @Test @@ -27,6 +26,6 @@ public class ServiceInstanceCredentialNameUnitTests { CredentialName credentialName = new ServiceInstanceCredentialName("broker-name", "service-name", "binding-id", "credential-name"); - assertThat(credentialName.getName(), equalTo("/c/broker-name/service-name/binding-id/credential-name")); + assertThat(credentialName.getName()).isEqualTo("/c/broker-name/service-name/binding-id/credential-name"); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/SimpleCredentialNameUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/SimpleCredentialNameUnitTests.java index 9484764..f31fa31 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/SimpleCredentialNameUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/SimpleCredentialNameUnitTests.java @@ -20,29 +20,28 @@ package org.springframework.credhub.support; import org.junit.Test; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class SimpleCredentialNameUnitTests { @Test public void singleElementNameIsConstructed() { CredentialName credentialName = new SimpleCredentialName("credential-name"); - assertThat(credentialName.getName(), equalTo("credential-name")); + assertThat(credentialName.getName()).isEqualTo("credential-name"); } @Test public void singleElementNameWithLeadingSlashIsParsed() { CredentialName credentialName = new CredentialName("/credential-name"); - assertThat(credentialName.getName(), equalTo("credential-name")); + assertThat(credentialName.getName()).isEqualTo("credential-name"); } @Test public void singleElementNameWithoutLeadingSlashIsParsed() { CredentialName credentialName = new CredentialName("credential-name"); - assertThat(credentialName.getName(), equalTo("credential-name")); + assertThat(credentialName.getName()).isEqualTo("credential-name"); } @Test @@ -50,20 +49,20 @@ public class SimpleCredentialNameUnitTests { CredentialName credentialName = new SimpleCredentialName("myorg", "example", "credential-name"); - assertThat(credentialName.getName(), equalTo("/myorg/example/credential-name")); + assertThat(credentialName.getName()).isEqualTo("/myorg/example/credential-name"); } @Test public void simpleNameIsParsed() { CredentialName credentialName = new CredentialName("/myorg/example/credential-name"); - assertThat(credentialName.getName(), equalTo("/myorg/example/credential-name")); + assertThat(credentialName.getName()).isEqualTo("/myorg/example/credential-name"); } @Test public void simpleNameWithoutLeadingSlashIsParsed() { CredentialName credentialName = new CredentialName("myorg/example/credential-name"); - assertThat(credentialName.getName(), equalTo("/myorg/example/credential-name")); + assertThat(credentialName.getName()).isEqualTo("/myorg/example/credential-name"); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateCredentialDetailsUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateCredentialDetailsUnitTests.java index 65a846f..be1ba9b 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateCredentialDetailsUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateCredentialDetailsUnitTests.java @@ -23,8 +23,7 @@ import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialType; import org.springframework.credhub.support.JsonParsingUnitTestsBase; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class CertificateCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private static final String CERT_CREDENTIALS = @@ -36,14 +35,14 @@ public class CertificateCredentialDetailsUnitTests extends JsonParsingUnitTestsB " }"; @Test - public void deserializeDetailsWithAllValues() throws Exception { + public void deserializeDetailsWithAllValues() { CredentialDetails data = parseDetails(CERT_CREDENTIALS); assertDetails(data, "cert", "authority", "private-key"); } @Test - public void deserializeDetailsCertOnly() throws Exception { + public void deserializeDetailsCertOnly() { final String credentials = " \"type\": \"certificate\"," + " \"value\": {" + @@ -55,7 +54,7 @@ public class CertificateCredentialDetailsUnitTests extends JsonParsingUnitTestsB } @Test - public void deserializeDetailsWithNoCert() throws Exception { + public void deserializeDetailsWithNoCert() { final String credentials = " \"type\": \"certificate\"," + " \"value\": {" + @@ -68,10 +67,10 @@ public class CertificateCredentialDetailsUnitTests extends JsonParsingUnitTestsB } @Test - public void deserializeDetailsData() throws Exception { + public void deserializeDetailsData() { CredentialDetailsData response = parseDetailsData(CERT_CREDENTIALS); - assertThat(response.getData().size(), equalTo(1)); + assertThat(response.getData()).hasSize(1); CredentialDetails data = response.getData().get(0); @@ -82,9 +81,9 @@ public class CertificateCredentialDetailsUnitTests extends JsonParsingUnitTestsB String certificate, String ca, String privateKey) { assertCommonDetails(data); - assertThat(data.getCredentialType(), equalTo(CredentialType.CERTIFICATE)); - assertThat(data.getValue().getCertificate(), equalTo(certificate)); - assertThat(data.getValue().getCertificateAuthority(), equalTo(ca)); - assertThat(data.getValue().getPrivateKey(), equalTo(privateKey)); + assertThat(data.getCredentialType()).isEqualTo(CredentialType.CERTIFICATE); + assertThat(data.getValue().getCertificate()).isEqualTo(certificate); + assertThat(data.getValue().getCertificateAuthority()).isEqualTo(ca); + assertThat(data.getValue().getPrivateKey()).isEqualTo(privateKey); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateCredentialRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateCredentialRequestUnitTests.java index 77343d1..92e7abe 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateCredentialRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateCredentialRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.certificate; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -23,11 +24,7 @@ import org.springframework.credhub.support.CredHubRequestUnitTestsBase; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; public class CertificateCredentialRequestUnitTests extends CredHubRequestUnitTestsBase { @Before @@ -36,53 +33,50 @@ public class CertificateCredentialRequestUnitTests extends CredHubRequestUnitTes } @Test - public void serializeWithAllValues() throws Exception { - String jsonValue = serializeToJson(requestBuilder); + public void serializeWithAllValues() { + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "certificate"); - assertThat(jsonValue, - allOf(hasJsonPath("$.value.certificate", equalTo("cert")), - hasJsonPath("$.value.ca", equalTo("ca")), - hasJsonPath("$.value.private_key", equalTo("private-key")))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "certificate"); + assertThat(json).hasPath("$.value.certificate").isEqualTo("cert"); + assertThat(json).hasPath("$.value.ca").isEqualTo("ca"); + assertThat(json).hasPath("$.value.private_key").isEqualTo("private-key"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test - public void serializeWithCertOnly() throws Exception { + public void serializeWithCertOnly() { buildRequest(new CertificateCredential("cert", null, null)); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "certificate"); - assertThat(jsonValue, - allOf(hasJsonPath("$.value.certificate", equalTo("cert")), - hasNoJsonPath("$.value.ca"), - hasNoJsonPath("$.value.private_key"))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "certificate"); + assertThat(json).hasPath("$.value.certificate").isEqualTo("cert"); + assertThat(json).hasNoPath("$.value.ca") + .hasNoPath("$.value.private_key"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test - public void serializeWithNoCert() throws Exception { + public void serializeWithNoCert() { buildRequest(new CertificateCredential(null, "ca", "private-key")); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "certificate"); - assertThat(jsonValue, - allOf(hasNoJsonPath("$.value.certificate"), - hasJsonPath("$.value.ca", equalTo("ca")), - hasJsonPath("$.value.private_key", equalTo("private-key")))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "certificate"); + assertThat(json).hasNoPath("$.value.certificate"); + assertThat(json).hasPath("$.value.ca").isEqualTo("ca"); + assertThat(json).hasPath("$.value.private_key").isEqualTo("private-key"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test(expected = IllegalArgumentException.class) - public void serializeWithNoValues() throws Exception { + public void serializeWithNoValues() { buildRequest(new CertificateCredential(null, null, null)); - serializeToJson(requestBuilder); + toJsonPath(requestBuilder); } @SuppressWarnings("deprecation") diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateParametersRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateParametersRequestUnitTests.java index dcf6148..16127ce 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateParametersRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/certificate/CertificateParametersRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.certificate; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -24,11 +25,7 @@ import org.springframework.credhub.support.KeyLength; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; @SuppressWarnings("deprecation") public class CertificateParametersRequestUnitTests extends CredHubRequestUnitTestsBase { @@ -38,8 +35,7 @@ public class CertificateParametersRequestUnitTests extends CredHubRequestUnitTes } @Test - @SuppressWarnings("unchecked") - public void serializeWithParameters() throws Exception { + public void serializeWithParameters() { requestBuilder = CertificateParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) @@ -65,42 +61,40 @@ public class CertificateParametersRequestUnitTests extends CredHubRequestUnitTes ExtendedKeyUsage.TIMESTAMPING) .build()); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "certificate"); - assertThat(jsonValue, - allOf(hasJsonPath("$.parameters.key_length", equalTo(2048)), - hasJsonPath("$.parameters.common_name", equalTo("common")), - hasJsonPath("$.parameters.alternative_names[0]", equalTo("alt1")), - hasJsonPath("$.parameters.alternative_names[1]", equalTo("alt2")), - hasJsonPath("$.parameters.organization", equalTo("org")), - hasJsonPath("$.parameters.organization_unit", equalTo("dev")), - hasJsonPath("$.parameters.locality", equalTo("city")), - hasJsonPath("$.parameters.state", equalTo("state")), - hasJsonPath("$.parameters.country", equalTo("country")), - hasJsonPath("$.parameters.duration", equalTo(1234)), - hasJsonPath("$.parameters.ca", equalTo("credential")), - hasJsonPath("$.parameters.is_ca", equalTo(true)), - hasJsonPath("$.parameters.self_sign", equalTo(false)), - hasJsonPath("$.parameters.key_usage[0]", equalTo(KeyUsage.CRL_SIGN.getValue())), - hasJsonPath("$.parameters.key_usage[1]", equalTo(KeyUsage.DATA_ENCIPHERMENT.getValue())), - hasJsonPath("$.parameters.key_usage[2]", equalTo(KeyUsage.DECIPHER_ONLY.getValue())), - hasJsonPath("$.parameters.key_usage[3]", equalTo(KeyUsage.DIGITAL_SIGNATURE.getValue())), - hasJsonPath("$.parameters.key_usage[4]", equalTo(KeyUsage.ENCIPHER_ONLY.getValue())), - hasJsonPath("$.parameters.key_usage[5]", equalTo(KeyUsage.KEY_AGREEMENT.getValue())), - hasJsonPath("$.parameters.key_usage[6]", equalTo(KeyUsage.KEY_CERT_SIGN.getValue())), - hasJsonPath("$.parameters.key_usage[7]", equalTo(KeyUsage.KEY_ENCIPHERMENT.getValue())), - hasJsonPath("$.parameters.key_usage[8]", equalTo(KeyUsage.NON_REPUDIATION.getValue())), - hasJsonPath("$.parameters.extended_key_usage[0]", equalTo(ExtendedKeyUsage.CLIENT_AUTH.getValue())), - hasJsonPath("$.parameters.extended_key_usage[1]", equalTo(ExtendedKeyUsage.CODE_SIGNING.getValue())), - hasJsonPath("$.parameters.extended_key_usage[2]", equalTo(ExtendedKeyUsage.EMAIL_PROTECTION.getValue())), - hasJsonPath("$.parameters.extended_key_usage[3]", equalTo(ExtendedKeyUsage.SERVER_AUTH.getValue())), - hasJsonPath("$.parameters.extended_key_usage[4]", equalTo(ExtendedKeyUsage.TIMESTAMPING.getValue())))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "certificate"); + assertThat(json).hasPath("$.parameters.key_length").isEqualTo(2048); + assertThat(json).hasPath("$.parameters.common_name").isEqualTo("common"); + assertThat(json).hasPath("$.parameters.alternative_names[0]").isEqualTo("alt1"); + assertThat(json).hasPath("$.parameters.alternative_names[1]").isEqualTo("alt2"); + assertThat(json).hasPath("$.parameters.organization").isEqualTo("org"); + assertThat(json).hasPath("$.parameters.organization_unit").isEqualTo("dev"); + assertThat(json).hasPath("$.parameters.locality").isEqualTo("city"); + assertThat(json).hasPath("$.parameters.state").isEqualTo("state"); + assertThat(json).hasPath("$.parameters.country").isEqualTo("country"); + assertThat(json).hasPath("$.parameters.duration").isEqualTo(1234); + assertThat(json).hasPath("$.parameters.ca").isEqualTo("credential"); + assertThat(json).hasPath("$.parameters.is_ca").isEqualTo(true); + assertThat(json).hasPath("$.parameters.self_sign").isEqualTo(false); + assertThat(json).hasPath("$.parameters.key_usage[0]").isEqualTo(KeyUsage.CRL_SIGN.getValue()); + assertThat(json).hasPath("$.parameters.key_usage[1]").isEqualTo(KeyUsage.DATA_ENCIPHERMENT.getValue()); + assertThat(json).hasPath("$.parameters.key_usage[2]").isEqualTo(KeyUsage.DECIPHER_ONLY.getValue()); + assertThat(json).hasPath("$.parameters.key_usage[3]").isEqualTo(KeyUsage.DIGITAL_SIGNATURE.getValue()); + assertThat(json).hasPath("$.parameters.key_usage[4]").isEqualTo(KeyUsage.ENCIPHER_ONLY.getValue()); + assertThat(json).hasPath("$.parameters.key_usage[5]").isEqualTo(KeyUsage.KEY_AGREEMENT.getValue()); + assertThat(json).hasPath("$.parameters.key_usage[6]").isEqualTo(KeyUsage.KEY_CERT_SIGN.getValue()); + assertThat(json).hasPath("$.parameters.key_usage[7]").isEqualTo(KeyUsage.KEY_ENCIPHERMENT.getValue()); + assertThat(json).hasPath("$.parameters.key_usage[8]").isEqualTo(KeyUsage.NON_REPUDIATION.getValue()); + assertThat(json).hasPath("$.parameters.extended_key_usage[0]").isEqualTo(ExtendedKeyUsage.CLIENT_AUTH.getValue()); + assertThat(json).hasPath("$.parameters.extended_key_usage[1]").isEqualTo(ExtendedKeyUsage.CODE_SIGNING.getValue()); + assertThat(json).hasPath("$.parameters.extended_key_usage[2]").isEqualTo(ExtendedKeyUsage.EMAIL_PROTECTION.getValue()); + assertThat(json).hasPath("$.parameters.extended_key_usage[3]").isEqualTo(ExtendedKeyUsage.SERVER_AUTH.getValue()); + assertThat(json).hasPath("$.parameters.extended_key_usage[4]").isEqualTo(ExtendedKeyUsage.TIMESTAMPING.getValue()); } @Test - @SuppressWarnings("unchecked") - public void serializeWithMinimalParameters() throws Exception { + public void serializeWithMinimalParameters() { requestBuilder = CertificateParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) @@ -110,41 +104,40 @@ public class CertificateParametersRequestUnitTests extends CredHubRequestUnitTes .certificateAuthorityCredential("credential") .build()); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.NO_OVERWRITE, "/example/credential", "certificate"); - assertThat(jsonValue, - allOf(hasNoJsonPath("$.parameters.key_length"), - hasJsonPath("$.parameters.common_name", equalTo("common")), - hasJsonPath("$.parameters.ca", equalTo("credential")), - hasNoJsonPath("$.parameters.alternative_names"), - hasNoJsonPath("$.parameters.organization"), - hasNoJsonPath("$.parameters.organization_unit"), - hasNoJsonPath("$.parameters.locality"), - hasNoJsonPath("$.parameters.state"), - hasNoJsonPath("$.parameters.country"), - hasNoJsonPath("$.parameters.duration"), - hasNoJsonPath("$.parameters.is_ca"), - hasNoJsonPath("$.parameters.self_sign"), - hasNoJsonPath("$.parameters.key_usage"), - hasNoJsonPath("$.parameters.extended_key_usage"))); + assertCommonRequestFields(json, true, WriteMode.NO_OVERWRITE, "/example/credential", "certificate"); + assertThat(json).hasNoPath("$.parameters.key_length"); + assertThat(json).hasPath("$.parameters.common_name").isEqualTo("common"); + assertThat(json).hasPath("$.parameters.ca").isEqualTo("credential"); + assertThat(json).hasNoPath("$.parameters.alternative_names"); + assertThat(json).hasNoPath("$.parameters.organization"); + assertThat(json).hasNoPath("$.parameters.organization_unit"); + assertThat(json).hasNoPath("$.parameters.locality"); + assertThat(json).hasNoPath("$.parameters.state"); + assertThat(json).hasNoPath("$.parameters.country"); + assertThat(json).hasNoPath("$.parameters.duration"); + assertThat(json).hasNoPath("$.parameters.is_ca"); + assertThat(json).hasNoPath("$.parameters.self_sign"); + assertThat(json).hasNoPath("$.parameters.key_usage"); + assertThat(json).hasNoPath("$.parameters.extended_key_usage"); } @Test - public void serializeWithNoParameters() throws Exception { + public void serializeWithNoParameters() { requestBuilder = CertificateParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.CONVERGE); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.CONVERGE, "/example/credential", "certificate"); - assertParametersNotSet(jsonValue); + assertCommonRequestFields(json, true, WriteMode.CONVERGE, "/example/credential", "certificate"); + assertParametersNotSet(json); } @Test(expected = IllegalArgumentException.class) - public void serializeWithEmptyParameters() throws Exception { + public void serializeWithEmptyParameters() { requestBuilder = CertificateParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) @@ -152,15 +145,14 @@ public class CertificateParametersRequestUnitTests extends CredHubRequestUnitTes .keyLength(KeyLength.LENGTH_2048) .build()); - serializeToJson(requestBuilder); + toJsonPath(requestBuilder); } - private void assertParametersNotSet(String jsonValue) { - assertThat(jsonValue, - allOf(hasNoJsonPath("$.parameters.key_length"), - hasNoJsonPath("$.parameters.exclude_lower"), - hasNoJsonPath("$.parameters.exclude_upper"), - hasNoJsonPath("$.parameters.exclude_number"), - hasNoJsonPath("$.parameters.include_special"))); + private void assertParametersNotSet(DocumentContext json) { + assertThat(json).hasNoPath("$.parameters.key_length"); + assertThat(json).hasNoPath("$.parameters.exclude_lower"); + assertThat(json).hasNoPath("$.parameters.exclude_upper"); + assertThat(json).hasNoPath("$.parameters.exclude_number"); + assertThat(json).hasNoPath("$.parameters.include_special"); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/json/JsonCredentialDetailsUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/json/JsonCredentialDetailsUnitTests.java index 0ea3c98..bf6e5f8 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/json/JsonCredentialDetailsUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/json/JsonCredentialDetailsUnitTests.java @@ -16,7 +16,6 @@ package org.springframework.credhub.support.json; -import org.hamcrest.CoreMatchers; import org.junit.Test; import org.springframework.credhub.support.CredentialDetails; @@ -24,8 +23,7 @@ import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialType; import org.springframework.credhub.support.JsonParsingUnitTestsBase; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class JsonCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private static final String JSON_CREDENTIALS = @@ -37,28 +35,28 @@ public class JsonCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { " }"; @Test - public void deserializeDetails() throws Exception { + public void deserializeDetails() { CredentialDetails data = parseDetails(JSON_CREDENTIALS); assertDetails(data); } @Test - public void deserializeDetailsData() throws Exception { + public void deserializeDetailsData() { CredentialDetailsData data = parseDetailsData(JSON_CREDENTIALS); - assertThat(data.getData().size(), equalTo(1)); + assertThat(data.getData()).hasSize(1); assertDetails(data.getData().get(0)); } private void assertDetails(CredentialDetails data) { assertCommonDetails(data); - assertThat(data.getCredentialType(), equalTo(CredentialType.JSON)); + assertThat(data.getCredentialType()).isEqualTo(CredentialType.JSON); JsonCredential valueMap = data.getValue(); - assertThat(valueMap.get("client_id"), CoreMatchers. equalTo("test-id")); - assertThat(valueMap.get("client_secret"), CoreMatchers. equalTo("test-secret")); - assertThat(valueMap.get("uri"), CoreMatchers. equalTo("https://example.com")); + assertThat(valueMap).containsEntry("client_id", "test-id") + .containsEntry("client_secret", "test-secret") + .containsEntry("uri", "https://example.com"); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/json/JsonCredentialRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/json/JsonCredentialRequestUnitTests.java index 968562f..cb126e2 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/json/JsonCredentialRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/json/JsonCredentialRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.json; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -23,10 +24,7 @@ import org.springframework.credhub.support.CredHubRequestUnitTestsBase; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; public class JsonCredentialRequestUnitTests extends CredHubRequestUnitTestsBase { @@ -44,14 +42,13 @@ public class JsonCredentialRequestUnitTests extends CredHubRequestUnitTestsBase } @Test - public void serializeWithJsonValue() throws Exception { - String jsonValue = serializeToJson(requestBuilder); + public void serializeWithJsonValue() { + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, false, WriteMode.OVERWRITE, "/example/credential", "json"); - assertThat(jsonValue, - allOf(hasJsonPath("$.value.data", equalTo("value")), - hasJsonPath("$.value.test", equalTo(true)))); + assertCommonRequestFields(json, false, WriteMode.OVERWRITE, "/example/credential", "json"); + assertThat(json).hasPath("$.value.data").isEqualTo("value"); + assertThat(json).hasPath("$.value.test").isEqualTo(true); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordCredentialDetailsUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordCredentialDetailsUnitTests.java index cd6dd91..3fe43ef 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordCredentialDetailsUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordCredentialDetailsUnitTests.java @@ -23,8 +23,7 @@ import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialType; import org.springframework.credhub.support.JsonParsingUnitTestsBase; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class PasswordCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private static final String PASSWORD_CREDENTIALS = @@ -32,7 +31,7 @@ public class PasswordCredentialDetailsUnitTests extends JsonParsingUnitTestsBase " \"value\": \"secret\""; @Test - public void deserializeDetails() throws Exception { + public void deserializeDetails() { CredentialDetails data = parseDetails(PASSWORD_CREDENTIALS); @@ -40,11 +39,11 @@ public class PasswordCredentialDetailsUnitTests extends JsonParsingUnitTestsBase } @Test - public void deserializeDetailsData() throws Exception { + public void deserializeDetailsData() { CredentialDetailsData response = parseDetailsData(PASSWORD_CREDENTIALS); - assertThat(response.getData().size(), equalTo(1)); + assertThat(response.getData()).hasSize(1); CredentialDetails data = response.getData().get(0); @@ -54,7 +53,7 @@ public class PasswordCredentialDetailsUnitTests extends JsonParsingUnitTestsBase private void assertDetails(CredentialDetails data) { assertCommonDetails(data); - assertThat(data.getCredentialType(), equalTo(CredentialType.PASSWORD)); - assertThat(data.getValue().getPassword(), equalTo("secret")); + assertThat(data.getCredentialType()).isEqualTo(CredentialType.PASSWORD); + assertThat(data.getValue().getPassword()).isEqualTo("secret"); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordCredentialRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordCredentialRequestUnitTests.java index 4624cb1..f5aa599 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordCredentialRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordCredentialRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.password; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -23,9 +24,7 @@ import org.springframework.credhub.support.CredHubRequestUnitTestsBase; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; @SuppressWarnings("deprecation") public class PasswordCredentialRequestUnitTests extends CredHubRequestUnitTestsBase { @@ -40,28 +39,28 @@ public class PasswordCredentialRequestUnitTests extends CredHubRequestUnitTestsB } @Test - public void serializeWithPasswordValue() throws Exception { - String jsonValue = serializeToJson(requestBuilder); + public void serializeWithPasswordValue() { + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "password"); - assertThat(jsonValue, hasJsonPath("$.value", equalTo("secret"))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "password"); + assertThat(json).hasPath("$.value").isEqualTo("secret"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test - public void serializeWithStringValue() throws Exception { + public void serializeWithStringValue() { requestBuilder = PasswordCredentialRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.OVERWRITE) .value("secret"); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "password"); - assertThat(jsonValue, hasJsonPath("$.value", equalTo("secret"))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "password"); + assertThat(json).hasPath("$.value").isEqualTo("secret"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordParametersRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordParametersRequestUnitTests.java index 3c8b229..b1dd958 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordParametersRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/password/PasswordParametersRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.password; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -23,11 +24,7 @@ import org.springframework.credhub.support.CredHubRequestUnitTestsBase; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; @SuppressWarnings("deprecation") public class PasswordParametersRequestUnitTests extends CredHubRequestUnitTestsBase { @@ -37,7 +34,7 @@ public class PasswordParametersRequestUnitTests extends CredHubRequestUnitTestsB } @Test - public void serializeWithParameters() throws Exception { + public void serializeWithParameters() { requestBuilder = PasswordParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) @@ -50,50 +47,48 @@ public class PasswordParametersRequestUnitTests extends CredHubRequestUnitTestsB .includeSpecial(false) .build()); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "password"); - assertThat(jsonValue, - allOf(hasJsonPath("$.parameters.length", equalTo(20)), - hasJsonPath("$.parameters.exclude_lower", equalTo(true)), - hasJsonPath("$.parameters.exclude_upper", equalTo(false)), - hasJsonPath("$.parameters.exclude_number", equalTo(true)), - hasJsonPath("$.parameters.include_special", equalTo(false)))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "password"); + assertThat(json).hasPath("$.parameters.length").isEqualTo(20); + assertThat(json).hasPath("$.parameters.exclude_lower").isEqualTo(true); + assertThat(json).hasPath("$.parameters.exclude_upper").isEqualTo(false); + assertThat(json).hasPath("$.parameters.exclude_number").isEqualTo(true); + assertThat(json).hasPath("$.parameters.include_special").isEqualTo(false); } @Test - public void serializeWithEmptyParameters() throws Exception { + public void serializeWithEmptyParameters() { requestBuilder = PasswordParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.CONVERGE) .parameters(new PasswordParameters()); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.CONVERGE, "/example/credential", "password"); - assertParametersNotSet(jsonValue); + assertCommonRequestFields(json, true, WriteMode.CONVERGE, "/example/credential", "password"); + assertParametersNotSet(json); } @Test - public void serializeWithNoParameters() throws Exception { + public void serializeWithNoParameters() { requestBuilder = PasswordParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.NO_OVERWRITE); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.NO_OVERWRITE, "/example/credential", "password"); - assertParametersNotSet(jsonValue); + assertCommonRequestFields(json, true, WriteMode.NO_OVERWRITE, "/example/credential", "password"); + assertParametersNotSet(json); } - private void assertParametersNotSet(String jsonValue) { - assertThat(jsonValue, - allOf(hasNoJsonPath("$.parameters.length"), - hasNoJsonPath("$.parameters.exclude_lower"), - hasNoJsonPath("$.parameters.exclude_upper"), - hasNoJsonPath("$.parameters.exclude_number"), - hasNoJsonPath("$.parameters.include_special"))); + private void assertParametersNotSet(DocumentContext json) { + assertThat(json).hasNoPath("$.parameters.length"); + assertThat(json).hasNoPath("$.parameters.exclude_lower"); + assertThat(json).hasNoPath("$.parameters.exclude_upper"); + assertThat(json).hasNoPath("$.parameters.exclude_number"); + assertThat(json).hasNoPath("$.parameters.include_special"); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionsUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionsUnitTests.java index 67ac4b8..244a65e 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionsUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/permissions/CredentialPermissionsUnitTests.java @@ -20,18 +20,16 @@ package org.springframework.credhub.support.permissions; import java.util.List; -import com.fasterxml.jackson.core.JsonProcessingException; +import com.jayway.jsonpath.DocumentContext; import org.junit.Test; import org.springframework.credhub.support.CredentialPermissions; import org.springframework.credhub.support.JsonParsingUnitTestsBase; +import org.springframework.credhub.support.JsonTestUtils; import org.springframework.credhub.support.SimpleCredentialName; -import org.springframework.credhub.support.utils.JsonUtils; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.core.IsEqual.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; import static org.springframework.credhub.support.permissions.ActorType.APP; import static org.springframework.credhub.support.permissions.ActorType.USER; import static org.springframework.credhub.support.permissions.Operation.DELETE; @@ -39,12 +37,10 @@ import static org.springframework.credhub.support.permissions.Operation.READ; import static org.springframework.credhub.support.permissions.Operation.READ_ACL; import static org.springframework.credhub.support.permissions.Operation.WRITE; import static org.springframework.credhub.support.permissions.Operation.WRITE_ACL; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.isJson; public class CredentialPermissionsUnitTests extends JsonParsingUnitTestsBase { @Test - public void deserializePermissions() throws Exception { + public void deserializePermissions() { String json = "{\"credential_name\": \"/c/example\"," + "\"permissions\": [" + " {" + @@ -67,60 +63,52 @@ public class CredentialPermissionsUnitTests extends JsonParsingUnitTestsBase { CredentialPermissions permissions = parsePermissions(json); - assertThat(permissions.getCredentialName(), equalTo("/c/example")); - assertThat(permissions.getPermissions().size(), equalTo(2)); + assertThat(permissions.getCredentialName()).isEqualTo("/c/example"); + assertThat(permissions.getPermissions().size()).isEqualTo(2); CredentialPermission permission = permissions.getPermissions().get(0); - assertThat(permission.getActor().getAuthType(), equalTo(APP)); - assertThat(permission.getActor().getPrimaryIdentifier(), equalTo("appid1")); + assertThat(permission.getActor().getAuthType()).isEqualTo(APP); + assertThat(permission.getActor().getPrimaryIdentifier()).isEqualTo("appid1"); List operations = permission.getOperations(); - assertThat(operations.size(), equalTo(5)); - assertThat(operations, contains(READ, WRITE, DELETE, READ_ACL, WRITE_ACL)); + assertThat(operations.size()).isEqualTo(5); + assertThat(operations).contains(READ, WRITE, DELETE, READ_ACL, WRITE_ACL); permission = permissions.getPermissions().get(1); - assertThat(permission.getActor().getAuthType(), equalTo(USER)); - assertThat(permission.getActor().getPrimaryIdentifier(), equalTo("zone1/userid")); + assertThat(permission.getActor().getAuthType()).isEqualTo(USER); + assertThat(permission.getActor().getPrimaryIdentifier()).isEqualTo("zone1/userid"); operations = permission.getOperations(); - assertThat(operations.size(), equalTo(1)); - assertThat(operations, contains(READ)); + assertThat(operations.size()).isEqualTo(1); + assertThat(operations).contains(READ); } @Test - public void deserializeWithNoPermissions() throws Exception { + public void deserializeWithNoPermissions() { String json = "{\"permissions\": []}"; CredentialPermissions permissions = parsePermissions(json); - assertThat(permissions.getPermissions().size(), equalTo(0)); + assertThat(permissions.getPermissions().size()).isEqualTo(0); } @Test - public void serialize() throws Exception { + public void serialize() { CredentialPermissions permissions = new CredentialPermissions(new SimpleCredentialName("example", "credentialName"), CredentialPermission.builder() .app("appid1") .operations(READ, WRITE) .build()); - String jsonValue = serializeToJson(permissions); + DocumentContext json = JsonTestUtils.toJsonPath(permissions); - assertThat(jsonValue, - allOf(hasJsonPath("$.credential_name", equalTo("/example/credentialName")), - hasJsonPath("$.permissions[0].actor", equalTo(Actor.app("appid1").getIdentity())), - hasJsonPath("$.permissions[0].operations[0]", equalTo("read")), - hasJsonPath("$.permissions[0].operations[1]", equalTo("write")))); + assertThat(json).hasPath("$.credential_name").isEqualTo("/example/credentialName"); + assertThat(json).hasPath("$.permissions[0].actor").isEqualTo(Actor.app("appid1").getIdentity()); + assertThat(json).hasPath("$.permissions[0].operations[0]").isEqualTo("read"); + assertThat(json).hasPath("$.permissions[0].operations[1]").isEqualTo("write"); } - protected String serializeToJson(Object obj) throws JsonProcessingException { - String jsonValue = JsonUtils.buildObjectMapper().writeValueAsString(obj); - assertThat(jsonValue, isJson()); - return jsonValue; - } - - @SuppressWarnings("unchecked") - private CredentialPermissions parsePermissions(String json) throws java.io.IOException { - return objectMapper.readValue(json, CredentialPermissions.class); + private CredentialPermissions parsePermissions(String json) { + return JsonTestUtils.fromJson(json, CredentialPermissions.class); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaCredentialDetailsUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaCredentialDetailsUnitTests.java index 0fbcab4..c18baa4 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaCredentialDetailsUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaCredentialDetailsUnitTests.java @@ -23,8 +23,7 @@ import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialType; import org.springframework.credhub.support.JsonParsingUnitTestsBase; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class RsaCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private static final String RSA_CREDENTIALS = @@ -35,14 +34,14 @@ public class RsaCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { " }"; @Test - public void deserializeDetailsWithPublicAndPrivateKeys() throws Exception { + public void deserializeDetailsWithPublicAndPrivateKeys() { CredentialDetails data = parseDetails(RSA_CREDENTIALS); assertDetails(data, "public-key", "private-key"); } @Test - public void deserializeDetailsWithPublicKey() throws Exception { + public void deserializeDetailsWithPublicKey() { final String credentials = " \"type\": \"rsa\"," + " \"value\": {" + @@ -54,7 +53,7 @@ public class RsaCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { } @Test - public void deserializeDetailsWithPrivateKey() throws Exception { + public void deserializeDetailsWithPrivateKey() { final String credentials = " \"type\": \"rsa\"," + " \"value\": {" + @@ -66,10 +65,10 @@ public class RsaCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { } @Test - public void deserializeDetailsData() throws Exception { + public void deserializeDetailsData() { CredentialDetailsData response = parseDetailsData(RSA_CREDENTIALS); - assertThat(response.getData().size(), equalTo(1)); + assertThat(response.getData()).hasSize(1); CredentialDetails data = response.getData().get(0); @@ -79,8 +78,8 @@ public class RsaCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private void assertDetails(CredentialDetails data, String publicKey, String privateKey) { assertCommonDetails(data); - assertThat(data.getCredentialType(), equalTo(CredentialType.RSA)); - assertThat(data.getValue().getPublicKey(), equalTo(publicKey)); - assertThat(data.getValue().getPrivateKey(), equalTo(privateKey)); + assertThat(data.getCredentialType()).isEqualTo(CredentialType.RSA); + assertThat(data.getValue().getPublicKey()).isEqualTo(publicKey); + assertThat(data.getValue().getPrivateKey()).isEqualTo(privateKey); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaCredentialRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaCredentialRequestUnitTests.java index 6a9ad3c..55d20ab 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaCredentialRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaCredentialRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.rsa; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -23,11 +24,7 @@ import org.springframework.credhub.support.CredHubRequestUnitTestsBase; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; public class RsaCredentialRequestUnitTests extends CredHubRequestUnitTestsBase { @Before @@ -36,50 +33,48 @@ public class RsaCredentialRequestUnitTests extends CredHubRequestUnitTestsBase { } @Test - public void serializeWithPublicAndPrivateKey() throws Exception { - String jsonValue = serializeToJson(requestBuilder); + public void serializeWithPublicAndPrivateKey() { + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); - assertThat(jsonValue, - allOf(hasJsonPath("$.value.public_key", equalTo("public-key")), - hasJsonPath("$.value.private_key", equalTo("private-key")))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); + assertThat(json).hasPath("$.value.public_key").isEqualTo("public-key"); + assertThat(json).hasPath("$.value.private_key").isEqualTo("private-key"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test - public void serializeWithPublicKey() throws Exception { + public void serializeWithPublicKey() { buildRequest(new RsaCredential("public-key", null)); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); - assertThat(jsonValue, - allOf(hasJsonPath("$.value.public_key", equalTo("public-key")), - hasNoJsonPath("$.value.private_key"))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); - assertNoPermissions(jsonValue); + assertThat(json).hasPath("$.value.public_key").isEqualTo("public-key"); + assertThat(json).hasNoPath("$.value.private_key"); + + assertNoPermissions(json); } @Test - public void serializeWithPrivateKey() throws Exception { + public void serializeWithPrivateKey() { buildRequest(new RsaCredential(null, "private-key")); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); - assertThat(jsonValue, - allOf(hasNoJsonPath("$.value.public_key"), - hasJsonPath("$.value.private_key", equalTo("private-key")))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); + assertThat(json).hasNoPath("$.value.public_key"); + assertThat(json).hasPath("$.value.private_key").isEqualTo("private-key"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test(expected = IllegalArgumentException.class) - public void serializeWithNeitherKey() throws Exception { + public void serializeWithNeitherKey() { buildRequest(new RsaCredential(null, null)); - serializeToJson(requestBuilder); + toJsonPath(requestBuilder); } @SuppressWarnings("deprecation") diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaParametersRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaParametersRequestUnitTests.java index 10a06b3..7121df7 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaParametersRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/rsa/RsaParametersRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.rsa; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -24,10 +25,7 @@ import org.springframework.credhub.support.KeyLength; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; @SuppressWarnings("deprecation") public class RsaParametersRequestUnitTests extends CredHubRequestUnitTestsBase { @@ -37,29 +35,29 @@ public class RsaParametersRequestUnitTests extends CredHubRequestUnitTestsBase { } @Test - public void serializeWithParameters() throws Exception { + public void serializeWithParameters() { requestBuilder = RsaParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.OVERWRITE) .parameters(new RsaParameters(KeyLength.LENGTH_4096)); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); - assertThat(jsonValue, hasJsonPath("$.parameters.key_length", equalTo(4096))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); + assertThat(json).hasPath("$.parameters.key_length").isEqualTo(4096); } @Test - public void serializeWithNoParameters() throws Exception { + public void serializeWithNoParameters() { requestBuilder = RsaParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.OVERWRITE); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); - assertThat(jsonValue, hasNoJsonPath("$.parameters.key_length")); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "rsa"); + assertThat(json).hasNoPath("$.parameters.key_length"); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshCredentialDetailsUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshCredentialDetailsUnitTests.java index 139d7dd..48c1694 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshCredentialDetailsUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshCredentialDetailsUnitTests.java @@ -23,8 +23,7 @@ import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialType; import org.springframework.credhub.support.JsonParsingUnitTestsBase; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class SshCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private static final String SSH_CREDENTIALS = @@ -36,14 +35,14 @@ public class SshCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { " }"; @Test - public void deserializeDetailsWithPublicAndPrivateKeys() throws Exception { + public void deserializeDetailsWithPublicAndPrivateKeys() { CredentialDetails data = parseDetails(SSH_CREDENTIALS); assertDetails(data, "public-key", "private-key", "fingerprint"); } @Test - public void deserializeDetailsWithPublicKey() throws Exception { + public void deserializeDetailsWithPublicKey() { final String credentials = " \"type\": \"ssh\"," + " \"value\": {" + @@ -55,7 +54,7 @@ public class SshCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { } @Test - public void deserializeDetailsWithPrivateKey() throws Exception { + public void deserializeDetailsWithPrivateKey() { final String credentials = " \"type\": \"ssh\"," + " \"value\": {" + @@ -67,10 +66,10 @@ public class SshCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { } @Test - public void deserializeDetailsData() throws Exception { + public void deserializeDetailsData() { CredentialDetailsData response = parseDetailsData(SSH_CREDENTIALS); - assertThat(response.getData().size(), equalTo(1)); + assertThat(response.getData()).hasSize(1); CredentialDetails data = response.getData().get(0); @@ -81,9 +80,9 @@ public class SshCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { String publicKey, String privateKey, String publicKeyFingerprint) { assertCommonDetails(data); - assertThat(data.getCredentialType(), equalTo(CredentialType.SSH)); - assertThat(data.getValue().getPublicKey(), equalTo(publicKey)); - assertThat(data.getValue().getPrivateKey(), equalTo(privateKey)); - assertThat(data.getValue().getPublicKeyFingerprint(), equalTo(publicKeyFingerprint)); + assertThat(data.getCredentialType()).isEqualTo(CredentialType.SSH); + assertThat(data.getValue().getPublicKey()).isEqualTo(publicKey); + assertThat(data.getValue().getPrivateKey()).isEqualTo(privateKey); + assertThat(data.getValue().getPublicKeyFingerprint()).isEqualTo(publicKeyFingerprint); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshCredentialRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshCredentialRequestUnitTests.java index da93119..7a65766 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshCredentialRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshCredentialRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.ssh; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -23,11 +24,7 @@ import org.springframework.credhub.support.CredHubRequestUnitTestsBase; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; public class SshCredentialRequestUnitTests extends CredHubRequestUnitTestsBase { @Before @@ -36,50 +33,47 @@ public class SshCredentialRequestUnitTests extends CredHubRequestUnitTestsBase { } @Test - public void serializeWithPublicAndPrivateKey() throws Exception { - String jsonValue = serializeToJson(requestBuilder); + public void serializeWithPublicAndPrivateKey() { + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); - assertThat(jsonValue, - allOf(hasJsonPath("$.value.public_key", equalTo("public-key")), - hasJsonPath("$.value.private_key", equalTo("private-key")))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); + assertThat(json).hasPath("$.value.public_key").isEqualTo("public-key"); + assertThat(json).hasPath("$.value.private_key").isEqualTo("private-key"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test - public void serializeWithPublicKey() throws Exception { + public void serializeWithPublicKey() { buildRequest(new SshCredential("public-key", null)); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); - assertThat(jsonValue, - allOf(hasJsonPath("$.value.public_key", equalTo("public-key")), - hasNoJsonPath("$.value.private_key"))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); + assertThat(json).hasPath("$.value.public_key").isEqualTo("public-key"); + assertThat(json).hasNoPath("$.value.private_key"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test - public void serializeWithPrivateKey() throws Exception { + public void serializeWithPrivateKey() { buildRequest(new SshCredential(null, "private-key")); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); - assertThat(jsonValue, - allOf(hasNoJsonPath("$.value.public_key"), - hasJsonPath("$.value.private_key", equalTo("private-key")))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); + assertThat(json).hasNoPath("$.value.public_key"); + assertThat(json).hasPath("$.value.private_key").isEqualTo("private-key"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test(expected = IllegalArgumentException.class) - public void serializeWithNeitherKey() throws Exception { + public void serializeWithNeitherKey() { buildRequest(new SshCredential(null, null)); - serializeToJson(requestBuilder); + toJsonPath(requestBuilder); } @SuppressWarnings("deprecation") diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshParametersRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshParametersRequestUnitTests.java index 309b413..6ac278e 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshParametersRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ssh/SshParametersRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.ssh; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -24,11 +25,7 @@ import org.springframework.credhub.support.KeyLength; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; @SuppressWarnings("deprecation") public class SshParametersRequestUnitTests extends CredHubRequestUnitTestsBase { @@ -36,66 +33,63 @@ public class SshParametersRequestUnitTests extends CredHubRequestUnitTestsBase { public void setUp() { requestBuilder = SshParametersRequest.builder(); } + @Test - public void serializeWithParameters() throws Exception { + public void serializeWithParameters() { requestBuilder = SshParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.OVERWRITE) .parameters(new SshParameters(KeyLength.LENGTH_2048, "ssh comment")); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); - assertThat(jsonValue, - allOf(hasJsonPath("$.parameters.key_length", equalTo(2048)), - hasJsonPath("$.parameters.ssh_comment", equalTo("ssh comment")))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); + assertThat(json).hasPath("$.parameters.key_length").isEqualTo(2048); + assertThat(json).hasPath("$.parameters.ssh_comment").isEqualTo("ssh comment"); } @Test - public void serializeWithLengthParameter() throws Exception { + public void serializeWithLengthParameter() { requestBuilder = SshParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.NO_OVERWRITE) .parameters(new SshParameters(KeyLength.LENGTH_2048)); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.NO_OVERWRITE, "/example/credential", "ssh"); - assertThat(jsonValue, - allOf(hasJsonPath("$.parameters.key_length", equalTo(2048)), - hasNoJsonPath("$.parameters.ssh_comment"))); + assertCommonRequestFields(json, true, WriteMode.NO_OVERWRITE, "/example/credential", "ssh"); + assertThat(json).hasPath("$.parameters.key_length").isEqualTo(2048); + assertThat(json).hasNoPath("$.parameters.ssh_comment"); } @Test - public void serializeWithCommentParameter() throws Exception { + public void serializeWithCommentParameter() { requestBuilder = SshParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.CONVERGE) .parameters(new SshParameters("ssh comment")); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.CONVERGE, "/example/credential", "ssh"); - assertThat(jsonValue, - allOf(hasNoJsonPath("$.parameters.key_length"), - hasJsonPath("$.parameters.ssh_comment", equalTo("ssh comment")))); + assertCommonRequestFields(json, true, WriteMode.CONVERGE, "/example/credential", "ssh"); + assertThat(json).hasNoPath("$.parameters.key_length"); + assertThat(json).hasPath("$.parameters.ssh_comment").isEqualTo("ssh comment"); } @Test - public void serializeWithNoParameters() throws Exception { + public void serializeWithNoParameters() { requestBuilder = SshParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.OVERWRITE); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); - assertThat(jsonValue, - allOf(hasNoJsonPath("$.parameters.key_length"), - hasNoJsonPath("$.parameters.ssh_comment"))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "ssh"); + assertThat(json).hasNoPath("$.parameters.key_length"); + assertThat(json).hasNoPath("$.parameters.ssh_comment"); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserCredentialDetailsUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserCredentialDetailsUnitTests.java index b6eddf6..46bc3ee 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserCredentialDetailsUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserCredentialDetailsUnitTests.java @@ -23,8 +23,7 @@ import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialType; import org.springframework.credhub.support.JsonParsingUnitTestsBase; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class UserCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private static final String USER_CREDENTIALS = @@ -36,17 +35,17 @@ public class UserCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { " }"; @Test - public void deserializeDetails() throws Exception { + public void deserializeDetails() { CredentialDetails data = parseDetails(USER_CREDENTIALS); assertDetails(data); } @Test - public void deserializeDetailsData() throws Exception { + public void deserializeDetailsData() { CredentialDetailsData response = parseDetailsData(USER_CREDENTIALS); - assertThat(response.getData().size(), equalTo(1)); + assertThat(response.getData()).hasSize(1); CredentialDetails data = response.getData().get(0); @@ -56,9 +55,9 @@ public class UserCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private void assertDetails(CredentialDetails data) { assertCommonDetails(data); - assertThat(data.getCredentialType(), equalTo(CredentialType.USER)); - assertThat(data.getValue().getUsername(), equalTo("myname")); - assertThat(data.getValue().getPassword(), equalTo("secret")); - assertThat(data.getValue().getPasswordHash(), equalTo("secret-hash")); + assertThat(data.getCredentialType()).isEqualTo(CredentialType.USER); + assertThat(data.getValue().getUsername()).isEqualTo("myname"); + assertThat(data.getValue().getPassword()).isEqualTo("secret"); + assertThat(data.getValue().getPasswordHash()).isEqualTo("secret-hash"); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserCredentialRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserCredentialRequestUnitTests.java index 74e504c..8e02b39 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserCredentialRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserCredentialRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.user; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -24,11 +25,7 @@ import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; import org.springframework.credhub.support.user.UserCredentialRequest.UserCredentialRequestBuilder; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; @SuppressWarnings("deprecation") public class UserCredentialRequestUnitTests extends CredHubRequestUnitTestsBase { @@ -42,34 +39,32 @@ public class UserCredentialRequestUnitTests extends CredHubRequestUnitTestsBase } @Test - public void serializeWithUsernameAndPassword() throws Exception { - String jsonValue = serializeToJson(requestBuilder); + public void serializeWithUsernameAndPassword() { + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "user"); - assertThat(jsonValue, - allOf(hasJsonPath("$.value.username", equalTo("myname")), - hasJsonPath("$.value.password", equalTo("secret")), - hasNoJsonPath("$.value.password_hash"))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "user"); + assertThat(json).hasPath("$.value.username").isEqualTo("myname"); + assertThat(json).hasPath("$.value.password").isEqualTo("secret"); + assertThat(json).hasNoPath("$.value.password_hash"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test - public void serializeWithPassword() throws Exception { + public void serializeWithPassword() { UserCredentialRequestBuilder builder = UserCredentialRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.OVERWRITE) .value(new UserCredential("secret")); - String jsonValue = serializeToJson(builder); + DocumentContext json = toJsonPath(builder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "user"); - assertThat(jsonValue, - allOf(hasNoJsonPath("$.value.username"), - hasJsonPath("$.value.password", equalTo("secret")), - hasNoJsonPath("$.value.password_hash"))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "user"); + assertThat(json).hasNoPath("$.value.username"); + assertThat(json).hasPath("$.value.password").isEqualTo("secret"); + assertThat(json).hasNoPath("$.value.password_hash"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserParametersRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserParametersRequestUnitTests.java index e6cf2be..d5fa73d 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserParametersRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/user/UserParametersRequestUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.support.user; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; @@ -24,11 +25,7 @@ import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.WriteMode; import org.springframework.credhub.support.password.PasswordParameters; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasNoJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; @SuppressWarnings("deprecation") public class UserParametersRequestUnitTests extends CredHubRequestUnitTestsBase { @@ -38,7 +35,7 @@ public class UserParametersRequestUnitTests extends CredHubRequestUnitTestsBase } @Test - public void serializeWithParameters() throws Exception { + public void serializeWithParameters() { requestBuilder = UserParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) @@ -52,52 +49,50 @@ public class UserParametersRequestUnitTests extends CredHubRequestUnitTestsBase .includeSpecial(false) .build()); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.OVERWRITE, "/example/credential", "user"); - assertThat(jsonValue, - allOf(hasJsonPath("$.value.username", equalTo("user")), - hasJsonPath("$.parameters.length", equalTo(20)), - hasJsonPath("$.parameters.exclude_lower", equalTo(true)), - hasJsonPath("$.parameters.exclude_upper", equalTo(false)), - hasJsonPath("$.parameters.exclude_number", equalTo(true)), - hasJsonPath("$.parameters.include_special", equalTo(false)))); + assertCommonRequestFields(json, true, WriteMode.OVERWRITE, "/example/credential", "user"); + assertThat(json).hasPath("$.value.username").isEqualTo("user"); + assertThat(json).hasPath("$.parameters.length").isEqualTo(20); + assertThat(json).hasPath("$.parameters.exclude_lower").isEqualTo(true); + assertThat(json).hasPath("$.parameters.exclude_upper").isEqualTo(false); + assertThat(json).hasPath("$.parameters.exclude_number").isEqualTo(true); + assertThat(json).hasPath("$.parameters.include_special").isEqualTo(false); } @Test - public void serializeWithEmptyParameters() throws Exception { + public void serializeWithEmptyParameters() { requestBuilder = UserParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.NO_OVERWRITE) .parameters(new PasswordParameters()); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.NO_OVERWRITE, "/example/credential", "user"); - assertParametersNotSet(jsonValue); + assertCommonRequestFields(json, true, WriteMode.NO_OVERWRITE, "/example/credential", "user"); + assertParametersNotSet(json); } @Test - public void serializeWithNoParameters() throws Exception { + public void serializeWithNoParameters() { requestBuilder = UserParametersRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .mode(WriteMode.CONVERGE); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertCommonRequestFields(jsonValue, true, WriteMode.CONVERGE, "/example/credential", "user"); - assertParametersNotSet(jsonValue); + assertCommonRequestFields(json, true, WriteMode.CONVERGE, "/example/credential", "user"); + assertParametersNotSet(json); } - private void assertParametersNotSet(String jsonValue) { - assertThat(jsonValue, - allOf(hasNoJsonPath("$.value.username"), - hasNoJsonPath("$.parameters.length"), - hasNoJsonPath("$.parameters.exclude_lower"), - hasNoJsonPath("$.parameters.exclude_upper"), - hasNoJsonPath("$.parameters.exclude_number"), - hasNoJsonPath("$.parameters.include_special"))); + private void assertParametersNotSet(DocumentContext json) { + assertThat(json).hasNoPath("$.value.username"); + assertThat(json).hasNoPath("$.parameters.length"); + assertThat(json).hasNoPath("$.parameters.exclude_lower"); + assertThat(json).hasNoPath("$.parameters.exclude_upper"); + assertThat(json).hasNoPath("$.parameters.exclude_number"); + assertThat(json).hasNoPath("$.parameters.include_special"); } } \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/value/ValueCredentialDetailsUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/value/ValueCredentialDetailsUnitTests.java index c9d550c..98b6b96 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/value/ValueCredentialDetailsUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/value/ValueCredentialDetailsUnitTests.java @@ -23,8 +23,7 @@ import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialType; import org.springframework.credhub.support.JsonParsingUnitTestsBase; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.assertj.core.api.Assertions.assertThat; public class ValueCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private static final String VALUE_CREDENTIALS = @@ -32,7 +31,7 @@ public class ValueCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { " \"value\": \"somevalue\""; @Test - public void deserializeDetails() throws Exception { + public void deserializeDetails() { CredentialDetails data = parseDetails(VALUE_CREDENTIALS); @@ -40,11 +39,11 @@ public class ValueCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { } @Test - public void deserializeDetailsData() throws Exception { + public void deserializeDetailsData() { CredentialDetailsData response = parseDetailsData(VALUE_CREDENTIALS); - assertThat(response.getData().size(), equalTo(1)); + assertThat(response.getData()).hasSize(1); CredentialDetails data = response.getData().get(0); @@ -54,7 +53,7 @@ public class ValueCredentialDetailsUnitTests extends JsonParsingUnitTestsBase { private void assertDetails(CredentialDetails data) { assertCommonDetails(data); - assertThat(data.getCredentialType(), equalTo(CredentialType.VALUE)); - assertThat(data.getValue().getValue(), equalTo("somevalue")); + assertThat(data.getCredentialType()).isEqualTo(CredentialType.VALUE); + assertThat(data.getValue().getValue()).isEqualTo("somevalue"); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/value/ValueCredentialRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/value/ValueCredentialRequestUnitTests.java index 4eb0779..f064d9f 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/value/ValueCredentialRequestUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/value/ValueCredentialRequestUnitTests.java @@ -16,20 +16,19 @@ package org.springframework.credhub.support.value; +import com.jayway.jsonpath.DocumentContext; import org.junit.Before; import org.junit.Test; import org.springframework.credhub.support.CredHubRequestUnitTestsBase; import org.springframework.credhub.support.SimpleCredentialName; -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; +import static org.springframework.credhub.support.JsonPathAssert.assertThat; public class ValueCredentialRequestUnitTests extends CredHubRequestUnitTestsBase { @Before + @SuppressWarnings("deprecation") public void setUp() { requestBuilder = ValueCredentialRequest.builder() .name(new SimpleCredentialName("example", "credential")) @@ -38,33 +37,32 @@ public class ValueCredentialRequestUnitTests extends CredHubRequestUnitTestsBase } @Test - public void serializeWithValue() throws Exception { - String jsonValue = serializeToJson(requestBuilder); + public void serializeWithValue() { + DocumentContext json = toJsonPath(requestBuilder); - assertThat(jsonValue, - allOf(hasJsonPath("$.overwrite", equalTo(true)), - hasJsonPath("$.name", equalTo("/example/credential")), - hasJsonPath("$.type", equalTo("value")), - hasJsonPath("$.value", equalTo("somevalue")))); + assertThat(json).hasPath("$.overwrite").isEqualTo(true); + assertThat(json).hasPath("$.name").isEqualTo("/example/credential"); + assertThat(json).hasPath("$.type").isEqualTo("value"); + assertThat(json).hasPath("$.value").isEqualTo("somevalue"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } @Test - public void serializeWithStringValue() throws Exception { + @SuppressWarnings("deprecation") + public void serializeWithStringValue() { requestBuilder = ValueCredentialRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) .value("somevalue"); - String jsonValue = serializeToJson(requestBuilder); + DocumentContext json = toJsonPath(requestBuilder); - assertThat(jsonValue, - allOf(hasJsonPath("$.overwrite", equalTo(true)), - hasJsonPath("$.name", equalTo("/example/credential")), - hasJsonPath("$.type", equalTo("value")), - hasJsonPath("$.value", equalTo("somevalue")))); + assertThat(json).hasPath("$.overwrite").isEqualTo(true); + assertThat(json).hasPath("$.name").isEqualTo("/example/credential"); + assertThat(json).hasPath("$.type").isEqualTo("value"); + assertThat(json).hasPath("$.value").isEqualTo("somevalue"); - assertNoPermissions(jsonValue); + assertNoPermissions(json); } } \ No newline at end of file diff --git a/spring-credhub-starter/build.gradle b/spring-credhub-starter/build.gradle index 3a8f17e..33090e8 100644 --- a/spring-credhub-starter/build.gradle +++ b/spring-credhub-starter/build.gradle @@ -43,4 +43,5 @@ dependencies { optional group: 'io.netty', name: 'netty-all', version: '4.1.8.Final' testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test' + testCompile group: 'org.assertj', name: 'assertj-core', version: "${assertJVersion}" } diff --git a/spring-credhub-starter/src/test/java/org/springframework/credhub/configuration/CredHubOAuth2TemplateAutoConfigurationTest.java b/spring-credhub-starter/src/test/java/org/springframework/credhub/configuration/CredHubOAuth2TemplateAutoConfigurationTest.java index 6e0c644..611c267 100644 --- a/spring-credhub-starter/src/test/java/org/springframework/credhub/configuration/CredHubOAuth2TemplateAutoConfigurationTest.java +++ b/spring-credhub-starter/src/test/java/org/springframework/credhub/configuration/CredHubOAuth2TemplateAutoConfigurationTest.java @@ -13,8 +13,6 @@ import org.springframework.security.oauth2.client.token.grant.client.ClientCrede import org.springframework.test.context.junit4.SpringRunner; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; /** * @author Daniel Lavoie @@ -37,13 +35,13 @@ public class CredHubOAuth2TemplateAutoConfigurationTest { @Test public void contextLoads() { - assertNotNull(credHubOperations); - assertThat(credHubOperations instanceof OAuth2CredHubTemplate); + assertThat(credHubOperations).isNotNull(); + assertThat(credHubOperations).isInstanceOf(OAuth2CredHubTemplate.class); - assertNotNull(credentialsDetails); - assertEquals("test-user", credentialsDetails.getClientId()); - assertEquals("test-secret", credentialsDetails.getClientSecret()); - assertEquals("https://uaa.example.com/oauth/token", credentialsDetails.getAccessTokenUri()); + assertThat(credentialsDetails).isNotNull(); + assertThat("test-user").isEqualTo(credentialsDetails.getClientId()); + assertThat("test-secret").isEqualTo(credentialsDetails.getClientSecret()); + assertThat("https://uaa.example.com/oauth/token").isEqualTo(credentialsDetails.getAccessTokenUri()); } @SpringBootApplication diff --git a/spring-credhub-starter/src/test/java/org/springframework/credhub/configuration/CredHubTemplateAutoConfigurationTest.java b/spring-credhub-starter/src/test/java/org/springframework/credhub/configuration/CredHubTemplateAutoConfigurationTest.java index f735094..8471bd4 100644 --- a/spring-credhub-starter/src/test/java/org/springframework/credhub/configuration/CredHubTemplateAutoConfigurationTest.java +++ b/spring-credhub-starter/src/test/java/org/springframework/credhub/configuration/CredHubTemplateAutoConfigurationTest.java @@ -10,9 +10,7 @@ import org.springframework.credhub.core.CredHubTemplate; import org.springframework.credhub.core.OAuth2CredHubTemplate; import org.springframework.test.context.junit4.SpringRunner; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; /** * @author Daniel Lavoie @@ -28,10 +26,10 @@ public class CredHubTemplateAutoConfigurationTest { @Test public void contextLoads() { - assertNotNull(credHubTemplate); - assertTrue(credHubTemplate instanceof CredHubTemplate); + assertThat(credHubTemplate).isNotNull(); + assertThat(credHubTemplate).isInstanceOf(CredHubTemplate.class); - assertNull(oauth2CredHubTemplate); + assertThat(oauth2CredHubTemplate).isNull(); } @SpringBootApplication