|
| 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 | +import java.util.List; |
| 22 | + |
| 23 | +import com.ecwid.consul.v1.ConsulClient; |
| 24 | +import com.ecwid.consul.v1.QueryParams; |
| 25 | +import com.ecwid.consul.v1.Response; |
| 26 | +import com.ecwid.consul.v1.kv.model.GetValue; |
| 27 | +import org.apache.commons.logging.LogFactory; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | +import static org.mockito.ArgumentMatchers.any; |
| 32 | +import static org.mockito.ArgumentMatchers.eq; |
| 33 | +import static org.mockito.ArgumentMatchers.nullable; |
| 34 | +import static org.mockito.Mockito.mock; |
| 35 | +import static org.mockito.Mockito.when; |
| 36 | + |
| 37 | +/** |
| 38 | + * Tests for {@link ConsulPropertySources}. |
| 39 | + */ |
| 40 | +public class ConsulPropertySourcesTests { |
| 41 | + |
| 42 | + /** |
| 43 | + * When using {@code spring.config.import=consul:/test}, the path has a leading slash |
| 44 | + * which must be normalized before storing in the watch index map, otherwise |
| 45 | + * ConfigWatch sends requests to Consul with a double-slash path (e.g. |
| 46 | + * {@code //test/}) and receives a 301 redirect instead of the expected JSON. |
| 47 | + */ |
| 48 | + @Test |
| 49 | + public void createPropertySourceStoresNormalizedContextInIndex() { |
| 50 | + ConsulClient consul = mock(ConsulClient.class); |
| 51 | + Response<List<GetValue>> response = new Response<>(Collections.emptyList(), 1L, false, 1L); |
| 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 | + sources.createPropertySource("/test/", consul, indexes::put); |
| 60 | + |
| 61 | + assertThat(indexes).containsKey("test/"); |
| 62 | + assertThat(indexes).doesNotContainKey("/test/"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void createPropertySourceFilesFormatStoresNormalizedContextInIndex() { |
| 67 | + ConsulClient consul = mock(ConsulClient.class); |
| 68 | + Response<GetValue> response = new Response<>(null, 1L, false, 1L); |
| 69 | + when(consul.getKVValue(eq("test.yml"), nullable(String.class))).thenReturn(response); |
| 70 | + |
| 71 | + ConsulConfigProperties properties = new ConsulConfigProperties(); |
| 72 | + properties.setFormat(ConsulConfigProperties.Format.FILES); |
| 73 | + ConsulPropertySources sources = new ConsulPropertySources(properties, |
| 74 | + LogFactory.getLog(ConsulPropertySourcesTests.class)); |
| 75 | + |
| 76 | + LinkedHashMap<String, Long> indexes = new LinkedHashMap<>(); |
| 77 | + sources.createPropertySource("/test.yml", consul, indexes::put); |
| 78 | + |
| 79 | + assertThat(indexes).containsKey("test.yml"); |
| 80 | + assertThat(indexes).doesNotContainKey("/test.yml"); |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments