|
|
@@ -1,16 +1,14 @@
|
|
|
package com.etotem.cfc.service;
|
|
|
|
|
|
-import com.etotem.cfc.entity.Supplier;
|
|
|
-import com.etotem.cfc.entity.SupplyRelationship;
|
|
|
import com.etotem.cfc.entity.SupplySystem;
|
|
|
-import com.etotem.cfc.mapper.SupplierMapper;
|
|
|
-import com.etotem.cfc.mapper.SupplyRelationshipMapper;
|
|
|
+import com.etotem.cfc.entity.SupplySystemMember;
|
|
|
+import com.etotem.cfc.mapper.SupplySystemMemberMapper;
|
|
|
+import com.etotem.cfc.mapper.UserMapper;
|
|
|
import org.junit.jupiter.api.BeforeEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.mockito.Mock;
|
|
|
import org.mockito.MockitoAnnotations;
|
|
|
|
|
|
-import java.math.BigDecimal;
|
|
|
import java.util.*;
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
@@ -22,22 +20,20 @@ import static org.mockito.Mockito.*;
|
|
|
*
|
|
|
* SupplySystemService 继承 ServiceImpl<SupplySystemMapper, SupplySystem>。
|
|
|
* supplySystemMapper 来自父类,不能直接注入。使用 spy + doReturn 模拟。
|
|
|
- * supplierMapper 和 supplyRelationshipMapper 是直接 @Resource 字段,可以 mock 注入。
|
|
|
*
|
|
|
* 测试范围:
|
|
|
- * - getAllSystems / getActiveSystems (ServiceImpl.list())
|
|
|
- * - createSystem / updateSystem (ServiceImpl.save/updateById)
|
|
|
- * - toggleStatus (this.getById + this.updateById)
|
|
|
- * - getSuppliers / createSupplier / updateSupplier / deleteSupplier
|
|
|
- * - getRelationsByProduct / getRelationsBySupplier / createRelationship / removeRelationshipByProduct
|
|
|
+ * - list / detail / listAll
|
|
|
+ * - create / update
|
|
|
+ * - toggleStatus
|
|
|
+ * - memberList / addMember / removeMember / changeRole
|
|
|
*/
|
|
|
public class SupplySystemServiceTest {
|
|
|
|
|
|
@Mock
|
|
|
- private SupplierMapper supplierMapper;
|
|
|
+ private SupplySystemMemberMapper supplySystemMemberMapper;
|
|
|
|
|
|
@Mock
|
|
|
- private SupplyRelationshipMapper supplyRelationshipMapper;
|
|
|
+ private UserMapper userMapper;
|
|
|
|
|
|
private SupplySystemService service;
|
|
|
|
|
|
@@ -45,10 +41,9 @@ public class SupplySystemServiceTest {
|
|
|
public void setup() throws Exception {
|
|
|
MockitoAnnotations.openMocks(this);
|
|
|
service = new SupplySystemService();
|
|
|
- // supplierMapper 和 supplyRelationshipMapper 是真实字段
|
|
|
- setField(service, "supplierMapper", supplierMapper);
|
|
|
- setField(service, "supplyRelationshipMapper", supplyRelationshipMapper);
|
|
|
- // supplySystemMapper 来自 ServiceImpl 父类,不能直接注入,使用 spy 模拟
|
|
|
+
|
|
|
+ setField(service, "supplySystemMemberMapper", supplySystemMemberMapper);
|
|
|
+ setField(service, "userMapper", userMapper);
|
|
|
}
|
|
|
|
|
|
private static void setField(Object target, String fieldName, Object value) throws Exception {
|
|
|
@@ -57,49 +52,35 @@ public class SupplySystemServiceTest {
|
|
|
f.set(target, value);
|
|
|
}
|
|
|
|
|
|
- // --- SupplySystem CRUD tests ---
|
|
|
+ // ========== SupplySystem CRUD ==========
|
|
|
|
|
|
@Test
|
|
|
- public void getAllSystems_returnsAll() {
|
|
|
- SupplySystem s1 = mockSystem(1L, "SystemA", "ACTIVE");
|
|
|
- SupplySystem s2 = mockSystem(2L, "SystemB", "INACTIVE");
|
|
|
+ public void listAll_returnsAll() {
|
|
|
SupplySystemService spy = spy(service);
|
|
|
- doReturn(Arrays.asList(s1, s2)).when(spy).list();
|
|
|
+ doReturn(Arrays.asList(mockSystem(1L), mockSystem(2L))).when(spy).list(any());
|
|
|
|
|
|
- List<SupplySystem> result = spy.getAllSystems();
|
|
|
+ List<SupplySystem> result = spy.listAll();
|
|
|
|
|
|
assertEquals(2, result.size());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void getActiveSystems_returnsOnlyActive() {
|
|
|
- SupplySystem active = mockSystem(1L, "ActiveSys", "ACTIVE");
|
|
|
- SupplySystemService spy = spy(service);
|
|
|
- doReturn(Collections.singletonList(active)).when(spy).list();
|
|
|
-
|
|
|
- List<SupplySystem> result = spy.getActiveSystems();
|
|
|
-
|
|
|
- assertEquals(1, result.size());
|
|
|
- assertEquals("ACTIVE", result.get(0).getStatus());
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getActiveSystems_emptyList() {
|
|
|
+ public void listAll_empty() {
|
|
|
SupplySystemService spy = spy(service);
|
|
|
- doReturn(Collections.emptyList()).when(spy).list();
|
|
|
+ doReturn(Collections.emptyList()).when(spy).list(any());
|
|
|
|
|
|
- List<SupplySystem> result = spy.getActiveSystems();
|
|
|
+ List<SupplySystem> result = spy.listAll();
|
|
|
|
|
|
assertTrue(result.isEmpty());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void createSystem_setsTimestamps() {
|
|
|
- SupplySystem system = mockSystem(null, "NewSystem", "ACTIVE");
|
|
|
+ public void create_setsTimestamps() {
|
|
|
+ SupplySystem system = mockSystem(null);
|
|
|
SupplySystemService spy = spy(service);
|
|
|
- doReturn(true).when(spy).save(system);
|
|
|
+ doReturn(true).when(spy).save(any());
|
|
|
|
|
|
- spy.createSystem(system);
|
|
|
+ spy.create(system);
|
|
|
|
|
|
assertNotNull(system.getCreatedAt());
|
|
|
assertNotNull(system.getUpdatedAt());
|
|
|
@@ -107,21 +88,53 @@ public class SupplySystemServiceTest {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void updateSystem_updatesTimestamp() {
|
|
|
- SupplySystem system = mockSystem(1L, "UpdatedSystem", "ACTIVE");
|
|
|
+ public void create_failsWithoutAdminId() {
|
|
|
+ SupplySystem system = new SupplySystem();
|
|
|
+ system.setAdminId(null);
|
|
|
+
|
|
|
+ assertThrows(IllegalArgumentException.class, () -> service.create(system));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void update_updatesTimestamp() {
|
|
|
+ SupplySystem system = mockSystem(1L);
|
|
|
SupplySystemService spy = spy(service);
|
|
|
- doReturn(true).when(spy).updateById(system);
|
|
|
+ doReturn(true).when(spy).updateById(any());
|
|
|
|
|
|
- boolean result = spy.updateSystem(system);
|
|
|
+ boolean result = spy.update(system);
|
|
|
|
|
|
assertTrue(result);
|
|
|
assertNotNull(system.getUpdatedAt());
|
|
|
verify(spy).updateById(system);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void detail_returnsById() {
|
|
|
+ SupplySystemService spy = spy(service);
|
|
|
+ SupplySystem expected = mockSystem(1L);
|
|
|
+ doReturn(expected).when(spy).getById(1L);
|
|
|
+
|
|
|
+ SupplySystem result = spy.detail(1L);
|
|
|
+
|
|
|
+ assertEquals(expected.getId(), result.getId());
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void detail_notFound() {
|
|
|
+ SupplySystemService spy = spy(service);
|
|
|
+ doReturn(null).when(spy).getById(999L);
|
|
|
+
|
|
|
+ SupplySystem result = spy.detail(999L);
|
|
|
+
|
|
|
+ assertNull(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ // ========== toggleStatus ==========
|
|
|
+
|
|
|
@Test
|
|
|
public void toggleStatus_activeToInactive() {
|
|
|
- SupplySystem system = mockSystem(1L, "ToggleSys", "ACTIVE");
|
|
|
+ SupplySystem system = mockSystem(1L);
|
|
|
+ system.setStatus("active");
|
|
|
SupplySystemService spy = spy(service);
|
|
|
doReturn(system).when(spy).getById(1L);
|
|
|
doReturn(true).when(spy).updateById(system);
|
|
|
@@ -129,12 +142,13 @@ public class SupplySystemServiceTest {
|
|
|
boolean result = spy.toggleStatus(1L);
|
|
|
|
|
|
assertTrue(result);
|
|
|
- assertEquals("INACTIVE", system.getStatus());
|
|
|
+ assertEquals("disabled", system.getStatus());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void toggleStatus_inactiveToActive() {
|
|
|
- SupplySystem system = mockSystem(1L, "ToggleSys", "INACTIVE");
|
|
|
+ SupplySystem system = mockSystem(1L);
|
|
|
+ system.setStatus("disabled");
|
|
|
SupplySystemService spy = spy(service);
|
|
|
doReturn(system).when(spy).getById(1L);
|
|
|
doReturn(true).when(spy).updateById(system);
|
|
|
@@ -142,7 +156,7 @@ public class SupplySystemServiceTest {
|
|
|
boolean result = spy.toggleStatus(1L);
|
|
|
|
|
|
assertTrue(result);
|
|
|
- assertEquals("ACTIVE", system.getStatus());
|
|
|
+ assertEquals("active", system.getStatus());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
@@ -155,159 +169,118 @@ public class SupplySystemServiceTest {
|
|
|
assertFalse(result);
|
|
|
}
|
|
|
|
|
|
- // --- Supplier tests ---
|
|
|
+ // ========== member management ==========
|
|
|
|
|
|
@Test
|
|
|
- public void getSuppliers_returnsBySystemId() {
|
|
|
- Supplier s = mockSupplier(1L, "SupplierA");
|
|
|
- when(supplierMapper.selectList(any())).thenReturn(Collections.singletonList(s));
|
|
|
+ public void memberList_returnsBySystemId() {
|
|
|
+ SupplySystemMember m = mockMember(1L, 1L, 100L);
|
|
|
+ when(supplySystemMemberMapper.selectList(any())).thenReturn(Collections.singletonList(m));
|
|
|
|
|
|
- List<Supplier> result = service.getSuppliers(1L);
|
|
|
+ List<SupplySystemMember> result = service.memberList(1L);
|
|
|
|
|
|
assertEquals(1, result.size());
|
|
|
- assertEquals("SupplierA", result.get(0).getName());
|
|
|
+ assertEquals(100L, result.get(0).getUserId());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void getSuppliers_empty() {
|
|
|
- when(supplierMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
+ public void memberList_empty() {
|
|
|
+ when(supplySystemMemberMapper.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
|
|
|
- List<Supplier> result = service.getSuppliers(999L);
|
|
|
+ List<SupplySystemMember> result = service.memberList(999L);
|
|
|
|
|
|
assertTrue(result.isEmpty());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void createSupplier_setsDefaults() {
|
|
|
- Supplier supplier = mockSupplier(null, "NewSupplier");
|
|
|
-
|
|
|
- Supplier result = service.createSupplier(supplier);
|
|
|
-
|
|
|
- assertEquals("ACTIVE", supplier.getStatus());
|
|
|
- assertNotNull(supplier.getCreatedAt());
|
|
|
- assertNotNull(supplier.getUpdatedAt());
|
|
|
- verify(supplierMapper).insert(supplier);
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void updateSupplier_updatesTimestamp() {
|
|
|
- Supplier supplier = mockSupplier(1L, "UpdatedSupplier");
|
|
|
- when(supplierMapper.updateById(supplier)).thenReturn(1);
|
|
|
-
|
|
|
- boolean result = service.updateSupplier(supplier);
|
|
|
-
|
|
|
- assertTrue(result);
|
|
|
- assertNotNull(supplier.getUpdatedAt());
|
|
|
- verify(supplierMapper).updateById(supplier);
|
|
|
+ public void addMember_success() {
|
|
|
+ when(userMapper.selectById(100L)).thenReturn(new com.etotem.cfc.entity.User() {{
|
|
|
+ setId(100L);
|
|
|
+ }});
|
|
|
+ when(supplySystemMemberMapper.insert(any())).thenReturn(1);
|
|
|
+
|
|
|
+ SupplySystemMember result = service.addMember(1L, 100L);
|
|
|
+
|
|
|
+ assertNotNull(result);
|
|
|
+ assertEquals(1L, result.getSystemId().longValue());
|
|
|
+ assertEquals(100L, result.getUserId().longValue());
|
|
|
+ assertEquals("member", result.getRole());
|
|
|
+ verify(supplySystemMemberMapper).insert(any());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void updateSupplier_notFound() {
|
|
|
- Supplier supplier = mockSupplier(999L, "NotExist");
|
|
|
- when(supplierMapper.updateById(supplier)).thenReturn(0);
|
|
|
+ public void addMember_userNotFound() {
|
|
|
+ when(userMapper.selectById(999L)).thenReturn(null);
|
|
|
+ when(supplySystemMemberMapper.insert(any())).thenReturn(1);
|
|
|
|
|
|
- boolean result = service.updateSupplier(supplier);
|
|
|
+ SupplySystemMember result = service.addMember(1L, 999L);
|
|
|
|
|
|
- assertFalse(result);
|
|
|
+ assertNotNull(result);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void deleteSupplier_deletesRelationsFirst() {
|
|
|
- when(supplierMapper.deleteById(1L)).thenReturn(1);
|
|
|
+ public void removeMember_success() {
|
|
|
+ SupplySystemMember member = mockMember(1L, 1L, 100L);
|
|
|
+ when(supplySystemMemberMapper.selectById(1L)).thenReturn(member);
|
|
|
+ when(supplySystemMemberMapper.updateById(any())).thenReturn(1);
|
|
|
|
|
|
- boolean result = service.deleteSupplier(1L);
|
|
|
+ boolean result = service.removeMember(1L);
|
|
|
|
|
|
assertTrue(result);
|
|
|
- verify(supplyRelationshipMapper).delete(any());
|
|
|
- verify(supplierMapper).deleteById(1L);
|
|
|
+ verify(supplySystemMemberMapper).selectById(1L);
|
|
|
+ verify(supplySystemMemberMapper).updateById(any());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void deleteSupplier_notFound() {
|
|
|
- when(supplierMapper.deleteById(999L)).thenReturn(0);
|
|
|
+ public void removeMember_notFound() {
|
|
|
+ when(supplySystemMemberMapper.selectById(999L)).thenReturn(null);
|
|
|
|
|
|
- boolean result = service.deleteSupplier(999L);
|
|
|
+ boolean result = service.removeMember(999L);
|
|
|
|
|
|
assertFalse(result);
|
|
|
}
|
|
|
|
|
|
- // --- SupplyRelationship tests ---
|
|
|
-
|
|
|
- @Test
|
|
|
- public void getRelationsByProduct_returnsByProductId() {
|
|
|
- SupplyRelationship r = mockRelation(1L, 100L, 1L);
|
|
|
- when(supplyRelationshipMapper.selectList(any())).thenReturn(Collections.singletonList(r));
|
|
|
-
|
|
|
- List<SupplyRelationship> result = service.getRelationsByProduct(1L);
|
|
|
-
|
|
|
- assertEquals(1, result.size());
|
|
|
- assertEquals(100L, result.get(0).getProductId());
|
|
|
- }
|
|
|
-
|
|
|
@Test
|
|
|
- public void getRelationsBySupplier_returnsBySupplierId() {
|
|
|
- SupplyRelationship r = mockRelation(1L, 100L, 1L);
|
|
|
- when(supplyRelationshipMapper.selectList(any())).thenReturn(Collections.singletonList(r));
|
|
|
+ public void changeRole_success() {
|
|
|
+ SupplySystemMember member = mockMember(1L, 1L, 100L);
|
|
|
+ when(supplySystemMemberMapper.selectById(1L)).thenReturn(member);
|
|
|
+ when(supplySystemMemberMapper.updateById(member)).thenReturn(1);
|
|
|
|
|
|
- List<SupplyRelationship> result = service.getRelationsBySupplier(1L);
|
|
|
+ boolean result = service.changeRole(1L, "admin");
|
|
|
|
|
|
- assertEquals(1, result.size());
|
|
|
- assertEquals(1L, result.get(0).getSupplierId());
|
|
|
+ assertTrue(result);
|
|
|
+ assertEquals("admin", member.getRole());
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- public void createRelationship_setsDefaults() {
|
|
|
- SupplyRelationship relationship = mockRelation(null, 100L, 1L);
|
|
|
-
|
|
|
- SupplyRelationship result = service.createRelationship(relationship);
|
|
|
+ public void changeRole_memberNotFound() {
|
|
|
+ when(supplySystemMemberMapper.selectById(999L)).thenReturn(null);
|
|
|
|
|
|
- assertEquals("ACTIVE", relationship.getStatus());
|
|
|
- assertNotNull(relationship.getCreatedAt());
|
|
|
- assertNotNull(relationship.getUpdatedAt());
|
|
|
- verify(supplyRelationshipMapper).insert(relationship);
|
|
|
- }
|
|
|
-
|
|
|
- @Test
|
|
|
- public void removeRelationshipByProduct_deletesAll() {
|
|
|
- service.removeRelationshipByProduct(1L);
|
|
|
+ boolean result = service.changeRole(999L, "admin");
|
|
|
|
|
|
- verify(supplyRelationshipMapper).delete(any());
|
|
|
+ assertFalse(result);
|
|
|
}
|
|
|
|
|
|
- // --- helpers ---
|
|
|
+ // ========== helpers ==========
|
|
|
|
|
|
- private SupplySystem mockSystem(Long id, String name, String status) {
|
|
|
+ private SupplySystem mockSystem(Long id) {
|
|
|
SupplySystem s = new SupplySystem();
|
|
|
s.setId(id);
|
|
|
- s.setName(name);
|
|
|
- s.setStatus(status);
|
|
|
- s.setPlatformProfitRate(BigDecimal.valueOf(0.1));
|
|
|
- s.setCreatedAt(new Date());
|
|
|
- s.setUpdatedAt(new Date());
|
|
|
- return s;
|
|
|
- }
|
|
|
-
|
|
|
- private Supplier mockSupplier(Long id, String name) {
|
|
|
- Supplier s = new Supplier();
|
|
|
- s.setId(id);
|
|
|
- s.setName(name);
|
|
|
- s.setSupplySystemId(1L);
|
|
|
- s.setCommissionRate(BigDecimal.valueOf(0.05));
|
|
|
- s.setStatus("ACTIVE");
|
|
|
+ s.setName("TestSystem");
|
|
|
+ s.setStatus("active");
|
|
|
+ s.setAdminId(1L);
|
|
|
s.setCreatedAt(new Date());
|
|
|
s.setUpdatedAt(new Date());
|
|
|
return s;
|
|
|
}
|
|
|
|
|
|
- private SupplyRelationship mockRelation(Long id, Long productId, Long supplierId) {
|
|
|
- SupplyRelationship r = new SupplyRelationship();
|
|
|
- r.setId(id);
|
|
|
- r.setProductId(productId);
|
|
|
- r.setSupplierId(supplierId);
|
|
|
- r.setRelationType("SUPPLY");
|
|
|
- r.setStatus("ACTIVE");
|
|
|
- r.setCreatedAt(new Date());
|
|
|
- r.setUpdatedAt(new Date());
|
|
|
- return r;
|
|
|
+ private SupplySystemMember mockMember(Long id, Long systemId, Long userId) {
|
|
|
+ SupplySystemMember m = new SupplySystemMember();
|
|
|
+ m.setId(id);
|
|
|
+ m.setSystemId(systemId);
|
|
|
+ m.setUserId(userId);
|
|
|
+ m.setRole("operator");
|
|
|
+ m.setStatus("active");
|
|
|
+ m.setJoinedAt(new Date());
|
|
|
+ return m;
|
|
|
}
|
|
|
-}
|
|
|
+}
|