-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a better documentation to the GsonUtils class that makes clear …
…which method expects the length of a String value and which doesn't. removed the error details property since it is not needed. updated the error reporting classes accordingly.
- Loading branch information
1 parent
76aeabd
commit 46c4444
Showing
4 changed files
with
100 additions
and
57 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,74 @@ | ||
package org.hobbit.core.rabbit; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.hobbit.core.data.ErrorData; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import org.junit.runners.Parameterized.Parameters; | ||
|
||
import com.google.gson.Gson; | ||
|
||
@RunWith(Parameterized.class) | ||
public class GsonUtilsTest { | ||
|
||
@Test | ||
public void testErrorData() { | ||
ErrorData original = new ErrorData(); | ||
original.setErrorType("http://example.org/generalError"); | ||
original.setContainerId("123"); | ||
protected static Gson gson = new Gson(); | ||
|
||
protected Object original; | ||
protected Class<?> clazz; | ||
|
||
Gson gson = new Gson(); | ||
ErrorData received; | ||
public GsonUtilsTest(Object original, Class<?> clazz) { | ||
super(); | ||
this.original = original; | ||
this.clazz = clazz; | ||
} | ||
|
||
received = GsonUtils.deserializeObjectWithGson(gson, GsonUtils.serializeObjectWithGson(gson, original), | ||
ErrorData.class); | ||
@Test | ||
public void testDirect() { | ||
Object received = GsonUtils.deserializeObjectWithGson(gson, GsonUtils.serializeObjectWithGson(gson, original), | ||
ErrorData.class, false); | ||
Assert.assertEquals(original, received); | ||
} | ||
|
||
original.setLabel("Example label"); | ||
received = GsonUtils.deserializeObjectWithGson(gson, GsonUtils.serializeObjectWithGson(gson, original), | ||
ErrorData.class); | ||
@Test | ||
public void testWithLengthPrefix() { | ||
byte[] data = GsonUtils.serializeObjectWithGson(gson, original); | ||
ByteBuffer buffer = ByteBuffer.allocate(data.length + 4); | ||
buffer.putInt(data.length); | ||
buffer.put(data); | ||
|
||
Object received = GsonUtils.deserializeObjectWithGson(gson, buffer.array(), | ||
ErrorData.class, true); | ||
Assert.assertEquals(original, received); | ||
} | ||
|
||
@Parameters | ||
public static List<Object[]> parameters() { | ||
List<Object[]> testCases = new ArrayList<>(); | ||
ErrorData original; | ||
|
||
original = new ErrorData(); | ||
original.setErrorType("http://example.org/generalError"); | ||
original.setContainerId("123"); | ||
testCases.add(new Object[] {original, original.getClass()}); | ||
|
||
original = new ErrorData(); | ||
original.setErrorType("http://example.org/generalError"); | ||
original.setContainerId("123"); | ||
original.setLabel("Example label"); | ||
testCases.add(new Object[] {original, original.getClass()}); | ||
|
||
original = new ErrorData(); | ||
original.setErrorType("http://example.org/generalError"); | ||
original.setContainerId("123"); | ||
original.setLabel("Example label"); | ||
original.setDescription("Some description"); | ||
received = GsonUtils.deserializeObjectWithGson(gson, GsonUtils.serializeObjectWithGson(gson, original), | ||
ErrorData.class); | ||
Assert.assertEquals(original, received); | ||
testCases.add(new Object[] {original, original.getClass()}); | ||
|
||
original.setDetails("A lot of details...."); | ||
received = GsonUtils.deserializeObjectWithGson(gson, GsonUtils.serializeObjectWithGson(gson, original), | ||
ErrorData.class); | ||
Assert.assertEquals(original, received); | ||
return testCases; | ||
} | ||
} |