This is an automated email from the ASF dual-hosted git repository.
xiaoyu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 056bdefb6 test admin: add selector event unit test. (#4971)
056bdefb6 is described below
commit 056bdefb642bef9730f22494512619411aac21de
Author: Hadoo <[email protected]>
AuthorDate: Mon Aug 14 14:23:14 2023 +0800
test admin: add selector event unit test. (#4971)
* [type: test]: Add selector event test
* [type: test]: Add selector event test
* [type: test]: Add selector event test
* [type: test]: Add selector event test
---------
Co-authored-by: hanchao53 <[email protected]>
Co-authored-by: xiaoyu <[email protected]>
---
.../selector/BatchSelectorDeletedEventTest.java | 133 +++++++++++++++++++
.../event/selector/SelectorChangedEventTest.java | 145 +++++++++++++++++++++
.../event/selector/SelectorCreatedEventTest.java | 75 +++++++++++
.../event/selector/SelectorUpdatedEventTest.java | 75 +++++++++++
4 files changed, 428 insertions(+)
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/BatchSelectorDeletedEventTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/BatchSelectorDeletedEventTest.java
new file mode 100644
index 000000000..3aea7c3e6
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/BatchSelectorDeletedEventTest.java
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.model.event.selector;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.admin.model.dto.SelectorConditionDTO;
+import org.apache.shenyu.admin.model.dto.SelectorDTO;
+import org.apache.shenyu.admin.model.entity.BaseDO;
+import org.apache.shenyu.admin.model.entity.PluginDO;
+import org.apache.shenyu.admin.model.entity.SelectorDO;
+import org.apache.shenyu.common.enums.SelectorTypeEnum;
+import org.apache.shenyu.common.utils.ListUtil;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
+
+/**
+ * Test case for {@linkplain BatchSelectorDeletedEvent}.
+ */
+public final class BatchSelectorDeletedEventTest {
+ private SelectorDO selectorDO;
+
+ private List<SelectorDO> selectorDOList;
+
+ private PluginDO pluginDO;
+
+ private List<PluginDO> pluginDOList;
+
+ private BatchSelectorDeletedEvent batchSelectorDeletedEvent;
+
+ @BeforeEach
+ public void init() {
+ selectorDO = buildSelectorDO();
+ selectorDOList = Collections.singletonList(selectorDO);
+
+ pluginDO = buildPluginDO();
+ pluginDOList = Collections.singletonList(pluginDO);
+
+ batchSelectorDeletedEvent = new
BatchSelectorDeletedEvent(selectorDOList, "test-operator", pluginDOList);
+ }
+
+ @Test
+ void buildContext() {
+ String expectMsg = String.format("the selector[%s] is %s",
selectorDO.getName(),
StringUtils.lowerCase(batchSelectorDeletedEvent.getType().getType().toString()));
+
+ String actualMsg = batchSelectorDeletedEvent.buildContext();
+
+ assertEquals(expectMsg, actualMsg);
+ }
+
+ @Test
+ void getSelectors() {
+ List<SelectorDO> selectors = batchSelectorDeletedEvent.getSelectors();
+
+ assertNotNull(selectors);
+ assertEquals(selectors.size(), selectorDOList.size());
+ }
+
+ @Test
+ void findPluginBySelectorId() {
+ PluginDO findPluginDO =
batchSelectorDeletedEvent.findPluginBySelectorId(selectorDO.getId());
+
+ assertEquals(pluginDO.getId(), findPluginDO.getId());
+ }
+
+ @Test
+ void getPlugins() {
+ List<PluginDO> plugins = batchSelectorDeletedEvent.getPlugins();
+
+ assertArrayEquals(pluginDOList.toArray(new PluginDO[0]),
plugins.toArray(new PluginDO[0]));
+ }
+
+ @Test
+ void getDeletedIds() {
+ List<String> expectDeleteIds = ListUtil.map(selectorDOList,
BaseDO::getId);
+
+ List<String> actualDeleteIds =
batchSelectorDeletedEvent.getDeletedIds();
+
+ assertArrayEquals(expectDeleteIds.toArray(new String[0]),
actualDeleteIds.toArray(new String[0]));
+ }
+
+ private SelectorDO buildSelectorDO() {
+ SelectorDTO selectorDTO = new SelectorDTO();
+ selectorDTO.setId("456");
+ selectorDTO.setPluginId("789");
+ selectorDTO.setName("kuan");
+ selectorDTO.setType(SelectorTypeEnum.FULL_FLOW.getCode());
+ selectorDTO.setHandle("[{\"upstreamHost\": \"127.0.0.1\",
\"protocol\": \"http://\", \"upstreamUrl\": \"anotherUrl\"}]");
+ SelectorConditionDTO selectorConditionDTO1 = new
SelectorConditionDTO();
+ selectorConditionDTO1.setId("111");
+ selectorConditionDTO1.setSelectorId("456");
+ SelectorConditionDTO selectorConditionDTO2 = new
SelectorConditionDTO();
+ selectorConditionDTO2.setId("222");
+ selectorConditionDTO2.setSelectorId("456");
+ selectorDTO.setSelectorConditions(Arrays.asList(selectorConditionDTO1,
selectorConditionDTO2));
+ SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO);
+ Timestamp now = Timestamp.valueOf(LocalDateTime.now());
+ selectorDO.setDateCreated(now);
+ selectorDO.setDateUpdated(now);
+ return selectorDO;
+ }
+
+ private PluginDO buildPluginDO() {
+ PluginDO pluginDO = new PluginDO();
+ pluginDO.setName("test");
+ pluginDO.setId("789");
+ return pluginDO;
+ }
+}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEventTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEventTest.java
new file mode 100644
index 000000000..96b964838
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorChangedEventTest.java
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.model.event.selector;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.admin.model.dto.SelectorDTO;
+import org.apache.shenyu.admin.model.entity.SelectorDO;
+import org.apache.shenyu.admin.model.enums.EventTypeEnum;
+import org.apache.shenyu.common.enums.SelectorTypeEnum;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Test case for {@linkplain SelectorChangedEvent}.
+ */
+public final class SelectorChangedEventTest {
+ private SelectorDO before;
+
+ private SelectorDO after;
+
+ @BeforeEach
+ public void init() {
+ before = buildBeforeSelectorDO();
+ after = buildAfterSelectorDO();
+ }
+
+ @Test
+ void buildContextAndBeforeIsNull() {
+ SelectorChangedEvent selectorChangedEvent
+ = new SelectorChangedEvent(before, null,
EventTypeEnum.SELECTOR_CREATE, "test-operator");
+
+ SelectorDO after = (SelectorDO) selectorChangedEvent.getAfter();
+ String expectMsg = String.format("the selector [%s] is %s",
after.getName(),
StringUtils.lowerCase(selectorChangedEvent.getType().getType().toString()));
+
+ String actualMsg = selectorChangedEvent.buildContext();
+
+ assertEquals(expectMsg, actualMsg);
+ }
+
+ @Test
+ void buildContextAndBeforeNotNullAndNoChange() {
+ SelectorChangedEvent selectorChangedEvent
+ = new SelectorChangedEvent(before, before,
EventTypeEnum.SELECTOR_CREATE, "test-operator");
+
+ String changeMsg = "it no change";
+ SelectorDO after = (SelectorDO) selectorChangedEvent.getAfter();
+ String expectMsg = String.format("the selector [%s] is %s : %s",
after.getName(),
StringUtils.lowerCase(selectorChangedEvent.getType().getType().toString()),
changeMsg);
+
+ String actualMsg = selectorChangedEvent.buildContext();
+
+ assertEquals(expectMsg, actualMsg);
+ }
+
+ @Test
+ void buildContextAndBeforeNotNullAndAllChange() {
+ SelectorChangedEvent selectorChangedEvent
+ = new SelectorChangedEvent(before, after,
EventTypeEnum.SELECTOR_CREATE, "test-operator");
+
+ SelectorDO before = (SelectorDO) selectorChangedEvent.getBefore();
+ SelectorDO after = (SelectorDO) selectorChangedEvent.getAfter();
+
+ final StringBuilder builder = new StringBuilder();
+ builder.append(String.format("name[%s => %s] ", before.getName(),
after.getName()));
+ builder.append(String.format("handle[%s => %s] ", before.getHandle(),
after.getHandle()));
+ builder.append(String.format("type[%s => %s] ", before.getType(),
after.getType()));
+ builder.append(String.format("enable[%s => %s] ", before.getEnabled(),
after.getEnabled()));
+ builder.append(String.format("sort[%s => %s] ", before.getSort(),
after.getSort()));
+ builder.append(String.format("loged[%s => %s] ", before.getLoged(),
after.getLoged()));
+
+ String changeMsg = builder.toString();
+ String expectMsg = String.format("the selector [%s] is %s : %s",
after.getName(),
StringUtils.lowerCase(selectorChangedEvent.getType().getType().toString()),
changeMsg);
+
+ String actualMsg = selectorChangedEvent.buildContext();
+
+ assertEquals(expectMsg, actualMsg);
+ }
+
+ @Test
+ void eventName() {
+ SelectorChangedEvent selectorChangedEvent
+ = new SelectorChangedEvent(before, null,
EventTypeEnum.SELECTOR_CREATE, "test-operator");
+
+ String expectName = "selector";
+
+ String actualName = selectorChangedEvent.eventName();
+
+ assertEquals(expectName, actualName);
+ }
+
+ private SelectorDO buildBeforeSelectorDO() {
+ SelectorDTO selectorDTO = new SelectorDTO();
+ selectorDTO.setId("456");
+ selectorDTO.setPluginId("789");
+ selectorDTO.setName("kuan");
+ selectorDTO.setType(SelectorTypeEnum.FULL_FLOW.getCode());
+ selectorDTO.setHandle("[{\"upstreamHost\": \"127.0.0.1\",
\"protocol\": \"http://\", \"upstreamUrl\": \"anotherUrl\"}]");
+
+ SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO);
+ Timestamp now = Timestamp.valueOf(LocalDateTime.now());
+ selectorDO.setDateCreated(now);
+ selectorDO.setDateUpdated(now);
+ selectorDO.setEnabled(true);
+ selectorDO.setSort(0);
+ selectorDO.setLoged(true);
+ return selectorDO;
+ }
+
+ private SelectorDO buildAfterSelectorDO() {
+ SelectorDTO selectorDTO = new SelectorDTO();
+ selectorDTO.setId("456");
+ selectorDTO.setPluginId("789");
+ selectorDTO.setName("kuan-after");
+ selectorDTO.setType(SelectorTypeEnum.CUSTOM_FLOW.getCode());
+ selectorDTO.setHandle("[{\"upstreamHost\": \"0.0.0.0\", \"protocol\":
\"http://\", \"upstreamUrl\": \"anotherUrl\"}]");
+
+ SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO);
+ Timestamp now = Timestamp.valueOf(LocalDateTime.now());
+ selectorDO.setDateCreated(now);
+ selectorDO.setDateUpdated(now);
+ selectorDO.setEnabled(false);
+ selectorDO.setSort(1);
+ selectorDO.setLoged(false);
+ return selectorDO;
+ }
+}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorCreatedEventTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorCreatedEventTest.java
new file mode 100644
index 000000000..bfa4af3aa
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorCreatedEventTest.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.model.event.selector;
+
+import org.apache.shenyu.admin.model.dto.SelectorConditionDTO;
+import org.apache.shenyu.admin.model.dto.SelectorDTO;
+import org.apache.shenyu.admin.model.entity.SelectorDO;
+import org.apache.shenyu.common.enums.SelectorTypeEnum;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Test case for {@linkplain SelectorCreatedEvent}.
+ */
+public final class SelectorCreatedEventTest {
+ private SelectorDO selectorDO;
+
+ private SelectorCreatedEvent selectorCreatedEvent;
+
+ @BeforeEach
+ public void init() {
+ selectorDO = buildSelectorDO();
+
+ selectorCreatedEvent = new SelectorCreatedEvent(selectorDO,
"test-operator");
+ }
+
+ @Test
+ void getSelector() {
+ SelectorDO selector = selectorCreatedEvent.getSelector();
+
+ assertEquals(selectorDO, selector);
+ }
+
+ private SelectorDO buildSelectorDO() {
+ SelectorDTO selectorDTO = new SelectorDTO();
+ selectorDTO.setId("456");
+ selectorDTO.setPluginId("789");
+ selectorDTO.setName("kuan");
+ selectorDTO.setType(SelectorTypeEnum.FULL_FLOW.getCode());
+ selectorDTO.setHandle("[{\"upstreamHost\": \"127.0.0.1\",
\"protocol\": \"http://\", \"upstreamUrl\": \"anotherUrl\"}]");
+ SelectorConditionDTO selectorConditionDTO1 = new
SelectorConditionDTO();
+ selectorConditionDTO1.setId("111");
+ selectorConditionDTO1.setSelectorId("456");
+ SelectorConditionDTO selectorConditionDTO2 = new
SelectorConditionDTO();
+ selectorConditionDTO2.setId("222");
+ selectorConditionDTO2.setSelectorId("456");
+ selectorDTO.setSelectorConditions(Arrays.asList(selectorConditionDTO1,
selectorConditionDTO2));
+ SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO);
+ Timestamp now = Timestamp.valueOf(LocalDateTime.now());
+ selectorDO.setDateCreated(now);
+ selectorDO.setDateUpdated(now);
+ return selectorDO;
+ }
+}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorUpdatedEventTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorUpdatedEventTest.java
new file mode 100644
index 000000000..a5992be27
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/selector/SelectorUpdatedEventTest.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.admin.model.event.selector;
+
+import org.apache.shenyu.admin.model.dto.SelectorConditionDTO;
+import org.apache.shenyu.admin.model.dto.SelectorDTO;
+import org.apache.shenyu.admin.model.entity.SelectorDO;
+import org.apache.shenyu.common.enums.SelectorTypeEnum;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Timestamp;
+import java.time.LocalDateTime;
+import java.util.Arrays;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Test case for {@linkplain SelectorUpdatedEvent}.
+ */
+public final class SelectorUpdatedEventTest {
+ private SelectorDO selectorDO;
+
+ private SelectorUpdatedEvent selectorUpdatedEvent;
+
+ @BeforeEach
+ public void init() {
+ selectorDO = buildSelectorDO();
+
+ selectorUpdatedEvent = new SelectorUpdatedEvent(selectorDO,
selectorDO, "test-operator");
+ }
+
+ @Test
+ void getSelector() {
+ SelectorDO selector = selectorUpdatedEvent.getSelector();
+
+ assertEquals(selectorDO, selector);
+ }
+
+ private SelectorDO buildSelectorDO() {
+ SelectorDTO selectorDTO = new SelectorDTO();
+ selectorDTO.setId("456");
+ selectorDTO.setPluginId("789");
+ selectorDTO.setName("kuan");
+ selectorDTO.setType(SelectorTypeEnum.FULL_FLOW.getCode());
+ selectorDTO.setHandle("[{\"upstreamHost\": \"127.0.0.1\",
\"protocol\": \"http://\", \"upstreamUrl\": \"anotherUrl\"}]");
+ SelectorConditionDTO selectorConditionDTO1 = new
SelectorConditionDTO();
+ selectorConditionDTO1.setId("111");
+ selectorConditionDTO1.setSelectorId("456");
+ SelectorConditionDTO selectorConditionDTO2 = new
SelectorConditionDTO();
+ selectorConditionDTO2.setId("222");
+ selectorConditionDTO2.setSelectorId("456");
+ selectorDTO.setSelectorConditions(Arrays.asList(selectorConditionDTO1,
selectorConditionDTO2));
+ SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO);
+ Timestamp now = Timestamp.valueOf(LocalDateTime.now());
+ selectorDO.setDateCreated(now);
+ selectorDO.setDateUpdated(now);
+ return selectorDO;
+ }
+}