This is an automated email from the ASF dual-hosted git repository.

wuzhiguo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/bigtop-manager.git


The following commit(s) were added to refs/heads/main by this push:
     new 7bc2d4b  BIGTOP-4240: Add ut cases for service classes (#84)
7bc2d4b is described below

commit 7bc2d4b215108de086c79dcbb16f03a3e3c519a9
Author: haopeng <[email protected]>
AuthorDate: Tue Oct 15 16:26:17 2024 +0800

    BIGTOP-4240: Add ut cases for service classes (#84)
---
 .../manager/server/service/ClusterServiceTest.java | 136 +++++++++++++++++++++
 .../manager/server/service/CommandServiceTest.java | 106 ++++++++++++++++
 .../server/service/ComponentServiceTest.java       |  54 ++++++++
 .../manager/server/service/ConfigServiceTest.java  |  90 ++++++++++++++
 .../server/service/HostComponentServiceTest.java   |  55 +++++++++
 .../manager/server/service/LoginServiceTest.java   |  79 ++++++++++++
 .../manager/server/service/UserServiceTest.java    |  74 +++++++++++
 7 files changed, 594 insertions(+)

diff --git 
a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/ClusterServiceTest.java
 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/ClusterServiceTest.java
new file mode 100644
index 0000000..2037a25
--- /dev/null
+++ 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/ClusterServiceTest.java
@@ -0,0 +1,136 @@
+/*
+ * 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
+ *
+ *    https://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.bigtop.manager.server.service;
+
+import org.apache.bigtop.manager.dao.po.ClusterPO;
+import org.apache.bigtop.manager.dao.po.RepoPO;
+import org.apache.bigtop.manager.dao.po.StackPO;
+import org.apache.bigtop.manager.dao.repository.ClusterDao;
+import org.apache.bigtop.manager.dao.repository.RepoDao;
+import org.apache.bigtop.manager.dao.repository.StackDao;
+import org.apache.bigtop.manager.server.enums.ApiExceptionEnum;
+import org.apache.bigtop.manager.server.exception.ApiException;
+import org.apache.bigtop.manager.server.model.dto.ClusterDTO;
+import org.apache.bigtop.manager.server.model.dto.RepoDTO;
+import org.apache.bigtop.manager.server.model.dto.ServiceDTO;
+import org.apache.bigtop.manager.server.model.dto.StackDTO;
+import org.apache.bigtop.manager.server.service.impl.ClusterServiceImpl;
+import org.apache.bigtop.manager.server.utils.StackUtils;
+
+import org.apache.commons.lang3.tuple.ImmutablePair;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.MockedStatic;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.CALLS_REAL_METHODS;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+public class ClusterServiceTest {
+    private static final String CLUSTER_NAME = "TestCluster";
+
+    @Mock
+    private ClusterDao clusterDao;
+
+    @Mock
+    private StackDao stackDao;
+
+    @Mock
+    private RepoDao repoDao;
+
+    @InjectMocks
+    private ClusterService clusterService = new ClusterServiceImpl();
+
+    @Mock
+    private HostService hostService;
+
+    private ClusterPO clusterPO;
+    private ClusterDTO clusterDTO;
+    Map<String, ImmutablePair<StackDTO, List<ServiceDTO>>> mockStackKeyMap = 
new HashMap<>();
+
+    @BeforeEach
+    public void setup() {
+        clusterPO = new ClusterPO();
+        clusterPO.setId(1L);
+        clusterPO.setClusterName(CLUSTER_NAME);
+
+        clusterDTO = new ClusterDTO();
+        clusterDTO.setClusterName(CLUSTER_NAME);
+        clusterDTO.setStackName("TestStack");
+        clusterDTO.setStackVersion("1.0.0");
+        RepoDTO repoDTO = new RepoDTO();
+        repoDTO.setArch("x86_64");
+        repoDTO.setOs("test");
+        clusterDTO.setRepoInfoList(List.of(repoDTO));
+        StackDTO stackDTO = new StackDTO();
+        stackDTO.setStackName("TestStack");
+        mockStackKeyMap.put(
+                StackUtils.fullStackName(clusterDTO.getStackName(), 
clusterDTO.getStackVersion()),
+                new ImmutablePair<>(stackDTO, new ArrayList<>()));
+    }
+
+    @Test
+    public void testListAndGetAndUpdate() {
+        when(clusterDao.findAllByJoin()).thenReturn(List.of(clusterPO));
+        assert clusterService.list().size() == 1;
+
+        assertEquals(
+                ApiExceptionEnum.CLUSTER_NOT_FOUND,
+                assertThrows(ApiException.class, () -> 
clusterService.get(1L)).getEx());
+
+        when(clusterDao.findByIdJoin(any())).thenReturn(clusterPO);
+        assert clusterService.get(1L).getClusterName().equals(CLUSTER_NAME);
+
+        ClusterDTO clusterDTO = new ClusterDTO();
+        clusterDTO.setClusterName(CLUSTER_NAME);
+        assert clusterService.update(1L, 
clusterDTO).getClusterName().equals(CLUSTER_NAME);
+    }
+
+    @Test
+    public void testSave() {
+        when(stackDao.findByStackNameAndStackVersion(any(), 
any())).thenReturn(new StackPO());
+        when(hostService.batchSave(any(), any())).thenReturn(null);
+        RepoPO repoPO = new RepoPO();
+        repoPO.setArch("x86_64");
+        repoPO.setOs("test");
+        when(repoDao.findAllByClusterId(any())).thenReturn(List.of(repoPO));
+        try (MockedStatic<StackUtils> mockedStackUtils = 
mockStatic(StackUtils.class, CALLS_REAL_METHODS)) {
+            
mockedStackUtils.when(StackUtils::getStackKeyMap).thenReturn(mockStackKeyMap);
+            clusterService.save(clusterDTO);
+            
when(clusterDao.findByClusterName(any())).thenReturn(Optional.ofNullable(clusterPO));
+            clusterService.save(clusterDTO);
+        }
+    }
+}
diff --git 
a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/CommandServiceTest.java
 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/CommandServiceTest.java
new file mode 100644
index 0000000..a7082c2
--- /dev/null
+++ 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/CommandServiceTest.java
@@ -0,0 +1,106 @@
+/*
+ * 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
+ *
+ *    https://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.bigtop.manager.server.service;
+
+import org.apache.bigtop.manager.dao.po.JobPO;
+import org.apache.bigtop.manager.dao.po.StagePO;
+import org.apache.bigtop.manager.dao.po.TaskPO;
+import org.apache.bigtop.manager.dao.repository.JobDao;
+import org.apache.bigtop.manager.dao.repository.StageDao;
+import org.apache.bigtop.manager.dao.repository.TaskDao;
+import org.apache.bigtop.manager.server.command.factory.JobFactories;
+import org.apache.bigtop.manager.server.command.factory.JobFactory;
+import org.apache.bigtop.manager.server.command.job.Job;
+import org.apache.bigtop.manager.server.command.job.JobContext;
+import org.apache.bigtop.manager.server.command.scheduler.JobScheduler;
+import org.apache.bigtop.manager.server.command.stage.Stage;
+import org.apache.bigtop.manager.server.command.task.Task;
+import 
org.apache.bigtop.manager.server.command.validator.ValidatorExecutionChain;
+import org.apache.bigtop.manager.server.holder.SpringContextHolder;
+import org.apache.bigtop.manager.server.model.dto.CommandDTO;
+import org.apache.bigtop.manager.server.service.impl.CommandServiceImpl;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.HashMap;
+import java.util.List;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+public class CommandServiceTest {
+    @Mock
+    JobFactory jobFactory;
+
+    @Mock
+    JobDao jobDao;
+
+    @Mock
+    private StageDao stageDao;
+
+    @Mock
+    private TaskDao taskDao;
+
+    @Mock
+    private JobScheduler jobScheduler;
+
+    @InjectMocks
+    private CommandService commandService = new CommandServiceImpl();
+
+    @Test
+    public void testCommand() {
+        Job mockJob = mock(Job.class);
+        JobContext mockJobContext = mock(JobContext.class);
+
+        CommandDTO mockCommandDTO = mock(CommandDTO.class);
+
+        when(mockJob.getJobContext()).thenReturn(mockJobContext);
+        when(mockJobContext.getCommandDTO()).thenReturn(mockCommandDTO);
+        when(mockJob.getJobPO()).thenReturn(new JobPO());
+
+        Stage mockStage = mock(Stage.class);
+        when(mockJob.getStages()).thenReturn(List.of(mockStage));
+        when(mockStage.getStagePO()).thenReturn(new StagePO());
+
+        Task mockTask = mock(Task.class);
+        when(mockStage.getTasks()).thenReturn(List.of(mockTask));
+        when(mockTask.getTaskPO()).thenReturn(new TaskPO());
+
+        try (var mockSpringContextHolder = 
mockStatic(SpringContextHolder.class)) {
+            mockSpringContextHolder
+                    .when(SpringContextHolder::getCommandValidators)
+                    .thenReturn(new HashMap<>());
+            mockStatic(ValidatorExecutionChain.class);
+            try (var mockJobFactories = mockStatic(JobFactories.class)) {
+                mockJobFactories.when(() -> 
JobFactories.getJobFactory(any())).thenReturn(jobFactory);
+                when(jobFactory.createJob(any())).thenReturn(mockJob);
+
+                assert commandService.command(mockCommandDTO) != null;
+            }
+        }
+    }
+}
diff --git 
a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/ComponentServiceTest.java
 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/ComponentServiceTest.java
new file mode 100644
index 0000000..854710e
--- /dev/null
+++ 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/ComponentServiceTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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
+ *
+ *    https://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.bigtop.manager.server.service;
+
+import org.apache.bigtop.manager.dao.po.ComponentPO;
+import org.apache.bigtop.manager.dao.repository.ComponentDao;
+import org.apache.bigtop.manager.server.service.impl.ComponentServiceImpl;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.List;
+import java.util.Optional;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+public class ComponentServiceTest {
+    @Mock
+    private ComponentDao componentDao;
+
+    @InjectMocks
+    private ComponentService componentService = new ComponentServiceImpl();
+
+    @Test
+    public void testListAndGetComponent() {
+        when(componentDao.findAllByClusterId(any())).thenReturn(List.of(new 
ComponentPO()));
+        assert componentService.list(1L).size() == 1;
+
+        when(componentDao.findOptionalById(any())).thenReturn(Optional.of(new 
ComponentPO()));
+        assert componentService.get(1L) != null;
+    }
+}
diff --git 
a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/ConfigServiceTest.java
 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/ConfigServiceTest.java
new file mode 100644
index 0000000..80bbe65
--- /dev/null
+++ 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/ConfigServiceTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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
+ *
+ *    https://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.bigtop.manager.server.service;
+
+import org.apache.bigtop.manager.dao.po.ClusterPO;
+import org.apache.bigtop.manager.dao.po.ServiceConfigPO;
+import org.apache.bigtop.manager.dao.po.ServicePO;
+import org.apache.bigtop.manager.dao.po.TypeConfigPO;
+import org.apache.bigtop.manager.dao.repository.ClusterDao;
+import org.apache.bigtop.manager.dao.repository.ServiceConfigDao;
+import org.apache.bigtop.manager.dao.repository.ServiceDao;
+import org.apache.bigtop.manager.dao.repository.TypeConfigDao;
+import org.apache.bigtop.manager.server.model.dto.PropertyDTO;
+import org.apache.bigtop.manager.server.model.dto.TypeConfigDTO;
+import org.apache.bigtop.manager.server.service.impl.ConfigServiceImpl;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.List;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+public class ConfigServiceTest {
+
+    @Mock
+    private ClusterDao clusterDao;
+
+    @Mock
+    private ServiceDao serviceDao;
+
+    @Mock
+    private ServiceConfigDao serviceConfigDao;
+
+    @Mock
+    private TypeConfigDao typeConfigDao;
+
+    @InjectMocks
+    private ConfigService configService = new ConfigServiceImpl();
+
+    @Test
+    public void testListAndLatest() {
+        configService.list(1L);
+        configService.latest(1L);
+    }
+
+    @Test
+    public void testUpsert() {
+        when(clusterDao.findById(1L)).thenReturn(new ClusterPO());
+        when(serviceDao.findById(1L)).thenReturn(new ServicePO());
+        TypeConfigDTO typeConfigDTO = new TypeConfigDTO();
+        typeConfigDTO.setTypeName("test");
+        PropertyDTO propertyDTO = new PropertyDTO();
+        propertyDTO.setName("test");
+        typeConfigDTO.setProperties(List.of(propertyDTO));
+        configService.upsert(1L, 1L, List.of(typeConfigDTO));
+
+        ServiceConfigPO serviceConfigPO = new ServiceConfigPO();
+        TypeConfigPO typeConfigPO = new TypeConfigPO();
+        typeConfigPO.setTypeName("test");
+        typeConfigPO.setPropertiesJson("[]");
+        serviceConfigPO.setConfigs(List.of(typeConfigPO));
+        serviceConfigPO.setVersion(1);
+        
when(serviceConfigDao.findByClusterIdAndServiceIdAndSelectedIsTrue(any(), 
any()))
+                .thenReturn(serviceConfigPO);
+        configService.upsert(1L, 1L, List.of(typeConfigDTO));
+    }
+}
diff --git 
a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/HostComponentServiceTest.java
 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/HostComponentServiceTest.java
new file mode 100644
index 0000000..65dc37d
--- /dev/null
+++ 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/HostComponentServiceTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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
+ *
+ *    https://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.bigtop.manager.server.service;
+
+import org.apache.bigtop.manager.dao.repository.HostComponentDao;
+import org.apache.bigtop.manager.server.service.impl.HostComponentServiceImpl;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.ArrayList;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+public class HostComponentServiceTest {
+    @Mock
+    private HostComponentDao hostComponentDao;
+
+    @InjectMocks
+    private HostComponentService hostComponentService = new 
HostComponentServiceImpl();
+
+    @Test
+    public void testListHostComponent() {
+        when(hostComponentDao.findAllByClusterId(any())).thenReturn(new 
ArrayList<>());
+        assert hostComponentService.list(1L) != null;
+
+        when(hostComponentDao.findAllByClusterIdAndHostId(any(), 
any())).thenReturn(null);
+        assert hostComponentService.listByHost(1L, 1L) == null;
+
+        when(hostComponentDao.findAllByClusterIdAndServiceId(any(), 
any())).thenReturn(null);
+        assert hostComponentService.listByService(1L, 1L) == null;
+    }
+}
diff --git 
a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/LoginServiceTest.java
 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/LoginServiceTest.java
new file mode 100644
index 0000000..d9620d9
--- /dev/null
+++ 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/LoginServiceTest.java
@@ -0,0 +1,79 @@
+/*
+ * 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
+ *
+ *    https://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.bigtop.manager.server.service;
+
+import org.apache.bigtop.manager.dao.po.UserPO;
+import org.apache.bigtop.manager.dao.repository.UserDao;
+import org.apache.bigtop.manager.server.enums.ApiExceptionEnum;
+import org.apache.bigtop.manager.server.exception.ApiException;
+import org.apache.bigtop.manager.server.model.dto.LoginDTO;
+import org.apache.bigtop.manager.server.service.impl.LoginServiceImpl;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+public class LoginServiceTest {
+
+    @Mock
+    private UserDao userDao;
+
+    @InjectMocks
+    private LoginService loginService = new LoginServiceImpl();
+
+    @Test
+    public void testLogin() {
+        when(userDao.findByUsername(any())).thenReturn(null);
+        assertEquals(
+                ApiExceptionEnum.INCORRECT_USERNAME_OR_PASSWORD,
+                assertThrows(ApiException.class, () -> loginService.login(new 
LoginDTO()))
+                        .getEx());
+
+        UserPO userPO = new UserPO();
+        userPO.setStatus(false);
+        userPO.setPassword("wrong_password");
+        LoginDTO loginDTO = new LoginDTO();
+        loginDTO.setPassword("password");
+        when(userDao.findByUsername(any())).thenReturn(userPO);
+        assertEquals(
+                ApiExceptionEnum.INCORRECT_USERNAME_OR_PASSWORD,
+                assertThrows(ApiException.class, () -> 
loginService.login(loginDTO))
+                        .getEx());
+
+        userPO.setPassword("password");
+        when(userDao.findByUsername(any())).thenReturn(userPO);
+        assertEquals(
+                ApiExceptionEnum.USER_IS_DISABLED,
+                assertThrows(ApiException.class, () -> 
loginService.login(loginDTO))
+                        .getEx());
+
+        userPO.setStatus(true);
+        when(userDao.findByUsername(any())).thenReturn(userPO);
+        assert loginService.login(loginDTO) != null;
+    }
+}
diff --git 
a/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/UserServiceTest.java
 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/UserServiceTest.java
new file mode 100644
index 0000000..c3b17e7
--- /dev/null
+++ 
b/bigtop-manager-server/src/test/java/org/apache/bigtop/manager/server/service/UserServiceTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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
+ *
+ *    https://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.bigtop.manager.server.service;
+
+import org.apache.bigtop.manager.dao.po.UserPO;
+import org.apache.bigtop.manager.dao.repository.UserDao;
+import org.apache.bigtop.manager.server.enums.ApiExceptionEnum;
+import org.apache.bigtop.manager.server.exception.ApiException;
+import org.apache.bigtop.manager.server.model.dto.UserDTO;
+import org.apache.bigtop.manager.server.service.impl.UserServiceImpl;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.InjectMocks;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+
+import java.util.Optional;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.when;
+
+@ExtendWith(MockitoExtension.class)
+public class UserServiceTest {
+
+    @Mock
+    private UserDao userDao;
+
+    @InjectMocks
+    private UserService userService = new UserServiceImpl();
+
+    @Test
+    public void testCurrentAndUpdate() {
+        when(userDao.findOptionalById(any())).thenReturn(Optional.of(new 
UserPO()));
+        assert userService.current() != null;
+
+        UserDTO userDTO = new UserDTO();
+        userDTO.setNickname("test");
+        assert userService.update(userDTO).getNickname().equals("test");
+    }
+
+    @Test
+    public void testNeedLogin() {
+        when(userDao.findOptionalById(any())).thenReturn(Optional.empty());
+        ApiException exception1 = assertThrows(ApiException.class, () -> {
+            userService.current();
+        });
+        assertEquals(ApiExceptionEnum.NEED_LOGIN, exception1.getEx());
+
+        ApiException exception2 = assertThrows(ApiException.class, () -> {
+            userService.update(new UserDTO());
+        });
+        assertEquals(ApiExceptionEnum.NEED_LOGIN, exception2.getEx());
+    }
+}

Reply via email to