Skip to content

Added missing tests #358

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -672,10 +672,10 @@ interface PubNub : EventEmitter, StatusEmitter {
@Deprecated(
replaceWith =
ReplaceWith(
"publish(message, channel)"
"fire(message, channel)"
),
level = DeprecationLevel.WARNING,
message = "Use publish(Object, String) instead",
message = "Use fire(Object, String) instead",
)
fun fire(): Publish

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,27 +107,32 @@ private void getSubscribedChannelsBasic() throws PubNubException {
// snippet.end
}


private void reconnectBasic() throws PubNubException {
private void disconnectBasic() throws PubNubException {
// https://www.pubnub.com/docs/sdks/java/api-reference/misc#basic-usage-8

PubNub pubNub = createPubNub();

// snippet.reconnectBasic
pubNub.reconnect();
// snippet.disconnectBasic
pubNub.disconnect();
// snippet.end
}

private void disconnectBasic() throws PubNubException {

private void reconnectBasic() throws PubNubException {
// https://www.pubnub.com/docs/sdks/java/api-reference/misc#basic-usage-9

PubNub pubNub = createPubNub();

// snippet.disconnectBasic
pubNub.disconnect();
// snippet.reconnectBasic
pubNub.reconnect();
// or
Long timetoken = 17276954606232118L; // Example timetoken received in publish/signal response
pubNub.reconnect(timetoken);
// snippet.end
}



private void timetokenToDateTimeBasic() {
// https://www.pubnub.com/docs/sdks/java/api-reference/misc#basic-usage-10

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.pubnub.api.integration;

import com.pubnub.api.PubNubException;
import com.pubnub.api.integration.util.BaseIntegrationTest;
import com.pubnub.api.models.consumer.channel_group.PNChannelGroupsListAllResult;
import org.junit.Test;

import java.util.Arrays;
Expand Down Expand Up @@ -131,6 +133,17 @@ public void testAddChannelsToGroup() throws InterruptedException {
signal.await();
}

@Test
public void testDeleteChannelGroup() throws PubNubException {
pubNub.addChannelsToChannelGroup().channelGroup(mGroup).channels(Arrays.asList(mChannel1, mChannel2, mChannel3)).sync();

pubNub.deleteChannelGroup().channelGroup(mGroup).sync();

PNChannelGroupsListAllResult channelGroupsList = pubNub.listAllChannelGroups().sync();

assertFalse(channelGroupsList.getGroups().contains(mGroup));
}

private void addChannelsToGroup() throws InterruptedException {
final CountDownLatch signal = new CountDownLatch(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.pubnub.api.models.consumer.history.PNFetchMessagesResult;
import com.pubnub.api.models.consumer.history.PNHistoryItemResult;
import com.pubnub.api.models.consumer.history.PNHistoryResult;
import com.pubnub.api.models.consumer.history.PNMessageCountResult;
import com.pubnub.api.models.consumer.message_actions.PNAddMessageActionResult;
import com.pubnub.api.models.consumer.message_actions.PNMessageAction;
import org.junit.Test;
Expand Down Expand Up @@ -671,4 +672,68 @@ public void testFetchSingleChannel_includeMessageTypeIsFalse() throws PubNubExce
assertNull(messageItem.getMessageType());
}
}

@Test
public void canGetMessageCounts() throws PubNubException {
String channel01 = randomChannel();
String channel02 = randomChannel();
long firstPublishToChannel01Timetoken = pubNub.publish().channel(channel01).message("FirstMessageChannel01").sync().getTimetoken();
pubNub.publish().channel(channel01).message("SecondMessage").sync();
long firstPublishToChannel02Timetoken = pubNub.publish().channel(channel02).message("FirstMessageChannel02").sync().getTimetoken();

// Test with multiple timetokens (one per channel)
PNMessageCountResult messagesCounts = pubNub.messageCounts()
.channels(Arrays.asList(channel01, channel02))
.channelsTimetoken(Arrays.asList(firstPublishToChannel01Timetoken - 1, firstPublishToChannel02Timetoken - 1))
.sync();
assertEquals(Long.valueOf(2), messagesCounts.getChannels().get(channel01));
assertEquals(Long.valueOf(1), messagesCounts.getChannels().get(channel02));
}

@Test
public void canGetMessageCountsWithSingleTimetoken() throws PubNubException {
String channel01 = randomChannel();
String channel02 = randomChannel();
long firstPublishToChannel01Timetoken = pubNub.publish().channel(channel01).message("FirstMessageChannel01").sync().getTimetoken();
pubNub.publish().channel(channel01).message("SecondMessage").sync();
pubNub.publish().channel(channel02).message("FirstMessageChannel02").sync();

// Test with single timetoken applied to all channels
PNMessageCountResult messagesCounts = pubNub.messageCounts()
.channels(Arrays.asList(channel01, channel02))
.channelsTimetoken(Arrays.asList(firstPublishToChannel01Timetoken - 1))
.sync();
assertEquals(Long.valueOf(2), messagesCounts.getChannels().get(channel01));
assertEquals(Long.valueOf(1), messagesCounts.getChannels().get(channel02));
}

@Test
public void canGetMessageCountsWithMultipleTimetokens() throws PubNubException {
String channel01 = randomChannel();
String channel02 = randomChannel();
String channel03 = randomChannel();

long firstPublishToChannel01Timetoken = pubNub.publish().channel(channel01).message("FirstMessageChannel01").sync().getTimetoken();
pubNub.publish().channel(channel01).message("SecondMessage").sync();
pubNub.publish().channel(channel01).message("ThirdMessage").sync();

long firstPublishToChannel02Timetoken = pubNub.publish().channel(channel02).message("FirstMessageChannel02").sync().getTimetoken();
pubNub.publish().channel(channel02).message("SecondMessage").sync();

long firstPublishToChannel03Timetoken = pubNub.publish().channel(channel03).message("FirstMessageChannel03").sync().getTimetoken();

// Test with multiple timetokens, each tailored to specific channels
PNMessageCountResult messagesCounts = pubNub.messageCounts()
.channels(Arrays.asList(channel01, channel02, channel03))
.channelsTimetoken(Arrays.asList(
firstPublishToChannel01Timetoken, // Should count 2 messages (second and third)
firstPublishToChannel02Timetoken - 1, // Should count 2 messages (all)
firstPublishToChannel03Timetoken - 1 // Should count 1 message (all)
))
.sync();

assertEquals(Long.valueOf(2), messagesCounts.getChannels().get(channel01));
assertEquals(Long.valueOf(2), messagesCounts.getChannels().get(channel02));
assertEquals(Long.valueOf(1), messagesCounts.getChannels().get(channel03));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,27 @@

import com.pubnub.api.PubNubException;
import com.pubnub.api.integration.util.BaseIntegrationTest;
import com.pubnub.api.java.v2.entities.Channel;
import com.pubnub.api.java.v2.subscriptions.Subscription;
import com.pubnub.api.models.consumer.files.PNFileUploadResult;
import com.pubnub.api.models.consumer.files.PNPublishFileMessageResult;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.concurrent.CountDownLatch;

import static com.pubnub.api.integration.util.Utils.randomChannel;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class PublishFileMessageIntegrationTest extends BaseIntegrationTest {

@Test
public void can_publishFileMessage() throws PubNubException {
public void canPublishFileMessage() throws PubNubException {
PNPublishFileMessageResult publishFileMessageResult = pubNub.publishFileMessage()
.channel("whatever")
.fileName("whatever")
Expand All @@ -21,4 +33,65 @@ public void can_publishFileMessage() throws PubNubException {

assertNotNull(publishFileMessageResult.getTimetoken());
}

@Test
public void publishFileMessageAndReceiveOnListener() throws Exception {
String channelName = randomChannel();
String fileName = "fileName_" + channelName + ".txt";
String messageSendFile = "This is a file message";
String publishFileMessage = "This is a publishFileMessage";
String customMessageType = "file-message";
String content = "This is the file content";
final CountDownLatch receivedFileEventFromSendFile = new CountDownLatch(1);
final CountDownLatch receivedFileEventFromPublishFileMessage = new CountDownLatch(1);


Channel channel = pubNub.channel(channelName);
Subscription subscription = channel.subscription();
subscription.setOnFile(pnFileEventResult -> {
if (pnFileEventResult.getMessage().equals(messageSendFile)) {
assertEquals(fileName, pnFileEventResult.getFile().getName());
receivedFileEventFromSendFile.countDown();
}
if (pnFileEventResult.getMessage().equals(publishFileMessage)) {
assertEquals(fileName, pnFileEventResult.getFile().getName());
receivedFileEventFromPublishFileMessage.countDown();
}
});
subscription.subscribe();
Thread.sleep(1000);

// 1. Upload a file using sendFile to get a real fileId and fileName
PNFileUploadResult uploadResult;
try (InputStream inputStream = new ByteArrayInputStream(content.getBytes(UTF_8))) {
uploadResult = pubNub.sendFile()
.channel(channelName)
.fileName(fileName)
.inputStream(inputStream)
.message(messageSendFile)
.customMessageType(customMessageType)
.sync();
}
String fileId = uploadResult.getFile().getId();
assertNotNull(fileId);

// 2. Use those values in publishFileMessage
PNPublishFileMessageResult publishFileMessageResult = pubNub.publishFileMessage()
.channel(channelName)
.fileName(fileName)
.fileId(fileId)
.message(publishFileMessage)
.customMessageType(customMessageType)
.sync();
assertNotNull(publishFileMessageResult.getTimetoken());


try {
assertTrue(receivedFileEventFromSendFile.await(10, SECONDS));
assertTrue(receivedFileEventFromPublishFileMessage.await(10, SECONDS));
} finally {
// Cleanup: delete the uploaded file
pubNub.deleteFile().channel(channelName).fileName(fileName).fileId(fileId).sync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import static com.pubnub.api.integration.util.Utils.randomChannel;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

Expand All @@ -59,6 +60,15 @@ protected void onAfter() {

}

@Test
public void canFireMessage() throws PubNubException {
final String expectedChannel = randomChannel();
final JsonObject messagePayload = generateMessage(pubNub);
PNPublishResult fireResult = pubNub.fire(messagePayload, expectedChannel).sync();

assertNotNull(fireResult.getTimetoken());
}

@Test
public void testPublishMessage() throws PubNubException {
final AtomicBoolean success = new AtomicBoolean();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,38 @@ class MiscellaneousOthers {
// snippet.end
}

private fun getSubscribedChannels(pubNub: PubNub) {
private fun getSubscribedChannelGroups(pubNub: PubNub) {
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-6

// snippet.getSubscribedChannels
val subscribedChannels = pubNub.getSubscribedChannels()
// snippet.getSubscribedChannelGroups
val subscribedChannelGroups = pubNub.getSubscribedChannelGroups()
// snippet.end
}

private fun getSubscribedChannelGroups(pubNub: PubNub) {
// snippet.getSubscribedChannelGroups
val subscribedChannelGroups = pubNub.getSubscribedChannelGroups()
private fun getSubscribedChannels(pubNub: PubNub) {
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-7

// snippet.getSubscribedChannels
val subscribedChannels = pubNub.getSubscribedChannels()
// snippet.end
}

private fun disconnect(pubNub: PubNub) {
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-7
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-8

// snippet.disconnect
pubNub.disconnect()
// snippet.end
}

private fun reconnect(pubNub: PubNub) {
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-8
// https://www.pubnub.com/docs/sdks/kotlin/api-reference/misc#basic-usage-9

// snippet.reconnect
pubNub.reconnect()
// or
val timetoken = 17276954606232118L // Example timetoken received in publish/signal response
pubNub.reconnect(timetoken)
// snippet.end
}

Expand Down
Loading
Loading