Java Code Examples for com.google.api.client.http.ByteArrayContent#fromString()

The following examples show how to use com.google.api.client.http.ByteArrayContent#fromString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RepositoryDocTest.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testEqualsWithContentHash() {
  AbstractInputStreamContent content = ByteArrayContent.fromString(null, "golden");
  RepositoryDoc doc1 =
      new RepositoryDoc.Builder()
          .setItem(new Item().setName("id1"))
          .setContent(content, Integer.toString(Objects.hash(content)), ContentFormat.TEXT)
          .build();
  RepositoryDoc doc2 =
      new RepositoryDoc.Builder()
          .setItem(new Item().setName("id1"))
          .setContent(content, Integer.toString(Objects.hash(content)), ContentFormat.TEXT)
          .build();
  assertEquals(doc1, doc2);
  assertEquals(doc1.hashCode(), doc2.hashCode());
}
 
Example 2
Source File: RepositoryDocTest.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotEqualsWithContentHash() {
  AbstractInputStreamContent content = ByteArrayContent.fromString(null, "golden");
  RepositoryDoc doc1 =
      new RepositoryDoc.Builder()
          .setItem(new Item().setName("id1"))
          .setContent(content, Integer.toString(Objects.hash(content)), ContentFormat.TEXT)
          .build();
  RepositoryDoc doc2 =
      new RepositoryDoc.Builder()
          .setItem(new Item().setName("id1"))
          .setContent(content, ContentFormat.TEXT)
          .build();
  assertNotEquals(doc1, doc2);
  assertNotEquals(doc1.hashCode(), doc2.hashCode());
}
 
Example 3
Source File: RepositoryDocTest.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotEqualsWithContent() {
  AbstractInputStreamContent content1 = ByteArrayContent.fromString(null, "golden");
  RepositoryDoc doc1 =
      new RepositoryDoc.Builder()
          .setItem(new Item().setName("id1"))
          .setContent(content1, ContentFormat.TEXT)
          .build();
  AbstractInputStreamContent content2 = ByteArrayContent.fromString(null, "golden2");
  RepositoryDoc doc2 =
      new RepositoryDoc.Builder()
          .setItem(new Item().setName("id1"))
          .setContent(content2, ContentFormat.TEXT)
          .build();
  assertNotEquals(doc1, doc2);
  assertNotEquals(doc1.hashCode(), doc2.hashCode());
}
 
Example 4
Source File: IndexingServiceTest.java    From connector-sdk with Apache License 2.0 6 votes vote down vote up
@Test
public void indexItemAndContent_withContentHash() throws Exception {
  Item item = new Item().setName(GOOD_ID);
  ByteArrayContent content = ByteArrayContent.fromString("text/plain", "Hello World.");
  String hash = Integer.toString(Objects.hash(content));

  this.indexingService.indexItemAndContent(
      item, content, hash, ContentFormat.TEXT, RequestMode.ASYNCHRONOUS);

  assertEquals(
      new ItemContent()
          .encodeInlineContent("Hello World.".getBytes(UTF_8))
          .setHash(hash)
          .setContentFormat("TEXT"),
      item.getContent());
  verify(quotaServer).acquire(Operations.DEFAULT);
}
 
Example 5
Source File: FullTraversalSample.java    From cloud-search-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a document for indexing.
 *
 * For this connector sample, the created document is domain public
 *  searchable. The content is a simple text string.
 *
 * @param id unique local id for the document
 * @return the fully formed document ready for indexing
 */
private ApiOperation buildDocument(int id) {
  // [START cloud_search_content_sdk_domain_acl]
  // Make the document publicly readable within the domain
  Acl acl = new Acl.Builder()
      .setReaders(Collections.singletonList(Acl.getCustomerPrincipal()))
      .build();
  // [END cloud_search_content_sdk_domain_acl]

  // [START cloud_search_content_sdk_build_item]
  // Url is required. Use google.com as a placeholder for this sample.
  String viewUrl = "https://www.google.com";

  // Version is required, set to current timestamp.
  byte[] version = Longs.toByteArray(System.currentTimeMillis());

  // Using the SDK item builder class to create the document with appropriate attributes
  // (this can be expanded to include metadata fields etc.)
  Item item = IndexingItemBuilder.fromConfiguration(Integer.toString(id))
      .setItemType(IndexingItemBuilder.ItemType.CONTENT_ITEM)
      .setAcl(acl)
      .setSourceRepositoryUrl(IndexingItemBuilder.FieldOrValue.withValue(viewUrl))
      .setVersion(version)
      .build();
  // [END cloud_search_content_sdk_build_item]

  // [START cloud_search_content_sdk_build_repository_doc]
  // For this sample, content is just plain text
  String content = String.format("Hello world from sample doc %d", id);
  ByteArrayContent byteContent = ByteArrayContent.fromString("text/plain", content);

  // Create the fully formed document
  RepositoryDoc doc = new RepositoryDoc.Builder()
      .setItem(item)
      .setContent(byteContent, IndexingService.ContentFormat.TEXT)
      .build();
  // [END cloud_search_content_sdk_build_repository_doc]
  return doc;
}
 
Example 6
Source File: FakeIndexingRepository.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
private RepositoryDoc getItem(MockItem createItem) {
  AbstractInputStreamContent content =
      ByteArrayContent.fromString("", createItem.getItem().toString());
  return new RepositoryDoc.Builder()
      .setItem(createItem.getItem())
      .setContent(content, ContentFormat.RAW)
      .build();
}
 
Example 7
Source File: RepositoryDocTest.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void testItemAndContentNotIncrement() throws IOException, InterruptedException {
  Item item = new Item().setName("id1").setAcl(getCustomerAcl());
  AbstractInputStreamContent content = ByteArrayContent.fromString("", "golden");
  RepositoryDoc doc =
      new RepositoryDoc.Builder()
          .setItem(item)
          .setContent(content, ContentFormat.TEXT)
          .setRequestMode(RequestMode.ASYNCHRONOUS)
          .build();
  SettableFuture<Item> updateFuture = SettableFuture.create();

  doAnswer(
          invocation -> {
            updateFuture.set(new Item());
            return updateFuture;
          })
      .when(mockIndexingService)
      .indexItemAndContent(
          any(), any(), any(), eq(ContentFormat.TEXT), eq(RequestMode.ASYNCHRONOUS));

  doc.execute(mockIndexingService);

  InOrder inOrder = inOrder(mockIndexingService);
  inOrder
      .verify(mockIndexingService)
      .indexItemAndContent(item, content, null, ContentFormat.TEXT, RequestMode.ASYNCHRONOUS);
}
 
Example 8
Source File: SendFusionTablesAsyncTask.java    From mytracks with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new row in Google Fusion Tables representing the track as a line
 * segment.
 * 
 * @param fusiontables fusion tables
 * @param tableId the table id
 * @param track the track
 */
private void createNewLineString(Fusiontables fusiontables, String tableId, Track track)
    throws IOException {
  String values = SendFusionTablesUtils.formatSqlValues(track.getName(), track.getDescription(),
      SendFusionTablesUtils.getKmlLineString(track.getLocations()));
  String sql = "INSERT INTO " + tableId + " (name,description,geometry) VALUES " + values;
  HttpContent content = ByteArrayContent.fromString(null, "sql=" + sql);
  GoogleUrl url = new GoogleUrl("https://www.googleapis.com/fusiontables/v1/query");
  fusiontables.getRequestFactory().buildPostRequest(url, content).execute();
}
 
Example 9
Source File: IndexingServiceTest.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void indexItemAndContent_nullItemId_throwsException() throws IOException {
  Item item = new Item();
  ByteArrayContent content = ByteArrayContent.fromString("text/plain", "Hello World.");
  thrown.expect(IllegalArgumentException.class);
  this.indexingService.indexItemAndContent(
      item, content, null, ContentFormat.TEXT, RequestMode.SYNCHRONOUS);
}
 
Example 10
Source File: IndexingServiceTest.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void indexItemAndContent_apiError_throwsException() throws Exception {
  when(batchingService.indexItem(any())).thenReturn(getExceptionFuture(HTTP_FORBIDDEN_ERROR));
  Item item = new Item().setName(ERROR_ID);
  ByteArrayContent content = ByteArrayContent.fromString("text/plain", "Hello World.");

  try {
    indexingService
        .indexItemAndContent(item, content, null, ContentFormat.TEXT, RequestMode.SYNCHRONOUS)
        .get();
  } catch (ExecutionException e) {
    validateApiError(e, HTTP_FORBIDDEN);
  }
}
 
Example 11
Source File: IndexingServiceTest.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
@Test
public void indexItemAndContent_emptyContentIsInlined() throws Exception {
  Item item = new Item().setName(GOOD_ID);
  ByteArrayContent content = ByteArrayContent.fromString("text/plain", "");

  this.indexingService.indexItemAndContent(
      item, content, null, ContentFormat.TEXT, RequestMode.ASYNCHRONOUS);

  assertEquals(
      new ItemContent().encodeInlineContent(new byte[0]).setContentFormat("TEXT"),
      item.getContent());
  verify(quotaServer).acquire(Operations.DEFAULT);
}
 
Example 12
Source File: QuickstartSdkListingChangeDetection.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a document for indexing.
 *
 * <p>For this connector sample, the created document is domain public searchable. The content
 * is a simple text string.
 *
 * <p>If this were a hierarchical data repository, this method would also use the {@link
 * RepositoryDoc.Builder#addChildId(String, PushItem)} method to push each document's children
 * to the Cloud Search queue. Each child ID would be processed in turn during a future {@link
 * #getDoc(Item)} call.
 *
 * @param id unique id for the document
 * @return the fully formed document ready for indexing
 */
RepositoryDoc createDoc(String id) {
  // the document ACL is required: for this sample, the document is publicly readable within the
  // data source domain
  Acl acl = new Acl.Builder()
      .setReaders(Collections.singletonList(Acl.getCustomerPrincipal())).build();

  // the document view URL is required: for this sample, just using a generic URL search link
  String viewUrl = "https://www.google.com";

  // using the SDK item builder class to create the document with appropriate attributes
  // (this can be expanded to include metadata fields on so on)
  Item item = new IndexingItemBuilder(id)
      .setItemType(ItemType.CONTENT_ITEM)
      .setAcl(acl)
      .setSourceRepositoryUrl(IndexingItemBuilder.FieldOrValue.withValue(viewUrl))
      // The document version is also required. For this sample, we use the SDK default which
      // just uses a current timestamp. If your data repository has a meaningful sense of
      // version, set it here.
      // .setVersion(documentManager.getVersion(id))
      .build();

  // for this sample, content is just plain text
  String content = documentManager.getContent(id);
  ByteArrayContent byteContent = ByteArrayContent.fromString("text/plain", content);

  // create the fully formed document
  RepositoryDoc document = new RepositoryDoc.Builder()
      .setItem(item)
      .setContent(byteContent, ContentFormat.TEXT)
      .build();

  return document;
}
 
Example 13
Source File: QuickstartSdkConnector.java    From connector-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a document for indexing.
 *
 * <p>For this connector sample, the created document is domain public searchable. The content
 * is a simple text string.
 *
 * @param id unique id for the document
 * @param content document's searchable content
 * @return the fully formed document ready for indexing
 */
private RepositoryDoc createDoc(String id, String content) {
  // the document ACL is required: for this sample, the document is publicly readable within the
  // data source domain
  Acl acl = new Acl.Builder()
      .setReaders(Collections.singletonList(Acl.getCustomerPrincipal())).build();

  // the document view URL is required: for this sample, just using a generic URL search link
  String viewUrl = "www.google.com";

  // the document version is required: for this sample, just using a current timestamp
  byte[] version = Long.toString(System.currentTimeMillis()).getBytes();

  // using the SDK item builder class to create the document with appropriate attributes
  // (this can be expanded to include metadata fields etc.)
  Item item = new IndexingItemBuilder(id)
      .setItemType(ItemType.CONTENT_ITEM)
      .setAcl(acl)
      .setSourceRepositoryUrl(IndexingItemBuilder.FieldOrValue.withValue(viewUrl))
      .setVersion(version)
      .build();

  // for this sample, content is just plain text
  ByteArrayContent byteContent = ByteArrayContent.fromString("text/plain", content);

  // create the fully formed document
  RepositoryDoc document = new RepositoryDoc.Builder()
      .setItem(item)
      .setContent(byteContent, ContentFormat.TEXT)
      .build();

  return document;
}
 
Example 14
Source File: ListTraversalSample.java    From cloud-search-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a document for indexing.
 * <p>
 * For this connector sample, the created document is domain public
 * searchable. The content is a simple text string.
 *
 * @param documentId unique local id for the document
 * @return the fully formed document ready for indexing
 */
private ApiOperation buildDocument(int documentId) {
  // [START cloud_search_content_sdk_domain_acl]
  // Make the document publicly readable within the domain
  Acl acl = new Acl.Builder()
      .setReaders(Collections.singletonList(Acl.getCustomerPrincipal()))
      .build();
  // [END cloud_search_content_sdk_domain_acl]

  // [START cloud_search_content_sdk_build_item]
  // Url is required. Use google.com as a placeholder for this sample.
  String viewUrl = "https://www.google.com";

  // Version is required, set to current timestamp.
  byte[] version = Longs.toByteArray(System.currentTimeMillis());

  // Set metadata hash so queue can detect changes
  String metadataHash = this.calculateMetadataHash(documentId);

  // Using the SDK item builder class to create the document with
  // appropriate attributes. This can be expanded to include metadata
  // fields etc.
  Item item = IndexingItemBuilder.fromConfiguration(Integer.toString(documentId))
      .setItemType(IndexingItemBuilder.ItemType.CONTENT_ITEM)
      .setAcl(acl)
      .setSourceRepositoryUrl(IndexingItemBuilder.FieldOrValue.withValue(viewUrl))
      .setVersion(version)
      .setHash(metadataHash)
      .build();
  // [END cloud_search_content_sdk_build_item]

  // [START cloud_search_content_sdk_build_repository_doc]
  // For this sample, content is just plain text
  String content = String.format("Hello world from sample doc %d", documentId);
  ByteArrayContent byteContent = ByteArrayContent.fromString("text/plain", content);

  // Create the fully formed document
  RepositoryDoc doc = new RepositoryDoc.Builder()
      .setItem(item)
      .setContent(byteContent, IndexingService.ContentFormat.TEXT)
      .build();
  // [END cloud_search_content_sdk_build_repository_doc]
  return doc;
}
 
Example 15
Source File: FullTraversalConnectorTest.java    From connector-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testTraverseSkipHandlerSucceed() throws Exception {
  setConfig("3", DefaultAclChoices.PUBLIC);
  FullTraversalConnector connector =
      new FullTraversalConnector(repositoryMock, checkpointHandlerMock);
  connector.init(connectorContextMock);
  List<ApiOperation> docs = new ArrayList<>();
  List<Item> items = new ArrayList<>();
  List<ByteArrayContent> contents = new ArrayList<>();
  for (int i = 1; i <= 10; i++) {

    Item item = new Item().setName("Test id" + i);
    items.add(item);
    ByteArrayContent byteArrayContent =
        ByteArrayContent.fromString("text/html", "Test " + "content" + i);
    RepositoryDoc doc =
        new RepositoryDoc.Builder()
            .setItem(item)
            .setContent(byteArrayContent, ContentFormat.HTML)
            .build();
    contents.add(byteArrayContent);
    docs.add(doc);
  }
  CheckpointCloseableIterable<ApiOperation> opIterableSpy =
      Mockito.spy(new CheckpointCloseableIterableImpl.Builder<>(docs).build());
  when(repositoryMock.getAllDocs(null)).thenReturn(opIterableSpy);

  for (int i = 0; i < 3; i++) {
    SettableFuture<Item> exceptionFuture = SettableFuture.create();
    StringBuilder sb = new StringBuilder();
    sb.append("Test exception");
    sb.append(i + 1);
    doAnswer(
            invocation -> {
              exceptionFuture.setException(new IOException(sb.toString()));
              return exceptionFuture;
            })
        .when(indexingServiceMock)
        .indexItemAndContent(
            eq(items.get(i)),
            eq(contents.get(i)),
            eq(null),
            eq(ContentFormat.HTML),
            eq(RequestMode.UNSPECIFIED));
  }

  for (int i = 3; i < 10; i++) {
    SettableFuture<Item> updateFuture = SettableFuture.create();
    doAnswer(
            invocation -> {
              updateFuture.set(new Item());
              return updateFuture;
            })
        .when(indexingServiceMock)
        .indexItemAndContent(
            eq(items.get(i)),
            eq(contents.get(i)),
            eq(null),
            eq(ContentFormat.HTML),
            eq(RequestMode.UNSPECIFIED));
  }
  SettableFuture<Operation> deleteQueueFuture = SettableFuture.create();
  deleteQueueFuture.set(new Operation().setDone(true));
  when(indexingServiceMock.deleteQueueItems(any())).thenReturn(deleteQueueFuture);

  connector.traverse();
  verify(opIterableSpy).close();
  verify(indexingServiceMock, times(10))
      .indexItemAndContent(any(), any(), any(), eq(ContentFormat.HTML), any());
}
 
Example 16
Source File: FullTraversalConnectorTest.java    From connector-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testTraverseSkipHandlerAbort() throws Exception {
  setConfig("3", DefaultAclChoices.PUBLIC);
  FullTraversalConnector connector =
      new FullTraversalConnector(repositoryMock, checkpointHandlerMock);
  connector.init(connectorContextMock);
  List<ApiOperation> docs = new ArrayList<>();
  List<Item> items = new ArrayList<>();
  List<ByteArrayContent> contents = new ArrayList<>();
  for (int i = 1; i <= 4; i++) {
    Item item = new Item().setName("Test id" + i);
    ByteArrayContent content = ByteArrayContent.fromString("text/html", "Test content" + i);
    RepositoryDoc doc =
        new RepositoryDoc.Builder().setItem(item).setContent(content, ContentFormat.HTML).build();
    docs.add(doc);
    items.add(item);
    contents.add(content);
  }
  when(repositoryMock.getAllDocs(null))
      .thenReturn(new CheckpointCloseableIterableImpl.Builder<>(docs).build());

  for (int i = 0; i < 4; i++) {
    SettableFuture<Item> updateFuture = SettableFuture.create();
    StringBuilder sb = new StringBuilder();
    sb.append("Test exception");
    sb.append(i + 1);
    doAnswer(
            invocation -> {
              updateFuture.setException(new IOException(sb.toString()));
              return updateFuture;
            })
        .when(indexingServiceMock)
        .indexItemAndContent(
            eq(items.get(i)),
            eq(contents.get(i)),
            eq(null),
            eq(ContentFormat.HTML),
            eq(RequestMode.UNSPECIFIED));
  }
  thrown.expect(IOException.class);
  thrown.expectMessage(containsString("Test exception"));
  connector.traverse();
}
 
Example 17
Source File: FullTraversalConnectorTest.java    From connector-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testTraverseSkipHandlerAbortWithDeleteItem() throws Exception {
  setConfig("3", DefaultAclChoices.PUBLIC);
  FullTraversalConnector connector =
      new FullTraversalConnector(repositoryMock, checkpointHandlerMock);
  connector.init(connectorContextMock);
  List<ApiOperation> docs = new ArrayList<>();
  List<Item> items = new ArrayList<>();
  List<ByteArrayContent> contents = new ArrayList<>();
  // Delete[fail], Update[fail], Update[success], Delete[fail], Update[fail], Update[success]
  for (int i = 0; i < 6; i++) { // extra docs may not be fetched
    if ((i % 3) == 0) {
      docs.add(ApiOperations.deleteItem("Delete id"));
      items.add(new Item());
      contents.add(new ByteArrayContent("test", new byte[] {}));
    } else {
      Item item = new Item().setName("Test id" + i);
      items.add(item);
      ByteArrayContent content = ByteArrayContent.fromString("text/html", "Test content" + i);
      contents.add(content);
      ApiOperation doc =
          new RepositoryDoc.Builder()
              .setItem(item)
              .setContent(content, ContentFormat.HTML)
              .build();
      docs.add(doc);
    }
  }
  when(repositoryMock.getAllDocs(null))
      .thenReturn(new CheckpointCloseableIterableImpl.Builder<>(docs).build());

  for (int i = 0; i < 6; i++) {
    final int ii = i;
    if ((i % 3) == 0) {
      SettableFuture<Operation> deleteFuture = SettableFuture.create();
      doAnswer(
              invocation -> {
                deleteFuture.setException(new IOException("Test exception" + ii));
                return deleteFuture;
              })
          .when(indexingServiceMock)
          .deleteItem("Delete id", null, RequestMode.UNSPECIFIED);
    } else {
      SettableFuture<Item> updateFuture = SettableFuture.create();
      doAnswer(
              invocation -> {
                if ((ii % 3) == 1) {
                  updateFuture.setException(new IOException("Test exception" + ii));
                } else {
                  updateFuture.set(new Item());
                }
                return updateFuture;
              })
          .when(indexingServiceMock)
          .indexItemAndContent(
              items.get(i),
              contents.get(i),
              null,
              ContentFormat.HTML,
              RequestMode.UNSPECIFIED);
    }
  }

  thrown.expect(IOException.class);
  thrown.expectMessage(containsString("Test exception"));
  connector.traverse();
}
 
Example 18
Source File: FullTraversalConnectorTest.java    From connector-sdk with Apache License 2.0 4 votes vote down vote up
@Test
public void testChangeHandlerPublicAcl() throws Exception {
  // target operations
  final int numberOfItems = 5;
  Map<String, String> targetItems = new HashMap<>();
  List<ApiOperation> docs = new ArrayList<>();

  for (int i = 0; i < numberOfItems; i++) {
    String id = "Test" + i;
    String content = "Test content " + i;
    targetItems.put(id, content);
    Item item = new Item();
    item.setName(id);
    ByteArrayContent byteArrayContent = ByteArrayContent.fromString("text/html", content);
    docs.add(
        new RepositoryDoc.Builder()
            .setItem(item)
            .setRequestMode(RequestMode.SYNCHRONOUS)
            .setContent(byteArrayContent, ContentFormat.HTML)
            .build());

    SettableFuture<Item> updateFuture = SettableFuture.create();
    doAnswer(
            invocation -> {
              updateFuture.set(new Item());
              return updateFuture;
            })
        .when(indexingServiceMock)
        .indexItemAndContent(
            eq(item),
            eq(byteArrayContent),
            eq(null),
            eq(ContentFormat.HTML),
            eq(RequestMode.SYNCHRONOUS));
  }

  docs.add(ApiOperations.deleteItem("delete id"));
  SettableFuture<Operation> deleteFuture = SettableFuture.create();
  deleteFuture.set(new Operation());
  doAnswer(invocation -> deleteFuture)
      .when(indexingServiceMock)
      .deleteItem("delete id", null, RequestMode.UNSPECIFIED);

  byte[] payload = "Payload Value".getBytes();
  byte[] newPayload = "New Payload Value".getBytes();
  when(checkpointHandlerMock
      .readCheckpoint(FullTraversalConnector.CHECKPOINT_INCREMENTAL))
      .thenReturn(payload);
  CheckpointCloseableIterable<ApiOperation> incrementalChanges =
      new CheckpointCloseableIterableImpl.Builder<>(docs).setCheckpoint(newPayload).build();
  when(repositoryMock.getChanges(any())).thenReturn(incrementalChanges);

  // incremental handler test
  FullTraversalConnector connector =
      new FullTraversalConnector(repositoryMock, checkpointHandlerMock);
  setConfig("0", DefaultAclChoices.PUBLIC);
  connector.init(connectorContextMock);
  connector.handleIncrementalChanges();

  // verify
  InOrder inOrderCheck = inOrder(indexingServiceMock, checkpointHandlerMock);
  // one get for the check point entry
  inOrderCheck.verify(checkpointHandlerMock)
      .readCheckpoint(FullTraversalConnector.CHECKPOINT_QUEUE);
  inOrderCheck.verify(checkpointHandlerMock)
      .readCheckpoint(FullTraversalConnector.CHECKPOINT_INCREMENTAL);
  // one update item with content for each of five targetItems in the db
  inOrderCheck
      .verify(indexingServiceMock, times(numberOfItems))
      .indexItemAndContent(
          itemListCaptor.capture(),
          contentCaptor.capture(),
          eq(null),
          eq(ContentFormat.HTML),
          eq(RequestMode.SYNCHRONOUS));
  // one delete operation
  verify(indexingServiceMock).deleteItem("delete id", null, RequestMode.UNSPECIFIED);
  inOrderCheck.verify(checkpointHandlerMock)
      .saveCheckpoint(FullTraversalConnector.CHECKPOINT_INCREMENTAL, newPayload);
  // verify objects in parameters
  List<Item> allItems = itemListCaptor.getAllValues();
  List<ByteArrayContent> allContent = contentCaptor.getAllValues();
  assertEquals(allItems.size(), numberOfItems);
  for (int i = 0; i < numberOfItems; i++) {
    assertThat(targetItems.keySet(), hasItem(allItems.get(i).getName())); // verify item
    String html =
        CharStreams.toString(
            new InputStreamReader(allContent.get(i).getInputStream(), UTF_8));
    assertEquals(targetItems.get(allItems.get(i).getName()), html); // verify content
    assertEquals(DOMAIN_PUBLIC_ACL, allItems.get(i).getAcl());
  }
  verifyNoMoreInteractions(indexingServiceMock, checkpointHandlerMock);
}
 
Example 19
Source File: GraphTraversalSample.java    From cloud-search-samples with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a document for indexing.
 * <p>
 * For this connector sample, the created document is domain public
 * searchable. The content is a simple text string. In addition to indexing
 * the current document, the child nodes are also pushed into the queue.
 * This method will later be called for the child nodes as they're pulled
 * from the queue.
 *
 * @param documentId unique local id for the document
 * @return the fully formed document ready for indexing
 */
private ApiOperation buildDocumentAndChildren(String documentId) {
  // [START cloud_search_content_sdk_domain_acl]
  // Make the document publicly readable within the domain
  Acl acl = new Acl.Builder()
      .setReaders(Collections.singletonList(Acl.getCustomerPrincipal()))
      .build();
  // [END cloud_search_content_sdk_domain_acl]


  // [START cloud_search_content_sdk_build_item]
  // Url is required. Use google.com as a placeholder for this sample.
  String viewUrl = "https://www.google.com";

  // Version is required, set to current timestamp.
  byte[] version = Longs.toByteArray(System.currentTimeMillis());

  // Using the SDK item builder class to create the document with
  // appropriate attributes. This can be expanded to include metadata
  // fields etc.
  Item item = IndexingItemBuilder.fromConfiguration(documentId)
      .setItemType(IndexingItemBuilder.ItemType.CONTENT_ITEM)
      .setAcl(acl)
      .setSourceRepositoryUrl(IndexingItemBuilder.FieldOrValue.withValue(viewUrl))
      .setVersion(version)
      .build();
  // [END cloud_search_content_sdk_build_item]

  // [START cloud_search_content_sdk_build_repository_doc]
  // For this sample, content is just plain text
  String content = String.format("Hello world from sample doc %s", documentId);
  ByteArrayContent byteContent = ByteArrayContent.fromString("text/plain", content);

  RepositoryDoc.Builder docBuilder = new RepositoryDoc.Builder()
      .setItem(item)
      .setContent(byteContent, IndexingService.ContentFormat.TEXT);
  // [END cloud_search_content_sdk_build_repository_doc]

  // [START cloud_search_content_sdk_add_children]
  // Queue the child nodes to visit after indexing this document
  Set<String> childIds = getChildItemNames(documentId);
  for (String id : childIds) {
    log.info(() -> String.format("Pushing child node %s", id));
    PushItem pushItem = new PushItem();
    docBuilder.addChildId(id, pushItem);
  }

  RepositoryDoc doc = docBuilder.build();
  // [END cloud_search_content_sdk_add_children]
  return doc;
}
 
Example 20
Source File: HttpEventPublisher.java    From beam with Apache License 2.0 3 votes vote down vote up
/**
 * Marshals a list of {@link SplunkEvent}s into an {@link HttpContent} object that can be used to
 * create an {@link HttpRequest}.
 *
 * @param events list of {@link SplunkEvent}s
 * @return {@link HttpContent} that can be used to create an {@link HttpRequest}.
 */
@VisibleForTesting
HttpContent getContent(List<SplunkEvent> events) {
  String payload = getStringPayload(events);
  LOG.debug("Payload content: {}", payload);
  return ByteArrayContent.fromString(CONTENT_TYPE, payload);
}