@ -0,0 +1,344 @@ | |||
// Pekka Helenius <fincer89@hotmail.com>, Fjordtek 2020 | |||
package com.fjordtek.bookstore.model.auth; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.junit.jupiter.api.Assertions.assertThrows; | |||
import java.security.SecureRandom; | |||
import javax.validation.ConstraintViolationException; | |||
import org.junit.Test; | |||
import org.junit.jupiter.api.MethodOrderer.Alphanumeric; | |||
import org.junit.jupiter.api.TestMethodOrder; | |||
import org.junit.runner.RunWith; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | |||
import org.springframework.dao.DataIntegrityViolationException; | |||
import org.springframework.orm.jpa.JpaSystemException; | |||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; | |||
import org.springframework.security.crypto.password.PasswordEncoder; | |||
import org.springframework.test.context.junit4.SpringRunner; | |||
/** | |||
* | |||
* TODO: N/A | |||
* | |||
* @author Pekka Helenius | |||
*/ | |||
@RunWith(SpringRunner.class) | |||
@DataJpaTest | |||
@TestMethodOrder(Alphanumeric.class) | |||
public class AuthRepositoriesTest { | |||
@Autowired | |||
private RoleRepository roleRepository; | |||
@Autowired | |||
private UserRepository userRepository; | |||
@Autowired | |||
private UserRoleRepository userRoleRepository; | |||
private PasswordEncoder passwordEncoder = new BCryptPasswordEncoder(14, new SecureRandom()); | |||
////////////////////////////// | |||
@Test | |||
public void testA_CreateRole() { | |||
Role roleA = roleRepository.save(new Role("TEST")); | |||
assertThat(roleA).isNotNull(); | |||
} | |||
@Test | |||
public void testB_CreateRoleWithoutNameShouldFail() { | |||
assertThrows( | |||
ConstraintViolationException.class, () -> { | |||
roleRepository.save(new Role()); | |||
roleRepository.findAll().forEach(System.out::print); | |||
} | |||
); | |||
} | |||
@Test | |||
public void testC_CreateRoleDuplicateNameShouldFail() { | |||
assertThrows( | |||
DataIntegrityViolationException.class, () -> { | |||
roleRepository.save(new Role("FOO")); | |||
roleRepository.save(new Role("FOO")); | |||
roleRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
@Test | |||
public void testD_DeleteUnusedRole() { | |||
Role roleA = roleRepository.save(new Role("FOO")); | |||
Long id = roleA.getId(); | |||
roleRepository.delete(roleA); | |||
assertThat(roleRepository.findById(id)).isEmpty(); | |||
} | |||
@Test | |||
public void testE_ChangeRoleName() { | |||
String nameA = "FOO"; | |||
String nameB = "BAR"; | |||
Role roleA = roleRepository.save(new Role(nameA)); | |||
roleA.setName(nameB); | |||
roleRepository.save(roleA); | |||
assertThat(roleA.getName().equals(nameA)).isEqualTo(false); | |||
//System.out.println(roleRepository.findById(roleA.getId()).get().getName()); | |||
} | |||
////////////////////////////// | |||
@Test | |||
public void testF_CreateUser() { | |||
User userA = userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("bar"), | |||
"foo.bar@fjordtek.com" | |||
) | |||
); | |||
assertThat(userA).isNotNull(); | |||
} | |||
@Test | |||
public void testG__CreateUserWithEmptyPasswordShouldFail() { | |||
/* | |||
* NOTE: null password must be forced for empty/blank passwords | |||
* if sign up form is implemented. | |||
* BCryptPasswordEncoder encode method checks for null value only | |||
* and throws exception accordingly. It does not work on empty | |||
* ("") values. | |||
* | |||
*/ | |||
assertThrows( | |||
IllegalArgumentException.class, () -> { | |||
userRepository.save(new User( | |||
"bar", | |||
passwordEncoder.encode(null), | |||
"bar@fjordtek.com" | |||
) | |||
); | |||
}); | |||
} | |||
@Test | |||
public void testH__CreateUserWithInvalidEmailShouldFail() { | |||
assertThrows( | |||
ConstraintViolationException.class, () -> { | |||
userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("bar is foo"), | |||
"bar//foobar.com" | |||
) | |||
); | |||
userRepository.findAll().forEach(System.out::print); | |||
}); | |||
assertThrows( | |||
ConstraintViolationException.class, () -> { | |||
userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("bar is foo"), | |||
"bar||foobar.com" | |||
) | |||
); | |||
userRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
@Test | |||
public void testI_CreateUserWithEmptyUsernameShouldFail() { | |||
assertThrows( | |||
ConstraintViolationException.class, () -> { | |||
userRepository.save(new User( | |||
"", | |||
passwordEncoder.encode("bar is foo"), | |||
"bar@foobar.com" | |||
) | |||
); | |||
userRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
@Test | |||
public void testJ_CreateUserDuplicateUsernamesShouldFail() { | |||
assertThrows( | |||
DataIntegrityViolationException.class, () -> { | |||
userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("foo"), | |||
"foo@foo.com" | |||
) | |||
); | |||
userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("bar"), | |||
"bar@bar.com" | |||
) | |||
); | |||
userRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
@Test | |||
public void testK_DeleteExistingUser() { | |||
User userA = userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("foo"), | |||
"foo@foo.com" | |||
) | |||
); | |||
Long id = userA.getId(); | |||
userRepository.delete(userA); | |||
assertThat(userRepository.findById(id)).isEmpty(); | |||
} | |||
@Test | |||
public void testL_ChangeUserName() { | |||
String nameA = "foo"; | |||
String nameB = "bar"; | |||
User userA = userRepository.save(new User( | |||
nameA, | |||
passwordEncoder.encode("foo"), | |||
"foo@foo.com" | |||
) | |||
); | |||
userA.setUsername(nameB); | |||
userRepository.save(userA); | |||
assertThat(userA.getUsername().equals(nameA)).isEqualTo(false); | |||
//System.out.println(userRepository.findById(userA.getId()).get().getUsername()); | |||
} | |||
////////////////////////////// | |||
@Test | |||
public void testK_JoinAddExistingRoleAndUser() { | |||
User userA = userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("foo"), | |||
"foo@foo.com" | |||
) | |||
); | |||
Role roleA = new Role("FOO"); | |||
UserRole userRoleA = new UserRole(userA, roleA); | |||
userRoleRepository.save(userRoleA); | |||
assertThat(userRoleA).isNotNull(); | |||
} | |||
@Test | |||
public void testL_JoinSetRoleNullKeepUserShouldFail() { | |||
assertThrows( | |||
JpaSystemException.class, () -> { | |||
User userA = userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("foo"), | |||
"foo@foo.com" | |||
) | |||
); | |||
userRepository.save(userA); | |||
userRoleRepository.save(new UserRole(userA, null)); | |||
}); | |||
} | |||
@Test | |||
public void testM_JoinSetUserNullKeepRoleShouldFail() { | |||
assertThrows( | |||
JpaSystemException.class, () -> { | |||
Role roleA = new Role("FOO"); | |||
roleRepository.save(roleA); | |||
userRoleRepository.save(new UserRole(null, roleA)); | |||
}); | |||
} | |||
@Test | |||
public void testN_JoinAddDuplicatePrimaryKeyShouldFail() { | |||
assertThrows( | |||
DataIntegrityViolationException.class, () -> { | |||
User userA = userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("foo"), | |||
"foo@foo.com" | |||
) | |||
); | |||
Role roleA = new Role("FOO"); | |||
userRepository.save(userA); | |||
roleRepository.save(roleA); | |||
userRoleRepository.save(new UserRole(userA, roleA)); | |||
userRoleRepository.save(new UserRole(userA, roleA)); | |||
}); | |||
} | |||
@Test | |||
public void testO_JoinDeleteExistingRoleAndUser() { | |||
User userA = userRepository.save(new User( | |||
"foo", | |||
passwordEncoder.encode("foo"), | |||
"foo@foo.com" | |||
) | |||
); | |||
Role roleA = new Role("FOO"); | |||
userRepository.save(userA); | |||
roleRepository.save(roleA); | |||
UserRole userRoleA = new UserRole(userA, roleA); | |||
userRoleRepository.save(userRoleA); | |||
userRoleRepository.deleteByCompositeId(userA.getId(), roleA.getId()); | |||
assertThat(userRoleRepository.findByCompositeId(userA.getId(), roleA.getId())).isNull(); | |||
} | |||
} |
@ -0,0 +1,351 @@ | |||
// Pekka Helenius <fincer89@hotmail.com>, Fjordtek 2020 | |||
package com.fjordtek.bookstore.model.book; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.junit.jupiter.api.Assertions.assertThrows; | |||
import java.math.BigDecimal; | |||
import javax.validation.ConstraintViolationException; | |||
import org.junit.Test; | |||
import org.junit.jupiter.api.MethodOrderer.Alphanumeric; | |||
import org.junit.jupiter.api.TestMethodOrder; | |||
import org.junit.runner.RunWith; | |||
import org.springframework.beans.factory.annotation.Autowired; | |||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | |||
import org.springframework.dao.DataIntegrityViolationException; | |||
import org.springframework.orm.jpa.JpaSystemException; | |||
import org.springframework.test.context.junit4.SpringRunner; | |||
/** | |||
* | |||
* TODO: N/A | |||
* | |||
* @author Pekka Helenius | |||
*/ | |||
@RunWith(SpringRunner.class) | |||
@DataJpaTest | |||
@TestMethodOrder(Alphanumeric.class) | |||
public class BookRepositoriesTest { | |||
@Autowired | |||
private BookRepository bookRepository; | |||
@Autowired | |||
private BookHashRepository bookHashRepository; | |||
@Autowired | |||
private AuthorRepository authorRepository; | |||
@Autowired | |||
private CategoryRepository categoryRepository; | |||
////////////////////////////// | |||
@Test | |||
public void testA_CreateAuthor() { | |||
Author authorA = authorRepository.save(new Author("Foo", "Bar")); | |||
assertThat(authorA).isNotNull(); | |||
} | |||
@Test | |||
public void testB_CreateAuthorWithoutFirstName() { | |||
Author authorA = authorRepository.save(new Author(null, "Bar")); | |||
assertThat(authorA).isNotNull(); | |||
} | |||
@Test | |||
public void testC_CreateAuthorWithoutLastName() { | |||
Author authorA = authorRepository.save(new Author("Foo", null)); | |||
assertThat(authorA).isNotNull(); | |||
} | |||
@Test | |||
public void testD_CreateAuthorDuplicateNameShouldPass() { | |||
authorRepository.save(new Author("Foo", "Bar")); | |||
authorRepository.save(new Author("Foo", "Bar")); | |||
int savedAuthorsCount = | |||
authorRepository | |||
.findByFirstNameIgnoreCaseContainingAndLastNameIgnoreCaseContaining( | |||
"Foo", "Bar" | |||
).size(); | |||
assertThat(savedAuthorsCount).isEqualTo(2); | |||
} | |||
////////////////////////////// | |||
@Test | |||
public void testE_CreateCategory() { | |||
Category categoryA = categoryRepository.save(new Category("FOO")); | |||
assertThat(categoryA).isNotNull(); | |||
} | |||
@Test | |||
public void testF_CreateCategoryWithoutNameShouldFail() { | |||
assertThrows( | |||
ConstraintViolationException.class, () -> { | |||
categoryRepository.save(new Category(null)); | |||
categoryRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
@Test | |||
public void testG_CreateCategoryDuplicateNameShouldFail() { | |||
assertThrows( | |||
DataIntegrityViolationException.class, () -> { | |||
categoryRepository.save(new Category("BAR")); | |||
categoryRepository.save(new Category("BAR")); | |||
categoryRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
////////////////////////////// | |||
@Test | |||
public void testH_CreateBookWithAuthor() { | |||
Author authorA = authorRepository.save(new Author("Fan", "Tasy")); | |||
Book bookA = bookRepository.save(new Book( | |||
"Bloody Chamber", | |||
authorA, | |||
1979, | |||
"4343434-14", | |||
new BigDecimal("18.00"), | |||
null, | |||
true | |||
) | |||
); | |||
assertThat(bookRepository.findById(bookA.getId())).isNotNull(); | |||
} | |||
@Test | |||
public void testI_CreateNewBookWithoutAuthor() { | |||
Book bookA = bookRepository.save(new Book( | |||
"Bloody Chamber", | |||
null, | |||
1979, | |||
"4343434-15", | |||
new BigDecimal("18.00"), | |||
null, | |||
true | |||
) | |||
); | |||
assertThat(bookRepository.findById(bookA.getId())).isNotNull(); | |||
} | |||
@Test | |||
public void testJ_CreateBookWithCategory() { | |||
Category categoryA = categoryRepository.save(new Category("Technology")); | |||
Book bookA = bookRepository.save(new Book( | |||
"Innovations in the era of information technology", | |||
null, | |||
1954, | |||
"7575757-15", | |||
new BigDecimal("43.22"), | |||
categoryA, | |||
true | |||
) | |||
); | |||
assertThat(bookRepository.findById(bookA.getId())).isNotNull(); | |||
} | |||
/* | |||
@Test | |||
public void testK_CreateBookWithoutCategory() { | |||
} | |||
*/ | |||
////////////////////////////// | |||
@Test | |||
public void testL_ExistingBookIsbnIsUnique() { | |||
assertThrows( | |||
DataIntegrityViolationException.class, () -> { | |||
bookRepository.save(new Book( | |||
"Exoplanets and Earth", | |||
null, | |||
2009, | |||
"7575757-15", | |||
new BigDecimal("43.22"), | |||
null, | |||
true | |||
) | |||
); | |||
bookRepository.save(new Book( | |||
"Interstellar research for astrobiological life", | |||
null, | |||
2006, | |||
"7575757-15", | |||
new BigDecimal("11.99"), | |||
null, | |||
false | |||
) | |||
); | |||
bookRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
////////////////////////////// | |||
@Test | |||
public void testM_BookHashCanBeCreated() { | |||
Book bookA = new Book( | |||
"Exoplanets and Earth", | |||
null, | |||
2009, | |||
"9191919-15", | |||
new BigDecimal("43.22"), | |||
null, | |||
true | |||
); | |||
BookHash bookHashA = new BookHash(); | |||
bookHashA.setBook(bookA); | |||
bookA.setBookHash(bookHashA); | |||
bookRepository.save(bookA); | |||
bookHashRepository.save(bookHashA); | |||
assertThat(bookA).isNotNull(); | |||
assertThat(bookHashA).isNotNull(); | |||
assertThat(bookHashA.getBookId() == bookA.getId()).isEqualTo(true); | |||
assertThat(bookHashA.getHashId().length() == 32).isEqualTo(true); | |||
assertThat(bookHashA.getHashId().toString().matches("^[a-z0-9]*$")).isEqualTo(true); | |||
assertThat(bookHashRepository.findByBookId(bookA.getId())).isNotNull(); | |||
assertThat(bookHashRepository.findByHashId(bookHashA.getHashId())).isNotNull(); | |||
} | |||
@Test | |||
public void testN_BookHashCanSetNewBookHashId() { | |||
Book bookA = new Book( | |||
"Exoplanets and Earth", | |||
null, | |||
2009, | |||
"9191919-16", | |||
new BigDecimal("43.22"), | |||
null, | |||
true | |||
); | |||
BookHash bookHashA = new BookHash(); | |||
bookHashA.setBook(bookA); | |||
bookA.setBookHash(bookHashA); | |||
bookRepository.save(bookA); | |||
bookHashRepository.save(bookHashA); | |||
String hashIdA = bookHashA.getHashId(); | |||
bookHashA.setHashId(); | |||
String hashIdB = bookHashA.getHashId(); | |||
bookHashRepository.save(bookHashA); | |||
assertThat(bookHashA.getHashId().equals(hashIdA)).isEqualTo(false); | |||
} | |||
@Test | |||
public void testO_BookHashCanNotDeleteBookId() { | |||
assertThrows( | |||
JpaSystemException.class, () -> { | |||
Book bookA = new Book( | |||
"Exoplanets and Earth", | |||
null, | |||
2009, | |||
"9191919-17", | |||
new BigDecimal("43.22"), | |||
null, | |||
true | |||
); | |||
BookHash bookHashA = new BookHash(); | |||
bookHashA.setBook(bookA); | |||
bookA.setBookHash(bookHashA); | |||
bookRepository.save(bookA); | |||
bookHashA.setBookId(null); | |||
bookHashRepository.save(bookHashA); | |||
bookHashRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
////////////////////////////// | |||
@Test | |||
public void testP_DeleteAuthorBookHasAuthorConstraintShouldFail() { | |||
assertThrows( | |||
DataIntegrityViolationException.class, () -> { | |||
Author authorA = authorRepository.save(new Author("Hor", "Ror")); | |||
bookRepository.save(new Book( | |||
"Twisted adventures in Elvenwood treehouse", | |||
authorA, | |||
2006, | |||
"7575757-15", | |||
new BigDecimal("11.99"), | |||
null, | |||
false | |||
) | |||
); | |||
authorRepository.delete(authorA); | |||
bookRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
@Test | |||
public void testQ_DeleteCategoryBookHasCategoryConstraintShouldFail() { | |||
assertThrows( | |||
DataIntegrityViolationException.class, () -> { | |||
Category categoryA = categoryRepository.save(new Category("Mystery")); | |||
bookRepository.save(new Book( | |||
"Ruins of Rkund", | |||
null, | |||
2009, | |||
"7575757-15", | |||
new BigDecimal("11.99"), | |||
categoryA, | |||
true | |||
) | |||
); | |||
categoryRepository.delete(categoryA); | |||
bookRepository.findAll().forEach(System.out::print); | |||
}); | |||
} | |||
} |