implementation for bael-7284 (#15636)
* implementation * feedback --------- Co-authored-by: technoddy <mail.technoddy@gmail.com>
This commit is contained in:
parent
d658a3d2b8
commit
e20c10f018
@ -0,0 +1,69 @@
|
||||
package com.baeldung.findby;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
|
||||
@Entity
|
||||
@Table(name = "ACCOUNTS")
|
||||
public class Account {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "accounts_seq")
|
||||
@SequenceGenerator(name = "accounts_seq", sequenceName = "accounts_seq", allocationSize = 1)
|
||||
@Column(name = "user_id")
|
||||
private int userId;
|
||||
private String username;
|
||||
private String password;
|
||||
private String email;
|
||||
private Timestamp createdOn;
|
||||
private Timestamp lastLogin;
|
||||
|
||||
@OneToOne
|
||||
@JoinColumn(name = "permissions_id")
|
||||
private Permission permission;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public Timestamp getCreatedOn() {
|
||||
return createdOn;
|
||||
}
|
||||
|
||||
public void setCreatedOn(Timestamp createdOn) {
|
||||
this.createdOn = createdOn;
|
||||
}
|
||||
|
||||
public void setLastLogin(Timestamp lastLogin) {
|
||||
this.lastLogin = lastLogin;
|
||||
}
|
||||
|
||||
public Permission getPermission() {
|
||||
return permission;
|
||||
}
|
||||
|
||||
public void setPermission(Permission permission) {
|
||||
this.permission = permission;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Account{" + "userId=" + userId + ", username='" + username + '\'' + ", password='" + password + '\'' + ", email='" + email + '\'' + ", createdOn=" + createdOn + ", lastLogin=" + lastLogin + ", permission=" + permission + '}';
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package com.baeldung.findby;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class AccountApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(AccountApplication.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package com.baeldung.findby;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface AccountRepository extends JpaRepository<Account, Integer> {
|
||||
|
||||
Account findByEmail(String email);
|
||||
|
||||
Account findByUsernameAndEmail(String username, String email);
|
||||
|
||||
Account findByUsernameOrEmail(String username, String email);
|
||||
|
||||
List<Account> findByUsernameInOrEmailIn(List<String> usernames, List<String> emails);
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.findby;
|
||||
|
||||
import jakarta.persistence.*;
|
||||
|
||||
@Entity
|
||||
@Table(name = "PERMISSIONS")
|
||||
public class Permission {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "permissions_id_sq")
|
||||
@SequenceGenerator(name = "permissions_id_sq", sequenceName = "permissions_id_sq", allocationSize = 1)
|
||||
private int id;
|
||||
|
||||
private String type;
|
||||
|
||||
public void setType(String type) {
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Permission{" + "id=" + id + ", type='" + type + '\'' + '}';
|
||||
}
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.baeldung.findby;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
@Repository
|
||||
public interface PermissionRepository extends JpaRepository<Permission, Integer> {
|
||||
Permission findByType(String type);
|
||||
}
|
@ -0,0 +1,105 @@
|
||||
package com.baeldung.boot.findby;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import com.baeldung.findby.Account;
|
||||
import com.baeldung.findby.AccountApplication;
|
||||
import com.baeldung.findby.AccountRepository;
|
||||
import com.baeldung.findby.Permission;
|
||||
import com.baeldung.findby.PermissionRepository;
|
||||
|
||||
@SpringBootTest(classes = AccountApplication.class)
|
||||
public class AccountRepositoryUnitTest {
|
||||
|
||||
@Autowired
|
||||
private AccountRepository accountRepository;
|
||||
|
||||
@Autowired
|
||||
private PermissionRepository permissionRepository;
|
||||
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
saveAccount();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
accountRepository.deleteAll();
|
||||
permissionRepository.deleteAll();
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAccountInDb_whenPerformFindByEmail_thenReturnsAccount() {
|
||||
String email = "test@test.com";
|
||||
Account account = accountRepository.findByEmail(email);
|
||||
assertThat(account.getEmail()).isEqualTo(email);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAccountInDb_whenPerformFindByUsernameAndEmail_thenReturnsAccount() {
|
||||
String email = "test@test.com";
|
||||
String username = "user_admin";
|
||||
Account account = accountRepository.findByUsernameAndEmail(username, email);
|
||||
assertThat(account.getUsername()).isEqualTo(username);
|
||||
assertThat(account.getEmail()).isEqualTo(email);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAccountInDb_whenPerformFindByUsernameOrEmail_thenReturnsAccount() {
|
||||
String email = "test@test.com";
|
||||
String username = "user_editor";
|
||||
Account account = accountRepository.findByUsernameOrEmail(username, email);
|
||||
assertThat(account.getUsername()).isNotEqualTo(username);
|
||||
assertThat(account.getEmail()).isEqualTo(email);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenAccountInDb_whenPerformFindByUsernameInOrEmailIn_thenReturnsAccounts() {
|
||||
List<String> emails = Arrays.asList("test@test.com", "abc@abc.com", "pqr@pqr.com");
|
||||
List<String> usernames = Arrays.asList("user_editor", "user_admin");
|
||||
List<Account> byUsernameInOrEmailIn = accountRepository.findByUsernameInOrEmailIn(usernames, emails);
|
||||
assertThat(byUsernameInOrEmailIn.size()).isEqualTo(1);
|
||||
assertThat(byUsernameInOrEmailIn.get(0)
|
||||
.getEmail()).isEqualTo("test@test.com");
|
||||
}
|
||||
|
||||
private Permission getPermissions() {
|
||||
Permission editor = new Permission();
|
||||
editor.setType("editor");
|
||||
permissionRepository.save(editor);
|
||||
return editor;
|
||||
}
|
||||
|
||||
private void saveAccount() {
|
||||
List<List<String>> sampleRecords = Arrays.asList(
|
||||
Arrays.asList("test@test.com", "user_admin"),
|
||||
Arrays.asList("test1@test.com", "user_admin_1"),
|
||||
Arrays.asList("test2@test.com", "user_admin_2")
|
||||
);
|
||||
|
||||
sampleRecords.forEach(sampleRecord -> {
|
||||
Account account = new Account();
|
||||
account.setEmail(sampleRecord.get(0));
|
||||
account.setUsername(sampleRecord.get(1));
|
||||
account.setPermission(getPermissions());
|
||||
account.setPassword(UUID.randomUUID()
|
||||
.toString());
|
||||
account.setCreatedOn(Timestamp.from(Instant.now()));
|
||||
account.setLastLogin(Timestamp.from(Instant.now()));
|
||||
accountRepository.save(account);
|
||||
System.out.println(account.toString());
|
||||
});
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user