添加文件传输支持;添加单元测数据库本地化支持;
This commit is contained in:
parent
27ebd6205a
commit
a64d65a70e
6
pom.xml
6
pom.xml
@ -144,6 +144,12 @@
|
|||||||
<version>2.5.4</version>
|
<version>2.5.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<scope>runtime</scope>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
@ -7,11 +7,11 @@ import org.springframework.stereotype.Component;
|
|||||||
// SHA1算法不可逆加密 主要用于JSON签名
|
// SHA1算法不可逆加密 主要用于JSON签名
|
||||||
@Component
|
@Component
|
||||||
public class SHA1Encoder {
|
public class SHA1Encoder {
|
||||||
String encode(CharSequence charSequence){
|
public String encode(CharSequence charSequence){
|
||||||
return DigestUtils.sha1Hex(charSequence.toString());
|
return DigestUtils.sha1Hex(charSequence.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean match(CharSequence charSequence, String s){
|
public boolean match (CharSequence charSequence, String s){
|
||||||
return s.equals(encode(charSequence));
|
return s.equals(encode(charSequence));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.codesdream.ase.component.datamanager;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
// 储存字符串标识的文件,并可以转换为json进行传输
|
||||||
|
@Data
|
||||||
|
public class StringFile {
|
||||||
|
private String strData = null;
|
||||||
|
private String sha1Checker = null;
|
||||||
|
private Integer size = null;
|
||||||
|
private String type = "none";
|
||||||
|
}
|
@ -0,0 +1,92 @@
|
|||||||
|
package com.codesdream.ase.component.datamanager;
|
||||||
|
|
||||||
|
import com.codesdream.ase.component.auth.SHA1Encoder;
|
||||||
|
import com.codesdream.ase.exception.StringFileConvertException;
|
||||||
|
import com.sun.xml.internal.messaging.saaj.util.ByteInputStream;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
|
// 将文件处理成可发送的字符串文件对象
|
||||||
|
@Component
|
||||||
|
public class StringFileGenerator {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private SHA1Encoder encoder;
|
||||||
|
|
||||||
|
// 用过读入流创建一个字符串文件
|
||||||
|
public Optional<StringFile> generateStringFile(InputStream stream){
|
||||||
|
StringFile file = new StringFile();
|
||||||
|
// 字符串内容计算
|
||||||
|
file.setStrData(generateFile2String(stream));
|
||||||
|
if(file.getStrData() == null) return Optional.empty();
|
||||||
|
// 相关校验值计算
|
||||||
|
file.setSha1Checker(generateSHA1Checker(file.getStrData()));
|
||||||
|
file.setSize(file.getStrData().length());
|
||||||
|
return Optional.of(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
private byte[] readSteamAll(InputStream stream) {
|
||||||
|
try {
|
||||||
|
byte[] bytes = new byte[stream.available()];
|
||||||
|
|
||||||
|
//检查文件书否完全读取
|
||||||
|
if (stream.read(bytes) != bytes.length) return null;
|
||||||
|
else return bytes;
|
||||||
|
} catch (IOException e){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String generateFile2String(InputStream stream){
|
||||||
|
ByteArrayOutputStream zipDataStream = new ByteArrayOutputStream();
|
||||||
|
try {
|
||||||
|
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(zipDataStream);
|
||||||
|
byte[] bytes = readSteamAll(stream);
|
||||||
|
if(bytes == null) return null;
|
||||||
|
gzipOutputStream.write(bytes);
|
||||||
|
gzipOutputStream.close();
|
||||||
|
return Base64.getEncoder().encodeToString(zipDataStream.toByteArray());
|
||||||
|
} catch (IOException e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成字符串文件的校验码
|
||||||
|
private String generateSHA1Checker(String str){
|
||||||
|
return encoder.encode(str);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查文件内容是否正确,包括大小与校验码
|
||||||
|
public boolean checkStringFile(StringFile file){
|
||||||
|
return file.getStrData().length() == file.getSize()
|
||||||
|
&& encoder.match(file.getStrData(), file.getSha1Checker());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从字符串文件中读取真实的文件数据
|
||||||
|
public InputStream readFileString(StringFile file){
|
||||||
|
try {
|
||||||
|
// 字符串转换为二进制数据
|
||||||
|
byte[] bytes = Base64.getDecoder().decode(file.getStrData());
|
||||||
|
GZIPInputStream stream = new GZIPInputStream(new ByteArrayInputStream(bytes), bytes.length);
|
||||||
|
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||||
|
|
||||||
|
// 数据解压缩
|
||||||
|
int readBits = 0;
|
||||||
|
byte[] rawBytes = new byte[1024];
|
||||||
|
while ((readBits = stream.read(rawBytes)) != -1) {
|
||||||
|
outputStream.write(rawBytes, 0, readBits);
|
||||||
|
}
|
||||||
|
|
||||||
|
stream.close();
|
||||||
|
return new ByteArrayInputStream(outputStream.toByteArray());
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new StringFileConvertException("Read FileString Failed");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -90,8 +90,6 @@ public class LoginController {
|
|||||||
// 返回失败对象
|
// 返回失败对象
|
||||||
return quickJSONRespond.getRespond400("CheckType Mismatch");
|
return quickJSONRespond.getRespond400("CheckType Mismatch");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ public class RegisterController {
|
|||||||
BaseStudentInfo studentInfo = baseInformationService.findStudentInfoByStudentId(student_id);
|
BaseStudentInfo studentInfo = baseInformationService.findStudentInfoByStudentId(student_id);
|
||||||
|
|
||||||
// 根据基本信息生成对应用户
|
// 根据基本信息生成对应用户
|
||||||
User user = userService.getUserByStudentInfo(studentInfo);
|
User user = userService.createUserByStudentInfo(studentInfo);
|
||||||
|
|
||||||
// 填充密保问题
|
// 填充密保问题
|
||||||
user.getUserAuth().setUserQuestion(user_question);
|
user.getUserAuth().setUserQuestion(user_question);
|
||||||
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.codesdream.ase.exception;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.EqualsAndHashCode;
|
||||||
|
|
||||||
|
@EqualsAndHashCode(callSuper = true)
|
||||||
|
@Data
|
||||||
|
public class StringFileConvertException extends RuntimeException {
|
||||||
|
public StringFileConvertException(String msg){
|
||||||
|
super(msg);
|
||||||
|
}
|
||||||
|
}
|
@ -12,7 +12,7 @@ import javax.persistence.*;
|
|||||||
@Table(name = "base_major")
|
@Table(name = "base_major")
|
||||||
public class BaseMajor {
|
public class BaseMajor {
|
||||||
@Id
|
@Id
|
||||||
private int id;
|
private String id;
|
||||||
|
|
||||||
private String name;
|
private String name;
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,6 @@ import org.springframework.stereotype.Repository;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
public interface BaseMajorRepository extends CrudRepository<BaseMajor, Integer> {
|
public interface BaseMajorRepository extends CrudRepository<BaseMajor, String> {
|
||||||
Optional<BaseMajor> findByName(String name);
|
Optional<BaseMajor> findByName(String name);
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ public interface IUserService {
|
|||||||
User getDefaultUser();
|
User getDefaultUser();
|
||||||
|
|
||||||
// 由学生基本信息生成对应用户
|
// 由学生基本信息生成对应用户
|
||||||
User getUserByStudentInfo(BaseStudentInfo studentInfo);
|
User createUserByStudentInfo(BaseStudentInfo studentInfo);
|
||||||
|
|
||||||
List<User> findAll();
|
List<User> findAll();
|
||||||
|
|
||||||
|
@ -97,14 +97,13 @@ public class UserService implements IUserService {
|
|||||||
if(userRepository.findByUsername(user.getUsername()).isPresent())
|
if(userRepository.findByUsername(user.getUsername()).isPresent())
|
||||||
throw new UsernameAlreadyExistException(user.getUsername());
|
throw new UsernameAlreadyExistException(user.getUsername());
|
||||||
|
|
||||||
// 用户信息一般性规范检查
|
// 用户关键信息一般性规范检查
|
||||||
if(user.getUserAuth().getUserAnswer() == null
|
if(user.getUserAuth().getUserAnswer() == null
|
||||||
|| user.getUserAuth().getUserQuestion() == null
|
|| user.getUserAuth().getUserQuestion() == null
|
||||||
|| user.getUserAuth().getStudentID() == null
|
|| user.getUserAuth().getStudentID() == null
|
||||||
|| user.getUserDetail().getRealName() == null
|
|| user.getUserDetail().getRealName() == null
|
||||||
|| user.getUserAuth().getMail() == null){
|
|| user.getUserAuth().getMail() == null){
|
||||||
|
throw new RuntimeException("Some Key Information IS NULL");
|
||||||
throw new RuntimeException("Key Information IS NULL");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -145,7 +144,7 @@ public class UserService implements IUserService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public User getUserByStudentInfo(BaseStudentInfo studentInfo) {
|
public User createUserByStudentInfo(BaseStudentInfo studentInfo) {
|
||||||
User user = getDefaultUser();
|
User user = getDefaultUser();
|
||||||
// 根据学生id生成用户名
|
// 根据学生id生成用户名
|
||||||
generateRandomUsernameByStudentID(user, studentInfo.getStudentId());
|
generateRandomUsernameByStudentID(user, studentInfo.getStudentId());
|
||||||
|
24
src/main/resources/application-test.properties
Normal file
24
src/main/resources/application-test.properties
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
server.port=8080
|
||||||
|
|
||||||
|
spring.thymeleaf.prefix=classpath:templates/
|
||||||
|
spring.thymeleaf.suffix=.html
|
||||||
|
spring.thymeleaf.mode=HTML
|
||||||
|
spring.thymeleaf.encoding=UTF-8
|
||||||
|
|
||||||
|
spring.jpa.generate-ddl=false
|
||||||
|
spring.jpa.show-sql=true
|
||||||
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
|
spring.jooq.sql-dialect=org.hibernate.dialect.MariaDB102Dialect
|
||||||
|
spring.jpa.open-in-view=true
|
||||||
|
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
|
||||||
|
|
||||||
|
spring.datasource.url=jdbc:h2:mem:test
|
||||||
|
spring.datasource.username=
|
||||||
|
spring.datasource.password=
|
||||||
|
spring.datasource.driver-class-name=org.h2.Driver
|
||||||
|
|
||||||
|
|
||||||
|
server.error.whitelabel.enabled=false
|
||||||
|
|
||||||
|
logging.level.root=info
|
||||||
|
logging.level.org.springframework.security=info
|
@ -21,5 +21,3 @@ server.error.whitelabel.enabled=false
|
|||||||
|
|
||||||
logging.level.root=info
|
logging.level.root=info
|
||||||
logging.level.org.springframework.security=info
|
logging.level.org.springframework.security=info
|
||||||
|
|
||||||
server.servlet.session.timeout=30m
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -3,22 +3,28 @@ package com.codesdream.ase.test;
|
|||||||
import com.codesdream.ase.component.ASESpringUtil;
|
import com.codesdream.ase.component.ASESpringUtil;
|
||||||
import com.codesdream.ase.component.datamanager.*;
|
import com.codesdream.ase.component.datamanager.*;
|
||||||
import com.codesdream.ase.repository.permission.UserRepository;
|
import com.codesdream.ase.repository.permission.UserRepository;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import java.io.*;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 测试DataModel相关查找器
|
* 测试DataModel相关查找器
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@ActiveProfiles("test")
|
||||||
public class DataManagerTest {
|
public class DataManagerTest {
|
||||||
@Resource
|
@Resource
|
||||||
ASESpringUtil springUtil;
|
ASESpringUtil springUtil;
|
||||||
@ -133,6 +139,7 @@ public class DataManagerTest {
|
|||||||
Assert.assertEquals(iterator.next(), "16");
|
Assert.assertEquals(iterator.next(), "16");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Excel表格导入测试
|
||||||
@Test
|
@Test
|
||||||
public void dataTableImportTest(){
|
public void dataTableImportTest(){
|
||||||
DataTable table = springUtil.getBean(DataTable.class);
|
DataTable table = springUtil.getBean(DataTable.class);
|
||||||
@ -144,6 +151,7 @@ public class DataManagerTest {
|
|||||||
Assert.assertEquals(iterator.next(), "18");
|
Assert.assertEquals(iterator.next(), "18");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Excel表格导出测试
|
||||||
@Test
|
@Test
|
||||||
public void dataTableExportTest(){
|
public void dataTableExportTest(){
|
||||||
DataTable table = springUtil.getBean(DataTable.class);
|
DataTable table = springUtil.getBean(DataTable.class);
|
||||||
@ -164,5 +172,32 @@ public class DataManagerTest {
|
|||||||
table.exportTable(new DataExcelGenerator("DataTableExport.xlsx"));
|
table.exportTable(new DataExcelGenerator("DataTableExport.xlsx"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 字符串文件测试
|
||||||
|
@Test
|
||||||
|
public void File2StringTest() throws IOException {
|
||||||
|
FileInputStream stream = new FileInputStream("test.pdf");
|
||||||
|
|
||||||
|
StringFileGenerator generator = springUtil.getBean(StringFileGenerator.class);
|
||||||
|
Optional<StringFile> file = generator.generateStringFile(stream);
|
||||||
|
|
||||||
|
// 检查是否转换成功
|
||||||
|
Assert.assertTrue(file.isPresent());
|
||||||
|
// 检查字符串文件的校验功能
|
||||||
|
Assert.assertTrue(generator.checkStringFile(file.get()));
|
||||||
|
|
||||||
|
// 输出转化
|
||||||
|
FileOutputStream outputStream = new FileOutputStream("testOut.pdf");
|
||||||
|
InputStream inputStream = generator.readFileString(file.get());
|
||||||
|
|
||||||
|
// 输出文件
|
||||||
|
int read = 0;
|
||||||
|
byte[] bytes = new byte[1024];
|
||||||
|
while ((read = inputStream.read(bytes)) != -1) {
|
||||||
|
outputStream.write(bytes, 0, read);
|
||||||
|
}
|
||||||
|
|
||||||
|
outputStream.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -10,9 +10,12 @@ import javafx.util.Pair;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.boot.test.context.SpringBootTest;
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
import org.springframework.test.context.junit4.SpringRunner;
|
import org.springframework.test.context.junit4.SpringRunner;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
import javax.annotation.Resource;
|
||||||
|
import javax.swing.text.html.Option;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 用户基本表单元测试
|
* 用户基本表单元测试
|
||||||
@ -20,6 +23,7 @@ import javax.annotation.Resource;
|
|||||||
*/
|
*/
|
||||||
@RunWith(SpringRunner.class)
|
@RunWith(SpringRunner.class)
|
||||||
@SpringBootTest
|
@SpringBootTest
|
||||||
|
@ActiveProfiles("test")
|
||||||
public class UserTest {
|
public class UserTest {
|
||||||
|
|
||||||
@Resource
|
@Resource
|
||||||
@ -30,12 +34,6 @@ public class UserTest {
|
|||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void UserBaseTest_1(){
|
public void UserBaseTest_1(){
|
||||||
// 查找数据库中是否有重复项
|
|
||||||
Pair<Boolean, User> checker = userService.checkIfUserExists("Tim");
|
|
||||||
if(checker.getKey()){
|
|
||||||
userService.delete(checker.getValue());
|
|
||||||
}
|
|
||||||
|
|
||||||
User user = userService.getDefaultUser();
|
User user = userService.getDefaultUser();
|
||||||
user.setUsername("Tim");
|
user.setUsername("Tim");
|
||||||
user.setPassword("123456");
|
user.setPassword("123456");
|
||||||
@ -44,6 +42,7 @@ public class UserTest {
|
|||||||
user.getUserAuth().setUserQuestion("Your favourite animal?");
|
user.getUserAuth().setUserQuestion("Your favourite animal?");
|
||||||
user.getUserAuth().setUserAnswer("Cat");
|
user.getUserAuth().setUserAnswer("Cat");
|
||||||
user.getUserDetail().setAtSchool(true);
|
user.getUserDetail().setAtSchool(true);
|
||||||
|
user.getUserDetail().setRealName("提姆");
|
||||||
userService.save(user);
|
userService.save(user);
|
||||||
|
|
||||||
user = userService.findUserByUsername("Tim");
|
user = userService.findUserByUsername("Tim");
|
||||||
@ -51,6 +50,7 @@ public class UserTest {
|
|||||||
assertEquals(user.getUsername(), "Tim");
|
assertEquals(user.getUsername(), "Tim");
|
||||||
assertEquals(user.getPassword(),
|
assertEquals(user.getPassword(),
|
||||||
"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
|
"8d969eef6ecad3c29a3a629280e686cf0c3f5d5a86aff3ca12020c923adc6c92");
|
||||||
|
|
||||||
// 检查账号状态
|
// 检查账号状态
|
||||||
assertTrue(user.isEnabled());
|
assertTrue(user.isEnabled());
|
||||||
assertFalse(user.isDeleted());
|
assertFalse(user.isDeleted());
|
||||||
@ -65,7 +65,19 @@ public class UserTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void UserBaseTest_2(){
|
public void UserBaseTest_2(){
|
||||||
|
User user = userService.findUserByUsername("Tim");
|
||||||
|
|
||||||
|
assertNotNull(user);
|
||||||
|
|
||||||
|
user.setEnabled(false);
|
||||||
|
user.getUserAuth().setMail("saturneric@163.com");
|
||||||
|
user.getUserDetail().setRealName("张三丰");
|
||||||
|
|
||||||
|
user = userService.update(user);
|
||||||
|
|
||||||
|
assertEquals(user.getUserAuth().getMail(), "saturneric@163.com");
|
||||||
|
assertEquals(user.getUserDetail().getRealName(), "张三丰");
|
||||||
|
assertFalse(user.isEnabled());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user