Search in sources :

Example 36 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class MapBinderTests method bindToMapNonScalarCollectionWithDotKeysShouldBind.

@Test
void bindToMapNonScalarCollectionWithDotKeysShouldBind() {
    Bindable<List<JavaBean>> valueType = Bindable.listOf(JavaBean.class);
    Bindable<Map<String, List<JavaBean>>> target = getMapBindable(String.class, valueType.getType());
    MockConfigurationPropertySource mockSource = new MockConfigurationPropertySource();
    mockSource.put("foo.bar.baz[0].value", "a");
    mockSource.put("foo.bar.baz[1].value", "b");
    mockSource.put("foo.bar.baz[2].value", "c");
    this.sources.add(mockSource);
    Map<String, List<JavaBean>> map = this.binder.bind("foo", target).get();
    List<String> values = map.get("bar.baz").stream().map(JavaBean::getValue).collect(Collectors.toList());
    assertThat(values).containsExactly("a", "b", "c");
}
Also used : JavaBean(org.springframework.boot.context.properties.bind.BinderTests.JavaBean) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 37 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class MapBinderTests method bindToMapShouldBeGreedyForScalars.

@Test
void bindToMapShouldBeGreedyForScalars() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.aaa.bbb.ccc", "foo-bar");
    source.put("foo.bbb.ccc.ddd", "BAR_BAZ");
    source.put("foo.ccc.ddd.eee", "bazboo");
    this.sources.add(source);
    Map<String, ExampleEnum> result = this.binder.bind("foo", Bindable.mapOf(String.class, ExampleEnum.class)).get();
    assertThat(result).hasSize(3);
    assertThat(result).containsEntry("aaa.bbb.ccc", ExampleEnum.FOO_BAR);
    assertThat(result).containsEntry("bbb.ccc.ddd", ExampleEnum.BAR_BAZ);
    assertThat(result).containsEntry("ccc.ddd.eee", ExampleEnum.BAZ_BOO);
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) ExampleEnum(org.springframework.boot.context.properties.bind.BinderTests.ExampleEnum) Test(org.junit.jupiter.api.Test)

Example 38 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class MapBinderTests method bindToMapShouldTriggerOnSuccess.

@Test
void bindToMapShouldTriggerOnSuccess() {
    this.sources.add(new MockConfigurationPropertySource("foo.bar", "1", "line1"));
    BindHandler handler = mock(BindHandler.class, Answers.CALLS_REAL_METHODS);
    Bindable<Map<String, Integer>> target = STRING_INTEGER_MAP;
    this.binder.bind("foo", target, handler);
    InOrder ordered = inOrder(handler);
    ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo.bar")), eq(Bindable.of(Integer.class)), any(), eq(1));
    ordered.verify(handler).onSuccess(eq(ConfigurationPropertyName.of("foo")), eq(target), any(), isA(Map.class));
}
Also used : InOrder(org.mockito.InOrder) MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.jupiter.api.Test)

Example 39 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class MapBinderTests method nestedMapsShouldNotBindToNull.

@Test
void nestedMapsShouldNotBindToNull() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.value", "one");
    source.put("foo.foos.foo1.value", "two");
    source.put("foo.foos.foo2.value", "three");
    this.sources.add(source);
    BindResult<NestableFoo> foo = this.binder.bind("foo", NestableFoo.class);
    assertThat(foo.get().getValue()).isNotNull();
    assertThat(foo.get().getFoos().get("foo1").getValue()).isEqualTo("two");
    assertThat(foo.get().getFoos().get("foo2").getValue()).isEqualTo("three");
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Example 40 with MockConfigurationPropertySource

use of org.springframework.boot.context.properties.source.MockConfigurationPropertySource in project spring-boot by spring-projects.

the class MapBinderTests method bindToMapWithNoDefaultConstructor.

@Test
void bindToMapWithNoDefaultConstructor() {
    MockConfigurationPropertySource source = new MockConfigurationPropertySource();
    source.put("foo.items.a", "b");
    this.sources.add(source);
    ExampleCustomNoDefaultConstructorBean result = this.binder.bind("foo", ExampleCustomNoDefaultConstructorBean.class).get();
    assertThat(result.getItems()).containsOnly(entry("foo", "bar"), entry("a", "b"));
}
Also used : MockConfigurationPropertySource(org.springframework.boot.context.properties.source.MockConfigurationPropertySource) Test(org.junit.jupiter.api.Test)

Aggregations

MockConfigurationPropertySource (org.springframework.boot.context.properties.source.MockConfigurationPropertySource)207 Test (org.junit.jupiter.api.Test)204 Map (java.util.Map)18 HashMap (java.util.HashMap)17 LinkedHashMap (java.util.LinkedHashMap)17 ArrayList (java.util.ArrayList)16 List (java.util.List)16 Binder (org.springframework.boot.context.properties.bind.Binder)15 InOrder (org.mockito.InOrder)8 LinkedList (java.util.LinkedList)7 ResolvableType (org.springframework.core.ResolvableType)7 ValidationBindHandler (org.springframework.boot.context.properties.bind.validation.ValidationBindHandler)5 ConfigurationProperty (org.springframework.boot.context.properties.source.ConfigurationProperty)5 StandardEnvironment (org.springframework.core.env.StandardEnvironment)5 ConfigurationPropertyName (org.springframework.boot.context.properties.source.ConfigurationPropertyName)4 LocalDate (java.time.LocalDate)3 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)3 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)3 BeforeEach (org.junit.jupiter.api.BeforeEach)3 BindException (org.springframework.boot.context.properties.bind.BindException)3