|
| 1 | +/* |
| 2 | + * Copyright 2013-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.cloud.consul.config; |
| 18 | + |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.LinkedHashMap; |
| 21 | + |
| 22 | +import com.ecwid.consul.v1.ConsulClient; |
| 23 | +import com.ecwid.consul.v1.QueryParams; |
| 24 | +import com.ecwid.consul.v1.Response; |
| 25 | +import com.ecwid.consul.v1.kv.model.GetValue; |
| 26 | +import org.apache.commons.logging.LogFactory; |
| 27 | +import org.junit.Test; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | +import static org.mockito.ArgumentMatchers.any; |
| 31 | +import static org.mockito.ArgumentMatchers.eq; |
| 32 | +import static org.mockito.ArgumentMatchers.nullable; |
| 33 | +import static org.mockito.Mockito.mock; |
| 34 | +import static org.mockito.Mockito.when; |
| 35 | + |
| 36 | +/** |
| 37 | + * Tests for {@link ConsulPropertySources}. |
| 38 | + */ |
| 39 | +public class ConsulPropertySourcesTests { |
| 40 | + |
| 41 | + /** |
| 42 | + * When using {@code spring.config.import=consul:/test}, the path has a leading slash |
| 43 | + * which must be normalized before storing in the watch index map, otherwise |
| 44 | + * ConfigWatch sends requests to Consul with a double-slash path (e.g. |
| 45 | + * {@code //test/}) and receives a 301 redirect instead of the expected JSON. |
| 46 | + */ |
| 47 | + @Test |
| 48 | + public void createPropertySourceStoresNormalizedContextInIndex() { |
| 49 | + ConsulClient consul = mock(ConsulClient.class); |
| 50 | + Response<java.util.List<GetValue>> response = new Response<>(Collections.emptyList(), 1L, false, 1L); |
| 51 | + // The mock must answer for the normalized path "test/" (no leading slash) |
| 52 | + when(consul.getKVValues(eq("test/"), nullable(String.class), any(QueryParams.class))).thenReturn(response); |
| 53 | + |
| 54 | + ConsulConfigProperties properties = new ConsulConfigProperties(); |
| 55 | + ConsulPropertySources sources = new ConsulPropertySources(properties, |
| 56 | + LogFactory.getLog(ConsulPropertySourcesTests.class)); |
| 57 | + |
| 58 | + LinkedHashMap<String, Long> indexes = new LinkedHashMap<>(); |
| 59 | + // Context with leading slash as produced by ConsulConfigDataLocationResolver |
| 60 | + // when parsing consul:/test |
| 61 | + sources.createPropertySource("/test/", consul, indexes::put); |
| 62 | + |
| 63 | + assertThat(indexes).as("index must be stored under the normalized path (no leading slash)") |
| 64 | + .containsKey("test/"); |
| 65 | + assertThat(indexes).as("index must NOT be stored under the leading-slash path").doesNotContainKey("/test/"); |
| 66 | + } |
| 67 | + |
| 68 | +} |
0 commit comments