|
| 1 | +/* |
| 2 | + * Copyright 2012-2020 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.boot.context.config; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import org.springframework.boot.diagnostics.FailureAnalysis; |
| 22 | +import org.springframework.boot.origin.Origin; |
| 23 | + |
| 24 | +import static org.assertj.core.api.Assertions.assertThat; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests for {@link ConfigDataNotFoundFailureAnalyzer} |
| 28 | + * |
| 29 | + * @author Michal Mlak |
| 30 | + * @author Phillip Webb |
| 31 | + */ |
| 32 | +class ConfigDataNotFoundFailureAnalyzerTests { |
| 33 | + |
| 34 | + private final ConfigDataNotFoundFailureAnalyzer analyzer = new ConfigDataNotFoundFailureAnalyzer(); |
| 35 | + |
| 36 | + @Test |
| 37 | + void analyzeWhenConfigDataLocationNotFoundException() { |
| 38 | + ConfigDataLocation location = ConfigDataLocation.of("test"); |
| 39 | + ConfigDataLocationNotFoundException exception = new ConfigDataLocationNotFoundException(location); |
| 40 | + FailureAnalysis result = this.analyzer.analyze(exception); |
| 41 | + assertThat(result.getDescription()).isEqualTo("Config data location 'test' does not exist"); |
| 42 | + assertThat(result.getAction()) |
| 43 | + .isEqualTo("Check that the value 'test' is correct, or prefix it with 'optional:'"); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void analyzeWhenOptionalConfigDataLocationNotFoundException() { |
| 48 | + ConfigDataLocation location = ConfigDataLocation.of("optional:test"); |
| 49 | + ConfigDataLocationNotFoundException exception = new ConfigDataLocationNotFoundException(location); |
| 50 | + FailureAnalysis result = this.analyzer.analyze(exception); |
| 51 | + assertThat(result.getDescription()).isEqualTo("Config data location 'optional:test' does not exist"); |
| 52 | + assertThat(result.getAction()).isEqualTo("Check that the value 'optional:test' is correct"); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void analyzeWhenConfigDataLocationWithOriginNotFoundException() { |
| 57 | + ConfigDataLocation location = ConfigDataLocation.of("test").withOrigin(new TestOrigin("origin")); |
| 58 | + ConfigDataLocationNotFoundException exception = new ConfigDataLocationNotFoundException(location); |
| 59 | + FailureAnalysis result = this.analyzer.analyze(exception); |
| 60 | + assertThat(result.getDescription()).isEqualTo("Config data location 'test' does not exist"); |
| 61 | + assertThat(result.getAction()) |
| 62 | + .isEqualTo("Check that the value 'test' at origin is correct, or prefix it with 'optional:'"); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void analyzeWhenConfigDataResourceNotFoundException() { |
| 67 | + ConfigDataResource resource = new TestConfigDataResource("myresource"); |
| 68 | + ConfigDataResourceNotFoundException exception = new ConfigDataResourceNotFoundException(resource); |
| 69 | + FailureAnalysis result = this.analyzer.analyze(exception); |
| 70 | + assertThat(result.getDescription()).isEqualTo("Config data resource 'myresource' does not exist"); |
| 71 | + assertThat(result.getAction()).isEqualTo("Check that the value is correct"); |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + void analyzeWhenConfigDataResourceWithLocationNotFoundException() { |
| 76 | + ConfigDataLocation location = ConfigDataLocation.of("test"); |
| 77 | + ConfigDataResource resource = new TestConfigDataResource("myresource"); |
| 78 | + ConfigDataResourceNotFoundException exception = new ConfigDataResourceNotFoundException(resource) |
| 79 | + .withLocation(location); |
| 80 | + FailureAnalysis result = this.analyzer.analyze(exception); |
| 81 | + assertThat(result.getDescription()) |
| 82 | + .isEqualTo("Config data resource 'myresource' via location 'test' does not exist"); |
| 83 | + assertThat(result.getAction()) |
| 84 | + .isEqualTo("Check that the value 'test' is correct, or prefix it with 'optional:'"); |
| 85 | + } |
| 86 | + |
| 87 | + static class TestOrigin implements Origin { |
| 88 | + |
| 89 | + private final String string; |
| 90 | + |
| 91 | + TestOrigin(String string) { |
| 92 | + this.string = string; |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public String toString() { |
| 97 | + return this.string; |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + |
| 102 | + static class TestConfigDataResource extends ConfigDataResource { |
| 103 | + |
| 104 | + private final String string; |
| 105 | + |
| 106 | + TestConfigDataResource(String string) { |
| 107 | + this.string = string; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String toString() { |
| 112 | + return this.string; |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | +} |
0 commit comments