Skip to content

Commit 6a8e06d

Browse files
committed
Polish #33 : Add ServiceInstancesChangedEvent test
1 parent c1f0a70 commit 6a8e06d

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/event/ServiceInstancesChangedEvent.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,19 @@
1616
*/
1717
package io.microsphere.spring.cloud.client.event;
1818

19-
/**
20-
* TODO Comment
21-
*
22-
* @author <a href="mailto:[email protected]">Mercy</a>
23-
* @since TODO
24-
*/
25-
2619
import org.springframework.cloud.client.ServiceInstance;
2720
import org.springframework.context.ApplicationEvent;
2821
import org.springframework.context.event.ApplicationEventMulticaster;
2922
import org.springframework.context.event.SimpleApplicationEventMulticaster;
23+
import org.springframework.util.Assert;
3024

3125
import java.util.List;
3226

3327
import static java.util.Collections.unmodifiableList;
28+
import static org.springframework.util.Assert.notEmpty;
3429

3530
/**
36-
* An event raised after the {@link ServiceInstance instances} of one service has been
31+
* An event raised when the {@link ServiceInstance instances} of one service has been
3732
* changed.
3833
*
3934
* @author <a href="mailto:[email protected]">Mercy</a>
@@ -58,6 +53,7 @@ public class ServiceInstancesChangedEvent extends ApplicationEvent {
5853
public ServiceInstancesChangedEvent(String serviceName,
5954
List<ServiceInstance> serviceInstances) {
6055
super(serviceName);
56+
notEmpty(serviceInstances, () -> "The arguments 'serviceInstances' must not be empty!");
6157
this.serviceInstances = unmodifiableList(serviceInstances);
6258
}
6359

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package io.microsphere.spring.cloud.client.event;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.cloud.client.DefaultServiceInstance;
6+
import org.springframework.cloud.client.ServiceInstance;
7+
8+
import java.net.URI;
9+
import java.util.Arrays;
10+
import java.util.UUID;
11+
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertFalse;
15+
import static org.junit.jupiter.api.Assertions.assertSame;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
18+
/**
19+
* {@link ServiceInstancesChangedEvent} Test
20+
*
21+
* @author <a href="mailto:[email protected]">Mercy<a/>
22+
* @see ServiceInstancesChangedEvent
23+
* @since 1.0.0
24+
*/
25+
public class ServiceInstancesChangedEventTest {
26+
27+
private String serviceName = "testService";
28+
29+
private ServiceInstancesChangedEvent event;
30+
31+
private ServiceInstance instance;
32+
33+
@BeforeEach
34+
public void init() {
35+
this.instance = createInstance(serviceName);
36+
this.event = new ServiceInstancesChangedEvent(serviceName, Arrays.asList(instance));
37+
}
38+
39+
private ServiceInstance createInstance(String serviceName) {
40+
DefaultServiceInstance instance = new DefaultServiceInstance();
41+
instance.setServiceId(serviceName);
42+
instance.setServiceId(UUID.randomUUID().toString());
43+
instance.setHost("127.0.0.1");
44+
instance.setPort(8080);
45+
instance.setUri(URI.create("http://127.0.0.1:8080/info"));
46+
return instance;
47+
}
48+
49+
@Test
50+
public void testGetServiceName() {
51+
assertEquals(this.serviceName, this.event.getServiceName());
52+
assertEquals(this.serviceName, this.event.getSource());
53+
}
54+
55+
@Test
56+
public void testGetServiceInstances() {
57+
assertEquals(Arrays.asList(this.instance), this.event.getServiceInstances());
58+
assertEquals(this.instance, this.event.getServiceInstances().get(0));
59+
assertSame(this.instance, this.event.getServiceInstances().get(0));
60+
}
61+
62+
@Test
63+
public void testProcessed() {
64+
assertFalse(this.event.isProcessed());
65+
this.event.processed();
66+
assertTrue(this.event.isProcessed());
67+
}
68+
}

0 commit comments

Comments
 (0)