use of com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership 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.ComplexKeyGroupMembership in project rest.li by linkedin.
the class GroupMembershipsResource3 method update.
/**
* @see AssociationResource#update
*/
@Override
public UpdateResponse update(ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> id, PatchRequest<ComplexKeyGroupMembership> patch) {
ComplexKeyGroupMembership membership = fromGroupMembership(_app.getMembershipMgr().get(complexKeyToCompoundKey(id)));
try {
PatchApplier.applyPatch(membership, patch);
} catch (DataProcessingException e) {
return new UpdateResponse(S_400_BAD_REQUEST);
}
//validate(membership);
// we set groupID, memberID based on the URI
membership.setId(id.getKey());
_app.getMembershipMgr().save(toGroupMembership(membership));
return new UpdateResponse(S_204_NO_CONTENT);
}
use of com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership 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<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership>(ids.size());
Map<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, RestLiServiceException> errors = new HashMap<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, RestLiServiceException>();
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<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership>(result, errors);
}
use of com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership 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);
}
use of com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership in project rest.li by linkedin.
the class TestGroupsClient method testComplexKeyCreateGetUpdateDelete.
@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestComplexBuilderDataProvider")
public void testComplexKeyCreateGetUpdateDelete(ProtocolVersion version, RootBuilderWrapper<ComplexResourceKey<GroupMembershipKey, GroupMembershipParam>, ComplexKeyGroupMembership> builders) throws RemoteInvocationException {
// Create a new complex key resource
ComplexResourceKey<GroupMembershipKey, GroupMembershipParam> complexKey = buildComplexKey(1, 1, 10, "String1");
ComplexKeyGroupMembership groupMembership = buildComplexKeyGroupMembership(complexKey.getKey(), "alfred@test.linkedin.com", "alfred", "hitchcock");
Request<EmptyRecord> createRequest = builders.create().input(groupMembership).build();
Response<EmptyRecord> createResponse = getClient().sendRequest(createRequest).getResponse();
Assert.assertEquals(createResponse.getStatus(), 201);
GroupMembershipParam param = new GroupMembershipParam();
param.setIntParameter(1);
param.setStringParameter("1");
GroupMembershipQueryParam groupMembershipQueryParam1 = new GroupMembershipQueryParam();
groupMembershipQueryParam1.setIntParameter(1);
groupMembershipQueryParam1.setStringParameter("1");
GroupMembershipQueryParam groupMembershipQueryParam2 = new GroupMembershipQueryParam();
groupMembershipQueryParam2.setIntParameter(2);
groupMembershipQueryParam2.setStringParameter("2");
GroupMembershipQueryParamArray queryParamArray = new GroupMembershipQueryParamArray(Arrays.asList(groupMembershipQueryParam1, groupMembershipQueryParam2));
// Get the resource back and check state
Request<ComplexKeyGroupMembership> request = builders.get().id(complexKey).fields(GroupMembership.fields().contactEmail()).setQueryParam("testParam", param).setQueryParam("testParamArray", queryParamArray).build();
ComplexKeyGroupMembership groupMembership1 = getClient().sendRequest(request).getResponse().getEntity();
Assert.assertNotNull(groupMembership1);
Assert.assertEquals(groupMembership1.getContactEmail(), "alfred@test.linkedin.com");
// Test the same with optional complex parameters
request = builders.get().id(complexKey).fields(GroupMembership.fields().contactEmail()).build();
groupMembership1 = getClient().sendRequest(request).getResponse().getEntity();
Assert.assertNotNull(groupMembership1);
Assert.assertEquals(groupMembership1.getContactEmail(), "alfred@test.linkedin.com");
// Update contact email and verify
groupMembership.setContactEmail("alphred@test.linkedin.com");
Request<EmptyRecord> updateRequest = builders.update().id(complexKey).input(groupMembership).build();
Response<EmptyRecord> updateResponse = getClient().sendRequest(updateRequest).getResponse();
Assert.assertEquals(updateResponse.getStatus(), 204);
groupMembership1 = getClient().sendRequest(request).getResponse().getEntity();
Assert.assertEquals(groupMembership1.getContactEmail(), "alphred@test.linkedin.com");
// Delete and verify
Request<EmptyRecord> deleteRequest = builders.delete().id(complexKey).build();
Response<EmptyRecord> deleteResponse = getClient().sendRequest(deleteRequest).getResponse();
Assert.assertEquals(deleteResponse.getStatus(), 204);
try {
getClient().sendRequest(request).getResponse().getEntity();
} catch (RestLiResponseException e) {
Assert.assertEquals(e.getStatus(), 404);
}
}
Aggregations