From d5f7319d9e1e98efe02e8e7643a6410404822085 Mon Sep 17 00:00:00 2001 From: Anton Mishyn <45338450+AntonMishyn@users.noreply.github.com> Date: Fri, 6 Nov 2020 16:06:22 +0200 Subject: [PATCH] Set up H2 database for tests (#28) * Fetch types optimized * Entity fetch types optimized, Lombok lib added to dependencies, lombok annotations used on Entities * Implemented storing java enums as varchars is DB, changed migration schema, set up h2 to be used with test * Updated entity names * Corrected namings, parameters, template files --- .../dokazovi/controller/PostController.java | 4 +- .../{Charity.java => CharityEntity.java} | 8 ++-- .../{Direction.java => DirectionEntity.java} | 12 ++--- ...nstitution.java => InstitutionEntity.java} | 10 ++--- .../entity/{Post.java => PostEntity.java} | 37 ++++++---------- .../{PostType.java => PostTypeEntity.java} | 8 ++-- .../entity/{Region.java => RegionEntity.java} | 9 ++-- .../entity/{Role.java => RoleEntity.java} | 8 ++-- .../entity/{Source.java => SourceEntity.java} | 12 +++-- .../entity/{Tag.java => TagEntity.java} | 8 ++-- .../entity/{User.java => UserEntity.java} | 24 +++++----- .../dokazovi/entity/UserInstitution.java | 44 ------------------- .../entity/UserInstitutionEntity.java | 44 +++++++++++++++++++ .../entity/embeddables/UserInstitutionId.java | 4 +- .../PostgreSQLPostStatusEnumType.java | 39 ---------------- .../PostgreSQLSourceTypeEnumType.java | 39 ---------------- .../PostgreSQLUserStatusEnumType.java | 39 ---------------- .../repositories/DirectionRepository.java | 4 +- .../repositories/InstitutionRepository.java | 4 +- .../dokazovi/repositories/PostRepository.java | 4 +- .../repositories/PostTypeRepository.java | 4 +- .../repositories/RegionRepository.java | 4 +- .../dokazovi/repositories/RoleRepository.java | 4 +- .../repositories/SourceRepository.java | 4 +- .../dokazovi/repositories/TagRepository.java | 4 +- .../dokazovi/repositories/UserRepository.java | 4 +- .../dokazovi/service/PostService.java | 4 +- .../service/impl/PostServiceImpl.java | 4 +- .../resources/db/migration/V1__init_db.sql | 32 ++------------ src/test/resources/application.properties | 20 +++++++++ 30 files changed, 153 insertions(+), 292 deletions(-) rename src/main/java/com/softserveinc/dokazovi/entity/{Charity.java => CharityEntity.java} (85%) rename src/main/java/com/softserveinc/dokazovi/entity/{Direction.java => DirectionEntity.java} (76%) rename src/main/java/com/softserveinc/dokazovi/entity/{Institution.java => InstitutionEntity.java} (82%) rename src/main/java/com/softserveinc/dokazovi/entity/{Post.java => PostEntity.java} (73%) rename src/main/java/com/softserveinc/dokazovi/entity/{PostType.java => PostTypeEntity.java} (82%) rename src/main/java/com/softserveinc/dokazovi/entity/{Region.java => RegionEntity.java} (79%) rename src/main/java/com/softserveinc/dokazovi/entity/{Role.java => RoleEntity.java} (82%) rename src/main/java/com/softserveinc/dokazovi/entity/{Source.java => SourceEntity.java} (75%) rename src/main/java/com/softserveinc/dokazovi/entity/{Tag.java => TagEntity.java} (82%) rename src/main/java/com/softserveinc/dokazovi/entity/{User.java => UserEntity.java} (80%) delete mode 100644 src/main/java/com/softserveinc/dokazovi/entity/UserInstitution.java create mode 100644 src/main/java/com/softserveinc/dokazovi/entity/UserInstitutionEntity.java delete mode 100644 src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLPostStatusEnumType.java delete mode 100644 src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLSourceTypeEnumType.java delete mode 100644 src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLUserStatusEnumType.java create mode 100644 src/test/resources/application.properties diff --git a/src/main/java/com/softserveinc/dokazovi/controller/PostController.java b/src/main/java/com/softserveinc/dokazovi/controller/PostController.java index ccbe81a0..2497854e 100644 --- a/src/main/java/com/softserveinc/dokazovi/controller/PostController.java +++ b/src/main/java/com/softserveinc/dokazovi/controller/PostController.java @@ -1,7 +1,7 @@ package com.softserveinc.dokazovi.controller; import com.softserveinc.dokazovi.annotations.ApiPageable; -import com.softserveinc.dokazovi.entity.Post; +import com.softserveinc.dokazovi.entity.PostEntity; import com.softserveinc.dokazovi.service.PostService; import io.swagger.annotations.ApiOperation; import org.springframework.data.domain.Page; @@ -25,7 +25,7 @@ public PostController(PostService postService) { @ApiOperation(value = "Get posts by page") @ApiPageable @GetMapping("/") - public ResponseEntity> findAll(Pageable pageable) { + public ResponseEntity> findAll(Pageable pageable) { return ResponseEntity .status(HttpStatus.OK) .body(postService.findAll(pageable)); diff --git a/src/main/java/com/softserveinc/dokazovi/entity/Charity.java b/src/main/java/com/softserveinc/dokazovi/entity/CharityEntity.java similarity index 85% rename from src/main/java/com/softserveinc/dokazovi/entity/Charity.java rename to src/main/java/com/softserveinc/dokazovi/entity/CharityEntity.java index 3637c563..24bfc1e9 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/Charity.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/CharityEntity.java @@ -18,9 +18,9 @@ @Data @NoArgsConstructor -@Entity(name = "charities") +@Entity(name = "charity_entity") @Table(name = "charities") -public class Charity implements Serializable { +public class CharityEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -32,7 +32,7 @@ public class Charity implements Serializable { @ManyToOne @JoinColumn(name = "author_id") - private User author; + private UserEntity author; @CreationTimestamp private Timestamp createdAt; @@ -40,7 +40,7 @@ public class Charity implements Serializable { @UpdateTimestamp private Timestamp modifiedAt; - public Charity(String body, User author) { + public CharityEntity(String body, UserEntity author) { this.body = body; this.author = author; } diff --git a/src/main/java/com/softserveinc/dokazovi/entity/Direction.java b/src/main/java/com/softserveinc/dokazovi/entity/DirectionEntity.java similarity index 76% rename from src/main/java/com/softserveinc/dokazovi/entity/Direction.java rename to src/main/java/com/softserveinc/dokazovi/entity/DirectionEntity.java index 5ef6ebb0..b459065b 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/Direction.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/DirectionEntity.java @@ -19,9 +19,9 @@ @Data @NoArgsConstructor -@Entity(name = "directions") +@Entity(name = "direction_entity") @Table(name = "directions") -public class Direction implements Serializable { +public class DirectionEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -33,19 +33,19 @@ public class Direction implements Serializable { @OneToMany(mappedBy = "mainDirection") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set mainPostsDirections = new HashSet<>(); + private Set mainPostsDirections = new HashSet<>(); @ManyToMany(mappedBy = "directions") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set posts = new HashSet<>(); + private Set posts = new HashSet<>(); @ManyToMany(mappedBy = "directions") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set users = new HashSet<>(); + private Set userEntities = new HashSet<>(); - public Direction(String name) { + public DirectionEntity(String name) { this.name = name; } diff --git a/src/main/java/com/softserveinc/dokazovi/entity/Institution.java b/src/main/java/com/softserveinc/dokazovi/entity/InstitutionEntity.java similarity index 82% rename from src/main/java/com/softserveinc/dokazovi/entity/Institution.java rename to src/main/java/com/softserveinc/dokazovi/entity/InstitutionEntity.java index 3fcf6e5d..616d534c 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/Institution.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/InstitutionEntity.java @@ -21,9 +21,9 @@ @Data @NoArgsConstructor -@Entity(name = "institutions") +@Entity(name = "institution_entity") @Table(name = "institutions") -public class Institution implements Serializable { +public class InstitutionEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -35,7 +35,7 @@ public class Institution implements Serializable { @ManyToOne @JoinColumn(name = "region_id", nullable = false) - private Region region; + private RegionEntity region; @Column(name = "address", nullable = false) private String address; @@ -47,9 +47,9 @@ public class Institution implements Serializable { ) @EqualsAndHashCode.Exclude @ToString.Exclude - private Set users = new HashSet<>(); + private Set users = new HashSet<>(); - public Institution(Region region, String name, String address) { + public InstitutionEntity(RegionEntity region, String name, String address) { this.name = name; this.region = region; this.address = address; diff --git a/src/main/java/com/softserveinc/dokazovi/entity/Post.java b/src/main/java/com/softserveinc/dokazovi/entity/PostEntity.java similarity index 73% rename from src/main/java/com/softserveinc/dokazovi/entity/Post.java rename to src/main/java/com/softserveinc/dokazovi/entity/PostEntity.java index ef939db7..660aed84 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/Post.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/PostEntity.java @@ -7,7 +7,6 @@ import lombok.ToString; import org.hibernate.annotations.ColumnDefault; import org.hibernate.annotations.CreationTimestamp; -import org.hibernate.annotations.Type; import org.hibernate.annotations.UpdateTimestamp; import javax.persistence.CascadeType; @@ -31,9 +30,9 @@ @Data @NoArgsConstructor -@Entity(name = "posts") +@Entity(name = "post_entity") @Table(name = "posts") -public class Post implements Serializable { +public class PostEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -50,24 +49,20 @@ public class Post implements Serializable { @ManyToOne @JoinColumn(name = "author_id") - private User author; + private UserEntity author; @ManyToOne @JoinColumn(name = "type_id") - private PostType type; + private PostTypeEntity type; @ManyToOne @JoinColumn(name = "direction_id") - private Direction mainDirection; + private DirectionEntity mainDirection; @Enumerated(EnumType.STRING) - @Type(type = "com.softserveinc.dokazovi.entity.enumerations.PostgreSQLPostStatusEnumType") private PostStatus status; - @ManyToMany( - cascade = {CascadeType.REFRESH, CascadeType.MERGE}, - fetch = FetchType.EAGER - ) + @ManyToMany(cascade = {CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.EAGER) @JoinTable( name = "posts_directions", joinColumns = {@JoinColumn(name = "post_id")}, @@ -75,12 +70,9 @@ public class Post implements Serializable { ) @EqualsAndHashCode.Exclude @ToString.Exclude - private Set directions = new HashSet<>(); + private Set directions = new HashSet<>(); - @ManyToMany( - cascade = {CascadeType.REFRESH, CascadeType.MERGE}, - fetch = FetchType.EAGER - ) + @ManyToMany(cascade = {CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.EAGER) @JoinTable( name = "posts_tags", joinColumns = {@JoinColumn(name = "post_id")}, @@ -88,12 +80,9 @@ public class Post implements Serializable { ) @EqualsAndHashCode.Exclude @ToString.Exclude - private Set tags = new HashSet<>(); + private Set tags = new HashSet<>(); - @ManyToMany( - cascade = {CascadeType.REFRESH, CascadeType.MERGE}, - fetch = FetchType.EAGER - ) + @ManyToMany(cascade = {CascadeType.REFRESH, CascadeType.MERGE}, fetch = FetchType.EAGER) @JoinTable( name = "posts_sources", joinColumns = {@JoinColumn(name = "source_id")}, @@ -101,7 +90,7 @@ public class Post implements Serializable { ) @EqualsAndHashCode.Exclude @ToString.Exclude - private Set sources = new HashSet<>(); + private Set sources = new HashSet<>(); @CreationTimestamp private Timestamp createdAt; @@ -109,8 +98,8 @@ public class Post implements Serializable { @UpdateTimestamp private Timestamp modifiedAt; - public Post(String title, String content, boolean important, User author, - PostType type, Direction mainDirection, PostStatus status) { + public PostEntity(String title, String content, boolean important, UserEntity author, + PostTypeEntity type, DirectionEntity mainDirection, PostStatus status) { this.title = title; this.content = content; this.important = important; diff --git a/src/main/java/com/softserveinc/dokazovi/entity/PostType.java b/src/main/java/com/softserveinc/dokazovi/entity/PostTypeEntity.java similarity index 82% rename from src/main/java/com/softserveinc/dokazovi/entity/PostType.java rename to src/main/java/com/softserveinc/dokazovi/entity/PostTypeEntity.java index b6f76222..723fbf4a 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/PostType.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/PostTypeEntity.java @@ -17,9 +17,9 @@ @Data @NoArgsConstructor -@Entity(name = "post_types") +@Entity(name = "post_type_entity") @Table(name = "post_types") -public class PostType implements Serializable { +public class PostTypeEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -31,9 +31,9 @@ public class PostType implements Serializable { @OneToMany(mappedBy = "type") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set posts; + private Set posts; - public PostType(String name) { + public PostTypeEntity(String name) { this.name = name; } diff --git a/src/main/java/com/softserveinc/dokazovi/entity/Region.java b/src/main/java/com/softserveinc/dokazovi/entity/RegionEntity.java similarity index 79% rename from src/main/java/com/softserveinc/dokazovi/entity/Region.java rename to src/main/java/com/softserveinc/dokazovi/entity/RegionEntity.java index a452a946..c9cd0b44 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/Region.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/RegionEntity.java @@ -7,7 +7,6 @@ import javax.persistence.Column; import javax.persistence.Entity; -import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @@ -19,9 +18,9 @@ @Data @NoArgsConstructor -@Entity(name = "regions") +@Entity(name = "region_entity") @Table(name = "regions") -public class Region implements Serializable { +public class RegionEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -33,9 +32,9 @@ public class Region implements Serializable { @OneToMany(mappedBy = "region") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set institutions = new HashSet<>(); + private Set institutions = new HashSet<>(); - public Region(String name) { + public RegionEntity(String name) { this.name = name; } diff --git a/src/main/java/com/softserveinc/dokazovi/entity/Role.java b/src/main/java/com/softserveinc/dokazovi/entity/RoleEntity.java similarity index 82% rename from src/main/java/com/softserveinc/dokazovi/entity/Role.java rename to src/main/java/com/softserveinc/dokazovi/entity/RoleEntity.java index a04bd539..00295cd7 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/Role.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/RoleEntity.java @@ -18,9 +18,9 @@ @Data @NoArgsConstructor -@Entity(name = "roles") +@Entity(name = "role_entity") @Table(name = "roles") -public class Role implements Serializable { +public class RoleEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -33,9 +33,9 @@ public class Role implements Serializable { @ManyToMany(mappedBy = "roles") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set users = new HashSet<>(); + private Set users = new HashSet<>(); - public Role(String name) { + public RoleEntity(String name) { this.name = name; } diff --git a/src/main/java/com/softserveinc/dokazovi/entity/Source.java b/src/main/java/com/softserveinc/dokazovi/entity/SourceEntity.java similarity index 75% rename from src/main/java/com/softserveinc/dokazovi/entity/Source.java rename to src/main/java/com/softserveinc/dokazovi/entity/SourceEntity.java index 58cc65db..3a017000 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/Source.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/SourceEntity.java @@ -5,7 +5,6 @@ import lombok.EqualsAndHashCode; import lombok.NoArgsConstructor; import lombok.ToString; -import org.hibernate.annotations.Type; import javax.persistence.Column; import javax.persistence.Entity; @@ -22,9 +21,9 @@ @Data @NoArgsConstructor -@Entity(name = "sources") +@Entity(name = "source_entity") @Table(name = "sources") -public class Source implements Serializable { +public class SourceEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -32,7 +31,6 @@ public class Source implements Serializable { private Integer id; @Enumerated(EnumType.STRING) - @Type(type = "com.softserveinc.dokazovi.entity.enumerations.PostgreSQLSourceTypeEnumType") private SourceType type; private String value; @@ -40,14 +38,14 @@ public class Source implements Serializable { @ManyToMany(mappedBy = "sources") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set posts = new HashSet<>(); + private Set posts = new HashSet<>(); @ManyToMany(mappedBy = "sources") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set users = new HashSet<>(); + private Set users = new HashSet<>(); - public Source(SourceType type, String value) { + public SourceEntity(SourceType type, String value) { this.type = type; this.value = value; } diff --git a/src/main/java/com/softserveinc/dokazovi/entity/Tag.java b/src/main/java/com/softserveinc/dokazovi/entity/TagEntity.java similarity index 82% rename from src/main/java/com/softserveinc/dokazovi/entity/Tag.java rename to src/main/java/com/softserveinc/dokazovi/entity/TagEntity.java index c483ee2b..dccc7a14 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/Tag.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/TagEntity.java @@ -18,9 +18,9 @@ @Data @NoArgsConstructor -@Entity(name = "tags") +@Entity(name = "tag_entity") @Table(name = "tags") -public class Tag implements Serializable { +public class TagEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -32,9 +32,9 @@ public class Tag implements Serializable { @ManyToMany(mappedBy = "tags") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set posts = new HashSet<>(); + private Set posts = new HashSet<>(); - public Tag(String tag) { + public TagEntity(String tag) { this.tag = tag; } diff --git a/src/main/java/com/softserveinc/dokazovi/entity/User.java b/src/main/java/com/softserveinc/dokazovi/entity/UserEntity.java similarity index 80% rename from src/main/java/com/softserveinc/dokazovi/entity/User.java rename to src/main/java/com/softserveinc/dokazovi/entity/UserEntity.java index 2b076ac8..539fb81d 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/User.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/UserEntity.java @@ -6,7 +6,6 @@ import lombok.NoArgsConstructor; import lombok.ToString; import org.hibernate.annotations.CreationTimestamp; -import org.hibernate.annotations.Type; import javax.persistence.CascadeType; import javax.persistence.Column; @@ -29,9 +28,9 @@ @Data @NoArgsConstructor -@Entity(name = "users") +@Entity(name = "user_entity") @Table(name = "users") -public class User implements Serializable { +public class UserEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -54,18 +53,17 @@ public class User implements Serializable { private String bio; @Enumerated(EnumType.STRING) - @Type(type = "com.softserveinc.dokazovi.entity.enumerations.PostgreSQLUserStatusEnumType") private UserStatus status; @OneToMany(mappedBy = "author") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set posts = new HashSet<>(); + private Set posts = new HashSet<>(); @OneToMany(mappedBy = "author") @EqualsAndHashCode.Exclude @ToString.Exclude - private Set charities = new HashSet<>(); + private Set charities = new HashSet<>(); @OneToMany( fetch = FetchType.EAGER, @@ -75,7 +73,7 @@ public class User implements Serializable { ) @EqualsAndHashCode.Exclude @ToString.Exclude - private Set institutions = new HashSet<>(); + private Set institutions = new HashSet<>(); @ManyToMany(fetch = FetchType.EAGER) @JoinTable( @@ -85,7 +83,7 @@ public class User implements Serializable { ) @EqualsAndHashCode.Exclude @ToString.Exclude - private Set roles = new HashSet<>(); + private Set roles = new HashSet<>(); @ManyToMany(fetch = FetchType.EAGER) @JoinTable( @@ -95,7 +93,7 @@ public class User implements Serializable { ) @EqualsAndHashCode.Exclude @ToString.Exclude - private Set directions = new HashSet<>(); + private Set directions = new HashSet<>(); @ManyToMany @JoinTable( @@ -105,12 +103,12 @@ public class User implements Serializable { ) @EqualsAndHashCode.Exclude @ToString.Exclude - private Set sources = new HashSet<>(); + private Set sources = new HashSet<>(); @CreationTimestamp private Timestamp createdAt; - public User(String firstName, String lastName, String email, String password, String qualification, + public UserEntity(String firstName, String lastName, String email, String password, String qualification, String phone, String bio, UserStatus status) { this.firstName = firstName; this.lastName = lastName; @@ -122,12 +120,12 @@ public User(String firstName, String lastName, String email, String password, St this.status = status; } - public void addRole(Role role) { + public void addRole(RoleEntity role) { roles.add(role); role.getUsers().add(this); } - public void removeRole(Role role) { + public void removeRole(RoleEntity role) { roles.remove(role); role.getUsers().remove(this); } diff --git a/src/main/java/com/softserveinc/dokazovi/entity/UserInstitution.java b/src/main/java/com/softserveinc/dokazovi/entity/UserInstitution.java deleted file mode 100644 index ddc05ba4..00000000 --- a/src/main/java/com/softserveinc/dokazovi/entity/UserInstitution.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.softserveinc.dokazovi.entity; - -import com.softserveinc.dokazovi.entity.embeddables.UserInstitutionId; -import lombok.Data; -import lombok.NoArgsConstructor; -import org.hibernate.annotations.ColumnDefault; - -import javax.persistence.EmbeddedId; -import javax.persistence.Entity; -import javax.persistence.JoinColumn; -import javax.persistence.ManyToOne; -import javax.persistence.MapsId; -import javax.persistence.Table; -import java.io.Serializable; - -@Data -@NoArgsConstructor -@Entity(name = "users_institutions") -@Table(name = "users_institutions") -public class UserInstitution implements Serializable { - - @EmbeddedId - private UserInstitutionId id = new UserInstitutionId(); - - @ManyToOne - @MapsId("userId") - @JoinColumn(name="user_id", insertable = false, updatable = false) - private User user; - - @ManyToOne - @MapsId("institutionId") - @JoinColumn(name="institution_id", insertable = false, updatable = false) - private Institution institution; - - @ColumnDefault("false") - private boolean isPrimary; - - public UserInstitution(User user, Institution institution, boolean isPrimary) { - this.user = user; - this.institution = institution; - this.isPrimary = isPrimary; - } - -} diff --git a/src/main/java/com/softserveinc/dokazovi/entity/UserInstitutionEntity.java b/src/main/java/com/softserveinc/dokazovi/entity/UserInstitutionEntity.java new file mode 100644 index 00000000..badeb401 --- /dev/null +++ b/src/main/java/com/softserveinc/dokazovi/entity/UserInstitutionEntity.java @@ -0,0 +1,44 @@ +package com.softserveinc.dokazovi.entity; + +import com.softserveinc.dokazovi.entity.embeddables.UserInstitutionId; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.ColumnDefault; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.MapsId; +import javax.persistence.Table; +import java.io.Serializable; + +@Data +@NoArgsConstructor +@Entity(name = "user_institution_entity") +@Table(name = "users_institutions") +public class UserInstitutionEntity implements Serializable { + + @EmbeddedId + private UserInstitutionId id = new UserInstitutionId(); + + @ManyToOne + @MapsId("userId") + @JoinColumn(name = "user_id", insertable = false, updatable = false) + private UserEntity user; + + @ManyToOne + @MapsId("institutionId") + @JoinColumn(name = "institution_id", insertable = false, updatable = false) + private InstitutionEntity institution; + + @ColumnDefault("false") + private boolean isPrimary; + + public UserInstitutionEntity(UserEntity user, InstitutionEntity institution, boolean isPrimary) { + this.user = user; + this.institution = institution; + this.isPrimary = isPrimary; + } + +} diff --git a/src/main/java/com/softserveinc/dokazovi/entity/embeddables/UserInstitutionId.java b/src/main/java/com/softserveinc/dokazovi/entity/embeddables/UserInstitutionId.java index d6cc903b..5b47644d 100644 --- a/src/main/java/com/softserveinc/dokazovi/entity/embeddables/UserInstitutionId.java +++ b/src/main/java/com/softserveinc/dokazovi/entity/embeddables/UserInstitutionId.java @@ -9,8 +9,8 @@ @Embeddable public class UserInstitutionId implements Serializable { - private Integer institutionId; + private Integer institutionId; - private Integer userId; + private Integer userId; } diff --git a/src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLPostStatusEnumType.java b/src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLPostStatusEnumType.java deleted file mode 100644 index f37b6755..00000000 --- a/src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLPostStatusEnumType.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.softserveinc.dokazovi.entity.enumerations; - -import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.type.EnumType; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; - -public class PostgreSQLPostStatusEnumType extends EnumType { - - @Override - public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) - throws HibernateException, SQLException { - if(value == null) { - st.setNull(index, Types.OTHER); - } - else { - st.setObject( - index, - value.toString().toLowerCase(), - Types.OTHER - ); - } - } - - @Override - public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) - throws SQLException { - String value = rs.getString(names[0]); - if (value == null) { - return null; - } else { - return PostStatus.valueOf(value.toUpperCase()); - } - } -} diff --git a/src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLSourceTypeEnumType.java b/src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLSourceTypeEnumType.java deleted file mode 100644 index b06b5195..00000000 --- a/src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLSourceTypeEnumType.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.softserveinc.dokazovi.entity.enumerations; - -import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.type.EnumType; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; - -public class PostgreSQLSourceTypeEnumType extends EnumType { - - @Override - public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) - throws HibernateException, SQLException { - if(value == null) { - st.setNull(index, Types.OTHER); - } - else { - st.setObject( - index, - value.toString().toLowerCase(), - Types.OTHER - ); - } - } - - @Override - public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) - throws SQLException { - String value = rs.getString(names[0]); - if (value == null) { - return null; - } else { - return SourceType.valueOf(value.toUpperCase()); - } - } -} diff --git a/src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLUserStatusEnumType.java b/src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLUserStatusEnumType.java deleted file mode 100644 index 5e159255..00000000 --- a/src/main/java/com/softserveinc/dokazovi/entity/enumerations/PostgreSQLUserStatusEnumType.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.softserveinc.dokazovi.entity.enumerations; - -import org.hibernate.HibernateException; -import org.hibernate.engine.spi.SharedSessionContractImplementor; -import org.hibernate.type.EnumType; - -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.sql.Types; - -public class PostgreSQLUserStatusEnumType extends EnumType { - - @Override - public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) - throws HibernateException, SQLException { - if(value == null) { - st.setNull(index, Types.OTHER); - } - else { - st.setObject( - index, - value.toString().toLowerCase(), - Types.OTHER - ); - } - } - - @Override - public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) - throws SQLException { - String value = rs.getString(names[0]); - if (value == null) { - return null; - } else { - return UserStatus.valueOf(value.toUpperCase()); - } - } -} diff --git a/src/main/java/com/softserveinc/dokazovi/repositories/DirectionRepository.java b/src/main/java/com/softserveinc/dokazovi/repositories/DirectionRepository.java index f009dac9..4a2ab269 100644 --- a/src/main/java/com/softserveinc/dokazovi/repositories/DirectionRepository.java +++ b/src/main/java/com/softserveinc/dokazovi/repositories/DirectionRepository.java @@ -1,9 +1,9 @@ package com.softserveinc.dokazovi.repositories; -import com.softserveinc.dokazovi.entity.Direction; +import com.softserveinc.dokazovi.entity.DirectionEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface DirectionRepository extends JpaRepository { +public interface DirectionRepository extends JpaRepository { } diff --git a/src/main/java/com/softserveinc/dokazovi/repositories/InstitutionRepository.java b/src/main/java/com/softserveinc/dokazovi/repositories/InstitutionRepository.java index b86ed4a6..3ae31abe 100644 --- a/src/main/java/com/softserveinc/dokazovi/repositories/InstitutionRepository.java +++ b/src/main/java/com/softserveinc/dokazovi/repositories/InstitutionRepository.java @@ -1,9 +1,9 @@ package com.softserveinc.dokazovi.repositories; -import com.softserveinc.dokazovi.entity.Institution; +import com.softserveinc.dokazovi.entity.InstitutionEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface InstitutionRepository extends JpaRepository { +public interface InstitutionRepository extends JpaRepository { } diff --git a/src/main/java/com/softserveinc/dokazovi/repositories/PostRepository.java b/src/main/java/com/softserveinc/dokazovi/repositories/PostRepository.java index 6bcca6d3..67ee7472 100644 --- a/src/main/java/com/softserveinc/dokazovi/repositories/PostRepository.java +++ b/src/main/java/com/softserveinc/dokazovi/repositories/PostRepository.java @@ -1,10 +1,10 @@ package com.softserveinc.dokazovi.repositories; -import com.softserveinc.dokazovi.entity.Post; +import com.softserveinc.dokazovi.entity.PostEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface PostRepository extends JpaRepository { +public interface PostRepository extends JpaRepository { } diff --git a/src/main/java/com/softserveinc/dokazovi/repositories/PostTypeRepository.java b/src/main/java/com/softserveinc/dokazovi/repositories/PostTypeRepository.java index 9e93a5da..cddd5733 100644 --- a/src/main/java/com/softserveinc/dokazovi/repositories/PostTypeRepository.java +++ b/src/main/java/com/softserveinc/dokazovi/repositories/PostTypeRepository.java @@ -1,9 +1,9 @@ package com.softserveinc.dokazovi.repositories; -import com.softserveinc.dokazovi.entity.PostType; +import com.softserveinc.dokazovi.entity.PostTypeEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface PostTypeRepository extends JpaRepository { +public interface PostTypeRepository extends JpaRepository { } diff --git a/src/main/java/com/softserveinc/dokazovi/repositories/RegionRepository.java b/src/main/java/com/softserveinc/dokazovi/repositories/RegionRepository.java index 40cb3315..08f2a5b0 100644 --- a/src/main/java/com/softserveinc/dokazovi/repositories/RegionRepository.java +++ b/src/main/java/com/softserveinc/dokazovi/repositories/RegionRepository.java @@ -1,9 +1,9 @@ package com.softserveinc.dokazovi.repositories; -import com.softserveinc.dokazovi.entity.Region; +import com.softserveinc.dokazovi.entity.RegionEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface RegionRepository extends JpaRepository { +public interface RegionRepository extends JpaRepository { } diff --git a/src/main/java/com/softserveinc/dokazovi/repositories/RoleRepository.java b/src/main/java/com/softserveinc/dokazovi/repositories/RoleRepository.java index dbc0a504..f92a9136 100644 --- a/src/main/java/com/softserveinc/dokazovi/repositories/RoleRepository.java +++ b/src/main/java/com/softserveinc/dokazovi/repositories/RoleRepository.java @@ -1,9 +1,9 @@ package com.softserveinc.dokazovi.repositories; -import com.softserveinc.dokazovi.entity.Role; +import com.softserveinc.dokazovi.entity.RoleEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface RoleRepository extends JpaRepository { +public interface RoleRepository extends JpaRepository { } diff --git a/src/main/java/com/softserveinc/dokazovi/repositories/SourceRepository.java b/src/main/java/com/softserveinc/dokazovi/repositories/SourceRepository.java index 4cdf89e3..dfc6e4e4 100644 --- a/src/main/java/com/softserveinc/dokazovi/repositories/SourceRepository.java +++ b/src/main/java/com/softserveinc/dokazovi/repositories/SourceRepository.java @@ -1,9 +1,9 @@ package com.softserveinc.dokazovi.repositories; -import com.softserveinc.dokazovi.entity.Source; +import com.softserveinc.dokazovi.entity.SourceEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface SourceRepository extends JpaRepository { +public interface SourceRepository extends JpaRepository { } diff --git a/src/main/java/com/softserveinc/dokazovi/repositories/TagRepository.java b/src/main/java/com/softserveinc/dokazovi/repositories/TagRepository.java index 674fe083..67ce7001 100644 --- a/src/main/java/com/softserveinc/dokazovi/repositories/TagRepository.java +++ b/src/main/java/com/softserveinc/dokazovi/repositories/TagRepository.java @@ -1,9 +1,9 @@ package com.softserveinc.dokazovi.repositories; -import com.softserveinc.dokazovi.entity.Tag; +import com.softserveinc.dokazovi.entity.TagEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface TagRepository extends JpaRepository { +public interface TagRepository extends JpaRepository { } diff --git a/src/main/java/com/softserveinc/dokazovi/repositories/UserRepository.java b/src/main/java/com/softserveinc/dokazovi/repositories/UserRepository.java index 20d7fb71..5c04d812 100644 --- a/src/main/java/com/softserveinc/dokazovi/repositories/UserRepository.java +++ b/src/main/java/com/softserveinc/dokazovi/repositories/UserRepository.java @@ -1,9 +1,9 @@ package com.softserveinc.dokazovi.repositories; -import com.softserveinc.dokazovi.entity.User; +import com.softserveinc.dokazovi.entity.UserEntity; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository -public interface UserRepository extends JpaRepository { +public interface UserRepository extends JpaRepository { } diff --git a/src/main/java/com/softserveinc/dokazovi/service/PostService.java b/src/main/java/com/softserveinc/dokazovi/service/PostService.java index 9d3cd3b0..c8b3814c 100644 --- a/src/main/java/com/softserveinc/dokazovi/service/PostService.java +++ b/src/main/java/com/softserveinc/dokazovi/service/PostService.java @@ -1,10 +1,10 @@ package com.softserveinc.dokazovi.service; -import com.softserveinc.dokazovi.entity.Post; +import com.softserveinc.dokazovi.entity.PostEntity; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; public interface PostService { - Page findAll(Pageable pageable); + Page findAll(Pageable pageable); } diff --git a/src/main/java/com/softserveinc/dokazovi/service/impl/PostServiceImpl.java b/src/main/java/com/softserveinc/dokazovi/service/impl/PostServiceImpl.java index 26aebcf0..e786d36e 100644 --- a/src/main/java/com/softserveinc/dokazovi/service/impl/PostServiceImpl.java +++ b/src/main/java/com/softserveinc/dokazovi/service/impl/PostServiceImpl.java @@ -1,6 +1,6 @@ package com.softserveinc.dokazovi.service.impl; -import com.softserveinc.dokazovi.entity.Post; +import com.softserveinc.dokazovi.entity.PostEntity; import com.softserveinc.dokazovi.repositories.PostRepository; import com.softserveinc.dokazovi.service.PostService; import org.springframework.data.domain.Page; @@ -17,7 +17,7 @@ public PostServiceImpl(PostRepository postRepository) { } @Override - public Page findAll(Pageable pageable) { + public Page findAll(Pageable pageable) { return postRepository.findAll(pageable); } } diff --git a/src/main/resources/db/migration/V1__init_db.sql b/src/main/resources/db/migration/V1__init_db.sql index 9e014fc2..95216e2e 100644 --- a/src/main/resources/db/migration/V1__init_db.sql +++ b/src/main/resources/db/migration/V1__init_db.sql @@ -13,37 +13,11 @@ SET default_tablespace = ''; SET default_table_access_method = heap; -CREATE TYPE public.post_status AS ENUM ( - 'draft', - 'moderation_first_sign', - 'moderation_second_sign', - 'published', - 'archived' -); - -ALTER TYPE public.post_status OWNER TO dokazovi; - -CREATE TYPE public.source_type AS ENUM ( - 'profile_image', - 'post_image', - 'post_video' -); - -ALTER TYPE public.source_type OWNER TO dokazovi; - -CREATE TYPE public.user_status AS ENUM ( - 'new', - 'active', - 'deleted' -); - -ALTER TYPE public.user_status OWNER TO dokazovi; - CREATE TABLE public.users ( "user_id" SERIAL PRIMARY KEY, "email" varchar, "password" varchar, - "status" public.user_status, + "status" varchar, "first_name" varchar, "last_name" varchar, "qualification" varchar, @@ -113,7 +87,7 @@ CREATE TABLE public.posts ( "type_id" int, "title" varchar, "content" text, - "status" public.post_status, + "status" varchar, "important" boolean, "tags" varchar, "created_at" TIMESTAMP DEFAULT (now()), @@ -134,7 +108,7 @@ ALTER TABLE public.charities OWNER TO dokazovi; CREATE TABLE public.sources ( "source_id" SERIAL PRIMARY KEY, - "type" public.source_type, + "type" varchar, "value" varchar ); diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties new file mode 100644 index 00000000..ee4a49d4 --- /dev/null +++ b/src/test/resources/application.properties @@ -0,0 +1,20 @@ +info.build.version=${BUILD_VERSION:0.0.0} +#------------------------- +# Database H2 +#------------------------- +spring.datasource.driver-class-name=org.h2.Driver +spring.datasource.url=${TEST_DATA_SOURCE:jdbc:h2:mem:db} +spring.datasource.username=${TEST_DATASOURCE_USER:sa} +spring.datasource.password=${TEST_DATASOURCE_PASSWORD:} +spring.h2.console.enabled=true + +#------------------------- +# JPA +#------------------------- +spring.jpa.hibernate.ddl-auto=create +spring.jpa.show-sql=true + +#------------------------- +# FlyWay +#------------------------- +spring.flyway.enabled=false \ No newline at end of file