Skip to content

Commit

Permalink
Fixing Unit test failures in GWCTest
Browse files Browse the repository at this point in the history
  • Loading branch information
smithkm committed Feb 19, 2018
1 parent e525bbe commit 3fb2fa7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 51 deletions.
10 changes: 6 additions & 4 deletions src/gwc/src/main/java/org/geoserver/gwc/GWC.java
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,15 @@ public class GWC implements DisposableBean, InitializingBean, ApplicationContext
// list of GeoServer contributed grid sets that should not be editable by the user
private final Set<String> geoserverEmbeddedGridSets = new HashSet<>();

private BlobStoreAggregator blobStoreAggregator;

public GWC(final GWCConfigPersister gwcConfigPersister, final StorageBroker sb,
final TileLayerDispatcher tld, final GridSetBroker gridSetBroker,
final TileBreeder tileBreeder, final DiskQuotaMonitor monitor,
final Dispatcher owsDispatcher, final Catalog catalog, final Catalog rawCatalog,
final DefaultStorageFinder storageFinder,
final JDBCConfigurationStorage jdbcConfigurationStorage) {
final JDBCConfigurationStorage jdbcConfigurationStorage,
final BlobStoreAggregator blobStoreAggregator) {

this.gwcConfigPersister = gwcConfigPersister;
this.tld = tld;
Expand All @@ -261,6 +264,7 @@ public GWC(final GWCConfigPersister gwcConfigPersister, final StorageBroker sb,
updateLockProvider(getConfig().getLockProviderName());

this.jdbcConfigurationStorage = jdbcConfigurationStorage;
this.blobStoreAggregator = blobStoreAggregator;
}

/**
Expand Down Expand Up @@ -1496,7 +1500,6 @@ public synchronized void modifyGridSet(final String oldGridSetName, final GridSe
}
}

//TODO: Verify we don't also need to remove from XMLConfiguration
getGridSetBroker().remove(oldGridSetName);
getGridSetBroker().put(newGridSet);

Expand Down Expand Up @@ -1556,8 +1559,7 @@ XMLConfiguration getXmlConfiguration() {
}

private BlobStoreAggregator getBlobStoreAggregator() {
// TODO set this during init instead.
return GeoWebCacheExtensions.bean(BlobStoreAggregator.class);
return blobStoreAggregator;
}

@SuppressWarnings("unchecked")
Expand Down
129 changes: 82 additions & 47 deletions src/gwc/src/test/java/org/geoserver/gwc/GWCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -89,7 +90,9 @@
import org.geowebcache.grid.BoundingBox;
import org.geowebcache.grid.GridSet;
import org.geowebcache.grid.GridSetBroker;
import org.geowebcache.grid.GridSetFactory;
import org.geowebcache.grid.GridSubset;
import org.geowebcache.grid.GridSubsetFactory;
import org.geowebcache.grid.SRS;
import org.geowebcache.layer.TileLayer;
import org.geowebcache.layer.TileLayerDispatcher;
Expand All @@ -98,6 +101,7 @@
import org.geowebcache.seed.SeedRequest;
import org.geowebcache.seed.TileBreeder;
import org.geowebcache.service.Service;
import org.geowebcache.storage.BlobStoreAggregator;
import org.geowebcache.storage.CompositeBlobStore;
import org.geowebcache.storage.DefaultStorageFinder;
import org.geowebcache.storage.StorageBroker;
Expand Down Expand Up @@ -202,6 +206,8 @@ static Resource tmpDir() throws IOException {
@Rule
public ExpectedException expected = ExpectedException.none();

private BlobStoreAggregator blobStoreAggregator;

@Before
public void setUp() throws Exception {

Expand All @@ -219,17 +225,10 @@ public void setUp() throws Exception {
when(gwcConfigPersister.getConfig()).thenReturn(defaults);

storageBroker = mock(StorageBroker.class);
gridSetBroker = new GridSetBroker(true, true);

tileLayerInfo = TileLayerInfoUtil.loadOrCreate(layer, defaults);
tileLayerGroupInfo = TileLayerInfoUtil.loadOrCreate(layerGroup, defaults);

tileLayer = new GeoServerTileLayer(layer, gridSetBroker, tileLayerInfo);
tileLayerGroup = new GeoServerTileLayer(layerGroup, gridSetBroker, tileLayerGroupInfo);

tld = mock(TileLayerDispatcher.class);
mockTileLayerDispatcher();

config = mock(TileLayerConfiguration.class);
tileBreeder = mock(TileBreeder.class);
quotaStore = mock(QuotaStore.class);
Expand All @@ -242,6 +241,19 @@ public void setUp() throws Exception {
storageFinder = mock(DefaultStorageFinder.class);
jdbcStorage = createMock(JDBCConfigurationStorage.class);
xmlConfig = mock(XMLConfiguration.class);
blobStoreAggregator = mock(BlobStoreAggregator.class);

gridSetBroker = new GridSetBroker(Arrays.asList(new DefaultGridsets(true, true), xmlConfig));
tileLayer = new GeoServerTileLayer(layer, gridSetBroker, tileLayerInfo);
GridSet testGridSet = namedGridsetCopy("TEST", gridSetBroker.getDefaults().worldEpsg4326());

GridSubset testGridSubset = GridSubsetFactory.createGridSubSet(testGridSet);
when(xmlConfig.getGridSet(eq("TEST"))).thenReturn(Optional.of(testGridSet));
tileLayer.addGridSubset(testGridSubset);
tileLayerGroup = new GeoServerTileLayer(layerGroup, gridSetBroker, tileLayerGroupInfo);
tileLayerGroup.addGridSubset(testGridSubset);
tld = mock(TileLayerDispatcher.class);
mockTileLayerDispatcher();

GeoWebCacheEnvironment genv = createMockBuilder(GeoWebCacheEnvironment.class)
.withConstructor()
Expand All @@ -266,6 +278,7 @@ public void setUp() throws Exception {
expect(appContext.getBean("geoWebCacheXMLConfiguration")).andReturn(xmlConfig).anyTimes();

replay(appContext);


GeoWebCacheExtensions gse = createMockBuilder(GeoWebCacheExtensions.class).createMock();
gse.setApplicationContext(appContext);
Expand Down Expand Up @@ -296,7 +309,8 @@ public void setUp() throws Exception {
replay(jdbcStorage);

mediator = new GWC(gwcConfigPersister, storageBroker, tld, gridSetBroker, tileBreeder,
diskQuotaMonitor, owsDispatcher, catalog, catalog, storageFinder, jdbcStorage);
diskQuotaMonitor, owsDispatcher, catalog, catalog, storageFinder, jdbcStorage,
blobStoreAggregator);
mediator.setApplicationContext(appContext);

mediator = spy(mediator);
Expand Down Expand Up @@ -377,7 +391,7 @@ public void testModifyTileLayer() throws Exception {
verify(tld, times(1)).modify(same(tileLayerGroup));

doNothing().when(tld).modify(same(tileLayer));
doThrow(new IOException()).when(config).modifyLayer(tileLayer);
doThrow(new ConfigurationPersistenceException(new IOException())).when(config).modifyLayer(tileLayer);
try {
mediator.save(tileLayer);
} catch (RuntimeException e) {
Expand Down Expand Up @@ -438,6 +452,7 @@ public void testModifyGridsetPreconditions() throws Exception {
} catch (NullPointerException e) {
assertTrue(true);
}
when(xmlConfig.getGridSet(eq("wrongOldName"))).thenReturn(Optional.empty());
try {
mediator.modifyGridSet("wrongOldName", oldGridset);
fail();
Expand All @@ -448,24 +463,26 @@ public void testModifyGridsetPreconditions() throws Exception {

@Test
public void testModifyGridsetNoNeedToTruncate() throws Exception {
final String oldName = "EPSG:4326";
final String newName = "MyEPSG:4326";

final String oldName = "TEST";
final String newName = "TEST_CHANGED";
final GridSet oldGridset = gridSetBroker.get(oldName);
final GridSet newGridset;
{
XMLGridSet xmlGridSet = new XMLGridSet(oldGridset);
xmlGridSet.setName(newName);
newGridset = xmlGridSet.makeGridSet();
}
newGridset = namedGridsetCopy(newName, oldGridset);

assertNotNull(tileLayer.getGridSubset(oldName));
assertNotNull(tileLayerGroup.getGridSubset(oldName));

when(xmlConfig.getGridSet(eq(newName))).thenReturn(Optional.empty());
when(xmlConfig.canSave(eq(newGridset))).thenReturn(true);

when(tld.getConfiguration(same(tileLayer))).thenReturn(config);
when(tld.getConfiguration(same(tileLayerGroup))).thenReturn(config);
mediator.modifyGridSet(oldName, newGridset);


when(xmlConfig.getGridSet(eq(oldName))).thenReturn(Optional.empty());
when(xmlConfig.getGridSet(eq(newName))).thenReturn(Optional.of(newGridset));

assertNull(tileLayer.getGridSubset(oldName));
assertNull(tileLayerGroup.getGridSubset(oldName));
assertNotNull(tileLayer.getGridSubset(newName));
Expand All @@ -478,10 +495,20 @@ public void testModifyGridsetNoNeedToTruncate() throws Exception {
assertEquals(newGridset, gridSetBroker.get(newName));
}

protected GridSet namedGridsetCopy(final String newName, final GridSet oldGridset) {
final GridSet newGridset;
{
XMLGridSet xmlGridSet = new XMLGridSet(oldGridset);
xmlGridSet.setName(newName);
newGridset = xmlGridSet.makeGridSet();
}
return newGridset;
}

@Test
public void testModifyGridsetTruncates() throws Exception {
final String oldName = "EPSG:4326";
final String newName = "MyEPSG:4326";
final String oldName = "TEST";
final String newName = "TEST_CHANGED";

final GridSet oldGridset = gridSetBroker.get(oldName);
final GridSet newGridset;
Expand All @@ -493,11 +520,16 @@ public void testModifyGridsetTruncates() throws Exception {
xmlGridSet.setAlignTopLeft(!xmlGridSet.getAlignTopLeft());
newGridset = xmlGridSet.makeGridSet();
}
when(xmlConfig.getGridSet(eq(newName))).thenReturn(Optional.empty());
when(xmlConfig.canSave(eq(newGridset))).thenReturn(true);

when(tld.getConfiguration(same(tileLayer))).thenReturn(config);
when(tld.getConfiguration(same(tileLayerGroup))).thenReturn(config);

mediator.modifyGridSet(oldName, newGridset);

when(xmlConfig.getGridSet(eq(oldName))).thenReturn(Optional.empty());
when(xmlConfig.getGridSet(eq(newName))).thenReturn(Optional.of(newGridset));

verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayer.getName()), eq(oldName));
verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayerGroup.getName()),
Expand All @@ -514,43 +546,42 @@ public void testRemoveGridsets() throws Exception {
}

{
final GridSet oldGridset = gridSetBroker.get("EPSG:4326");
final GridSet newGridset;
XMLGridSet xmlGridSet = new XMLGridSet(oldGridset);
xmlGridSet.setName("My4326");
// make it a bit different
xmlGridSet.setAlignTopLeft(!xmlGridSet.getAlignTopLeft());
newGridset = xmlGridSet.makeGridSet();
gridSetBroker.put(newGridset);
final GridSet oldGridset = gridSetBroker.get("TEST");
final GridSet newGridset = this.namedGridsetCopy("My4326", oldGridset);
when(xmlConfig.getGridSet(eq("My4326"))).thenReturn(Optional.of(newGridset));
}

when(tld.getConfiguration(same(tileLayer))).thenReturn(config);
when(tld.getConfiguration(same(tileLayerGroup))).thenReturn(config);
doNothing().when(tld).modify(same(tileLayer));
doNothing().when(tld).modify(same(tileLayerGroup));
when(tld.removeGridset(eq("EPSG:4326"))).thenReturn(config);
when(tld.removeGridset(eq("TEST"))).thenReturn(config);
when(tld.removeGridset(eq("My4326"))).thenReturn(config);

mediator.removeGridSets(ImmutableSet.of("My4326", "EPSG:4326"));
mediator.removeGridSets(ImmutableSet.of("My4326", "TEST"));

assertEquals(ImmutableSet.of("EPSG:900913"), tileLayer.getGridSubsets());
assertEquals(ImmutableSet.of("EPSG:900913"), tileLayerGroup.getGridSubsets());
assertEquals(ImmutableSet.of("EPSG:4326","EPSG:900913"), tileLayer.getGridSubsets());
assertEquals(ImmutableSet.of("EPSG:4326","EPSG:900913"), tileLayerGroup.getGridSubsets());

verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayer.getName()), eq("EPSG:4326"));
verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayer.getName()), eq("TEST"));
verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayerGroup.getName()),
eq("EPSG:4326"));
eq("TEST"));

verify(storageBroker, never()).deleteByGridSetId(eq(tileLayer.getName()),
eq("EPSG:900913"));
verify(storageBroker, never()).deleteByGridSetId(eq(tileLayer.getName()),
eq("EPSG:4326"));
verify(storageBroker, never()).deleteByGridSetId(eq(tileLayer.getName()), eq("My4326"));
verify(storageBroker, never()).deleteByGridSetId(eq(tileLayerGroup.getName()),
eq("EPSG:900913"));
verify(storageBroker, never()).deleteByGridSetId(eq(tileLayerGroup.getName()),
eq("EPSG:4326"));
verify(storageBroker, never()).deleteByGridSetId(eq(tileLayerGroup.getName()),
eq("My4326"));

verify(tld, times(1)).modify(same(tileLayer));
verify(tld, times(1)).modify(same(tileLayerGroup));
verify(tld, times(1)).removeGridset(eq("EPSG:4326"));
verify(tld, times(1)).removeGridset(eq("TEST"));
verify(tld, times(1)).removeGridset(eq("My4326"));
}

Expand All @@ -568,16 +599,19 @@ public void testRemoveAllLayerGridsetsDisablesLayer() throws Exception {
assertTrue(tileLayer.getInfo().isEnabled());
assertTrue(tileLayer.getInfo().isEnabled());

mediator.removeGridSets(ImmutableSet.of("EPSG:900913", "EPSG:4326"));
// Defaults can't be removed from the broker so remove them from the layers first
tileLayer.removeGridSubset("EPSG:900913");
tileLayer.removeGridSubset("EPSG:4326");
tileLayerGroup.removeGridSubset("EPSG:900913");
tileLayerGroup.removeGridSubset("EPSG:4326");
mediator.save(tileLayer);
mediator.save(tileLayerGroup);
mediator.removeGridSets(ImmutableSet.of("TEST"));

verify(tld, times(1)).modify(same(tileLayer));// all other checks are in testRemoveGridsets
verify(tld, times(1)).modify(same(tileLayerGroup));
verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayer.getName()), eq("EPSG:4326"));
verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayerGroup.getName()),
eq("EPSG:900913"));
verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayer.getName()), eq("EPSG:4326"));
verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayerGroup.getName()),
eq("EPSG:900913"));
verify(tld, times(2)).modify(same(tileLayer));// all other checks are in testRemoveGridsets
verify(tld, times(2)).modify(same(tileLayerGroup));
verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayer.getName()), eq("TEST"));
verify(storageBroker, times(1)).deleteByGridSetId(eq(tileLayer.getName()), eq("TEST"));

assertTrue(tileLayer.getGridSubsets().isEmpty());
assertTrue(tileLayerGroup.getGridSubsets().isEmpty());
Expand Down Expand Up @@ -1404,7 +1438,7 @@ public void testSetBlobStoresSavesConfig() throws Exception {
mock(BlobStoreInfo.class));
when(xmlConfig.getBlobStores()).thenReturn(configList);

BlobStoreInfo config = new FileBlobStoreInfo();
BlobStoreInfo config = new FileBlobStoreInfo("TestBlobStore");
List<BlobStoreInfo> newStores = ImmutableList.<BlobStoreInfo> of(config);
mediator.setBlobStores(newStores);

Expand All @@ -1419,9 +1453,10 @@ public void testSetBlobStoresRestoresRuntimeStoresOnSaveFailure() throws Excepti
CompositeBlobStore composite = mock(CompositeBlobStore.class);
doReturn(composite).when(mediator).getCompositeBlobStore();

BlobStoreInfo config = new FileBlobStoreInfo();
BlobStoreInfo config = new FileBlobStoreInfo("TestStore");


doThrow(new IOException("expected")).when(xmlConfig).addBlobStore(config);
doThrow(new ConfigurationPersistenceException(new IOException("expected"))).when(xmlConfig).addBlobStore(config);

List<BlobStoreInfo> oldStores = Lists.newArrayList(mock(BlobStoreInfo.class),
mock(BlobStoreInfo.class));
Expand Down

0 comments on commit 3fb2fa7

Please sign in to comment.