Skip to content

Commit

Permalink
test(controller): unit test for project classes and some tiny change (#…
Browse files Browse the repository at this point in the history
…1137)

* normalization of autowired objects and mappers

* unit test for project classes
  • Loading branch information
dreamlandliu authored Sep 7, 2022
1 parent 5041d49 commit e0305ae
Show file tree
Hide file tree
Showing 17 changed files with 1,024 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import ai.starwhale.mlops.exception.api.StarWhaleApiException;
import com.github.pagehelper.PageInfo;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -47,14 +46,19 @@
@RequestMapping("${sw.controller.apiPrefix}")
public class ProjectController implements ProjectApi{

@Resource
private ProjectService projectService;
private final ProjectService projectService;

@Resource
private UserService userService;
private final UserService userService;

private final IDConvertor idConvertor;

public ProjectController(ProjectService projectService, UserService userService,
IDConvertor idConvertor) {
this.projectService = projectService;
this.userService = userService;
this.idConvertor = idConvertor;
}

@Resource
private IDConvertor idConvertor;

@Override
public ResponseEntity<ResponseMessage<PageInfo<ProjectVO>>> listProject(String projectName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ public class ProjectRoleVO {
private ProjectVO project;

private RoleVO role;

public static ProjectRoleVO empty() {
return new ProjectRoleVO("", UserVO.empty(), ProjectVO.empty(), RoleVO.empty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ public class RoleVO {
private String code;

private String description;

public static RoleVO empty() {
return new RoleVO("", "", "", "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,26 @@
import ai.starwhale.mlops.domain.user.UserConvertor;
import ai.starwhale.mlops.exception.ConvertException;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ProjectConvertor implements Convertor<ProjectEntity, ProjectVO> {

@Resource
private IDConvertor idConvertor;
private final IDConvertor idConvertor;

@Resource
private UserConvertor userConvertor;
private final UserConvertor userConvertor;

private final LocalDateTimeConvertor localDateTimeConvertor;

public ProjectConvertor(IDConvertor idConvertor, UserConvertor userConvertor,
LocalDateTimeConvertor localDateTimeConvertor) {
this.idConvertor = idConvertor;
this.userConvertor = userConvertor;
this.localDateTimeConvertor = localDateTimeConvertor;
}

@Resource
private LocalDateTimeConvertor localDateTimeConvertor;

@Override
public ProjectVO convert(ProjectEntity entity) throws ConvertException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -38,20 +37,20 @@
@Service
public class ProjectManager implements ProjectAccessor{



@Resource
private ProjectMapper projectMapper;

@Resource
private IDConvertor idConvertor;
private final ProjectMapper projectMapper;
private final IDConvertor idConvertor;

private static final Map<String, String> SORT_MAP = Map.of(
"id", "project_id",
"name", "project_name",
"time", "project_created_time",
"createdTime", "project_created_time");

public ProjectManager(ProjectMapper projectMapper, IDConvertor idConvertor) {
this.projectMapper = projectMapper;
this.idConvertor = idConvertor;
}

public List<ProjectEntity> listProjects(String projectName, Long userId, OrderParams orderParams) {
return projectMapper.listProjects(projectName, orderParams.getOrderSQL(SORT_MAP), 0, userId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public ProjectRoleConvertor(IDConvertor idConvertor,

@Override
public ProjectRoleVO convert(ProjectRoleEntity entity) throws ConvertException {
if(entity == null) {
return ProjectRoleVO.empty();
}
return ProjectRoleVO.builder()
.id(idConvertor.convert(entity.getId()))
.project(projectConvertor.convert(entity.getProject()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,12 @@
import ai.starwhale.mlops.exception.SWValidationException.ValidSubject;
import ai.starwhale.mlops.exception.api.StarWhaleApiException;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.Page.Function;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
Expand All @@ -56,29 +53,35 @@
@Service
public class ProjectService {

@Resource
private ProjectMapper projectMapper;
private final ProjectMapper projectMapper;

@Resource
private ProjectManager projectManager;
private final ProjectManager projectManager;

@Resource
private ProjectConvertor projectConvertor;
private final ProjectConvertor projectConvertor;

@Resource
private ProjectRoleMapper projectRoleMapper;
private final ProjectRoleMapper projectRoleMapper;

@Resource
private ProjectRoleConvertor projectRoleConvertor;
private final ProjectRoleConvertor projectRoleConvertor;

@Resource
private IDConvertor idConvertor;
private final IDConvertor idConvertor;

@Resource
private UserService userService;
private final UserService userService;

private static final String DELETE_SUFFIX = ".deleted";

public ProjectService(ProjectMapper projectMapper, ProjectManager projectManager,
ProjectConvertor projectConvertor, ProjectRoleMapper projectRoleMapper,
ProjectRoleConvertor projectRoleConvertor, IDConvertor idConvertor,
UserService userService) {
this.projectMapper = projectMapper;
this.projectManager = projectManager;
this.projectConvertor = projectConvertor;
this.projectRoleMapper = projectRoleMapper;
this.projectRoleConvertor = projectRoleConvertor;
this.idConvertor = idConvertor;
this.userService = userService;
}

/**
* Find a project by parameters.
* @param projectUrl Project URL must be set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ public static Privacy fromName(String name) {
return PRIVATE;
}
}
public static Privacy fromValue(int value) {
public static Privacy fromValue(Integer value) {
if(value == null) {
return PRIVATE;
}
for (Privacy privacy : Privacy.values()) {
if(privacy.getValue() == value) {
return privacy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import ai.starwhale.mlops.domain.project.po.ProjectObjectCountEntity;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface ProjectMapper {

int createProject(@Param("project")ProjectEntity project);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ai.starwhale.mlops.domain.project.mapper;

import ai.starwhale.mlops.domain.project.po.ProjectRoleEntity;
import ai.starwhale.mlops.domain.user.po.RoleEntity;
import java.util.List;
import javax.validation.constraints.NotNull;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

@Mapper
public interface ProjectRoleMapper {

List<ProjectRoleEntity> listSystemRoles();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ public RoleConvertor(IDConvertor idConvertor) {

@Override
public RoleVO convert(RoleEntity roleEntity) throws ConvertException {
if(roleEntity == null) {
return RoleVO.empty();
}
return RoleVO.builder()
.id(idConvertor.convert(roleEntity.getId()))
.name(roleEntity.getRoleName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@
import ai.starwhale.mlops.domain.user.po.UserEntity;
import ai.starwhale.mlops.exception.ConvertException;
import java.util.Objects;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UserConvertor implements Convertor<UserEntity, UserVO> {

@Resource private IDConvertor idConvertor;
private final IDConvertor idConvertor;

private final LocalDateTimeConvertor localDateTimeConvertor;

public UserConvertor(IDConvertor idConvertor, LocalDateTimeConvertor localDateTimeConvertor) {
this.idConvertor = idConvertor;
this.localDateTimeConvertor = localDateTimeConvertor;
}

@Resource
private LocalDateTimeConvertor localDateTimeConvertor;

@Override
public UserVO convert(UserEntity entity) throws ConvertException {
Expand All @@ -45,7 +49,7 @@ public UserVO convert(UserEntity entity) throws ConvertException {
.id(idConvertor.convert(entity.getId()))
.name(entity.getUserName())
.createdTime(localDateTimeConvertor.convert(entity.getCreatedTime()))
.isEnabled(entity.getUserEnabled() == 1)
.isEnabled(entity.getUserEnabled() != null && entity.getUserEnabled()== 1)
.build();
}

Expand Down
Loading

0 comments on commit e0305ae

Please sign in to comment.