mirror of
https://github.com/google/nomulus.git
synced 2025-05-03 05:27:52 +02:00
This doesn't change the end result of a successful run, though this is what a typical flow looks like prior to this fix: Consider a sheet with 10 data rows (+ 1 header row = 11). A 10-row data set will call worksheet.setRowCount(10), which truncates the last row of the existing sheet. This row will eventually be added again in the last for loop, but if the synchronizer fails mid-sync, this last row will remain dropped. This fix will prevent this last row from being dropped. This doesn't fix the broader issue of SheetSynchronizer not behaving transactionally -- that's a different can of worms. See the linked bug for an instance where the synchronizer failed mid-run and dropped a data row as a result. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=136398109
146 lines
6 KiB
Java
146 lines
6 KiB
Java
// Copyright 2016 The Nomulus Authors. All Rights Reserved.
|
|
//
|
|
// 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 google.registry.export.sheet;
|
|
|
|
import static org.mockito.Matchers.any;
|
|
import static org.mockito.Matchers.eq;
|
|
import static org.mockito.Mockito.atLeastOnce;
|
|
import static org.mockito.Mockito.mock;
|
|
import static org.mockito.Mockito.verify;
|
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
|
import static org.mockito.Mockito.when;
|
|
|
|
import com.google.common.collect.ImmutableList;
|
|
import com.google.common.collect.ImmutableMap;
|
|
import com.google.gdata.client.spreadsheet.SpreadsheetService;
|
|
import com.google.gdata.data.spreadsheet.CustomElementCollection;
|
|
import com.google.gdata.data.spreadsheet.ListEntry;
|
|
import com.google.gdata.data.spreadsheet.ListFeed;
|
|
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
|
|
import com.google.gdata.data.spreadsheet.WorksheetEntry;
|
|
import java.net.URL;
|
|
import org.junit.After;
|
|
import org.junit.Before;
|
|
import org.junit.Test;
|
|
import org.junit.runner.RunWith;
|
|
import org.junit.runners.JUnit4;
|
|
|
|
/** Unit tests for {@link SheetSynchronizer}. */
|
|
@RunWith(JUnit4.class)
|
|
public class SheetSynchronizerTest {
|
|
|
|
private final SpreadsheetService spreadsheetService = mock(SpreadsheetService.class);
|
|
private final SpreadsheetEntry spreadsheet = mock(SpreadsheetEntry.class);
|
|
private final WorksheetEntry worksheet = mock(WorksheetEntry.class);
|
|
private final ListFeed listFeed = mock(ListFeed.class);
|
|
private final SheetSynchronizer sheetSynchronizer = new SheetSynchronizer();
|
|
|
|
@Before
|
|
public void before() throws Exception {
|
|
sheetSynchronizer.spreadsheetService = spreadsheetService;
|
|
when(spreadsheetService.getEntry(any(URL.class), eq(SpreadsheetEntry.class)))
|
|
.thenReturn(spreadsheet);
|
|
when(spreadsheet.getWorksheets())
|
|
.thenReturn(ImmutableList.of(worksheet));
|
|
when(worksheet.getListFeedUrl())
|
|
.thenReturn(new URL("http://example.com/spreadsheet"));
|
|
when(spreadsheetService.getFeed(any(URL.class), eq(ListFeed.class)))
|
|
.thenReturn(listFeed);
|
|
when(worksheet.update())
|
|
.thenReturn(worksheet);
|
|
}
|
|
|
|
@After
|
|
public void after() throws Exception {
|
|
verify(spreadsheetService).getEntry(
|
|
new URL("https://spreadsheets.google.com/feeds/spreadsheets/foobar"),
|
|
SpreadsheetEntry.class);
|
|
verify(spreadsheet).getWorksheets();
|
|
verify(worksheet).getListFeedUrl();
|
|
verify(spreadsheetService).getFeed(new URL("http://example.com/spreadsheet"), ListFeed.class);
|
|
verify(listFeed).getEntries();
|
|
verifyNoMoreInteractions(spreadsheetService, spreadsheet, worksheet, listFeed);
|
|
}
|
|
|
|
@Test
|
|
public void testSynchronize_bothEmpty_doNothing() throws Exception {
|
|
when(listFeed.getEntries()).thenReturn(ImmutableList.<ListEntry>of());
|
|
sheetSynchronizer.synchronize("foobar", ImmutableList.<ImmutableMap<String, String>>of());
|
|
verify(worksheet).setRowCount(1);
|
|
verify(worksheet).update();
|
|
}
|
|
|
|
@Test
|
|
public void testSynchronize_bothContainSameRow_doNothing() throws Exception {
|
|
ListEntry entry = makeListEntry(ImmutableMap.of("key", "value"));
|
|
when(listFeed.getEntries()).thenReturn(ImmutableList.of(entry));
|
|
sheetSynchronizer.synchronize("foobar", ImmutableList.of(
|
|
ImmutableMap.of("key", "value")));
|
|
verify(worksheet).setRowCount(2);
|
|
verify(worksheet).update();
|
|
verify(entry, atLeastOnce()).getCustomElements();
|
|
verifyNoMoreInteractions(entry);
|
|
}
|
|
|
|
@Test
|
|
public void testSynchronize_cellIsDifferent_updateRow() throws Exception {
|
|
ListEntry entry = makeListEntry(ImmutableMap.of("key", "value"));
|
|
when(listFeed.getEntries()).thenReturn(ImmutableList.of(entry));
|
|
sheetSynchronizer.synchronize("foobar", ImmutableList.of(
|
|
ImmutableMap.of("key", "new value")));
|
|
verify(entry.getCustomElements()).setValueLocal("key", "new value");
|
|
verify(entry).update();
|
|
verify(worksheet).setRowCount(2);
|
|
verify(worksheet).update();
|
|
verify(entry, atLeastOnce()).getCustomElements();
|
|
verifyNoMoreInteractions(entry);
|
|
}
|
|
|
|
@Test
|
|
public void testSynchronize_spreadsheetMissingRow_insertRow() throws Exception {
|
|
ListEntry entry = makeListEntry(ImmutableMap.<String, String>of());
|
|
when(listFeed.getEntries()).thenReturn(ImmutableList.<ListEntry>of());
|
|
when(listFeed.createEntry()).thenReturn(entry);
|
|
sheetSynchronizer.synchronize("foobar", ImmutableList.of(
|
|
ImmutableMap.of("key", "value")));
|
|
verify(entry.getCustomElements()).setValueLocal("key", "value");
|
|
verify(listFeed).insert(entry);
|
|
verify(worksheet).setRowCount(2);
|
|
verify(worksheet).update();
|
|
verify(listFeed).createEntry();
|
|
verify(entry, atLeastOnce()).getCustomElements();
|
|
verifyNoMoreInteractions(entry);
|
|
}
|
|
|
|
@Test
|
|
public void testSynchronize_spreadsheetRowNoLongerInData_deleteRow() throws Exception {
|
|
ListEntry entry = makeListEntry(ImmutableMap.of("key", "value"));
|
|
when(listFeed.getEntries()).thenReturn(ImmutableList.of(entry));
|
|
sheetSynchronizer.synchronize("foobar", ImmutableList.<ImmutableMap<String, String>>of());
|
|
verify(worksheet).setRowCount(1);
|
|
verify(worksheet).update();
|
|
verifyNoMoreInteractions(entry);
|
|
}
|
|
|
|
private static ListEntry makeListEntry(ImmutableMap<String, String> values) {
|
|
CustomElementCollection collection = mock(CustomElementCollection.class);
|
|
for (ImmutableMap.Entry<String, String> entry : values.entrySet()) {
|
|
when(collection.getValue(eq(entry.getKey()))).thenReturn(entry.getValue());
|
|
}
|
|
ListEntry listEntry = mock(ListEntry.class);
|
|
when(listEntry.getCustomElements()).thenReturn(collection);
|
|
return listEntry;
|
|
}
|
|
}
|