use of com.linkedin.restli.examples.groups.api.GroupMembershipKey in project rest.li by linkedin.
the class GroupMembershipsResource3 method toGroupMembership.
// This is a hack for the sample resource. So as not to write a separate persistence for this resource,
// convert from and to GroupMembership.
private static GroupMembership toGroupMembership(ComplexKeyGroupMembership complexKeyMembership) {
GroupMembership groupMembership = new GroupMembership(complexKeyMembership.data());
GroupMembershipKey complexKey = complexKeyMembership.getId();
CompoundKey compoundKey = new CompoundKey().append(GROUP_ID, complexKey.getGroupID()).append(MEMBER_ID, complexKey.getMemberID());
groupMembership.setId(URIParamUtils.encodeKeyForBody(compoundKey, true, AllProtocolVersions.BASELINE_PROTOCOL_VERSION));
groupMembership.setMemberID(complexKey.getMemberID());
groupMembership.setGroupID(complexKey.getGroupID());
return groupMembership;
}
use of com.linkedin.restli.examples.groups.api.GroupMembershipKey in project rest.li by linkedin.
the class GroupMembershipsResource3 method create.
/**
* @see com.linkedin.restli.server.resources.ComplexKeyResourceTemplate#create(com.linkedin.data.template.RecordTemplate)
*/
@Override
public CreateResponse create(ComplexKeyGroupMembership groupMembership) {
// object
if (!groupMembership.getId().hasMemberID() || !groupMembership.getId().hasGroupID()) {
throw new RestLiServiceException(S_400_BAD_REQUEST, "groupID and memberID fields must be set while creating a ComplexKeyGroupMembership.");
}
GroupMembershipKey groupMembershipKey = new GroupMembershipKey();
groupMembershipKey.setMemberID(groupMembership.getId().getMemberID());
groupMembershipKey.setGroupID(groupMembership.getId().getGroupID());
ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> complexResourceKey = new ComplexResourceKey<>(groupMembershipKey, new GroupMembershipParam());
groupMembership.setId(complexResourceKey.getKey());
_app.getMembershipMgr().save(toGroupMembership(groupMembership));
return new CreateResponse(complexResourceKey, HttpStatus.S_201_CREATED);
}
use of com.linkedin.restli.examples.groups.api.GroupMembershipKey in project rest.li by linkedin.
the class GroupMembershipsResource3 method complexKeyToCompoundKey.
private static CompoundKey complexKeyToCompoundKey(ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> id) {
GroupMembershipKey key = id.getKey();
CompoundKey compoundKey = new CompoundKey();
compoundKey.append(GROUP_ID, key.getGroupID(GetMode.NULL));
compoundKey.append(MEMBER_ID, key.getMemberID(GetMode.NULL));
return compoundKey;
}
use of com.linkedin.restli.examples.groups.api.GroupMembershipKey in project rest.li by linkedin.
the class GroupMembershipsResource3 method batchGet.
/**
* @see GroupMembershipsResource2#batchGet(Set)
*/
@Override
public BatchResult<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership> batchGet(Set<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>> ids) {
Map<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership> result = new HashMap<>(ids.size());
Map<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, RestLiServiceException> errors = new HashMap<>();
Iterator<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>> iterator = ids.iterator();
while (iterator.hasNext()) {
ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> key = iterator.next();
ComplexKeyGroupMembership membership = fromGroupMembership(_app.getMembershipMgr().get(complexKeyToCompoundKey(key)));
if (membership != null) {
result.put(key, membership);
} else {
errors.put(key, new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
}
}
return new BatchResult<>(result, errors);
}
use of com.linkedin.restli.examples.groups.api.GroupMembershipKey in project rest.li by linkedin.
the class TestGroupsClient method testComplexKeyBatchCreateGetUpdateDelete.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestComplexBuilderDataProvider")
public void testComplexKeyBatchCreateGetUpdateDelete(ProtocolVersion version, RootBuilderWrapper<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership> builders) throws RemoteInvocationException {
ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> complexKey1 = buildComplexKey(1, 1, 10, "String1");
ComplexKeyGroupMembership groupMembership1 = buildComplexKeyGroupMembership(complexKey1.getKey(), "alfred@test.linkedin.com", "alfred", "hitchcock");
ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> complexKey2 = buildComplexKey(2, 1, 20, "String2");
ComplexKeyGroupMembership groupMembership2 = buildComplexKeyGroupMembership(complexKey2.getKey(), "bruce@test.linkedin.com", "bruce", "willis");
ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> complexKey3 = buildComplexKey(3, 1, 30, "String3");
ComplexKeyGroupMembership groupMembership3 = buildComplexKeyGroupMembership(complexKey3.getKey(), "carole@test.linkedin.com", "carole", "bouquet");
Request<CollectionResponse<CreateStatus>> createRequest = builders.batchCreate().input(groupMembership1).input(groupMembership2).input(groupMembership3).build();
Response<CollectionResponse<CreateStatus>> createResponse = getClient().sendRequest(createRequest).getResponse();
Assert.assertEquals(createResponse.getStatus(), 200);
final RestliRequestOptions requestOptions = builders.getRequestOptions();
@SuppressWarnings("unchecked") Request<BatchKVResponse<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, EntityResponse<ComplexKeyGroupMembership>>> request = new GroupMembershipsComplexRequestBuilders(requestOptions).batchGet().ids(complexKey1, complexKey2, complexKey3).fields(GroupMembership.fields().contactEmail()).build();
BatchKVResponse<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, EntityResponse<ComplexKeyGroupMembership>> groupMemberships = getClient().sendRequest(request).getResponse().getEntity();
Map<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, EntityResponse<ComplexKeyGroupMembership>> results = groupMemberships.getResults();
ComplexKeyGroupMembership groupMembership1_ = results.get(complexKey1).getEntity();
ComplexKeyGroupMembership groupMembership2_ = results.get(complexKey2).getEntity();
ComplexKeyGroupMembership groupMembership3_ = results.get(complexKey3).getEntity();
Assert.assertNotNull(groupMembership1_);
Assert.assertEquals(groupMembership1_.getContactEmail(), "alfred@test.linkedin.com");
Assert.assertNotNull(groupMembership2_);
Assert.assertEquals(groupMembership2_.getContactEmail(), "bruce@test.linkedin.com");
Assert.assertNotNull(groupMembership3_);
Assert.assertEquals(groupMembership3_.getContactEmail(), "carole@test.linkedin.com");
// Update and verify
groupMembership1.setContactEmail("alfred+@test.linkedin.com");
groupMembership2.setContactEmail("bruce+@test.linkedin.com");
groupMembership3.setContactEmail("carole+@test.linkedin.com");
Request<BatchKVResponse<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, UpdateStatus>> updateRequest = builders.batchUpdate().input(complexKey1, groupMembership1).input(complexKey2, groupMembership2).input(complexKey3, groupMembership3).build();
int status = getClient().sendRequest(updateRequest).getResponse().getStatus();
Assert.assertEquals(status, 200);
}
Aggregations