Skip to content

Commit

Permalink
Code optimization for StandaloneAdminTest
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-divago committed Mar 5, 2024
1 parent 713f7c6 commit 35c9589
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import org.apache.eventmesh.storage.standalone.broker.model.TopicMetadata;

import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -35,8 +35,6 @@ public class TestUtils {
public static final int OFF_SET = 0;
public static final int LENGTH = 5;
public static final int EXCEEDED_MESSAGE_STORE_WINDOW = 60 * 60 * 1000 + 1000;
public static final boolean TOPIC_EXISTS = true;
public static final boolean TOPIC_DO_NOT_EXISTS = false;

public static ConcurrentHashMap<TopicMetadata, MessageQueue> createDefaultMessageContainer() {
ConcurrentHashMap<TopicMetadata, MessageQueue> messageContainer = new ConcurrentHashMap<>(1);
Expand All @@ -63,7 +61,7 @@ public static CloudEvent createDefaultCloudEvent() {
}

public static List<CloudEvent> createCloudEvents() {
return Arrays.asList(createDefaultCloudEvent());
return Collections.singletonList(createDefaultCloudEvent());
}

public static MessageEntity createDefaultMessageEntity() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@
import static org.apache.eventmesh.storage.standalone.TestUtils.LENGTH;
import static org.apache.eventmesh.storage.standalone.TestUtils.OFF_SET;
import static org.apache.eventmesh.storage.standalone.TestUtils.TEST_TOPIC;
import static org.apache.eventmesh.storage.standalone.TestUtils.TOPIC_DO_NOT_EXISTS;
import static org.apache.eventmesh.storage.standalone.TestUtils.TOPIC_EXISTS;
import static org.apache.eventmesh.storage.standalone.TestUtils.createDefaultCloudEvent;
import static org.apache.eventmesh.storage.standalone.TestUtils.createDefaultMessageContainer;
import static org.apache.eventmesh.storage.standalone.TestUtils.createDefaultMessageEntity;

import org.apache.eventmesh.api.admin.TopicProperties;
import org.apache.eventmesh.storage.standalone.broker.StandaloneBroker;
import org.apache.eventmesh.storage.standalone.broker.model.MessageEntity;

Expand Down Expand Up @@ -72,8 +71,9 @@ public void testIsClosed() {

@Test
public void testGetTopic() throws Exception {
Assertions.assertNotNull(standaloneAdmin.getTopic());
Assertions.assertFalse(standaloneAdmin.getTopic().isEmpty());
List<TopicProperties> topicPropertiesList = standaloneAdmin.getTopic();
Assertions.assertNotNull(topicPropertiesList);
Assertions.assertFalse(topicPropertiesList.isEmpty());
}

@Test
Expand All @@ -90,7 +90,7 @@ public void testDeleteTopic() {

@Test
public void testGetEvent() throws Exception {
Mockito.when(standaloneBroker.checkTopicExist(TEST_TOPIC)).thenReturn(TOPIC_EXISTS);
Mockito.when(standaloneBroker.checkTopicExist(TEST_TOPIC)).thenReturn(Boolean.TRUE);
Mockito.when(standaloneBroker.getMessage(TEST_TOPIC, OFF_SET)).thenReturn(createDefaultCloudEvent());
List<CloudEvent> events = standaloneAdmin.getEvent(TEST_TOPIC, OFF_SET, LENGTH);
Assertions.assertNotNull(events);
Expand All @@ -99,7 +99,7 @@ public void testGetEvent() throws Exception {

@Test
public void testGetEvent_throwException() {
Mockito.when(standaloneBroker.checkTopicExist(TEST_TOPIC)).thenReturn(TOPIC_DO_NOT_EXISTS);
Mockito.when(standaloneBroker.checkTopicExist(TEST_TOPIC)).thenReturn(Boolean.FALSE);
Exception exception = Assertions.assertThrows(Exception.class, () -> standaloneAdmin.getEvent(TEST_TOPIC, OFF_SET, LENGTH));
Assertions.assertEquals("The topic name doesn't exist in the message queue", exception.getMessage());
}
Expand Down

0 comments on commit 35c9589

Please sign in to comment.