Skip to content

Commit

Permalink
add ut for java
Browse files Browse the repository at this point in the history
  • Loading branch information
anda-ren committed Oct 28, 2022
1 parent e41e0f3 commit 38faf6a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.same;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.when;

import ai.starwhale.mlops.api.protocol.swds.DatasetVersionVo;
import ai.starwhale.mlops.api.protocol.swds.DatasetVo;
Expand All @@ -58,6 +59,7 @@
import javax.servlet.ServletOutputStream;
import javax.servlet.WriteListener;
import javax.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.stubbing.Answer;
Expand Down Expand Up @@ -351,4 +353,17 @@ public void testHeadDataset() {
resp = controller.headDataset("p2", "d1", "v1");
assertThat(resp.getStatusCode(), is(HttpStatus.OK));
}

@Test
public void testSignLink() {
String pj = "pj";
String ds = "ds";
String v = "v";
String uri = "uri";
String auth = "auth";
when(swdsService.query(pj, ds, v)).thenReturn(SwDatasetVersionEntity.builder().id(1L).build());
String signUrl = "sign-url";
when(swdsService.signLink(1L, uri, auth, 100L)).thenReturn(signUrl);
Assertions.assertEquals(signUrl, controller.signLink(pj, ds, v, uri, auth, 100L).getBody().getData());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* 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.api;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import ai.starwhale.mlops.storage.LengthAbleInputStream;
import ai.starwhale.mlops.storage.StorageAccessService;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.DelegatingServletOutputStream;

public class ObjectStoreControllerTest {

private ObjectStoreController objectStoreController;

private StorageAccessService storageAccessService;

@BeforeEach
public void setUp() {
storageAccessService = mock(StorageAccessService.class);
objectStoreController = new ObjectStoreController(storageAccessService);
}

@Test
public void testWithRange() throws IOException {
var rsp = mock(HttpServletResponse.class);
DelegatingServletOutputStream outputStream = new DelegatingServletOutputStream(new ByteArrayOutputStream());
when(rsp.getOutputStream()).thenReturn(outputStream);
var p = "p";
var r = "bytes=0-100";
String content = "content";
LengthAbleInputStream inputStream = new LengthAbleInputStream(new ByteArrayInputStream(content.getBytes()),
content.length());
when(storageAccessService.get(p, 0L, 101L)).thenReturn(inputStream);
objectStoreController.getObjectContent(p, r, System.currentTimeMillis() + 1000, rsp);
Assertions.assertEquals(content, outputStream.getTargetStream().toString());

}

@Test
public void testWithOutRange() throws IOException {
var rsp = mock(HttpServletResponse.class);
DelegatingServletOutputStream outputStream = new DelegatingServletOutputStream(new ByteArrayOutputStream());
when(rsp.getOutputStream()).thenReturn(outputStream);
var p = "p";
String r = null;
String content = "content";
LengthAbleInputStream inputStream = new LengthAbleInputStream(new ByteArrayInputStream(content.getBytes()),
content.length());
when(storageAccessService.get(p)).thenReturn(inputStream);
objectStoreController.getObjectContent(p, r, System.currentTimeMillis() + 1000, rsp);
Assertions.assertEquals(content, outputStream.getTargetStream().toString());

}

@Test
public void testWithOutRangeExcept() throws IOException {
var rsp = mock(HttpServletResponse.class);
DelegatingServletOutputStream outputStream = new DelegatingServletOutputStream(new ByteArrayOutputStream());
when(rsp.getOutputStream()).thenReturn(outputStream);
var p = "p";
var r = "asdfa";
String content = "content";
LengthAbleInputStream inputStream = new LengthAbleInputStream(new ByteArrayInputStream(content.getBytes()),
content.length());
when(storageAccessService.get(p)).thenReturn(inputStream);
objectStoreController.getObjectContent(p, r, System.currentTimeMillis() + 1000, rsp);
Assertions.assertEquals(content, outputStream.getTargetStream().toString());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -388,5 +388,13 @@ public void testDataOf() {
assertThat(res, notNullValue());
}

@Test
public void testLinkOf() {
given(dsFileGetter.linkOf(same(1L), anyString(), anyString(), anyLong()))
.willReturn("link");

var res = dsFileGetter.linkOf(1L, "", "", 1L);
assertThat(dsFileGetter.linkOf(1L, "", "", 1L), is("link"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
public class DsFileGetterTest {

@Test
public void testFileGetter() throws IOException {
public void testDataOf() throws IOException {
StorageAccessParser storageAccessParser = mock(StorageAccessParser.class);
StorageAccessService storageAccessService = mock(
StorageAccessService.class);
Expand All @@ -56,4 +56,22 @@ public void testFileGetter() throws IOException {

}

@Test
public void testLinkOf() throws IOException {
StorageAccessParser storageAccessParser = mock(StorageAccessParser.class);
StorageAccessService storageAccessService = mock(
StorageAccessService.class);
when(storageAccessService.head("bdcsd")).thenReturn(new StorageObjectInfo(false, 1L, null));
when(storageAccessService.head("bdc/bdcsd")).thenReturn(new StorageObjectInfo(true, 1L, null));
when(storageAccessService.signedUrl(eq("bdc/bdcsd"), anyLong())).thenReturn("abc");
when(storageAccessParser.getStorageAccessServiceFromAuth(anyLong(), anyString(), anyString())).thenReturn(
storageAccessService);
SwDatasetVersionMapper versionMapper = mock(SwDatasetVersionMapper.class);
when(versionMapper.getVersionById(anyLong())).thenReturn(
SwDatasetVersionEntity.builder().storagePath("bdc").build());
DsFileGetter fileGetter = new DsFileGetter(storageAccessParser, versionMapper);
Assertions.assertEquals("abc", fileGetter.linkOf(1L, "bdcsd", "", 1L));
Assertions.assertEquals("abc", fileGetter.linkOf(1L, "bdc/bdcsd", "", 1L));
}

}

0 comments on commit 38faf6a

Please sign in to comment.