Skip to content

Commit

Permalink
Merge pull request #5 from Sqooba/moretests
Browse files Browse the repository at this point in the history
Moretests
  • Loading branch information
pietrotull authored Dec 18, 2018
2 parents 9b20cee + 0d4652a commit f471b32
Show file tree
Hide file tree
Showing 18 changed files with 233 additions and 100 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
organization := "io.sqooba"
scalaVersion := "2.12.7"
version := "0.3.5.7"
version := "0.3.7"
name := "sq-conf"

crossScalaVersions := Seq("2.12.7", "2.11.12")
Expand Down
11 changes: 3 additions & 8 deletions java-test-app/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,10 @@
<dependencies>
<dependency>
<groupId>io.sqooba</groupId>
<artifactId>sq-conf_2.11</artifactId>
<version>0.3.5.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
<artifactId>sq-conf_2.12</artifactId>
<version>0.3.6.3</version>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
4 changes: 3 additions & 1 deletion java-test-app/src/main/resources/another.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
some.int = 16
some.string = "perses"
some.intList = [15, 16, 17]
some.intList = [15, 16, 17]
some.stringList = ["string1", "string2", "string3"]
some.durationList = [10s, 10m, 10h]
4 changes: 3 additions & 1 deletion java-test-app/src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
some.int = 15
some.string = "perse"
some.boolean = false
some.intList = [15, 16, 17]
some.intList = [15, 16, 17]
some.stringList = ["string1", "string2", "string3"]
some.durationList = [10s, 10m, 10h]
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import io.sqooba.conf.JavaSqConf;
import io.sqooba.conf.SqConf;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;

Expand All @@ -22,12 +20,7 @@ public void getIntCorrectly() {
@Test
public void getIntListCorrectly() {
Iterable<Integer> someInts = conf.getIterable("some.intList");
List<Integer> ints = new ArrayList<>();
Iterator<Integer> iter = someInts.iterator();

while (iter.hasNext()) {
ints.add(iter.next());
}
List<Integer> ints = Util.fromIterToList(someInts);
assertEquals(3, ints.size());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import io.sqooba.conf.JavaSqConf;
import io.sqooba.conf.SqConf;
import java.util.HashMap;
import java.util.Map;
import java.time.Duration;
import java.util.*;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -33,4 +33,37 @@ public void useOverWrites() {
String newVal = newConf.getString("some.string");
assertEquals("some.other.string", newVal);
}

@Test
public void getIterableOfStrings() {

Iterable<String> someStrings = conf.getIterable("some.stringList");

List<String> asList = Util.fromIterToList(someStrings);

assertEquals(3, asList.size());
assertEquals(asList.get(0), "string1");
assertEquals(asList.get(1), "string2");
assertEquals(asList.get(2), "string3");
}

@Test
public void getListOfStrings() {

List<String> someStrings = conf.getListOf("some.stringList");

assertEquals(3, someStrings.size());
assertEquals(someStrings.get(0), "string1");
assertEquals(someStrings.get(1), "string2");
assertEquals(someStrings.get(2), "string3");
}

@Test
public void getListOfDuration() {

Iterable<Duration> someStrings = conf.getIterable("some.durationList");
List<Duration> asList = Util.fromIterToList(someStrings);

assertEquals(3, asList.size());
}
}
18 changes: 18 additions & 0 deletions java-test-app/src/test/java/io/sqooba/sqconf/someapp/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.sqooba.sqconf.someapp;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Util {

public static <T> List<T> fromIterToList(Iterable<T> src) {
List<T> res = new ArrayList<>();
Iterator<T> iter = src.iterator();

while (iter.hasNext()) {
res.add(iter.next());
}
return res;
}
}
17 changes: 17 additions & 0 deletions src/main/scala/io/sqooba/conf/IterToList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.sqooba.conf;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IterToList {
public static <T> List<T> fromIterToList(Iterable<T> src) {
List<T> res = new ArrayList<>();
Iterator<T> iter = src.iterator();

while (iter.hasNext()) {
res.add(iter.next());
}
return res;
}
}
4 changes: 4 additions & 0 deletions src/main/scala/io/sqooba/conf/JavaSqConf.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.sqooba.conf

import java.util

class JavaSqConf(sqConf: SqConf) {

def getSqConf: SqConf = sqConf
Expand All @@ -20,6 +22,8 @@ class JavaSqConf(sqConf: SqConf) {
list.asJava
}

def getListOf[T](key: String): util.List[T] = IterToList.fromIterToList(getIterable[T](key))

def keyAsEnv(key: String): String = sqConf.keyAsEnv(key)

def withOverwrites(ow: java.util.Map[java.lang.String, java.lang.String]): JavaSqConf = {
Expand Down
95 changes: 43 additions & 52 deletions src/main/scala/io/sqooba/conf/SqConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ package io.sqooba.conf
import java.io.File
import java.time.Duration

import scala.collection.JavaConverters._
import scala.util.Properties
import scala.collection.JavaConverters._

import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
Expand All @@ -17,15 +17,15 @@ class SqConf(fileName: String = null,
file: File = null,
config: Config = null,
prefix: String = null,
valueOverwrites: Map[String, String] = Map()) extends LazyLogging {
valueOverwrites: Map[String, String] = Map()) extends LazyLogging {

def this() = this(null, null, null, null)

def asJava() = new JavaSqConf(this)

val conf: Config = {
(fileName, file, config) match {
case (null, null, conf:Config) => conf
case (null, null, conf: Config) => conf
case (fileN: String, null, null) => ConfigFactory.load(fileN)
case (_, fileF: File, null) => ConfigFactory.parseFile(fileF)
case _ => ConfigFactory.load()
Expand All @@ -39,47 +39,36 @@ class SqConf(fileName: String = null,
}
}

def getInt(key: String): Int = {
val fullKey = buildKey(key)
if (valueOverwrites.contains(fullKey)) {
valueOverwrites(fullKey).toInt
} else {
Properties.envOrNone(keyAsEnv(fullKey)) match {
case Some(env) => env.toInt
case None => conf.getInt(fullKey)
}
}
}
def getInt(key: String): Int = getValueForKey[Int](key, x => x.toInt)

def getString(key: String): String = {
val fullKey = buildKey(key)
if (valueOverwrites.contains(fullKey)) {
valueOverwrites(fullKey)
} else {
Properties.envOrElse(keyAsEnv(fullKey), conf.getString(fullKey))
}
}
def getString(key: String): String = getValueForKey[String](key, x => x)

def getBoolean(key: String): Boolean = getValueForKey[Boolean](key, x => x.toBoolean)

def getLong(key: String): Long = getValueForKey[Long](key, x => x.toLong)

def getBoolean(key: String): Boolean = {
def getBigInt(key: String): BigInt = getValueForKey[BigInt](key, x => BigInt(x))

def getDuration(key: String): Duration = {
val fullKey = buildKey(key)
if (valueOverwrites.contains(fullKey)) {
valueOverwrites(fullKey).toBoolean
DurationParser.parseDurationString(valueOverwrites(fullKey), fullKey, "valueOverrides")
} else {
Properties.envOrNone(keyAsEnv(fullKey)) match {
case Some(env) => env.toBoolean
case None => conf.getBoolean(fullKey)
case Some(env) => DurationParser.parseDurationString(env, fullKey, "environmentVariable")
case None => conf.getDuration(fullKey)
}
}
}

def getDuration(key: String): Duration = {
def getValueForKey[T](key: String, converter: String => T): T = {
val fullKey = buildKey(key)
if (valueOverwrites.contains(fullKey)) {
DurationParser.parseDurationString(valueOverwrites(fullKey), fullKey, "valueOverrides")
converter(valueOverwrites(fullKey))
} else {
Properties.envOrNone(keyAsEnv(fullKey)) match {
case Some(env) => DurationParser.parseDurationString(env, fullKey, "environmentVariable")
case None => conf.getDuration(fullKey)
case Some(env) => converter(env)
case None => converter(conf.getString(fullKey))
}
}
}
Expand All @@ -92,28 +81,24 @@ class SqConf(fileName: String = null,
asEnvKey
}

def getListOf[T](key: String, convert: String => T, cast: Boolean): List[T] = {
val l = conf.getAnyRefList(key)
l.asScala.toList.map(x => {
if (cast) {
x.asInstanceOf[T]
} else {
convert(x.toString)
}
})
}

def getListOf[T](key: String): List[T] = {
val l = conf.getAnyRefList(key)
l.toArray.map(x => {
x.asInstanceOf[T]
}).toList
}

def getListOfWithConversion[T](key: String, convert: String => T): List[T] = {
val fullKey = buildKey(key)

def stringToT(string: String): List[T] = string.split(',').map(convert).toList

if (valueOverwrites.contains(fullKey)) {
stringToT(valueOverwrites(fullKey))
} else {
Properties.envOrNone(keyAsEnv(fullKey)) match {
case Some(env) => stringToT(env)
case None => getListOf[T](fullKey)
}
}
}

def getListOfInt(key: String): List[Int] = getListOfWithConversion(key, str => str.trim.toInt)

def getListOfDouble(key: String): List[Double] = getListOfWithConversion(key, str => str.trim.toDouble)
Expand All @@ -122,19 +107,25 @@ class SqConf(fileName: String = null,

def getListOfBoolean(key: String): List[Boolean] = getListOfWithConversion(key, str => str.trim.toBoolean)

def getListOfDuration(key: String): List[Duration] = {
def getListOfDuration(key: String): List[Duration] = getListOfWithConversion[Duration](key, str =>
DurationParser.parseDurationString(str, key, "listOfDuration"), cast = false)

def getListOfWithConversion[T](key: String, convert: String => T, cast: Boolean = true): List[T] = {
val fullKey = buildKey(key)
if (valueOverwrites.contains(fullKey) || Properties.envOrNone(keyAsEnv(fullKey)).isDefined) {
getListOfWithConversion(key, str => {
DurationParser.parseDurationString(str, key, "listOfDuration")
})

def stringToT(string: String): List[T] = string.split(',').map(x => convert(x)).toList

if (valueOverwrites.contains(fullKey)) {
stringToT(valueOverwrites(fullKey))
} else {
conf.getDurationList(fullKey).asScala.toList
Properties.envOrNone(keyAsEnv(fullKey)) match {
case Some(env) => stringToT(env)
case None => getListOf[T](fullKey, convert, cast)
}
}
}

def getConfig(confPath: String): SqConf = {
// new SqConf(fileName, Some(confPath))
new SqConf(null, null, config, confPath)
}

Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ some {
testStringValue = "string thing"
testIntValue = 187
testBooleanValue = true
testLongValue = 100
testBigIntValue = 123456789
testStringListValue = ["some","list", "last"]
testIntListValue = [100, 200, 300]
testBooleanListValue = [true, false, true]
testIntListValue = [123, 23, 69]
testDurationValue = 10m
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/prop.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
testIntVal=15
testStringVal=perse
testBooleanVal=false
7 changes: 7 additions & 0 deletions src/test/scala/io/sqooba/conf/EnvOverwritesSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,11 @@ class EnvOverwritesSpec extends FlatSpec with Matchers {
durationArray(2) shouldBe Duration.ofHours(1)
EnvUtil.removeEnv(conf.keyAsEnv("some.testDurationListValue"))
}

"reading key that is not in the conf but is env variable" should "work just as well" in {
val testKey = "this.key.does.not.exist"
val testVal = "this_is_test_val"
EnvUtil.setEnv(conf.keyAsEnv(testKey), testVal)
conf.getString(testKey) shouldBe testVal
}
}
12 changes: 12 additions & 0 deletions src/test/scala/io/sqooba/conf/GenGetterSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.sqooba.conf

import java.time.Duration

import org.scalatest.{FlatSpec, Matchers}

class GenGetterSpec extends FlatSpec with Matchers {

val conf: SqConf = new SqConf


}
9 changes: 9 additions & 0 deletions src/test/scala/io/sqooba/conf/JavaWrapperSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ class JavaWrapperSpec extends FlatSpec with Matchers {
stringy shouldBe a [java.lang.String]
stringy shouldBe "string thing"
}

"get list of ints" should "return valid java list" in {
// testIntListValue = [123, 23, 69]
val listOf = javaWrapper.getListOf[Integer]("some.testIntListValue")
listOf.size() shouldBe 3
listOf.get(0) shouldBe 123
listOf.get(1) shouldBe 23
listOf.get(2) shouldBe 69
}
}
Loading

0 comments on commit f471b32

Please sign in to comment.