Skip to content

Commit

Permalink
Refactor ComputeNodeOnlineDispatchEventBuilderTest (#32897)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Sep 15, 2024
1 parent 31bbe98 commit 8ffbfdc
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String getSubscribedKey() {

@Override
public Collection<Type> getSubscribedTypes() {
return Arrays.asList(Type.ADDED, Type.UPDATED, Type.DELETED);
return Arrays.asList(Type.ADDED, Type.DELETED);
}

@Override
Expand All @@ -59,16 +59,17 @@ public Optional<DispatchEvent> build(final DataChangedEvent event) {

private Optional<DispatchEvent> createInstanceEvent(final DataChangedEvent event) {
Matcher matcher = getInstanceOnlinePathMatcher(event.getKey());
if (matcher.find()) {
ComputeNodeData computeNodeData = new YamlComputeNodeDataSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlComputeNodeData.class));
InstanceMetaData instanceMetaData = InstanceMetaDataFactory.create(matcher.group(2),
InstanceType.valueOf(matcher.group(1).toUpperCase()), computeNodeData.getAttribute(), computeNodeData.getVersion());
if (Type.ADDED == event.getType()) {
return Optional.of(new InstanceOnlineEvent(instanceMetaData));
}
if (Type.DELETED == event.getType()) {
return Optional.of(new InstanceOfflineEvent(instanceMetaData));
}
if (!matcher.find()) {
return Optional.empty();
}
ComputeNodeData computeNodeData = new YamlComputeNodeDataSwapper().swapToObject(YamlEngine.unmarshal(event.getValue(), YamlComputeNodeData.class));
InstanceMetaData instanceMetaData = InstanceMetaDataFactory.create(
matcher.group(2), InstanceType.valueOf(matcher.group(1).toUpperCase()), computeNodeData.getAttribute(), computeNodeData.getVersion());
if (Type.ADDED == event.getType()) {
return Optional.of(new InstanceOnlineEvent(instanceMetaData));
}
if (Type.DELETED == event.getType()) {
return Optional.of(new InstanceOfflineEvent(instanceMetaData));
}
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ class ClusterStateDispatchEventBuilderTest {

private final ClusterStateDispatchEventBuilder builder = new ClusterStateDispatchEventBuilder();

@Test
void assertGetSubscribedKey() {
assertThat(builder.getSubscribedKey(), is("/nodes/compute_nodes/status"));
}

@Test
void assertBuildEventWhenDelete() {
assertFalse(builder.build(new DataChangedEvent("/nodes/compute_nodes/status", ClusterState.READ_ONLY.name(), Type.DELETED)).isPresent());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ComputeNodeOnlineDispatchEventBuilderTest {

private final ComputeNodeOnlineDispatchEventBuilder builder = new ComputeNodeOnlineDispatchEventBuilder();

@Test
void assertComputeNodeOnline() {
void assertGetSubscribedKey() {
assertThat(builder.getSubscribedKey(), is("/nodes/compute_nodes/online"));
}

@Test
void assertBuildWithInvalidInstanceOnlinePath() {
assertFalse(builder.build(new DataChangedEvent("/nodes/compute_nodes/online/foo", "{attribute: 127.0.0.1@3307,version: 1}", Type.ADDED)).isPresent());
}

@Test
void assertBuildComputeNodeOnlineEvent() {
Optional<DispatchEvent> actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/online/proxy/foo_instance_id", "{attribute: 127.0.0.1@3307,version: 1}", Type.ADDED));
assertTrue(actual.isPresent());
InstanceOnlineEvent event = (InstanceOnlineEvent) actual.get();
Expand All @@ -48,7 +59,7 @@ void assertComputeNodeOnline() {
}

@Test
void assertComputeNodeOffline() {
void assertBuildWithComputeNodeOfflineEvent() {
Optional<DispatchEvent> actual = builder.build(new DataChangedEvent("/nodes/compute_nodes/online/proxy/foo_instance_id", "{attribute: 127.0.0.1@3307,version: 1}", Type.DELETED));
assertTrue(actual.isPresent());
InstanceOfflineEvent event = (InstanceOfflineEvent) actual.get();
Expand All @@ -58,4 +69,9 @@ void assertComputeNodeOffline() {
assertThat(event.getInstanceMetaData().getVersion(), is("1"));
assertThat(event.getInstanceMetaData().getAttributes(), is("127.0.0.1@3307"));
}

@Test
void assertBuildWithInvalidOperationType() {
assertFalse(builder.build(new DataChangedEvent("/nodes/compute_nodes/online/proxy/foo_instance_id", "{attribute: 127.0.0.1@3307,version: 1}", Type.UPDATED)).isPresent());
}
}

0 comments on commit 8ffbfdc

Please sign in to comment.