From c404eeb25fecd572d32cc09b6144da35ce831a30 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Sat, 17 Sep 2022 20:53:03 +0800 Subject: [PATCH 1/2] feature: Add data jpa --- pom.xml | 5 ++ server/pom.xml | 10 ++++ .../src/main/etc/conf/application.properties | 4 ++ .../server/configure/DataJpaConfigure.java | 54 +++++++++++++++++++ 4 files changed, 73 insertions(+) create mode 100644 server/src/main/java/io/edurt/datacap/server/configure/DataJpaConfigure.java diff --git a/pom.xml b/pom.xml index 636bc6b8..7daf2c86 100644 --- a/pom.xml +++ b/pom.xml @@ -46,6 +46,11 @@ spring-boot-starter-web ${springboot.version} + + org.springframework.boot + spring-boot-starter-data-jpa + ${springboot.version} + diff --git a/server/pom.xml b/server/pom.xml index 5292a122..b0f07436 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -13,6 +13,7 @@ DataCap for server + 8.0.28 3.1.1 @@ -21,6 +22,15 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + ${mysql.version} + diff --git a/server/src/main/etc/conf/application.properties b/server/src/main/etc/conf/application.properties index e1f38019..5eb01ebe 100644 --- a/server/src/main/etc/conf/application.properties +++ b/server/src/main/etc/conf/application.properties @@ -1 +1,5 @@ server.port=9096 +# Set env for datasource +spring.datasource.url=jdbc:mysql://localhost:3306/datacap?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false +spring.datasource.username=root +spring.datasource.password=12345678 diff --git a/server/src/main/java/io/edurt/datacap/server/configure/DataJpaConfigure.java b/server/src/main/java/io/edurt/datacap/server/configure/DataJpaConfigure.java new file mode 100644 index 00000000..f897de2e --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/configure/DataJpaConfigure.java @@ -0,0 +1,54 @@ +package io.edurt.datacap.server.configure; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaRepositories; +import org.springframework.orm.jpa.JpaTransactionManager; +import org.springframework.orm.jpa.JpaVendorAdapter; +import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; +import org.springframework.orm.jpa.vendor.Database; +import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.annotation.EnableTransactionManagement; + +import javax.persistence.EntityManagerFactory; +import javax.sql.DataSource; + +@Configuration +@EnableJpaRepositories(basePackages = "io.edurt.datacap.server.repository", + repositoryImplementationPostfix = "Impl", + entityManagerFactoryRef = "entityManagerFactory", + transactionManagerRef = "transactionManager") +@EnableTransactionManagement +public class DataJpaConfigure +{ + @Bean + public JpaVendorAdapter jpaVendorAdapter() + { + HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); + jpaVendorAdapter.setDatabase(Database.MYSQL); + jpaVendorAdapter.setShowSql(true); + jpaVendorAdapter.setGenerateDdl(false); + jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect"); + jpaVendorAdapter.setGenerateDdl(true); + return jpaVendorAdapter; + } + + @Bean + public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter) + { + LocalContainerEntityManagerFactoryBean managerFactoryBean = new LocalContainerEntityManagerFactoryBean(); + managerFactoryBean.setDataSource(dataSource); + managerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter); + managerFactoryBean.setPackagesToScan("io.edurt.datacap.server.entity"); + return managerFactoryBean; + } + + @Bean + public PlatformTransactionManager transactionManager(EntityManagerFactory managerFactory) + { + JpaTransactionManager transactionManager = new JpaTransactionManager(); + transactionManager.setEntityManagerFactory(managerFactory); + return transactionManager; + } +} From e94b18761cbf72b1a546d6f9054008a5db041db3 Mon Sep 17 00:00:00 2001 From: qianmoQ Date: Sun, 18 Sep 2022 13:21:08 +0800 Subject: [PATCH 2/2] api: Add source --- configure/git-hook/commit-msg | 2 +- configure/git-hook/pre-commit | 2 +- pom.xml | 11 ++ server/pom.xml | 27 +++++ .../io/edurt/datacap/server/common/JSON.java | 23 ++++ .../datacap/server/common/ProtocolEnum.java | 8 ++ .../edurt/datacap/server/common/Response.java | 46 ++++++++ .../datacap/server/common/ServiceState.java | 25 ++++ .../io/edurt/datacap/server/common/State.java | 25 ++++ .../server/configure/DataJpaConfigure.java | 3 +- .../server/controller/SourceController.java | 55 +++++++++ .../datacap/server/entity/PageEntity.java | 33 ++++++ .../datacap/server/entity/SourceEntity.java | 72 ++++++++++++ .../server/repository/SourceRepository.java | 9 ++ .../datacap/server/service/SourceService.java | 14 +++ .../service/impl/SourceServiceImpl.java | 42 +++++++ .../server/validation/ValidationGroup.java | 27 +++++ .../edurt/datacap/server/BaseParamTest.java | 22 ++++ .../controller/SourceControllerTest.java | 111 ++++++++++++++++++ .../repository/SourceRepositoryTest.java | 30 +++++ .../server/service/SourceServiceTest.java | 28 +++++ server/src/test/resources/data/source.sql | 2 + server/src/test/resources/schema/source.sql | 15 +++ 23 files changed, 628 insertions(+), 4 deletions(-) create mode 100644 server/src/main/java/io/edurt/datacap/server/common/JSON.java create mode 100644 server/src/main/java/io/edurt/datacap/server/common/ProtocolEnum.java create mode 100644 server/src/main/java/io/edurt/datacap/server/common/Response.java create mode 100644 server/src/main/java/io/edurt/datacap/server/common/ServiceState.java create mode 100644 server/src/main/java/io/edurt/datacap/server/common/State.java create mode 100644 server/src/main/java/io/edurt/datacap/server/controller/SourceController.java create mode 100644 server/src/main/java/io/edurt/datacap/server/entity/PageEntity.java create mode 100644 server/src/main/java/io/edurt/datacap/server/entity/SourceEntity.java create mode 100644 server/src/main/java/io/edurt/datacap/server/repository/SourceRepository.java create mode 100644 server/src/main/java/io/edurt/datacap/server/service/SourceService.java create mode 100644 server/src/main/java/io/edurt/datacap/server/service/impl/SourceServiceImpl.java create mode 100644 server/src/main/java/io/edurt/datacap/server/validation/ValidationGroup.java create mode 100644 server/src/test/java/io/edurt/datacap/server/BaseParamTest.java create mode 100644 server/src/test/java/io/edurt/datacap/server/controller/SourceControllerTest.java create mode 100644 server/src/test/java/io/edurt/datacap/server/repository/SourceRepositoryTest.java create mode 100644 server/src/test/java/io/edurt/datacap/server/service/SourceServiceTest.java create mode 100644 server/src/test/resources/data/source.sql create mode 100644 server/src/test/resources/schema/source.sql diff --git a/configure/git-hook/commit-msg b/configure/git-hook/commit-msg index 37d7a649..e4dba110 100644 --- a/configure/git-hook/commit-msg +++ b/configure/git-hook/commit-msg @@ -3,7 +3,7 @@ commit_msg=$(cat "$1") email=$(git config user.email) -msg_re="^(feature|fix|docs|style|refactor|perf|test|workflow|build|ci|env|release)(\(.+\))?: .{1,100}" +msg_re="^(feature|fix|docs|style|refactor|perf|test|workflow|build|ci|env|release|api|web)(\(.+\))?: .{1,100}" if [[ ! $commit_msg =~ $msg_re ]]; then printf "Invalid commit message submission format. Please use the correct format:\ diff --git a/configure/git-hook/pre-commit b/configure/git-hook/pre-commit index 47b7f16b..b5b36742 100644 --- a/configure/git-hook/pre-commit +++ b/configure/git-hook/pre-commit @@ -2,4 +2,4 @@ printf "Code verification before submission" -./mvnw clean checkstyle:checkstyle findbugs:findbugs cobertura:cobertura -X +./mvnw clean checkstyle:checkstyle findbugs:findbugs -X diff --git a/pom.xml b/pom.xml index 7daf2c86..4d3dccee 100644 --- a/pom.xml +++ b/pom.xml @@ -18,6 +18,7 @@ 2.7.3 1.18.24 4.13.2 + 3.12.0 3.0.0 3.0.5 3.3 @@ -51,6 +52,16 @@ spring-boot-starter-data-jpa ${springboot.version} + + org.springframework.boot + spring-boot-starter-test + ${springboot.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + diff --git a/server/pom.xml b/server/pom.xml index b0f07436..0f8e1613 100644 --- a/server/pom.xml +++ b/server/pom.xml @@ -14,6 +14,8 @@ 8.0.28 + 2.1.214 + 3.0.1 3.1.1 @@ -26,11 +28,36 @@ org.springframework.boot spring-boot-starter-data-jpa + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-validation + ${springboot.version} + mysql mysql-connector-java ${mysql.version} + + com.h2database + h2 + ${h2.version} + test + + + org.apache.commons + commons-lang3 + + + com.google.code.findbugs + findbugs + ${findbugs.version} + diff --git a/server/src/main/java/io/edurt/datacap/server/common/JSON.java b/server/src/main/java/io/edurt/datacap/server/common/JSON.java new file mode 100644 index 00000000..b4f09bd5 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/common/JSON.java @@ -0,0 +1,23 @@ +package io.edurt.datacap.server.common; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class JSON +{ + public static final ObjectMapper objectmapper = new ObjectMapper(); + + private JSON() {} + + public static String toJSON(Object object) + { + String json; + try { + json = objectmapper.writeValueAsString(object); + } + catch (JsonProcessingException e) { + json = null; + } + return json; + } +} diff --git a/server/src/main/java/io/edurt/datacap/server/common/ProtocolEnum.java b/server/src/main/java/io/edurt/datacap/server/common/ProtocolEnum.java new file mode 100644 index 00000000..721834f2 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/common/ProtocolEnum.java @@ -0,0 +1,8 @@ +package io.edurt.datacap.server.common; + +public enum ProtocolEnum +{ + HTTP, + HTTPS, + SSH +} diff --git a/server/src/main/java/io/edurt/datacap/server/common/Response.java b/server/src/main/java/io/edurt/datacap/server/common/Response.java new file mode 100644 index 00000000..fa17e734 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/common/Response.java @@ -0,0 +1,46 @@ +package io.edurt.datacap.server.common; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@Data +@ToString +@NoArgsConstructor +@AllArgsConstructor +public class Response +{ + private Boolean status; + private Integer code; + private String message; + private T data; + + public static Response success(Object data) + { + Response response = new Response(); + response.code = State.SUCCESS.getCode(); + response.message = State.SUCCESS.getValue(); + response.data = data; + response.status = true; + return response; + } + + public static Response failure(String message) + { + Response response = new Response(); + response.code = State.FAILURE.getCode(); + response.message = message; + response.status = false; + return response; + } + + public static Response failure(ServiceState state) + { + Response response = new Response(); + response.code = state.getCode(); + response.message = state.getValue(); + response.status = false; + return response; + } +} diff --git a/server/src/main/java/io/edurt/datacap/server/common/ServiceState.java b/server/src/main/java/io/edurt/datacap/server/common/ServiceState.java new file mode 100644 index 00000000..557fcfb1 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/common/ServiceState.java @@ -0,0 +1,25 @@ +package io.edurt.datacap.server.common; + +public enum ServiceState +{ + SOURCE_NOT_FOUND(1001, "Source does not exist"); + + private Integer code; + private String value; + + ServiceState(Integer code, String value) + { + this.code = code; + this.value = value; + } + + public Integer getCode() + { + return code; + } + + public String getValue() + { + return value; + } +} diff --git a/server/src/main/java/io/edurt/datacap/server/common/State.java b/server/src/main/java/io/edurt/datacap/server/common/State.java new file mode 100644 index 00000000..21496c25 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/common/State.java @@ -0,0 +1,25 @@ +package io.edurt.datacap.server.common; + +public enum State +{ + SUCCESS(200, "Query successful"), + FAILURE(400, "Query failure"); + private Integer code; + private String value; + + State(Integer code, String value) + { + this.code = code; + this.value = value; + } + + public Integer getCode() + { + return code; + } + + public String getValue() + { + return value; + } +} diff --git a/server/src/main/java/io/edurt/datacap/server/configure/DataJpaConfigure.java b/server/src/main/java/io/edurt/datacap/server/configure/DataJpaConfigure.java index f897de2e..d14c427f 100644 --- a/server/src/main/java/io/edurt/datacap/server/configure/DataJpaConfigure.java +++ b/server/src/main/java/io/edurt/datacap/server/configure/DataJpaConfigure.java @@ -28,9 +28,8 @@ public class DataJpaConfigure HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter(); jpaVendorAdapter.setDatabase(Database.MYSQL); jpaVendorAdapter.setShowSql(true); - jpaVendorAdapter.setGenerateDdl(false); jpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.MySQL5Dialect"); - jpaVendorAdapter.setGenerateDdl(true); + jpaVendorAdapter.setGenerateDdl(false); return jpaVendorAdapter; } diff --git a/server/src/main/java/io/edurt/datacap/server/controller/SourceController.java b/server/src/main/java/io/edurt/datacap/server/controller/SourceController.java new file mode 100644 index 00000000..acdfe893 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/controller/SourceController.java @@ -0,0 +1,55 @@ +package io.edurt.datacap.server.controller; + +import io.edurt.datacap.server.common.Response; +import io.edurt.datacap.server.entity.PageEntity; +import io.edurt.datacap.server.entity.SourceEntity; +import io.edurt.datacap.server.service.SourceService; +import io.edurt.datacap.server.validation.ValidationGroup; +import org.springframework.http.MediaType; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController() +@RequestMapping(value = "/api/v1/source") +public class SourceController +{ + private final SourceService sourceService; + + public SourceController(SourceService sourceService) + { + this.sourceService = sourceService; + } + + @PostMapping(produces = {MediaType.APPLICATION_JSON_VALUE}) + public Response save(@RequestBody @Validated(ValidationGroup.Crud.Create.class) SourceEntity configure) + { + return this.sourceService.saveOrUpdate(configure); + } + + @PutMapping(produces = {MediaType.APPLICATION_JSON_VALUE}) + public Response update(@RequestBody @Validated(ValidationGroup.Crud.Update.class) SourceEntity configure) + { + return this.sourceService.saveOrUpdate(configure); + } + + @GetMapping + public Response> getAll(@RequestParam(value = "start", defaultValue = "1") int start, + @RequestParam(value = "size", defaultValue = "10") int end) + { + return this.sourceService.getAll(start, end); + } + + @DeleteMapping(value = "{id}") + public Response delete(@PathVariable(value = "id") Long id) + { + return this.sourceService.delete(id); + } +} diff --git a/server/src/main/java/io/edurt/datacap/server/entity/PageEntity.java b/server/src/main/java/io/edurt/datacap/server/entity/PageEntity.java new file mode 100644 index 00000000..972afc4c --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/entity/PageEntity.java @@ -0,0 +1,33 @@ +package io.edurt.datacap.server.entity; + +import lombok.Data; +import lombok.ToString; +import org.springframework.data.domain.Page; + +import java.util.List; + +@Data +@ToString +public class PageEntity +{ + private int page; + private int size; + private long total; + private long totalPage; + private List content; + + private PageEntity() + { + } + + public static PageEntity build(Page page) + { + PageEntity pageEntity = new PageEntity<>(); + pageEntity.setPage(page.getNumber()); + pageEntity.setSize(page.getSize()); + pageEntity.setTotal(page.getTotalElements()); + pageEntity.setTotalPage(page.getTotalPages()); + pageEntity.setContent(page.getContent()); + return pageEntity; + } +} diff --git a/server/src/main/java/io/edurt/datacap/server/entity/SourceEntity.java b/server/src/main/java/io/edurt/datacap/server/entity/SourceEntity.java new file mode 100644 index 00000000..c84278a4 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/entity/SourceEntity.java @@ -0,0 +1,72 @@ +package io.edurt.datacap.server.entity; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; +import io.edurt.datacap.server.common.ProtocolEnum; +import io.edurt.datacap.server.validation.ValidationGroup; +import lombok.Data; +import lombok.NoArgsConstructor; +import lombok.ToString; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.EnumType; +import javax.persistence.Enumerated; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; +import javax.validation.constraints.NotEmpty; +import javax.validation.constraints.NotNull; + +import java.sql.Timestamp; + +@Data +@ToString +@NoArgsConstructor +@Entity +@Table(name = "source") +@org.hibernate.annotations.Table(appliesTo = "source", comment = "The storage is used to query the data connection source") +@SuppressFBWarnings(value = {"EI_EXPOSE_REP"}, + justification = "I prefer to suppress these FindBugs warnings") +public class SourceEntity +{ + @Id() + @GeneratedValue(strategy = GenerationType.IDENTITY) + @NotNull(groups = {ValidationGroup.Crud.Update.class}, message = "The passed source id cannot be empty") + private Long id; + + @Column(name = "name", unique = true, nullable = false) + @NotEmpty(message = "The passed name cannot by empty") + private String name; + + @Column(name = "description") + private String description; + + @Column(name = "protocol", unique = true, nullable = false, columnDefinition = "varchar default 'HTTP'") + @Enumerated(EnumType.STRING) + @NotNull(message = "The passed protocol cannot by empty") + private ProtocolEnum protocol; + + @Column(name = "host", unique = true, nullable = false) + @NotEmpty(message = "The passed host cannot by empty") + private String host; + + @Column(name = "port", unique = true, nullable = false) + @NotNull(message = "The passed port cannot by empty") + private Integer port; + + @Column(name = "username") + private String username; + + @Column(name = "password") + private String password; + + @Column(name = "_catalog") + private String catalog; + + @Column(name = "_database") + private String database; + + @Column(name = "create_time", columnDefinition = "datetime default CURRENT_TIMESTAMP()") + private Timestamp createTime; +} diff --git a/server/src/main/java/io/edurt/datacap/server/repository/SourceRepository.java b/server/src/main/java/io/edurt/datacap/server/repository/SourceRepository.java new file mode 100644 index 00000000..d4df7a04 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/repository/SourceRepository.java @@ -0,0 +1,9 @@ +package io.edurt.datacap.server.repository; + +import io.edurt.datacap.server.entity.SourceEntity; +import org.springframework.data.repository.PagingAndSortingRepository; + +public interface SourceRepository + extends PagingAndSortingRepository +{ +} diff --git a/server/src/main/java/io/edurt/datacap/server/service/SourceService.java b/server/src/main/java/io/edurt/datacap/server/service/SourceService.java new file mode 100644 index 00000000..9a489e5f --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/service/SourceService.java @@ -0,0 +1,14 @@ +package io.edurt.datacap.server.service; + +import io.edurt.datacap.server.common.Response; +import io.edurt.datacap.server.entity.PageEntity; +import io.edurt.datacap.server.entity.SourceEntity; + +public interface SourceService +{ + Response saveOrUpdate(SourceEntity configure); + + Response> getAll(int offset, int limit); + + Response delete(Long id); +} diff --git a/server/src/main/java/io/edurt/datacap/server/service/impl/SourceServiceImpl.java b/server/src/main/java/io/edurt/datacap/server/service/impl/SourceServiceImpl.java new file mode 100644 index 00000000..328a6507 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/service/impl/SourceServiceImpl.java @@ -0,0 +1,42 @@ +package io.edurt.datacap.server.service.impl; + +import io.edurt.datacap.server.common.Response; +import io.edurt.datacap.server.entity.PageEntity; +import io.edurt.datacap.server.entity.SourceEntity; +import io.edurt.datacap.server.repository.SourceRepository; +import io.edurt.datacap.server.service.SourceService; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; +import org.springframework.stereotype.Service; + +@Service +public class SourceServiceImpl + implements SourceService +{ + private final SourceRepository sourceRepository; + + public SourceServiceImpl(SourceRepository sourceRepository) + { + this.sourceRepository = sourceRepository; + } + + @Override + public Response saveOrUpdate(SourceEntity configure) + { + return Response.success(this.sourceRepository.save(configure)); + } + + @Override + public Response> getAll(int offset, int limit) + { + Pageable pageable = PageRequest.of(offset, limit); + return Response.success(PageEntity.build(this.sourceRepository.findAll(pageable))); + } + + @Override + public Response delete(Long id) + { + this.sourceRepository.deleteById(id); + return Response.success(id); + } +} diff --git a/server/src/main/java/io/edurt/datacap/server/validation/ValidationGroup.java b/server/src/main/java/io/edurt/datacap/server/validation/ValidationGroup.java new file mode 100644 index 00000000..40e864c7 --- /dev/null +++ b/server/src/main/java/io/edurt/datacap/server/validation/ValidationGroup.java @@ -0,0 +1,27 @@ +package io.edurt.datacap.server.validation; + +import javax.validation.groups.Default; + +public interface ValidationGroup + extends Default +{ + interface Crud + extends ValidationGroup + { + interface Create + extends Crud + {} + + interface Update + extends Crud + {} + + interface Query + extends Crud + {} + + interface Delete + extends Crud + {} + } +} diff --git a/server/src/test/java/io/edurt/datacap/server/BaseParamTest.java b/server/src/test/java/io/edurt/datacap/server/BaseParamTest.java new file mode 100644 index 00000000..56cf5a8b --- /dev/null +++ b/server/src/test/java/io/edurt/datacap/server/BaseParamTest.java @@ -0,0 +1,22 @@ +package io.edurt.datacap.server; + +import io.edurt.datacap.server.common.ProtocolEnum; +import io.edurt.datacap.server.entity.SourceEntity; + +public class BaseParamTest +{ + private BaseParamTest() + { + } + + public static SourceEntity builderSource() + { + SourceEntity source = new SourceEntity(); + source.setName("Test"); + source.setDescription("This is test source"); + source.setHost("localhost"); + source.setPort(3306); + source.setProtocol(ProtocolEnum.HTTP); + return source; + } +} diff --git a/server/src/test/java/io/edurt/datacap/server/controller/SourceControllerTest.java b/server/src/test/java/io/edurt/datacap/server/controller/SourceControllerTest.java new file mode 100644 index 00000000..185ae859 --- /dev/null +++ b/server/src/test/java/io/edurt/datacap/server/controller/SourceControllerTest.java @@ -0,0 +1,111 @@ +package io.edurt.datacap.server.controller; + +import io.edurt.datacap.server.BaseParamTest; +import io.edurt.datacap.server.common.JSON; +import io.edurt.datacap.server.entity.SourceEntity; +import lombok.extern.slf4j.Slf4j; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.SqlGroup; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultHandlers; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; + +@RunWith(value = SpringRunner.class) +@SpringBootTest +@WebAppConfiguration +@Slf4j +public class SourceControllerTest +{ + @Autowired + private SourceController sourceController; + + private MockMvc mockMvc; + + @Before + public void setup() + { + mockMvc = MockMvcBuilders.standaloneSetup(sourceController).build(); + } + + @Test + @Sql(value = "classpath:schema/source.sql") + public void save() + throws Exception + { + MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/api/v1/source") + .contentType(MediaType.APPLICATION_JSON) + .content(JSON.objectmapper.writeValueAsString(BaseParamTest.builderSource()))) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200)) + .andDo(MockMvcResultHandlers.print()) + .andReturn(); + log.info(mvcResult.getResponse().getContentAsString()); + } + + @Test + @SqlGroup(value = { + @Sql(value = "classpath:schema/source.sql"), + @Sql(value = "classpath:data/source.sql") + }) + public void update() + throws Exception + { + SourceEntity source = BaseParamTest.builderSource(); + source.setName("TestSource_1"); + source.setId(1L); + MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.put("/api/v1/source") + .contentType(MediaType.APPLICATION_JSON) + .content(JSON.objectmapper.writeValueAsString(source))) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200)) + .andDo(MockMvcResultHandlers.print()) + .andReturn(); + log.info(mvcResult.getResponse().getContentAsString()); + } + + @Test + @SqlGroup(value = { + @Sql(value = "classpath:schema/source.sql"), + @Sql(value = "classpath:data/source.sql") + }) + public void getAll() + throws Exception + { + MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/api/v1/source") + .param("start", "1") + .param("end", "10")) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200)) + .andDo(MockMvcResultHandlers.print()) + .andReturn(); + log.info(mvcResult.getResponse().getContentAsString()); + } + + @Test + @SqlGroup(value = { + @Sql(value = "classpath:schema/source.sql"), + @Sql(value = "classpath:data/source.sql") + }) + public void delete() + throws Exception + { + Long id = Long.valueOf(1); + MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.delete("/api/v1/source/" + id)) + .andExpect(MockMvcResultMatchers.status().isOk()) + .andExpect(MockMvcResultMatchers.jsonPath("$.code").value(200)) + .andDo(MockMvcResultHandlers.print()) + .andReturn(); + log.info(mvcResult.getResponse().getContentAsString()); + } +} \ No newline at end of file diff --git a/server/src/test/java/io/edurt/datacap/server/repository/SourceRepositoryTest.java b/server/src/test/java/io/edurt/datacap/server/repository/SourceRepositoryTest.java new file mode 100644 index 00000000..7eb77600 --- /dev/null +++ b/server/src/test/java/io/edurt/datacap/server/repository/SourceRepositoryTest.java @@ -0,0 +1,30 @@ +package io.edurt.datacap.server.repository; + +import io.edurt.datacap.server.BaseParamTest; +import io.edurt.datacap.server.common.ProtocolEnum; +import io.edurt.datacap.server.entity.SourceEntity; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(value = SpringRunner.class) +@DataJpaTest +public class SourceRepositoryTest +{ + @Autowired + private SourceRepository sourceRepository; + + @Test + @Sql(value = "classpath:schema/source.sql") + public void save() + { + SourceEntity source = BaseParamTest.builderSource(); + this.sourceRepository.save(source); + assertThat(source.getId() > 0); + } +} diff --git a/server/src/test/java/io/edurt/datacap/server/service/SourceServiceTest.java b/server/src/test/java/io/edurt/datacap/server/service/SourceServiceTest.java new file mode 100644 index 00000000..9475a1f1 --- /dev/null +++ b/server/src/test/java/io/edurt/datacap/server/service/SourceServiceTest.java @@ -0,0 +1,28 @@ +package io.edurt.datacap.server.service; + +import io.edurt.datacap.server.BaseParamTest; +import io.edurt.datacap.server.common.ProtocolEnum; +import io.edurt.datacap.server.entity.SourceEntity; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.junit4.SpringRunner; + +import static org.assertj.core.api.Assertions.assertThat; + +@RunWith(value = SpringRunner.class) +@SpringBootTest +public class SourceServiceTest +{ + @Autowired + private SourceService sourceService; + + @Test + @Sql(value = "classpath:schema/source.sql") + public void saveOrUpdate() + { + assertThat(this.sourceService.saveOrUpdate(BaseParamTest.builderSource()).getStatus()); + } +} \ No newline at end of file diff --git a/server/src/test/resources/data/source.sql b/server/src/test/resources/data/source.sql new file mode 100644 index 00000000..8c0a7f17 --- /dev/null +++ b/server/src/test/resources/data/source.sql @@ -0,0 +1,2 @@ +insert into source(_catalog, _database, description, host, name, password, port, protocol, username) +values ('default', 'datacap', 'This is description', 'localhost', 'TestSource', null, 3306, 'HTTP', 'default'); diff --git a/server/src/test/resources/schema/source.sql b/server/src/test/resources/schema/source.sql new file mode 100644 index 00000000..c55eded2 --- /dev/null +++ b/server/src/test/resources/schema/source.sql @@ -0,0 +1,15 @@ +create table if not exists source +( + id bigint not null auto_increment, + _catalog varchar(255), + create_time datetime default CURRENT_TIMESTAMP(), + _database varchar(255), + description varchar(255), + host varchar(255) not null, + name varchar(255) not null, + password varchar(255), + port bigint not null, + protocol varchar(255), + username varchar(255), + primary key (id) +);