Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @SpringBootTest
- @RunWith(SpringRunner.class)
- public class CompanyAuditStorageTest {
- @Autowired
- private CompanyRepository companyRepository;
- @Autowired
- private EntityManagerFactory entityManagerFactory;
- @Autowired
- private JdbcTemplate jdbcTemplate;
- @MockBean
- private AuditorAware<String> auditorAware;
- private Company company;
- @Before
- public void setUp() throws Exception {
- when(auditorAware.getCurrentAuditor()).thenReturn(Optional.of("user"));
- company = new Company();
- company.setComment("comment");
- company.setName("new Company");
- companyRepository.save(company);
- }
- @Test
- public void historyCorrectlyStoresTest() {
- company.setName("updated company");
- companyRepository.save(company);
- AuditReader auditReader = AuditReaderFactory.get(entityManagerFactory.createEntityManager());
- Company firstRevision = auditReader.find(Company.class, company.getId(), 1);
- assertThat(firstRevision.getName(), is("new Company"));
- assertThat(firstRevision.getChangedAt(), notNullValue());
- assertThat(firstRevision.getChangedBy(), is("user"));
- Company secondRevision = auditReader.find(Company.class, this.company.getId(), 2);
- assertThat(secondRevision.getName(), is("updated company"));
- assertThat(secondRevision.getChangedAt(), notNullValue());
- assertThat(secondRevision.getChangedBy(), is("user"));
- }
- @After
- public void tearDown() throws Exception {
- Long companyId = company.getId();
- jdbcTemplate.execute("delete from companies_event_log where id = " + companyId);
- companyRepository.deleteById(companyId);
- }
- }
- @Test
- public void historyCorrectlyStoresTest() {
- company.setName("updated company");
- companyRepository.save(company);
- AuditReader auditReader = AuditReaderFactory.get(entityManagerFactory.createEntityManager());
- List<Number> revisionsListIds = auditReader.getRevisions(Company.class, company.getId());
- Company firstRevision = auditReader.find(Company.class, company.getId(), revisionsListIds.get(0));
- assertThat(firstRevision.getName(), is("new Company"));
- assertThat(firstRevision.getChangedAt(), notNullValue());
- assertThat(firstRevision.getChangedBy(), is("user"));
- Company secondRevision = auditReader.find(Company.class, this.company.getId(), revisionsListIds.get(1));
- assertThat(secondRevision.getName(), is("updated company"));
- assertThat(secondRevision.getChangedAt(), notNullValue());
- assertThat(secondRevision.getChangedBy(), is("user"));
- }
Add Comment
Please, Sign In to add comment