Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ben221199 committed May 19, 2024
1 parent f2493c1 commit c174a98
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
<version>20240303</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.9.2</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/yocto/yoclib/jsonrpc/JSONRPCException.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@

public class JSONRPCException extends Exception{

public JSONRPCException(String message){
super(message);
}

}
36 changes: 35 additions & 1 deletion src/main/java/com/yocto/yoclib/jsonrpc/Message.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.yocto.yoclib.jsonrpc;

import org.json.JSONObject;
import org.json.*;

public abstract class Message{

Expand Down Expand Up @@ -61,4 +61,38 @@ public JSONObject toObject(){
return this.value;
}

/**
*
* @param object The object
* @return JSON string
* @throws JSONRPCException An exception
*/
public static String encodeJSON(Object object) throws JSONRPCException {
try{
if(object instanceof JSONObject){
return ((JSONObject) object).toString(0);
}
if(object instanceof JSONArray){
return ((JSONArray) object).toString(0);
}
return JSONObject.valueToString(object);
}catch(JSONException e){
throw new JSONRPCException("Failed to encode JSON.");
}
}

/**
*
* @param json JSON string
* @return The object
* @throws JSONRPCException An exception
*/
public static Object decodeJSON(String json) throws JSONRPCException {
try{
return new JSONTokener(json).nextValue();
}catch(JSONException $e){
throw new JSONRPCException("Failed to decode JSON.");
}
}

}
20 changes: 18 additions & 2 deletions src/test/java/com/yocto/yoclib/jsonrpc/tests/MessageTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,23 @@
package com.yocto.yoclib.jsonrpc.tests;

import junit.framework.TestCase;
import com.yocto.yoclib.jsonrpc.JSONRPCException;
import com.yocto.yoclib.jsonrpc.Message;

public class MessageTest extends TestCase{
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class MessageTest{

@Test
public void testDecodeEmptyJSON(){
Throwable t = assertThrows(JSONRPCException.class,() -> Message.decodeJSON(""));

assertEquals("Failed to decode JSON.",t.getMessage());
}

@Test
public void testDecodeJSONString() throws JSONRPCException {
assertEquals("abc",Message.decodeJSON("\"abc\""));
}

}

0 comments on commit c174a98

Please sign in to comment.