/** retrieve album */
 private void getAlbumSummary(PrintWriter respWriter, long newAlbumId)
     throws RemoteInvocationException {
   final Request<Album> getReq = _albumBuilders.get().id(newAlbumId).build();
   final ResponseFuture<Album> getFuture = _restClient.sendRequest(getReq);
   final Response<Album> getResp = getFuture.getResponse();
   respWriter.println("Album: " + getResp.getEntity().toString());
 }
  private void testGetMain(
      RootBuilderWrapper.MethodBuilderWrapper<
              ComplexResourceKey<TyperefRecord, TwoPartKey>, TyperefRecord, TyperefRecord>
          requestBuilder)
      throws RemoteInvocationException {
    final ByteString byteData = ByteString.copy(new byte[] {0, 32, -95});
    Request<TyperefRecord> request = requestBuilder.id(getComplexKey(byteData)).build();
    ResponseFuture<TyperefRecord> future = getClient().sendRequest(request);
    Response<TyperefRecord> response = future.getResponse();

    Assert.assertEquals(response.getEntity().getBytes(), byteData);
  }
  private void partialUpdatePhoto(PrintWriter respWriter, long photoId)
      throws RemoteInvocationException {
    final Request<Photo> getReq = _photoBuilders.get().id(photoId).build();
    final ResponseFuture<Photo> getFuture = _restClient.sendRequest(getReq);
    final Response<Photo> getResp = getFuture.getResponse();
    final Photo originalPhoto = getResp.getEntity();

    final Photo updatedPhoto = new Photo().setTitle("Partially Updated Photo");
    final PatchRequest<Photo> patch = PatchGenerator.diff(originalPhoto, updatedPhoto);

    final Request<EmptyRecord> partialUpdateRequest =
        _photoBuilders.partialUpdate().id(photoId).input(patch).build();
    final int status = _restClient.sendRequest(partialUpdateRequest).getResponse().getStatus();
    respWriter.println("Partial update photo is successful: " + (status == 202));
  }
  private long createPhoto(PrintWriter respWriter) throws RemoteInvocationException {
    // make create photo request and send with the rest client synchronously
    // response of create request does not have body, therefore use EmptyRecord as template

    // create an instance of photo pragmatically
    // this resembles to photo-create.json
    final LatLong newLatLong = new LatLong().setLatitude(37.42394f).setLongitude(-122.0708f);
    final EXIF newExif = new EXIF().setLocation(newLatLong);
    final Photo newPhoto =
        new Photo().setTitle("New Photo").setFormat(PhotoFormats.PNG).setExif(newExif);

    final Request<EmptyRecord> createReq1 = _photoBuilders.create().input(newPhoto).build();
    final ResponseFuture<EmptyRecord> createFuture1 = _restClient.sendRequest(createReq1);
    // Future.getResource() blocks until server responds
    final Response<EmptyRecord> createResp1 = createFuture1.getResponse();

    // HTTP header Location also shows the relative URI of the created resource
    final long newPhotoId = Long.parseLong(createResp1.getId());
    respWriter.println("New photo ID: " + newPhotoId);

    return newPhotoId;
  }
  /** use Finder to find the photo with some criteria */
  private void findPhoto(PrintWriter respWriter) throws RemoteInvocationException {
    final long newPhotoId = createPhoto(respWriter);
    createPhoto(respWriter);
    createPhoto(respWriter);

    final Request<Photo> getReq = _photoBuilders.get().id(newPhotoId).build();
    final ResponseFuture<Photo> getFuture = _restClient.sendRequest(getReq);
    final Response<Photo> getResp = getFuture.getResponse();
    final Photo photo = getResp.getEntity();

    final FindRequest<Photo> findReq =
        _photoBuilders
            .findByTitleAndOrFormat()
            .titleParam(photo.getTitle())
            .formatParam(photo.getFormat())
            .build();

    final CollectionResponse<Photo> crPhotos =
        _restClient.sendRequest(findReq).getResponse().getEntity();
    final List<Photo> photos = crPhotos.getElements();

    respWriter.println("Found " + photos.size() + " photos with title " + photo.getTitle());
  }
 /** failed request that try to access non-existing photo and throw RestException */
 private void getNonPhoto() throws RemoteInvocationException {
   final Request<Photo> failReq = _photoBuilders.get().id(-1L).build();
   final ResponseFuture<Photo> failFuture = _restClient.sendRequest(failReq);
   final Response<Photo> failResponse = failFuture.getResponse();
 }
 /** call action purge to delete all photos on server */
 private void purgeAllPhotos(PrintWriter respWriter) throws RemoteInvocationException {
   final Request<Integer> purgeReq = _photoBuilders.actionPurge().build();
   final ResponseFuture<Integer> purgeFuture = _restClient.sendRequest(purgeReq);
   final Response<Integer> purgeResp = purgeFuture.getResponse();
   respWriter.println("Purged " + purgeResp.getEntity() + " photos");
 }
 /** send request to retrieve created photo */
 private void getPhoto(PrintWriter respWriter, long newPhotoId) throws RemoteInvocationException {
   final Request<Photo> getReq = _photoBuilders.get().id(newPhotoId).build();
   final ResponseFuture<Photo> getFuture = _restClient.sendRequest(getReq);
   final Response<Photo> getResp = getFuture.getResponse();
   respWriter.println("Photo: " + getResp.getEntity().toString());
 }
  /**
   * Retrieve the album information and each photo in the album. The photos are retrieved in
   * parallel.
   */
  private void getAlbum(PrintWriter respWriter, long albumId) throws RemoteInvocationException {
    // get the specific album
    final Request<Album> getAlbumReq = _albumBuilders.get().id(albumId).build();
    final ResponseFuture<Album> getAlbumFuture = _restClient.sendRequest(getAlbumReq);
    final Response<Album> getResp = getAlbumFuture.getResponse();
    final Album album = getResp.getEntity();

    respWriter.println(album.getTitle());
    respWriter.println("Created on " + new Date(album.getCreationTime()));

    // get the album's entries
    final FindRequest<AlbumEntry> searchReq =
        _albumEntryBuilders.findBySearch().albumIdParam(albumId).build();
    final ResponseFuture<CollectionResponse<AlbumEntry>> responseFuture =
        _restClient.sendRequest(searchReq);
    final Response<CollectionResponse<AlbumEntry>> response = responseFuture.getResponse();
    final List<AlbumEntry> entries = new ArrayList<AlbumEntry>(response.getEntity().getElements());

    entries.add(new AlbumEntry().setAlbumId(-1).setPhotoId(9999));

    // don't return until all photo requests done
    final CountDownLatch latch = new CountDownLatch(entries.size());

    // fetch every photo asynchronously
    // store either a photo or an exception
    final Object[] photos = new Object[entries.size()];
    for (int i = 0; i < entries.size(); i++) {
      final int finalI = i; // need final version for callback
      final AlbumEntry entry = entries.get(i);
      final long photoId = entry.getPhotoId();
      final Request<Photo> getPhotoReq = _photoBuilders.get().id(photoId).build();
      _restClient.sendRequest(
          getPhotoReq,
          new Callback<Response<Photo>>() {
            @Override
            public void onSuccess(Response<Photo> result) {
              photos[finalI] = result.getEntity();
              latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
              photos[finalI] = e;
            }
          });
    }

    try {
      // wait for all requests to finish
      latch.await(2, TimeUnit.SECONDS);
      if (latch.getCount() > 0) {
        respWriter.println("Failed to retrieve some photo(s)");
      }
    } catch (InterruptedException e) {
      e.printStackTrace(respWriter);
    }

    // print photo data
    for (int i = 0; i < entries.size(); i++) {
      final Object val = photos[i];
      final AlbumEntry entry = entries.get(i);
      if (val instanceof Throwable) {
        respWriter.println("Failed to load photo " + entry.getPhotoId());
        respWriter.println("Stack trace:");
        ((Throwable) val).printStackTrace(respWriter);
        respWriter.println();
      } else if (val instanceof Photo) {
        final Photo photo = (Photo) val;
        respWriter.println("Photo " + photo.getTitle() + ":");
        respWriter.println(photo);
        respWriter.println("Added on " + new Date(entry.getAddTime()));
      } else {
        throw new AssertionError("expected photo or exception");
      }
    }
  }
Beispiel #10
0
  @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, "[email protected]", "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());
  }