Package br.com.objectos.way.gdrive

Source Code of br.com.objectos.way.gdrive.DriveExecTest

/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.gdrive;

import static br.com.objectos.way.base.testing.WayMatchers.equalTo;
import static br.com.objectos.way.base.testing.WayMatchers.greaterThan;
import static br.com.objectos.way.base.testing.WayMatchers.is;
import static br.com.objectos.way.base.testing.WayMatchers.notNullValue;
import static com.google.common.collect.Lists.transform;
import static org.hamcrest.MatcherAssert.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

import org.testng.annotations.BeforeClass;
import org.testng.annotations.Guice;
import org.testng.annotations.Test;

import com.google.api.services.drive.model.Change;
import com.google.api.services.drive.model.File;
import com.google.api.services.drive.model.File.Labels;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableList;
import com.google.common.io.Files;
import com.google.inject.Inject;

/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
@Test
@Guice(modules = { WayGDriveTestModule.class })
public class DriveExecTest {

  @Inject
  private DriveExec exec;

  private String root;
  private String folder;
  private String changes;
  private String trashable;

  @BeforeClass
  public void setUp() {
    root = "0AFfV80wEwsDzUk9PVA";
    folder = "0B1fV80wEwsDzalN6Z3VWNVNFSkU";
    changes = "0B1fV80wEwsDzeE9oVjJvSjItMUk";
    trashable = "1KoR4Fjd8T2JwVIKY2Mp1cRHJtn2tPWjHKLzdRfEyVWE";
  }

  public void directory_of() {
    File res = exec.directoryOf(root);

    assertThat(res, notNullValue());
    assertThat(res.getId(), equalTo(root));
    assertThat(res.isEmpty(), is(false));
  }

  public void iterate_files() {
    Iterator<File> iter = exec.iterateFilesOf(root);
    List<File> res = ImmutableList.copyOf(iter);

    assertThat(res.size(), equalTo(2));

    List<String> names = transform(res, FileToName.INSTANCE);
    assertThat(names.get(0), equalTo("world"));
    assertThat(names.get(1), equalTo("hello"));
  }

  public void mkdir() {
    File res = exec.mkdir(root, "temp0");
    assertThat(res.getTitle(), equalTo("temp0"));
    trash(res);
  }

  public void write_text_plain() throws IOException {
    String title = "write";
    java.io.File file = new java.io.File(title);
    Files.write("write", file, Charsets.UTF_8);
    From from = From.of(file);
    MimeType mimeType = MimeType.TEXT_PLAIN;

    File res = exec.write(folder, from, title, mimeType);
    assertThat(res.getTitle(), equalTo("write"));
    file.delete();
    trash(res);
  }

  public void write_ms_excel() throws IOException {
    String title = "void.xls";
    java.io.File file = IOUtils.openXlsFile(getClass(), title);
    From from = From.of(file);
    MimeType mimeType = MimeType.APPLICATION_MS_EXCEL;

    File res = exec.write(folder, from, title, mimeType);
    assertThat(res.getTitle(), equalTo(title));
    trash(res);
  }

  public void write_ms_excel_input_stream() throws IOException {
    String title = "void.xls";
    InputStream stream = IOUtils.open(getClass(), title);
    From from = From.of(stream);
    MimeType mimeType = MimeType.APPLICATION_MS_EXCEL;

    File res = exec.write(folder, from, title, mimeType);
    assertThat(res.getTitle(), equalTo(title));
    trash(res);
  }

  public void write_openxml_sheet_input_stream() throws IOException {
    String title = "void.xlsx";
    InputStream stream = IOUtils.open(getClass(), title);
    From from = From.of(stream);
    MimeType mimeType = MimeType.APPLICATION_OPENXML_SHEET;

    File res = exec.write(folder, from, title, mimeType);
    assertThat(res.getTitle(), equalTo(title));
    trash(res);
  }

  public void trash() {
    File trash = exec.trash(trashable);
    Labels res = trash.getLabels();

    assertThat(res.getTrashed(), is(true));
  }

  @Test(dependsOnMethods = { "trash" })
  public void untrash() {
    File untrash = exec.untrash(trashable);
    Labels res = untrash.getLabels();

    assertThat(res.getTrashed(), is(false));
  }

  public void iterate_changes() {
    List<Change> before = listChanges(changes, ChangeId.empty());

    int size = before.size();
    assertThat(size, greaterThan(1));

    int changeNumber = 3;
    Change change = before.get(changeNumber);
    ChangeId changeId = ChangeId.of(change.getId());

    List<Change> res = listChanges(changes, changeId);
    assertThat(res.size(), equalTo(size - changeNumber));
  }

  public void root_id_iterate_changes() {
    List<Change> res = listChanges(root, ChangeId.empty());
    assertThat(res.size(), greaterThan(0));
  }

  private List<Change> listChanges(String id, ChangeId changeId) {
    Iterator<Change> iter = exec.iterateChangesOf(id, changeId);
    return ImmutableList.copyOf(iter);
  }

  private void trash(File file) {
    try {
      exec.trash(file.getId());
    } catch (Exception e) {
    }
  }

}
TOP

Related Classes of br.com.objectos.way.gdrive.DriveExecTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.