Skip to content

Commit aa17143

Browse files
authored
Merge pull request #140 from intercom/MM/get_conv_parts_recursion
don't refetch conversation parts if list is empty
2 parents 16badb0 + bdc2d29 commit aa17143

File tree

6 files changed

+271
-2
lines changed

6 files changed

+271
-2
lines changed

build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ subprojects {
5050
compile 'org.slf4j:slf4j-api:1.7.7'
5151
compile 'commons-codec:commons-codec:1.9'
5252
testCompile 'junit:junit:4.11'
53+
testCompile 'org.powermock:powermock-module-junit4:1.6.6'
54+
testCompile 'org.powermock:powermock-api-mockito:1.6.6'
5355
}
5456
}
5557

intercom-java/src/main/java/io/intercom/api/Conversation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,8 +265,8 @@ public long getUpdatedAt() {
265265
}
266266

267267
public ConversationPartCollection getConversationPartCollection() {
268-
if (conversationPartCollection == null || conversationPartCollection.getPage().isEmpty()) {
269-
this.conversationPartCollection = find(this.getId()).getConversationPartCollection();
268+
if (conversationPartCollection == null) {
269+
conversationPartCollection = find(this.getId()).getConversationPartCollection();
270270
}
271271

272272
return conversationPartCollection;

intercom-java/src/test/java/io/intercom/api/ConversationTest.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,33 @@
11
package io.intercom.api;
22

3+
import com.fasterxml.jackson.databind.ObjectMapper;
34
import com.google.common.collect.Maps;
5+
import org.junit.BeforeClass;
46
import org.junit.Test;
7+
import org.junit.runner.RunWith;
8+
import org.mockito.Mockito;
9+
import org.powermock.api.mockito.PowerMockito;
10+
import org.powermock.core.classloader.annotations.PrepareForTest;
11+
import org.powermock.modules.junit4.PowerMockRunner;
512

13+
import java.io.IOException;
614
import java.util.Map;
715

16+
import static io.intercom.api.TestSupport.load;
817
import static org.junit.Assert.*;
918
import static org.junit.Assert.assertTrue;
1019

20+
@RunWith(PowerMockRunner.class)
21+
@PrepareForTest( { Conversation.class })
1122
public class ConversationTest {
1223

24+
private static ObjectMapper objectMapper;
25+
26+
@BeforeClass
27+
public static void beforeClass() {
28+
objectMapper = MapperSupport.objectMapper();
29+
}
30+
1331
@Test
1432
public void testIsNullOrBlank() {
1533
assertTrue(Conversation.isNullOrBlank(null));
@@ -91,6 +109,52 @@ public void testDisplayAs() {
91109
}
92110
}
93111

112+
@Test
113+
public void testGetConversationsPartFromConversation() throws IOException {
114+
PowerMockito.mockStatic(Conversation.class);
115+
116+
String json = load("conversation.json");
117+
final Conversation conversation = objectMapper.readValue(json, Conversation.class);
118+
assertEquals(2, conversation.getConversationPartCollection().getPage().size());
119+
120+
PowerMockito.verifyStatic(Mockito.never());
121+
Conversation.find(conversation.getId());
122+
}
123+
124+
@Test
125+
public void testGetConversationsPartFromConversationCollection() throws IOException {
126+
PowerMockito.mockStatic(Conversation.class);
127+
128+
String conversationsJson = load("conversations.json");
129+
final ConversationCollection conversationCollection = objectMapper.readValue(conversationsJson, ConversationCollection.class);
130+
final Conversation conversation = conversationCollection.getPage().get(0);
131+
132+
String conversationJson = load("conversation.json");
133+
final Conversation conversationWithParts = objectMapper.readValue(conversationJson, Conversation.class);
134+
Mockito.when(Conversation.find(conversation.getId())).thenReturn(conversationWithParts);
135+
assertEquals(2, conversation.getConversationPartCollection().getPage().size());
136+
137+
PowerMockito.verifyStatic(Mockito.times(1));
138+
Conversation.find(conversation.getId());
139+
}
140+
141+
@Test
142+
public void testGetEmptyConversationsPartFromConversationCollection() throws IOException {
143+
PowerMockito.mockStatic(Conversation.class);
144+
145+
String conversationsJson = load("conversations.json");
146+
final ConversationCollection conversationCollection = objectMapper.readValue(conversationsJson, ConversationCollection.class);
147+
final Conversation conversation = conversationCollection.getPage().get(0);
148+
149+
String conversationJson = load("conversation_no_parts.json");
150+
final Conversation conversationWithParts = objectMapper.readValue(conversationJson, Conversation.class);
151+
Mockito.when(Conversation.find(conversation.getId())).thenReturn(conversationWithParts);
152+
assertEquals(0, conversation.getConversationPartCollection().getPage().size());
153+
154+
PowerMockito.verifyStatic(Mockito.times(1));
155+
Conversation.find(conversation.getId());
156+
}
157+
94158
private Map<String, String> buildRequestParameters(String html) {
95159
Map<String, String> params2 = Maps.newHashMap();
96160
params2.put("type", "admin");
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"type": "conversation",
3+
"id": "5143511111",
4+
"created_at": 1466703132,
5+
"updated_at": 1468236397,
6+
"conversation_message": {
7+
"type": "conversation_message",
8+
"id": "33954111",
9+
"subject": "",
10+
"body": "<p>test</p>",
11+
"author": {
12+
"type": "lead",
13+
"id": "576c1a139d0baad1010011111"
14+
},
15+
"attachments": [],
16+
"url": null
17+
},
18+
"user": {
19+
"type": "user",
20+
"id": "576c1a139d0baad1010001111"
21+
},
22+
"assignee": {
23+
"type": "admin",
24+
"id": "358111"
25+
},
26+
"conversation_parts": {
27+
"type": "conversation_part.list",
28+
"conversation_parts": [
29+
{
30+
"type": "conversation_part",
31+
"id": "142533411",
32+
"part_type": "comment",
33+
"body": "<p>dm-9187dba8-fb3b-cb99-da05-37a932d3d678</p>",
34+
"created_at": 1468031160,
35+
"updated_at": 1468031160,
36+
"notified_at": 1468031160,
37+
"assigned_to": null,
38+
"author": {
39+
"type": "user",
40+
"id": "576c1a139d0baad1010001111"
41+
},
42+
"attachments": [],
43+
"external_id": null
44+
},
45+
{
46+
"type": "conversation_part",
47+
"id": "142533511",
48+
"part_type": "comment",
49+
"body": "<p>im-99a3a78f-5105-449d-a114-a7b5eb7e5b80</p>",
50+
"created_at": 1468031171,
51+
"updated_at": 1468031171,
52+
"notified_at": 1468031171,
53+
"assigned_to": null,
54+
"author": {
55+
"type": "admin",
56+
"id": "358111"
57+
},
58+
"attachments": [],
59+
"external_id": null
60+
}
61+
],
62+
"total_count": 2
63+
},
64+
"open": true,
65+
"read": false,
66+
"tags": {
67+
"type": "tag.list",
68+
"tags": []
69+
}
70+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"type": "conversation",
3+
"id": "5143511111",
4+
"created_at": 1466703132,
5+
"updated_at": 1468236397,
6+
"conversation_message": {
7+
"type": "conversation_message",
8+
"id": "33954111",
9+
"subject": "",
10+
"body": "<p>test</p>",
11+
"author": {
12+
"type": "lead",
13+
"id": "576c1a139d0baad1010011111"
14+
},
15+
"attachments": [],
16+
"url": null
17+
},
18+
"user": {
19+
"type": "user",
20+
"id": "576c1a139d0baad1010001111"
21+
},
22+
"assignee": {
23+
"type": "admin",
24+
"id": "358111"
25+
},
26+
"conversation_parts": {
27+
"type": "conversation_part.list",
28+
"conversation_parts": [],
29+
"total_count": 0
30+
},
31+
"open": true,
32+
"read": false,
33+
"tags": {
34+
"type": "tag.list",
35+
"tags": []
36+
}
37+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
{
2+
"type": "conversation.list",
3+
"pages": {
4+
"type": "pages",
5+
"next": null,
6+
"page": 1,
7+
"per_page": 20,
8+
"total_pages": 1
9+
},
10+
"conversations": [
11+
{
12+
"type": "conversation",
13+
"id": "5680931777",
14+
"created_at": 1471438637,
15+
"updated_at": 1471438637,
16+
"user": {
17+
"type": "user",
18+
"id": "57b45f16d1aad7e69e011111"
19+
},
20+
"assignee": {
21+
"type": "nobody_admin",
22+
"id": null
23+
},
24+
"conversation_message": {
25+
"type": "conversation_message",
26+
"id": "41111111",
27+
"subject": "",
28+
"body": "<p>Hey2</p>",
29+
"author": {
30+
"type": "user",
31+
"id": "57b45f16d1aad7e69e011111"
32+
},
33+
"attachments": [],
34+
"url": null
35+
},
36+
"open": true,
37+
"read": true
38+
},
39+
{
40+
"type": "conversation",
41+
"id": "5680912811",
42+
"created_at": 1471438151,
43+
"updated_at": 1471438151,
44+
"user": {
45+
"type": "user",
46+
"id": "57b45bfef436c8d78611111"
47+
},
48+
"assignee": {
49+
"type": "nobody_admin",
50+
"id": null
51+
},
52+
"conversation_message": {
53+
"type": "conversation_message",
54+
"id": "41141111",
55+
"subject": "",
56+
"body": "<p>Hey</p>",
57+
"author": {
58+
"type": "user",
59+
"id": "57b45bfef436c8d786000111"
60+
},
61+
"attachments": [],
62+
"url": null
63+
},
64+
"open": true,
65+
"read": true
66+
},
67+
{
68+
"type": "conversation",
69+
"id": "5143511111",
70+
"created_at": 1466703132,
71+
"updated_at": 1468236397,
72+
"user": {
73+
"type": "lead",
74+
"id": "576c1a139d0baad101001111"
75+
},
76+
"assignee": {
77+
"type": "admin",
78+
"id": "358111"
79+
},
80+
"conversation_message": {
81+
"type": "conversation_message",
82+
"id": "33954838",
83+
"subject": "",
84+
"body": "<p>test</p>",
85+
"author": {
86+
"type": "lead",
87+
"id": "576c1a139d0baad101001111"
88+
},
89+
"attachments": [],
90+
"url": null
91+
},
92+
"open": true,
93+
"read": false
94+
}
95+
]
96+
}

0 commit comments

Comments
 (0)