Skip to content

Commit

Permalink
!14 完善单元测试并提交单元测试的构建流水线
Browse files Browse the repository at this point in the history
* 修改流水线脚本的保存位置
* 修改代码生成器的编译部署流水线
* 解决代码生成器生成的Vo的版本号错误的BUG
  • Loading branch information
YunlongChen committed Dec 16, 2023
1 parent b0e0cab commit f76d46a
Show file tree
Hide file tree
Showing 16 changed files with 607 additions and 25 deletions.
40 changes: 40 additions & 0 deletions qing-codegen-plugin/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
pipeline {
agent {
docker {
image 'maven:3.9.5-eclipse-temurin-17-alpine'
args '-v /root/.m2:/root/.m2'
}
}
stages {
stage('编译打包') {
steps {
sh '''mvn clean install -pl qing-codegen-plugin/qing-codegen-apt -am -f pom.xml'''
}
}
stage('代码生成器单元测试') {
steps {
sh '''echo 清理工作空间'''
sh '''mvn clean -pl qing-codegen-plugin/qing-codegen-samples -f pom.xml'''
sh '''echo 开始测试'''
sh '''mvn clean test -pl qing-codegen-plugin/qing-codegen-samples -f pom.xml'''
}
post {
always {
// 收集测试报告
junit 'qing-codegen-plugin/qing-codegen-samples/target/surefire-reports/*.xml'
}
}
}
stage('代码生成器示例项目单元测试') {
steps {
sh '''mvn clean test -pl qing-codegen-plugin/qing-codegen-samples -f pom.xml'''
}
post {
always {
// 收集测试报告
junit 'qing-codegen-plugin/qing-codegen-samples/target/surefire-reports/*.xml'
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,18 @@
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeSpec.Builder;
import io.swagger.v3.oas.annotations.media.Schema;
import java.util.Objects;
import java.util.Set;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import lombok.AccessLevel;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import java.util.Objects;
import java.util.Set;

/**
* vo 代码生成器。
*
Expand All @@ -53,37 +54,38 @@ public class GenVoCodeProcessor extends AbstractCodeGenProcessor {
public void generateClass(TypeElement typeElement, RoundEnvironment roundEnv,
boolean useLombok) {
Set<VariableElement> fields =
findFields(typeElement,
variableElement -> Objects.isNull(variableElement.getAnnotation(IgnoreVo.class)));
findFields(typeElement,
variableElement -> Objects.isNull(variableElement.getAnnotation(IgnoreVo.class)));
String sourceClassName = typeElement.getSimpleName() + SUFFIX;
Builder builder = TypeSpec
.classBuilder(sourceClassName)
.superclass(AbstractBaseJpaVo.class)
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Schema.class);
.classBuilder(sourceClassName)
.superclass(AbstractBaseJpaVo.class)
.addModifiers(Modifier.PUBLIC)
.addAnnotation(Schema.class);
if (useLombok) {
builder.addAnnotation(Data.class);
builder.addAnnotation(///////////
AnnotationSpec.builder(EqualsAndHashCode.class).addMember("callSuper", "$L", true)
.build());
AnnotationSpec.builder(EqualsAndHashCode.class).addMember("callSuper", "$L", true)
.build());
builder.addAnnotation(AnnotationSpec
.builder(NoArgsConstructor.class)
.addMember("access", "$T.PROTECTED", AccessLevel.class)
.build());
.builder(NoArgsConstructor.class)
.addMember("access", "$T.PROTECTED", AccessLevel.class)
.build());
}
addSetterAndGetterMethod(builder, fields);
MethodSpec.Builder constructorSpecBuilder = MethodSpec
.constructorBuilder()
.addParameter(TypeName.get(typeElement.asType()), "source")
.addModifiers(Modifier.PUBLIC);
.constructorBuilder()
.addParameter(TypeName.get(typeElement.asType()), "source")
.addModifiers(Modifier.PUBLIC);
constructorSpecBuilder.addStatement("super()")
.addStatement("this.setId(source.getId());")
.addStatement("this.setCreatedAt(source.getCreatedAt());")
.addStatement("this.setUpdatedAt(source.getCreatedAt());");
.addStatement("this.setId(source.getId());")
.addStatement("this.setCreatedAt(source.getCreatedAt());")
.addStatement("this.setUpdatedAt(source.getCreatedAt());")
.addStatement("this.setVersion(source.getVersion());");

fields.forEach(
variableElement -> constructorSpecBuilder.addStatement("this.set$L(source.get$L())",
getFieldDefaultName(variableElement), getFieldDefaultName(variableElement)));
variableElement -> constructorSpecBuilder.addStatement("this.set$L(source.get$L())",
getFieldDefaultName(variableElement), getFieldDefaultName(variableElement)));
builder.addMethod(constructorSpecBuilder.build());
genJavaSourceFile(typeElement, builder);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package cn.chenyunlong.qing.samples.codegen.domain.controller;

import cn.chenyunlong.common.constants.CodeEnum;
import cn.chenyunlong.common.model.JsonResult;
import cn.chenyunlong.common.model.PageRequestWrapper;
import cn.chenyunlong.common.model.PageResult;
import cn.chenyunlong.qing.samples.codegen.domain.dto.creator.TestDomainCreator;
import cn.chenyunlong.qing.samples.codegen.domain.dto.query.TestDomainQuery;
import cn.chenyunlong.qing.samples.codegen.domain.dto.request.TestDomainCreateRequest;
import cn.chenyunlong.qing.samples.codegen.domain.dto.request.TestDomainQueryRequest;
import cn.chenyunlong.qing.samples.codegen.domain.dto.request.TestDomainUpdateRequest;
import cn.chenyunlong.qing.samples.codegen.domain.dto.response.TestDomainResponse;
import cn.chenyunlong.qing.samples.codegen.domain.dto.updater.TestDomainUpdater;
import cn.chenyunlong.qing.samples.codegen.domain.dto.vo.TestDomainVO;
import cn.chenyunlong.qing.samples.codegen.domain.mapper.TestDomainMapper;
import cn.chenyunlong.qing.samples.codegen.domain.service.ITestDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.stream.Collectors;

@RestController
@Slf4j
@RequestMapping("api/v1/test-domain")
@RequiredArgsConstructor
public class TestDomainController {
private final ITestDomainService testDomainService;

/**
* createRequest
*/
@PostMapping
public JsonResult<Long> createTestDomain(@RequestBody TestDomainCreateRequest request) {
TestDomainCreator creator = TestDomainMapper.INSTANCE.request2Dto(request);
return JsonResult.success(testDomainService.createTestDomain(creator));
}

/**
* update request
*/
@PostMapping("updateTestDomain")
public JsonResult<String> updateTestDomain(@RequestBody TestDomainUpdateRequest request) {
TestDomainUpdater updater = TestDomainMapper.INSTANCE.request2Updater(request);
testDomainService.updateTestDomain(updater);
return JsonResult.success(CodeEnum.Success.getName());
}

/**
* valid
*/
@PostMapping("valid/{id}")
public JsonResult<String> validTestDomain(@PathVariable Long id) {
testDomainService.validTestDomain(id);
return JsonResult.success(CodeEnum.Success.getName());
}

/**
* invalid
*/
@PostMapping("invalid/{id}")
public JsonResult<String> invalidTestDomain(@PathVariable Long id) {
testDomainService.invalidTestDomain(id);
return JsonResult.success(CodeEnum.Success.getName());
}

/**
* findById
*/
@GetMapping("findById/{id}")
public JsonResult<TestDomainResponse> findById(@PathVariable Long id) {
TestDomainVO vo = testDomainService.findById(id);
TestDomainResponse response = TestDomainMapper.INSTANCE.vo2CustomResponse(vo);
return JsonResult.success(response);
}

/**
* findByPage request
*/
@PostMapping("page")
public JsonResult<PageResult<TestDomainResponse>> page(
@RequestBody PageRequestWrapper<TestDomainQueryRequest> request) {
PageRequestWrapper<TestDomainQuery> wrapper = new PageRequestWrapper<>();
wrapper.setBean(TestDomainMapper.INSTANCE.request2Query(request.getBean()));
wrapper.setSorts(request.getSorts());
wrapper.setPageSize(request.getPageSize());
wrapper.setPage(request.getPage());
Page<TestDomainVO> page = testDomainService.findByPage(wrapper);
return JsonResult.success(
PageResult.of(
page.getContent().stream()
.map(TestDomainMapper.INSTANCE::vo2CustomResponse)
.collect(Collectors.toList()),
page.getTotalElements(),
page.getSize(),
page.getNumber())
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cn.chenyunlong.qing.samples.codegen.domain.dto.creator;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Schema
@Data
public class TestDomainCreator {
@Schema(
title = "username",
description = "username"
)
private String username;

@Schema(
title = "password",
description = "password"
)
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package cn.chenyunlong.qing.samples.codegen.domain.dto.query;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Schema
@Data
public class TestDomainQuery {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cn.chenyunlong.qing.samples.codegen.domain.dto.request;

import cn.chenyunlong.common.model.Request;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Schema
@Data
public class TestDomainCreateRequest implements Request {
@Schema(
title = "username",
description = "username"
)
private String username;

@Schema(
title = "password",
description = "password"
)
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package cn.chenyunlong.qing.samples.codegen.domain.dto.request;

import cn.chenyunlong.common.model.Request;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Schema
@Data
public class TestDomainQueryRequest implements Request {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package cn.chenyunlong.qing.samples.codegen.domain.dto.request;

import cn.chenyunlong.common.model.Request;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

@Schema
@Data
public class TestDomainUpdateRequest implements Request {
@Schema(
title = "username",
description = "username"
)
private String username;

@Schema(
title = "password",
description = "password"
)
private String password;

private Long id;

public Long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package cn.chenyunlong.qing.samples.codegen.domain.dto.response;

import cn.chenyunlong.common.model.AbstractJpaResponse;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

@Data
@Schema
@EqualsAndHashCode(
callSuper = true
)
public class TestDomainResponse extends AbstractJpaResponse {
@Schema(
title = "username",
description = "username"
)
private String username;

@Schema(
title = "password",
description = "password"
)
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.chenyunlong.qing.samples.codegen.domain.dto.updater;

import cn.chenyunlong.qing.samples.codegen.domain.TestDomain;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.Optional;

@Schema
@Data
public class TestDomainUpdater {
@Schema(
title = "username",
description = "username"
)
private String username;

@Schema(
title = "password",
description = "password"
)
private String password;

private Long id;

public void updateTestDomain(TestDomain param) {
Optional.ofNullable(getUsername()).ifPresent(param::setUsername);
Optional.ofNullable(getPassword()).ifPresent(param::setPassword);
}

public Long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}
}
Loading

0 comments on commit f76d46a

Please sign in to comment.