From f76307bf230f35d420c10f61cb60ee8edf24b280 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Thu, 16 Jan 2025 19:56:17 +0800 Subject: [PATCH 1/7] Polish #33 : Add SimpleAutoServiceRegistrationAutoConfiguration test --- ...oServiceRegistrationAutoConfiguration.java | 5 +- ...strationEndpointAutoConfigurationTest.java | 48 +++++++++++++++ .../ServiceRegistryAutoConfigurationTest.java | 2 - ...viceRegistrationAutoConfigurationTest.java | 58 +++++++++++++++++++ 4 files changed, 108 insertions(+), 5 deletions(-) create mode 100644 microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/actuate/autoconfigure/ServiceRegistrationEndpointAutoConfigurationTest.java create mode 100644 microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/SimpleAutoServiceRegistrationAutoConfigurationTest.java diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/SimpleAutoServiceRegistrationAutoConfiguration.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/SimpleAutoServiceRegistrationAutoConfiguration.java index 7115ef0..81b4661 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/SimpleAutoServiceRegistrationAutoConfiguration.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/SimpleAutoServiceRegistrationAutoConfiguration.java @@ -63,9 +63,9 @@ public class SimpleAutoServiceRegistrationAutoConfiguration { /** - * The property name prefix + * The property name prefix : "microsphere.spring.cloud.service-registry.auto-registration.simple." */ - public static final String PROPERTY_NAME_PREFIX = MICROSPHERE_SPRING_CLOUD_PROPERTY_NAME_PREFIX + "simple."; + public static final String PROPERTY_NAME_PREFIX = MICROSPHERE_SPRING_CLOUD_PROPERTY_NAME_PREFIX + "service-registry.auto-registration.simple."; @Bean public Registration registration( @@ -85,7 +85,6 @@ public Registration registration( return registration; } - @Bean @ConditionalOnMissingBean public ServiceRegistry serviceRegistry() { diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/actuate/autoconfigure/ServiceRegistrationEndpointAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/actuate/autoconfigure/ServiceRegistrationEndpointAutoConfigurationTest.java new file mode 100644 index 0000000..ae98a63 --- /dev/null +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/actuate/autoconfigure/ServiceRegistrationEndpointAutoConfigurationTest.java @@ -0,0 +1,48 @@ +package io.microsphere.spring.cloud.client.service.registry.actuate.autoconfigure; + +import io.microsphere.spring.cloud.client.service.registry.endpoint.ServiceDeregistrationEndpoint; +import io.microsphere.spring.cloud.client.service.registry.endpoint.ServiceRegistrationEndpoint; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * {@link ServiceRegistrationEndpointAutoConfiguration} Test + * + * @author Mercy + * @see ServiceRegistrationEndpointAutoConfiguration + * @since 1.0.0 + */ +@SpringBootTest( + classes = { + ServiceRegistrationEndpointAutoConfigurationTest.class + }, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT +) +@TestPropertySource( + properties = { + "microsphere.spring.cloud.service-registry.auto-registration.simple.enabled=true", + "management.endpoint.serviceRegistration.enabled=true", + "management.endpoint.serviceDeregistration.enabled=true", + } +) +@EnableAutoConfiguration +public class ServiceRegistrationEndpointAutoConfigurationTest { + + @Autowired + private ObjectProvider serviceRegistrationEndpoint; + + @Autowired + private ObjectProvider serviceDeregistrationEndpoint; + + @Test + public void testEndpoints() { + assertNotNull(serviceRegistrationEndpoint); + } + +} \ No newline at end of file diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java index 5b09845..70598e2 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java @@ -1,7 +1,5 @@ package io.microsphere.spring.cloud.client.service.registry.autoconfigure; -import static org.junit.jupiter.api.Assertions.*; - /** * {@link ServiceRegistryAutoConfiguration} Test * diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/SimpleAutoServiceRegistrationAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/SimpleAutoServiceRegistrationAutoConfigurationTest.java new file mode 100644 index 0000000..7406277 --- /dev/null +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/SimpleAutoServiceRegistrationAutoConfigurationTest.java @@ -0,0 +1,58 @@ +package io.microsphere.spring.cloud.client.service.registry.autoconfigure; + +import io.microsphere.spring.cloud.client.service.registry.SimpleAutoServiceRegistration; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.web.server.LocalServerPort; +import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.cloud.client.serviceregistry.ServiceRegistry; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * {@link SimpleAutoServiceRegistrationAutoConfiguration} Test + * + * @author Mercy + * @see SimpleAutoServiceRegistrationAutoConfiguration + * @since 1.0.0 + */ +@SpringBootTest( + classes = { + SimpleAutoServiceRegistrationAutoConfigurationTest.class + }, + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, + properties = { + "microsphere.spring.cloud.service-registry.auto-registration.simple.enabled=true", + "spring.application.name=test-service" + } +) +@EnableAutoConfiguration +public class SimpleAutoServiceRegistrationAutoConfigurationTest { + + @Autowired + private Registration registration; + + @Autowired + private ServiceRegistry serviceRegistry; + + @Autowired + private SimpleAutoServiceRegistration simpleAutoServiceRegistration; + + @LocalServerPort + private Integer port; + + @Test + public void test() { + assertEquals("test-service", registration.getServiceId()); + assertNotNull(registration.getHost()); + assertNotNull(registration.getPort()); + assertNotNull(registration.getUri()); + assertNotNull(registration.getInstanceId()); + assertNotNull(registration.getMetadata()); + assertNotNull(port); + } + +} \ No newline at end of file From 0e7d90c28eac805fd7341e7f7728daae08c387d7 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Thu, 16 Jan 2025 20:18:23 +0800 Subject: [PATCH 2/7] Polish #33 : Add ServiceRegistryAutoConfiguration test --- .../ServiceRegistryAutoConfigurationTest.java | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java index 70598e2..a519332 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java @@ -1,5 +1,27 @@ package io.microsphere.spring.cloud.client.service.registry.autoconfigure; +import io.microsphere.spring.cloud.client.service.registry.DefaultRegistration; +import io.microsphere.spring.cloud.client.service.registry.InMemoryServiceRegistry; +import io.microsphere.spring.cloud.client.service.registry.event.RegistrationDeregisteredEvent; +import io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent; +import io.microsphere.spring.cloud.client.service.registry.event.RegistrationPreDeregisteredEvent; +import io.microsphere.spring.cloud.client.service.registry.event.RegistrationPreRegisteredEvent; +import io.microsphere.spring.cloud.client.service.registry.event.RegistrationRegisteredEvent; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.aop.support.AopUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.EnableAspectJAutoProxy; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.springframework.aop.support.AopUtils.getTargetClass; + /** * {@link ServiceRegistryAutoConfiguration} Test * @@ -7,6 +29,81 @@ * @see ServiceRegistryAutoConfiguration * @since 1.0.0 */ +@SpringBootTest( + classes = { + InMemoryServiceRegistry.class, + ServiceRegistryAutoConfiguration.class, + ServiceRegistryAutoConfigurationTest.class + } +) +@EnableAspectJAutoProxy public class ServiceRegistryAutoConfigurationTest { + @Autowired + private ConfigurableApplicationContext context; + + @Autowired + private ServiceRegistry serviceRegistry; + + private Registration registration; + + private int count; + + @BeforeEach + public void init() { + DefaultRegistration registration = new DefaultRegistration(); + registration.setServiceId("test-service"); + registration.setInstanceId("127.0.0.1:8080"); + this.registration = registration; + } + + @Test + public void testEventPublishingRegistrationAspect() { + + context.addApplicationListener(this::onApplicationEvent); + + serviceRegistry.register(registration); + + assertEquals(2, count); + + serviceRegistry.deregister(registration); + + assertEquals(4, count); + } + + private void onApplicationEvent(ApplicationEvent event) { + if (event instanceof RegistrationPreRegisteredEvent) { + onRegistrationPreRegisteredEvent((RegistrationPreRegisteredEvent) event); + } else if (event instanceof RegistrationRegisteredEvent) { + onRegistrationRegisteredEvent((RegistrationRegisteredEvent) event); + } else if (event instanceof RegistrationPreDeregisteredEvent) { + onRegistrationPreDeregisteredEvent((RegistrationPreDeregisteredEvent) event); + } else if (event instanceof RegistrationDeregisteredEvent) { + onRegistrationDeregisteredEvent((RegistrationDeregisteredEvent) event); + } + } + + private void onRegistrationPreRegisteredEvent(RegistrationPreRegisteredEvent event) { + assertRegistrationEvent(event); + } + + private void onRegistrationRegisteredEvent(RegistrationRegisteredEvent event) { + assertRegistrationEvent(event); + } + + private void onRegistrationPreDeregisteredEvent(RegistrationPreDeregisteredEvent event) { + assertRegistrationEvent(event); + } + + private void onRegistrationDeregisteredEvent(RegistrationDeregisteredEvent event) { + assertRegistrationEvent(event); + } + + private void assertRegistrationEvent(RegistrationEvent event) { + Registration registration = event.getRegistration(); + assertEquals(this.registration, registration); + assertSame(this.registration, registration); + assertSame(getTargetClass(this.serviceRegistry), getTargetClass(event.getRegistry())); + count++; + } } \ No newline at end of file From 8fac9e44d9ba3fddd603095adcbca99fdf325cd4 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Thu, 16 Jan 2025 20:20:48 +0800 Subject: [PATCH 3/7] Update WebMvcServiceRegistryAutoConfigurationTest.java --- .../WebMvcServiceRegistryAutoConfigurationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java index f0693bf..7af49b8 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java @@ -38,7 +38,7 @@ @SpringBootTest( classes = {WebMvcServiceRegistryAutoConfigurationTest.class}, properties = { - "microsphere.spring.cloud.simple.enabled=true", + "microsphere.spring.cloud.service-registry.auto-registration.simple.enabled=true", "spring.cloud.service-registry.auto-registration.enabled=true", "spring.cloud.kubernetes.enabled=false", "kubernetes.informer.enabled=false", From 3580141a3c456d02a5550485b942f469d7addda5 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Thu, 16 Jan 2025 20:32:30 +0800 Subject: [PATCH 4/7] Polish #33 : Add testing coverage --- .../event/RegistrationDeregisteredEvent.java | 10 ++-- .../registry/event/RegistrationEvent.java | 49 +++++++++++++++++-- .../RegistrationPreDeregisteredEvent.java | 12 ++--- .../event/RegistrationPreRegisteredEvent.java | 12 ++--- .../event/RegistrationRegisteredEvent.java | 12 ++--- .../ServiceRegistryAutoConfigurationTest.java | 29 +++++++++++ 6 files changed, 93 insertions(+), 31 deletions(-) diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationDeregisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationDeregisteredEvent.java index 294583c..898b039 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationDeregisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationDeregisteredEvent.java @@ -19,6 +19,8 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED; + /** * The after-{@link ServiceRegistry#deregister(Registration) deregister} event. * @@ -33,12 +35,8 @@ public RegistrationDeregisteredEvent(ServiceRegistry registry, Reg } @Override - public boolean isRegistered() { - return false; + public Type getType() { + return DEREGISTERED; } - @Override - public boolean isDeregistered() { - return true; - } } diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationEvent.java index d51d26a..b8e1b26 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationEvent.java @@ -22,6 +22,11 @@ import org.springframework.lang.NonNull; import org.springframework.util.Assert; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.REGISTERED; + /** * The Spring event for {@link ServiceRegistry} * @@ -80,7 +85,7 @@ public ServiceRegistry getRegistry() { * @return true if pre-registered */ public final boolean isPreRegistered() { - return !isRegistered(); + return getType() == PRE_REGISTERED; } /** @@ -89,7 +94,9 @@ public final boolean isPreRegistered() { * * @return true if registered */ - public abstract boolean isRegistered(); + public final boolean isRegistered() { + return getType() == REGISTERED; + } /** * Current event is raised before the {@link #getRegistration() registration} is @@ -98,7 +105,7 @@ public final boolean isPreRegistered() { * @return true if pre-deregistered */ public final boolean isPreDeregistered() { - return !isDeregistered(); + return getType() == PRE_DEREGISTERED; } /** @@ -107,6 +114,40 @@ public final boolean isPreDeregistered() { * * @return true if deregistered */ - public abstract boolean isDeregistered(); + public final boolean isDeregistered() { + return getType() == DEREGISTERED; + } + + /** + * Get the {@link Type} of the {@link RegistrationEvent} + * + * @return non-null + */ + public abstract Type getType(); + /** + * The {@link Type} of the {@link RegistrationEvent} + */ + public static enum Type { + + /** + * The {@link RegistrationPreRegisteredEvent} + */ + PRE_REGISTERED, + + /** + * The {@link RegistrationRegisteredEvent} + */ + REGISTERED, + + /** + * The {@link RegistrationPreDeregisteredEvent} + */ + PRE_DEREGISTERED, + + /** + * The {@link RegistrationDeregisteredEvent} + */ + DEREGISTERED + } } diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java index 6e19475..892bdf0 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java @@ -19,6 +19,9 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; + /** * The before-{@link ServiceRegistry#deregister(Registration) deregister} event. * @@ -33,12 +36,7 @@ public RegistrationPreDeregisteredEvent(ServiceRegistry registry, } @Override - public boolean isRegistered() { - return false; - } - - @Override - public boolean isDeregistered() { - return false; + public Type getType() { + return PRE_DEREGISTERED; } } \ No newline at end of file diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java index 2e0d192..44501ed 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java @@ -19,6 +19,9 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; + /** * The before-{@link ServiceRegistry#register(Registration) register} event. * @@ -33,12 +36,7 @@ public RegistrationPreRegisteredEvent(ServiceRegistry registry, Re } @Override - public boolean isRegistered() { - return false; - } - - @Override - public boolean isDeregistered() { - return false; + public Type getType() { + return PRE_REGISTERED; } } diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java index cc20ed1..5fdb3ac 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java @@ -19,6 +19,9 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.REGISTERED; + /** * The after-{@link ServiceRegistry#register(Registration) register} event. * @@ -33,12 +36,7 @@ public RegistrationRegisteredEvent(ServiceRegistry registry, Regis } @Override - public boolean isRegistered() { - return true; - } - - @Override - public boolean isDeregistered() { - return false; + public Type getType() { + return REGISTERED; } } \ No newline at end of file diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java index a519332..2f23cd9 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java @@ -18,8 +18,15 @@ import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.EnableAspectJAutoProxy; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; +import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.REGISTERED; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.springframework.aop.support.AopUtils.getTargetClass; /** @@ -85,18 +92,38 @@ private void onApplicationEvent(ApplicationEvent event) { private void onRegistrationPreRegisteredEvent(RegistrationPreRegisteredEvent event) { assertRegistrationEvent(event); + assertTrue(event.isPreRegistered()); + assertFalse(event.isRegistered()); + assertFalse(event.isPreDeregistered()); + assertFalse(event.isDeregistered()); + assertEquals(PRE_REGISTERED, event.getType()); } private void onRegistrationRegisteredEvent(RegistrationRegisteredEvent event) { assertRegistrationEvent(event); + assertFalse(event.isPreRegistered()); + assertTrue(event.isRegistered()); + assertFalse(event.isPreDeregistered()); + assertFalse(event.isDeregistered()); + assertEquals(REGISTERED, event.getType()); } private void onRegistrationPreDeregisteredEvent(RegistrationPreDeregisteredEvent event) { assertRegistrationEvent(event); + assertFalse(event.isPreRegistered()); + assertFalse(event.isRegistered()); + assertTrue(event.isPreDeregistered()); + assertFalse(event.isDeregistered()); + assertEquals(PRE_DEREGISTERED, event.getType()); } private void onRegistrationDeregisteredEvent(RegistrationDeregisteredEvent event) { assertRegistrationEvent(event); + assertFalse(event.isPreRegistered()); + assertFalse(event.isRegistered()); + assertFalse(event.isPreDeregistered()); + assertTrue(event.isDeregistered()); + assertEquals(DEREGISTERED, event.getType()); } private void assertRegistrationEvent(RegistrationEvent event) { @@ -104,6 +131,8 @@ private void assertRegistrationEvent(RegistrationEvent event) { assertEquals(this.registration, registration); assertSame(this.registration, registration); assertSame(getTargetClass(this.serviceRegistry), getTargetClass(event.getRegistry())); + assertNotNull(event.getSource()); + assertNotNull(event.getType()); count++; } } \ No newline at end of file From 2804a05b058f5b7e502e7125b61457649f89e09c Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Thu, 16 Jan 2025 20:33:39 +0800 Subject: [PATCH 5/7] Polish #33 : Reimport --- .../service/registry/event/RegistrationPreDeregisteredEvent.java | 1 - .../service/registry/event/RegistrationRegisteredEvent.java | 1 - 2 files changed, 2 deletions(-) diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java index 892bdf0..3ef61b6 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreDeregisteredEvent.java @@ -19,7 +19,6 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; -import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.DEREGISTERED; import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; /** diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java index 5fdb3ac..06306ac 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationRegisteredEvent.java @@ -19,7 +19,6 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; -import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.REGISTERED; /** From 66a5c459d5b952524cde597968035d86ec219095 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Thu, 16 Jan 2025 20:57:17 +0800 Subject: [PATCH 6/7] Temp Commit --- .../spring/cloud/client/event/ServiceInstancesChangedEvent.java | 1 - .../service/registry/event/RegistrationPreRegisteredEvent.java | 1 - .../spring/cloud/client/discovery/UnionDiscoveryClientTest.java | 1 - .../autoconfigure/DiscoveryClientAutoConfigurationTest.java | 1 - .../cloud/client/event/ServiceInstancesChangedEventTest.java | 1 - .../autoconfigure/ServiceRegistryAutoConfigurationTest.java | 1 - .../WebMvcServiceRegistryAutoConfigurationTest.java | 1 - 7 files changed, 7 deletions(-) diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/event/ServiceInstancesChangedEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/event/ServiceInstancesChangedEvent.java index 95e6c25..805706a 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/event/ServiceInstancesChangedEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/event/ServiceInstancesChangedEvent.java @@ -20,7 +20,6 @@ import org.springframework.context.ApplicationEvent; import org.springframework.context.event.ApplicationEventMulticaster; import org.springframework.context.event.SimpleApplicationEventMulticaster; -import org.springframework.util.Assert; import java.util.List; diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java index 44501ed..ccceec1 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/event/RegistrationPreRegisteredEvent.java @@ -19,7 +19,6 @@ import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.ServiceRegistry; -import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_DEREGISTERED; import static io.microsphere.spring.cloud.client.service.registry.event.RegistrationEvent.Type.PRE_REGISTERED; /** diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/discovery/UnionDiscoveryClientTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/discovery/UnionDiscoveryClientTest.java index 064bf06..513a4a3 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/discovery/UnionDiscoveryClientTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/discovery/UnionDiscoveryClientTest.java @@ -16,7 +16,6 @@ */ package io.microsphere.spring.cloud.client.discovery; -import io.microsphere.spring.cloud.client.discovery.autoconfigure.DiscoveryClientAutoConfiguration; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/discovery/autoconfigure/DiscoveryClientAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/discovery/autoconfigure/DiscoveryClientAutoConfigurationTest.java index b8a1872..a13bb34 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/discovery/autoconfigure/DiscoveryClientAutoConfigurationTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/discovery/autoconfigure/DiscoveryClientAutoConfigurationTest.java @@ -28,7 +28,6 @@ import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClient; import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration; import org.springframework.cloud.commons.util.UtilAutoConfiguration; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/event/ServiceInstancesChangedEventTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/event/ServiceInstancesChangedEventTest.java index 63d6403..000595e 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/event/ServiceInstancesChangedEventTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/event/ServiceInstancesChangedEventTest.java @@ -9,7 +9,6 @@ import java.util.Arrays; import java.util.UUID; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertSame; diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java index 2f23cd9..4ba1fb5 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/ServiceRegistryAutoConfigurationTest.java @@ -9,7 +9,6 @@ import io.microsphere.spring.cloud.client.service.registry.event.RegistrationRegisteredEvent; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.springframework.aop.support.AopUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.cloud.client.serviceregistry.Registration; diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java index 7af49b8..5f24ed7 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java @@ -18,7 +18,6 @@ import io.microsphere.spring.webmvc.annotation.EnableWebMvcExtension; import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; From 0988b10e97d2c3e34cd59c8604a9b9ac2f4c1021 Mon Sep 17 00:00:00 2001 From: Mercy Ma Date: Fri, 17 Jan 2025 17:48:00 +0800 Subject: [PATCH 7/7] Polish #33 : Bugfix @ConditionalOnProperty using @AliasFor --- .../ConditionalOnAutoServiceRegistrationEnabled.java | 2 +- .../ServiceRegistrationEndpointAutoConfigurationTest.java | 5 +---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/condition/ConditionalOnAutoServiceRegistrationEnabled.java b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/condition/ConditionalOnAutoServiceRegistrationEnabled.java index 9da6d95..0db565e 100644 --- a/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/condition/ConditionalOnAutoServiceRegistrationEnabled.java +++ b/microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/condition/ConditionalOnAutoServiceRegistrationEnabled.java @@ -40,7 +40,7 @@ @Retention(RetentionPolicy.RUNTIME) @Target({ElementType.TYPE, ElementType.METHOD}) @Documented -@ConditionalOnProperty(name = SERVICE_REGISTRY_AUTO_REGISTRATION_ENABLED_PROPERTY_NAME) +@ConditionalOnProperty(name = SERVICE_REGISTRY_AUTO_REGISTRATION_ENABLED_PROPERTY_NAME, matchIfMissing = true) public @interface ConditionalOnAutoServiceRegistrationEnabled { /** diff --git a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/actuate/autoconfigure/ServiceRegistrationEndpointAutoConfigurationTest.java b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/actuate/autoconfigure/ServiceRegistrationEndpointAutoConfigurationTest.java index ae98a63..528396d 100644 --- a/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/actuate/autoconfigure/ServiceRegistrationEndpointAutoConfigurationTest.java +++ b/microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/actuate/autoconfigure/ServiceRegistrationEndpointAutoConfigurationTest.java @@ -7,7 +7,6 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.TestPropertySource; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -22,9 +21,7 @@ classes = { ServiceRegistrationEndpointAutoConfigurationTest.class }, - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT -) -@TestPropertySource( + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = { "microsphere.spring.cloud.service-registry.auto-registration.simple.enabled=true", "management.endpoint.serviceRegistration.enabled=true",