Addressing PR feedback

This commit is contained in:
technoddy 2023-05-25 23:40:16 -04:00
parent ebbd80e6c7
commit 01e8fa41fd
5 changed files with 34 additions and 86 deletions

View File

@ -8,10 +8,10 @@ import java.time.Instant;
import java.util.Date; import java.util.Date;
@Entity @Entity
@Table(name="ACCOUNTS") @Table(name = "ACCOUNTS")
public class Account { public class Account {
@Id @Id
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator = "accounts_seq") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accounts_seq")
@SequenceGenerator(name = "accounts_seq", sequenceName = "accounts_seq", allocationSize = 1) @SequenceGenerator(name = "accounts_seq", sequenceName = "accounts_seq", allocationSize = 1)
@Column(name = "user_id") @Column(name = "user_id")
private int userId; private int userId;
@ -25,14 +25,6 @@ public class Account {
@JoinColumn(name = "permissions_id") @JoinColumn(name = "permissions_id")
private Permission permission; private Permission permission;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUsername() { public String getUsername() {
return username; return username;
} }
@ -41,18 +33,10 @@ public class Account {
this.username = username; this.username = username;
} }
public String getPassword() {
return password;
}
public void setPassword(String password) { public void setPassword(String password) {
this.password = password; this.password = password;
} }
public String getEmail() {
return email;
}
public void setEmail(String email) { public void setEmail(String email) {
this.email = email; this.email = email;
} }
@ -65,10 +49,6 @@ public class Account {
this.createdOn = createdOn; this.createdOn = createdOn;
} }
public Timestamp getLastLogin() {
return lastLogin;
}
public void setLastLogin(Timestamp lastLogin) { public void setLastLogin(Timestamp lastLogin) {
this.lastLogin = lastLogin; this.lastLogin = lastLogin;
} }
@ -83,14 +63,6 @@ public class Account {
@Override @Override
public String toString() { public String toString() {
return "Account{" + return "Account{" + "userId=" + userId + ", username='" + username + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", createdOn=" + createdOn + ", lastLogin=" + lastLogin + ", permission=" + permission + '}';
"userId=" + userId +
", username='" + username + '\'' +
", password='" + password + '\'' +
", email='" + email + '\'' +
", createdOn=" + createdOn +
", lastLogin=" + lastLogin +
", permission=" + permission +
'}';
} }
} }

View File

@ -3,37 +3,22 @@ package com.baeldung.countrows.entity;
import javax.persistence.*; import javax.persistence.*;
@Entity @Entity
@Table(name="PERMISSIONS") @Table(name = "PERMISSIONS")
public class Permission { public class Permission {
@Id @Id
@GeneratedValue(strategy= GenerationType.SEQUENCE, generator = "permissions_id_sq") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "permissions_id_sq")
@SequenceGenerator(name = "permissions_id_sq", sequenceName = "permissions_id_sq", allocationSize = 1) @SequenceGenerator(name = "permissions_id_sq", sequenceName = "permissions_id_sq", allocationSize = 1)
private int id; private int id;
private String type; private String type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) { public void setType(String type) {
this.type = type; this.type = type;
} }
@Override @Override
public String toString() { public String toString() {
return "Permission{" + return "Permission{" + "id=" + id + ", type='" + type + '\'' + '}';
"id=" + id +
", type='" + type + '\'' +
'}';
} }
} }

View File

@ -2,6 +2,7 @@ package com.baeldung.countrows.repository;
import com.baeldung.countrows.entity.Account; import com.baeldung.countrows.entity.Account;
import com.baeldung.countrows.entity.Permission; import com.baeldung.countrows.entity.Permission;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query; import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param; import org.springframework.data.repository.query.Param;

View File

@ -11,8 +11,10 @@ import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
import javax.persistence.Query; import javax.persistence.Query;
import javax.persistence.criteria.*; import javax.persistence.criteria.*;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import com.baeldung.countrows.entity.Account; import com.baeldung.countrows.entity.Account;
import com.baeldung.countrows.entity.Permission; import com.baeldung.countrows.entity.Permission;
import com.baeldung.countrows.repository.AccountRepository; import com.baeldung.countrows.repository.AccountRepository;
@ -29,19 +31,18 @@ public class AccountStatsLogic {
@Autowired @Autowired
private PermissionRepository permissionRepository; private PermissionRepository permissionRepository;
public long getAccountCount(){ public long getAccountCount() {
return accountRepository.count(); return accountRepository.count();
} }
public long getAccountCountByUsername(String username){ public long getAccountCountByUsername(String username) {
return accountRepository.countByUsername(username); return accountRepository.countByUsername(username);
} }
public long getAccountCountByPermission(Permission permission){ public long getAccountCountByPermission(Permission permission) {
return accountRepository.countByPermission(permission); return accountRepository.countByPermission(permission);
} }
public long getAccountCountByPermissionAndCreatedOn(Permission permission, Date date) throws ParseException { public long getAccountCountByPermissionAndCreatedOn(Permission permission, Date date) throws ParseException {
return accountRepository.countByPermissionAndCreatedOnGreaterThan(permission, new Timestamp(date.getTime())); return accountRepository.countByPermissionAndCreatedOnGreaterThan(permission, new Timestamp(date.getTime()));
} }
@ -53,11 +54,11 @@ public class AccountStatsLogic {
Root<Account> accountRoot = criteriaQuery.from(Account.class); Root<Account> accountRoot = criteriaQuery.from(Account.class);
// select query // select query
criteriaQuery criteriaQuery.select(builder.count(accountRoot));
.select(builder.count(accountRoot));
// execute and get the result // execute and get the result
return entityManager.createQuery(criteriaQuery).getSingleResult(); return entityManager.createQuery(criteriaQuery)
.getSingleResult();
} }
public long getAccountsByPermissionUsingCQ(Permission permission) throws ParseException { public long getAccountsByPermissionUsingCQ(Permission permission) throws ParseException {
@ -68,11 +69,11 @@ public class AccountStatsLogic {
List<Predicate> predicateList = new ArrayList<>(); // list of predicates that will go in where clause List<Predicate> predicateList = new ArrayList<>(); // list of predicates that will go in where clause
predicateList.add(builder.equal(accountRoot.get("permission"), permission)); predicateList.add(builder.equal(accountRoot.get("permission"), permission));
criteriaQuery criteriaQuery.select(builder.count(accountRoot))
.select(builder.count(accountRoot))
.where(builder.and(predicateList.toArray(new Predicate[0]))); .where(builder.and(predicateList.toArray(new Predicate[0])));
return entityManager.createQuery(criteriaQuery).getSingleResult(); return entityManager.createQuery(criteriaQuery)
.getSingleResult();
} }
public long getAccountsByPermissionAndCreateOnUsingCQ(Permission permission, Date date) throws ParseException { public long getAccountsByPermissionAndCreateOnUsingCQ(Permission permission, Date date) throws ParseException {
@ -87,12 +88,12 @@ public class AccountStatsLogic {
predicateList.add(builder.greaterThan(accountRoot.get("createdOn"), new Timestamp(date.getTime()))); predicateList.add(builder.greaterThan(accountRoot.get("createdOn"), new Timestamp(date.getTime())));
// select query // select query
criteriaQuery criteriaQuery.select(builder.count(accountRoot))
.select(builder.count(accountRoot))
.where(builder.and(predicateList.toArray(new Predicate[0]))); .where(builder.and(predicateList.toArray(new Predicate[0])));
// execute and get the result // execute and get the result
return entityManager.createQuery(criteriaQuery).getSingleResult(); return entityManager.createQuery(criteriaQuery)
.getSingleResult();
} }
public long getAccountsUsingJPQL() throws ParseException { public long getAccountsUsingJPQL() throws ParseException {
@ -112,12 +113,4 @@ public class AccountStatsLogic {
query.setParameter(2, new Timestamp(date.getTime())); query.setParameter(2, new Timestamp(date.getTime()));
return (long) query.getSingleResult(); return (long) query.getSingleResult();
} }
private static Date getDate() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = dateFormat.parse("2023-04-29");
System.out.println("parseDate: "+parsedDate);
return parsedDate;
}
} }

View File

@ -35,15 +35,15 @@ class AccountStatsUnitTest {
private AccountStatsLogic accountStatsLogic; private AccountStatsLogic accountStatsLogic;
@AfterEach @AfterEach
public void afterEach(){ public void afterEach() {
accountRepository.deleteAll(); accountRepository.deleteAll();
permissionRepository.deleteAll(); permissionRepository.deleteAll();
} }
@Test @Test
public void givenAccountInTable_whenPerformCount_returnsAppropriateCount(){ public void givenAccountInTable_whenPerformCount_returnsAppropriateCount() {
savePermissions(); savePermissions();
Account account = saveAccount(); saveAccount();
assertThat(accountStatsLogic.getAccountCount()).isEqualTo(1); assertThat(accountStatsLogic.getAccountCount()).isEqualTo(1);
} }
@ -66,7 +66,7 @@ class AccountStatsUnitTest {
@Test @Test
public void givenAccountInTable_whenPerformCountUsingCQ_returnsAppropriateCount() throws ParseException { public void givenAccountInTable_whenPerformCountUsingCQ_returnsAppropriateCount() throws ParseException {
savePermissions(); savePermissions();
Account account = saveAccount(); saveAccount();
long count = accountStatsLogic.getAccountsUsingCQ(); long count = accountStatsLogic.getAccountsUsingCQ();
assertThat(count).isEqualTo(1); assertThat(count).isEqualTo(1);
} }
@ -90,10 +90,11 @@ class AccountStatsUnitTest {
@Test @Test
public void givenAccountInTable_whenPerformCountUsingJPQL_returnsAppropriateCount() throws ParseException { public void givenAccountInTable_whenPerformCountUsingJPQL_returnsAppropriateCount() throws ParseException {
savePermissions(); savePermissions();
Account account = saveAccount(); saveAccount();
long count = accountStatsLogic.getAccountsUsingJPQL(); long count = accountStatsLogic.getAccountsUsingJPQL();
assertThat(count).isEqualTo(1); assertThat(count).isEqualTo(1);
} }
@Test @Test
public void givenAccountInTable_whenPerformCountByPermissionUsingJPQL_returnsAppropriateCount() throws ParseException { public void givenAccountInTable_whenPerformCountByPermissionUsingJPQL_returnsAppropriateCount() throws ParseException {
savePermissions(); savePermissions();
@ -101,6 +102,7 @@ class AccountStatsUnitTest {
long count = accountStatsLogic.getAccountsByPermissionUsingJPQL(account.getPermission()); long count = accountStatsLogic.getAccountsByPermissionUsingJPQL(account.getPermission());
assertThat(count).isEqualTo(1); assertThat(count).isEqualTo(1);
} }
@Test @Test
public void givenAccountInTable_whenPerformCountByPermissionAndCreatedOnUsingJPQL_returnsAppropriateCount() throws ParseException { public void givenAccountInTable_whenPerformCountByPermissionAndCreatedOnUsingJPQL_returnsAppropriateCount() throws ParseException {
savePermissions(); savePermissions();
@ -109,11 +111,11 @@ class AccountStatsUnitTest {
assertThat(count).isEqualTo(1); assertThat(count).isEqualTo(1);
} }
private Account saveAccount(){ private Account saveAccount() {
return accountRepository.save(getAccount()); return accountRepository.save(getAccount());
} }
private void savePermissions(){ private void savePermissions() {
Permission editor = new Permission(); Permission editor = new Permission();
editor.setType("editor"); editor.setType("editor");
permissionRepository.save(editor); permissionRepository.save(editor);
@ -123,18 +125,13 @@ class AccountStatsUnitTest {
permissionRepository.save(admin); permissionRepository.save(admin);
} }
private static Date getDate() throws ParseException {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date parsedDate = dateFormat.parse("2023-04-29");
return parsedDate;
}
private Account getAccount() { private Account getAccount() {
Permission permission = permissionRepository.findByType("admin"); Permission permission = permissionRepository.findByType("admin");
Account account = new Account(); Account account = new Account();
String seed = UUID.randomUUID().toString(); String seed = UUID.randomUUID()
account.setUsername("username_"+seed); .toString();
account.setEmail("username_"+seed+"@gmail.com"); account.setUsername("username_" + seed);
account.setEmail("username_" + seed + "@gmail.com");
account.setPermission(permission); account.setPermission(permission);
account.setPassword("password_q1234"); account.setPassword("password_q1234");
account.setCreatedOn(Timestamp.from(Instant.now())); account.setCreatedOn(Timestamp.from(Instant.now()));