Skip to content

Commit

Permalink
Added ConfigMapping test to Yaml Source.
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez committed Nov 26, 2020
1 parent 98f1fac commit ec3a252
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import org.eclipse.microprofile.config.spi.ConfigSource;
import org.junit.jupiter.api.Test;

public class ArrayTest {

class ArrayTest {
@Test
public void array() {
void array() {
String yaml = "de:\n" +
" javahippie:\n" +
" mpadmin:\n" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -15,10 +16,11 @@
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.Yaml;

import io.smallrye.config.ConfigMapping;
import io.smallrye.config.SmallRyeConfig;
import io.smallrye.config.SmallRyeConfigBuilder;

public class YamlConfigSourceTest {
class YamlConfigSourceTest {
@Test
void flatten() throws Exception {
YamlConfigSource yaml = new YamlConfigSource("yaml",
Expand Down Expand Up @@ -82,7 +84,6 @@ void config() throws Exception {
void propertyNames() throws Exception {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(new YamlConfigSource("yaml", YamlConfigSourceTest.class.getResourceAsStream("/example.yml")))
.withConverter(Users.class, 100, new UserConverter())
.build();

final List<String> propertyNames = StreamSupport.stream(config.getPropertyNames().spliterator(), false)
Expand All @@ -102,7 +103,6 @@ void quotedProperties() throws Exception {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(
new YamlConfigSource("yaml", YamlConfigSourceTest.class.getResourceAsStream("/example-quotes.yml")))
.withConverter(Users.class, 100, new UserConverter())
.build();

final List<String> propertyNames = StreamSupport.stream(config.getPropertyNames().spliterator(), false)
Expand All @@ -127,13 +127,29 @@ void commas() throws Exception {
}

@Test
void intKeys() throws Exception {
void intKeys() {
try {
new SmallRyeConfigBuilder()
.withSources(new YamlConfigSource("yaml",
YamlConfigSourceTest.class.getResourceAsStream("/example-int-keys.yml")))
.build();
} catch (Exception e) {
fail(e);
}
}

@Test
void configMapping() throws Exception {
SmallRyeConfig config = new SmallRyeConfigBuilder()
.withSources(
new YamlConfigSource("yaml", YamlConfigSourceTest.class.getResourceAsStream("/example-int-keys.yml")))
.withSources(new YamlConfigSource("yaml", YamlConfigSourceTest.class.getResourceAsStream("/example-216.yml")))
.withConverter(Users.class, 100, new UserConverter())
.withMapping(UsersMapping.class, "admin")
.build();

UsersMapping usersMapping = config.getConfigMapping(UsersMapping.class);
assertEquals(2, usersMapping.users().getUsers().size());
assertEquals(usersMapping.users().users.get(0).getEmail(), "[email protected]");
assertEquals(usersMapping.users().users.get(0).getRoles(), Stream.of("Moderator", "Admin").collect(toList()));
}

public static class Users {
Expand Down Expand Up @@ -193,4 +209,9 @@ public Users convert(final String value) {
return new Yaml().loadAs(value, Users.class);
}
}

@ConfigMapping(prefix = "admin")
interface UsersMapping {
Users users();
}
}

0 comments on commit ec3a252

Please sign in to comment.