-
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.
* refactor of core vs send/receive packages * create classes for receiving webhook events (limited functionality) * increase test coverage to 92% (cobertura)
- Loading branch information
Showing
13 changed files
with
382 additions
and
35 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
27 changes: 27 additions & 0 deletions
27
src/main/java/net/hawry/messaging/core/send/MetadataMessage.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,27 @@ | ||
package net.hawry.messaging.core.send; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
import net.hawry.messaging.core.Content; | ||
|
||
public class MetadataMessage extends Content { | ||
@SerializedName("metadata") String metadata; | ||
|
||
/** | ||
* Sets the optional metadata string. A character limit of 1000 characters. The string will be concatenated to the first 1000 characters if this limit is exceeded. | ||
* | ||
* @param metadata the metadata to set | ||
*/ | ||
public void setMetadata(String metadata) { | ||
if (metadata.length() > 1000) | ||
metadata = metadata.substring(0, 1000); | ||
this.metadata = metadata; | ||
} | ||
|
||
/** | ||
* @return the metadata string | ||
*/ | ||
public String getMetadata() { | ||
return this.metadata; | ||
} | ||
} |
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,36 @@ | ||
package net.hawry.messaging.core.webhook; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class Entry { | ||
@SerializedName("id") String id; | ||
@SerializedName("time") long time; | ||
@SerializedName("messaging") Messaging[] messaging; | ||
|
||
/** | ||
* @return page id of page | ||
*/ | ||
public String getId() { | ||
return this.id; | ||
} | ||
|
||
/** | ||
* @return time of update (epoch in ms) | ||
*/ | ||
public long getTime() { | ||
return this.time; | ||
} | ||
|
||
/** | ||
* @return message if exists, null otherwise | ||
*/ | ||
public Messaging getMessaging() { | ||
if (this.messaging == null) | ||
return null; | ||
|
||
if (!(this.messaging.length>0)) | ||
return null; | ||
|
||
return this.messaging[0]; // there should ever only be one messaging object here according to the spec | ||
} | ||
} |
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,16 @@ | ||
package net.hawry.messaging.core.webhook; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
public class Event { | ||
@SerializedName("object") String object; | ||
@SerializedName("entry") Entry[] entry; | ||
|
||
public String getObject() { | ||
return this.object; | ||
} | ||
|
||
public Entry[] getEntries() { | ||
return this.entry; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/net/hawry/messaging/core/webhook/Messaging.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,33 @@ | ||
package net.hawry.messaging.core.webhook; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
import net.hawry.messaging.core.Participant; | ||
|
||
public class Messaging { | ||
@SerializedName("sender") Participant sender; | ||
@SerializedName("recipient") Participant recipient; | ||
@SerializedName("timestamp") long timestamp; | ||
@SerializedName("message") WebhookMessage message; | ||
@SerializedName("seq") int seq; // part of examples, but not in the spec - unsure if it's something that will be received | ||
|
||
public Participant getSender() { | ||
return this.sender; | ||
} | ||
|
||
public Participant getRecipient() { | ||
return this.recipient; | ||
} | ||
|
||
public long getTimestamp() { | ||
return this.timestamp; | ||
} | ||
|
||
public WebhookMessage getMessage() { | ||
return this.message; | ||
} | ||
|
||
public int getSeq() { | ||
return this.seq; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/net/hawry/messaging/core/webhook/WebhookMessage.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,16 @@ | ||
package net.hawry.messaging.core.webhook; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
|
||
import net.hawry.messaging.core.Content; | ||
|
||
public class WebhookMessage extends Content { | ||
@SerializedName("mid") String mid; | ||
|
||
/** | ||
* @return message id string | ||
*/ | ||
public String getMessageId() { | ||
return this.mid; | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
import static org.junit.Assert.assertNull; | ||
import com.google.gson.Gson; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import net.hawry.messaging.core.webhook.Entry; | ||
|
||
public class EntryTest { | ||
static Gson g = null; | ||
@BeforeClass | ||
public static void setUpBeforeClass() { | ||
g = new Gson(); | ||
} | ||
|
||
@Test | ||
public void testDeSerialize() { | ||
String json = "{\"id\":\"123456\",\"time\":1530472304539,\"messaging\":[{\"sender\":{\"id\":\"123\"},\"recipient\":{\"id\":\"456\"},\"timestamp\":1530472304539,\"message\":{\"mid\":\"mid.1457764197618:41d102a3e1ae206a38\",\"text\":\"hello, world\"}}]}"; | ||
Entry e = g.fromJson(json,Entry.class); | ||
|
||
assertEquals("123456", e.getId()); | ||
assertEquals(1530472304539L, e.getTime()); | ||
assertNotNull(e.getMessaging()); | ||
} | ||
|
||
@Test | ||
public void testDeSerialize_MessagingNull() { | ||
String json = "{\"id\":\"123456\",\"time\":1530472304539}"; | ||
Entry e = g.fromJson(json,Entry.class); | ||
|
||
assertEquals("123456", e.getId()); | ||
assertEquals(1530472304539L, e.getTime()); | ||
assertNull(e.getMessaging()); | ||
} | ||
|
||
@Test | ||
public void testDeSerialize_MessagingEmpty() { | ||
String json = "{\"id\":\"123456\",\"time\":1530472304539,\"messaging\":[]}"; | ||
Entry e = g.fromJson(json,Entry.class); | ||
|
||
assertEquals("123456", e.getId()); | ||
assertEquals(1530472304539L, e.getTime()); | ||
assertNull(e.getMessaging()); | ||
} | ||
} |
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,29 @@ | ||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertNotNull; | ||
|
||
import com.google.gson.Gson; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import net.hawry.messaging.core.webhook.Event; | ||
|
||
public class EventTest { | ||
static Gson g = null; | ||
@BeforeClass | ||
public static void setUpBeforeClass() { | ||
g = new Gson(); | ||
} | ||
|
||
@Test | ||
public void testSimpleSerialization() { | ||
String entry = "{\"id\":\"123456\",\"time\":1530472304539,\"messaging\":[{\"sender\":{\"id\":\"123\"},\"recipient\":{\"id\":\"456\"},\"timestamp\":1530472304539,\"message\":{\"mid\":\"mid.1457764197618:41d102a3e1ae206a38\",\"text\":\"hello, world\"}}]}"; | ||
String raw = "{\"object\":\"page\",\"entry\": [%s]}"; | ||
|
||
String json = String.format(raw, entry); | ||
|
||
Event e = g.fromJson(json, Event.class); | ||
assertEquals("page", e.getObject()); | ||
assertNotNull(e.getEntries()); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import com.google.gson.Gson; | ||
import static org.junit.Assert.*; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import net.hawry.messaging.core.webhook.Messaging; | ||
|
||
public class MessagingTest { | ||
static Gson g = null; | ||
@BeforeClass | ||
public static void setUpBeforeClass() { | ||
g = new Gson(); | ||
} | ||
|
||
@Test | ||
public void testDeSerialization() { | ||
String json = "{'sender':{'id':'123'},'recipient':{'id':'456'},'timestamp':1530472304539,'seq':12345, 'message':{}}"; | ||
Messaging m = g.fromJson(json, Messaging.class); | ||
assertEquals("123", m.getSender().getId()); | ||
assertEquals("456", m.getRecipient().getId()); | ||
assertEquals(1530472304539L, m.getTimestamp()); | ||
assertNotNull(m.getMessage()); | ||
assertEquals(12345, m.getSeq()); | ||
} | ||
} |
Oops, something went wrong.