Merge pull request #9641 from azhwani/yamlft
BAEL-4320: Mapping list in Yaml to list of objects in Spring Boot
This commit is contained in:
commit
e65e8ffab3
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.properties.yamllist;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class YamlListApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(YamlListApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package com.baeldung.properties.yamllist.pojo;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "application")
|
||||
public class ApplicationProps {
|
||||
|
||||
private List<Object> profiles;
|
||||
private List<Map<String, Object>> props;
|
||||
private List<User> users;
|
||||
|
||||
public List<Object> getProfiles() {
|
||||
return profiles;
|
||||
}
|
||||
|
||||
public void setProfiles(List<Object> profiles) {
|
||||
this.profiles = profiles;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getProps() {
|
||||
return props;
|
||||
}
|
||||
|
||||
public void setProps(List<Map<String, Object>> props) {
|
||||
this.props = props;
|
||||
}
|
||||
|
||||
public List<User> getUsers() {
|
||||
return users;
|
||||
}
|
||||
|
||||
public void setUsers(List<User> users) {
|
||||
this.users = users;
|
||||
}
|
||||
|
||||
public static class User {
|
||||
|
||||
private String username;
|
||||
private String password;
|
||||
private List<String> roles;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public List<String> getRoles() {
|
||||
return roles;
|
||||
}
|
||||
|
||||
public void setRoles(List<String> roles) {
|
||||
this.roles = roles;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -20,3 +20,36 @@ server:
|
|||
guest:
|
||||
username: guest
|
||||
password: guestpass
|
||||
---
|
||||
application:
|
||||
profiles:
|
||||
- dev
|
||||
- test
|
||||
- prod
|
||||
- 1
|
||||
- 2
|
||||
props:
|
||||
-
|
||||
name: YamlList
|
||||
url: http://yamllist.dev
|
||||
description: Mapping list in Yaml to list of objects in Spring Boot
|
||||
-
|
||||
ip: 10.10.10.10
|
||||
port: 8091
|
||||
-
|
||||
email: support@yamllist.dev
|
||||
contact: http://yamllist.dev/contact
|
||||
users:
|
||||
-
|
||||
username: admin
|
||||
password: admin@10@
|
||||
roles:
|
||||
- READ
|
||||
- WRITE
|
||||
- VIEW
|
||||
- DELETE
|
||||
-
|
||||
username: guest
|
||||
password: guest@01
|
||||
roles:
|
||||
- VIEW
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.properties.yamllist;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import com.baeldung.properties.yamllist.pojo.ApplicationProps;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
|
||||
@EnableConfigurationProperties(value = ApplicationProps.class)
|
||||
class YamlComplexListsUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ApplicationProps applicationProps;
|
||||
|
||||
@Test
|
||||
public void whenYamlNestedLists_thenLoadComplexLists() {
|
||||
assertThat(applicationProps.getUsers()
|
||||
.get(0)
|
||||
.getPassword()).isEqualTo("admin@10@");
|
||||
assertThat(applicationProps.getProps()
|
||||
.get(0)
|
||||
.get("name")).isEqualTo("YamlList");
|
||||
assertThat(applicationProps.getProps()
|
||||
.get(1)
|
||||
.get("port")
|
||||
.getClass()).isEqualTo(Integer.class);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
package com.baeldung.properties.yamllist;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.test.context.ConfigFileApplicationContextInitializer;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import com.baeldung.properties.yamllist.pojo.ApplicationProps;
|
||||
|
||||
@ExtendWith(SpringExtension.class)
|
||||
@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)
|
||||
@EnableConfigurationProperties(value = ApplicationProps.class)
|
||||
class YamlSimpleListUnitTest {
|
||||
|
||||
@Autowired
|
||||
private ApplicationProps applicationProps;
|
||||
|
||||
@Test
|
||||
public void whenYamlList_thenLoadSimpleList() {
|
||||
assertThat(applicationProps.getProfiles()
|
||||
.get(0)).isEqualTo("dev");
|
||||
assertThat(applicationProps.getProfiles()
|
||||
.get(4)
|
||||
.getClass()).isEqualTo(Integer.class);
|
||||
assertThat(applicationProps.getProfiles()
|
||||
.size()).isEqualTo(5);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue