forked from baskbuddy/zhangshun
4 changed files with 159 additions and 6 deletions
@ -0,0 +1,91 @@ |
|||||
|
package com.mydreamjob.review; |
||||
|
|
||||
|
import org.junit.jupiter.api.AfterEach; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
import org.springframework.boot.test.mock.mockito.MockBean; |
||||
|
import org.springframework.data.redis.core.RedisTemplate; |
||||
|
import org.springframework.jdbc.core.JdbcTemplate; |
||||
|
import org.springframework.test.context.ActiveProfiles; |
||||
|
|
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
|
||||
|
@SpringBootTest |
||||
|
@ActiveProfiles("integration-test") |
||||
|
class ReviewControllerFixtureIsolationTest { |
||||
|
|
||||
|
private static final long SENTINEL_ID = 9_320_099L; |
||||
|
private static final long PROBE_ID = 9_320_098L; |
||||
|
|
||||
|
@Autowired private JdbcTemplate jdbcTemplate; |
||||
|
@MockBean private RedisTemplate<String, Object> redisTemplate; |
||||
|
|
||||
|
@AfterEach |
||||
|
void cleanup() { |
||||
|
ReviewControllerFixtureUsers.remove(jdbcTemplate, |
||||
|
AdminReviewControllerTest.ADMIN_ID, |
||||
|
AdminReviewControllerTest.STUDENT_ID, |
||||
|
CompanyReviewManagementControllerTest.COMPANY_USER_ID, |
||||
|
CompanyReviewManagementControllerTest.STUDENT_USER_ID, |
||||
|
SENTINEL_ID, PROBE_ID); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void fixtureIdsAreOutsideLegacySeedRangeAndMutuallyDistinct() { |
||||
|
List<Long> ids = List.of( |
||||
|
AdminReviewControllerTest.ADMIN_ID, |
||||
|
AdminReviewControllerTest.STUDENT_ID, |
||||
|
CompanyReviewManagementControllerTest.COMPANY_USER_ID, |
||||
|
CompanyReviewManagementControllerTest.STUDENT_USER_ID); |
||||
|
|
||||
|
assertThat(ids).doesNotContain(1L, 2L, 3L).doesNotHaveDuplicates(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void provisionCreatesAnActiveVerifiedUserAndCanBeRepeated() { |
||||
|
for (int round = 0; round < 3; round++) { |
||||
|
ReviewControllerFixtureUsers.provision(jdbcTemplate, PROBE_ID, "r01b-review-probe", "ADMIN"); |
||||
|
} |
||||
|
|
||||
|
Map<String, Object> row = jdbcTemplate.queryForMap( |
||||
|
"SELECT id, role, is_active, is_verified FROM users WHERE id = ?", PROBE_ID); |
||||
|
assertThat(((Number) row.get("id")).longValue()).isEqualTo(PROBE_ID); |
||||
|
assertThat(row.get("role")).isEqualTo("ADMIN"); |
||||
|
assertThat(((Number) row.get("is_active")).intValue()).isEqualTo(1); |
||||
|
assertThat(((Number) row.get("is_verified")).intValue()).isEqualTo(1); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void cleanupRemovesOnlyOwnedRows() { |
||||
|
ReviewControllerFixtureUsers.provision(jdbcTemplate, PROBE_ID, "r01b-review-probe", "ADMIN"); |
||||
|
ReviewControllerFixtureUsers.provision(jdbcTemplate, SENTINEL_ID, "r01b-review-sentinel", "STUDENT"); |
||||
|
|
||||
|
ReviewControllerFixtureUsers.remove(jdbcTemplate, PROBE_ID); |
||||
|
|
||||
|
assertThat(countUser(PROBE_ID)).isZero(); |
||||
|
assertThat(countUser(SENTINEL_ID)).isEqualTo(1); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
void fixtureCycleLeavesLegacySeedRowsUnchanged() { |
||||
|
List<Map<String, Object>> before = seedRows(); |
||||
|
|
||||
|
ReviewControllerFixtureUsers.provision(jdbcTemplate, PROBE_ID, "r01b-review-probe", "ADMIN"); |
||||
|
ReviewControllerFixtureUsers.remove(jdbcTemplate, PROBE_ID); |
||||
|
|
||||
|
assertThat(seedRows()).isEqualTo(before); |
||||
|
} |
||||
|
|
||||
|
private int countUser(long id) { |
||||
|
return jdbcTemplate.queryForObject("SELECT COUNT(*) FROM users WHERE id = ?", Integer.class, id); |
||||
|
} |
||||
|
|
||||
|
private List<Map<String, Object>> seedRows() { |
||||
|
return jdbcTemplate.queryForList( |
||||
|
"SELECT id, phone, role, is_active, is_verified FROM users WHERE id IN (1, 2, 3) ORDER BY id"); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
package com.mydreamjob.review; |
||||
|
|
||||
|
import org.springframework.jdbc.core.JdbcTemplate; |
||||
|
|
||||
|
/** Test-only fixture helper that creates and removes only explicitly owned users. */ |
||||
|
final class ReviewControllerFixtureUsers { |
||||
|
|
||||
|
private ReviewControllerFixtureUsers() { |
||||
|
} |
||||
|
|
||||
|
static void provision(JdbcTemplate jdbcTemplate, long id, String phone, String role) { |
||||
|
remove(jdbcTemplate, id); |
||||
|
jdbcTemplate.update("INSERT INTO users (id, phone, nickname, role, is_active, is_verified) " |
||||
|
+ "VALUES (?, ?, ?, ?, 1, 1)", id, phone, "r01b-review-fixture-" + role, role); |
||||
|
} |
||||
|
|
||||
|
static void remove(JdbcTemplate jdbcTemplate, long... ids) { |
||||
|
for (long id : ids) { |
||||
|
jdbcTemplate.update("DELETE FROM auth_tokens WHERE user_id = ?", id); |
||||
|
jdbcTemplate.update("DELETE FROM user_roles WHERE user_id = ?", id); |
||||
|
jdbcTemplate.update("DELETE FROM users WHERE id = ?", id); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue