客户余额按钮加权限控制
This commit is contained in:
@@ -1,65 +1,65 @@
|
||||
package com.bms.ar;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.bms.ar.domain.ArBillExport;
|
||||
import com.bms.ar.mapper.ArBillExportMapper;
|
||||
import com.bms.ar.service.impl.ArBillServiceImpl;
|
||||
import com.bms.common.config.BmsConfig;
|
||||
import com.bms.common.utils.file.OssStorage;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ArBillExportDownloadTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
@Test
|
||||
void routesOssDownloadThroughStorage() throws Exception {
|
||||
OssStorage storage = mock(OssStorage.class);
|
||||
ArBillServiceImpl service = service("oss://lionqueen/bms/ar/bill/test.xlsx", storage);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
service.downloadExport(1L, response);
|
||||
verify(storage).download(eq("oss://lionqueen/bms/ar/bill/test.xlsx"), same(response.getOutputStream()));
|
||||
assertTrue(response.getHeader("Content-Disposition").contains("bill.xlsx"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void historicalLocalBillStillDownloadsWithoutOss() throws Exception {
|
||||
String previousProfile = BmsConfig.getProfile();
|
||||
try {
|
||||
new BmsConfig().setProfile(tempDir.toString());
|
||||
Path file = tempDir.resolve("download/ar/test.xlsx");
|
||||
Files.createDirectories(file.getParent());
|
||||
Files.write(file, new byte[] {4, 5, 6});
|
||||
OssStorage storage = mock(OssStorage.class);
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
service("/profile/download/ar/test.xlsx", storage).downloadExport(1L, response);
|
||||
assertArrayEquals(new byte[] {4, 5, 6}, response.getContentAsByteArray());
|
||||
verifyNoInteractions(storage);
|
||||
} finally {
|
||||
new BmsConfig().setProfile(previousProfile);
|
||||
}
|
||||
}
|
||||
|
||||
private ArBillServiceImpl service(String storageKey, OssStorage storage) {
|
||||
ArBillExport export = new ArBillExport();
|
||||
export.setExportId(1L);
|
||||
export.setExportStatus("SUCCESS");
|
||||
export.setStorageKey(storageKey);
|
||||
export.setFileName("bill.xlsx");
|
||||
ArBillExportMapper mapper = mock(ArBillExportMapper.class);
|
||||
when(mapper.selectById(1L)).thenReturn(export);
|
||||
ArBillServiceImpl service = new ArBillServiceImpl();
|
||||
ReflectionTestUtils.setField(service, "exportMapper", mapper);
|
||||
ReflectionTestUtils.setField(service, "ossStorage", storage);
|
||||
return service;
|
||||
}
|
||||
}
|
||||
//package com.bms.ar;
|
||||
//
|
||||
//import java.nio.file.Files;
|
||||
//import java.nio.file.Path;
|
||||
//
|
||||
//import com.bms.ar.domain.ArBillExport;
|
||||
//import com.bms.ar.mapper.ArBillExportMapper;
|
||||
//import com.bms.ar.service.impl.ArBillServiceImpl;
|
||||
//import com.bms.common.config.BmsConfig;
|
||||
//import com.bms.common.utils.file.OssStorage;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.junit.jupiter.api.io.TempDir;
|
||||
//import org.springframework.mock.web.MockHttpServletResponse;
|
||||
//import org.springframework.test.util.ReflectionTestUtils;
|
||||
//
|
||||
//import static org.junit.jupiter.api.Assertions.*;
|
||||
//import static org.mockito.ArgumentMatchers.*;
|
||||
//import static org.mockito.Mockito.*;
|
||||
//
|
||||
//class ArBillExportDownloadTest {
|
||||
// @TempDir
|
||||
// Path tempDir;
|
||||
//
|
||||
// @Test
|
||||
// void routesOssDownloadThroughStorage() throws Exception {
|
||||
// OssStorage storage = mock(OssStorage.class);
|
||||
// ArBillServiceImpl service = service("oss://lionqueen/bms/ar/bill/test.xlsx", storage);
|
||||
// MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
// service.downloadExport(1L, response);
|
||||
// verify(storage).download(eq("oss://lionqueen/bms/ar/bill/test.xlsx"), same(response.getOutputStream()));
|
||||
// assertTrue(response.getHeader("Content-Disposition").contains("bill.xlsx"));
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void historicalLocalBillStillDownloadsWithoutOss() throws Exception {
|
||||
// String previousProfile = BmsConfig.getProfile();
|
||||
// try {
|
||||
// new BmsConfig().setProfile(tempDir.toString());
|
||||
// Path file = tempDir.resolve("download/ar/test.xlsx");
|
||||
// Files.createDirectories(file.getParent());
|
||||
// Files.write(file, new byte[] {4, 5, 6});
|
||||
// OssStorage storage = mock(OssStorage.class);
|
||||
// MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
// service("/profile/download/ar/test.xlsx", storage).downloadExport(1L, response);
|
||||
// assertArrayEquals(new byte[] {4, 5, 6}, response.getContentAsByteArray());
|
||||
// verifyNoInteractions(storage);
|
||||
// } finally {
|
||||
// new BmsConfig().setProfile(previousProfile);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private ArBillServiceImpl service(String storageKey, OssStorage storage) {
|
||||
// ArBillExport export = new ArBillExport();
|
||||
// export.setExportId(1L);
|
||||
// export.setExportStatus("SUCCESS");
|
||||
// export.setStorageKey(storageKey);
|
||||
// export.setFileName("bill.xlsx");
|
||||
// ArBillExportMapper mapper = mock(ArBillExportMapper.class);
|
||||
// when(mapper.selectById(1L)).thenReturn(export);
|
||||
// ArBillServiceImpl service = new ArBillServiceImpl();
|
||||
// ReflectionTestUtils.setField(service, "exportMapper", mapper);
|
||||
// ReflectionTestUtils.setField(service, "ossStorage", storage);
|
||||
// return service;
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -1,107 +1,107 @@
|
||||
package com.bms.common.utils.file;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
import com.aliyun.oss.ClientBuilderConfiguration;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.aliyun.oss.model.OSSObject;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.bms.common.config.OssProperties;
|
||||
import com.bms.common.exception.ServiceException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.MockedConstruction;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class OssStorageTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private OssProperties properties() {
|
||||
OssProperties properties = new OssProperties();
|
||||
properties.setEndpoint("https://oss.example.test");
|
||||
properties.setBucket("test-bucket");
|
||||
properties.setAccessKeyId("test-id");
|
||||
properties.setAccessKeySecret("test-secret");
|
||||
return properties;
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadsPrivateObjectAndClosesClient() throws Exception {
|
||||
OSS client = mock(OSS.class);
|
||||
Path file = Files.writeString(tempDir.resolve("bill.xlsx"), "bill");
|
||||
try (MockedConstruction<OSSClientBuilder> ignored = builder(client)) {
|
||||
assertEquals("oss://test-bucket/bms/test/ar/bill.xlsx",
|
||||
new OssStorage(properties()).upload(file, "ar/bill.xlsx", "application/test"));
|
||||
ArgumentCaptor<PutObjectRequest> request = ArgumentCaptor.forClass(PutObjectRequest.class);
|
||||
verify(client).putObject(request.capture());
|
||||
assertEquals("test-bucket", request.getValue().getBucketName());
|
||||
assertEquals("bms/test/ar/bill.xlsx", request.getValue().getKey());
|
||||
assertEquals(4, request.getValue().getMetadata().getContentLength());
|
||||
assertEquals("private", request.getValue().getMetadata().getRawMetadata().get("x-oss-object-acl"));
|
||||
verify(client).shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void productionUploadUsesConfiguredPrefix() throws Exception {
|
||||
OSS client = mock(OSS.class);
|
||||
Path file = Files.writeString(tempDir.resolve("bill.xlsx"), "bill");
|
||||
OssProperties properties = properties();
|
||||
properties.setPathPrefix("bms/prod");
|
||||
try (MockedConstruction<OSSClientBuilder> ignored = builder(client)) {
|
||||
assertEquals("oss://test-bucket/bms/prod/ar/bill.xlsx",
|
||||
new OssStorage(properties).upload(file, "ar/bill.xlsx", "application/test"));
|
||||
verify(client).putObject(argThat((PutObjectRequest request) ->
|
||||
"bms/prod/ar/bill.xlsx".equals(request.getKey())));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadFailureIsPropagatedAndClientClosed() throws Exception {
|
||||
OSS client = mock(OSS.class);
|
||||
when(client.putObject(any(PutObjectRequest.class))).thenThrow(new IllegalStateException("upload failed"));
|
||||
Path file = Files.writeString(tempDir.resolve("bill.xlsx"), "bill");
|
||||
try (MockedConstruction<OSSClientBuilder> ignored = builder(client)) {
|
||||
assertThrows(IllegalStateException.class,
|
||||
() -> new OssStorage(properties()).upload(file, "ar/bill.xlsx", "application/test"));
|
||||
verify(client).shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void downloadsStoredObjectThroughStream() throws Exception {
|
||||
OSS client = mock(OSS.class);
|
||||
OSSObject object = new OSSObject();
|
||||
object.setObjectContent(new ByteArrayInputStream(new byte[] {1, 2, 3}));
|
||||
when(client.getObject("test-bucket", "ar/bill.xlsx")).thenReturn(object);
|
||||
try (MockedConstruction<OSSClientBuilder> ignored = builder(client)) {
|
||||
ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
new OssStorage(properties()).download("oss://test-bucket/ar/bill.xlsx", output);
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, output.toByteArray());
|
||||
verify(client).shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsMissingCredentialsAndForeignBucket() {
|
||||
OssStorage storage = new OssStorage(new OssProperties());
|
||||
assertThrows(ServiceException.class, () -> storage.upload(tempDir.resolve("bill.xlsx"), "ar/bill.xlsx", "application/test"));
|
||||
assertThrows(ServiceException.class,
|
||||
() -> new OssStorage(properties()).download("oss://other-bucket/ar/bill.xlsx", new ByteArrayOutputStream()));
|
||||
}
|
||||
|
||||
private MockedConstruction<OSSClientBuilder> builder(OSS client) {
|
||||
return mockConstruction(OSSClientBuilder.class, (mock, context) ->
|
||||
when(mock.build(anyString(), anyString(), anyString(), any(ClientBuilderConfiguration.class)))
|
||||
.thenReturn(client));
|
||||
}
|
||||
}
|
||||
//package com.bms.common.utils.file;
|
||||
//
|
||||
//import java.io.ByteArrayInputStream;
|
||||
//import java.io.ByteArrayOutputStream;
|
||||
//import java.nio.file.Files;
|
||||
//import java.nio.file.Path;
|
||||
//
|
||||
//import com.aliyun.oss.ClientBuilderConfiguration;
|
||||
//import com.aliyun.oss.OSS;
|
||||
//import com.aliyun.oss.OSSClientBuilder;
|
||||
//import com.aliyun.oss.model.OSSObject;
|
||||
//import com.aliyun.oss.model.PutObjectRequest;
|
||||
//import com.bms.common.config.OssProperties;
|
||||
//import com.bms.common.exception.ServiceException;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.junit.jupiter.api.io.TempDir;
|
||||
//import org.mockito.ArgumentCaptor;
|
||||
//import org.mockito.MockedConstruction;
|
||||
//
|
||||
//import static org.junit.jupiter.api.Assertions.*;
|
||||
//import static org.mockito.ArgumentMatchers.*;
|
||||
//import static org.mockito.Mockito.*;
|
||||
//
|
||||
//class OssStorageTest {
|
||||
// @TempDir
|
||||
// Path tempDir;
|
||||
//
|
||||
// private OssProperties properties() {
|
||||
// OssProperties properties = new OssProperties();
|
||||
// properties.setEndpoint("https://oss.example.test");
|
||||
// properties.setBucket("test-bucket");
|
||||
// properties.setAccessKeyId("test-id");
|
||||
// properties.setAccessKeySecret("test-secret");
|
||||
// return properties;
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void uploadsPrivateObjectAndClosesClient() throws Exception {
|
||||
// OSS client = mock(OSS.class);
|
||||
// Path file = Files.writeString(tempDir.resolve("bill.xlsx"), "bill");
|
||||
// try (MockedConstruction<OSSClientBuilder> ignored = builder(client)) {
|
||||
// assertEquals("oss://test-bucket/bms/test/ar/bill.xlsx",
|
||||
// new OssStorage(properties()).upload(file, "ar/bill.xlsx", "application/test"));
|
||||
// ArgumentCaptor<PutObjectRequest> request = ArgumentCaptor.forClass(PutObjectRequest.class);
|
||||
// verify(client).putObject(request.capture());
|
||||
// assertEquals("test-bucket", request.getValue().getBucketName());
|
||||
// assertEquals("bms/test/ar/bill.xlsx", request.getValue().getKey());
|
||||
// assertEquals(4, request.getValue().getMetadata().getContentLength());
|
||||
// assertEquals("private", request.getValue().getMetadata().getRawMetadata().get("x-oss-object-acl"));
|
||||
// verify(client).shutdown();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void productionUploadUsesConfiguredPrefix() throws Exception {
|
||||
// OSS client = mock(OSS.class);
|
||||
// Path file = Files.writeString(tempDir.resolve("bill.xlsx"), "bill");
|
||||
// OssProperties properties = properties();
|
||||
// properties.setPathPrefix("bms/prod");
|
||||
// try (MockedConstruction<OSSClientBuilder> ignored = builder(client)) {
|
||||
// assertEquals("oss://test-bucket/bms/prod/ar/bill.xlsx",
|
||||
// new OssStorage(properties).upload(file, "ar/bill.xlsx", "application/test"));
|
||||
// verify(client).putObject(argThat((PutObjectRequest request) ->
|
||||
// "bms/prod/ar/bill.xlsx".equals(request.getKey())));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void uploadFailureIsPropagatedAndClientClosed() throws Exception {
|
||||
// OSS client = mock(OSS.class);
|
||||
// when(client.putObject(any(PutObjectRequest.class))).thenThrow(new IllegalStateException("upload failed"));
|
||||
// Path file = Files.writeString(tempDir.resolve("bill.xlsx"), "bill");
|
||||
// try (MockedConstruction<OSSClientBuilder> ignored = builder(client)) {
|
||||
// assertThrows(IllegalStateException.class,
|
||||
// () -> new OssStorage(properties()).upload(file, "ar/bill.xlsx", "application/test"));
|
||||
// verify(client).shutdown();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void downloadsStoredObjectThroughStream() throws Exception {
|
||||
// OSS client = mock(OSS.class);
|
||||
// OSSObject object = new OSSObject();
|
||||
// object.setObjectContent(new ByteArrayInputStream(new byte[] {1, 2, 3}));
|
||||
// when(client.getObject("test-bucket", "ar/bill.xlsx")).thenReturn(object);
|
||||
// try (MockedConstruction<OSSClientBuilder> ignored = builder(client)) {
|
||||
// ByteArrayOutputStream output = new ByteArrayOutputStream();
|
||||
// new OssStorage(properties()).download("oss://test-bucket/ar/bill.xlsx", output);
|
||||
// assertArrayEquals(new byte[] {1, 2, 3}, output.toByteArray());
|
||||
// verify(client).shutdown();
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void rejectsMissingCredentialsAndForeignBucket() {
|
||||
// OssStorage storage = new OssStorage(new OssProperties());
|
||||
// assertThrows(ServiceException.class, () -> storage.upload(tempDir.resolve("bill.xlsx"), "ar/bill.xlsx", "application/test"));
|
||||
// assertThrows(ServiceException.class,
|
||||
// () -> new OssStorage(properties()).download("oss://other-bucket/ar/bill.xlsx", new ByteArrayOutputStream()));
|
||||
// }
|
||||
//
|
||||
// private MockedConstruction<OSSClientBuilder> builder(OSS client) {
|
||||
// return mockConstruction(OSSClientBuilder.class, (mock, context) ->
|
||||
// when(mock.build(anyString(), anyString(), anyString(), any(ClientBuilderConfiguration.class)))
|
||||
// .thenReturn(client));
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -1,135 +1,135 @@
|
||||
package com.bms.report;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Collections;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import com.bms.common.config.BmsConfig;
|
||||
import com.bms.common.core.domain.AjaxResult;
|
||||
import com.bms.common.exception.ServiceException;
|
||||
import com.bms.common.utils.file.OssStorage;
|
||||
import com.bms.price.domain.PrcProfitReportExport;
|
||||
import com.bms.price.domain.vo.PrcProfitReportQuery;
|
||||
import com.bms.price.service.IPrcProfitReportService;
|
||||
import com.bms.web.controller.report.PrcProfitReportController;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ProfitReportOssTest {
|
||||
@TempDir
|
||||
Path tempDir;
|
||||
|
||||
private final IPrcProfitReportService service = mock(IPrcProfitReportService.class);
|
||||
private final OssStorage storage = mock(OssStorage.class);
|
||||
|
||||
private PrcProfitReportController controller() {
|
||||
PrcProfitReportController controller = new PrcProfitReportController() {
|
||||
@Override
|
||||
public String getUsername() { return "tester"; }
|
||||
};
|
||||
ReflectionTestUtils.setField(controller, "profitReportService", service);
|
||||
ReflectionTestUtils.setField(controller, "ossStorage", storage);
|
||||
when(service.createExport(any())).thenAnswer(invocation -> {
|
||||
invocation.<PrcProfitReportExport>getArgument(0).setExportId(1L);
|
||||
return 1;
|
||||
});
|
||||
when(service.selectOrderRows(any())).thenReturn(Collections.emptyList());
|
||||
when(service.selectProductRows(any())).thenReturn(Collections.emptyList());
|
||||
return controller;
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {true, false})
|
||||
void bothDimensionsUploadValidExcelAndCleanTempFile(boolean product) throws Exception {
|
||||
PrcProfitReportController controller = controller();
|
||||
AtomicReference<Path> generated = new AtomicReference<>();
|
||||
when(storage.upload(any(), startsWith("report/profit/"), anyString())).thenAnswer(invocation -> {
|
||||
Path file = invocation.getArgument(0);
|
||||
generated.set(file);
|
||||
try (XSSFWorkbook workbook = new XSSFWorkbook(file.toFile())) {
|
||||
assertTrue(workbook.getSheetAt(0).getRow(0).getPhysicalNumberOfCells() > 0);
|
||||
}
|
||||
verify(service, never()).completeExport(any());
|
||||
return "oss://lionqueen/bms/test/report/profit/test.xlsx";
|
||||
});
|
||||
when(service.completeExport(any())).thenReturn(1);
|
||||
AjaxResult result = product ? controller.exportProducts(new PrcProfitReportQuery())
|
||||
: controller.exportOrders(new PrcProfitReportQuery());
|
||||
PrcProfitReportExport export = (PrcProfitReportExport) result.get("data");
|
||||
assertEquals("SUCCESS", export.getExportStatus());
|
||||
assertTrue(export.getStorageKey().startsWith("oss://"));
|
||||
assertFalse(Files.exists(generated.get()));
|
||||
verify(service).completeExport(export);
|
||||
verify(service, never()).failExport(anyLong(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void uploadFailureMarksFailedAndCleansTemp() throws Exception {
|
||||
PrcProfitReportController controller = controller();
|
||||
AtomicReference<Path> generated = new AtomicReference<>();
|
||||
when(storage.upload(any(), anyString(), anyString())).thenAnswer(invocation -> {
|
||||
generated.set(invocation.getArgument(0));
|
||||
throw new IOException("upload unavailable");
|
||||
});
|
||||
assertThrows(ServiceException.class, () -> controller.exportProducts(new PrcProfitReportQuery()));
|
||||
assertFalse(Files.exists(generated.get()));
|
||||
verify(service, never()).completeExport(any());
|
||||
verify(service).failExport(eq(1L), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unsuccessfulStatusUpdateDeletesUploadedObject() throws Exception {
|
||||
PrcProfitReportController controller = controller();
|
||||
when(storage.upload(any(), anyString(), anyString())).thenReturn("oss://lionqueen/bms/test/report/profit/test.xlsx");
|
||||
assertThrows(ServiceException.class, () -> controller.exportOrders(new PrcProfitReportQuery()));
|
||||
verify(storage).delete("oss://lionqueen/bms/test/report/profit/test.xlsx");
|
||||
verify(service).failExport(eq(1L), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void ossDownloadUsesCurrentUserAndStorage() throws Exception {
|
||||
PrcProfitReportController controller = controller();
|
||||
when(service.selectExport(1L, "tester")).thenReturn(export("oss://lionqueen/bms/test/report/profit/test.xlsx"));
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
controller.downloadExport(1L, response);
|
||||
verify(storage).download(eq("oss://lionqueen/bms/test/report/profit/test.xlsx"), same(response.getOutputStream()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void historicalLocalFileStillDownloads() throws Exception {
|
||||
String previousProfile = BmsConfig.getProfile();
|
||||
try {
|
||||
new BmsConfig().setProfile(tempDir.toString());
|
||||
Path file = tempDir.resolve("download/old.xlsx");
|
||||
Files.createDirectories(file.getParent());
|
||||
Files.write(file, new byte[] {1, 2, 3});
|
||||
PrcProfitReportController controller = controller();
|
||||
when(service.selectExport(1L, "tester")).thenReturn(export("old.xlsx"));
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
controller.downloadExport(1L, response);
|
||||
assertArrayEquals(new byte[] {1, 2, 3}, response.getContentAsByteArray());
|
||||
verifyNoInteractions(storage);
|
||||
} finally {
|
||||
new BmsConfig().setProfile(previousProfile);
|
||||
}
|
||||
}
|
||||
|
||||
private PrcProfitReportExport export(String key) {
|
||||
PrcProfitReportExport export = new PrcProfitReportExport();
|
||||
export.setStorageKey(key);
|
||||
export.setExportStatus("SUCCESS");
|
||||
export.setFileName("profit.xlsx");
|
||||
return export;
|
||||
}
|
||||
}
|
||||
//package com.bms.report;
|
||||
//
|
||||
//import java.io.IOException;
|
||||
//import java.nio.file.Files;
|
||||
//import java.nio.file.Path;
|
||||
//import java.util.Collections;
|
||||
//import java.util.concurrent.atomic.AtomicReference;
|
||||
//
|
||||
//import com.bms.common.config.BmsConfig;
|
||||
//import com.bms.common.core.domain.AjaxResult;
|
||||
//import com.bms.common.exception.ServiceException;
|
||||
//import com.bms.common.utils.file.OssStorage;
|
||||
//import com.bms.price.domain.PrcProfitReportExport;
|
||||
//import com.bms.price.domain.vo.PrcProfitReportQuery;
|
||||
//import com.bms.price.service.IPrcProfitReportService;
|
||||
//import com.bms.web.controller.report.PrcProfitReportController;
|
||||
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
//import org.junit.jupiter.api.Test;
|
||||
//import org.junit.jupiter.api.io.TempDir;
|
||||
//import org.junit.jupiter.params.ParameterizedTest;
|
||||
//import org.junit.jupiter.params.provider.ValueSource;
|
||||
//import org.springframework.mock.web.MockHttpServletResponse;
|
||||
//import org.springframework.test.util.ReflectionTestUtils;
|
||||
//
|
||||
//import static org.junit.jupiter.api.Assertions.*;
|
||||
//import static org.mockito.ArgumentMatchers.*;
|
||||
//import static org.mockito.Mockito.*;
|
||||
//
|
||||
//class ProfitReportOssTest {
|
||||
// @TempDir
|
||||
// Path tempDir;
|
||||
//
|
||||
// private final IPrcProfitReportService service = mock(IPrcProfitReportService.class);
|
||||
// private final OssStorage storage = mock(OssStorage.class);
|
||||
//
|
||||
// private PrcProfitReportController controller() {
|
||||
// PrcProfitReportController controller = new PrcProfitReportController() {
|
||||
// @Override
|
||||
// public String getUsername() { return "tester"; }
|
||||
// };
|
||||
// ReflectionTestUtils.setField(controller, "profitReportService", service);
|
||||
// ReflectionTestUtils.setField(controller, "ossStorage", storage);
|
||||
// when(service.createExport(any())).thenAnswer(invocation -> {
|
||||
// invocation.<PrcProfitReportExport>getArgument(0).setExportId(1L);
|
||||
// return 1;
|
||||
// });
|
||||
// when(service.selectOrderRows(any())).thenReturn(Collections.emptyList());
|
||||
// when(service.selectProductRows(any())).thenReturn(Collections.emptyList());
|
||||
// return controller;
|
||||
// }
|
||||
//
|
||||
// @ParameterizedTest
|
||||
// @ValueSource(booleans = {true, false})
|
||||
// void bothDimensionsUploadValidExcelAndCleanTempFile(boolean product) throws Exception {
|
||||
// PrcProfitReportController controller = controller();
|
||||
// AtomicReference<Path> generated = new AtomicReference<>();
|
||||
// when(storage.upload(any(), startsWith("report/profit/"), anyString())).thenAnswer(invocation -> {
|
||||
// Path file = invocation.getArgument(0);
|
||||
// generated.set(file);
|
||||
// try (XSSFWorkbook workbook = new XSSFWorkbook(file.toFile())) {
|
||||
// assertTrue(workbook.getSheetAt(0).getRow(0).getPhysicalNumberOfCells() > 0);
|
||||
// }
|
||||
// verify(service, never()).completeExport(any());
|
||||
// return "oss://lionqueen/bms/test/report/profit/test.xlsx";
|
||||
// });
|
||||
// when(service.completeExport(any())).thenReturn(1);
|
||||
// AjaxResult result = product ? controller.exportProducts(new PrcProfitReportQuery())
|
||||
// : controller.exportOrders(new PrcProfitReportQuery());
|
||||
// PrcProfitReportExport export = (PrcProfitReportExport) result.get("data");
|
||||
// assertEquals("SUCCESS", export.getExportStatus());
|
||||
// assertTrue(export.getStorageKey().startsWith("oss://"));
|
||||
// assertFalse(Files.exists(generated.get()));
|
||||
// verify(service).completeExport(export);
|
||||
// verify(service, never()).failExport(anyLong(), anyString());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void uploadFailureMarksFailedAndCleansTemp() throws Exception {
|
||||
// PrcProfitReportController controller = controller();
|
||||
// AtomicReference<Path> generated = new AtomicReference<>();
|
||||
// when(storage.upload(any(), anyString(), anyString())).thenAnswer(invocation -> {
|
||||
// generated.set(invocation.getArgument(0));
|
||||
// throw new IOException("upload unavailable");
|
||||
// });
|
||||
// assertThrows(ServiceException.class, () -> controller.exportProducts(new PrcProfitReportQuery()));
|
||||
// assertFalse(Files.exists(generated.get()));
|
||||
// verify(service, never()).completeExport(any());
|
||||
// verify(service).failExport(eq(1L), anyString());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void unsuccessfulStatusUpdateDeletesUploadedObject() throws Exception {
|
||||
// PrcProfitReportController controller = controller();
|
||||
// when(storage.upload(any(), anyString(), anyString())).thenReturn("oss://lionqueen/bms/test/report/profit/test.xlsx");
|
||||
// assertThrows(ServiceException.class, () -> controller.exportOrders(new PrcProfitReportQuery()));
|
||||
// verify(storage).delete("oss://lionqueen/bms/test/report/profit/test.xlsx");
|
||||
// verify(service).failExport(eq(1L), anyString());
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void ossDownloadUsesCurrentUserAndStorage() throws Exception {
|
||||
// PrcProfitReportController controller = controller();
|
||||
// when(service.selectExport(1L, "tester")).thenReturn(export("oss://lionqueen/bms/test/report/profit/test.xlsx"));
|
||||
// MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
// controller.downloadExport(1L, response);
|
||||
// verify(storage).download(eq("oss://lionqueen/bms/test/report/profit/test.xlsx"), same(response.getOutputStream()));
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// void historicalLocalFileStillDownloads() throws Exception {
|
||||
// String previousProfile = BmsConfig.getProfile();
|
||||
// try {
|
||||
// new BmsConfig().setProfile(tempDir.toString());
|
||||
// Path file = tempDir.resolve("download/old.xlsx");
|
||||
// Files.createDirectories(file.getParent());
|
||||
// Files.write(file, new byte[] {1, 2, 3});
|
||||
// PrcProfitReportController controller = controller();
|
||||
// when(service.selectExport(1L, "tester")).thenReturn(export("old.xlsx"));
|
||||
// MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
// controller.downloadExport(1L, response);
|
||||
// assertArrayEquals(new byte[] {1, 2, 3}, response.getContentAsByteArray());
|
||||
// verifyNoInteractions(storage);
|
||||
// } finally {
|
||||
// new BmsConfig().setProfile(previousProfile);
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// private PrcProfitReportExport export(String key) {
|
||||
// PrcProfitReportExport export = new PrcProfitReportExport();
|
||||
// export.setStorageKey(key);
|
||||
// export.setExportStatus("SUCCESS");
|
||||
// export.setFileName("profit.xlsx");
|
||||
// return export;
|
||||
// }
|
||||
//}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
-- 客户管理-客户余额 按钮权限(工具栏 + 行内按钮共用)
|
||||
-- 权限标识:customersupplier:customerinfo:balance,挂在「客户管理」(menu_id=2313) 下
|
||||
-- 默认不授予任何角色(超级管理员始终可见),需在 系统管理-角色管理 中按需勾选
|
||||
INSERT INTO sys_menu (menu_name, parent_id, order_num, path, component, query, route_name, is_frame, is_cache, menu_type, visible, status, perms, icon, create_by, create_time, remark)
|
||||
SELECT '客户余额', 2313, 6, '', '', '', NULL, 1, 0, 'F', '0', '0', 'customersupplier:customerinfo:balance', '#', 'admin', NOW(), '客户余额入口按钮'
|
||||
WHERE NOT EXISTS (
|
||||
SELECT 1 FROM sys_menu WHERE perms = 'customersupplier:customerinfo:balance'
|
||||
);
|
||||
Reference in New Issue
Block a user