From 8a1cc78d75bc32a3456a84e8a3f1674e8e8efba1 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Tue, 23 Oct 2018 15:37:58 -0500 Subject: [PATCH] Add InfoOperations and version operation. Put operations into subpackages. --- ...InterpolationServiceDataPostProcessor.java | 4 +- ...polationServiceDataPostProcessorTests.java | 2 +- .../credhub/core/CredHubOperations.java | 18 ++++- .../credhub/core/CredHubTemplate.java | 32 +++++++-- .../credhub/core/ExceptionUtils.java | 4 +- .../CredHubCertificateOperations.java | 2 +- .../CredHubCertificateTemplate.java | 7 +- .../CredHubCredentialOperations.java} | 4 +- .../CredHubCredentialTemplate.java} | 11 +-- .../core/info/CredHubInfoOperations.java | 35 ++++++++++ .../core/info/CredHubInfoTemplate.java | 64 +++++++++++++++++ .../CredHubInterpolationOperations.java | 2 +- .../CredHubInterpolationTemplate.java | 7 +- .../CredHubPermissionOperations.java} | 4 +- .../CredHubPermissionTemplate.java} | 10 +-- .../credhub/support/info/VersionInfo.java | 69 +++++++++++++++++++ .../CredHubCertificateTemplateUnitTests.java | 7 +- ...ubCredentialTemplateSummaryUnitTests.java} | 9 +-- .../CredHubCredentialTemplateUnitTests.java} | 8 +-- ...edHubCredentialTemplateUnitTestsBase.java} | 7 +- ...HubTemplateDetailCertificateUnitTests.java | 2 +- .../CredHubTemplateDetailJsonUnitTests.java | 2 +- ...redHubTemplateDetailPasswordUnitTests.java | 2 +- .../CredHubTemplateDetailRsaUnitTests.java | 2 +- .../CredHubTemplateDetailSshUnitTests.java | 2 +- .../CredHubTemplateDetailUnitTestsBase.java | 17 ++--- .../CredHubTemplateDetailUserUnitTests.java | 2 +- .../CredHubTemplateDetailValueUnitTests.java | 2 +- .../info/CredHubInfoTemplateUnitTests.java | 63 +++++++++++++++++ ...CredHubInterpolationTemplateUnitTests.java | 5 +- .../CredHubPermissionsTemplateUnitTests.java | 11 +-- .../support/info/VersionInfoTests.java | 50 ++++++++++++++ .../integration/CredHubIntegrationTests.java | 24 +++++++ .../CredentialIntegrationTests.java | 40 +++++------ .../integration/InfoIntegrationTests.java | 40 +++++++++++ ...s.java => PermissionIntegrationTests.java} | 44 ++++++------ 36 files changed, 502 insertions(+), 112 deletions(-) rename spring-credhub-core/src/main/java/org/springframework/credhub/core/{ => certificate}/CredHubCertificateOperations.java (96%) rename spring-credhub-core/src/main/java/org/springframework/credhub/core/{ => certificate}/CredHubCertificateTemplate.java (89%) rename spring-credhub-core/src/main/java/org/springframework/credhub/core/{CredHubCredentialsOperations.java => credential/CredHubCredentialOperations.java} (98%) rename spring-credhub-core/src/main/java/org/springframework/credhub/core/{CredHubCredentialsTemplate.java => credential/CredHubCredentialTemplate.java} (95%) create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/core/info/CredHubInfoOperations.java create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/core/info/CredHubInfoTemplate.java rename spring-credhub-core/src/main/java/org/springframework/credhub/core/{ => interpolation}/CredHubInterpolationOperations.java (98%) rename spring-credhub-core/src/main/java/org/springframework/credhub/core/{ => interpolation}/CredHubInterpolationTemplate.java (86%) rename spring-credhub-core/src/main/java/org/springframework/credhub/core/{CredHubPermissionsOperations.java => permission/CredHubPermissionOperations.java} (94%) rename spring-credhub-core/src/main/java/org/springframework/credhub/core/{CredHubPermissionsTemplate.java => permission/CredHubPermissionTemplate.java} (89%) create mode 100644 spring-credhub-core/src/main/java/org/springframework/credhub/support/info/VersionInfo.java rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => certificate}/CredHubCertificateTemplateUnitTests.java (90%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{CredHubCredentialsTemplateSummaryUnitTests.java => credential/CredHubCredentialTemplateSummaryUnitTests.java} (89%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{CredHubCredentialsTemplateUnitTests.java => credential/CredHubCredentialTemplateUnitTests.java} (82%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{CredHubCredentialsTemplateUnitTestsBase.java => credential/CredHubCredentialTemplateUnitTestsBase.java} (85%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => credential}/CredHubTemplateDetailCertificateUnitTests.java (98%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => credential}/CredHubTemplateDetailJsonUnitTests.java (98%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => credential}/CredHubTemplateDetailPasswordUnitTests.java (98%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => credential}/CredHubTemplateDetailRsaUnitTests.java (98%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => credential}/CredHubTemplateDetailSshUnitTests.java (98%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => credential}/CredHubTemplateDetailUnitTestsBase.java (92%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => credential}/CredHubTemplateDetailUserUnitTests.java (98%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => credential}/CredHubTemplateDetailValueUnitTests.java (98%) create mode 100644 spring-credhub-core/src/test/java/org/springframework/credhub/core/info/CredHubInfoTemplateUnitTests.java rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => interpolation}/CredHubInterpolationTemplateUnitTests.java (93%) rename spring-credhub-core/src/test/java/org/springframework/credhub/core/{ => permission}/CredHubPermissionsTemplateUnitTests.java (88%) create mode 100644 spring-credhub-core/src/test/java/org/springframework/credhub/support/info/VersionInfoTests.java create mode 100644 spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredHubIntegrationTests.java create mode 100644 spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/InfoIntegrationTests.java rename spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/{PermissionsIntegrationTests.java => PermissionIntegrationTests.java} (66%) diff --git a/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessor.java b/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessor.java index c9f0210..f938d3e 100644 --- a/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessor.java +++ b/spring-credhub-cloud-connector/src/main/java/org/springframework/credhub/cloud/CredHubInterpolationServiceDataPostProcessor.java @@ -22,7 +22,7 @@ import java.util.logging.Logger; import org.springframework.cloud.cloudfoundry.CloudFoundryRawServiceData; import org.springframework.cloud.cloudfoundry.ServiceDataPostProcessor; import org.springframework.credhub.configuration.CredHubTemplateFactory; -import org.springframework.credhub.core.CredHubInterpolationOperations; +import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations; import org.springframework.credhub.core.CredHubOperations; import org.springframework.credhub.core.CredHubProperties; import org.springframework.credhub.support.ServicesData; @@ -82,7 +82,7 @@ public class CredHubInterpolationServiceDataPostProcessor /** * Process the provided {@literal serviceData} parsed from {@literal VCAP_SERVICES} by * Spring Cloud Connectors using the - * {@link org.springframework.credhub.core.CredHubInterpolationOperations#interpolateServiceData(ServicesData)} API. + * {@link CredHubInterpolationOperations#interpolateServiceData(ServicesData)} API. * * @param serviceData raw service data parsed from {@literal VCAP_SERVICES} * @return serviceData with CredHub references replaced by stored credentials 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 8ccf0bc..5d94b1a 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 @@ -29,7 +29,7 @@ import org.mockito.junit.MockitoJUnitRunner; import org.springframework.cloud.cloudfoundry.CloudFoundryRawServiceData; import org.springframework.credhub.core.CredHubException; -import org.springframework.credhub.core.CredHubInterpolationOperations; +import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations; import org.springframework.credhub.support.ServicesData; import org.springframework.http.HttpStatus; diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java index 492a278..32684b1 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubOperations.java @@ -16,6 +16,11 @@ package org.springframework.credhub.core; +import org.springframework.credhub.core.certificate.CredHubCertificateOperations; +import org.springframework.credhub.core.credential.CredHubCredentialOperations; +import org.springframework.credhub.core.info.CredHubInfoOperations; +import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations; +import org.springframework.credhub.core.permission.CredHubPermissionOperations; import org.springframework.web.client.RestTemplate; /** @@ -29,14 +34,14 @@ public interface CredHubOperations { * * @return the credentials operations */ - CredHubCredentialsOperations credentials(); + CredHubCredentialOperations credentials(); /** * Get the operations for adding, retrieving, and deleting credential permissions. * * @return the permissions operations */ - CredHubPermissionsOperations permissions(); + CredHubPermissionOperations permissions(); /** * Get the operations for retrieving, regenerating, and updating certificates. @@ -46,12 +51,19 @@ public interface CredHubOperations { CredHubCertificateOperations certificates(); /** - * Get the operations interpolating service binding credentials. + * Get the operations for interpolating service binding credentials. * * @return the interpolation operations */ CredHubInterpolationOperations interpolation(); + /** + * Get the operations for retrieving CredHub server information. + * + * @return the info operations + */ + CredHubInfoOperations info(); + /** * Allow interaction with the configured {@link RestTemplate} not provided * by other methods. diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java index 0cab39c..dbd0dc8 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubTemplate.java @@ -16,6 +16,16 @@ package org.springframework.credhub.core; +import org.springframework.credhub.core.certificate.CredHubCertificateOperations; +import org.springframework.credhub.core.certificate.CredHubCertificateTemplate; +import org.springframework.credhub.core.credential.CredHubCredentialOperations; +import org.springframework.credhub.core.credential.CredHubCredentialTemplate; +import org.springframework.credhub.core.info.CredHubInfoOperations; +import org.springframework.credhub.core.info.CredHubInfoTemplate; +import org.springframework.credhub.core.interpolation.CredHubInterpolationOperations; +import org.springframework.credhub.core.interpolation.CredHubInterpolationTemplate; +import org.springframework.credhub.core.permission.CredHubPermissionOperations; +import org.springframework.credhub.core.permission.CredHubPermissionTemplate; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.util.Assert; import org.springframework.web.client.HttpStatusCodeException; @@ -35,7 +45,7 @@ public class CredHubTemplate implements CredHubOperations { * * @param restTemplate the {@link RestTemplate} to use for interactions with CredHub */ - CredHubTemplate(RestTemplate restTemplate) { + public CredHubTemplate(RestTemplate restTemplate) { Assert.notNull(restTemplate, "restTemplate must not be null"); this.restTemplate = restTemplate; @@ -64,8 +74,8 @@ public class CredHubTemplate implements CredHubOperations { * @return the credentials operations */ @Override - public CredHubCredentialsOperations credentials() { - return new CredHubCredentialsTemplate(this); + public CredHubCredentialOperations credentials() { + return new CredHubCredentialTemplate(this); } /** @@ -74,8 +84,8 @@ public class CredHubTemplate implements CredHubOperations { * @return the permissions operations */ @Override - public CredHubPermissionsOperations permissions() { - return new CredHubPermissionsTemplate(this); + public CredHubPermissionOperations permissions() { + return new CredHubPermissionTemplate(this); } /** @@ -89,7 +99,7 @@ public class CredHubTemplate implements CredHubOperations { } /** - * Get the operations interpolating service binding credentials. + * Get the operations for interpolating service binding credentials. * * @return the interpolation operations */ @@ -98,6 +108,16 @@ public class CredHubTemplate implements CredHubOperations { return new CredHubInterpolationTemplate(this); } + /** + * Get the operations for retrieving CredHub server information. + * + * @return the info operations + */ + @Override + public CredHubInfoOperations info() { + return new CredHubInfoTemplate(this); + } + /** * Allow interaction with the configured {@link RestTemplate} not provided * by other methods. diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/ExceptionUtils.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/ExceptionUtils.java index a8a00d2..33a21db 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/ExceptionUtils.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/ExceptionUtils.java @@ -20,14 +20,14 @@ import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.client.RestTemplate; -class ExceptionUtils { +public class ExceptionUtils { /** * Helper method to throw an appropriate exception if a request to CredHub * returns with an error code. * * @param response a {@link ResponseEntity} returned from {@link RestTemplate} */ - static void throwExceptionOnError(ResponseEntity response) { + public static void throwExceptionOnError(ResponseEntity response) { if (!response.getStatusCode().equals(HttpStatus.OK)) { throw new CredHubException(response.getStatusCode()); } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCertificateOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateOperations.java similarity index 96% rename from spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCertificateOperations.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateOperations.java index fbddd53..621a7fe 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCertificateOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateOperations.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.certificate; import org.springframework.credhub.support.CertificateSummary; import org.springframework.credhub.support.CredentialName; diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCertificateTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplate.java similarity index 89% rename from spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCertificateTemplate.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplate.java index c0671bf..de64f56 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCertificateTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplate.java @@ -14,8 +14,11 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.certificate; +import org.springframework.credhub.core.CredHubOperations; +import org.springframework.credhub.core.ExceptionUtils; +import org.springframework.credhub.core.RestOperationsCallback; import org.springframework.credhub.support.CertificateSummary; import org.springframework.credhub.support.CertificateSummaryData; import org.springframework.credhub.support.CredentialName; @@ -42,7 +45,7 @@ public class CredHubCertificateTemplate implements CredHubCertificateOperations * * @param credHubOperations the {@link CredHubOperations} to use for interactions with CredHub */ - CredHubCertificateTemplate(CredHubOperations credHubOperations) { + public CredHubCertificateTemplate(CredHubOperations credHubOperations) { this.credHubOperations = credHubOperations; } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialOperations.java similarity index 98% rename from spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsOperations.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialOperations.java index a07ba90..0d77a7a 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialOperations.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import org.springframework.credhub.support.CredentialDetails; import org.springframework.credhub.support.CredentialName; @@ -31,7 +31,7 @@ import java.util.List; * * @author Scott Frederick */ -public interface CredHubCredentialsOperations { +public interface CredHubCredentialOperations { /** * Write a new credential to CredHub, or overwrite an existing credential with a new * value. diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialTemplate.java similarity index 95% rename from spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsTemplate.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialTemplate.java index 7b5e023..ab96169 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubCredentialsTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/credential/CredHubCredentialTemplate.java @@ -14,9 +14,12 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import org.springframework.core.ParameterizedTypeReference; +import org.springframework.credhub.core.CredHubOperations; +import org.springframework.credhub.core.ExceptionUtils; +import org.springframework.credhub.core.RestOperationsCallback; import org.springframework.credhub.support.CredentialDetails; import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialName; @@ -45,7 +48,7 @@ import static org.springframework.http.HttpMethod.PUT; * * @author Scott Frederick */ -public class CredHubCredentialsTemplate implements CredHubCredentialsOperations { +public class CredHubCredentialTemplate implements CredHubCredentialOperations { static final String BASE_URL_PATH = "/api/v1/data"; static final String ID_URL_PATH = BASE_URL_PATH + "/{id}"; static final String NAME_URL_QUERY = BASE_URL_PATH + "?name={name}"; @@ -59,11 +62,11 @@ public class CredHubCredentialsTemplate implements CredHubCredentialsOperations private CredHubOperations credHubOperations; /** - * Create a new {@link CredHubCredentialsTemplate}. + * Create a new {@link CredHubCredentialTemplate}. * * @param credHubOperations the {@link CredHubOperations} to use for interactions with CredHub */ - CredHubCredentialsTemplate(CredHubOperations credHubOperations) { + public CredHubCredentialTemplate(CredHubOperations credHubOperations) { this.credHubOperations = credHubOperations; } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/info/CredHubInfoOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/info/CredHubInfoOperations.java new file mode 100644 index 0000000..9980189 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/info/CredHubInfoOperations.java @@ -0,0 +1,35 @@ +/* + * 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.core.info; + +import org.springframework.credhub.support.info.VersionInfo; + +/** + * Specifies the interactions with CredHub for retrieving server information. + * + * @author Scott Frederick + */ +public interface CredHubInfoOperations { + + /** + * Retrieve the version information from the CredHub server. + * + * @return the server version information + */ + VersionInfo version(); + +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/info/CredHubInfoTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/info/CredHubInfoTemplate.java new file mode 100644 index 0000000..af053d8 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/info/CredHubInfoTemplate.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.core.info; + +import org.springframework.credhub.core.CredHubOperations; +import org.springframework.credhub.core.ExceptionUtils; +import org.springframework.credhub.core.RestOperationsCallback; +import org.springframework.credhub.support.info.VersionInfo; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestOperations; + +/** + * Implements the interaction with CredHub retrieve server information. + * + * @author Scott Frederick + */ +public class CredHubInfoTemplate implements CredHubInfoOperations { + static final String VERSION_URL_PATH = "/version"; + + private CredHubOperations credHubOperations; + + /** + * Create a new {@link CredHubInfoTemplate}. + * + * @param credHubOperations the {@link CredHubOperations} to use for interactions with CredHub + */ + public CredHubInfoTemplate(CredHubOperations credHubOperations) { + this.credHubOperations = credHubOperations; + } + + /** + * Retrieve the version information from the CredHub server. + * + * @return the server version information + */ + @Override + public VersionInfo version() { + return credHubOperations.doWithRest(new RestOperationsCallback() { + @Override + public VersionInfo doWithRestOperations(RestOperations restOperations) { + ResponseEntity response = restOperations + .getForEntity(VERSION_URL_PATH, VersionInfo.class); + + ExceptionUtils.throwExceptionOnError(response); + + return response.getBody(); + } + }); + } +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubInterpolationOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/interpolation/CredHubInterpolationOperations.java similarity index 98% rename from spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubInterpolationOperations.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/core/interpolation/CredHubInterpolationOperations.java index d0a9cb3..fcd150d 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubInterpolationOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/interpolation/CredHubInterpolationOperations.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.interpolation; import org.springframework.credhub.support.ServicesData; diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubInterpolationTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/interpolation/CredHubInterpolationTemplate.java similarity index 86% rename from spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubInterpolationTemplate.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/core/interpolation/CredHubInterpolationTemplate.java index 24e2831..5db5464 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubInterpolationTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/interpolation/CredHubInterpolationTemplate.java @@ -14,8 +14,11 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.interpolation; +import org.springframework.credhub.core.CredHubOperations; +import org.springframework.credhub.core.ExceptionUtils; +import org.springframework.credhub.core.RestOperationsCallback; import org.springframework.credhub.support.ServicesData; import org.springframework.http.HttpEntity; import org.springframework.http.ResponseEntity; @@ -39,7 +42,7 @@ public class CredHubInterpolationTemplate implements CredHubInterpolationOperati * * @param credHubOperations the {@link CredHubOperations} to use for interactions with CredHub */ - CredHubInterpolationTemplate(CredHubOperations credHubOperations) { + public CredHubInterpolationTemplate(CredHubOperations credHubOperations) { this.credHubOperations = credHubOperations; } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubPermissionsOperations.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionOperations.java similarity index 94% rename from spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubPermissionsOperations.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionOperations.java index e1f6995..21e4f83 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubPermissionsOperations.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionOperations.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.permission; import org.springframework.credhub.support.CredentialName; import org.springframework.credhub.support.permissions.Actor; @@ -27,7 +27,7 @@ import java.util.List; * * @author Scott Frederick */ -public interface CredHubPermissionsOperations { +public interface CredHubPermissionOperations { /** * Get the permissions associated with a credential. * diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubPermissionsTemplate.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionTemplate.java similarity index 89% rename from spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubPermissionsTemplate.java rename to spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionTemplate.java index 83d4857..312f5b0 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/CredHubPermissionsTemplate.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/permission/CredHubPermissionTemplate.java @@ -14,8 +14,10 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.permission; +import org.springframework.credhub.core.CredHubOperations; +import org.springframework.credhub.core.RestOperationsCallback; import org.springframework.credhub.support.CredentialName; import org.springframework.credhub.support.CredentialPermissions; import org.springframework.credhub.support.permissions.Actor; @@ -35,7 +37,7 @@ import static org.springframework.http.HttpMethod.POST; * * @author Scott Frederick */ -public class CredHubPermissionsTemplate implements CredHubPermissionsOperations { +public class CredHubPermissionTemplate implements CredHubPermissionOperations { static final String PERMISSIONS_URL_PATH = "/api/v1/permissions"; static final String PERMISSIONS_URL_QUERY = PERMISSIONS_URL_PATH + "?credential_name={name}"; static final String PERMISSIONS_ACTOR_URL_QUERY = PERMISSIONS_URL_QUERY + "&actor={actor}"; @@ -43,11 +45,11 @@ public class CredHubPermissionsTemplate implements CredHubPermissionsOperations private CredHubOperations credHubOperations; /** - * Create a new {@link CredHubPermissionsTemplate}. + * Create a new {@link CredHubPermissionTemplate}. * * @param credHubOperations the {@link CredHubOperations} to use for interactions with CredHub */ - CredHubPermissionsTemplate(CredHubOperations credHubOperations) { + public CredHubPermissionTemplate(CredHubOperations credHubOperations) { this.credHubOperations = credHubOperations; } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/info/VersionInfo.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/info/VersionInfo.java new file mode 100644 index 0000000..01c11c8 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/info/VersionInfo.java @@ -0,0 +1,69 @@ +/* + * 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.info; + +/** + * Version information about a CredHub server. + * + * @author Scott Frederick + */ +public class VersionInfo { + private final String version; + + @SuppressWarnings("unused") + VersionInfo() { + this.version = ""; + } + + /** + * Create a new {@literal VersionInfo} containing the specified version string. Intended for + * internal use. Clients will get {@literal VersionInfo} objects populated from + * CredHub responses. + * + * @param version a version string + */ + public VersionInfo(String version) { + this.version = version; + } + + /** + * Get the value of the version string returned from the CredHub server. + * + * @return the version string + */ + public String getVersion() { + return this.version; + } + + /** + * Determine if the CredHub server implements the v1 API. + * + * @return {@code true} if the server implements the CredHub v1 API; {@code false} otherwise + */ + public boolean isVersion1() { + return this.version.startsWith("1."); + } + + /** + * Determine if the CredHub server implements the v2 API. + * + * @return {@code true} if the server implements the CredHub v2 API; {@code false} otherwise + */ + public boolean isVersion2() { + return this.version.startsWith("2."); + } +} diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCertificateTemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplateUnitTests.java similarity index 90% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCertificateTemplateUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplateUnitTests.java index 3ec8071..6bc5def 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCertificateTemplateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/certificate/CredHubCertificateTemplateUnitTests.java @@ -14,13 +14,14 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.certificate; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.credhub.core.CredHubTemplate; import org.springframework.credhub.support.CertificateSummary; import org.springframework.credhub.support.CertificateSummaryData; import org.springframework.credhub.support.SimpleCredentialName; @@ -31,8 +32,8 @@ import java.util.List; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.when; -import static org.springframework.credhub.core.CredHubCertificateTemplate.BASE_URL_PATH; -import static org.springframework.credhub.core.CredHubCertificateTemplate.NAME_URL_QUERY; +import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.BASE_URL_PATH; +import static org.springframework.credhub.core.certificate.CredHubCertificateTemplate.NAME_URL_QUERY; import static org.springframework.http.HttpStatus.OK; @RunWith(MockitoJUnitRunner.class) 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/credential/CredHubCredentialTemplateSummaryUnitTests.java similarity index 89% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateSummaryUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubCredentialTemplateSummaryUnitTests.java index a16a7dc..9a4bfe2 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/credential/CredHubCredentialTemplateSummaryUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import java.util.List; @@ -24,6 +24,7 @@ import org.junit.experimental.theories.Theories; import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; +import org.springframework.credhub.core.CredHubException; import org.springframework.credhub.support.CredentialSummary; import org.springframework.credhub.support.CredentialSummaryData; import org.springframework.http.ResponseEntity; @@ -31,13 +32,13 @@ import org.springframework.http.ResponseEntity; 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; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.NAME_LIKE_URL_QUERY; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.PATH_URL_QUERY; import static org.springframework.http.HttpStatus.OK; import static org.springframework.http.HttpStatus.UNAUTHORIZED; @RunWith(Theories.class) -public class CredHubCredentialsTemplateSummaryUnitTests extends CredHubCredentialsTemplateUnitTestsBase { +public class CredHubCredentialTemplateSummaryUnitTests extends CredHubCredentialTemplateUnitTestsBase { @DataPoint("responses") public static ResponseEntity successfulResponse = new ResponseEntity<>(new CredentialSummaryData(new CredentialSummary(NAME)), OK); diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubCredentialTemplateUnitTests.java similarity index 82% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubCredentialTemplateUnitTests.java index 4c5155e..07c0b70 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubCredentialTemplateUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import org.junit.Test; @@ -29,12 +29,12 @@ import java.util.List; 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.CredHubCredentialsTemplate.NAME_URL_QUERY; -import static org.springframework.credhub.core.CredHubCredentialsTemplate.SHOW_ALL_URL_QUERY; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.NAME_URL_QUERY; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.SHOW_ALL_URL_QUERY; import static org.springframework.http.HttpStatus.OK; @RunWith(MockitoJUnitRunner.class) -public class CredHubCredentialsTemplateUnitTests extends CredHubCredentialsTemplateUnitTestsBase { +public class CredHubCredentialTemplateUnitTests extends CredHubCredentialTemplateUnitTestsBase { @Test public void getAllPaths() { when(restTemplate.getForEntity(SHOW_ALL_URL_QUERY, CredentialPathData.class)) diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubCredentialTemplateUnitTestsBase.java similarity index 85% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTestsBase.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubCredentialTemplateUnitTestsBase.java index bbdf6f7..a630f86 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubCredentialsTemplateUnitTestsBase.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubCredentialTemplateUnitTestsBase.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import org.junit.Before; import org.junit.Rule; @@ -23,10 +23,11 @@ import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoRule; import org.mockito.quality.Strictness; +import org.springframework.credhub.core.CredHubTemplate; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.web.client.RestTemplate; -public abstract class CredHubCredentialsTemplateUnitTestsBase { +public abstract class CredHubCredentialTemplateUnitTestsBase { protected static final SimpleCredentialName NAME = new SimpleCredentialName("example", "credential"); @Rule @@ -35,7 +36,7 @@ public abstract class CredHubCredentialsTemplateUnitTestsBase { @Mock protected RestTemplate restTemplate; - protected CredHubCredentialsOperations credHubTemplate; + protected CredHubCredentialOperations credHubTemplate; @Before public void setUpCredHubTemplateUnitTests() { diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailCertificateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailCertificateUnitTests.java similarity index 98% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailCertificateUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailCertificateUnitTests.java index 7c9b2f9..9c537ce 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailCertificateUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailCertificateUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import java.util.List; diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailJsonUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailJsonUnitTests.java similarity index 98% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailJsonUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailJsonUnitTests.java index 17efb0d..4d17c2d 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailJsonUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailJsonUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import java.util.List; diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailPasswordUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailPasswordUnitTests.java similarity index 98% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailPasswordUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailPasswordUnitTests.java index 09d44a4..a6ad2ee 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailPasswordUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailPasswordUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import java.util.List; diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailRsaUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailRsaUnitTests.java similarity index 98% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailRsaUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailRsaUnitTests.java index 5eac0f9..f32b78d 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailRsaUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailRsaUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import java.util.List; diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailSshUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailSshUnitTests.java similarity index 98% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailSshUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailSshUnitTests.java index 9cfb526..d7cd828 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailSshUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailSshUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import java.util.List; 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/credential/CredHubTemplateDetailUnitTestsBase.java similarity index 92% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailUnitTestsBase.java index 6ce537b..d7f90b0 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/credential/CredHubTemplateDetailUnitTestsBase.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import java.util.Arrays; import java.util.HashMap; @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; import org.springframework.core.ParameterizedTypeReference; +import org.springframework.credhub.core.CredHubException; import org.springframework.credhub.support.CredentialDetails; import org.springframework.credhub.support.CredentialDetailsData; import org.springframework.credhub.support.CredentialRequest; @@ -37,12 +38,12 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.isA; import static org.mockito.ArgumentMatchers.isNull; import static org.mockito.Mockito.when; -import static org.springframework.credhub.core.CredHubCredentialsTemplate.BASE_URL_PATH; -import static org.springframework.credhub.core.CredHubCredentialsTemplate.ID_URL_PATH; -import static org.springframework.credhub.core.CredHubCredentialsTemplate.NAME_URL_QUERY; -import static org.springframework.credhub.core.CredHubCredentialsTemplate.NAME_URL_QUERY_CURRENT; -import static org.springframework.credhub.core.CredHubCredentialsTemplate.NAME_URL_QUERY_VERSIONS; -import static org.springframework.credhub.core.CredHubCredentialsTemplate.REGENERATE_URL_PATH; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.BASE_URL_PATH; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.ID_URL_PATH; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.NAME_URL_QUERY; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.NAME_URL_QUERY_CURRENT; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.NAME_URL_QUERY_VERSIONS; +import static org.springframework.credhub.core.credential.CredHubCredentialTemplate.REGENERATE_URL_PATH; import static org.springframework.http.HttpMethod.GET; import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.HttpMethod.PUT; @@ -50,7 +51,7 @@ import static org.springframework.http.HttpStatus.OK; import static org.springframework.http.HttpStatus.UNAUTHORIZED; @SuppressWarnings("unchecked") -public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCredentialsTemplateUnitTestsBase { +public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubCredentialTemplateUnitTestsBase { private static final String CREDENTIAL_ID = "1111-1111-1111-1111"; protected abstract Class getType(); diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUserUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailUserUnitTests.java similarity index 98% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUserUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailUserUnitTests.java index 79f9270..5a27f9d 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUserUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailUserUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import java.util.List; diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailValueUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailValueUnitTests.java similarity index 98% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailValueUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailValueUnitTests.java index 1622694..8003306 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailValueUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/credential/CredHubTemplateDetailValueUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.credential; import java.util.List; diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/info/CredHubInfoTemplateUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/info/CredHubInfoTemplateUnitTests.java new file mode 100644 index 0000000..f5b1ad5 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/info/CredHubInfoTemplateUnitTests.java @@ -0,0 +1,63 @@ +/* + * 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.core.info; + +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.MockitoRule; +import org.mockito.quality.Strictness; +import org.springframework.credhub.core.CredHubTemplate; +import org.springframework.credhub.support.info.VersionInfo; +import org.springframework.http.ResponseEntity; +import org.springframework.web.client.RestTemplate; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; +import static org.springframework.credhub.core.info.CredHubInfoTemplate.VERSION_URL_PATH; +import static org.springframework.http.HttpStatus.OK; + +@RunWith(MockitoJUnitRunner.class) +public class CredHubInfoTemplateUnitTests { + @Rule + public MockitoRule mockitoRule = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); + + @Mock + private RestTemplate restTemplate; + + private CredHubInfoOperations credHubTemplate; + + @Before + public void setUp() { + credHubTemplate = new CredHubTemplate(restTemplate).info(); + } + + @Test + public void getVersion() { + when(restTemplate.getForEntity(VERSION_URL_PATH, VersionInfo.class)) + .thenReturn(new ResponseEntity<>(new VersionInfo("2.0.0"), OK)); + + VersionInfo response = credHubTemplate.version(); + + assertThat(response.getVersion()).isEqualTo("2.0.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/interpolation/CredHubInterpolationTemplateUnitTests.java similarity index 93% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubInterpolationTemplateUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/interpolation/CredHubInterpolationTemplateUnitTests.java index b29883e..17a6e39 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/interpolation/CredHubInterpolationTemplateUnitTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.interpolation; import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Before; @@ -26,6 +26,7 @@ import org.mockito.junit.MockitoJUnit; import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoRule; import org.mockito.quality.Strictness; +import org.springframework.credhub.core.CredHubTemplate; import org.springframework.credhub.support.ServiceInstanceCredentialName; import org.springframework.credhub.support.ServicesData; import org.springframework.credhub.support.utils.JsonUtils; @@ -37,7 +38,7 @@ import java.io.IOException; 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.credhub.core.interpolation.CredHubInterpolationTemplate.INTERPOLATE_URL_PATH; import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.HttpStatus.OK; 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/permission/CredHubPermissionsTemplateUnitTests.java similarity index 88% rename from spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubPermissionsTemplateUnitTests.java rename to spring-credhub-core/src/test/java/org/springframework/credhub/core/permission/CredHubPermissionsTemplateUnitTests.java index 0fc4e20..a8ec745 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/permission/CredHubPermissionsTemplateUnitTests.java @@ -14,13 +14,14 @@ * limitations under the License. */ -package org.springframework.credhub.core; +package org.springframework.credhub.core.permission; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import org.springframework.credhub.core.CredHubTemplate; import org.springframework.credhub.support.CredentialPermissions; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.permissions.Actor; @@ -36,9 +37,9 @@ import java.util.List; 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; -import static org.springframework.credhub.core.CredHubPermissionsTemplate.PERMISSIONS_URL_PATH; -import static org.springframework.credhub.core.CredHubPermissionsTemplate.PERMISSIONS_URL_QUERY; +import static org.springframework.credhub.core.permission.CredHubPermissionTemplate.PERMISSIONS_ACTOR_URL_QUERY; +import static org.springframework.credhub.core.permission.CredHubPermissionTemplate.PERMISSIONS_URL_PATH; +import static org.springframework.credhub.core.permission.CredHubPermissionTemplate.PERMISSIONS_URL_QUERY; import static org.springframework.http.HttpMethod.POST; import static org.springframework.http.HttpStatus.OK; @@ -49,7 +50,7 @@ public class CredHubPermissionsTemplateUnitTests { @Mock private RestTemplate restTemplate; - private CredHubPermissionsOperations credHubTemplate; + private CredHubPermissionOperations credHubTemplate; @Before public void setUp() { diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/info/VersionInfoTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/info/VersionInfoTests.java new file mode 100644 index 0000000..488e25c --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/info/VersionInfoTests.java @@ -0,0 +1,50 @@ +/* + * 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.info; + +import org.junit.Test; +import org.springframework.credhub.support.JsonParsingUnitTestsBase; + +import static org.assertj.core.api.Assertions.assertThat; + +public class VersionInfoTests extends JsonParsingUnitTestsBase { + @Test + public void deserializeWithV1() { + String json = "{\n" + + " \"version\": \"1.9.0\"\n" + + "}"; + + VersionInfo versionInfo = parseResponse(json, VersionInfo.class); + + assertThat(versionInfo.getVersion()).isEqualTo("1.9.0"); + assertThat(versionInfo.isVersion1()).isTrue(); + assertThat(versionInfo.isVersion2()).isFalse(); + } + + @Test + public void deserializeWithV2() { + String json = "{\n" + + " \"version\": \"2.0.2\"\n" + + "}"; + + VersionInfo versionInfo = parseResponse(json, VersionInfo.class); + + assertThat(versionInfo.getVersion()).isEqualTo("2.0.2"); + assertThat(versionInfo.isVersion1()).isFalse(); + assertThat(versionInfo.isVersion2()).isTrue(); + } +} \ No newline at end of file diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredHubIntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredHubIntegrationTests.java new file mode 100644 index 0000000..8b81d8c --- /dev/null +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredHubIntegrationTests.java @@ -0,0 +1,24 @@ +package org.springframework.credhub.integration; + +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.credhub.autoconfig.CredHubAutoConfiguration; +import org.springframework.credhub.autoconfig.CredHubOAuth2TemplateAutoConfiguration; +import org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration; +import org.springframework.credhub.core.CredHubOperations; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = {TestApplication.class, + CredHubAutoConfiguration.class, + CredHubTemplateAutoConfiguration.class, + CredHubOAuth2TemplateAutoConfiguration.class}) +@ActiveProfiles("test") +public abstract class CredHubIntegrationTests { + + @Autowired + protected CredHubOperations operations; + +} diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredentialIntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredentialIntegrationTests.java index 05a9eae..48f516a 100644 --- a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredentialIntegrationTests.java +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/CredentialIntegrationTests.java @@ -1,43 +1,41 @@ +/* + * 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.integration; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.credhub.autoconfig.CredHubAutoConfiguration; -import org.springframework.credhub.autoconfig.CredHubOAuth2TemplateAutoConfiguration; -import org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration; -import org.springframework.credhub.core.CredHubCredentialsOperations; -import org.springframework.credhub.core.CredHubOperations; +import org.springframework.credhub.core.credential.CredHubCredentialOperations; import org.springframework.credhub.support.CredentialDetails; import org.springframework.credhub.support.CredentialSummary; import org.springframework.credhub.support.CredentialType; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.value.ValueCredential; import org.springframework.credhub.support.value.ValueCredentialRequest; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(SpringRunner.class) -@SpringBootTest(classes = {TestApplication.class, - CredHubAutoConfiguration.class, - CredHubTemplateAutoConfiguration.class, - CredHubOAuth2TemplateAutoConfiguration.class}) -@ActiveProfiles("test") -public class CredentialIntegrationTests { +public class CredentialIntegrationTests extends CredHubIntegrationTests { private static final SimpleCredentialName CREDENTIAL_NAME = new SimpleCredentialName("spring-credhub", "integration-test", "test-value-credential"); private static final String CREDENTIAL_VALUE = "test-value"; - @Autowired - private CredHubOperations operations; - - private CredHubCredentialsOperations credentials; + private CredHubCredentialOperations credentials; @Before public void setUp() { diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/InfoIntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/InfoIntegrationTests.java new file mode 100644 index 0000000..11ec26f --- /dev/null +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/InfoIntegrationTests.java @@ -0,0 +1,40 @@ +/* + * 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.integration; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.credhub.core.info.CredHubInfoOperations; +import org.springframework.credhub.support.info.VersionInfo; + +import static org.assertj.core.api.Assertions.assertThat; + +public class InfoIntegrationTests extends CredHubIntegrationTests { + private CredHubInfoOperations info; + + @Before + public void setUp() { + info = operations.info(); + } + + @Test + public void getInfo() { + VersionInfo version = info.version(); + + assertThat(version.getVersion()).isNotNull(); + } +} diff --git a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionsIntegrationTests.java b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java similarity index 66% rename from spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionsIntegrationTests.java rename to spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java index a9aba06..7c3ede7 100644 --- a/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionsIntegrationTests.java +++ b/spring-credhub-integration-tests/src/test/java/org/springframework/credhub/integration/PermissionIntegrationTests.java @@ -1,44 +1,42 @@ +/* + * 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.integration; import org.junit.Before; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.credhub.autoconfig.CredHubAutoConfiguration; -import org.springframework.credhub.autoconfig.CredHubOAuth2TemplateAutoConfiguration; -import org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration; -import org.springframework.credhub.core.CredHubCredentialsOperations; -import org.springframework.credhub.core.CredHubOperations; -import org.springframework.credhub.core.CredHubPermissionsOperations; +import org.springframework.credhub.core.credential.CredHubCredentialOperations; +import org.springframework.credhub.core.permission.CredHubPermissionOperations; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.permissions.Actor; import org.springframework.credhub.support.permissions.CredentialPermission; import org.springframework.credhub.support.permissions.Operation; import org.springframework.credhub.support.value.ValueCredentialRequest; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringRunner; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; -@RunWith(SpringRunner.class) -@SpringBootTest(classes = {TestApplication.class, - CredHubAutoConfiguration.class, - CredHubTemplateAutoConfiguration.class, - CredHubOAuth2TemplateAutoConfiguration.class}) -@ActiveProfiles("test") -public class PermissionsIntegrationTests { +public class PermissionIntegrationTests extends CredHubIntegrationTests { private static final SimpleCredentialName CREDENTIAL_NAME = new SimpleCredentialName("spring-credhub", "integration-test", "test-permissions-credential"); private static final String CREDENTIAL_VALUE = "test-value"; - @Autowired - private CredHubOperations operations; - - private CredHubCredentialsOperations credentials; - private CredHubPermissionsOperations permissions; + private CredHubCredentialOperations credentials; + private CredHubPermissionOperations permissions; @Before public void setUp() {