diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetails.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetails.java index 35db185..fa2ddb9 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetails.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetails.java @@ -17,15 +17,12 @@ package org.springframework.credhub.support; import java.util.Date; -import java.util.Map; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; -import org.springframework.util.Assert; - /** * The details of a credential that has been written to CredHub. Clients don't * typically instantiate objects of this type, but will receive them in response @@ -60,8 +57,8 @@ public class CredentialDetails extends CredentialSummary { * @param versionCreatedAt the {@link Date} when this version of the credential was * created */ - CredentialDetails(String id, CredentialName name, ValueType valueType, - Object value, Date versionCreatedAt) { + public CredentialDetails(String id, CredentialName name, ValueType valueType, + Object value, Date versionCreatedAt) { super(name, versionCreatedAt); this.id = id; this.valueType = valueType; @@ -95,17 +92,6 @@ public class CredentialDetails extends CredentialSummary { return this.value; } - /** - * Create a builder for a {@link CredentialDetails} object. Intended for internal - * use in tests. Clients will get {@link CredentialDetails} objects populated from - * CredHub responses. - * - * @return the builder - */ - public static CredentialDetailsBuilder detailsBuilder() { - return new CredentialDetailsBuilder(); - } - @Override public boolean equals(Object o) { if (this == o) @@ -145,90 +131,4 @@ public class CredentialDetails extends CredentialSummary { + ", versionCreatedAt='" + versionCreatedAt + '\'' + '}'; } - - /** - * A builder that provides a fluent API for constructing {@link CredentialDetails} - * instances. Intended to be used internally for testing. - */ - public static class CredentialDetailsBuilder { - private String id; - private CredentialName name; - private ValueType valueType; - private Object value; - private Date versionCreatedAt; - - CredentialDetailsBuilder() { - } - - /** - * Set the ID of the credential. - * - * @param id the ID; must not be {@literal null} - * @return the builder - */ - public CredentialDetailsBuilder id(String id) { - Assert.notNull(id, "id must not be null"); - this.id = id; - return this; - } - - /** - * Set the name of the credential. - * - * @param name the name; must not be {@literal null} - * @return the builder - */ - public CredentialDetailsBuilder name(CredentialName name) { - Assert.notNull(name, "name must not be null"); - this.name = name; - return this; - } - - /** - * Set a password value and {@link ValueType#PASSWORD} type for the credential. - * - * @param value the password value; must not be {@literal null} - * @return the builder - */ - public CredentialDetailsBuilder passwordValue(String value) { - Assert.notNull(value, "value must not be null"); - this.valueType = ValueType.PASSWORD; - this.value = value; - return this; - } - - /** - * Set a JSON value and {@link ValueType#JSON} type for the credential. - * - * @param value the JSON value; must not be {@literal null} - * @return the builder - */ - public CredentialDetailsBuilder jsonValue(Map value) { - Assert.notNull(value, "value must not be null"); - this.valueType = ValueType.JSON; - this.value = value; - return this; - } - - /** - * Set a creation date for the credential. - * - * @param versionCreatedAt the creation date; must not be {@literal null} - * @return the builder - */ - public CredentialDetailsBuilder versionCreatedAt(Date versionCreatedAt) { - Assert.notNull(versionCreatedAt, "versionCreatedAt must not be null"); - this.versionCreatedAt = versionCreatedAt; - return this; - } - - /** - * Construct a {@link CredentialDetails} from the provided values. - * - * @return a {@link CredentialDetails} - */ - public CredentialDetails build() { - return new CredentialDetails(id, name, valueType, value, versionCreatedAt); - } - } } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetailsData.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetailsData.java index c4630af..7855abd 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetailsData.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialDetailsData.java @@ -18,13 +18,11 @@ package org.springframework.credhub.support; -import java.util.ArrayList; -import java.util.Collection; +import java.util.Arrays; import java.util.List; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonInclude; -import org.springframework.util.Assert; import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY; @@ -53,8 +51,8 @@ public class CredentialDetailsData { * * @param data a collection of {@link CredentialDetails} */ - CredentialDetailsData(List data) { - this.data = data; + public CredentialDetailsData(CredentialDetails... data) { + this.data = Arrays.asList(data); } /** @@ -66,17 +64,6 @@ public class CredentialDetailsData { return this.data; } - /** - * Create a builder for a {@link CredentialDetailsData} object. Intended for internal - * use. Clients will get {@link CredentialDetailsData} objects populated from - * CredHub responses. - * - * @return the builder - */ - public static CredentialDetailsDataBuilder builder() { - return new CredentialDetailsDataBuilder(); - } - @Override public boolean equals(Object o) { if (this == o) @@ -104,74 +91,4 @@ public class CredentialDetailsData { + "data=" + data + '}'; } - - /** - * A builder that provides a fluent API for constructing {@link CredentialDetailsData} - * instances. Intended to be used internally for testing. - */ - public static class CredentialDetailsDataBuilder { - private List data; - - /** - * Create a {@link CredentialDetailsDataBuilder}. - */ - CredentialDetailsDataBuilder() { - } - - /** - * Add a {@link CredentialDetails} to the collection of details. - * - * @param datum a {@link CredentialDetails} to add; must not be - * {@literal null} - * @return the builder - */ - public CredentialDetailsDataBuilder datum(CredentialDetails datum) { - Assert.notNull(datum, "datum must not be null"); - initData(); - this.data.add(datum); - return this; - } - - /** - * Add a collection of {@link CredentialDetails} to the collection of details. - * - * @param data a collection of {@link CredentialDetails} to add; - * must not be {@literal null} - * @return the builder - */ - public CredentialDetailsDataBuilder data(Collection data) { - Assert.notNull(data, "data must not be null"); - initData(); - this.data.addAll(data); - return this; - } - - private void initData() { - if (this.data == null) { - this.data = new ArrayList(); - } - } - - /** - * Construct a {@link CredentialDetailsData} from the provided values. - * - * @return a {@link CredentialDetailsData} - */ - public CredentialDetailsData build() { - List data; - switch (this.data == null ? 0 : this.data.size()) { - case 0: - data = java.util.Collections.emptyList(); - break; - case 1: - data = java.util.Collections.singletonList(this.data.get(0)); - break; - default: - data = java.util.Collections - .unmodifiableList(new ArrayList(this.data)); - } - - return new CredentialDetailsData(data); - } - } } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummary.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummary.java index f8ef5f3..c43d5b4 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummary.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummary.java @@ -51,7 +51,7 @@ public class CredentialSummary { * @param versionCreatedAt the {@link Date} when this version of the credential was * created */ - CredentialSummary(CredentialName name, Date versionCreatedAt) { + public CredentialSummary(CredentialName name, Date versionCreatedAt) { this.name = name; this.versionCreatedAt = versionCreatedAt; } @@ -74,17 +74,6 @@ public class CredentialSummary { return this.versionCreatedAt; } - /** - * Create a builder for a {@link CredentialSummary} object. Intended for internal - * use in tests. Clients will get {@link CredentialSummary} objects populated from - * CredHub responses. - * - * @return the builder - */ - public static CredentialSummaryBuilder summaryBuilder() { - return new CredentialSummaryBuilder(); - } - @Override public boolean equals(Object o) { if (this == o) @@ -115,49 +104,4 @@ public class CredentialSummary { + ", versionCreatedAt='" + versionCreatedAt + '\'' + '}'; } - - /** - * A builder that provides a fluent API for constructing {@link CredentialSummary} - * instances. Intended to be used internally for testing. - */ - public static class CredentialSummaryBuilder { - protected CredentialName name; - protected Date versionCreatedAt; - - CredentialSummaryBuilder() { - } - - /** - * Set the name of the credential. - * - * @param name the name; must not be {@literal null} - * @return the builder - */ - public CredentialSummaryBuilder name(CredentialName name) { - Assert.notNull(name, "name must not be null"); - this.name = name; - return this; - } - - /** - * Set a creation date for the credential. - * - * @param versionCreatedAt the creation date; must not be {@literal null} - * @return the builder - */ - public CredentialSummaryBuilder versionCreatedAt(Date versionCreatedAt) { - Assert.notNull(versionCreatedAt, "versionCreatedAt must not be null"); - this.versionCreatedAt = versionCreatedAt; - return this; - } - - /** - * Construct a {@link CredentialSummary} from the provided values. - * - * @return a {@link CredentialSummary} - */ - public CredentialSummary build() { - return new CredentialSummary(name, versionCreatedAt); - } - } } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java index 43ce305..4b3631d 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/CredentialSummaryData.java @@ -16,14 +16,12 @@ package org.springframework.credhub.support; -import java.util.ArrayList; -import java.util.Collection; +import java.util.Arrays; import java.util.List; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.databind.PropertyNamingStrategy; import com.fasterxml.jackson.databind.annotation.JsonNaming; -import org.springframework.util.Assert; /** * A collection of {@link CredentialSummary}s. Clients don't typically instantiate @@ -50,8 +48,8 @@ public class CredentialSummaryData { * * @param credentials a collection of {@link CredentialSummary}s */ - CredentialSummaryData(List credentials) { - this.credentials = credentials; + public CredentialSummaryData(CredentialSummary... credentials) { + this.credentials = Arrays.asList(credentials); } /** @@ -63,17 +61,6 @@ public class CredentialSummaryData { return this.credentials; } - /** - * Create a builder for a {@link CredentialSummaryData} object. Intended for internal - * use. Clients will get {@link CredentialSummaryData} objects populated from - * CredHub responses. - * - * @return the builder - */ - public static CredentialSummaryDataBuilder builder() { - return new CredentialSummaryDataBuilder(); - } - @Override public boolean equals(Object o) { if (this == o) @@ -102,78 +89,4 @@ public class CredentialSummaryData { + "credentials=" + credentials + '}'; } - - /** - * Create a builder for a {@link CredentialSummaryData} object. Intended for internal - * use. Clients will get {@link CredentialSummaryData} objects populated from - * CredHub responses. - * - * @return the builder - */ - public static class CredentialSummaryDataBuilder { - private List credentialSummaries; - - CredentialSummaryDataBuilder() { - } - - /** - * Add a {@link CredentialSummary} to the collection of summaries. - * - * @param credential the {@link CredentialSummary} to add; must not be - * {@literal null} - * @return the builder - */ - public CredentialSummaryDataBuilder credential(CredentialSummary credential) { - Assert.notNull(credential, "credential must not be null"); - initCredentials(); - this.credentialSummaries.add(credential); - return this; - } - - /** - * Add a collection of {@link CredentialSummary}s to the collection of summaries. - * - * @param credentials the {@link CredentialSummary}s to add; must not be - * {@literal null} - * @return the builder - */ - public CredentialSummaryDataBuilder credentials( - Collection credentials) { - Assert.notNull(credentials, "credentials must not be null"); - initCredentials(); - this.credentialSummaries.addAll(credentials); - return this; - } - - private void initCredentials() { - if (this.credentialSummaries == null) { - this.credentialSummaries = new ArrayList(); - } - } - - /** - * Construct a {@link CredentialSummaryData} from the provided values. - * - * @return a {@link CredentialSummaryData} - */ - public CredentialSummaryData build() { - List credentialSummaries; - switch (this.credentialSummaries == null ? 0 - : this.credentialSummaries.size()) { - case 0: - credentialSummaries = java.util.Collections.emptyList(); - break; - case 1: - credentialSummaries = java.util.Collections - .singletonList(this.credentialSummaries.get(0)); - break; - default: - credentialSummaries = java.util.Collections.unmodifiableList( - new ArrayList(this.credentialSummaries)); - } - - return new CredentialSummaryData(credentialSummaries); - } - } - } diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/JsonWriteRequest.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/JsonWriteRequest.java new file mode 100644 index 0000000..b8ec219 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/JsonWriteRequest.java @@ -0,0 +1,93 @@ +/* + * 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 java.util.List; +import java.util.Map; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; + +import org.springframework.util.Assert; + +import static org.springframework.credhub.support.ValueType.JSON; + +/** + * The details of a request to write a new or update an existing JSON credential in CredHub. + * + * @author Scott Frederick + */ +@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) +public class JsonWriteRequest extends WriteRequest> { + /** + * Create a {@link JsonWriteRequest} from the provided parameters. Intended for internal + * use. Clients should use {@link #builder()} to construct instances of this class. + * + * @param name the name of the credential + * @param overwrite {@literal false} to create a new credential, or + * {@literal true} to update and existing credential + * @param value the value of the credential + * @param additionalPermissions access control permissions for the credential + */ + private JsonWriteRequest(CredentialName name, boolean overwrite, Map value, + List additionalPermissions) { + super(name, overwrite, value, ValueType.PASSWORD, additionalPermissions); + } + + /** + * Create a builder that provides a fluent API for providing the values required + * to construct a {@link JsonWriteRequest}. + * + * @return a builder + */ + public static JsonWriteRequestBuilder builder() { + return new JsonWriteRequestBuilder(); + } + + /** + * A builder that provides a fluent API for constructing {@link JsonWriteRequest}s. + */ + public static class JsonWriteRequestBuilder extends WriteRequestBuilder, JsonWriteRequestBuilder> { + /** + * Create a {@link JsonWriteRequestBuilder}. Intended for internal use. + */ + JsonWriteRequestBuilder() { + } + + @Override + protected JsonWriteRequestBuilder getBuilder() { + return this; + } + + /** + * Set the value of a JSON credential. A JSON credential consists of + * one or more fields in a JSON document. The provided {@literal Map} parameter. + * will be converted to a JSON document before sending to CredHub. The type of + * the credential is set to {@link ValueType#JSON}. + * + * @param value the json credential value; must not be {@literal null} + * @return the builder + */ + public JsonWriteRequestBuilder value(Map value) { + Assert.notNull(value, "value must not be null"); + this.valueType = JSON; + this.value = value; + return this; + } + } + +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordWriteRequest.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordWriteRequest.java new file mode 100644 index 0000000..3ade795 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordWriteRequest.java @@ -0,0 +1,88 @@ +/* + * 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 java.util.List; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; + +import org.springframework.util.Assert; + +/** + * The details of a request to write a new or update an existing password credential in CredHub. + * + * @author Scott Frederick + */ +@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) +public class PasswordWriteRequest extends WriteRequest { + /** + * Create a {@link PasswordWriteRequest} from the provided parameters. Intended for internal + * use. Clients should use {@link #builder()} to construct instances of this class. + * + * @param name the name of the credential + * @param overwrite {@literal false} to create a new credential, or + * {@literal true} to update and existing credential + * @param value the value of the credential + * @param additionalPermissions access control permissions for the credential + */ + private PasswordWriteRequest(CredentialName name, boolean overwrite, String value, + List additionalPermissions) { + super(name, overwrite, value, ValueType.PASSWORD, additionalPermissions); + } + + /** + * Create a builder that provides a fluent API for providing the values required + * to construct a {@link PasswordWriteRequest}. + * + * @return a builder + */ + public static PasswordWriteRequestBuilder builder() { + return new PasswordWriteRequestBuilder(); + } + + /** + * A builder that provides a fluent API for constructing {@link PasswordWriteRequest}s. + */ + public static class PasswordWriteRequestBuilder extends WriteRequestBuilder { + /** + * Create a {@link PasswordWriteRequestBuilder}. Intended for internal use. + */ + PasswordWriteRequestBuilder() { + } + + @Override + protected PasswordWriteRequestBuilder getBuilder() { + return this; + } + + /** + * Set the value of a password credential. A password credential consists of + * a single string value. The type of the credential is set to {@link ValueType#PASSWORD}. + * + * @param value the password credential value; must not be {@literal null} + * @return the builder + */ + public PasswordWriteRequestBuilder value(String value) { + Assert.notNull(value, "value must not be null"); + this.valueType = ValueType.PASSWORD; + this.value = value; + return this; + } + } + +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/WriteRequest.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/WriteRequest.java index 4258fa2..4b4fba9 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/WriteRequest.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/WriteRequest.java @@ -19,9 +19,9 @@ package org.springframework.credhub.support; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.List; -import java.util.Map; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.databind.PropertyNamingStrategy; @@ -30,7 +30,6 @@ import com.fasterxml.jackson.databind.annotation.JsonNaming; import org.springframework.util.Assert; import static com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY; -import static org.springframework.credhub.support.ValueType.JSON; /** * The details of a request to write a new or update an existing credential in CredHub. @@ -38,17 +37,17 @@ import static org.springframework.credhub.support.ValueType.JSON; * @author Scott Frederick */ @JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) -public class WriteRequest { +public class WriteRequest { private boolean overwrite; private CredentialName name; private ValueType valueType; - private Object value; + private T value; @JsonInclude(NON_EMPTY) private List additionalPermissions; /** * Create a {@link WriteRequest} from the provided parameters. Intended for internal - * use. Clients should use {@link #builder()} to construct instances of this class. + * use. * * @param name the name of the credential * @param overwrite {@literal false} to create a new credential, or @@ -57,9 +56,9 @@ public class WriteRequest { * @param valueType the {@link ValueType} of the credential * @param additionalPermissions access control permissions for the credential */ - private WriteRequest(CredentialName name, boolean overwrite, - Object value, ValueType valueType, - List additionalPermissions) { + WriteRequest(CredentialName name, boolean overwrite, + T value, ValueType valueType, + List additionalPermissions) { this.name = name; this.overwrite = overwrite; this.valueType = valueType; @@ -93,7 +92,7 @@ public class WriteRequest { * * @return the value of the credential */ - public Object getValue() { + public T getValue() { return this.value; } @@ -115,16 +114,6 @@ public class WriteRequest { return this.additionalPermissions; } - /** - * Create a builder that provides a fluent API for providing the values required - * to construct a {@link WriteRequest}. - * - * @return a builder - */ - public static WriteRequestBuilder builder() { - return new WriteRequestBuilder(); - } - @Override public boolean equals(Object o) { if (this == o) @@ -169,48 +158,38 @@ public class WriteRequest { /** * A builder that provides a fluent API for constructing {@link WriteRequest}s. */ - public static class WriteRequestBuilder { + @SuppressWarnings("unchecked") + public static abstract class WriteRequestBuilder> { + private final B thisObj; + private CredentialName name; private boolean overwrite; - private Object value; - private ValueType valueType; + protected T value; + protected ValueType valueType; private ArrayList additionalPermissions; /** * Create a {@link WriteRequestBuilder}. Intended for internal use. */ WriteRequestBuilder() { + this.thisObj = getBuilder(); } /** - * Set the value of a password credential. A password credential consists of - * a single string value. The type of the credential is set to {@link ValueType#PASSWORD}. + * Provide the concrete builder. * - * @param value the password credential value; must not be {@literal null} * @return the builder */ - public WriteRequestBuilder passwordValue(String value) { - Assert.notNull(value, "value must not be null"); - this.valueType = ValueType.PASSWORD; - this.value = value; - return this; - } + protected abstract B getBuilder(); /** - * Set the value of a JSON credential. A JSON credential consists of - * one or more fields in a JSON document. The provided {@literal Map} parameter. - * will be converted to a JSON document before sending to CredHub. The type of - * the credential is set to {@link ValueType#JSON}. + * Set the value of a credential. In concrete builders, this should set the value + * and the value type. * - * @param value the json credential value; must not be {@literal null} + * @param value the credential value; must not be {@literal null} * @return the builder */ - public WriteRequestBuilder jsonValue(Map value) { - Assert.notNull(value, "value must not be null"); - this.valueType = JSON; - this.value = value; - return this; - } + public abstract B value(T value); /** * Set the {@link CredentialName} for the credential. @@ -218,10 +197,10 @@ public class WriteRequest { * @param name the credential name; must not be {@literal null} * @return the builder */ - public WriteRequestBuilder name(CredentialName name) { + public B name(CredentialName name) { Assert.notNull(name, "name must not be null"); this.name = name; - return this; + return thisObj; } /** @@ -232,9 +211,9 @@ public class WriteRequest { * {@literal true} to update and existing credential * @return the builder */ - public WriteRequestBuilder overwrite(boolean overwrite) { + public B overwrite(boolean overwrite) { this.overwrite = overwrite; - return this; + return thisObj; } /** @@ -245,10 +224,10 @@ public class WriteRequest { * credential * @return the builder */ - public WriteRequestBuilder additionalPermission(AdditionalPermission additionalPermission) { + public B additionalPermission(AdditionalPermission additionalPermission) { initPermissions(); this.additionalPermissions.add(additionalPermission); - return this; + return thisObj; } /** @@ -259,10 +238,24 @@ public class WriteRequest { * assign to the credential * @return the builder */ - public WriteRequestBuilder additionalPermissions(Collection permissions) { + public B additionalPermissions(Collection permissions) { initPermissions(); this.additionalPermissions.addAll(permissions); - return this; + return thisObj; + } + + /** + * Add a collection of {@link AdditionalPermission}s to the controls that will be + * assigned to the credential. + * + * @param permissions an collection of {@link AdditionalPermission}s to + * assign to the credential + * @return the builder + */ + public B additionalPermissions(AdditionalPermission... permissions) { + initPermissions(); + this.additionalPermissions.addAll(Arrays.asList(permissions)); + return thisObj; } private void initPermissions() { diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailResponseUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailResponseUnitTests.java index 94c149b..f4b4279 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailResponseUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailResponseUnitTests.java @@ -23,6 +23,8 @@ import org.junit.experimental.theories.Theory; import org.junit.runner.RunWith; import org.springframework.credhub.support.CredentialDetails; +import org.springframework.credhub.support.PasswordWriteRequest; +import org.springframework.credhub.support.ValueType; import org.springframework.credhub.support.WriteRequest; import org.springframework.http.HttpEntity; import org.springframework.http.HttpStatus; @@ -49,24 +51,19 @@ public class CredHubTemplateDetailResponseUnitTests extends CredHubTemplateUnitT @DataPoint("responses") public static ResponseEntity successfulResponse = - new ResponseEntity(CredentialDetails.detailsBuilder() - .name(NAME) - .id(CREDENTIAL_ID) - .passwordValue(CREDENTIAL_VALUE) - .versionCreatedAt(new Date()) - .build(), + new ResponseEntity( + new CredentialDetails(CREDENTIAL_ID, NAME, ValueType.PASSWORD, CREDENTIAL_VALUE, new Date()), OK); @DataPoint("responses") public static ResponseEntity httpErrorResponse = - new ResponseEntity(CredentialDetails.detailsBuilder().build(), - UNAUTHORIZED); + new ResponseEntity(new CredentialDetails(), UNAUTHORIZED); @Theory public void write(@FromDataPoints("responses") ResponseEntity expectedResponse) { - WriteRequest request = WriteRequest.builder() + WriteRequest request = PasswordWriteRequest.builder() .name(NAME) - .passwordValue("secret") + .value("secret") .build(); when(restTemplate.exchange(BASE_URL_PATH, PUT, diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailsResponseUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailsResponseUnitTests.java index f9e7567..6dce33e 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailsResponseUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailsResponseUnitTests.java @@ -16,6 +16,7 @@ package org.springframework.credhub.core; +import java.util.Date; import java.util.List; import org.junit.experimental.theories.DataPoint; @@ -26,6 +27,7 @@ import org.junit.runner.RunWith; import org.springframework.credhub.support.CredentialDetails; import org.springframework.credhub.support.CredentialDetailsData; +import org.springframework.credhub.support.ValueType; import org.springframework.http.ResponseEntity; import static org.hamcrest.Matchers.containsString; @@ -46,20 +48,14 @@ public class CredHubTemplateDetailsResponseUnitTests extends CredHubTemplateUnit @DataPoint("responses") public static ResponseEntity successfulResponse = new ResponseEntity( - CredentialDetailsData.builder() - .datum(CredentialDetails.detailsBuilder() - .name(NAME) - .id(CREDENTIAL_ID) - .passwordValue(CREDENTIAL_VALUE) - .build()) - .build(), + new CredentialDetailsData( + new CredentialDetails(CREDENTIAL_ID, NAME, ValueType.PASSWORD, CREDENTIAL_VALUE, new Date())), OK); @DataPoint("responses") public static ResponseEntity httpErrorResponse = new ResponseEntity( - CredentialDetailsData.builder() - .build(), + new CredentialDetailsData(), UNAUTHORIZED); @Theory diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateSummaryResponseUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateSummaryResponseUnitTests.java index 96f81a5..ed86ed9 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateSummaryResponseUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateSummaryResponseUnitTests.java @@ -45,19 +45,13 @@ public class CredHubTemplateSummaryResponseUnitTests extends CredHubTemplateUnit @DataPoint("responses") public static ResponseEntity successfulResponse = new ResponseEntity( - CredentialSummaryData.builder() - .credential(CredentialSummary.summaryBuilder() - .name(NAME) - .versionCreatedAt(new Date()) - .build()) - .build(), + new CredentialSummaryData(new CredentialSummary(NAME, new Date())), OK); @DataPoint("responses") public static ResponseEntity httpErrorResponse = new ResponseEntity( - CredentialSummaryData.builder() - .build(), + new CredentialSummaryData(), UNAUTHORIZED); @Theory diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonWriteRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonWriteRequestUnitTests.java new file mode 100644 index 0000000..798d766 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/JsonWriteRequestUnitTests.java @@ -0,0 +1,57 @@ +/* + * 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 java.util.HashMap; + +import org.junit.Before; +import org.junit.Test; + +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; + +public class JsonWriteRequestUnitTests extends WriteRequestUnitTestsBase { + + @Before + public void setUp() { + requestBuilder = JsonWriteRequest.builder() + .name(new SimpleCredentialName("example", "credential")) + .value(new HashMap() { + { + put("data", "value"); + put("test", true); + } + }); + } + + @Test + public void serializationWithJsonValue() throws Exception { + String jsonValue = serializeToJson(requestBuilder); + + assertThat(jsonValue, + allOf(hasJsonPath("$.overwrite", equalTo(false)), + hasJsonPath("$.name", equalTo("/c/example/credential")), + hasJsonPath("$.type", equalTo("json")), + hasJsonPath("$.value.data", equalTo("value")), + hasJsonPath("$.value.test", equalTo(true)))); + + assertThat(jsonValue, hasNoJsonPath("$.additional_permissions")); + } +} \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/PasswordWriteRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/PasswordWriteRequestUnitTests.java new file mode 100644 index 0000000..d74e9e7 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/PasswordWriteRequestUnitTests.java @@ -0,0 +1,52 @@ +/* + * + * * Copyright 2013-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 org.junit.Before; +import org.junit.Test; + +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; + +public class PasswordWriteRequestUnitTests extends WriteRequestUnitTestsBase { + + @Before + public void setUp() { + requestBuilder = PasswordWriteRequest.builder() + .name(new SimpleCredentialName("example", "credential")) + .overwrite(true) + .value("secret"); + } + + @Test + public void serializationWithPasswordValue() throws Exception { + String jsonValue = serializeToJson(requestBuilder); + + assertThat(jsonValue, + allOf(hasJsonPath("$.overwrite", equalTo(true)), + hasJsonPath("$.name", equalTo("/c/example/credential")), + hasJsonPath("$.type", equalTo("password")), + hasJsonPath("$.value", equalTo("secret")))); + + assertThat(jsonValue, hasNoJsonPath("$.additional_permissions")); + } +} \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTests.java deleted file mode 100644 index 72c35ad..0000000 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTests.java +++ /dev/null @@ -1,138 +0,0 @@ -/* - * - * * Copyright 2013-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 java.util.HashMap; - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Before; -import org.junit.Test; - -import static org.hamcrest.CoreMatchers.allOf; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.assertTrue; -import static org.springframework.credhub.support.AdditionalPermission.Operation.READ; -import static org.springframework.credhub.support.AdditionalPermission.Operation.WRITE; -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 class WriteRequestUnitTests { - - private ObjectMapper mapper; - - private WriteRequest.WriteRequestBuilder requestBuilder; - - @Before - public void setUp() { - mapper = new ObjectMapper(); - - requestBuilder = WriteRequest.builder().name( - new SimpleCredentialName("example", "credential")); - } - - @Test - public void typeIsSerializable() { - assertTrue(mapper.canSerialize(WriteRequest.class)); - } - - @Test - public void serializationWithJsonValue() throws Exception { - requestBuilder.jsonValue(new HashMap() { - { - put("data", "value"); - put("test", true); - } - }); - - String jsonValue = serializeToJson(requestBuilder); - - assertThat(jsonValue, - allOf(hasJsonPath("$.overwrite", equalTo(false)), - hasJsonPath("$.name", equalTo("/c/example/credential")), - hasJsonPath("$.type", equalTo("json")), - hasJsonPath("$.value.data", equalTo("value")), - hasJsonPath("$.value.test", equalTo(true)))); - - assertThat(jsonValue, hasNoJsonPath("$.additional_permissions")); - } - - @Test - public void serializationWithPasswordValue() throws Exception { - requestBuilder.overwrite(true).passwordValue("secret"); - - String jsonValue = serializeToJson(requestBuilder); - - assertThat(jsonValue, - allOf(hasJsonPath("$.overwrite", equalTo(true)), - hasJsonPath("$.name", equalTo("/c/example/credential")), - hasJsonPath("$.type", equalTo("password")), - hasJsonPath("$.value", equalTo("secret")))); - - assertThat(jsonValue, hasNoJsonPath("$.additional_permissions")); - } - - @Test - public void serializationWithOnePermission() throws Exception { - requestBuilder.passwordValue("secret").additionalPermission( - AdditionalPermission.builder().app("app-id").operation(READ).build()); - - String jsonValue = serializeToJson(requestBuilder); - - assertThat(jsonValue, - allOf(hasJsonPath("$.additional_permissions[0].actor", - equalTo("mtls-app:app-id")), - hasJsonPath("$.additional_permissions[0].operations[0]", - equalTo("read")))); - } - - @Test - public void serializationWithTwoPermissions() throws Exception { - requestBuilder.passwordValue("secret") - .additionalPermission(AdditionalPermission.builder().app("app1-id") - .operation(READ).operation(WRITE).build()) - .additionalPermission(AdditionalPermission.builder().app("app2-id") - .operation(WRITE).operation(READ).build()); - - String jsonValue = serializeToJson(requestBuilder); - - assertThat(jsonValue, allOf( - hasJsonPath("$.additional_permissions[0].actor", - equalTo("mtls-app:app1-id")), - hasJsonPath("$.additional_permissions[0].operations[0]", equalTo("read")), - hasJsonPath("$.additional_permissions[0].operations[1]", - equalTo("write")), - hasJsonPath("$.additional_permissions[1].actor", - equalTo("mtls-app:app2-id")), - hasJsonPath("$.additional_permissions[1].operations[0]", - equalTo("write")), - hasJsonPath("$.additional_permissions[1].operations[1]", - equalTo("read")))); - } - - private String serializeToJson(WriteRequest.WriteRequestBuilder requestBuilder) - throws JsonProcessingException { - String jsonValue = mapper.writeValueAsString(requestBuilder.build()); - assertThat(jsonValue, isJson()); - return jsonValue; - } - -} \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTestsBase.java new file mode 100644 index 0000000..08e3631 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/WriteRequestUnitTestsBase.java @@ -0,0 +1,99 @@ +/* + * 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 org.junit.Before; +import org.junit.Test; +import org.springframework.credhub.support.WriteRequest.WriteRequestBuilder; + +import static org.hamcrest.CoreMatchers.allOf; +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.assertTrue; +import static org.springframework.credhub.support.AdditionalPermission.Operation.READ; +import static org.springframework.credhub.support.AdditionalPermission.Operation.WRITE; +import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; +import static org.valid4j.matchers.jsonpath.JsonPathMatchers.isJson; + +public abstract class WriteRequestUnitTestsBase { + protected ObjectMapper mapper; + protected WriteRequestBuilder requestBuilder; + + @Before + public void setUpWriteRequestUnitTestsBase() { + mapper = new ObjectMapper(); + } + + @Test + public void typeIsSerializable() { + assertTrue(mapper.canSerialize(WriteRequest.class)); + } + + @Test + public void serializationWithOnePermission() throws Exception { + requestBuilder + .additionalPermission(AdditionalPermission.builder() + .app("app-id") + .operation(READ) + .build()); + + String jsonValue = serializeToJson(requestBuilder); + + assertThat(jsonValue, + allOf(hasJsonPath("$.additional_permissions[0].actor", + equalTo("mtls-app:app-id")), + hasJsonPath("$.additional_permissions[0].operations[0]", + equalTo("read")))); + } + + @Test + public void serializationWithTwoPermissions() throws Exception { + requestBuilder + .additionalPermission(AdditionalPermission.builder() + .app("app1-id") + .operation(READ).operation(WRITE) + .build()) + .additionalPermission(AdditionalPermission.builder() + .app("app2-id") + .operation(WRITE).operation(READ) + .build()); + + String jsonValue = serializeToJson(requestBuilder); + + assertThat(jsonValue, allOf( + hasJsonPath("$.additional_permissions[0].actor", + equalTo("mtls-app:app1-id")), + hasJsonPath("$.additional_permissions[0].operations[0]", equalTo("read")), + hasJsonPath("$.additional_permissions[0].operations[1]", + equalTo("write")), + hasJsonPath("$.additional_permissions[1].actor", + equalTo("mtls-app:app2-id")), + hasJsonPath("$.additional_permissions[1].operations[0]", + equalTo("write")), + hasJsonPath("$.additional_permissions[1].operations[1]", + equalTo("read")))); + } + + protected String serializeToJson(WriteRequestBuilder requestBuilder) + throws JsonProcessingException { + String jsonValue = mapper.writeValueAsString(requestBuilder.build()); + assertThat(jsonValue, isJson()); + return jsonValue; + } +} diff --git a/spring-credhub-demo/src/main/java/org/springframework/credhub/demo/CredHubDemoController.java b/spring-credhub-demo/src/main/java/org/springframework/credhub/demo/CredHubDemoController.java index 7af000a..276c6d1 100644 --- a/spring-credhub-demo/src/main/java/org/springframework/credhub/demo/CredHubDemoController.java +++ b/spring-credhub-demo/src/main/java/org/springframework/credhub/demo/CredHubDemoController.java @@ -29,9 +29,11 @@ import org.springframework.credhub.support.AdditionalPermission; import org.springframework.credhub.support.CredentialDetails; import org.springframework.credhub.support.CredentialName; import org.springframework.credhub.support.CredentialSummary; +import org.springframework.credhub.support.JsonWriteRequest; import org.springframework.credhub.support.SimpleCredentialName; import org.springframework.credhub.support.VcapServicesData; import org.springframework.credhub.support.WriteRequest; +import org.springframework.credhub.support.WriteRequest.WriteRequestBuilder; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -73,12 +75,12 @@ public class CredHubDemoController { } @SuppressWarnings("unchecked") - private CredentialDetails writeCredentials(T value, Results results) { + private CredentialDetails writeCredentials(Map value, Results results) { try { - WriteRequest.WriteRequestBuilder requestBuilder = WriteRequest.builder() + WriteRequestBuilder requestBuilder = JsonWriteRequest.builder() .overwrite(true) .name(new SimpleCredentialName("spring-credhub", "demo", "credentials_json")) - .jsonValue((Map) value); + .value(value); if (StringUtils.hasText(appId)) { requestBuilder.additionalPermission(