This is an automated email from the ASF dual-hosted git repository.
penzheng 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 fa5adf3a6 [type:test][ISSUE #4540] add test cases for
shenyu.admin.model.event.role (#4540) (#4965)
fa5adf3a6 is described below
commit fa5adf3a6c6e5e0a65b02d03480f64e10c054600
Author: devin <[email protected]>
AuthorDate: Sat Aug 12 21:57:39 2023 +0800
[type:test][ISSUE #4540] add test cases for shenyu.admin.model.event.role
(#4540) (#4965)
---
.../event/role/BatchRoleDeletedEventTest.java | 115 ++++++++++++++++++
.../model/event/role/RoleChangedEventTest.java | 133 +++++++++++++++++++++
.../model/event/role/RoleCreatedEventTest.java | 56 +++++++++
.../model/event/role/RoleUpdatedEventTest.java | 77 ++++++++++++
4 files changed, 381 insertions(+)
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/BatchRoleDeletedEventTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/BatchRoleDeletedEventTest.java
new file mode 100644
index 000000000..e24d293c8
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/BatchRoleDeletedEventTest.java
@@ -0,0 +1,115 @@
+/*
+ * 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.role;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.admin.model.entity.BaseDO;
+import org.apache.shenyu.admin.model.entity.RoleDO;
+import org.apache.shenyu.common.utils.UUIDUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * test cast for {@link BatchRoleDeletedEvent}.
+ */
+public final class BatchRoleDeletedEventTest {
+
+ private BatchRoleDeletedEvent batchRoleDeletedEventTest;
+
+ private BatchRoleDeletedEvent batchRoleDeletedEventEmptySourceTest;
+
+ private List<RoleDO> roleDOList;
+
+ private List<RoleDO> emptyRoleDOList;
+
+ @BeforeEach
+ public void setUp() {
+ roleDOList = batchInitRoleDOList();
+ emptyRoleDOList = new ArrayList<>();
+ batchRoleDeletedEventTest = new BatchRoleDeletedEvent(roleDOList,
"test-op");
+ batchRoleDeletedEventEmptySourceTest = new
BatchRoleDeletedEvent(emptyRoleDOList, "test-op");
+ }
+
+ @Test
+ public void testBuildContext() {
+ String expectedSelector = roleDOList
+ .stream()
+ .map(RoleDO::getRoleName)
+ .collect(Collectors.joining(","));
+ String expectedStr = String.format("the role[%s] is %s",
expectedSelector,
+
StringUtils.lowerCase(batchRoleDeletedEventTest.getType().getType().toString()));
+ assertEquals(expectedStr, batchRoleDeletedEventTest.buildContext());
+
+ String expectedEmptySelector = emptyRoleDOList
+ .stream()
+ .map(RoleDO::getRoleName)
+ .collect(Collectors.joining(","));
+ String expectedEmptyStr = String.format("the role[%s] is %s",
expectedEmptySelector,
+
StringUtils.lowerCase(batchRoleDeletedEventEmptySourceTest.getType().getType().toString()));
+ assertEquals(expectedEmptyStr,
batchRoleDeletedEventEmptySourceTest.buildContext());
+ }
+
+ @Test
+ public void testGetRoles() {
+ List<RoleDO> roles = batchRoleDeletedEventTest.getRoles();
+ assertEquals(roles, roleDOList);
+
+ List<RoleDO> emptyRoles =
batchRoleDeletedEventEmptySourceTest.getRoles();
+ assertEquals(emptyRoles, emptyRoleDOList);
+ }
+
+ @Test
+ public void testGetDeletedIds() {
+ List<String> ids = roleDOList
+ .stream()
+ .map(BaseDO::getId)
+ .collect(Collectors.toList());
+ assertEquals(ids, batchRoleDeletedEventTest.getDeletedIds());
+
+ List<String> emptyIds = emptyRoleDOList
+ .stream()
+ .map(BaseDO::getId)
+ .collect(Collectors.toList());
+ assertEquals(emptyIds,
batchRoleDeletedEventEmptySourceTest.getDeletedIds());
+ }
+
+ private List<RoleDO> batchInitRoleDOList() {
+ int defaultInsertNum = 10;
+ List<RoleDO> roleDOList = new ArrayList<>();
+ for (int i = 0; i < defaultInsertNum; i++) {
+ Timestamp now = new Timestamp(System.currentTimeMillis());
+ String id = UUIDUtils.getInstance().generateShortUuid();
+ RoleDO roleDO = RoleDO.builder()
+ .id(id)
+ .roleName("test-" + i)
+ .description("role-test-description")
+ .dateCreated(now)
+ .dateUpdated(now)
+ .build();
+ roleDOList.add(roleDO);
+ }
+ return roleDOList;
+ }
+}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/RoleChangedEventTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/RoleChangedEventTest.java
new file mode 100644
index 000000000..73c7c7904
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/RoleChangedEventTest.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.role;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.shenyu.admin.model.entity.RoleDO;
+import org.apache.shenyu.admin.model.enums.EventTypeEnum;
+import org.apache.shenyu.common.utils.UUIDUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Timestamp;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * test cast for {@link RoleChangedEvent}.
+ */
+public final class RoleChangedEventTest {
+
+ private RoleChangedEvent roleChangedEventRoleNameChangeTest;
+
+ private RoleChangedEvent roleChangedEventDescriptionChangeTest;
+
+ private RoleChangedEvent roleChangedEventTest;
+
+ private RoleChangedEvent roleChangedEventBeforeNullTest;
+
+ private RoleChangedEvent roleChangedEventWithoutChangeTest;
+
+ private RoleDO roleDO;
+
+ private RoleDO roleDORoleNameChange;
+
+ private RoleDO roleDODescriptionChange;
+
+ private RoleDO roleDOChange;
+
+ @BeforeEach
+ public void setUp() {
+ Timestamp now = new Timestamp(System.currentTimeMillis());
+ String id = UUIDUtils.getInstance().generateShortUuid();
+ roleDO = RoleDO.builder()
+ .id(id)
+ .roleName("test-role")
+ .description("test role")
+ .dateUpdated(now)
+ .dateCreated(now)
+ .build();
+ roleDORoleNameChange = RoleDO.builder()
+ .id(id)
+ .roleName("test-role-name-chang")
+ .description("test role")
+ .dateUpdated(now)
+ .dateCreated(now)
+ .build();
+ roleDODescriptionChange = RoleDO.builder()
+ .id(id)
+ .roleName("test-role")
+ .description("test role description change")
+ .dateUpdated(now)
+ .dateCreated(now)
+ .build();
+ roleDOChange = RoleDO.builder()
+ .id(id)
+ .roleName("test-role-name-change")
+ .description("test role description change")
+ .dateUpdated(now)
+ .dateCreated(now)
+ .build();
+ roleChangedEventRoleNameChangeTest =
+ new RoleChangedEvent(roleDORoleNameChange, roleDO,
EventTypeEnum.ROLE_UPDATE, "test-op");
+ roleChangedEventDescriptionChangeTest =
+ new RoleChangedEvent(roleDODescriptionChange, roleDO,
EventTypeEnum.ROLE_UPDATE, "test-op");
+ roleChangedEventTest =
+ new RoleChangedEvent(roleDOChange, roleDO,
EventTypeEnum.ROLE_UPDATE, "test-op");
+ roleChangedEventBeforeNullTest =
+ new RoleChangedEvent(roleDO, null, EventTypeEnum.ROLE_UPDATE,
"test-op");
+ roleChangedEventWithoutChangeTest =
+ new RoleChangedEvent(roleDO, roleDO,
EventTypeEnum.ROLE_UPDATE, "test-op");
+ }
+
+ @Test
+ public void testBuildContext() {
+ String roleUpdateStr =
StringUtils.lowerCase(EventTypeEnum.ROLE_UPDATE.getType().toString());
+
+ String roleNameChangeStr =
+ String.format("name[%s => %s] ", roleDO.getRoleName(),
roleDORoleNameChange.getRoleName());
+ String roleNameChangeExpectedStr = String.format("the role [%s] is %s
: %s",
+ roleDORoleNameChange.getRoleName(), roleUpdateStr,
roleNameChangeStr);
+ assertEquals(roleNameChangeExpectedStr,
roleChangedEventRoleNameChangeTest.buildContext());
+
+ String descriptionChangeStr =
+ String.format("disc[%s => %s] ", roleDO.getDescription(),
roleDODescriptionChange.getDescription());
+ String descriptionChangeExpectedStr = String.format("the role [%s] is
%s : %s",
+ roleDODescriptionChange.getRoleName(), roleUpdateStr,
descriptionChangeStr);
+ assertEquals(descriptionChangeExpectedStr,
roleChangedEventDescriptionChangeTest.buildContext());
+
+ String changeStr = String.format("name[%s => %s] disc[%s => %s] ",
+ roleDO.getRoleName(), roleDOChange.getRoleName(),
+ roleDO.getDescription(), roleDOChange.getDescription());
+ String changeExpectedStr = String.format("the role [%s] is %s : %s",
+ roleDOChange.getRoleName(), roleUpdateStr, changeStr);
+ assertEquals(changeExpectedStr, roleChangedEventTest.buildContext());
+
+ String beforeNullExpectedStr = String.format("the role [%s] is %s",
roleDO.getRoleName(), roleUpdateStr);
+ assertEquals(beforeNullExpectedStr,
roleChangedEventBeforeNullTest.buildContext());
+
+ String withoutChangeExpectedStr = String.format("the role [%s] is %s :
it no change",
+ roleDO.getRoleName(), roleUpdateStr);
+ assertEquals(withoutChangeExpectedStr,
roleChangedEventWithoutChangeTest.buildContext());
+ }
+
+ @Test
+ public void testEventName() {
+ assertEquals("role", roleChangedEventTest.eventName());
+ }
+}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/RoleCreatedEventTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/RoleCreatedEventTest.java
new file mode 100644
index 000000000..a847716cb
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/RoleCreatedEventTest.java
@@ -0,0 +1,56 @@
+/*
+ * 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.role;
+
+import org.apache.shenyu.admin.model.entity.RoleDO;
+import org.apache.shenyu.common.utils.UUIDUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.sql.Timestamp;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * test cast for {@link RoleCreatedEvent}.
+ */
+public final class RoleCreatedEventTest {
+
+ private RoleCreatedEvent roleCreatedEvent;
+
+ private RoleDO roleDO;
+
+ @BeforeEach
+ public void setUp() {
+ Timestamp now = new Timestamp(System.currentTimeMillis());
+ String id = UUIDUtils.getInstance().generateShortUuid();
+ roleDO = RoleDO.builder()
+ .id(id)
+ .roleName("test-role")
+ .description("test role")
+ .dateUpdated(now)
+ .dateCreated(now)
+ .build();
+ roleCreatedEvent = new RoleCreatedEvent(roleDO, "test-op");
+ }
+
+ @Test
+ public void testGetRole() {
+ assertEquals(roleDO, roleCreatedEvent.getRole());
+ }
+}
diff --git
a/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/RoleUpdatedEventTest.java
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/RoleUpdatedEventTest.java
new file mode 100644
index 000000000..e43aa5204
--- /dev/null
+++
b/shenyu-admin/src/test/java/org/apache/shenyu/admin/model/event/role/RoleUpdatedEventTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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.role;
+
+import org.apache.shenyu.admin.model.entity.RoleDO;
+import org.apache.shenyu.common.utils.UUIDUtils;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import javax.validation.constraints.NotBlank;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * test cast for {@link RoleUpdatedEvent}.
+ */
+public final class RoleUpdatedEventTest {
+
+ private RoleUpdatedEvent roleUpdatedEvent;
+
+ private RoleDO roleDO;
+
+ private List<@NotBlank String> permissionList;
+
+ @BeforeEach
+ public void setUp() {
+ Timestamp now = new Timestamp(System.currentTimeMillis());
+ String id = UUIDUtils.getInstance().generateShortUuid();
+ roleDO = RoleDO.builder()
+ .id(id)
+ .roleName("test-role")
+ .description("test role")
+ .dateUpdated(now)
+ .dateCreated(now)
+ .build();
+ RoleDO roleBefore;
+ roleBefore = RoleDO.builder()
+ .id(id)
+ .roleName("test-role-before")
+ .description("test role")
+ .dateUpdated(now)
+ .dateCreated(now)
+ .build();
+ permissionList = new ArrayList<>();
+ permissionList.add("test-permission1");
+ permissionList.add("test-permission2");
+ roleUpdatedEvent = new RoleUpdatedEvent(roleDO, roleBefore, "test-op",
permissionList);
+ }
+
+ @Test
+ public void testGetRole() {
+ assertEquals(roleDO, roleUpdatedEvent.getRole());
+ }
+
+ @Test
+ public void testGetNewPermission() {
+ assertEquals(permissionList, roleUpdatedEvent.getNewPermission());
+ }
+}