From 78a3d07b48c6a1b54c5be06c92ec33520b3792b3 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Tue, 25 Apr 2017 15:37:43 +0200 Subject: [PATCH] Consider configured meta attributes in VaultTokenRequestBuilder. Closes gh-80. --- .../vault/support/VaultTokenRequest.java | 8 +-- .../support/VaultTokenRequestUnitTests.java | 57 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 spring-vault-core/src/test/java/org/springframework/vault/support/VaultTokenRequestUnitTests.java diff --git a/spring-vault-core/src/main/java/org/springframework/vault/support/VaultTokenRequest.java b/spring-vault-core/src/main/java/org/springframework/vault/support/VaultTokenRequest.java index 337fad36..468cf388 100644 --- a/spring-vault-core/src/main/java/org/springframework/vault/support/VaultTokenRequest.java +++ b/spring-vault-core/src/main/java/org/springframework/vault/support/VaultTokenRequest.java @@ -1,5 +1,5 @@ /* - * Copyright 2016 the original author or authors. + * 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. @@ -28,7 +28,7 @@ import org.springframework.util.Assert; /** * Value object to bind Vault HTTP Token API requests. - * + * * @author Mark Paluch */ public class VaultTokenRequest { @@ -418,14 +418,14 @@ public class VaultTokenRequest { this.policies)); } - Map meta = null; + Map meta; switch (this.meta.size()) { case 0: meta = Collections.emptyMap(); break; default: meta = Collections - .unmodifiableMap(new LinkedHashMap(meta)); + .unmodifiableMap(new LinkedHashMap(this.meta)); } return new VaultTokenRequest(id, policies, meta, noParent, noDefaultPolicy, diff --git a/spring-vault-core/src/test/java/org/springframework/vault/support/VaultTokenRequestUnitTests.java b/spring-vault-core/src/test/java/org/springframework/vault/support/VaultTokenRequestUnitTests.java new file mode 100644 index 00000000..d083e813 --- /dev/null +++ b/spring-vault-core/src/test/java/org/springframework/vault/support/VaultTokenRequestUnitTests.java @@ -0,0 +1,57 @@ +/* + * Copyright 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.vault.support; + +import java.util.Collections; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Unit tests for {@link VaultTokenRequest}. + * + * @author Mark Paluch + */ +public class VaultTokenRequestUnitTests { + + @Test + public void shouldBuildEmptyRequest() { + + VaultTokenRequest tokenRequest = VaultTokenRequest.builder().build(); + + assertThat(tokenRequest.getMeta()).isEmpty(); + assertThat(tokenRequest.getPolicies()).isEmpty(); + } + + @Test + public void shouldBuildRequestWithMeta() { + + VaultTokenRequest tokenRequest = VaultTokenRequest.builder() + .meta(Collections.singletonMap("key", "value")).build(); + + assertThat(tokenRequest.getMeta()).containsEntry("key", "value"); + } + + @Test + public void shouldBuildRequestWithPolicies() { + + VaultTokenRequest tokenRequest = VaultTokenRequest.builder().withPolicy("foo") + .build(); + + assertThat(tokenRequest.getPolicies()).containsOnly("foo"); + } +}