Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(controller): unit test for dataset and runtime controller #1299

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,20 @@
import ai.starwhale.mlops.domain.swds.bo.SwdsVersionQuery;
import ai.starwhale.mlops.domain.swds.po.SwDatasetVersionEntity;
import ai.starwhale.mlops.domain.swds.upload.SwdsUploader;
import ai.starwhale.mlops.exception.ApiOperationException;
import ai.starwhale.mlops.exception.SwProcessException;
import ai.starwhale.mlops.exception.SwProcessException.ErrorType;
import ai.starwhale.mlops.exception.SwValidationException;
import ai.starwhale.mlops.exception.SwValidationException.ValidSubject;
import ai.starwhale.mlops.exception.api.StarwhaleApiException;
import cn.hutool.core.lang.Assert;
import com.github.pagehelper.PageInfo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -67,14 +63,15 @@
@Slf4j
public class DatasetController implements DatasetApi {

@Resource
private SwDatasetService swDatasetService;
private final SwDatasetService swDatasetService;
private final IdConvertor idConvertor;
private final SwdsUploader swdsUploader;

@Resource
private IdConvertor idConvertor;

@Resource
private SwdsUploader swdsUploader;
public DatasetController(SwDatasetService swDatasetService, IdConvertor idConvertor, SwdsUploader swdsUploader) {
this.swDatasetService = swDatasetService;
this.idConvertor = idConvertor;
this.swdsUploader = swdsUploader;
}

@Override
public ResponseEntity<ResponseMessage<String>> revertDatasetVersion(String projectUrl,
Expand Down Expand Up @@ -215,7 +212,10 @@ public ResponseEntity<ResponseMessage<String>> modifyDatasetVersionInfo(
String projectUrl, String datasetUrl, String versionUrl, SwdsTagRequest swdsTagRequest) {
Boolean res = swDatasetService.modifySwdsVersion(projectUrl, datasetUrl, versionUrl,
SwdsVersion.builder().tag(swdsTagRequest.getTag()).build());
Assert.isTrue(Optional.of(res).orElseThrow(ApiOperationException::new));
if (!res) {
throw new StarwhaleApiException(new SwValidationException(ValidSubject.SWDS).tip("Modify dataset failed."),
HttpStatus.INTERNAL_SERVER_ERROR);
}
return ResponseEntity.ok(Code.success.asResponse("success"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import ai.starwhale.mlops.exception.SwValidationException.ValidSubject;
import ai.starwhale.mlops.exception.api.StarwhaleApiException;
import com.github.pagehelper.PageInfo;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
Expand All @@ -50,8 +49,11 @@
@RequestMapping("${sw.controller.apiPrefix}")
public class RuntimeController implements RuntimeApi {

@Resource
private RuntimeService runtimeService;
private final RuntimeService runtimeService;

public RuntimeController(RuntimeService runtimeService) {
this.runtimeService = runtimeService;
}

@Override
public ResponseEntity<ResponseMessage<PageInfo<RuntimeVo>>> listRuntime(String projectUrl,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ public class RuntimeVo {

@JsonProperty("version")
private RuntimeVersionVo version;

public static RuntimeVo empty() {
return new RuntimeVo("", "", -1L, UserVo.empty(), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@
import ai.starwhale.mlops.domain.runtime.po.RuntimeEntity;
import ai.starwhale.mlops.domain.user.UserConvertor;
import ai.starwhale.mlops.exception.ConvertException;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class RuntimeConvertor implements Convertor<RuntimeEntity, RuntimeVo> {

@Resource
private IdConvertor idConvertor;
@Resource
private UserConvertor userConvertor;
private final IdConvertor idConvertor;
private final UserConvertor userConvertor;

public RuntimeConvertor(IdConvertor idConvertor, UserConvertor userConvertor) {
this.idConvertor = idConvertor;
this.userConvertor = userConvertor;
}

@Override
public RuntimeVo convert(RuntimeEntity entity) throws ConvertException {
if (entity == null) {
return RuntimeVo.empty();
}
return RuntimeVo.builder()
.id(idConvertor.convert(entity.getId()))
.name(entity.getRuntimeName())
Expand Down
Loading