forked from apache/bigtop-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BIGTOP-4240: Add ut cases for service classes (apache#84)
- Loading branch information
Showing
7 changed files
with
594 additions
and
0 deletions.
There are no files selected for viewing
136 changes: 136 additions & 0 deletions
136
...ger-server/src/test/java/org/apache/bigtop/manager/server/service/ClusterServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...ger-server/src/test/java/org/apache/bigtop/manager/server/service/CommandServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...r-server/src/test/java/org/apache/bigtop/manager/server/service/ComponentServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
...ager-server/src/test/java/org/apache/bigtop/manager/server/service/ConfigServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} |
Oops, something went wrong.