Search in sources :

Example 1 with GroupMembershipParam

use of com.linkedin.restli.examples.groups.api.GroupMembershipParam 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, GroupMembershipParam>(groupMembershipKey, new GroupMembershipParam());
    groupMembership.setId(complexResourceKey.getKey());
    _app.getMembershipMgr().save(toGroupMembership(groupMembership));
    return new CreateResponse(complexResourceKey, HttpStatus.S_201_CREATED);
}
Also used : GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) GroupMembershipKey(com.linkedin.restli.examples.groups.api.GroupMembershipKey) CreateResponse(com.linkedin.restli.server.CreateResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey)

Example 2 with GroupMembershipParam

use of com.linkedin.restli.examples.groups.api.GroupMembershipParam 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);
}
Also used : GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) HashMap(java.util.HashMap) GroupMembershipKey(com.linkedin.restli.examples.groups.api.GroupMembershipKey) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ComplexKeyGroupMembership(com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership) BatchResult(com.linkedin.restli.server.BatchResult)

Example 3 with GroupMembershipParam

use of com.linkedin.restli.examples.groups.api.GroupMembershipParam in project rest.li by linkedin.

the class TestGroupsClient method testCollectionCreateGetUpdateDeleteId.

@Test(dataProvider = com.linkedin.restli.internal.common.TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "requestOptionsDataProvider")
public void testCollectionCreateGetUpdateDeleteId(RestliRequestOptions requestOptions) throws RemoteInvocationException {
    // find with optional params
    Group group = new Group();
    String name = "test";
    int memberID = 1;
    group.setName(name);
    group.setOwner(buildGroupMembership(memberID, "a@a.a", "f", "l"));
    GroupMembershipParam param = new GroupMembershipParam();
    param.setIntParameter(1);
    param.setStringParameter("String");
    final GroupsRequestBuilders groupBuilders = new GroupsRequestBuilders(requestOptions);
    final GroupMembershipsRequestBuilders membershipBuilders = new GroupMembershipsRequestBuilders(requestOptions);
    // Create
    Response<IdResponse<Integer>> response = getClient().sendRequest(groupBuilders.create().input(group).build()).getResponse();
    Assert.assertEquals(response.getStatus(), 201);
    Integer createdId = response.getEntity().getId();
    Assert.assertNotNull(createdId);
    @SuppressWarnings("deprecation") String stringId = response.getId();
    Assert.assertEquals(createdId.intValue(), Integer.parseInt(stringId));
    // Get newly created group and verify name
    Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse().getEntity().getName(), name);
    // Partial update - change name
    String newName = "new name";
    group.setName(newName);
    PatchRequest<Group> patch = PatchGenerator.diffEmpty(group);
    ResponseFuture<EmptyRecord> responseFuture = getClient().sendRequest(groupBuilders.partialUpdate().id(createdId).input(patch).build());
    Assert.assertEquals(204, responseFuture.getResponse().getStatus());
    // Get updated group and verify name
    Assert.assertEquals(getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse().getEntity().getName(), newName);
    // Delete
    responseFuture = getClient().sendRequest(groupBuilders.delete().id(createdId).build());
    Assert.assertEquals(204, responseFuture.getResponse().getStatus());
    // Verify deleted
    try {
        getClient().sendRequest(groupBuilders.get().id(createdId).build()).getResponse();
        Assert.fail("Expected RestLiResponseException");
    } catch (RestLiResponseException e) {
        Assert.assertEquals(e.getStatus(), 404);
    }
    // Cleanup - delete the owner's membership that was created along with the group
    responseFuture = getClient().sendRequest(membershipBuilders.delete().id(buildCompoundKey(memberID, createdId)).build());
    Assert.assertEquals(204, responseFuture.getResponse().getStatus());
}
Also used : Group(com.linkedin.restli.examples.groups.api.Group) EmptyRecord(com.linkedin.restli.common.EmptyRecord) IdResponse(com.linkedin.restli.common.IdResponse) GroupMembershipsRequestBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsRequestBuilders) GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) GroupsRequestBuilders(com.linkedin.restli.examples.groups.client.GroupsRequestBuilders) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) Test(org.testng.annotations.Test)

Example 4 with GroupMembershipParam

use of com.linkedin.restli.examples.groups.api.GroupMembershipParam 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);
}
Also used : RestliRequestOptions(com.linkedin.restli.client.RestliRequestOptions) GroupMembershipsComplexRequestBuilders(com.linkedin.restli.examples.groups.client.GroupMembershipsComplexRequestBuilders) GroupMembershipKey(com.linkedin.restli.examples.groups.api.GroupMembershipKey) CollectionResponse(com.linkedin.restli.common.CollectionResponse) BatchKVResponse(com.linkedin.restli.client.response.BatchKVResponse) GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) EntityResponse(com.linkedin.restli.common.EntityResponse) ComplexResourceKey(com.linkedin.restli.common.ComplexResourceKey) ComplexKeyGroupMembership(com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership) Test(org.testng.annotations.Test)

Example 5 with GroupMembershipParam

use of com.linkedin.restli.examples.groups.api.GroupMembershipParam 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);
    }
}
Also used : EmptyRecord(com.linkedin.restli.common.EmptyRecord) GroupMembershipParam(com.linkedin.restli.examples.groups.api.GroupMembershipParam) GroupMembershipKey(com.linkedin.restli.examples.groups.api.GroupMembershipKey) ComplexKeyGroupMembership(com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership) RestLiResponseException(com.linkedin.restli.client.RestLiResponseException) GroupMembershipQueryParamArray(com.linkedin.restli.examples.groups.api.GroupMembershipQueryParamArray) GroupMembershipQueryParam(com.linkedin.restli.examples.groups.api.GroupMembershipQueryParam) Test(org.testng.annotations.Test)

Aggregations

GroupMembershipParam (com.linkedin.restli.examples.groups.api.GroupMembershipParam)9 Test (org.testng.annotations.Test)6 GroupMembershipKey (com.linkedin.restli.examples.groups.api.GroupMembershipKey)5 RestLiResponseException (com.linkedin.restli.client.RestLiResponseException)4 ComplexResourceKey (com.linkedin.restli.common.ComplexResourceKey)4 EmptyRecord (com.linkedin.restli.common.EmptyRecord)3 ComplexKeyGroupMembership (com.linkedin.restli.examples.groups.api.ComplexKeyGroupMembership)3 IdResponse (com.linkedin.restli.common.IdResponse)2 Group (com.linkedin.restli.examples.groups.api.Group)2 GroupMembershipsRequestBuilders (com.linkedin.restli.examples.groups.client.GroupMembershipsRequestBuilders)2 GroupsRequestBuilders (com.linkedin.restli.examples.groups.client.GroupsRequestBuilders)2 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)2 RestliRequestOptions (com.linkedin.restli.client.RestliRequestOptions)1 BatchKVResponse (com.linkedin.restli.client.response.BatchKVResponse)1 CollectionResponse (com.linkedin.restli.common.CollectionResponse)1 EntityResponse (com.linkedin.restli.common.EntityResponse)1 GroupMembershipParamArray (com.linkedin.restli.examples.groups.api.GroupMembershipParamArray)1 GroupMembershipQueryParam (com.linkedin.restli.examples.groups.api.GroupMembershipQueryParam)1 GroupMembershipQueryParamArray (com.linkedin.restli.examples.groups.api.GroupMembershipQueryParamArray)1 GroupMembershipsComplexRequestBuilders (com.linkedin.restli.examples.groups.client.GroupMembershipsComplexRequestBuilders)1