Skip to content

Commit

Permalink
Polish #33 : Add testing coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Jan 16, 2025
1 parent 8fac9e4 commit 3580141
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 31 deletions.
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,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.
*
Expand All @@ -33,12 +36,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,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.
*
Expand All @@ -33,12 +36,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,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.
*
Expand All @@ -33,12 +36,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 @@ -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;

/**
Expand Down Expand Up @@ -85,25 +92,47 @@ 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) {
Registration registration = event.getRegistration();
assertEquals(this.registration, registration);
assertSame(this.registration, registration);
assertSame(getTargetClass(this.serviceRegistry), getTargetClass(event.getRegistry()));
assertNotNull(event.getSource());
assertNotNull(event.getType());
count++;
}
}

0 comments on commit 3580141

Please sign in to comment.