Java Code Examples for com.google.api.services.drive.model.File#setId()

The following examples show how to use com.google.api.services.drive.model.File#setId() . 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: GoogleDriveGetRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunAtDriverWithPath() throws Exception {
    String qA = "name='A' and 'root' in parents and mimeType='application/vnd.google-apps.folder'";
    String qB = "name='B' and 'A' in parents and mimeType='application/vnd.google-apps.folder'";
    String qC = "name='C' and 'B' in parents and mimeType='application/vnd.google-apps.folder'";
    File file = new File();
    file.setId("fileName-id");
    file.setMimeType(GoogleDriveMimeTypes.MIME_TYPE_GOOGLE_DOCUMENT);

    when(drive.files().list().setQ(qA).execute()).thenReturn(createFolderFileList("A", false));
    when(drive.files().list().setQ(qB).execute()).thenReturn(createFolderFileList("B", false));
    when(drive.files().list().setQ(qC).execute()).thenReturn(createFolderFileList("C", false));
    when(drive.files().get(anyString()).setFields(anyString()).execute()).thenReturn(file);
    //
    properties.file.setValue("/A/B/C");
    testRuntime.initialize(container, properties);
    testRuntime.runAtDriver(container);
    assertEquals("C", container.getComponentData(TEST_CONTAINER, getStudioName(GoogleDriveGetDefinition.RETURN_FILE_ID)));
    assertNull(container.getComponentData(TEST_CONTAINER, getStudioName(GoogleDriveGetDefinition.RETURN_CONTENT)));
}
 
Example 2
Source File: GoogleDriveCreateRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
@Test(expected = ComponentException.class)
public void testManyResourcesMatching() throws Exception {
    FileList flA = new FileList();
    List<File> fs = new ArrayList<>();
    File fA = new File();
    fA.setId("A");
    fs.add(fA);
    File fAp = new File();
    fAp.setId("A");
    fs.add(fAp);
    flA.setFiles(fs);
    when(drive.files().list().setQ(eq(qA)).execute()).thenReturn(flA);

    properties.parentFolder.setValue("/A");
    testRuntime.initialize(container, properties);
    testRuntime.runAtDriver(container);
    fail("Should not be here");
}
 
Example 3
Source File: GoogleDrivePutRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunAtDriverOverwriteError() throws Exception {
    FileList hasfilelist = new FileList();
    List<File> hfiles = new ArrayList<>();
    File hfile = new File();
    hfile.setId(FILE_PUT_NAME);
    hfiles.add(hfile);
    hasfilelist.setFiles(hfiles);
    when(drive.files().list().setQ(anyString()).execute()).thenReturn(hasfilelist);
    properties.overwrite.setValue(false);
    testRuntime.initialize(container, properties);
    try {
        testRuntime.runAtDriver(container);
        fail("Should not be here");
    } catch (Exception e) {
    }
}
 
Example 4
Source File: GoogleDrivePutRuntimeTest.java    From components with Apache License 2.0 6 votes vote down vote up
@Test
public void testRunAtDriverTooManyFiles() throws Exception {
    FileList hasfilelist = new FileList();
    List<File> hfiles = new ArrayList<>();
    File hfile = new File();
    hfile.setId(FILE_PUT_NAME);
    hfiles.add(hfile);
    hfiles.add(new File());
    hasfilelist.setFiles(hfiles);
    when(drive.files().list().setQ(anyString()).execute()).thenReturn(hasfilelist);
    properties.overwrite.setValue(true);
    testRuntime.initialize(container, properties);
    try {
        testRuntime.runAtDriver(container);
        fail("Should not be here");
    } catch (Exception e) {
    }
}
 
Example 5
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
private void updateFile(File serverFile, java.io.File localFile) throws IOException {
  FileContent content = new FileContent(MIME_TYPE, localFile);
  File file = new File();
  file.setTitle(serverFile.getTitle());
  file.setId(serverFile.getId());
  driveApi
      .files()
      .update(serverFile.getId(), file, content)
      .execute();
}
 
Example 6
Source File: GoogleDriveCreateRuntimeTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();

    properties = new GoogleDriveCreateProperties("test");
    properties.setupProperties();
    properties = (GoogleDriveCreateProperties) setupConnectionWithInstalledApplicationWithIdAndSecret(properties);
    //
    properties.parentFolder.setValue(FOLDER_ROOT);
    properties.newFolder.setValue(FOLDER_CREATE);

    testRuntime = spy(GoogleDriveCreateRuntime.class);
    doReturn(drive).when(testRuntime).getDriveService();
    File fc = new File();
    fc.setId(FOLDER_CREATE_ID);
    when(drive.files().create(any(File.class)).setFields(eq("id")).execute()).thenReturn(fc);
    //

    FileList flA = new FileList();
    List<File> fs = new ArrayList<>();
    File fA = new File();
    fA.setId("A");
    fs.add(fA);
    flA.setFiles(fs);
    when(drive.files().list().setQ(qA).execute()).thenReturn(flA);
    FileList flB = new FileList();
    List<File> fsA = new ArrayList<>();
    File fB = new File();
    fB.setId("B");
    fsA.add(fB);
    flB.setFiles(fsA);
    when(drive.files().list().setQ(qB).execute()).thenReturn(flB);
    FileList flC = new FileList();
    List<File> fsC = new ArrayList<>();
    File fC = new File();
    fC.setId("C");
    fsC.add(fC);
    flC.setFiles(fsC);
    when(drive.files().list().setQ(qC).execute()).thenReturn(flC);
}
 
Example 7
Source File: GoogleDrivePutReaderTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();

    properties = new GoogleDrivePutProperties("test");
    properties.connection.setupProperties();
    properties.connection.setupLayout();
    properties.schemaMain.setupProperties();
    properties.schemaMain.setupLayout();
    properties.setupProperties();
    properties.setupLayout();
    properties = (GoogleDrivePutProperties) setupConnectionWithAccessToken(properties);
    properties.uploadMode.setValue(UploadMode.UPLOAD_LOCAL_FILE);
    properties.fileName.setValue(FILE_PUT_NAME);
    properties.localFilePath
            .setValue(Paths.get(getClass().getClassLoader().getResource("service_account.json").toURI()).toString());
    properties.overwrite.setValue(true);
    properties.destinationFolder.setValue("root");

    when(drive.files().list().setQ(anyString()).execute()).thenReturn(emptyFileList);
    //
    File putFile = new File();
    putFile.setId(PUT_FILE_ID);
    putFile.setParents(Collections.singletonList(PUT_FILE_PARENT_ID));
    when(drive.files().create(any(File.class), any(AbstractInputStreamContent.class)).setFields(anyString()).execute())
            .thenReturn(putFile);

}
 
Example 8
Source File: GoogleDrivePutWriterTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();

    properties = new GoogleDrivePutProperties("test");
    properties.connection.setupProperties();
    properties.connection.setupLayout();
    properties.schemaMain.setupProperties();
    properties.schemaMain.setupLayout();
    properties.setupProperties();
    properties.setupLayout();
    properties = (GoogleDrivePutProperties) setupConnectionWithAccessToken(properties);
    properties.uploadMode.setValue(UploadMode.UPLOAD_LOCAL_FILE);
    properties.fileName.setValue("GoogleDrive Put test BR");
    properties.localFilePath.setValue("c:/Users/undx/brasil.jpg");
    properties.overwrite.setValue(true);
    properties.destinationFolder.setValue("root");

    sink.initialize(container, properties);
    wop = (GoogleDriveWriteOperation) sink.createWriteOperation();
    writer = new GoogleDrivePutWriter(wop, properties, container);

    when(drive.files().list().setQ(anyString()).execute()).thenReturn(emptyFileList);
    //
    File putFile = new File();
    putFile.setId(PUT_FILE_ID);
    putFile.setParents(Collections.singletonList(PUT_FILE_PARENT_ID));
    when(drive.files().create(any(File.class), any(AbstractInputStreamContent.class)).setFields(anyString()).execute())
            .thenReturn(putFile);
}
 
Example 9
Source File: GoogleDriveFsHelperTest.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
public void testPagination() throws IOException, FileBasedHelperException {
  State state = new State();
  state.appendToSetProp(GoogleDriveFileSystem.PAGE_SIZE, Integer.toString(1));

  GoogleDriveFsHelper fsHelper = new GoogleDriveFsHelper(state, client, Closer.create());
  List listRequest = mock(List.class);
  when(files.list()).thenReturn(listRequest);
  when(listRequest.setPageSize(anyInt())).thenReturn(listRequest);
  when(listRequest.setFields(anyString())).thenReturn(listRequest);
  when(listRequest.setQ(anyString())).thenReturn(listRequest);
  when(listRequest.setPageToken(anyString())).thenReturn(listRequest);

  int paginatedCalls = 5;
  final MutableInt i = new MutableInt(paginatedCalls);
  final File file = new File();
  file.setId("testId");
  file.setModifiedTime(new DateTime(System.currentTimeMillis()));

  when(listRequest.execute()).thenAnswer(new Answer<FileList>() {

    @Override
    public FileList answer(InvocationOnMock invocation) throws Throwable {
      FileList fileList = new FileList();
      fileList.setFiles(ImmutableList.of(file));
      if (i.intValue() > 0) {
        fileList.setNextPageToken("token");
        i.decrement();
      }
      return fileList;
    }
  });

  fsHelper.ls("test");

  int expectedCalls = 1 + paginatedCalls;
  verify(listRequest, times(expectedCalls)).execute();
}
 
Example 10
Source File: GoogleDrivePutRuntimeTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();

    properties = new GoogleDrivePutProperties("test");
    properties.connection.setupProperties();
    properties.connection.setupLayout();
    properties.schemaMain.setupProperties();
    properties.schemaMain.setupLayout();
    properties.setupProperties();
    properties.setupLayout();
    properties = (GoogleDrivePutProperties) setupConnectionWithAccessToken(properties);
    properties.uploadMode.setValue(UploadMode.UPLOAD_LOCAL_FILE);
    properties.fileName.setValue(FILE_PUT_NAME);
    properties.localFilePath.setValue("c:/Users/undx/brasil.jpg");
    properties.overwrite.setValue(true);
    properties.destinationFolder.setValue("root");

    testRuntime = spy(GoogleDrivePutRuntime.class);
    doReturn(drive).when(testRuntime).getDriveService();

    when(drive.files().list().setQ(anyString()).execute()).thenReturn(emptyFileList);
    //
    File putFile = new File();
    putFile.setId(PUT_FILE_ID);
    putFile.setParents(Collections.singletonList(PUT_FILE_PARENT_ID));
    when(drive.files().create(any(File.class), any(AbstractInputStreamContent.class)).setFields(anyString()).execute())
            .thenReturn(putFile);

}
 
Example 11
Source File: GoogleDriveCreateReaderTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();

    properties = new GoogleDriveCreateProperties("test");
    properties.setupProperties();
    properties = (GoogleDriveCreateProperties) setupConnectionWithInstalledApplicationWithIdAndSecret(properties);
    //
    properties.parentFolder.setValue(FOLDER_ROOT);
    properties.newFolder.setValue(FOLDER_CREATE);

    File fc = new File();
    fc.setId(FOLDER_CREATE_ID);
    when(drive.files().create(any(File.class)).setFields(eq("id")).execute()).thenReturn(fc);
}
 
Example 12
Source File: GoogleDriveListReaderTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdvance() throws Exception {
    FileList fileList = new FileList();
    for (int i = 0; i < 5; i++) {
        File f = new File();
        f.setName("sd" + i);
        f.setMimeType("text/text");
        f.setId("id-" + i);
        f.setModifiedTime(com.google.api.client.util.DateTime.parseRfc3339("2017-09-29T10:00:00"));
        f.setSize(100L);
        f.setKind("drive#fileName");
        f.setTrashed(false);
        f.setParents(Collections.singletonList(FOLDER_ROOT));
        f.setWebViewLink("https://toto.com");
        fileList.setFiles(Arrays.asList(f));
    }
    when(mockList.execute()).thenReturn(fileList);
    //
    properties.folder.setValue("A");
    source.initialize(container, properties);
    GoogleDriveListReader reader = ((GoogleDriveListReader) source.createReader(container));
    assertTrue(reader.start());
    while (reader.advance()) {
        assertNotNull(reader.getCurrent());
    }
    reader.close();
}
 
Example 13
Source File: GoogleDriveListReaderTest.java    From components with Apache License 2.0 5 votes vote down vote up
@Test
public void testStartOnly() throws Exception {
    FileList fileList = new FileList();
    File f = new File();
    f.setName("sd");
    f.setMimeType("text/text");
    f.setId("id-1");
    f.setModifiedTime(com.google.api.client.util.DateTime.parseRfc3339("2017-09-29T10:00:00"));
    f.setSize(100L);
    f.setKind("drive#fileName");
    f.setTrashed(false);
    f.setParents(Collections.singletonList(FOLDER_ROOT));
    f.setWebViewLink("https://toto.com");
    fileList.setFiles(Arrays.asList(f));

    when(mockList.execute()).thenReturn(fileList);
    //
    source.initialize(container, properties);
    GoogleDriveListReader reader = ((GoogleDriveListReader) source.createReader(container));
    assertTrue(reader.start());
    IndexedRecord record = (IndexedRecord) reader.getCurrent();
    assertNotNull(record);
    assertEquals(9, record.getSchema().getFields().size());
    assertEquals("id-1", record.get(0));
    assertEquals("sd", record.get(1));
    assertFalse(reader.advance());
    reader.close();
}
 
Example 14
Source File: TGDriveBrowser.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void cdRoot(TGBrowserCallBack<Object> cb) {
	try {
		File file = new File();
		file.setId(ROOT_FOLDER);
		
		this.folder = new TGDriveBrowserFile(file, null);
		
		cb.onSuccess(this.folder);
	} catch (Throwable e) {
		cb.handleError(e);
	}
}
 
Example 15
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
@Override
public long updateExperimentProto(
    java.io.File localFile,
    DriveFile serverExperimentProtoMetadata,
    String packageId,
    String experimentTitle)
    throws IOException {
  FileContent content = new FileContent(MIME_TYPE, localFile);

  // We can't re-use the file metadata that we already have, for some opaque reason about fields
  // that are already defined in the metadata we have, and can't be defined for an update.
  File file = new File();
  file.setTitle(serverExperimentProtoMetadata.getTitle());
  file.setId(serverExperimentProtoMetadata.getId());
  driveApi
      .files()
      .update(serverExperimentProtoMetadata.getId(), file, content)
      .execute();

  File drivePackage = new File();
  drivePackage.setId(packageId);
  drivePackage.setTitle(experimentTitle);
  driveApi
      .files()
      .patch(packageId, drivePackage)
      .execute();

  return getExperimentProtoMetadata(packageId).getVersion();
}
 
Example 16
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
@Override
public long insertExperimentProto(
    java.io.File localFile, String packageId, String experimentTitle) throws IOException {
  FileContent content = new FileContent(MIME_TYPE, localFile);

  File file = new File();
  file.setTitle(EXPERIMENT_PROTO_FILE);
  file.setParents(Collections.singletonList(new ParentReference().setId(packageId)));
  driveApi
      .files()
      .insert(file, content)
      .execute();

  File drivePackage = new File();
  drivePackage.setId(packageId);
  drivePackage.setTitle(experimentTitle);
  driveApi
      .files()
      .patch(packageId, drivePackage)
      .execute();

  FileVersion versionProto =
      FileVersion.newBuilder().setMinorVersion(MINOR_VERSION).setVersion(VERSION).build();

  ByteArrayContent versionBytes = new ByteArrayContent(MIME_TYPE, versionProto.toByteArray());

  File version = new File();
  version.setParents(Collections.singletonList(new ParentReference().setId(packageId)));
  version.setTitle(VERSION_PROTO_FILE);
  driveApi.files().insert(version, versionBytes).execute();

  return getExperimentProtoMetadata(packageId).getVersion();
}
 
Example 17
Source File: GoogleDriveApiImpl.java    From science-journal with Apache License 2.0 5 votes vote down vote up
@Override
public void updateExperimentLibraryFile(java.io.File libraryFile, String fileId)
    throws IOException {
  File file = new File();
  file.setId(fileId);
  FileContent content = new FileContent(MIME_TYPE, libraryFile);
  driveApi
      .files()
      .update(fileId, file, content)
      .execute();
}
 
Example 18
Source File: GoogleDriveInputReaderTest.java    From components with Apache License 2.0 4 votes vote down vote up
@Test
public void testAdvance() throws Exception {
    dataSource = spy(dataSource);
    Drive drive = mock(Drive.class, RETURNS_DEEP_STUBS);
    GoogleDriveUtils utils = mock(GoogleDriveUtils.class, RETURNS_DEEP_STUBS);
    doReturn(drive).when(dataSource).getDriveService();
    doReturn(utils).when(dataSource).getDriveUtils();

    List mockList = mock(List.class, RETURNS_DEEP_STUBS);
    when(drive.files().list()).thenReturn(mockList);
    //
    // String qA = "name='A' and 'root' in parents and mimeType='application/vnd.google-apps.folder' and trashed=false";
    //
    // when(drive.files().list().setQ(eq(qA)).execute()).thenReturn(createFolderFileList("A", false));
    //
    // GoogleDriveAbstractListReader alr = mock(GoogleDriveAbstractListReader.class);
    // doReturn(true).when(alr).start();

    inputProperties.getDatasetProperties().folder.setValue("A");

    FileList fileList = new FileList();
    File f = new File();
    f.setName("sd");
    f.setMimeType("text/text");
    f.setId("id-1");
    f.setModifiedTime(com.google.api.client.util.DateTime.parseRfc3339("2017-09-29T10:00:00"));
    f.setSize(100L);
    f.setKind("drive#fileName");
    f.setTrashed(false);
    f.setParents(Collections.singletonList(FOLDER_ROOT));
    f.setWebViewLink("https://toto.com");
    fileList.setFiles(Arrays.asList(f, f, f, f, f));

    when(mockList.execute()).thenReturn(fileList);

    dataSource.initialize(container, inputProperties);
    reader = (GoogleDriveInputReader) dataSource.createReader(container);
    reader.setLimit(2);
    assertTrue(reader.start());
    reader.getCurrent();
    assertTrue(reader.advance());
    reader.getCurrent();
    assertFalse(reader.advance());
}
 
Example 19
Source File: GoogleDriveCopyReaderTest.java    From components with Apache License 2.0 4 votes vote down vote up
@Test
public void testStartCopyFolder() throws Exception {
    final String q1 = "name='folder' and 'root' in parents and mimeType='application/vnd.google-apps.folder'";
    final String q2 = "'" + SOURCE_ID + "' in parents and trashed=false";
    final String q3 = "'folder-id2' in parents and trashed=false";
    //
    FileList fsource = new FileList();
    List<File> fsfiles = new ArrayList<>();
    File fsfolder = new File();
    fsfolder.setMimeType(GoogleDriveMimeTypes.MIME_TYPE_FOLDER);
    fsfolder.setName("folder");
    fsfolder.setId(SOURCE_ID);
    fsfiles.add(fsfolder);
    fsource.setFiles(fsfiles);
    when(drive.files().list().setQ(eq(q1)).execute()).thenReturn(fsource);

    FileList flist = new FileList();
    List<File> ffiles = new ArrayList<>();
    File ffile = new File();
    ffile.setMimeType(GoogleDriveMimeTypes.MIME_TYPE_CSV);
    ffile.setName("fileName");
    ffile.setId("fileName-id");
    ffiles.add(ffile);
    File ffolder = new File();
    ffolder.setMimeType(GoogleDriveMimeTypes.MIME_TYPE_FOLDER);
    ffolder.setName("folder");
    ffolder.setId("folder-id2");
    ffiles.add(ffolder);
    flist.setFiles(ffiles);
    when(drive.files().list().setQ(eq(q2)).execute()).thenReturn(flist);
    when(drive.files().list().setQ(eq(q3)).execute()).thenReturn(emptyFileList);

    properties.copyMode.setValue(CopyMode.Folder);
    properties.source.setValue("/folder");
    source.initialize(container, properties);
    BoundedReader reader = source.createReader(container);
    assertTrue(reader.start());
    IndexedRecord record = (IndexedRecord) reader.getCurrent();
    assertNotNull(record);
    assertEquals(2, record.getSchema().getFields().size());
    assertEquals(SOURCE_ID, record.get(0));
    assertEquals(DESTINATION_ID, record.get(1));
    reader.close();
    Map<String, Object> returnValues = reader.getReturnValues();
    assertNotNull(returnValues);
    assertEquals(SOURCE_ID, returnValues.get(GoogleDriveCopyDefinition.RETURN_SOURCE_ID));
    assertEquals(DESTINATION_ID, returnValues.get(GoogleDriveCopyDefinition.RETURN_DESTINATION_ID));
}
 
Example 20
Source File: GoogleDriveCopyRuntimeTest.java    From components with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
    super.setUp();

    testRuntime = spy(GoogleDriveCopyRuntime.class);
    doReturn(drive).when(testRuntime).getDriveService();

    properties = new GoogleDriveCopyProperties("test");
    properties.setupProperties();
    properties = (GoogleDriveCopyProperties) setupConnectionWithInstalledApplicationWithIdAndSecret(properties);
    //
    properties.copyMode.setValue(CopyMode.File);
    properties.source.setValue(FILE_COPY_NAME);
    properties.destinationFolder.setValue("/A");
    properties.newName.setValue("newName");
    // source fileName/folder
    File dest = new File();
    dest.setId(SOURCE_ID);
    FileList list = new FileList();
    List<File> files = new ArrayList<>();
    files.add(dest);
    list.setFiles(files);
    final String q1 = "name='A' and 'root' in parents and mimeType='application/vnd.google-apps.folder'";
    final String q2 = "name='fileName-copy-name' and mimeType!='application/vnd.google-apps.folder'";
    final String q3 = "name='A' and mimeType='application/vnd.google-apps.folder'";

    when(drive.files().list().setQ(eq(q1)).execute()).thenReturn(list);
    when(drive.files().list().setQ(eq(q2)).execute()).thenReturn(list);
    when(drive.files().list().setQ(eq(q3)).execute()).thenReturn(list);

    // destination/copied
    File copiedFile = new File();
    copiedFile.setId(DESTINATION_ID);
    copiedFile.setParents(Collections.singletonList(SOURCE_ID));
    when(drive.files().copy(anyString(), any(File.class)).setFields(anyString()).execute()).thenReturn(copiedFile);

    File destFolder = new File();
    destFolder.setId(DESTINATION_ID);
    destFolder.setParents(Collections.singletonList(SOURCE_ID));
    when(drive.files().create(any(File.class)).setFields(anyString()).execute()).thenReturn(destFolder);
}