Skip to content

Commit

Permalink
Rc 0.2.0 (#10)
Browse files Browse the repository at this point in the history
* refactor of core vs send/receive packages
* create classes for receiving webhook events (limited functionality)
* increase test coverage to 92% (cobertura)
  • Loading branch information
hawry authored Jul 1, 2018
1 parent 9f86444 commit 8b70f20
Show file tree
Hide file tree
Showing 13 changed files with 382 additions and 35 deletions.
26 changes: 3 additions & 23 deletions src/main/java/net/hawry/messaging/core/Content.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
import net.hawry.messaging.exceptions.MissingRequiredFieldException;

public class Content {
@SerializedName("text") String text;
// @SerializedName("attachment") Attachment attachment;
// @SerializedName("quick_replies") QuickReply[] quickReplies;
@SerializedName("metadata") String metadata;
@SerializedName("text") protected String text;
// @SerializedName("attachment") protected Attachment attachment;
// @SerializedName("quick_replies") protected QuickReply[] quickReplies;

/**
* Sets the text content of the message. Must be UTF-8 and has a 2000 character limit. The text will be concatenated to the first 2000 characters if the length is exceeded.
Expand All @@ -28,25 +27,6 @@ public String getText() {
return this.text;
}

/**
* 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;
}

void validate() throws MissingRequiredFieldException {
if (this.text==null)
throw new MissingRequiredFieldException("text");
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/net/hawry/messaging/core/send/MetadataMessage.java
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;
}
}
36 changes: 36 additions & 0 deletions src/main/java/net/hawry/messaging/core/webhook/Entry.java
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
}
}
16 changes: 16 additions & 0 deletions src/main/java/net/hawry/messaging/core/webhook/Event.java
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 src/main/java/net/hawry/messaging/core/webhook/Messaging.java
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 src/main/java/net/hawry/messaging/core/webhook/WebhookMessage.java
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;
}
}
24 changes: 21 additions & 3 deletions src/test/java/ContentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import org.junit.Test;

import net.hawry.messaging.core.Content;
import net.hawry.messaging.core.send.MetadataMessage;
import net.hawry.messaging.core.webhook.WebhookMessage;

public class ContentTest {
static Gson g = null;
Expand Down Expand Up @@ -39,7 +41,7 @@ public void textMaxLimit_IsEnforced() {

@Test
public void metadataMaxLimit_IsEnforced() {
Content c = new Content();
MetadataMessage c = new MetadataMessage();
c.setMetadata(longString);
assertTrue(c.getMetadata().length() == 1000);
assertEquals(longString.subSequence(0, 1000), c.getMetadata());
Expand All @@ -48,7 +50,7 @@ public void metadataMaxLimit_IsEnforced() {

@Test
public void testSetData() {
Content c = new Content();
MetadataMessage c = new MetadataMessage();
c.setText("hello world");
c.setMetadata("hello metadata");

Expand All @@ -58,7 +60,7 @@ public void testSetData() {

@Test
public void testSerialization() {
Content c = new Content();
MetadataMessage c = new MetadataMessage();
c.setText("hello text");
c.setMetadata("hello metadata");

Expand All @@ -67,4 +69,20 @@ public void testSerialization() {
assertTrue(json.contains("\"text\":\"hello text\""));
assertTrue(json.contains("\"metadata\":\"hello metadata\""));
}

@Test
public void testWebhookMessage() {
WebhookMessage wh = new WebhookMessage();
wh.setText("hello webhook");
String json = g.toJson(wh);
assertTrue(json.contains("\"text\":\"hello webhook\""));
}

@Test
public void testDeserialization_Webhook() {
String json = "{\"mid\":\"mid.1457764197618:41d102a3e1ae206a38\",\"text\":\"hello, world!\"}";
WebhookMessage wh = g.fromJson(json, WebhookMessage.class);
assertEquals("mid.1457764197618:41d102a3e1ae206a38", wh.getMessageId());
assertEquals("hello, world!", wh.getText());
}
}
47 changes: 47 additions & 0 deletions src/test/java/EntryTest.java
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());
}
}
29 changes: 29 additions & 0 deletions src/test/java/EventTest.java
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());
}
}
28 changes: 20 additions & 8 deletions src/test/java/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public void testSerialization() {
m.setContent(new Content());

String json = g.toJson(m);
System.out.println(json);

assertTrue(json.contains("\"messaging_type\":\"RESPONSE\""));
assertTrue(json.contains("\"recipient\":{\"id\":\"456\"}"));
Expand All @@ -53,15 +52,28 @@ public void testDeserialization() {
assertNotNull(m.getContent()); // content is tested in separate file
}

public void testOwnSerialization() {
@Test
public void testToJson() {
Content c = new Content();
c.setText("hello content");

Message m = new Message();
m.setContent(new Content());
m.setSender(new Participant("123"));
m.setRecipient(new Participant("1235"));
m.setRecipient(new Participant("123"));
m.setMessageType(MessageType.RESPONSE);


m.setContent(c);

String json;
try {
System.out.println(m.toJson());
} catch (MissingRequiredFieldException e) {
e.printStackTrace();
json = m.toJson();
} catch (MissingRequiredFieldException ex) {
json = "";
ex.printStackTrace();
}
System.out.println(json);
assertTrue(json.contains("\"recipient\":{\"id\":\"123\"}"));
assertTrue(json.contains("\"messaging_type\":\"RESPONSE\""));
assertTrue(json.contains("\"message\":{\"text\":\"hello content\"}"));
}
}
26 changes: 26 additions & 0 deletions src/test/java/MessagingTest.java
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());
}
}
Loading

0 comments on commit 8b70f20

Please sign in to comment.