diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/core/JsonUtils.java b/spring-credhub-core/src/main/java/org/springframework/credhub/core/JsonUtils.java index 4aaed81..1fd0483 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/core/JsonUtils.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/core/JsonUtils.java @@ -24,6 +24,8 @@ import com.fasterxml.jackson.databind.jsontype.NamedType; import com.fasterxml.jackson.databind.util.ISO8601DateFormat; import org.springframework.credhub.support.JsonCredential; +import org.springframework.credhub.support.PasswordCredential; +import org.springframework.credhub.support.ValueCredential; import org.springframework.credhub.support.ValueType; /** @@ -58,7 +60,8 @@ public class JsonUtils { */ private static void configureCredentialDetailValueTypeMapping(ObjectMapper objectMapper) { objectMapper.registerSubtypes( - new NamedType(String.class, ValueType.PASSWORD.type()), + new NamedType(PasswordCredential.class, ValueType.PASSWORD.type()), + new NamedType(ValueCredential.class, ValueType.VALUE.type()), new NamedType(JsonCredential.class, ValueType.JSON.type()) ); } 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 index d2a08a4..637f37d 100644 --- 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 @@ -60,7 +60,7 @@ public class JsonWriteRequest extends WriteRequest { * 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} + * @param value the credential value; must not be {@literal null} * @return the builder */ public JsonWriteRequestBuilder value(JsonCredential value) { diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordCredential.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordCredential.java new file mode 100644 index 0000000..14f013a --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/PasswordCredential.java @@ -0,0 +1,47 @@ +/* + * 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.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * A container type for a credential that contains a single string {@literal password} value. + * + * @author Scott Frederick + */ +public class PasswordCredential extends StringCredential { + /** + * Create a {@link PasswordCredential} containing the specified password value. + * + * @param value the password + */ + @JsonCreator + public PasswordCredential(String value) { + super(value); + } + + /** + * Get the password value. + * + * @return the password value + */ + @JsonValue + public String getPassword() { + return value; + } +} 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 index dec3a34..7e69112 100644 --- 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 @@ -25,7 +25,7 @@ import static org.springframework.credhub.support.ValueType.PASSWORD; * * @author Scott Frederick */ -public class PasswordWriteRequest extends WriteRequest { +public class PasswordWriteRequest extends WriteRequest { /** * Create a builder that provides a fluent API for providing the values required * to construct a {@link PasswordWriteRequest}. @@ -40,7 +40,7 @@ public class PasswordWriteRequest extends WriteRequest { * A builder that provides a fluent API for constructing {@link PasswordWriteRequest}s. */ public static class PasswordWriteRequestBuilder - extends WriteRequestBuilder { + extends WriteRequestBuilder { @Override protected PasswordWriteRequest createTarget() { return new PasswordWriteRequest(); @@ -55,10 +55,10 @@ public class PasswordWriteRequest extends WriteRequest { * 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} + * @param value the credential value; must not be {@literal null} * @return the builder */ - public PasswordWriteRequestBuilder value(String value) { + public PasswordWriteRequestBuilder value(PasswordCredential value) { Assert.notNull(value, "value must not be null"); targetObj.setType(PASSWORD); targetObj.setValue(value); diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/StringCredential.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/StringCredential.java new file mode 100644 index 0000000..521e3a5 --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/StringCredential.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; + +/** + * A base type for a credential that contains a single string value. + * + * @author Scott Frederick + */ +public class StringCredential { + protected String value; + + /** + * Create a credential containing the specified value. + * + * @param value the credential value + */ + protected StringCredential(String value) { + this.value = value; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof StringCredential)) return false; + + StringCredential that = (StringCredential) o; + + if (value != null ? !value.equals(that.value) : that.value != null) return false; + + return true; + } + + @Override + public int hashCode() { + return value != null ? value.hashCode() : 0; + } + + @Override + public String toString() { + return value; + } +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueCredential.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueCredential.java new file mode 100644 index 0000000..e6c4e2b --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueCredential.java @@ -0,0 +1,47 @@ +/* + * 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.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonValue; + +/** + * A container type for a credential that contains a single string value. + * + * @author Scott Frederick + */ +public class ValueCredential extends StringCredential { + /** + * Create a {@link ValueCredential} containing the specified string value. + * + * @param value the value + */ + @JsonCreator + public ValueCredential(String value) { + super(value); + } + + /** + * Get the credential value. + * + * @return the credential value + */ + @JsonValue + public String getValue() { + return value; + } +} diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueType.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueType.java index 7495354..846eff9 100644 --- a/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueType.java +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueType.java @@ -30,6 +30,12 @@ public enum ValueType { */ PASSWORD("password"), + /** + * A value credential consists of a single string value. The value + * is provided by the client (i.e. not generated by CredHub). + */ + VALUE("value"), + /** * A JSON credential consists of one or more fields in a JSON document. The keys and * values in the JSON document are determined by the client. diff --git a/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueWriteRequest.java b/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueWriteRequest.java new file mode 100644 index 0000000..83c12bd --- /dev/null +++ b/spring-credhub-core/src/main/java/org/springframework/credhub/support/ValueWriteRequest.java @@ -0,0 +1,70 @@ +/* + * 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 org.springframework.util.Assert; + +import static org.springframework.credhub.support.ValueType.VALUE; + +/** + * The details of a request to write a new or update an existing value credential in CredHub. + * + * @author Scott Frederick + */ +public class ValueWriteRequest extends WriteRequest { + /** + * Create a builder that provides a fluent API for providing the values required + * to construct a {@link ValueWriteRequest}. + * + * @return a builder + */ + public static ValueWriteRequestBuilder builder() { + return new ValueWriteRequestBuilder(); + } + + /** + * A builder that provides a fluent API for constructing {@link ValueWriteRequest}s. + */ + public static class ValueWriteRequestBuilder + extends WriteRequestBuilder { + @Override + protected ValueWriteRequest createTarget() { + return new ValueWriteRequest(); + } + + @Override + protected ValueWriteRequestBuilder createBuilder() { + return this; + } + + /** + * Set the value of a {@literal value} credential. A {@literal value} credential + * consists of a single string value. The type of the credential is set to + * {@link ValueType#VALUE}. + * + * @param value the credential value; must not be {@literal null} + * @return the builder + */ + public ValueWriteRequestBuilder value(ValueCredential value) { + Assert.notNull(value, "value must not be null"); + targetObj.setType(VALUE); + targetObj.setValue(value); + return this; + } + } + +} 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/CredHubTemplateDetailJsonUnitTests.java new file mode 100644 index 0000000..e461340 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailJsonUnitTests.java @@ -0,0 +1,107 @@ +/* + * 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; + +import org.junit.experimental.theories.DataPoint; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +import org.springframework.credhub.support.CredentialDetails; +import org.springframework.credhub.support.CredentialDetailsData; +import org.springframework.credhub.support.JsonCredential; +import org.springframework.credhub.support.JsonWriteRequest; +import org.springframework.credhub.support.ValueType; +import org.springframework.credhub.support.WriteRequest; +import org.springframework.http.ResponseEntity; + +import static org.springframework.http.HttpStatus.OK; +import static org.springframework.http.HttpStatus.UNAUTHORIZED; + +@RunWith(Theories.class) +public class CredHubTemplateDetailJsonUnitTests + extends CredHubTemplateDetailUnitTestsBase { + private static final JsonCredential CREDENTIAL = new JsonCredential() { + { + put("data", "value"); + put("test", true); + } + }; + + @DataPoint("detail-responses") + public static ResponseEntity> successfulDetailResponse = + new ResponseEntity>( + new CredentialDetails(CREDENTIAL_ID, NAME, + ValueType.JSON, CREDENTIAL), + OK); + + @DataPoint("detail-responses") + public static ResponseEntity> httpErrorDetailResponse = + new ResponseEntity>( + new CredentialDetails(), UNAUTHORIZED); + + @DataPoint("data-responses") + public static ResponseEntity> successfulResponse = + new ResponseEntity>( + new CredentialDetailsData( + new CredentialDetails(CREDENTIAL_ID, NAME, + ValueType.JSON, CREDENTIAL)), + OK); + + @DataPoint("data-responses") + public static ResponseEntity> httpErrorResponse = + new ResponseEntity>( + new CredentialDetailsData(), UNAUTHORIZED); + + @Override + public WriteRequest getRequest() { + return JsonWriteRequest.builder() + .name(NAME) + .value(CREDENTIAL) + .build(); + } + + @Override + public Class getType() { + return JsonCredential.class; + } + + @Theory + public void write(@FromDataPoints("detail-responses") + ResponseEntity> expectedResponse) { + verifyWrite(expectedResponse); + } + + @Theory + public void getById(@FromDataPoints("detail-responses") + ResponseEntity> expectedResponse) { + verifyGetById(expectedResponse); + } + + @Theory + public void getByNameWithString(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithString(expectedResponse); + } + + @Theory + public void getByNameWithCredentialName(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithCredentialName(expectedResponse); + } +} \ No newline at end of file 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/CredHubTemplateDetailPasswordUnitTests.java new file mode 100644 index 0000000..5716137 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailPasswordUnitTests.java @@ -0,0 +1,102 @@ +/* + * 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; + +import org.junit.experimental.theories.DataPoint; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +import org.springframework.credhub.support.CredentialDetails; +import org.springframework.credhub.support.CredentialDetailsData; +import org.springframework.credhub.support.PasswordCredential; +import org.springframework.credhub.support.PasswordWriteRequest; +import org.springframework.credhub.support.ValueType; +import org.springframework.credhub.support.WriteRequest; +import org.springframework.http.ResponseEntity; + +import static org.springframework.http.HttpStatus.OK; +import static org.springframework.http.HttpStatus.UNAUTHORIZED; + +@RunWith(Theories.class) +public class CredHubTemplateDetailPasswordUnitTests + extends CredHubTemplateDetailUnitTestsBase { + private static final PasswordCredential CREDENTIAL = new PasswordCredential("secret"); + + @DataPoint("detail-responses") + public static ResponseEntity> successfulDetailResponse = + new ResponseEntity>( + new CredentialDetails(CREDENTIAL_ID, NAME, + ValueType.PASSWORD, CREDENTIAL), + OK); + + @DataPoint("detail-responses") + public static ResponseEntity> httpErrorDetailResponse = + new ResponseEntity>( + new CredentialDetails(), UNAUTHORIZED); + + @DataPoint("data-responses") + public static ResponseEntity> successfulResponse = + new ResponseEntity>( + new CredentialDetailsData( + new CredentialDetails(CREDENTIAL_ID, NAME, + ValueType.PASSWORD, CREDENTIAL)), + OK); + + @DataPoint("data-responses") + public static ResponseEntity> httpErrorResponse = + new ResponseEntity>( + new CredentialDetailsData(), UNAUTHORIZED); + + @Override + public WriteRequest getRequest() { + return PasswordWriteRequest.builder() + .name(NAME) + .value(CREDENTIAL) + .build(); + } + + @Override + public Class getType() { + return PasswordCredential.class; + } + + @Theory + public void write(@FromDataPoints("detail-responses") + ResponseEntity> expectedResponse) { + verifyWrite(expectedResponse); + } + + @Theory + public void getById(@FromDataPoints("detail-responses") + ResponseEntity> expectedResponse) { + verifyGetById(expectedResponse); + } + + @Theory + public void getByNameWithString(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithString(expectedResponse); + } + + @Theory + public void getByNameWithCredentialName(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithCredentialName(expectedResponse); + } +} \ No newline at end of file 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 deleted file mode 100644 index 36702bf..0000000 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailResponseUnitTests.java +++ /dev/null @@ -1,119 +0,0 @@ -/* - * 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; - -import org.junit.experimental.theories.DataPoint; -import org.junit.experimental.theories.FromDataPoints; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; - -import org.springframework.core.ParameterizedTypeReference; -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; -import org.springframework.http.ResponseEntity; - -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.when; -import static org.springframework.credhub.core.CredHubTemplate.BASE_URL_PATH; -import static org.springframework.credhub.core.CredHubTemplate.ID_URL_PATH; -import static org.springframework.credhub.core.TypeUtils.getDetailsReference; -import static org.springframework.http.HttpMethod.GET; -import static org.springframework.http.HttpMethod.PUT; -import static org.springframework.http.HttpStatus.OK; -import static org.springframework.http.HttpStatus.UNAUTHORIZED; - -@RunWith(Theories.class) -public class CredHubTemplateDetailResponseUnitTests extends CredHubTemplateUnitTestsBase { - private static final String CREDENTIAL_ID = "1111-1111-1111-1111"; - private static final String CREDENTIAL_VALUE = "secret"; - - @DataPoint("responses") - public static ResponseEntity> successfulResponse = - new ResponseEntity>( - new CredentialDetails(CREDENTIAL_ID, NAME, ValueType.PASSWORD, CREDENTIAL_VALUE), - OK); - - @DataPoint("responses") - public static ResponseEntity> httpErrorResponse = - new ResponseEntity>(new CredentialDetails(), UNAUTHORIZED); - - @Theory - public void write(@FromDataPoints("responses") ResponseEntity> expectedResponse) { - PasswordWriteRequest request = PasswordWriteRequest.builder() - .name(NAME) - .value("secret") - .build(); - - final ParameterizedTypeReference> ref = getDetailsReference(String.class); - - when(restTemplate.exchange(BASE_URL_PATH, PUT, new HttpEntity>(request), ref)) - .thenReturn(expectedResponse); - - if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) { - try { - credHubTemplate.write(request); - fail("Exception should have been thrown"); - } - catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); - } - } - else { - CredentialDetails response = credHubTemplate.write(request); - - assertResponseContainsExpectedCredentials(expectedResponse, response); - } - } - - @Theory - public void getById(@FromDataPoints("responses") ResponseEntity> expectedResponse) { - final ParameterizedTypeReference> ref = getDetailsReference(String.class); - - when(restTemplate.exchange(ID_URL_PATH, GET, null, ref, CREDENTIAL_ID)) - .thenReturn(expectedResponse); - - if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) { - try { - credHubTemplate.getById(CREDENTIAL_ID, String.class); - fail("Exception should have been thrown"); - } - catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); - } - } - else { - CredentialDetails response = credHubTemplate.getById(CREDENTIAL_ID, String.class); - - assertResponseContainsExpectedCredentials(expectedResponse, response); - } - } - - private void assertResponseContainsExpectedCredentials( - ResponseEntity> expectedResponse, CredentialDetails response) { - assertThat(response, notNullValue()); - assertThat(response, equalTo(expectedResponse.getBody())); - } -} \ No newline at end of file diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java new file mode 100644 index 0000000..cc71bef --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailUnitTestsBase.java @@ -0,0 +1,163 @@ +/* + * 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; + +import java.util.List; + +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.credhub.support.CredentialDetails; +import org.springframework.credhub.support.CredentialDetailsData; +import org.springframework.credhub.support.WriteRequest; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.notNullValue; +import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; +import static org.mockito.Mockito.when; +import static org.springframework.credhub.core.CredHubTemplate.BASE_URL_PATH; +import static org.springframework.credhub.core.CredHubTemplate.ID_URL_PATH; +import static org.springframework.credhub.core.CredHubTemplate.NAME_URL_QUERY; +import static org.springframework.credhub.core.TypeUtils.getDetailsReference; +import static org.springframework.http.HttpMethod.GET; +import static org.springframework.http.HttpMethod.PUT; +import static org.springframework.http.HttpStatus.OK; + +public abstract class CredHubTemplateDetailUnitTestsBase extends CredHubTemplateUnitTestsBase { + static final String CREDENTIAL_ID = "1111-1111-1111-1111"; + + public abstract WriteRequest getRequest(); + public abstract Class getType(); + + void verifyWrite(ResponseEntity> expectedResponse) { + WriteRequest request = getRequest(); + + final ParameterizedTypeReference> ref = + getDetailsReference(getType()); + + when(restTemplate.exchange(BASE_URL_PATH, PUT, + new HttpEntity>(request), ref)) + .thenReturn(expectedResponse); + + if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) { + try { + credHubTemplate.write(request); + fail("Exception should have been thrown"); + } + catch (CredHubException e) { + assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); + } + } + else { + CredentialDetails response = credHubTemplate.write(request); + + assertResponseContainsExpectedCredentials(expectedResponse, response); + } + } + + void verifyGetById(ResponseEntity> expectedResponse) { + final ParameterizedTypeReference> ref = + getDetailsReference(getType()); + + when(restTemplate.exchange(ID_URL_PATH, GET, null, ref, CREDENTIAL_ID)) + .thenReturn(expectedResponse); + + if (!expectedResponse.getStatusCode().equals(HttpStatus.OK)) { + try { + credHubTemplate.getById(CREDENTIAL_ID, String.class); + fail("Exception should have been thrown"); + } + catch (CredHubException e) { + assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); + } + } + else { + CredentialDetails response = + credHubTemplate.getById(CREDENTIAL_ID, getType()); + + assertResponseContainsExpectedCredentials(expectedResponse, response); + } + } + + void verifyGetByNameWithString(ResponseEntity> expectedResponse) { + ParameterizedTypeReference> ref = TypeUtils + .getDetailsDataReference(getType()); + + when(restTemplate.exchange(NAME_URL_QUERY, GET, null, ref, NAME.getName())) + .thenReturn(expectedResponse); + + if (!expectedResponse.getStatusCode().equals(OK)) { + try { + credHubTemplate.getByName(NAME.getName(), String.class); + fail("Exception should have been thrown"); + } + catch (CredHubException e) { + assertThat(e.getMessage(), + containsString(expectedResponse.getStatusCode().toString())); + } + } + else { + List> response = credHubTemplate + .getByName(NAME.getName(), getType()); + + assertResponseContainsExpectedCredentials(expectedResponse, response); + } + } + + + void verifyGetByNameWithCredentialName(ResponseEntity> expectedResponse) { + ParameterizedTypeReference> ref = TypeUtils + .getDetailsDataReference(getType()); + + when(restTemplate.exchange(NAME_URL_QUERY, GET, null, ref, NAME.getName())) + .thenReturn(expectedResponse); + + if (!expectedResponse.getStatusCode().equals(OK)) { + try { + credHubTemplate.getByName(NAME, String.class); + fail("Exception should have been thrown"); + } + catch (CredHubException e) { + assertThat(e.getMessage(), + containsString(expectedResponse.getStatusCode().toString())); + } + } + else { + List> response = credHubTemplate.getByName(NAME, getType()); + + assertResponseContainsExpectedCredentials(expectedResponse, response); + } + } + + private void assertResponseContainsExpectedCredentials( + ResponseEntity> expectedResponse, + List> response) { + assertThat(response, notNullValue()); + assertThat(response.size(), equalTo(expectedResponse.getBody().getData().size())); + assertThat(response.get(0), equalTo(expectedResponse.getBody().getData().get(0))); + } + + private void assertResponseContainsExpectedCredentials( + ResponseEntity> expectedResponse, + CredentialDetails response) { + assertThat(response, notNullValue()); + assertThat(response, equalTo(expectedResponse.getBody())); + } +} \ No newline at end of file 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/CredHubTemplateDetailValueUnitTests.java new file mode 100644 index 0000000..70d43d3 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailValueUnitTests.java @@ -0,0 +1,102 @@ +/* + * 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; + +import org.junit.experimental.theories.DataPoint; +import org.junit.experimental.theories.FromDataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; + +import org.springframework.credhub.support.CredentialDetails; +import org.springframework.credhub.support.CredentialDetailsData; +import org.springframework.credhub.support.ValueCredential; +import org.springframework.credhub.support.ValueType; +import org.springframework.credhub.support.ValueWriteRequest; +import org.springframework.credhub.support.WriteRequest; +import org.springframework.http.ResponseEntity; + +import static org.springframework.http.HttpStatus.OK; +import static org.springframework.http.HttpStatus.UNAUTHORIZED; + +@RunWith(Theories.class) +public class CredHubTemplateDetailValueUnitTests + extends CredHubTemplateDetailUnitTestsBase { + private static final ValueCredential CREDENTIAL = new ValueCredential("secret"); + + @DataPoint("detail-responses") + public static ResponseEntity> successfulDetailResponse = + new ResponseEntity>( + new CredentialDetails(CREDENTIAL_ID, NAME, + ValueType.VALUE, CREDENTIAL), + OK); + + @DataPoint("detail-responses") + public static ResponseEntity> httpErrorDetailResponse = + new ResponseEntity>( + new CredentialDetails(), UNAUTHORIZED); + + @DataPoint("data-responses") + public static ResponseEntity> successfulResponse = + new ResponseEntity>( + new CredentialDetailsData( + new CredentialDetails(CREDENTIAL_ID, NAME, + ValueType.VALUE, CREDENTIAL)), + OK); + + @DataPoint("data-responses") + public static ResponseEntity> httpErrorResponse = + new ResponseEntity>( + new CredentialDetailsData(), UNAUTHORIZED); + + @Override + public WriteRequest getRequest() { + return ValueWriteRequest.builder() + .name(NAME) + .value(CREDENTIAL) + .build(); + } + + @Override + public Class getType() { + return ValueCredential.class; + } + + @Theory + public void write(@FromDataPoints("detail-responses") + ResponseEntity> expectedResponse) { + verifyWrite(expectedResponse); + } + + @Theory + public void getById(@FromDataPoints("detail-responses") + ResponseEntity> expectedResponse) { + verifyGetById(expectedResponse); + } + + @Theory + public void getByNameWithString(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithString(expectedResponse); + } + + @Theory + public void getByNameWithCredentialName(@FromDataPoints("data-responses") + ResponseEntity> expectedResponse) { + verifyGetByNameWithCredentialName(expectedResponse); + } +} \ No newline at end of file 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 deleted file mode 100644 index b524cd7..0000000 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/core/CredHubTemplateDetailsResponseUnitTests.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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; - -import java.util.List; - -import org.junit.experimental.theories.DataPoint; -import org.junit.experimental.theories.FromDataPoints; -import org.junit.experimental.theories.Theories; -import org.junit.experimental.theories.Theory; -import org.junit.runner.RunWith; - -import org.springframework.core.ParameterizedTypeReference; -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; -import static org.hamcrest.Matchers.equalTo; -import static org.hamcrest.Matchers.notNullValue; -import static org.junit.Assert.assertThat; -import static org.junit.Assert.fail; -import static org.mockito.Mockito.when; -import static org.springframework.credhub.core.CredHubTemplate.NAME_URL_QUERY; -import static org.springframework.http.HttpMethod.GET; -import static org.springframework.http.HttpStatus.OK; -import static org.springframework.http.HttpStatus.UNAUTHORIZED; - -@RunWith(Theories.class) -public class CredHubTemplateDetailsResponseUnitTests extends CredHubTemplateUnitTestsBase { - private static final String CREDENTIAL_ID = "1111-1111-1111-1111"; - private static final String CREDENTIAL_VALUE = "secret"; - - @DataPoint("responses") - public static ResponseEntity> successfulResponse = - new ResponseEntity>( - new CredentialDetailsData( - new CredentialDetails(CREDENTIAL_ID, NAME, ValueType.PASSWORD, CREDENTIAL_VALUE)), - OK); - - @DataPoint("responses") - public static ResponseEntity> httpErrorResponse = - new ResponseEntity>(new CredentialDetailsData(), UNAUTHORIZED); - - @Theory - public void getByNameWithString(@FromDataPoints("responses") - ResponseEntity> expectedResponse) { - ParameterizedTypeReference> ref = TypeUtils.getDetailsDataReference(String.class); - - when(restTemplate.exchange(NAME_URL_QUERY, GET, null, ref, NAME.getName())) - .thenReturn(expectedResponse); - - if (!expectedResponse.getStatusCode().equals(OK)) { - try { - credHubTemplate.getByName(NAME.getName(), String.class); - fail("Exception should have been thrown"); - } - catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); - } - } - else { - List> response = credHubTemplate.getByName(NAME.getName(), String.class); - - assertResponseContainsExpectedCredentials(expectedResponse, response); - } - } - - @Theory - public void getByNameWithCredentialName(@FromDataPoints("responses") - ResponseEntity> expectedResponse) { - ParameterizedTypeReference> ref = TypeUtils.getDetailsDataReference(String.class); - - when(restTemplate.exchange(NAME_URL_QUERY, GET, null, ref, NAME.getName())) - .thenReturn(expectedResponse); - - if (!expectedResponse.getStatusCode().equals(OK)) { - try { - credHubTemplate.getByName(NAME, String.class); - fail("Exception should have been thrown"); - } - catch (CredHubException e) { - assertThat(e.getMessage(), containsString(expectedResponse.getStatusCode().toString())); - } - } - else { - List> response = credHubTemplate.getByName(NAME, String.class); - - assertResponseContainsExpectedCredentials(expectedResponse, response); - } - } - - private void assertResponseContainsExpectedCredentials( - ResponseEntity> expectedResponse, - List> response) { - assertThat(response, notNullValue()); - assertThat(response.size(), equalTo(expectedResponse.getBody().getData().size())); - assertThat(response.get(0), equalTo(expectedResponse.getBody().getData().get(0))); - } -} \ No newline at end of file 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 8b6f3f2..a7b9ef7 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 @@ -16,7 +16,6 @@ package org.springframework.credhub.core; -import java.util.Date; import java.util.List; import org.junit.experimental.theories.DataPoint; diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialDetailsPasswordUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialDetailsPasswordUnitTests.java index 45115b0..1e05ee2 100644 --- a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialDetailsPasswordUnitTests.java +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialDetailsPasswordUnitTests.java @@ -28,26 +28,28 @@ public class CredentialDetailsPasswordUnitTests extends JsonParsingUnitTestsBase @Test public void deserializeDetails() throws Exception { - CredentialDetails data = parseDetails(PASSWORD_CREDENTIALS, String.class); + CredentialDetails data = + parseDetails(PASSWORD_CREDENTIALS, PasswordCredential.class); assertDetails(data); } @Test public void deserializeDetailsData() throws Exception { - CredentialDetailsData response = parseDetailsData(PASSWORD_CREDENTIALS, String.class); + CredentialDetailsData response = + parseDetailsData(PASSWORD_CREDENTIALS, PasswordCredential.class); assertThat(response.getData().size(), equalTo(1)); - CredentialDetails data = response.getData().get(0); + CredentialDetails data = response.getData().get(0); assertDetails(data); } - private void assertDetails(CredentialDetails data) { + private void assertDetails(CredentialDetails data) { assertCommonDetails(data); assertThat(data.getValueType(), equalTo(ValueType.PASSWORD)); - assertThat(data.getValue(), equalTo("secret")); + assertThat(data.getValue().getPassword(), equalTo("secret")); } } diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialDetailsValueUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialDetailsValueUnitTests.java new file mode 100644 index 0000000..5b24d52 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/CredentialDetailsValueUnitTests.java @@ -0,0 +1,55 @@ +/* + * 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 org.junit.Test; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.junit.Assert.assertThat; + +public class CredentialDetailsValueUnitTests extends JsonParsingUnitTestsBase { + private static final String VALUE_CREDENTIALS = + " \"type\": \"value\"," + + " \"value\": \"somevalue\""; + + @Test + public void deserializeDetails() throws Exception { + CredentialDetails data = + parseDetails(VALUE_CREDENTIALS, ValueCredential.class); + + assertDetails(data); + } + + @Test + public void deserializeDetailsData() throws Exception { + CredentialDetailsData response = + parseDetailsData(VALUE_CREDENTIALS, ValueCredential.class); + + assertThat(response.getData().size(), equalTo(1)); + + CredentialDetails data = response.getData().get(0); + + assertDetails(data); + } + + private void assertDetails(CredentialDetails data) { + assertCommonDetails(data); + + assertThat(data.getValueType(), equalTo(ValueType.VALUE)); + assertThat(data.getValue().getValue(), equalTo("somevalue")); + } +} 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 index 0b2dc16..3bb6b43 100644 --- 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 @@ -34,7 +34,7 @@ public class PasswordWriteRequestUnitTests extends WriteRequestUnitTestsBase { requestBuilder = PasswordWriteRequest.builder() .name(new SimpleCredentialName("example", "credential")) .overwrite(true) - .value("secret"); + .value(new PasswordCredential("secret")); } @Test diff --git a/spring-credhub-core/src/test/java/org/springframework/credhub/support/ValueWriteRequestUnitTests.java b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ValueWriteRequestUnitTests.java new file mode 100644 index 0000000..f008958 --- /dev/null +++ b/spring-credhub-core/src/test/java/org/springframework/credhub/support/ValueWriteRequestUnitTests.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; + +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 ValueWriteRequestUnitTests extends WriteRequestUnitTestsBase { + + @Before + public void setUp() { + requestBuilder = ValueWriteRequest.builder() + .name(new SimpleCredentialName("example", "credential")) + .overwrite(true) + .value(new ValueCredential("somevalue")); + } + + @Test + public void serializeWithValue() throws Exception { + String jsonValue = serializeToJson(requestBuilder); + + assertThat(jsonValue, + allOf(hasJsonPath("$.overwrite", equalTo(true)), + hasJsonPath("$.name", equalTo("/c/example/credential")), + hasJsonPath("$.type", equalTo("value")), + hasJsonPath("$.value", equalTo("somevalue")))); + + assertThat(jsonValue, hasNoJsonPath("$.additional_permissions")); + } +} \ 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 index ec02290..578d52f 100644 --- 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 @@ -33,7 +33,7 @@ import static org.valid4j.matchers.jsonpath.JsonPathMatchers.hasJsonPath; import static org.valid4j.matchers.jsonpath.JsonPathMatchers.isJson; public abstract class WriteRequestUnitTestsBase { - protected ObjectMapper mapper; + private ObjectMapper mapper; protected WriteRequestBuilder requestBuilder; @Before @@ -91,7 +91,7 @@ public abstract class WriteRequestUnitTestsBase { equalTo("read")))); } - protected String serializeToJson(WriteRequestBuilder requestBuilder) + String serializeToJson(T requestBuilder) throws JsonProcessingException { String jsonValue = mapper.writeValueAsString(requestBuilder.build()); assertThat(jsonValue, isJson());