Migrate MessageSenderTest to JUnit 5
This commit is contained in:
parent
e08c5a412e
commit
aeb9f67266
|
@ -20,8 +20,8 @@ import static org.mockito.Mockito.when;
|
||||||
import com.google.protobuf.ByteString;
|
import com.google.protobuf.ByteString;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import org.apache.commons.lang3.RandomStringUtils;
|
import org.apache.commons.lang3.RandomStringUtils;
|
||||||
import org.junit.Before;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
import org.whispersystems.textsecuregcm.entities.MessageProtos;
|
import org.whispersystems.textsecuregcm.entities.MessageProtos;
|
||||||
import org.whispersystems.textsecuregcm.metrics.PushLatencyManager;
|
import org.whispersystems.textsecuregcm.metrics.PushLatencyManager;
|
||||||
|
@ -29,134 +29,134 @@ import org.whispersystems.textsecuregcm.storage.Account;
|
||||||
import org.whispersystems.textsecuregcm.storage.Device;
|
import org.whispersystems.textsecuregcm.storage.Device;
|
||||||
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
import org.whispersystems.textsecuregcm.storage.MessagesManager;
|
||||||
|
|
||||||
public class MessageSenderTest {
|
class MessageSenderTest {
|
||||||
|
|
||||||
private Account account;
|
private Account account;
|
||||||
private Device device;
|
private Device device;
|
||||||
private MessageProtos.Envelope message;
|
private MessageProtos.Envelope message;
|
||||||
|
|
||||||
private ClientPresenceManager clientPresenceManager;
|
private ClientPresenceManager clientPresenceManager;
|
||||||
private MessagesManager messagesManager;
|
private MessagesManager messagesManager;
|
||||||
private GCMSender gcmSender;
|
private GCMSender gcmSender;
|
||||||
private APNSender apnSender;
|
private APNSender apnSender;
|
||||||
private MessageSender messageSender;
|
private MessageSender messageSender;
|
||||||
|
|
||||||
private static final UUID ACCOUNT_UUID = UUID.randomUUID();
|
private static final UUID ACCOUNT_UUID = UUID.randomUUID();
|
||||||
private static final long DEVICE_ID = 1L;
|
private static final long DEVICE_ID = 1L;
|
||||||
|
|
||||||
@Before
|
@BeforeEach
|
||||||
public void setUp() {
|
void setUp() {
|
||||||
|
|
||||||
account = mock(Account.class);
|
account = mock(Account.class);
|
||||||
device = mock(Device.class);
|
device = mock(Device.class);
|
||||||
message = generateRandomMessage();
|
message = generateRandomMessage();
|
||||||
|
|
||||||
clientPresenceManager = mock(ClientPresenceManager.class);
|
clientPresenceManager = mock(ClientPresenceManager.class);
|
||||||
messagesManager = mock(MessagesManager.class);
|
messagesManager = mock(MessagesManager.class);
|
||||||
gcmSender = mock(GCMSender.class);
|
gcmSender = mock(GCMSender.class);
|
||||||
apnSender = mock(APNSender.class);
|
apnSender = mock(APNSender.class);
|
||||||
messageSender = new MessageSender(mock(ApnFallbackManager.class),
|
messageSender = new MessageSender(mock(ApnFallbackManager.class),
|
||||||
clientPresenceManager,
|
clientPresenceManager,
|
||||||
messagesManager,
|
messagesManager,
|
||||||
gcmSender,
|
gcmSender,
|
||||||
apnSender,
|
apnSender,
|
||||||
mock(PushLatencyManager.class));
|
mock(PushLatencyManager.class));
|
||||||
|
|
||||||
when(account.getUuid()).thenReturn(ACCOUNT_UUID);
|
when(account.getUuid()).thenReturn(ACCOUNT_UUID);
|
||||||
when(device.getId()).thenReturn(DEVICE_ID);
|
when(device.getId()).thenReturn(DEVICE_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSendOnlineMessageClientPresent() throws Exception {
|
void testSendOnlineMessageClientPresent() throws Exception {
|
||||||
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(true);
|
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(true);
|
||||||
when(device.getGcmId()).thenReturn("gcm-id");
|
when(device.getGcmId()).thenReturn("gcm-id");
|
||||||
|
|
||||||
messageSender.sendMessage(account, device, message, true);
|
messageSender.sendMessage(account, device, message, true);
|
||||||
|
|
||||||
ArgumentCaptor<MessageProtos.Envelope> envelopeArgumentCaptor = ArgumentCaptor.forClass(
|
ArgumentCaptor<MessageProtos.Envelope> envelopeArgumentCaptor = ArgumentCaptor.forClass(
|
||||||
MessageProtos.Envelope.class);
|
MessageProtos.Envelope.class);
|
||||||
|
|
||||||
verify(messagesManager).insert(any(), anyLong(), envelopeArgumentCaptor.capture());
|
verify(messagesManager).insert(any(), anyLong(), envelopeArgumentCaptor.capture());
|
||||||
|
|
||||||
assertTrue(envelopeArgumentCaptor.getValue().getEphemeral());
|
assertTrue(envelopeArgumentCaptor.getValue().getEphemeral());
|
||||||
|
|
||||||
verifyNoInteractions(gcmSender);
|
verifyNoInteractions(gcmSender);
|
||||||
verifyNoInteractions(apnSender);
|
verifyNoInteractions(apnSender);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSendOnlineMessageClientNotPresent() throws Exception {
|
void testSendOnlineMessageClientNotPresent() throws Exception {
|
||||||
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(false);
|
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(false);
|
||||||
when(device.getGcmId()).thenReturn("gcm-id");
|
when(device.getGcmId()).thenReturn("gcm-id");
|
||||||
|
|
||||||
messageSender.sendMessage(account, device, message, true);
|
messageSender.sendMessage(account, device, message, true);
|
||||||
|
|
||||||
verify(messagesManager, never()).insert(any(), anyLong(), any());
|
verify(messagesManager, never()).insert(any(), anyLong(), any());
|
||||||
verifyNoInteractions(gcmSender);
|
verifyNoInteractions(gcmSender);
|
||||||
verifyNoInteractions(apnSender);
|
verifyNoInteractions(apnSender);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSendMessageClientPresent() throws Exception {
|
void testSendMessageClientPresent() throws Exception {
|
||||||
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(true);
|
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(true);
|
||||||
when(device.getGcmId()).thenReturn("gcm-id");
|
when(device.getGcmId()).thenReturn("gcm-id");
|
||||||
|
|
||||||
messageSender.sendMessage(account, device, message, false);
|
messageSender.sendMessage(account, device, message, false);
|
||||||
|
|
||||||
final ArgumentCaptor<MessageProtos.Envelope> envelopeArgumentCaptor = ArgumentCaptor.forClass(
|
final ArgumentCaptor<MessageProtos.Envelope> envelopeArgumentCaptor = ArgumentCaptor.forClass(
|
||||||
MessageProtos.Envelope.class);
|
MessageProtos.Envelope.class);
|
||||||
|
|
||||||
verify(messagesManager).insert(eq(ACCOUNT_UUID), eq(DEVICE_ID), envelopeArgumentCaptor.capture());
|
verify(messagesManager).insert(eq(ACCOUNT_UUID), eq(DEVICE_ID), envelopeArgumentCaptor.capture());
|
||||||
|
|
||||||
assertFalse(envelopeArgumentCaptor.getValue().getEphemeral());
|
assertFalse(envelopeArgumentCaptor.getValue().getEphemeral());
|
||||||
assertEquals(message, envelopeArgumentCaptor.getValue());
|
assertEquals(message, envelopeArgumentCaptor.getValue());
|
||||||
verifyNoInteractions(gcmSender);
|
verifyNoInteractions(gcmSender);
|
||||||
verifyNoInteractions(apnSender);
|
verifyNoInteractions(apnSender);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSendMessageGcmClientNotPresent() throws Exception {
|
void testSendMessageGcmClientNotPresent() throws Exception {
|
||||||
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(false);
|
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(false);
|
||||||
when(device.getGcmId()).thenReturn("gcm-id");
|
when(device.getGcmId()).thenReturn("gcm-id");
|
||||||
|
|
||||||
messageSender.sendMessage(account, device, message, false);
|
messageSender.sendMessage(account, device, message, false);
|
||||||
|
|
||||||
verify(messagesManager).insert(ACCOUNT_UUID, DEVICE_ID, message);
|
verify(messagesManager).insert(ACCOUNT_UUID, DEVICE_ID, message);
|
||||||
verify(gcmSender).sendMessage(any());
|
verify(gcmSender).sendMessage(any());
|
||||||
verifyNoInteractions(apnSender);
|
verifyNoInteractions(apnSender);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSendMessageApnClientNotPresent() throws Exception {
|
void testSendMessageApnClientNotPresent() throws Exception {
|
||||||
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(false);
|
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(false);
|
||||||
when(device.getApnId()).thenReturn("apn-id");
|
when(device.getApnId()).thenReturn("apn-id");
|
||||||
|
|
||||||
messageSender.sendMessage(account, device, message, false);
|
messageSender.sendMessage(account, device, message, false);
|
||||||
|
|
||||||
verify(messagesManager).insert(ACCOUNT_UUID, DEVICE_ID, message);
|
verify(messagesManager).insert(ACCOUNT_UUID, DEVICE_ID, message);
|
||||||
verifyNoInteractions(gcmSender);
|
verifyNoInteractions(gcmSender);
|
||||||
verify(apnSender).sendMessage(any());
|
verify(apnSender).sendMessage(any());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testSendMessageFetchClientNotPresent() throws Exception {
|
void testSendMessageFetchClientNotPresent() throws Exception {
|
||||||
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(false);
|
when(clientPresenceManager.isPresent(ACCOUNT_UUID, DEVICE_ID)).thenReturn(false);
|
||||||
when(device.getFetchesMessages()).thenReturn(true);
|
when(device.getFetchesMessages()).thenReturn(true);
|
||||||
|
|
||||||
messageSender.sendMessage(account, device, message, false);
|
messageSender.sendMessage(account, device, message, false);
|
||||||
|
|
||||||
verify(messagesManager).insert(ACCOUNT_UUID, DEVICE_ID, message);
|
verify(messagesManager).insert(ACCOUNT_UUID, DEVICE_ID, message);
|
||||||
verifyNoInteractions(gcmSender);
|
verifyNoInteractions(gcmSender);
|
||||||
verifyNoInteractions(apnSender);
|
verifyNoInteractions(apnSender);
|
||||||
}
|
}
|
||||||
|
|
||||||
private MessageProtos.Envelope generateRandomMessage() {
|
private MessageProtos.Envelope generateRandomMessage() {
|
||||||
return MessageProtos.Envelope.newBuilder()
|
return MessageProtos.Envelope.newBuilder()
|
||||||
.setTimestamp(System.currentTimeMillis())
|
.setTimestamp(System.currentTimeMillis())
|
||||||
.setServerTimestamp(System.currentTimeMillis())
|
.setServerTimestamp(System.currentTimeMillis())
|
||||||
.setContent(ByteString.copyFromUtf8(RandomStringUtils.randomAlphanumeric(256)))
|
.setContent(ByteString.copyFromUtf8(RandomStringUtils.randomAlphanumeric(256)))
|
||||||
.setType(MessageProtos.Envelope.Type.CIPHERTEXT)
|
.setType(MessageProtos.Envelope.Type.CIPHERTEXT)
|
||||||
.setServerGuid(UUID.randomUUID().toString())
|
.setServerGuid(UUID.randomUUID().toString())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue