|
1 | 1 | package io.intercom.api; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
3 | 4 | import com.google.common.collect.Maps; |
| 5 | +import org.junit.BeforeClass; |
4 | 6 | 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; |
5 | 12 |
|
| 13 | +import java.io.IOException; |
6 | 14 | import java.util.Map; |
7 | 15 |
|
| 16 | +import static io.intercom.api.TestSupport.load; |
8 | 17 | import static org.junit.Assert.*; |
9 | 18 | import static org.junit.Assert.assertTrue; |
10 | 19 |
|
| 20 | +@RunWith(PowerMockRunner.class) |
| 21 | +@PrepareForTest( { Conversation.class }) |
11 | 22 | public class ConversationTest { |
12 | 23 |
|
| 24 | + private static ObjectMapper objectMapper; |
| 25 | + |
| 26 | + @BeforeClass |
| 27 | + public static void beforeClass() { |
| 28 | + objectMapper = MapperSupport.objectMapper(); |
| 29 | + } |
| 30 | + |
13 | 31 | @Test |
14 | 32 | public void testIsNullOrBlank() { |
15 | 33 | assertTrue(Conversation.isNullOrBlank(null)); |
@@ -91,6 +109,52 @@ public void testDisplayAs() { |
91 | 109 | } |
92 | 110 | } |
93 | 111 |
|
| 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 | + |
94 | 158 | private Map<String, String> buildRequestParameters(String html) { |
95 | 159 | Map<String, String> params2 = Maps.newHashMap(); |
96 | 160 | params2.put("type", "admin"); |
|
0 commit comments