Skip to content

Commit

Permalink
Fix/networknt#425 status merge (networknt#426)
Browse files Browse the repository at this point in the history
* - overload getConfigStream method in config module to get config stream from classpath or default

* - fixed status merge
- added test

* Update test cases

* Update test cases
  • Loading branch information
jiachen1120 authored and stevehu committed Mar 15, 2019
1 parent cdc29d8 commit d29f696
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
3 changes: 2 additions & 1 deletion server/src/main/java/com/networknt/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,8 @@ protected static void mergeStatusConfig() {
return;
}
Map<String, Object> statusConfig = Config.getInstance().getJsonMapConfig(STATUS_CONFIG_NAME[0]);
Set<String> duplicatedStatusSet = statusConfig.keySet();
// clone the default status config key set
Set<String> duplicatedStatusSet = new HashSet<>(statusConfig.keySet());
duplicatedStatusSet.retainAll(appStatusConfig.keySet());
if (!duplicatedStatusSet.isEmpty()) {
logger.error("The status code(s): " + duplicatedStatusSet.toString() + " is already in use by light-4j and cannot be overwritten," +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@
import com.networknt.status.Status;
import junit.framework.TestCase;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestMergeStatusConfig extends TestCase {

private Config config = null;
Expand Down Expand Up @@ -43,9 +46,15 @@ public void tearDown() throws Exception {
@Test
public void testAppStatus() {
config.clear();
// test default element without merging with app-status
Status status0 = new Status("ERR10053");
Assert.assertEquals(401, status0.getStatusCode());
Server.mergeStatusConfig();
Status status = new Status("ERR99999");
Assert.assertEquals(404, status.getStatusCode());
// test default element after merging
Status status1 = new Status("ERR10053");
Assert.assertEquals(401, status1.getStatusCode());
}

@Test
Expand All @@ -61,6 +70,35 @@ public void testDuplicateStatus() {
}
}

@Test
public void testWithoutAppStatus() {
config.clear();
File appStatus = new File(homeDir + "/app-status.yml");
appStatus.delete();
// test default element without merging with app-status
Status status0 = new Status("ERR10053");
Assert.assertEquals(401, status0.getStatusCode());
Server.mergeStatusConfig();
// test default element after merging
Status status1 = new Status("ERR10053");
Assert.assertEquals(401, status1.getStatusCode());
}

@Test
public void testEmptyAppStatus() {
config.clear();
File appStatus = new File(homeDir + "/app-status.yml");
appStatus.delete();
new File(homeDir + "/app-status.yml");
// test default element without merging with app-status
Status status0 = new Status("ERR10053");
Assert.assertEquals(401, status0.getStatusCode());
Server.mergeStatusConfig();
// test default element after merging
Status status1 = new Status("ERR10053");
Assert.assertEquals(401, status1.getStatusCode());
}

private void setExternalizedConfigDir(String externalizedDir) throws Exception {
Field f1 = config.getClass().getDeclaredField("EXTERNALIZED_PROPERTY_DIR");
f1.setAccessible(true);
Expand Down

0 comments on commit d29f696

Please sign in to comment.