Skip to content

Commit

Permalink
Added possibility to add status when setting ChannelMembers (#251)
Browse files Browse the repository at this point in the history
* Added possibility to add status when setting ChannelMembers
  • Loading branch information
marcin-cebo authored Nov 4, 2022
1 parent 85835f6 commit 9d7298c
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 32 deletions.
1 change: 0 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@ task integrationTest(type: Test) {
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath += sourceSets.integrationTest.runtimeClasspath
}

jacoco {
toolVersion = "0.8.2"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,47 @@

import com.pubnub.api.PubNubException;
import com.pubnub.api.integration.objects.ObjectsApiBaseIT;
import com.pubnub.api.models.consumer.objects_api.member.*;
import com.pubnub.api.models.consumer.objects_api.member.PNGetChannelMembersResult;
import com.pubnub.api.models.consumer.objects_api.member.PNManageChannelMembersResult;
import com.pubnub.api.models.consumer.objects_api.member.PNMembers;
import com.pubnub.api.models.consumer.objects_api.member.PNRemoveChannelMembersResult;
import com.pubnub.api.models.consumer.objects_api.member.PNSetChannelMembersResult;
import com.pubnub.api.models.consumer.objects_api.member.PNUUID;
import com.pubnub.api.models.consumer.objects_api.uuid.PNUUIDMetadata;
import org.apache.http.HttpStatus;
import org.junit.After;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.stream.Collectors;

import static com.pubnub.api.endpoints.objects_api.utils.Include.PNUUIDDetailsLevel.UUID_WITH_CUSTOM;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

public class ChannelMembersIT extends ObjectsApiBaseIT {
private final static Logger LOG = LoggerFactory.getLogger(ChannelMembersIT.class);
private static final Logger LOG = LoggerFactory.getLogger(ChannelMembersIT.class);
private static final String STATUS_01 = "myStatus01";
private static final String STATUS_02 = "myStatus02";

private final List<PNSetChannelMembersResult> createdMembersList = new ArrayList<>();

Expand All @@ -30,7 +55,8 @@ public class ChannelMembersIT extends ObjectsApiBaseIT {
public void addChannelMembersHappyPath() throws PubNubException {
//given
Map<String, Object> customMembershipObject = customChannelMembershipObject();
final Collection<PNUUID> channelMembers = Arrays.asList(PNUUID.uuid(TEST_UUID1),
final Collection<PNUUID> channelMembers = Arrays.asList(
PNUUID.uuid(TEST_UUID1, STATUS_01),
PNUUID.uuidWithCustom(TEST_UUID2, customMembershipObject));

//when
Expand Down Expand Up @@ -67,15 +93,23 @@ public void addChannelMembersHappyPath() throws PubNubException {
}
}

List<String> actualStatusList = setChannelMembersResult.getData()
.stream()
.map(PNMembers::getStatus)
.filter(Objects::nonNull)
.collect(Collectors.toList());

assertThat(returnedUUIDs, containsInAnyOrder(expectedUUIDs.toArray()));
assertThat(receivedCustomObjects, hasSize(1));
assertThat(actualStatusList, containsInAnyOrder(STATUS_01));
}

@Test
public void getChannelMembersHappyPath() throws PubNubException {
//given
final Collection<PNUUID> channelMembers = Arrays.asList(PNUUID.uuid(TEST_UUID1),
PNUUID.uuidWithCustom(TEST_UUID2, customChannelMembershipObject()));
final Collection<PNUUID> channelMembers = Arrays.asList(
PNUUID.uuid(TEST_UUID1),
PNUUID.uuidWithCustom(TEST_UUID2, customChannelMembershipObject(), STATUS_02));

final PNSetChannelMembersResult setChannelMembersResult = pubNubUnderTest.setChannelMembers()
.channel(testChannelId)
Expand Down Expand Up @@ -117,17 +151,25 @@ public void getChannelMembersHappyPath() throws PubNubException {
receivedCustomObjects.add(custom);
}
}

List<String> actualStatusList = setChannelMembersResult.getData()
.stream()
.map(PNMembers::getStatus)
.filter(Objects::nonNull)
.collect(Collectors.toList());

assertThat(returnedUUIDs, containsInAnyOrder(expectedUUIDs.toArray()));
assertThat(receivedCustomObjects, hasSize(1));

assertThat(actualStatusList, containsInAnyOrder(STATUS_02));

}

@Test
public void removeChannelMembersHappyPath() throws PubNubException {
//given
final Collection<PNUUID> channelMembers = Arrays.asList(PNUUID.uuid(TEST_UUID1),
PNUUID.uuidWithCustom(TEST_UUID2, customChannelMembershipObject()));
final Collection<PNUUID> channelMembers = Arrays.asList(
PNUUID.uuid(TEST_UUID1, STATUS_01),
PNUUID.uuidWithCustom(TEST_UUID2, customChannelMembershipObject(), STATUS_02));

final PNSetChannelMembersResult setChannelMembersResult = pubNubUnderTest.setChannelMembers()
.channel(testChannelId)
Expand Down Expand Up @@ -165,7 +207,7 @@ public void removeChannelMembersHappyPath() throws PubNubException {
public void manageChannelMembersHappyPath() throws PubNubException {
//given
final List<PNUUID> channelMembersToRemove = Collections.singletonList(
PNUUID.uuidWithCustom(TEST_UUID1, customChannelMembershipObject()));
PNUUID.uuidWithCustom(TEST_UUID1, customChannelMembershipObject(), STATUS_01));

final PNSetChannelMembersResult setChannelMembersResult = pubNubUnderTest.setChannelMembers()
.channel(testChannelId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,61 @@

import com.google.gson.annotations.JsonAdapter;
import com.pubnub.api.models.consumer.objects_api.util.CustomPayloadJsonInterceptor;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.NonNull;
import lombok.ToString;

import java.util.HashMap;
import java.util.Map;

@RequiredArgsConstructor
@Data
public abstract class PNUUID {
@AllArgsConstructor
@EqualsAndHashCode
@Getter
public static class UUIDId {
private String id;
}

@Getter
@NonNull
private final UUIDId uuid;
private final String status;

public static PNUUID uuid(final String uuid) {
return new JustUUID(new UUIDId(uuid));
return new UUIDWithoutCustom(new UUIDId(uuid), null);
}

public static PNUUID uuid(final String uuid, final String status) {
return new UUIDWithoutCustom(new UUIDId(uuid), status);
}

public static PNUUID uuidWithCustom(final String uuid, final Map<String, Object> custom) {
return new UUIDWithCustom(new UUIDId(uuid), new HashMap<>(custom));
return new UUIDWithCustom(new UUIDId(uuid), new HashMap<>(custom), null);
}

public static PNUUID uuidWithCustom(final String uuid, final Map<String, Object> custom, final String status) {
return new UUIDWithCustom(new UUIDId(uuid), new HashMap<>(custom), status);
}

@Data
public static class UUIDId {
private final String id;
}

@Getter
@EqualsAndHashCode(callSuper = true)
public static class JustUUID extends PNUUID {
JustUUID(UUIDId uuid) {
super(uuid);
@ToString
private static class UUIDWithoutCustom extends PNUUID {
private UUIDWithoutCustom(UUIDId uuid, String status) {
super(uuid, status);
}
}

@Getter
@EqualsAndHashCode(callSuper = true)
@ToString
public static class UUIDWithCustom extends PNUUID {
@JsonAdapter(CustomPayloadJsonInterceptor.class)
private final Object custom;

UUIDWithCustom(UUIDId uuid, Object custom) {
super(uuid);
private UUIDWithCustom(UUIDId uuid, Object custom, String status) {
super(uuid, status);
this.custom = custom;
}
}
}
}

0 comments on commit 9d7298c

Please sign in to comment.