forked from networknt/light-4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
status/src/main/java/com/networknt/status/StatusWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.networknt.status; | ||
|
||
import io.undertow.server.HttpServerExchange; | ||
|
||
/** | ||
* Interface to allow custom Status and then inject the customized status into framework through SingletonFactory | ||
* <p> | ||
* Implement the interface and configure in service.yml | ||
* - com.networknt.status.StatusWrapper | ||
* - custom implementation | ||
* | ||
* @author Jiachen Sun | ||
*/ | ||
|
||
public interface StatusWrapper { | ||
/** | ||
* Encapsulate the default status of the framework as a custom status | ||
* | ||
* @param status The status to be wrapped | ||
* @param exchange The source of custom info used to wrap status | ||
* @return the custom status | ||
*/ | ||
Status wrap(Status status, HttpServerExchange exchange); | ||
} |
66 changes: 66 additions & 0 deletions
66
status/src/test/java/com/networknt/status/StatusWrapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.networknt.status; | ||
|
||
import com.networknt.config.Config; | ||
import com.networknt.service.SingletonServiceFactory; | ||
import org.junit.AfterClass; | ||
import org.junit.Assert; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@RunWith(SeparateClassloaderTestRunner.class) | ||
public class StatusWrapperTest { | ||
private static Config config = null; | ||
private static final String homeDir = System.getProperty("user.home"); | ||
|
||
@BeforeClass | ||
public static void setUp() throws Exception { | ||
config = Config.getInstance(); | ||
|
||
// write a config file into the user home directory. | ||
List<String> implementationList = new ArrayList<>(); | ||
implementationList.add("com.networknt.status.TestStatusWrapper"); | ||
Map<String, List<String>> implementationMap = new HashMap<>(); | ||
implementationMap.put("com.networknt.status.StatusWrapper", implementationList); | ||
List<Map<String, List<String>>> interfaceList = new ArrayList<>(); | ||
interfaceList.add(implementationMap); | ||
Map<String, Object> singletons = new HashMap<>(); | ||
singletons.put("singletons", interfaceList); | ||
config.getMapper().writeValue(new File(homeDir + "/service.json"), singletons); | ||
|
||
// Add home directory to the classpath of the system class loader. | ||
AppURLClassLoader classLoader = new AppURLClassLoader(new URL[0], ClassLoader.getSystemClassLoader()); | ||
classLoader.addURL(new File(homeDir).toURI().toURL()); | ||
config.setClassLoader(classLoader); | ||
} | ||
|
||
@AfterClass | ||
public static void tearDown() { | ||
// Remove the service.yml from home directory | ||
File test = new File(homeDir + "/service.json"); | ||
test.delete(); | ||
} | ||
|
||
@Test | ||
public void testStatusWrap() { | ||
StatusWrapper statusWrapper = SingletonServiceFactory.getBean(StatusWrapper.class); | ||
Status status = new Status("ERR10001"); | ||
status = statusWrapper.wrap(status, null); | ||
Assert.assertEquals("{\"error\":{\"statusCode\":401,\"code\":\"ERR10001\",\"message\":\"AUTH_TOKEN_EXPIRED\",\"description\":\"Jwt token in authorization header expired\",\"customInfo\":\"custom_info\",\"severity\":\"ERROR\"}}", status.toString()); | ||
} | ||
|
||
@Test | ||
public void testStatusWrapWithArgs() { | ||
StatusWrapper statusWrapper = SingletonServiceFactory.getBean(StatusWrapper.class); | ||
Status status = new Status("ERR11000","arg1", "arg2"); | ||
status = statusWrapper.wrap(status, null); | ||
Assert.assertEquals("{\"error\":{\"statusCode\":400,\"code\":\"ERR11000\",\"message\":\"VALIDATOR_REQUEST_PARAMETER_QUERY_MISSING\",\"description\":\"Query parameter arg1 is required on path arg2 but not found in request.\",\"customInfo\":\"custom_info\",\"severity\":\"ERROR\"}}", status.toString()); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
status/src/test/java/com/networknt/status/TestStatusWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.networknt.status; | ||
|
||
import io.undertow.server.HttpServerExchange; | ||
|
||
public class TestStatusWrapper implements StatusWrapper { | ||
@Override | ||
public Status wrap(Status status, HttpServerExchange exchange) { | ||
return new TestStatus(status, exchange); | ||
} | ||
|
||
private class TestStatus extends Status { | ||
private String customInfo; | ||
|
||
public TestStatus(Status status, HttpServerExchange exchange) { | ||
this.setStatusCode(status.getStatusCode()); | ||
this.setCode(status.getCode()); | ||
this.setDescription(status.getDescription()); | ||
this.setMessage(status.getMessage()); | ||
this.setSeverity(status.getSeverity()); | ||
this.setCustomInfo("custom_info"); | ||
} | ||
|
||
public String getCustomInfo() { | ||
return customInfo; | ||
} | ||
|
||
public void setCustomInfo(String customInfo) { | ||
this.customInfo = customInfo; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
String message = "{\"error\":" + "{\"statusCode\":" + getStatusCode() | ||
+ ",\"code\":\"" + getCode() | ||
+ "\",\"message\":\"" + getMessage() | ||
+ "\",\"description\":\"" + getDescription() | ||
+ "\",\"customInfo\":\"" + getCustomInfo() | ||
+ "\",\"severity\":\"" + getSeverity() + "\"}" + "}"; | ||
return message; | ||
} | ||
} | ||
} |