From 7ed8cffc2941116a49f4eea43d120c2d3502df1d Mon Sep 17 00:00:00 2001 From: Daniel Koch Date: Tue, 1 Oct 2024 15:40:08 +0200 Subject: [PATCH] chore: include test cases for emitting local geoserver lifecycle events --- ...logApplicationEventsConfigurationTest.java | 68 +++++++++++++------ .../TestConfigurationAutoConfiguration.java | 23 +++++++ 2 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/catalog/events/src/test/java/org/geoserver/cloud/config/catalog/events/CatalogApplicationEventsConfigurationTest.java b/src/catalog/events/src/test/java/org/geoserver/cloud/config/catalog/events/CatalogApplicationEventsConfigurationTest.java index 424b95d77..5dc0075dc 100644 --- a/src/catalog/events/src/test/java/org/geoserver/cloud/config/catalog/events/CatalogApplicationEventsConfigurationTest.java +++ b/src/catalog/events/src/test/java/org/geoserver/cloud/config/catalog/events/CatalogApplicationEventsConfigurationTest.java @@ -6,9 +6,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertTrue; import lombok.NonNull; @@ -21,26 +18,20 @@ import org.geoserver.catalog.plugin.Patch; import org.geoserver.catalog.plugin.Patch.Property; import org.geoserver.catalog.plugin.PropertyDiff; +import org.geoserver.cloud.event.GeoServerEvent; import org.geoserver.cloud.event.catalog.CatalogInfoAdded; import org.geoserver.cloud.event.catalog.CatalogInfoModified; import org.geoserver.cloud.event.catalog.CatalogInfoRemoved; import org.geoserver.cloud.event.config.ConfigInfoAdded; import org.geoserver.cloud.event.config.ConfigInfoModified; import org.geoserver.cloud.event.config.ServiceRemoved; -import org.geoserver.cloud.event.info.ConfigInfoType; -import org.geoserver.cloud.event.info.InfoAdded; -import org.geoserver.cloud.event.info.InfoEvent; -import org.geoserver.cloud.event.info.InfoModified; -import org.geoserver.cloud.event.info.InfoRemoved; +import org.geoserver.cloud.event.info.*; +import org.geoserver.cloud.event.lifecycle.LifecycleEvent; +import org.geoserver.cloud.event.lifecycle.ReloadEvent; +import org.geoserver.cloud.event.lifecycle.ResetEvent; import org.geoserver.cloud.test.ApplicationEventCapturingListener; -import org.geoserver.config.ConfigurationListener; -import org.geoserver.config.CoverageAccessInfo; +import org.geoserver.config.*; import org.geoserver.config.CoverageAccessInfo.QueueType; -import org.geoserver.config.GeoServer; -import org.geoserver.config.GeoServerInfo; -import org.geoserver.config.LoggingInfo; -import org.geoserver.config.ServiceInfo; -import org.geoserver.config.SettingsInfo; import org.geoserver.config.impl.CoverageAccessInfoImpl; import org.geoserver.config.impl.SettingsInfoImpl; import org.geoserver.wms.WMSInfoImpl; @@ -71,7 +62,7 @@ class CatalogApplicationEventsConfigurationTest { private CatalogTestData testData; public @BeforeEach void before() { - listener.setCapureEventsOf(InfoEvent.class); + listener.setCapureEventsOf(GeoServerEvent.class); catalog.dispose(); listener.clear(); testData = CatalogTestData.empty(() -> catalog, () -> geoserver).initialize(); @@ -83,7 +74,7 @@ class CatalogApplicationEventsConfigurationTest { @Test void testCatalogEventBroadcasterHasSetUpItself() { - Optional publiherListener = + Optional publisherListener = catalog.getListeners().stream() .filter( l -> @@ -92,12 +83,49 @@ void testCatalogEventBroadcasterHasSetUpItself() { CatalogApplicationEventPublisher .LocalCatalogEventPublisher) .findFirst(); - assertTrue(publiherListener.isPresent()); + assertTrue(publisherListener.isPresent()); + } + + @Test + void testGSLifeCycleDispatchOnReset() { + geoserver.reset(); + + // Check that there is no other event being triggered, we expect a single one. + List allEvents = listener.allOf(LifecycleEvent.class); + assertEquals(1, allEvents.size()); + + // And we expect it to be a (local) ResetEvent. + ResetEvent resetEvent = listener.expectOne(ResetEvent.class); + assertTrue(resetEvent.isLocal()); + } + + @Test + void testGSLifeCycleIgnoreOnDispose() { + geoserver.dispose(); + + // We don't expect any event on disposal. + List allEvents = listener.allOf(LifecycleEvent.class); + assertEquals(0, allEvents.size()); + } + + @Test + void testGSLifeCycleDispatchOnReload() throws Exception { + geoserver.reload(); + + // Check that there is no other event being triggered, we expect two ones. + List allEvents = listener.allOf(LifecycleEvent.class); + assertEquals(2, allEvents.size()); + + // And we expect them to be a (local) ResetEvent and a (local) ReloadEvent. + ReloadEvent reloadEvent = listener.expectOne(ReloadEvent.class); + assertTrue(reloadEvent.isLocal()); + ResetEvent resetEvent = listener.expectOne(ResetEvent.class); + assertTrue(resetEvent.isLocal()); } @Test void testConfigEventBroadcasterHasSetUpItself() { - Optional publiherListener = + Optional publisherListener = geoserver.getListeners().stream() .filter( l -> @@ -106,7 +134,7 @@ void testConfigEventBroadcasterHasSetUpItself() { CatalogApplicationEventPublisher .LocalConfigEventPublisher) .findFirst(); - assertTrue(publiherListener.isPresent()); + assertTrue(publisherListener.isPresent()); } @Test diff --git a/src/catalog/events/src/test/java/org/geoserver/cloud/config/catalog/events/TestConfigurationAutoConfiguration.java b/src/catalog/events/src/test/java/org/geoserver/cloud/config/catalog/events/TestConfigurationAutoConfiguration.java index 091581dea..a22b002c6 100644 --- a/src/catalog/events/src/test/java/org/geoserver/cloud/config/catalog/events/TestConfigurationAutoConfiguration.java +++ b/src/catalog/events/src/test/java/org/geoserver/cloud/config/catalog/events/TestConfigurationAutoConfiguration.java @@ -6,9 +6,13 @@ import org.geoserver.catalog.Catalog; import org.geoserver.catalog.plugin.CatalogPlugin; +import org.geoserver.config.DefaultGeoServerLoader; import org.geoserver.config.GeoServer; +import org.geoserver.config.GeoServerLoader; import org.geoserver.config.plugin.GeoServerImpl; import org.geoserver.config.util.XStreamPersisterFactory; +import org.geoserver.platform.GeoServerExtensions; +import org.geoserver.platform.GeoServerResourceLoader; import org.geoserver.platform.config.DefaultUpdateSequence; import org.geoserver.platform.config.UpdateSequence; import org.springframework.beans.factory.annotation.Qualifier; @@ -42,4 +46,23 @@ GeoServer geoServer(@Qualifier("catalog") Catalog catalog) { gs.setCatalog(catalog); return gs; } + + @Bean + GeoServerExtensions geoserverExtensions() { + return new GeoServerExtensions(); + } + + @Bean + GeoServerResourceLoader geoServerResourceLoader() { + return new GeoServerResourceLoader(); + } + + @Bean + GeoServerLoader geoserverLoader( + @Qualifier("geoServer") GeoServer geoServer, + @Qualifier("geoServerResourceLoader") GeoServerResourceLoader geoServerResourceLoader) { + DefaultGeoServerLoader loader = new DefaultGeoServerLoader(geoServerResourceLoader); + loader.postProcessBeforeInitialization(geoServer, "geoserver"); + return loader; + } }