-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #161 from yvasyliev/160-bug-messageevent-doesnt-su…
…pport-plain-string-in-payload-probable-fix-provided Fixed #160
- Loading branch information
Showing
4 changed files
with
98 additions
and
3 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
80 changes: 80 additions & 0 deletions
80
src/test/java/api/longpoll/bots/adapters/deserializers/PayloadDeserializerTest.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,80 @@ | ||
package api.longpoll.bots.adapters.deserializers; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonPrimitive; | ||
import com.google.gson.annotations.JsonAdapter; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class PayloadDeserializerTest { | ||
Gson gson = new Gson(); | ||
|
||
@Test | ||
void parseBooleanTest() { | ||
String json = "{\"payload\":true}"; | ||
|
||
JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload; | ||
assertTrue(payload.isJsonPrimitive()); | ||
|
||
JsonPrimitive jsonPrimitive = payload.getAsJsonPrimitive(); | ||
assertTrue(jsonPrimitive.isBoolean()); | ||
assertTrue(jsonPrimitive.getAsBoolean()); | ||
} | ||
|
||
@Test | ||
void parseStringTest() { | ||
String json = "{\"payload\":\"Hello World!\"}"; | ||
|
||
JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload; | ||
assertTrue(payload.isJsonPrimitive()); | ||
|
||
JsonPrimitive jsonPrimitive = payload.getAsJsonPrimitive(); | ||
assertTrue(jsonPrimitive.isString()); | ||
assertEquals("Hello World!", jsonPrimitive.getAsString()); | ||
} | ||
|
||
@Test | ||
void parseJsonTest() { | ||
String json = "{\"payload\":{\"foo\":\"bar\"}}"; | ||
|
||
JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload; | ||
assertTrue(payload.isJsonObject()); | ||
|
||
JsonObject jsonObject = payload.getAsJsonObject(); | ||
assertTrue(jsonObject.has("foo")); | ||
|
||
JsonElement foo = jsonObject.get("foo"); | ||
assertTrue(foo.isJsonPrimitive()); | ||
|
||
JsonPrimitive jsonPrimitive = foo.getAsJsonPrimitive(); | ||
assertTrue(jsonPrimitive.isString()); | ||
assertEquals("bar", jsonPrimitive.getAsString()); | ||
} | ||
|
||
@Test | ||
void parseStringJsonTest() { | ||
String json = "{\"payload\":\"{\\\"foo\\\":\\\"bar\\\"}\"}"; | ||
|
||
JsonElement payload = gson.fromJson(json, PayloadHolder.class).payload; | ||
assertTrue(payload.isJsonObject()); | ||
|
||
JsonObject jsonObject = payload.getAsJsonObject(); | ||
assertTrue(jsonObject.has("foo")); | ||
|
||
JsonElement foo = jsonObject.get("foo"); | ||
assertTrue(foo.isJsonPrimitive()); | ||
|
||
JsonPrimitive jsonPrimitive = foo.getAsJsonPrimitive(); | ||
assertTrue(jsonPrimitive.isString()); | ||
assertEquals("bar", jsonPrimitive.getAsString()); | ||
} | ||
|
||
static class PayloadHolder { | ||
@JsonAdapter(PayloadDeserializer.class) | ||
JsonElement payload; | ||
} | ||
} |