Java Code Examples for com.google.api.services.sheets.v4.model.ValueRange#setValues()

The following examples show how to use com.google.api.services.sheets.v4.model.ValueRange#setValues() . 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: GoogleSheetsWorkitemHandlerTest.java    From jbpm-work-items with Apache License 2.0 6 votes vote down vote up
@Before
public void setUp() {
    try {
        ValueRange valueRange = new ValueRange();
        List<List<Object>> testValues = new ArrayList<>();
        List<Object> testRowValues = new ArrayList<>();
        testRowValues.add("testValueOne");
        testRowValues.add("testValueTwo");
        testValues.add(testRowValues);
        valueRange.setValues(testValues);

        when(auth.getSheetsService(anyString(),
                                   anyString())).thenReturn(sheetsClient);
        when(sheetsClient.spreadsheets()).thenReturn(spreadsheets);
        when(spreadsheets.values()).thenReturn(spreasheetsValues);
        when(spreasheetsValues.get(anyString(),
                                   anyString())).thenReturn(spreasheetsValuesGet);
        when(spreasheetsValuesGet.execute()).thenReturn(valueRange);
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
 
Example 2
Source File: GoogleSheetsRetrieveValuesCustomizerTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void testAfterProducerColumnDimension() throws Exception {
    String range = "A1:A5";
    String sheetName = "Sheet1";
    String majorDimension = RangeCoordinate.DIMENSION_COLUMNS;

    List<List<Object>> values = Collections.singletonList(Arrays.asList("a1", "a2", "a3", "a4", "a5"));
    List<String> expectedValueModel = Collections.singletonList("{\"spreadsheetId\":\"%s\", \"#1\":\"a1\",\"#2\":\"a2\",\"#3\":\"a3\",\"#4\":\"a4\",\"#5\":\"a5\"}");

    Map<String, Object> options = new HashMap<>();
    options.put("spreadsheetId", getSpreadsheetId());
    options.put("range", range);
    options.put("splitResults", false);

    customizer.customize(getComponent(), options);

    Exchange inbound = new DefaultExchange(createCamelContext());

    ValueRange valueRange = new ValueRange();
    valueRange.setRange(sheetName + "!" + range);
    valueRange.setMajorDimension(majorDimension);
    valueRange.setValues(values);

    inbound.getIn().setBody(valueRange);
    getComponent().getAfterProducer().process(inbound);

    Assert.assertEquals(GoogleSheetsApiCollection.getCollection().getApiName(SheetsSpreadsheetsValuesApiMethod.class).getName(), ConnectorOptions.extractOption(options, "apiName"));
    Assert.assertEquals("get", ConnectorOptions.extractOption(options, "methodName"));

    @SuppressWarnings("unchecked")
    List<String> model = inbound.getIn().getBody(List.class);
    Assert.assertEquals(expectedValueModel.size(), model.size());
    Iterator<String> modelIterator = model.iterator();
    for (String expected : expectedValueModel) {
        assertThatJson(modelIterator.next()).isEqualTo(String.format(expected, getSpreadsheetId()));
    }
}
 
Example 3
Source File: GoogleSheetsNamedColumnsTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetValuesCustomizer() throws Exception {
    Map<String, Object> options = new HashMap<>();
    options.put("spreadsheetId", getSpreadsheetId());
    options.put("range", range);
    options.put("columnNames", columnNames);
    options.put("splitResults", false);

    GoogleSheetsGetValuesCustomizer customizer = new GoogleSheetsGetValuesCustomizer();
    customizer.customize(getComponent(), options);

    Exchange inbound = new DefaultExchange(createCamelContext());

    ValueRange valueRange = new ValueRange();
    valueRange.setRange(range);
    valueRange.setMajorDimension(RangeCoordinate.DIMENSION_ROWS);
    valueRange.setValues(values);

    inbound.getIn().setBody(valueRange);
    getComponent().getBeforeConsumer().process(inbound);

    Assert.assertEquals(GoogleSheetsApiCollection.getCollection().getApiName(SheetsSpreadsheetsValuesApiMethod.class).getName(), ConnectorOptions.extractOption(options, "apiName"));
    Assert.assertEquals("get", ConnectorOptions.extractOption(options, "methodName"));

    @SuppressWarnings("unchecked")
    List<String> body = inbound.getIn().getBody(List.class);
    Assert.assertEquals(model.size(), body.size());
    Iterator<String> modelIterator = body.iterator();
    for (String expected : model) {
        assertThatJson(modelIterator.next()).isEqualTo(String.format(expected, getSpreadsheetId()));
    }
}
 
Example 4
Source File: GoogleSheetsGetValuesCustomizerTest.java    From syndesis with Apache License 2.0 5 votes vote down vote up
@Test
public void testBeforeConsumer() throws Exception {
    Map<String, Object> options = new HashMap<>();
    options.put("spreadsheetId", getSpreadsheetId());
    options.put("range", range);
    options.put("splitResults", false);

    customizer.customize(getComponent(), options);

    Exchange inbound = new DefaultExchange(createCamelContext());

    ValueRange valueRange = new ValueRange();
    valueRange.setRange(sheetName + "!" + range);
    valueRange.setMajorDimension(majorDimension);
    valueRange.setValues(values);

    inbound.getIn().setBody(valueRange);
    getComponent().getBeforeConsumer().process(inbound);

    Assert.assertEquals(GoogleSheetsApiCollection.getCollection().getApiName(SheetsSpreadsheetsValuesApiMethod.class).getName(), ConnectorOptions.extractOption(options, "apiName"));
    Assert.assertEquals("get", ConnectorOptions.extractOption(options, "methodName"));

    @SuppressWarnings("unchecked")
    List<String> model = inbound.getIn().getBody(List.class);
    Assert.assertEquals(expectedValueModel.size(), model.size());
    Iterator<String> modelIterator = model.iterator();
    for (String expected : expectedValueModel) {
        assertThatJson(modelIterator.next()).isEqualTo(String.format(expected, getSpreadsheetId()));
    }
}
 
Example 5
Source File: GoogleSheetsUpdateValuesCustomizer.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private void beforeProducer(Exchange exchange) throws IOException {
    final Message in = exchange.getIn();

    List<String> jsonBeans = null;
    if (in.getBody() instanceof List) {
        jsonBeans = in.getBody(List.class);
    } else if (in.getBody(String.class) != null) {
        String body = in.getBody(String.class);
        if (JsonUtils.isJsonArray(body)) {
            jsonBeans = JsonUtils.arrayToJsonBeans(JsonUtils.reader().readTree(body));
        } else if (JsonUtils.isJson(body)) {
            jsonBeans = Collections.singletonList(body);
        }
    }

    ValueRange valueRange = new ValueRange();
    List<List<Object>> values = new ArrayList<>();

    if (ObjectHelper.isNotEmpty(jsonBeans)) {
        final ObjectSchema spec = getItemSchema(GoogleSheetsMetaDataHelper.createSchema(range, majorDimension, columnNames));

        for (String json : jsonBeans) {
            Map<String, Object> dataShape = JsonUtils.reader().forType(Map.class).readValue(json);

            if (dataShape.containsKey(SPREADSHEET_ID)) {
                spreadsheetId = Optional.ofNullable(dataShape.remove(SPREADSHEET_ID))
                        .map(Object::toString)
                        .orElse(spreadsheetId);
            }

            List<Object> rangeValues = new ArrayList<>();
            spec.getProperties()
                    .entrySet()
                    .stream()
                    .filter(specEntry -> !Objects.equals(SPREADSHEET_ID, specEntry.getKey()))
                    .forEach(specEntry -> rangeValues.add(Optional.ofNullable(dataShape.get(specEntry.getKey()))
                                                                  .orElse("")));

            values.add(rangeValues);
        }
    }

    valueRange.setMajorDimension(majorDimension);
    valueRange.setValues(values);

    in.setHeader(GoogleSheetsStreamConstants.SPREADSHEET_ID, spreadsheetId);
    in.setHeader(GoogleSheetsStreamConstants.RANGE, range);
    in.setHeader(GoogleSheetsStreamConstants.MAJOR_DIMENSION, majorDimension);
    in.setHeader(GoogleSheetsConstants.PROPERTY_PREFIX + "values", valueRange);
    in.setHeader(GoogleSheetsConstants.PROPERTY_PREFIX + "valueInputOption", valueInputOption);
}
 
Example 6
Source File: GoogleSheetsRetrieveValuesCustomizerTest.java    From syndesis with Apache License 2.0 4 votes vote down vote up
@Test
public void testAfterProducerRowDimension() throws Exception {
    String range = "A1:A5";
    String sheetName = "Sheet1";
    String majorDimension = RangeCoordinate.DIMENSION_ROWS;

    List<List<Object>> values = Arrays.asList(Collections.singletonList("a1"),
            Collections.singletonList("a2"),
            Collections.singletonList("a3"),
            Collections.singletonList("a4"),
            Collections.singletonList("a5"));

    List<String> expectedValueModel = Arrays.asList("{\"spreadsheetId\":\"%s\", \"A\":\"a1\"}",
            "{\"spreadsheetId\":\"%s\", \"A\":\"a2\"}",
            "{\"spreadsheetId\":\"%s\", \"A\":\"a3\"}",
            "{\"spreadsheetId\":\"%s\", \"A\":\"a4\"}",
            "{\"spreadsheetId\":\"%s\", \"A\":\"a5\"}");


    Map<String, Object> options = new HashMap<>();
    options.put("spreadsheetId", getSpreadsheetId());
    options.put("range", range);
    options.put("splitResults", false);

    customizer.customize(getComponent(), options);

    Exchange inbound = new DefaultExchange(createCamelContext());

    ValueRange valueRange = new ValueRange();
    valueRange.setRange(sheetName + "!" + range);
    valueRange.setMajorDimension(majorDimension);
    valueRange.setValues(values);

    inbound.getIn().setBody(valueRange);
    getComponent().getAfterProducer().process(inbound);

    Assert.assertEquals(GoogleSheetsApiCollection.getCollection().getApiName(SheetsSpreadsheetsValuesApiMethod.class).getName(), ConnectorOptions.extractOption(options, "apiName"));
    Assert.assertEquals("get", ConnectorOptions.extractOption(options, "methodName"));

    @SuppressWarnings("unchecked")
    List<String> model = inbound.getIn().getBody(List.class);
    Assert.assertEquals(expectedValueModel.size(), model.size());
    Iterator<String> modelIterator = model.iterator();
    for (String expected : expectedValueModel) {
        assertThatJson(modelIterator.next()).isEqualTo(String.format(expected, getSpreadsheetId()));
    }
}