Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APP-2884 #175

Merged
merged 6 commits into from
Aug 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.symphony.bdk.core.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.symphony.bdk.core.config.legacy.LegacyConfigMapper;
import com.symphony.bdk.core.config.legacy.model.LegacySymConfig;
import com.symphony.bdk.core.config.model.BdkConfig;
import com.symphony.bdk.core.exceptions.BdkConfigException;
Expand All @@ -27,7 +27,7 @@ public class BdkConfigLoader {
*
* @return Symphony Bot Configuration
*/
public static BdkConfig loadFromFile(String configPath) throws JsonProcessingException, BdkConfigException {
public static BdkConfig loadFromFile(String configPath) throws BdkConfigException {
try {
File file = new File(configPath);
InputStream inputStream = new FileInputStream(file);
Expand All @@ -44,16 +44,16 @@ public static BdkConfig loadFromFile(String configPath) throws JsonProcessingExc
*
* @return Symphony Bot Configuration
*/
public static BdkConfig loadFromInputStream(InputStream inputStream) throws JsonProcessingException, BdkConfigException {
public static BdkConfig loadFromInputStream(InputStream inputStream) throws BdkConfigException {
if (inputStream != null) {
JsonNode jsonNode = BdkConfigParser.parse(inputStream);
if (jsonNode != null) {
JSON_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
if (jsonNode.at("botUsername") != null) {
LegacySymConfig legacySymConfig = JSON_MAPPER.treeToValue(jsonNode, LegacySymConfig.class);
return LegacyConfigMapper.map(legacySymConfig);
if (jsonNode.at("/botUsername").isMissingNode()) {
return JSON_MAPPER.convertValue(jsonNode, BdkConfig.class);
} else {
return JSON_MAPPER.treeToValue(jsonNode, BdkConfig.class);
LegacySymConfig legacySymConfig = JSON_MAPPER.convertValue(jsonNode, LegacySymConfig.class);
return LegacyConfigMapper.map(legacySymConfig);
}
}
}
Expand All @@ -67,7 +67,7 @@ public static BdkConfig loadFromInputStream(InputStream inputStream) throws Json
*
* @return Symphony Bot Configuration
*/
public static BdkConfig loadFromClasspath(String configPath) throws JsonProcessingException, BdkConfigException {
public static BdkConfig loadFromClasspath(String configPath) throws BdkConfigException {
InputStream inputStream = BdkConfigLoader.class.getResourceAsStream(configPath);
if (inputStream != null) {
return loadFromInputStream(inputStream);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import com.symphony.bdk.core.exceptions.BdkConfigException;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.stream.Collectors;

@Slf4j
class BdkConfigParser {
Expand All @@ -20,14 +24,18 @@ class BdkConfigParser {
public static JsonNode parse(InputStream inputStream) throws BdkConfigException {
JSON_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
YAML_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String content = new BufferedReader(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

since we have commons io as a dependency we could use IOUtils.toString(inputstream)

new InputStreamReader(inputStream, StandardCharsets.UTF_8))
.lines()
.collect(Collectors.joining("\n"));
try {
return JSON_MAPPER.readTree(inputStream);
return JSON_MAPPER.readTree(content);
} catch (IOException e) {
log.debug("Config file is not in JSON format");
}

try {
return YAML_MAPPER.readTree(inputStream);
return YAML_MAPPER.readTree(content);
} catch (IOException e) {
log.debug("Config file is not in YAML format");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.symphony.bdk.core.config;
package com.symphony.bdk.core.config.legacy;

import com.symphony.bdk.core.config.legacy.model.LegacySymConfig;
import com.symphony.bdk.core.config.model.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.symphony.bdk.core.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.symphony.bdk.core.config.model.BdkConfig;
import com.symphony.bdk.core.exceptions.BdkConfigException;
import org.junit.jupiter.api.Test;

import java.io.InputStream;

import static org.junit.jupiter.api.Assertions.*;

public class BdkConfigLoaderTest {

@Test
public void loadFromYamlInputStreamTest() throws JsonProcessingException, BdkConfigException {
InputStream inputStream = BdkConfigLoaderTest.class.getResourceAsStream("/config.yaml");
BdkConfig config = BdkConfigLoader.loadFromInputStream(inputStream);
assertEquals(config.getBot().getUsername(), "tibot");
}

@Test
public void loadFromJsonInputStreamTest() throws JsonProcessingException, BdkConfigException {
InputStream inputStream = BdkConfigLoaderTest.class.getResourceAsStream("/config.json");
BdkConfig config = BdkConfigLoader.loadFromInputStream(inputStream);
assertEquals(config.getBot().getUsername(), "tibot");
}

@Test
public void loadFromYamlFileTest() throws JsonProcessingException, BdkConfigException {
String configPath = System.getProperty("user.dir") + "/src/test/resources/config.yaml";
BdkConfig config = BdkConfigLoader.loadFromFile(configPath);
assertEquals(config.getBot().getUsername(), "tibot");
}

@Test
public void loadFromJsonFileTest() throws JsonProcessingException, BdkConfigException {
String configPath = System.getProperty("user.dir") + "/src/test/resources/config.json";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work in IDEA? not sure the default current folder is the module's folder

BdkConfig config = BdkConfigLoader.loadFromFile(configPath);
assertEquals(config.getBot().getUsername(), "tibot");
}

@Test
public void loadFromJsonClasspathTest() throws JsonProcessingException, BdkConfigException {
BdkConfig config = BdkConfigLoader.loadFromClasspath("/config.json");
assertEquals(config.getBot().getUsername(), "tibot");
}

@Test
public void loadFromYamlClasspathTest() throws JsonProcessingException, BdkConfigException {
BdkConfig config = BdkConfigLoader.loadFromClasspath("/config.yaml");
assertEquals(config.getBot().getUsername(), "tibot");
}
}
21 changes: 21 additions & 0 deletions symphony-bdk-core/src/test/resources/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"_version": "2.0",

"pod": {
"host": "devx1.symphony.com"
},
"agent": {
"host": "devx1.symphony.com"
},
"keyManager": {
"host": "devx1.symphony.com"
},
"bot": {
"username": "tibot",
"privateKeyPath": "/Users/thibault.pensec/local/conf/agent/privatekey.pem"
},
"app": {
"appId": "tibapp",
"privateKeyPath": "/Users/thibault.pensec/local/conf/agent/privatekey.pem"
}
}
18 changes: 18 additions & 0 deletions symphony-bdk-core/src/test/resources/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
_version: '2.0'

pod:
host: devx1.symphony.com

agent:
host: devx1.symphony.com

keyManager:
host: devx1.symphony.com

bot:
username: tibot
privateKeyPath: /Users/thibault.pensec/local/conf/agent/privatekey.pem

app:
appId: tibapp
privateKeyPath: /Users/thibault.pensec/local/conf/agent/privatekey.pem