Guest User

Untitled

a guest
Jan 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. @SpringBootTest
  2. @RunWith(SpringRunner.class)
  3. public class CompanyAuditStorageTest {
  4. @Autowired
  5. private CompanyRepository companyRepository;
  6. @Autowired
  7. private EntityManagerFactory entityManagerFactory;
  8. @Autowired
  9. private JdbcTemplate jdbcTemplate;
  10. @MockBean
  11. private AuditorAware<String> auditorAware;
  12.  
  13. private Company company;
  14.  
  15. @Before
  16. public void setUp() throws Exception {
  17. when(auditorAware.getCurrentAuditor()).thenReturn(Optional.of("user"));
  18.  
  19. company = new Company();
  20. company.setComment("comment");
  21. company.setName("new Company");
  22. companyRepository.save(company);
  23. }
  24.  
  25. @Test
  26. public void historyCorrectlyStoresTest() {
  27. company.setName("updated company");
  28. companyRepository.save(company);
  29.  
  30. AuditReader auditReader = AuditReaderFactory.get(entityManagerFactory.createEntityManager());
  31. Company firstRevision = auditReader.find(Company.class, company.getId(), 1);
  32. assertThat(firstRevision.getName(), is("new Company"));
  33. assertThat(firstRevision.getChangedAt(), notNullValue());
  34. assertThat(firstRevision.getChangedBy(), is("user"));
  35.  
  36. Company secondRevision = auditReader.find(Company.class, this.company.getId(), 2);
  37.  
  38. assertThat(secondRevision.getName(), is("updated company"));
  39. assertThat(secondRevision.getChangedAt(), notNullValue());
  40. assertThat(secondRevision.getChangedBy(), is("user"));
  41. }
  42.  
  43. @After
  44. public void tearDown() throws Exception {
  45. Long companyId = company.getId();
  46. jdbcTemplate.execute("delete from companies_event_log where id = " + companyId);
  47. companyRepository.deleteById(companyId);
  48. }
  49. }
  50.  
  51. @Test
  52. public void historyCorrectlyStoresTest() {
  53. company.setName("updated company");
  54. companyRepository.save(company);
  55.  
  56. AuditReader auditReader = AuditReaderFactory.get(entityManagerFactory.createEntityManager());
  57.  
  58. List<Number> revisionsListIds = auditReader.getRevisions(Company.class, company.getId());
  59.  
  60. Company firstRevision = auditReader.find(Company.class, company.getId(), revisionsListIds.get(0));
  61. assertThat(firstRevision.getName(), is("new Company"));
  62. assertThat(firstRevision.getChangedAt(), notNullValue());
  63. assertThat(firstRevision.getChangedBy(), is("user"));
  64.  
  65. Company secondRevision = auditReader.find(Company.class, this.company.getId(), revisionsListIds.get(1));
  66.  
  67. assertThat(secondRevision.getName(), is("updated company"));
  68. assertThat(secondRevision.getChangedAt(), notNullValue());
  69. assertThat(secondRevision.getChangedBy(), is("user"));
  70. }
Add Comment
Please, Sign In to add comment