From cdfebd74c3678db48e250f2aa28dcf0291f8b514 Mon Sep 17 00:00:00 2001 From: gaoxinxing <15031259256@163.com> Date: Thu, 11 May 2023 17:42:54 +0800 Subject: [PATCH] add ut --- .../mlops/common/CustomMultipartFile.java | 140 ------------------ .../mlops/domain/job/JobService.java | 3 + .../mlops/domain/model/ModelService.java | 3 + .../mlops/domain/runtime/RuntimeDao.java | 13 +- .../mlops/domain/runtime/RuntimeService.java | 5 - .../mlops/domain/job/JobServiceTest.java | 3 + .../mlops/domain/model/ModelServiceTest.java | 6 +- 7 files changed, 17 insertions(+), 156 deletions(-) delete mode 100644 server/controller/src/main/java/ai/starwhale/mlops/common/CustomMultipartFile.java diff --git a/server/controller/src/main/java/ai/starwhale/mlops/common/CustomMultipartFile.java b/server/controller/src/main/java/ai/starwhale/mlops/common/CustomMultipartFile.java deleted file mode 100644 index 87a1feb59f..0000000000 --- a/server/controller/src/main/java/ai/starwhale/mlops/common/CustomMultipartFile.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2022 Starwhale, Inc. All Rights Reserved. - * - * Licensed 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 - * - * http://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 ai.starwhale.mlops.common; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import org.springframework.lang.NonNull; -import org.springframework.lang.Nullable; -import org.springframework.util.Assert; -import org.springframework.util.FileCopyUtils; -import org.springframework.web.multipart.MultipartFile; - - -public class CustomMultipartFile implements MultipartFile { - - private final String name; - - private final String originalFilename; - - @Nullable - private final String contentType; - - private final byte[] content; - - - /** - * Create a new CustomMultipartFile with the given content. - * - * @param name the name of the file - * @param content the content of the file - */ - public CustomMultipartFile(String name, @Nullable byte[] content) { - this(name, "", null, content); - } - - /** - * Create a new CustomMultipartFile with the given content. - * - * @param name the name of the file - * @param contentStream the content of the file as stream - * @throws IOException if reading from the stream failed - */ - public CustomMultipartFile(String name, InputStream contentStream) throws IOException { - this(name, "", null, FileCopyUtils.copyToByteArray(contentStream)); - } - - /** - * Create a new CustomMultipartFile with the given content. - * - * @param name the name of the file - * @param originalFilename the original filename (as on the client's machine) - * @param contentType the content type (if known) - * @param content the content of the file - */ - public CustomMultipartFile( - String name, @Nullable String originalFilename, @Nullable String contentType, @Nullable byte[] content) { - - Assert.hasLength(name, "Name must not be empty"); - this.name = name; - this.originalFilename = (originalFilename != null ? originalFilename : ""); - this.contentType = contentType; - this.content = (content != null ? content : new byte[0]); - } - - /** - * Create a new CustomMultipartFile with the given content. - * - * @param name the name of the file - * @param originalFilename the original filename (as on the client's machine) - * @param contentType the content type (if known) - * @param contentStream the content of the file as stream - * @throws IOException if reading from the stream failed - */ - public CustomMultipartFile( - String name, @Nullable String originalFilename, @Nullable String contentType, InputStream contentStream) - throws IOException { - - this(name, originalFilename, contentType, FileCopyUtils.copyToByteArray(contentStream)); - } - - - @Override - public String getName() { - return this.name; - } - - @Override - @NonNull - public String getOriginalFilename() { - return this.originalFilename; - } - - @Override - @Nullable - public String getContentType() { - return this.contentType; - } - - @Override - public boolean isEmpty() { - return (this.content.length == 0); - } - - @Override - public long getSize() { - return this.content.length; - } - - @Override - public byte[] getBytes() throws IOException { - return this.content; - } - - @Override - public InputStream getInputStream() throws IOException { - return new ByteArrayInputStream(this.content); - } - - @Override - public void transferTo(File dest) throws IOException, IllegalStateException { - FileCopyUtils.copy(this.content, dest); - } - -} diff --git a/server/controller/src/main/java/ai/starwhale/mlops/domain/job/JobService.java b/server/controller/src/main/java/ai/starwhale/mlops/domain/job/JobService.java index de25159881..6b85930b52 100644 --- a/server/controller/src/main/java/ai/starwhale/mlops/domain/job/JobService.java +++ b/server/controller/src/main/java/ai/starwhale/mlops/domain/job/JobService.java @@ -186,6 +186,9 @@ public Long createJob(String projectUrl, log.debug("try to find built-in runtime for model:{}", modelVersion.getId()); runtimeVersionUrl = modelVersion.getBuiltInRuntime(); } + if (!StringUtils.hasText(runtimeVersionUrl)) { + throw new SwValidationException(ValidSubject.RUNTIME, "no runtime or built-in runtime"); + } runtimeVersion = runtimeService.findRuntimeVersion(runtimeVersionUrl); var runtime = runtimeService.findRuntime(runtimeVersion.getRuntimeId()); diff --git a/server/controller/src/main/java/ai/starwhale/mlops/domain/model/ModelService.java b/server/controller/src/main/java/ai/starwhale/mlops/domain/model/ModelService.java index 676141ead7..2f6dd6a9ec 100644 --- a/server/controller/src/main/java/ai/starwhale/mlops/domain/model/ModelService.java +++ b/server/controller/src/main/java/ai/starwhale/mlops/domain/model/ModelService.java @@ -324,6 +324,9 @@ private List parseManifestFiles(String manifest) throws JsonProcessing } public Boolean modifyModelVersion(String projectUrl, String modelUrl, String versionUrl, ModelVersion version) { + if (!StringUtils.hasText(version.getTag()) && !StringUtils.hasText(version.getBuiltInRuntime())) { + throw new SwValidationException(ValidSubject.MODEL, "no attributes set for model version"); + } Long versionId = bundleManager.getBundleVersionId(BundleVersionUrl .create(projectUrl, modelUrl, versionUrl)); ModelVersionEntity entity = ModelVersionEntity.builder() diff --git a/server/controller/src/main/java/ai/starwhale/mlops/domain/runtime/RuntimeDao.java b/server/controller/src/main/java/ai/starwhale/mlops/domain/runtime/RuntimeDao.java index dd2f76f79f..912a22dd94 100644 --- a/server/controller/src/main/java/ai/starwhale/mlops/domain/runtime/RuntimeDao.java +++ b/server/controller/src/main/java/ai/starwhale/mlops/domain/runtime/RuntimeDao.java @@ -66,15 +66,6 @@ public RuntimeEntity getRuntime(Long id) { } public RuntimeVersionEntity getRuntimeVersion(String versionUrl) { - RuntimeVersionEntity entity = getRuntimeVersionAllowNull(versionUrl); - if (entity == null) { - throw new SwNotFoundException(ResourceType.BUNDLE_VERSION, - String.format("Unable to find Runtime Version %s", versionUrl)); - } - return entity; - } - - public RuntimeVersionEntity getRuntimeVersionAllowNull(String versionUrl) { RuntimeVersionEntity entity; if (idConvertor.isId(versionUrl)) { var id = idConvertor.revert(versionUrl); @@ -82,6 +73,10 @@ public RuntimeVersionEntity getRuntimeVersionAllowNull(String versionUrl) { } else { entity = runtimeVersionMapper.findByNameAndRuntimeId(versionUrl, null); } + if (entity == null) { + throw new SwNotFoundException(ResourceType.BUNDLE_VERSION, + String.format("Unable to find Runtime Version %s", versionUrl)); + } return entity; } diff --git a/server/controller/src/main/java/ai/starwhale/mlops/domain/runtime/RuntimeService.java b/server/controller/src/main/java/ai/starwhale/mlops/domain/runtime/RuntimeService.java index 46c7cba100..83cd387e2c 100644 --- a/server/controller/src/main/java/ai/starwhale/mlops/domain/runtime/RuntimeService.java +++ b/server/controller/src/main/java/ai/starwhale/mlops/domain/runtime/RuntimeService.java @@ -238,11 +238,6 @@ public Runtime findRuntime(Long runtimeId) { return Runtime.fromEntity(entity); } - public RuntimeVersion findRuntimeVersionAllowNull(String versioUrl) { - RuntimeVersionEntity entity = runtimeDao.getRuntimeVersionAllowNull(versioUrl); - return RuntimeVersion.fromEntity(entity); - } - public RuntimeVersion findRuntimeVersion(String versioUrl) { RuntimeVersionEntity entity = runtimeDao.getRuntimeVersion(versioUrl); return RuntimeVersion.fromEntity(entity); diff --git a/server/controller/src/test/java/ai/starwhale/mlops/domain/job/JobServiceTest.java b/server/controller/src/test/java/ai/starwhale/mlops/domain/job/JobServiceTest.java index 2322c3b71e..5fddfb0e74 100644 --- a/server/controller/src/test/java/ai/starwhale/mlops/domain/job/JobServiceTest.java +++ b/server/controller/src/test/java/ai/starwhale/mlops/domain/job/JobServiceTest.java @@ -279,6 +279,9 @@ public void testCreateJob() { assertThrows(StarwhaleApiException.class, () -> service.createJob("1", "3", "1", "2", "", "1", "h", "s", JobType.EVALUATION)); + assertThrows(SwValidationException.class, () -> service.createJob("1", "3", "1", "", + "", "1", "h", "s", JobType.EVALUATION)); + var res = service.createJob("1", "3", "1", "2", "", "1", "mnist.evaluator:MNISTInference.cmp", "", JobType.EVALUATION); assertThat(res, is(1L)); diff --git a/server/controller/src/test/java/ai/starwhale/mlops/domain/model/ModelServiceTest.java b/server/controller/src/test/java/ai/starwhale/mlops/domain/model/ModelServiceTest.java index cf6aaf3576..afa9707f3c 100644 --- a/server/controller/src/test/java/ai/starwhale/mlops/domain/model/ModelServiceTest.java +++ b/server/controller/src/test/java/ai/starwhale/mlops/domain/model/ModelServiceTest.java @@ -389,10 +389,12 @@ public void testModifyModelVersion() { given(modelVersionMapper.update(argThat(entity -> entity.getId() == 1L))) .willReturn(1); - var res = service.modifyModelVersion("1", "1", "v1", new ModelVersion()); + assertThrows(SwValidationException.class, () -> service.modifyModelVersion("1", "1", "v1", new ModelVersion())); + + var res = service.modifyModelVersion("1", "1", "v1", ModelVersion.builder().tag("v1").build()); assertThat(res, is(true)); - res = service.modifyModelVersion("1", "1", "v2", new ModelVersion()); + res = service.modifyModelVersion("1", "1", "v2", ModelVersion.builder().tag("v1").build()); assertThat(res, is(false)); }