Skip to content
This repository was archived by the owner on Nov 23, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7bbebcc
Add comprehensive repository tests for Role, User, UserPreferences, a…
AdithaBuwaneka Nov 21, 2025
9748b1b
feat: Add comprehensive unit tests for AuthService including authenti…
AdithaBuwaneka Nov 21, 2025
9df11de
feat: Add comprehensive unit tests for UserService covering user mana…
AdithaBuwaneka Nov 21, 2025
9b62425
feat: Update role IDs to use Long type and add comprehensive tests fo…
AdithaBuwaneka Nov 21, 2025
e32f8bf
refactor: Improve readability of authority retrieval in AuthServiceTest
AdithaBuwaneka Nov 21, 2025
355f72c
refactor: Simplify authority retrieval in AuthServiceTest
AdithaBuwaneka Nov 21, 2025
52002b0
refactor: Optimize authority retrieval in AuthServiceTest
AdithaBuwaneka Nov 21, 2025
98f3c03
refactor: Replace array list with singleton for user authorities in A…
AdithaBuwaneka Nov 21, 2025
f3857e6
refactor: Consolidate mock declarations and setup in AuthServiceTest …
AdithaBuwaneka Nov 21, 2025
88a9292
refactor: Replace array list with singleton for authorities in AuthSe…
AdithaBuwaneka Nov 21, 2025
f60fe28
refactor: Replace array list with singleton for authorities in AuthSe…
AdithaBuwaneka Nov 21, 2025
0dab850
refactor: Improve readability of authority retrieval in UserServiceTest
AdithaBuwaneka Nov 21, 2025
3c4b79c
refactor: Update mock request IP address and email verification in Au…
AdithaBuwaneka Nov 21, 2025
729741f
refactor: Simplify tokenService.createRefreshToken calls in AuthServi…
AdithaBuwaneka Nov 21, 2025
fa2d0b8
refactor: Use lenient stubbing for request IP address in AuthServiceTest
AdithaBuwaneka Nov 21, 2025
4424cf0
refactor: Update tokenService.createRefreshToken calls to use fixed I…
AdithaBuwaneka Nov 21, 2025
17aae48
refactor: Use lenient stubbing for userRepository in AuthServiceTest …
AdithaBuwaneka Nov 21, 2025
3ec4611
refactor: Use lenient stubbing for userRepository in UserServiceTest
AdithaBuwaneka Nov 21, 2025
55d9b9e
refactor: Use lenient stubbing for roleRepository in UserServiceTest
AdithaBuwaneka Nov 21, 2025
90d51f0
refactor: Use lenient stubbing for roleRepository in UserServiceTest
AdithaBuwaneka Nov 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,286 @@
package com.techtorque.auth_service.repository;

import com.techtorque.auth_service.entity.LoginLock;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Comprehensive test class for LoginLockRepository
* Tests all repository methods, edge cases, and database constraints
*/
@DataJpaTest
@ActiveProfiles("test")
class LoginLockRepositoryTest {

@Autowired
private TestEntityManager entityManager;

@Autowired
private LoginLockRepository loginLockRepository;

private LoginLock testLoginLock;

@BeforeEach
void setUp() {
testLoginLock = LoginLock.builder()
.username("testuser")
.failedAttempts(3)
.lockUntil(LocalDateTime.now().plusMinutes(15))
.build();
}

@Test
void save_WhenValidLoginLock_ShouldPersistSuccessfully() {
// When
LoginLock savedLock = loginLockRepository.save(testLoginLock);

// Then
assertThat(savedLock.getId()).isNotNull();
assertThat(savedLock.getUsername()).isEqualTo("testuser");
assertThat(savedLock.getFailedAttempts()).isEqualTo(3);
assertThat(savedLock.getLockUntil()).isNotNull();
}

@Test
void save_WithDefaultFailedAttempts_ShouldUseZero() {
// Given
LoginLock lockWithDefaults = LoginLock.builder()
.username("newuser")
.build();

// When
LoginLock savedLock = loginLockRepository.save(lockWithDefaults);

// Then
assertThat(savedLock.getId()).isNotNull();
assertThat(savedLock.getUsername()).isEqualTo("newuser");
assertThat(savedLock.getFailedAttempts()).isEqualTo(0);
assertThat(savedLock.getLockUntil()).isNull();
}

@Test
void findByUsername_WhenLockExists_ShouldReturnLock() {
// Given
entityManager.persistAndFlush(testLoginLock);

// When
Optional<LoginLock> result = loginLockRepository.findByUsername("testuser");

// Then
assertThat(result).isPresent();
assertThat(result.get().getUsername()).isEqualTo("testuser");
assertThat(result.get().getFailedAttempts()).isEqualTo(3);
}

@Test
void findByUsername_WhenLockDoesNotExist_ShouldReturnEmpty() {
// When
Optional<LoginLock> result = loginLockRepository.findByUsername("nonexistent");

// Then
assertThat(result).isEmpty();
}

@Test
void findByUsername_CaseSensitive_ShouldReturnEmpty() {
// Given
entityManager.persistAndFlush(testLoginLock);

// When
Optional<LoginLock> result = loginLockRepository.findByUsername("TESTUSER");

// Then
assertThat(result).isEmpty();
}

@Test
@Transactional
void deleteByUsername_WhenLockExists_ShouldDeleteLock() {
// Given
entityManager.persistAndFlush(testLoginLock);

LoginLock anotherLock = LoginLock.builder()
.username("anotheruser")
.failedAttempts(1)
.build();
entityManager.persistAndFlush(anotherLock);

// When
loginLockRepository.deleteByUsername("testuser");
entityManager.flush();

// Then
Optional<LoginLock> deletedLock = loginLockRepository.findByUsername("testuser");
Optional<LoginLock> remainingLock = loginLockRepository.findByUsername("anotheruser");

assertThat(deletedLock).isEmpty();
assertThat(remainingLock).isPresent();
}

@Test
void update_WhenLockExists_ShouldUpdateSuccessfully() {
// Given
LoginLock savedLock = entityManager.persistAndFlush(testLoginLock);
entityManager.detach(savedLock);

// When
savedLock.setFailedAttempts(5);
savedLock.setLockUntil(LocalDateTime.now().plusMinutes(30));
LoginLock updatedLock = loginLockRepository.save(savedLock);

// Then
assertThat(updatedLock.getFailedAttempts()).isEqualTo(5);
assertThat(updatedLock.getLockUntil()).isNotNull();
assertThat(updatedLock.getUsername()).isEqualTo("testuser"); // Should remain unchanged
}

@Test
void findById_WhenLockExists_ShouldReturnLock() {
// Given
LoginLock savedLock = entityManager.persistAndFlush(testLoginLock);

// When
Optional<LoginLock> result = loginLockRepository.findById(savedLock.getId());

// Then
assertThat(result).isPresent();
assertThat(result.get().getId()).isEqualTo(savedLock.getId());
assertThat(result.get().getUsername()).isEqualTo("testuser");
}

@Test
void findById_WhenLockDoesNotExist_ShouldReturnEmpty() {
// When
Optional<LoginLock> result = loginLockRepository.findById(999L);

// Then
assertThat(result).isEmpty();
}

@Test
void findAll_ShouldReturnAllLocks() {
// Given
LoginLock lock1 = LoginLock.builder()
.username("user1")
.failedAttempts(2)
.lockUntil(LocalDateTime.now().plusMinutes(10))
.build();

LoginLock lock2 = LoginLock.builder()
.username("user2")
.failedAttempts(1)
.build();

entityManager.persistAndFlush(lock1);
entityManager.persistAndFlush(lock2);

// When
List<LoginLock> allLocks = loginLockRepository.findAll();

// Then
assertThat(allLocks).hasSize(2);
assertThat(allLocks).extracting("username")
.containsExactlyInAnyOrder("user1", "user2");
}

@Test
void count_ShouldReturnCorrectCount() {
// Given
entityManager.persistAndFlush(testLoginLock);

LoginLock secondLock = LoginLock.builder()
.username("user2")
.failedAttempts(1)
.build();
entityManager.persistAndFlush(secondLock);

// When
long count = loginLockRepository.count();

// Then
assertThat(count).isEqualTo(2);
}

@Test
void save_WithoutLockUntil_ShouldPersistSuccessfully() {
// Given
LoginLock lockWithoutLockUntil = LoginLock.builder()
.username("testuser2")
.failedAttempts(1)
.lockUntil(null)
.build();

// When
LoginLock savedLock = loginLockRepository.save(lockWithoutLockUntil);

// Then
assertThat(savedLock.getId()).isNotNull();
assertThat(savedLock.getUsername()).isEqualTo("testuser2");
assertThat(savedLock.getFailedAttempts()).isEqualTo(1);
assertThat(savedLock.getLockUntil()).isNull();
}

@Test
void save_WithZeroFailedAttempts_ShouldPersistSuccessfully() {
// Given
LoginLock lockWithZeroAttempts = LoginLock.builder()
.username("testuser3")
.failedAttempts(0)
.build();

// When
LoginLock savedLock = loginLockRepository.save(lockWithZeroAttempts);

// Then
assertThat(savedLock.getId()).isNotNull();
assertThat(savedLock.getUsername()).isEqualTo("testuser3");
assertThat(savedLock.getFailedAttempts()).isEqualTo(0);
}

@Test
void existsById_WhenLockExists_ShouldReturnTrue() {
// Given
LoginLock savedLock = entityManager.persistAndFlush(testLoginLock);

// When
boolean exists = loginLockRepository.existsById(savedLock.getId());

// Then
assertThat(exists).isTrue();
}

@Test
void existsById_WhenLockDoesNotExist_ShouldReturnFalse() {
// When
boolean exists = loginLockRepository.existsById(999L);

// Then
assertThat(exists).isFalse();
}

@Test
void deleteById_WhenLockExists_ShouldRemoveLock() {
// Given
LoginLock savedLock = entityManager.persistAndFlush(testLoginLock);
Long lockId = savedLock.getId();

// When
loginLockRepository.deleteById(lockId);
entityManager.flush();

// Then
Optional<LoginLock> result = loginLockRepository.findById(lockId);
assertThat(result).isEmpty();
}
}
Loading