Add support for credentials of type "value".
This commit is contained in:
@@ -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())
|
||||
);
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class JsonWriteRequest extends WriteRequest<JsonCredential> {
|
||||
* 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) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,7 @@ import static org.springframework.credhub.support.ValueType.PASSWORD;
|
||||
*
|
||||
* @author Scott Frederick
|
||||
*/
|
||||
public class PasswordWriteRequest extends WriteRequest<String> {
|
||||
public class PasswordWriteRequest extends WriteRequest<PasswordCredential> {
|
||||
/**
|
||||
* 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<String> {
|
||||
* A builder that provides a fluent API for constructing {@link PasswordWriteRequest}s.
|
||||
*/
|
||||
public static class PasswordWriteRequestBuilder
|
||||
extends WriteRequestBuilder<String, PasswordWriteRequest, PasswordWriteRequestBuilder> {
|
||||
extends WriteRequestBuilder<PasswordCredential, PasswordWriteRequest, PasswordWriteRequestBuilder> {
|
||||
@Override
|
||||
protected PasswordWriteRequest createTarget() {
|
||||
return new PasswordWriteRequest();
|
||||
@@ -55,10 +55,10 @@ public class PasswordWriteRequest extends WriteRequest<String> {
|
||||
* 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);
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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.
|
||||
|
||||
@@ -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<ValueCredential> {
|
||||
/**
|
||||
* 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<ValueCredential, ValueWriteRequest, ValueWriteRequestBuilder> {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<JsonCredential> {
|
||||
private static final JsonCredential CREDENTIAL = new JsonCredential() {
|
||||
{
|
||||
put("data", "value");
|
||||
put("test", true);
|
||||
}
|
||||
};
|
||||
|
||||
@DataPoint("detail-responses")
|
||||
public static ResponseEntity<CredentialDetails<JsonCredential>> successfulDetailResponse =
|
||||
new ResponseEntity<CredentialDetails<JsonCredential>>(
|
||||
new CredentialDetails<JsonCredential>(CREDENTIAL_ID, NAME,
|
||||
ValueType.JSON, CREDENTIAL),
|
||||
OK);
|
||||
|
||||
@DataPoint("detail-responses")
|
||||
public static ResponseEntity<CredentialDetails<JsonCredential>> httpErrorDetailResponse =
|
||||
new ResponseEntity<CredentialDetails<JsonCredential>>(
|
||||
new CredentialDetails<JsonCredential>(), UNAUTHORIZED);
|
||||
|
||||
@DataPoint("data-responses")
|
||||
public static ResponseEntity<CredentialDetailsData<JsonCredential>> successfulResponse =
|
||||
new ResponseEntity<CredentialDetailsData<JsonCredential>>(
|
||||
new CredentialDetailsData<JsonCredential>(
|
||||
new CredentialDetails<JsonCredential>(CREDENTIAL_ID, NAME,
|
||||
ValueType.JSON, CREDENTIAL)),
|
||||
OK);
|
||||
|
||||
@DataPoint("data-responses")
|
||||
public static ResponseEntity<CredentialDetailsData<JsonCredential>> httpErrorResponse =
|
||||
new ResponseEntity<CredentialDetailsData<JsonCredential>>(
|
||||
new CredentialDetailsData<JsonCredential>(), UNAUTHORIZED);
|
||||
|
||||
@Override
|
||||
public WriteRequest<JsonCredential> getRequest() {
|
||||
return JsonWriteRequest.builder()
|
||||
.name(NAME)
|
||||
.value(CREDENTIAL)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<JsonCredential> getType() {
|
||||
return JsonCredential.class;
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void write(@FromDataPoints("detail-responses")
|
||||
ResponseEntity<CredentialDetails<JsonCredential>> expectedResponse) {
|
||||
verifyWrite(expectedResponse);
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getById(@FromDataPoints("detail-responses")
|
||||
ResponseEntity<CredentialDetails<JsonCredential>> expectedResponse) {
|
||||
verifyGetById(expectedResponse);
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getByNameWithString(@FromDataPoints("data-responses")
|
||||
ResponseEntity<CredentialDetailsData<JsonCredential>> expectedResponse) {
|
||||
verifyGetByNameWithString(expectedResponse);
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getByNameWithCredentialName(@FromDataPoints("data-responses")
|
||||
ResponseEntity<CredentialDetailsData<JsonCredential>> expectedResponse) {
|
||||
verifyGetByNameWithCredentialName(expectedResponse);
|
||||
}
|
||||
}
|
||||
@@ -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<PasswordCredential> {
|
||||
private static final PasswordCredential CREDENTIAL = new PasswordCredential("secret");
|
||||
|
||||
@DataPoint("detail-responses")
|
||||
public static ResponseEntity<CredentialDetails<PasswordCredential>> successfulDetailResponse =
|
||||
new ResponseEntity<CredentialDetails<PasswordCredential>>(
|
||||
new CredentialDetails<PasswordCredential>(CREDENTIAL_ID, NAME,
|
||||
ValueType.PASSWORD, CREDENTIAL),
|
||||
OK);
|
||||
|
||||
@DataPoint("detail-responses")
|
||||
public static ResponseEntity<CredentialDetails<PasswordCredential>> httpErrorDetailResponse =
|
||||
new ResponseEntity<CredentialDetails<PasswordCredential>>(
|
||||
new CredentialDetails<PasswordCredential>(), UNAUTHORIZED);
|
||||
|
||||
@DataPoint("data-responses")
|
||||
public static ResponseEntity<CredentialDetailsData<PasswordCredential>> successfulResponse =
|
||||
new ResponseEntity<CredentialDetailsData<PasswordCredential>>(
|
||||
new CredentialDetailsData<PasswordCredential>(
|
||||
new CredentialDetails<PasswordCredential>(CREDENTIAL_ID, NAME,
|
||||
ValueType.PASSWORD, CREDENTIAL)),
|
||||
OK);
|
||||
|
||||
@DataPoint("data-responses")
|
||||
public static ResponseEntity<CredentialDetailsData<PasswordCredential>> httpErrorResponse =
|
||||
new ResponseEntity<CredentialDetailsData<PasswordCredential>>(
|
||||
new CredentialDetailsData<PasswordCredential>(), UNAUTHORIZED);
|
||||
|
||||
@Override
|
||||
public WriteRequest<PasswordCredential> getRequest() {
|
||||
return PasswordWriteRequest.builder()
|
||||
.name(NAME)
|
||||
.value(CREDENTIAL)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<PasswordCredential> getType() {
|
||||
return PasswordCredential.class;
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void write(@FromDataPoints("detail-responses")
|
||||
ResponseEntity<CredentialDetails<PasswordCredential>> expectedResponse) {
|
||||
verifyWrite(expectedResponse);
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getById(@FromDataPoints("detail-responses")
|
||||
ResponseEntity<CredentialDetails<PasswordCredential>> expectedResponse) {
|
||||
verifyGetById(expectedResponse);
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getByNameWithString(@FromDataPoints("data-responses")
|
||||
ResponseEntity<CredentialDetailsData<PasswordCredential>> expectedResponse) {
|
||||
verifyGetByNameWithString(expectedResponse);
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getByNameWithCredentialName(@FromDataPoints("data-responses")
|
||||
ResponseEntity<CredentialDetailsData<PasswordCredential>> expectedResponse) {
|
||||
verifyGetByNameWithCredentialName(expectedResponse);
|
||||
}
|
||||
}
|
||||
@@ -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<CredentialDetails<String>> successfulResponse =
|
||||
new ResponseEntity<CredentialDetails<String>>(
|
||||
new CredentialDetails<String>(CREDENTIAL_ID, NAME, ValueType.PASSWORD, CREDENTIAL_VALUE),
|
||||
OK);
|
||||
|
||||
@DataPoint("responses")
|
||||
public static ResponseEntity<CredentialDetails<String>> httpErrorResponse =
|
||||
new ResponseEntity<CredentialDetails<String>>(new CredentialDetails<String>(), UNAUTHORIZED);
|
||||
|
||||
@Theory
|
||||
public void write(@FromDataPoints("responses") ResponseEntity<CredentialDetails<String>> expectedResponse) {
|
||||
PasswordWriteRequest request = PasswordWriteRequest.builder()
|
||||
.name(NAME)
|
||||
.value("secret")
|
||||
.build();
|
||||
|
||||
final ParameterizedTypeReference<CredentialDetails<String>> ref = getDetailsReference(String.class);
|
||||
|
||||
when(restTemplate.exchange(BASE_URL_PATH, PUT, new HttpEntity<WriteRequest<String>>(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<String> response = credHubTemplate.write(request);
|
||||
|
||||
assertResponseContainsExpectedCredentials(expectedResponse, response);
|
||||
}
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getById(@FromDataPoints("responses") ResponseEntity<CredentialDetails<String>> expectedResponse) {
|
||||
final ParameterizedTypeReference<CredentialDetails<String>> 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<String> response = credHubTemplate.getById(CREDENTIAL_ID, String.class);
|
||||
|
||||
assertResponseContainsExpectedCredentials(expectedResponse, response);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertResponseContainsExpectedCredentials(
|
||||
ResponseEntity<CredentialDetails<String>> expectedResponse, CredentialDetails<String> response) {
|
||||
assertThat(response, notNullValue());
|
||||
assertThat(response, equalTo(expectedResponse.getBody()));
|
||||
}
|
||||
}
|
||||
@@ -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<T> extends CredHubTemplateUnitTestsBase {
|
||||
static final String CREDENTIAL_ID = "1111-1111-1111-1111";
|
||||
|
||||
public abstract WriteRequest<T> getRequest();
|
||||
public abstract Class<T> getType();
|
||||
|
||||
void verifyWrite(ResponseEntity<CredentialDetails<T>> expectedResponse) {
|
||||
WriteRequest<T> request = getRequest();
|
||||
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> ref =
|
||||
getDetailsReference(getType());
|
||||
|
||||
when(restTemplate.exchange(BASE_URL_PATH, PUT,
|
||||
new HttpEntity<WriteRequest<T>>(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<T> response = credHubTemplate.write(request);
|
||||
|
||||
assertResponseContainsExpectedCredentials(expectedResponse, response);
|
||||
}
|
||||
}
|
||||
|
||||
void verifyGetById(ResponseEntity<CredentialDetails<T>> expectedResponse) {
|
||||
final ParameterizedTypeReference<CredentialDetails<T>> 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<T> response =
|
||||
credHubTemplate.getById(CREDENTIAL_ID, getType());
|
||||
|
||||
assertResponseContainsExpectedCredentials(expectedResponse, response);
|
||||
}
|
||||
}
|
||||
|
||||
void verifyGetByNameWithString(ResponseEntity<CredentialDetailsData<T>> expectedResponse) {
|
||||
ParameterizedTypeReference<CredentialDetailsData<T>> 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<CredentialDetails<T>> response = credHubTemplate
|
||||
.getByName(NAME.getName(), getType());
|
||||
|
||||
assertResponseContainsExpectedCredentials(expectedResponse, response);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void verifyGetByNameWithCredentialName(ResponseEntity<CredentialDetailsData<T>> expectedResponse) {
|
||||
ParameterizedTypeReference<CredentialDetailsData<T>> 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<CredentialDetails<T>> response = credHubTemplate.getByName(NAME, getType());
|
||||
|
||||
assertResponseContainsExpectedCredentials(expectedResponse, response);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertResponseContainsExpectedCredentials(
|
||||
ResponseEntity<CredentialDetailsData<T>> expectedResponse,
|
||||
List<CredentialDetails<T>> 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<CredentialDetails<T>> expectedResponse,
|
||||
CredentialDetails<T> response) {
|
||||
assertThat(response, notNullValue());
|
||||
assertThat(response, equalTo(expectedResponse.getBody()));
|
||||
}
|
||||
}
|
||||
@@ -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<ValueCredential> {
|
||||
private static final ValueCredential CREDENTIAL = new ValueCredential("secret");
|
||||
|
||||
@DataPoint("detail-responses")
|
||||
public static ResponseEntity<CredentialDetails<ValueCredential>> successfulDetailResponse =
|
||||
new ResponseEntity<CredentialDetails<ValueCredential>>(
|
||||
new CredentialDetails<ValueCredential>(CREDENTIAL_ID, NAME,
|
||||
ValueType.VALUE, CREDENTIAL),
|
||||
OK);
|
||||
|
||||
@DataPoint("detail-responses")
|
||||
public static ResponseEntity<CredentialDetails<ValueCredential>> httpErrorDetailResponse =
|
||||
new ResponseEntity<CredentialDetails<ValueCredential>>(
|
||||
new CredentialDetails<ValueCredential>(), UNAUTHORIZED);
|
||||
|
||||
@DataPoint("data-responses")
|
||||
public static ResponseEntity<CredentialDetailsData<ValueCredential>> successfulResponse =
|
||||
new ResponseEntity<CredentialDetailsData<ValueCredential>>(
|
||||
new CredentialDetailsData<ValueCredential>(
|
||||
new CredentialDetails<ValueCredential>(CREDENTIAL_ID, NAME,
|
||||
ValueType.VALUE, CREDENTIAL)),
|
||||
OK);
|
||||
|
||||
@DataPoint("data-responses")
|
||||
public static ResponseEntity<CredentialDetailsData<ValueCredential>> httpErrorResponse =
|
||||
new ResponseEntity<CredentialDetailsData<ValueCredential>>(
|
||||
new CredentialDetailsData<ValueCredential>(), UNAUTHORIZED);
|
||||
|
||||
@Override
|
||||
public WriteRequest<ValueCredential> getRequest() {
|
||||
return ValueWriteRequest.builder()
|
||||
.name(NAME)
|
||||
.value(CREDENTIAL)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<ValueCredential> getType() {
|
||||
return ValueCredential.class;
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void write(@FromDataPoints("detail-responses")
|
||||
ResponseEntity<CredentialDetails<ValueCredential>> expectedResponse) {
|
||||
verifyWrite(expectedResponse);
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getById(@FromDataPoints("detail-responses")
|
||||
ResponseEntity<CredentialDetails<ValueCredential>> expectedResponse) {
|
||||
verifyGetById(expectedResponse);
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getByNameWithString(@FromDataPoints("data-responses")
|
||||
ResponseEntity<CredentialDetailsData<ValueCredential>> expectedResponse) {
|
||||
verifyGetByNameWithString(expectedResponse);
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getByNameWithCredentialName(@FromDataPoints("data-responses")
|
||||
ResponseEntity<CredentialDetailsData<ValueCredential>> expectedResponse) {
|
||||
verifyGetByNameWithCredentialName(expectedResponse);
|
||||
}
|
||||
}
|
||||
@@ -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<CredentialDetailsData<String>> successfulResponse =
|
||||
new ResponseEntity<CredentialDetailsData<String>>(
|
||||
new CredentialDetailsData<String>(
|
||||
new CredentialDetails<String>(CREDENTIAL_ID, NAME, ValueType.PASSWORD, CREDENTIAL_VALUE)),
|
||||
OK);
|
||||
|
||||
@DataPoint("responses")
|
||||
public static ResponseEntity<CredentialDetailsData<String>> httpErrorResponse =
|
||||
new ResponseEntity<CredentialDetailsData<String>>(new CredentialDetailsData<String>(), UNAUTHORIZED);
|
||||
|
||||
@Theory
|
||||
public void getByNameWithString(@FromDataPoints("responses")
|
||||
ResponseEntity<CredentialDetailsData<String>> expectedResponse) {
|
||||
ParameterizedTypeReference<CredentialDetailsData<String>> 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<CredentialDetails<String>> response = credHubTemplate.getByName(NAME.getName(), String.class);
|
||||
|
||||
assertResponseContainsExpectedCredentials(expectedResponse, response);
|
||||
}
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void getByNameWithCredentialName(@FromDataPoints("responses")
|
||||
ResponseEntity<CredentialDetailsData<String>> expectedResponse) {
|
||||
ParameterizedTypeReference<CredentialDetailsData<String>> 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<CredentialDetails<String>> response = credHubTemplate.getByName(NAME, String.class);
|
||||
|
||||
assertResponseContainsExpectedCredentials(expectedResponse, response);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertResponseContainsExpectedCredentials(
|
||||
ResponseEntity<CredentialDetailsData<String>> expectedResponse,
|
||||
List<CredentialDetails<String>> response) {
|
||||
assertThat(response, notNullValue());
|
||||
assertThat(response.size(), equalTo(expectedResponse.getBody().getData().size()));
|
||||
assertThat(response.get(0), equalTo(expectedResponse.getBody().getData().get(0)));
|
||||
}
|
||||
}
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.credhub.core;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.experimental.theories.DataPoint;
|
||||
|
||||
@@ -28,26 +28,28 @@ public class CredentialDetailsPasswordUnitTests extends JsonParsingUnitTestsBase
|
||||
|
||||
@Test
|
||||
public void deserializeDetails() throws Exception {
|
||||
CredentialDetails<String> data = parseDetails(PASSWORD_CREDENTIALS, String.class);
|
||||
CredentialDetails<PasswordCredential> data =
|
||||
parseDetails(PASSWORD_CREDENTIALS, PasswordCredential.class);
|
||||
|
||||
assertDetails(data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeDetailsData() throws Exception {
|
||||
CredentialDetailsData<String> response = parseDetailsData(PASSWORD_CREDENTIALS, String.class);
|
||||
CredentialDetailsData<PasswordCredential> response =
|
||||
parseDetailsData(PASSWORD_CREDENTIALS, PasswordCredential.class);
|
||||
|
||||
assertThat(response.getData().size(), equalTo(1));
|
||||
|
||||
CredentialDetails<String> data = response.getData().get(0);
|
||||
CredentialDetails<PasswordCredential> data = response.getData().get(0);
|
||||
|
||||
assertDetails(data);
|
||||
}
|
||||
|
||||
private void assertDetails(CredentialDetails<String> data) {
|
||||
private void assertDetails(CredentialDetails<PasswordCredential> data) {
|
||||
assertCommonDetails(data);
|
||||
|
||||
assertThat(data.getValueType(), equalTo(ValueType.PASSWORD));
|
||||
assertThat(data.getValue(), equalTo("secret"));
|
||||
assertThat(data.getValue().getPassword(), equalTo("secret"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<ValueCredential> data =
|
||||
parseDetails(VALUE_CREDENTIALS, ValueCredential.class);
|
||||
|
||||
assertDetails(data);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void deserializeDetailsData() throws Exception {
|
||||
CredentialDetailsData<ValueCredential> response =
|
||||
parseDetailsData(VALUE_CREDENTIALS, ValueCredential.class);
|
||||
|
||||
assertThat(response.getData().size(), equalTo(1));
|
||||
|
||||
CredentialDetails<ValueCredential> data = response.getData().get(0);
|
||||
|
||||
assertDetails(data);
|
||||
}
|
||||
|
||||
private void assertDetails(CredentialDetails<ValueCredential> data) {
|
||||
assertCommonDetails(data);
|
||||
|
||||
assertThat(data.getValueType(), equalTo(ValueType.VALUE));
|
||||
assertThat(data.getValue().getValue(), equalTo("somevalue"));
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
<T extends WriteRequestBuilder> String serializeToJson(T requestBuilder)
|
||||
throws JsonProcessingException {
|
||||
String jsonValue = mapper.writeValueAsString(requestBuilder.build());
|
||||
assertThat(jsonValue, isJson());
|
||||
|
||||
Reference in New Issue
Block a user