📍Use lombok, reformat, and optimize the code (#1560)
* Use lombok, reformat, and optimize the code * Fix merge conflicts and some sonar issues Co-authored-by: va1m <va1m@email.com>
This commit is contained in:
		@@ -27,8 +27,7 @@ import com.iluwatar.cqrs.commandes.CommandServiceImpl;
 | 
			
		||||
import com.iluwatar.cqrs.constants.AppConstants;
 | 
			
		||||
import com.iluwatar.cqrs.queries.QueryServiceImpl;
 | 
			
		||||
import com.iluwatar.cqrs.util.HibernateUtil;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * CQRS : Command Query Responsibility Segregation. A pattern used to separate query services from
 | 
			
		||||
@@ -41,8 +40,8 @@ import org.slf4j.LoggerFactory;
 | 
			
		||||
 * data model to persist(insert,update,delete) objects to a database. And a query side that uses
 | 
			
		||||
 * native queries to get data from the database and return objects as DTOs (Data transfer Objects).
 | 
			
		||||
 */
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class App {
 | 
			
		||||
  private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Program entry point.
 | 
			
		||||
 
 | 
			
		||||
@@ -27,10 +27,16 @@ import javax.persistence.Entity;
 | 
			
		||||
import javax.persistence.GeneratedValue;
 | 
			
		||||
import javax.persistence.GenerationType;
 | 
			
		||||
import javax.persistence.Id;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import lombok.Setter;
 | 
			
		||||
import lombok.ToString;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This is an Author entity. It is used by Hibernate for persistence.
 | 
			
		||||
 */
 | 
			
		||||
@ToString
 | 
			
		||||
@Getter
 | 
			
		||||
@Setter
 | 
			
		||||
@Entity
 | 
			
		||||
public class Author {
 | 
			
		||||
  @Id
 | 
			
		||||
@@ -56,41 +62,4 @@ public class Author {
 | 
			
		||||
  protected Author() {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public long getId() {
 | 
			
		||||
    return id;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setId(long id) {
 | 
			
		||||
    this.id = id;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getUsername() {
 | 
			
		||||
    return username;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setUsername(String username) {
 | 
			
		||||
    this.username = username;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getName() {
 | 
			
		||||
    return name;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setName(String name) {
 | 
			
		||||
    this.name = name;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getEmail() {
 | 
			
		||||
    return email;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setEmail(String email) {
 | 
			
		||||
    this.email = email;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String toString() {
 | 
			
		||||
    return "Author [name=" + name + ", email=" + email + "]";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -28,11 +28,17 @@ import javax.persistence.GeneratedValue;
 | 
			
		||||
import javax.persistence.GenerationType;
 | 
			
		||||
import javax.persistence.Id;
 | 
			
		||||
import javax.persistence.ManyToOne;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import lombok.Setter;
 | 
			
		||||
import lombok.ToString;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This is a Book entity. It is used by Hibernate for persistence. Many books can be written by one
 | 
			
		||||
 * {@link Author}
 | 
			
		||||
 */
 | 
			
		||||
@ToString
 | 
			
		||||
@Setter
 | 
			
		||||
@Getter
 | 
			
		||||
@Entity
 | 
			
		||||
public class Book {
 | 
			
		||||
  @Id
 | 
			
		||||
@@ -59,41 +65,4 @@ public class Book {
 | 
			
		||||
  protected Book() {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public long getId() {
 | 
			
		||||
    return id;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setId(long id) {
 | 
			
		||||
    this.id = id;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getTitle() {
 | 
			
		||||
    return title;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setTitle(String title) {
 | 
			
		||||
    this.title = title;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public double getPrice() {
 | 
			
		||||
    return price;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setPrice(double price) {
 | 
			
		||||
    this.price = price;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public Author getAuthor() {
 | 
			
		||||
    return author;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public void setAuthor(Author author) {
 | 
			
		||||
    this.author = author;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String toString() {
 | 
			
		||||
    return "Book [title=" + title + ", price=" + price + ", author=" + author + "]";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -23,67 +23,24 @@
 | 
			
		||||
 | 
			
		||||
package com.iluwatar.cqrs.dto;
 | 
			
		||||
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import lombok.AllArgsConstructor;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
import lombok.ToString;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This is a DTO (Data Transfer Object) author, contains only useful information to be returned.
 | 
			
		||||
 */
 | 
			
		||||
@ToString
 | 
			
		||||
@EqualsAndHashCode
 | 
			
		||||
@Getter
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
public class Author {
 | 
			
		||||
 | 
			
		||||
  private String name;
 | 
			
		||||
  private String email;
 | 
			
		||||
  private String username;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Constructor.
 | 
			
		||||
   *
 | 
			
		||||
   * @param name     name of the author
 | 
			
		||||
   * @param email    email of the author
 | 
			
		||||
   * @param username username of the author
 | 
			
		||||
   */
 | 
			
		||||
  public Author(String name, String email, String username) {
 | 
			
		||||
    this.name = name;
 | 
			
		||||
    this.email = email;
 | 
			
		||||
    this.username = username;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public Author() {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getName() {
 | 
			
		||||
    return name;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getEmail() {
 | 
			
		||||
    return email;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getUsername() {
 | 
			
		||||
    return username;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String toString() {
 | 
			
		||||
    return "AuthorDTO [name=" + name + ", email=" + email + ", username=" + username + "]";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public int hashCode() {
 | 
			
		||||
    return Objects.hash(username, name, email);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public boolean equals(Object obj) {
 | 
			
		||||
    if (this == obj) {
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    if (!(obj instanceof Author)) {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
    var other = (Author) obj;
 | 
			
		||||
    return username.equals(other.getUsername()) && email.equals(other.getEmail()) && name
 | 
			
		||||
        .equals(other.getName());
 | 
			
		||||
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -23,58 +23,23 @@
 | 
			
		||||
 | 
			
		||||
package com.iluwatar.cqrs.dto;
 | 
			
		||||
 | 
			
		||||
import java.util.Objects;
 | 
			
		||||
import lombok.AllArgsConstructor;
 | 
			
		||||
import lombok.EqualsAndHashCode;
 | 
			
		||||
import lombok.Getter;
 | 
			
		||||
import lombok.NoArgsConstructor;
 | 
			
		||||
import lombok.ToString;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This is a DTO (Data Transfer Object) book, contains only useful information to be returned.
 | 
			
		||||
 */
 | 
			
		||||
@ToString
 | 
			
		||||
@EqualsAndHashCode
 | 
			
		||||
@Getter
 | 
			
		||||
@AllArgsConstructor
 | 
			
		||||
@NoArgsConstructor
 | 
			
		||||
public class Book {
 | 
			
		||||
 | 
			
		||||
  private String title;
 | 
			
		||||
  private double price;
 | 
			
		||||
 | 
			
		||||
  /**
 | 
			
		||||
   * Constructor.
 | 
			
		||||
   *
 | 
			
		||||
   * @param title title of the book
 | 
			
		||||
   * @param price price of the book
 | 
			
		||||
   */
 | 
			
		||||
  public Book(String title, double price) {
 | 
			
		||||
    this.title = title;
 | 
			
		||||
    this.price = price;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public Book() {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public String getTitle() {
 | 
			
		||||
    return title;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  public double getPrice() {
 | 
			
		||||
    return price;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public String toString() {
 | 
			
		||||
    return "BookDTO [title=" + title + ", price=" + price + "]";
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public int hashCode() {
 | 
			
		||||
    return Objects.hash(title, price);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Override
 | 
			
		||||
  public boolean equals(Object obj) {
 | 
			
		||||
    if (this == obj) {
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    if (!(obj instanceof Book)) {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
    var book = (Book) obj;
 | 
			
		||||
    return title.equals(book.getTitle()) && price == book.getPrice();
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -23,20 +23,19 @@
 | 
			
		||||
 | 
			
		||||
package com.iluwatar.cqrs.util;
 | 
			
		||||
 | 
			
		||||
import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.hibernate.SessionFactory;
 | 
			
		||||
import org.hibernate.boot.MetadataSources;
 | 
			
		||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
 | 
			
		||||
import org.slf4j.Logger;
 | 
			
		||||
import org.slf4j.LoggerFactory;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * This class simply returns one instance of {@link SessionFactory} initialized when the application
 | 
			
		||||
 * is started.
 | 
			
		||||
 */
 | 
			
		||||
@Slf4j
 | 
			
		||||
public class HibernateUtil {
 | 
			
		||||
 | 
			
		||||
  private static final SessionFactory SESSIONFACTORY = buildSessionFactory();
 | 
			
		||||
  private static final Logger LOGGER = LoggerFactory.getLogger(HibernateUtil.class);
 | 
			
		||||
 | 
			
		||||
  private static SessionFactory buildSessionFactory() {
 | 
			
		||||
 | 
			
		||||
@@ -46,7 +45,7 @@ public class HibernateUtil {
 | 
			
		||||
      return new MetadataSources(registry).buildMetadata().buildSessionFactory();
 | 
			
		||||
    } catch (Exception ex) {
 | 
			
		||||
      StandardServiceRegistryBuilder.destroy(registry);
 | 
			
		||||
      LOGGER.error("Initial SessionFactory creation failed." + ex);
 | 
			
		||||
      LOGGER.error("Initial SessionFactory creation failed.", ex);
 | 
			
		||||
      throw new ExceptionInInitializerError(ex);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
@@ -38,12 +38,12 @@ import org.junit.jupiter.api.Test;
 | 
			
		||||
/**
 | 
			
		||||
 * Integration test of IQueryService and ICommandService with h2 data
 | 
			
		||||
 */
 | 
			
		||||
public class IntegrationTest {
 | 
			
		||||
class IntegrationTest {
 | 
			
		||||
 | 
			
		||||
  private static IQueryService queryService;
 | 
			
		||||
 | 
			
		||||
  @BeforeAll
 | 
			
		||||
  public static void initializeAndPopulateDatabase() {
 | 
			
		||||
  static void initializeAndPopulateDatabase() {
 | 
			
		||||
    var commandService = new CommandServiceImpl();
 | 
			
		||||
    queryService = new QueryServiceImpl();
 | 
			
		||||
 | 
			
		||||
@@ -67,7 +67,7 @@ public class IntegrationTest {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testGetAuthorByUsername() {
 | 
			
		||||
  void testGetAuthorByUsername() {
 | 
			
		||||
    var author = queryService.getAuthorByUsername("username1");
 | 
			
		||||
    assertEquals("username1", author.getUsername());
 | 
			
		||||
    assertEquals("name1", author.getName());
 | 
			
		||||
@@ -75,7 +75,7 @@ public class IntegrationTest {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testGetUpdatedAuthorByUsername() {
 | 
			
		||||
  void testGetUpdatedAuthorByUsername() {
 | 
			
		||||
    var author = queryService.getAuthorByUsername("new_username2");
 | 
			
		||||
    var expectedAuthor = new Author("new_name2", "new_email2", "new_username2");
 | 
			
		||||
    assertEquals(expectedAuthor, author);
 | 
			
		||||
@@ -83,14 +83,14 @@ public class IntegrationTest {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testGetBook() {
 | 
			
		||||
  void testGetBook() {
 | 
			
		||||
    var book = queryService.getBook("title1");
 | 
			
		||||
    assertEquals("title1", book.getTitle());
 | 
			
		||||
    assertEquals(10, book.getPrice(), 0.01);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testGetAuthorBooks() {
 | 
			
		||||
  void testGetAuthorBooks() {
 | 
			
		||||
    var books = queryService.getAuthorBooks("username1");
 | 
			
		||||
    assertEquals(2, books.size());
 | 
			
		||||
    assertTrue(books.contains(new Book("title1", 10)));
 | 
			
		||||
@@ -98,13 +98,13 @@ public class IntegrationTest {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testGetAuthorBooksCount() {
 | 
			
		||||
  void testGetAuthorBooksCount() {
 | 
			
		||||
    var bookCount = queryService.getAuthorBooksCount("username1");
 | 
			
		||||
    assertEquals(new BigInteger("2"), bookCount);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  @Test
 | 
			
		||||
  public void testGetAuthorsCount() {
 | 
			
		||||
  void testGetAuthorsCount() {
 | 
			
		||||
    var authorCount = queryService.getAuthorsCount();
 | 
			
		||||
    assertEquals(new BigInteger("2"), authorCount);
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user