Skip to content

Commit

Permalink
Merge pull request #37 from mercyblitz/dev-1.x
Browse files Browse the repository at this point in the history
Add test-cases
  • Loading branch information
mercyblitz authored Jan 17, 2025
2 parents 0f9f5cc + 0988b10 commit 6edc28b
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -85,7 +85,6 @@ public Registration registration(
return registration;
}


@Bean
@ConditionalOnMissingBean
public ServiceRegistry<Registration> serviceRegistry() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -33,12 +35,8 @@ public RegistrationDeregisteredEvent(ServiceRegistry<Registration> registry, Reg
}

@Override
public boolean isRegistered() {
return false;
public Type getType() {
return DEREGISTERED;
}

@Override
public boolean isDeregistered() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}
*
Expand Down Expand Up @@ -80,7 +85,7 @@ public ServiceRegistry<Registration> getRegistry() {
* @return <code>true</code> if pre-registered
*/
public final boolean isPreRegistered() {
return !isRegistered();
return getType() == PRE_REGISTERED;
}

/**
Expand All @@ -89,7 +94,9 @@ public final boolean isPreRegistered() {
*
* @return <code>true</code> if registered
*/
public abstract boolean isRegistered();
public final boolean isRegistered() {
return getType() == REGISTERED;
}

/**
* Current event is raised before the {@link #getRegistration() registration} is
Expand All @@ -98,7 +105,7 @@ public final boolean isPreRegistered() {
* @return <code>true</code> if pre-deregistered
*/
public final boolean isPreDeregistered() {
return !isDeregistered();
return getType() == PRE_DEREGISTERED;
}

/**
Expand All @@ -107,6 +114,40 @@ public final boolean isPreDeregistered() {
*
* @return <code>true</code> 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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.PRE_DEREGISTERED;

/**
* The before-{@link ServiceRegistry#deregister(Registration) deregister} event.
*
Expand All @@ -33,12 +35,7 @@ public RegistrationPreDeregisteredEvent(ServiceRegistry<Registration> registry,
}

@Override
public boolean isRegistered() {
return false;
}

@Override
public boolean isDeregistered() {
return false;
public Type getType() {
return PRE_DEREGISTERED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.PRE_REGISTERED;

/**
* The before-{@link ServiceRegistry#register(Registration) register} event.
*
Expand All @@ -33,12 +35,7 @@ public RegistrationPreRegisteredEvent(ServiceRegistry<Registration> registry, Re
}

@Override
public boolean isRegistered() {
return false;
}

@Override
public boolean isDeregistered() {
return false;
public Type getType() {
return PRE_REGISTERED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.REGISTERED;

/**
* The after-{@link ServiceRegistry#register(Registration) register} event.
*
Expand All @@ -33,12 +35,7 @@ public RegistrationRegisteredEvent(ServiceRegistry<Registration> registry, Regis
}

@Override
public boolean isRegistered() {
return true;
}

@Override
public boolean isDeregistered() {
return false;
public Type getType() {
return REGISTERED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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 static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* {@link ServiceRegistrationEndpointAutoConfiguration} Test
*
* @author <a href="mailto:[email protected]">Mercy<a/>
* @see ServiceRegistrationEndpointAutoConfiguration
* @since 1.0.0
*/
@SpringBootTest(
classes = {
ServiceRegistrationEndpointAutoConfigurationTest.class
},
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
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> serviceRegistrationEndpoint;

@Autowired
private ObjectProvider<ServiceDeregistrationEndpoint> serviceDeregistrationEndpoint;

@Test
public void testEndpoints() {
assertNotNull(serviceRegistrationEndpoint);
}

}
Loading

0 comments on commit 6edc28b

Please sign in to comment.