|
| 1 | +package com.pubnub.api.integration; |
| 2 | + |
| 3 | +import com.pubnub.api.integration.util.BaseIntegrationTest; |
| 4 | +import org.junit.Test; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.Collections; |
| 8 | +import java.util.concurrent.CountDownLatch; |
| 9 | + |
| 10 | +import static com.pubnub.api.integration.util.Utils.random; |
| 11 | +import static com.pubnub.api.integration.util.Utils.randomChannel; |
| 12 | +import static org.junit.Assert.*; |
| 13 | + |
| 14 | +public class GroupManagementIntegrationTests extends BaseIntegrationTest { |
| 15 | + |
| 16 | + private String mChannel1; |
| 17 | + private String mChannel2; |
| 18 | + private String mChannel3; |
| 19 | + |
| 20 | + private String mGroup; |
| 21 | + |
| 22 | + @Override |
| 23 | + protected void onBefore() { |
| 24 | + mChannel1 = randomChannel(); |
| 25 | + mChannel2 = randomChannel(); |
| 26 | + mChannel3 = randomChannel(); |
| 27 | + mGroup = "cg_".concat(random()); |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + protected void onAfter() { |
| 32 | + |
| 33 | + } |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testRemoveChannelsFromGroup() throws InterruptedException { |
| 37 | + final CountDownLatch signal = new CountDownLatch(1); |
| 38 | + |
| 39 | + addChannelsToGroup(); |
| 40 | + |
| 41 | + pubNub.removeChannelsFromChannelGroup() |
| 42 | + .channelGroup(mGroup) |
| 43 | + .channels(Arrays.asList(mChannel1, mChannel2, mChannel3)) |
| 44 | + .async((result, status) -> { |
| 45 | + assertFalse(status.isError()); |
| 46 | + assert status.getAffectedChannels() != null; |
| 47 | + assertEquals(3, status.getAffectedChannels().size()); |
| 48 | + assertEquals(0, pubNub.getSubscribedChannels().size()); |
| 49 | + assert status.getAffectedChannelGroups() != null; |
| 50 | + assertEquals(1, status.getAffectedChannelGroups().size()); |
| 51 | + assertEquals(0, pubNub.getSubscribedChannelGroups().size()); |
| 52 | + signal.countDown(); |
| 53 | + }); |
| 54 | + |
| 55 | + signal.await(); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + public void testRemoveChannelFromGroup() throws InterruptedException { |
| 60 | + final CountDownLatch signal = new CountDownLatch(1); |
| 61 | + |
| 62 | + addChannelsToGroup(); |
| 63 | + |
| 64 | + pubNub.removeChannelsFromChannelGroup() |
| 65 | + .channelGroup(mGroup) |
| 66 | + .channels(Collections.singletonList(mChannel1)) |
| 67 | + .async((result, status) -> { |
| 68 | + assertFalse(status.isError()); |
| 69 | + signal.countDown(); |
| 70 | + }); |
| 71 | + |
| 72 | + signal.await(); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void testSubscribeToChannelGroup() throws InterruptedException { |
| 77 | + addChannelsToGroup(); |
| 78 | + subscribeToChannelGroup(pubNub, mGroup); |
| 79 | + |
| 80 | + boolean isGroupSubscribed = false; |
| 81 | + for (int i = 0; i < pubNub.getSubscribedChannelGroups().size(); i++) { |
| 82 | + if (pubNub.getSubscribedChannelGroups().get(i).equals(mGroup)) { |
| 83 | + isGroupSubscribed = true; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + assertTrue(isGroupSubscribed); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testUnsubscribeFromChannelGroup() throws InterruptedException { |
| 92 | + addChannelsToGroup(); |
| 93 | + subscribeToChannelGroup(pubNub, mGroup); |
| 94 | + |
| 95 | + pubNub.unsubscribe() |
| 96 | + .channelGroups(Collections.singletonList(mGroup)) |
| 97 | + .execute(); |
| 98 | + |
| 99 | + pause(1); |
| 100 | + |
| 101 | + assertEquals(0, pubNub.getSubscribedChannelGroups().size()); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testGetAllChannelsFromGroup() throws InterruptedException { |
| 106 | + final CountDownLatch signal = new CountDownLatch(1); |
| 107 | + |
| 108 | + addChannelsToGroup(); |
| 109 | + |
| 110 | + pubNub.listChannelsForChannelGroup() |
| 111 | + .channelGroup(mGroup) |
| 112 | + .async((result, status) -> { |
| 113 | + assertFalse(status.isError()); |
| 114 | + assert result != null; |
| 115 | + assertEquals(3, result.getChannels().size()); |
| 116 | + signal.countDown(); |
| 117 | + }); |
| 118 | + |
| 119 | + signal.await(); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void testAddChannelsToGroup() throws InterruptedException { |
| 124 | + final CountDownLatch signal = new CountDownLatch(1); |
| 125 | + |
| 126 | + pubNub.addChannelsToChannelGroup() |
| 127 | + .channelGroup(mGroup) |
| 128 | + .channels(Arrays.asList(mChannel1, mChannel2, mChannel3)) |
| 129 | + .async((result, status) -> { |
| 130 | + assertFalse(status.isError()); |
| 131 | + assert status.getAffectedChannelGroups() != null; |
| 132 | + assertEquals(1, status.getAffectedChannelGroups().size()); |
| 133 | + assert status.getAffectedChannels() != null; |
| 134 | + assertEquals(3, status.getAffectedChannels().size()); |
| 135 | + signal.countDown(); |
| 136 | + }); |
| 137 | + |
| 138 | + signal.await(); |
| 139 | + } |
| 140 | + |
| 141 | + private void addChannelsToGroup() throws InterruptedException { |
| 142 | + final CountDownLatch signal = new CountDownLatch(1); |
| 143 | + |
| 144 | + pubNub.addChannelsToChannelGroup() |
| 145 | + .channelGroup(mGroup) |
| 146 | + .channels(Arrays.asList(mChannel1, mChannel2, mChannel3)) |
| 147 | + .async((result, status) -> { |
| 148 | + assertFalse(status.isError()); |
| 149 | + signal.countDown(); |
| 150 | + }); |
| 151 | + |
| 152 | + signal.await(); |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments