Skip to content

Commit

Permalink
Merge branch 'pr/1'
Browse files Browse the repository at this point in the history
  • Loading branch information
ikonkere committed Nov 27, 2019
2 parents 14f16c6 + 9621821 commit 97b3993
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 159 deletions.
25 changes: 12 additions & 13 deletions src/main/java/com/kumuluz/ee/config/microprofile/ConfigImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* out of or in connection with the software or the use or other dealings in the
* software. See the License for the specific language governing permissions and
* limitations under the License.
*/
*/
package com.kumuluz.ee.config.microprofile;

import java.io.Serializable;
Expand Down Expand Up @@ -50,11 +50,10 @@
public class ConfigImpl implements Config, Serializable {
private static final Logger logger = Logger.getLogger(ConfigImpl.class.getCanonicalName());

private static final String ARRAY_SEPARATOR_REGEX = "(?<!\\\\)" + Pattern.quote(",");
private Map<Type, Converter> converters;
private List<ConfigSource> configSources;

private static final String ARRAY_SEPARATOR_REGEX = "(?<!\\\\)" + Pattern.quote(",");

public ConfigImpl(List<ConfigSource> configSources, Map<Type, Converter> converters) {
this.configSources = configSources;
this.converters = converters;
Expand Down Expand Up @@ -111,7 +110,7 @@ public <T> T convert(String value, Class<T> asType) {
Array.set(arr, i, a.get(i));
}

return (T)arr;
return (T) arr;
}

Converter<T> converter = getConverter(asType);
Expand Down Expand Up @@ -152,13 +151,13 @@ private <T> Converter<T> getConverter(Class asType) {
return converter;
}

@Override
public Iterable<String> getPropertyNames() {
return this.configSources.stream().flatMap(e -> e.getPropertyNames().stream()).collect(Collectors.toSet());
}
@Override
public Iterable<String> getPropertyNames() {
return this.configSources.stream().flatMap(e -> e.getPropertyNames().stream()).collect(Collectors.toSet());
}

@Override
public Iterable<ConfigSource> getConfigSources() {
return this.configSources;
}
}
@Override
public Iterable<ConfigSource> getConfigSources() {
return this.configSources;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,18 +59,18 @@ public int getOrdinal() {

@Override
public String getValue(String s) {
String val = configurationSource.get(s).orElse(null);
if (val != null) {
Optional<Integer> listSize = this.configurationSource.getListSize(s);
//this is a list or an array
if (listSize.isPresent()) {
//we ignore the returned value and build the array
return buildArray(s, listSize.get());
}
}
String val = configurationSource.get(s).orElse(null);

if (val != null) {
Optional<Integer> listSize = this.configurationSource.getListSize(s);

//this is a list or an array
if (listSize.isPresent()) {
//we ignore the returned value and build the array
return buildArray(s, listSize.get());
}
}

return val;
}

Expand Down Expand Up @@ -106,28 +106,23 @@ private void buildPropertiesMap(Map<String, String> map, String prefix) {
}
}
}

private String buildArray(String propertyName, int size) {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < size; i++) {
String prefix = String.format("%s[%d]", propertyName, i);
Optional<List<String>> objectKeys = this.configurationSource.getMapKeys(prefix);

//array item is an object, so we just omit it
if (objectKeys.isPresent()) {
sb.append("");
}
else {
Optional<String> item = this.configurationSource.get(String.format("%s[%d]", propertyName, i));
item.ifPresent(sb::append);
}

if (i < size - 1) {
sb.append(',');
}
}

return sb.toString();
}

private String buildArray(String propertyName, int size) {
StringBuilder sb = new StringBuilder();

for (int i = 0; i < size; i++) {
String prefix = String.format("%s[%d]", propertyName, i);
Optional<List<String>> objectKeys = this.configurationSource.getMapKeys(prefix);

if (!objectKeys.isPresent()) {
Optional<String> item = this.configurationSource.get(String.format("%s[%d]", propertyName, i));
if (i > 0) {
sb.append(',');
}
item.ifPresent(sb::append);
} // else array item is an object, so we just omit it
}

return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,101 +1,100 @@
/*
* Copyright (c) 2014-2017 Kumuluz and/or its affiliates
* and other contributors as indicated by the @author tags and
* the contributor list.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* The software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. in no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the software or the use or other dealings in the
* software. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kumuluz.ee.config.microprofile.tests;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import javax.inject.Inject;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
* Tests that check MP Config array/list injection from KumuluzEE's config.yaml
*
* @author Yog Sothoth
* @since 1.4
*
*/
@Test
public class ArrayInjectionTest extends Arquillian {
@Deployment
public static JavaArchive deploy() {
JavaArchive testJar = ShrinkWrap
.create(JavaArchive.class, "arrayInjectionTest.jar")
.addClasses(ArrayInjectionTest.class)
.addAsResource("config.yaml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.as(JavaArchive.class);
return testJar;
}

@Inject
@ConfigProperty(name="parameter.arrayParameter")
private List<String> listParam;

@Inject
@ConfigProperty(name="parameter.arrayParameter")
private String[] arrayParam;

@Inject
@ConfigProperty(name="parameter.arrayParameter")
private Optional<String[]> arrayParamOpt;

@Inject
@ConfigProperty(name="parameter.stringParameter")
private Optional<String> stringParam;

@Inject
@ConfigProperty(name="parameter.arrayParameter[0]")
private String arrayItemParam;

private static final String[] TEST_TARGET =
new String[]{"one ", " two", " [three, four] "};

@Test
public void testNotParsingDelimeters() {
Assert.assertEquals("[here be, dragons]", stringParam.get());
}

@Test
public void testArrayItemInjection() {
Assert.assertEquals(TEST_TARGET[0], arrayItemParam);
}

@Test
public void testArrayInjection() {
Assert.assertEquals(TEST_TARGET, arrayParamOpt.get());
Assert.assertEquals(TEST_TARGET, arrayParam);
}

@Test
public void testListInjection() {
Assert.assertEquals(Arrays.asList(TEST_TARGET), listParam);
}
}
/*
* Copyright (c) 2014-2017 Kumuluz and/or its affiliates
* and other contributors as indicated by the @author tags and
* the contributor list.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* The software is provided "AS IS", WITHOUT WARRANTY OF ANY KIND, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. in no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the software or the use or other dealings in the
* software. See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.kumuluz.ee.config.microprofile.tests;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.testng.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.testng.Assert;
import org.testng.annotations.Test;

import javax.inject.Inject;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
* Tests that check MP Config array/list injection from KumuluzEE's config.yaml
*
* @author Yog Sothoth
* @since 1.4
*/
@Test
public class ArrayInjectionTest extends Arquillian {

private static final String[] TEST_TARGET =
new String[]{"one ", " two", " [three, four] "};

@Inject
@ConfigProperty(name = "parameter.arrayParameter")
private List<String> listParam;

@Inject
@ConfigProperty(name = "parameter.arrayParameter")
private String[] arrayParam;

@Inject
@ConfigProperty(name = "parameter.arrayParameter")
private Optional<String[]> arrayParamOpt;

@Inject
@ConfigProperty(name = "parameter.stringParameter")
private Optional<String> stringParam;

@Inject
@ConfigProperty(name = "parameter.arrayParameter[0]")
private String arrayItemParam;

@Deployment
public static JavaArchive deploy() {
JavaArchive testJar = ShrinkWrap
.create(JavaArchive.class, "arrayInjectionTest.jar")
.addClasses(ArrayInjectionTest.class)
.addAsResource("config.yaml")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml")
.as(JavaArchive.class);
return testJar;
}

@Test
public void testNotParsingDelimeters() {
Assert.assertEquals("[here be, dragons]", stringParam.get());
}

@Test
public void testArrayItemInjection() {
Assert.assertEquals(TEST_TARGET[0], arrayItemParam);
}

@Test
public void testArrayInjection() {
Assert.assertEquals(TEST_TARGET, arrayParamOpt.get());
Assert.assertEquals(TEST_TARGET, arrayParam);
}

@Test
public void testListInjection() {
Assert.assertEquals(Arrays.asList(TEST_TARGET), listParam);
}
}
18 changes: 9 additions & 9 deletions src/test/resources/config.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
parameter:
stringParameter: "[here be, dragons]"
arrayParameter:
- "one "
- " two"
- " [three\\, four] "
- five:
six: seven
eight: nine
parameter:
stringParameter: "[here be, dragons]"
arrayParameter:
- "one "
- " two"
- " [three\\, four] "
- five:
six: seven
eight: nine

0 comments on commit 97b3993

Please sign in to comment.