Commit ffa655be authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch 'release-1.4' into 'master'

Release 1.4

See merge request genesys-pgr/application-blocks!34
parents cabd3831 149f457d
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -31,6 +31,9 @@ import javax.persistence.Table;
import org.genesys.blocks.auditlog.annotations.Audited;
import org.genesys.blocks.model.BasicModel;
/**
* The Class ExampleAuditedEntity.
*/
@Entity
@Table(name = "exampleauditedentity")
@Audited
......@@ -54,34 +57,74 @@ public class ExampleAuditedEntity extends BasicModel {
@CollectionTable(name = "entity_set", joinColumns = @JoinColumn(name = "entityId", referencedColumnName = "id"))
private Set<String> set;
/**
* Gets the name.
*
* @return the name
*/
public String getName() {
return name;
}
/**
* Sets the name.
*
* @param name the new name
*/
public void setName(String name) {
this.name = name;
}
/**
* Sets the reference.
*
* @param refEntity the new reference
*/
public void setReference(ExampleAuditedEntity refEntity) {
this.reference = refEntity;
}
/**
* Gets the reference.
*
* @return the reference
*/
public ExampleAuditedEntity getReference() {
return reference;
}
/**
* Gets the list.
*
* @return the list
*/
public List<Long> getList() {
return list;
}
/**
* Sets the list.
*
* @param list the new list
*/
public void setList(List<Long> list) {
this.list = list;
}
/**
* Gets the sets the.
*
* @return the sets the
*/
public Set<String> getSet() {
return set;
}
/**
* Sets the sets the.
*
* @param set the new sets the
*/
public void setSet(Set<String> set) {
this.set = set;
}
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -37,6 +37,9 @@ import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
/**
* The Class AuditTrailServiceTest.
*/
public class AuditTrailServiceTest extends ServiceTest {
@Autowired
......@@ -57,6 +60,9 @@ public class AuditTrailServiceTest extends ServiceTest {
auditLogRepository.deleteAll();
}
/**
* Save 1.
*/
@Test
public void save1() {
final Long entity = new Long(42);
......@@ -71,6 +77,9 @@ public class AuditTrailServiceTest extends ServiceTest {
assertThat(log.getReferencedEntity(), nullValue());
}
/**
* Save ref ent.
*/
@Test
public void saveRefEnt() {
final Long entity = new Long(42);
......@@ -86,6 +95,9 @@ public class AuditTrailServiceTest extends ServiceTest {
assertThat(log.getReferencedEntity(), equalTo(classPkService.getClassPk(AuditLog.class)));
}
/**
* Test example audited entity.
*/
@Test
public void testExampleAuditedEntity() {
ExampleAuditedEntity entity = new ExampleAuditedEntity();
......@@ -100,6 +112,9 @@ public class AuditTrailServiceTest extends ServiceTest {
assertThat(listAuditLogs(entity), hasSize(1));
}
/**
* Test transactional list query.
*/
@Test
public void testTransactionalListQuery() {
ExampleAuditedEntity entity = exampleAuditedEntityService.testList();
......@@ -115,6 +130,9 @@ public class AuditTrailServiceTest extends ServiceTest {
assertThat(lastLog.getNewState(), is(entity.getName()));
}
/**
* Test delete.
*/
@Test
public void testDelete() {
ExampleAuditedEntity entity = exampleAuditedEntityService.testList();
......@@ -131,6 +149,9 @@ public class AuditTrailServiceTest extends ServiceTest {
assertThat(lastLog.getPreviousState(), is(entity.getName()));
}
/**
* Test rollback.
*/
@Test
public void testRollback() {
ExampleAuditedEntity entity = new ExampleAuditedEntity();
......@@ -153,6 +174,9 @@ public class AuditTrailServiceTest extends ServiceTest {
}
}
/**
* Test load referenced entity.
*/
@Test
public void testLoadReferencedEntity() {
ExampleAuditedEntity entity = new ExampleAuditedEntity();
......@@ -169,6 +193,9 @@ public class AuditTrailServiceTest extends ServiceTest {
assertThat(((ExampleAuditedEntity) lookup).getId(), is(entity.getId()));
}
/**
* Test referenced entity audit log.
*/
@Test
public void testReferencedEntityAuditLog() {
ExampleAuditedEntity refEntity = new ExampleAuditedEntity();
......@@ -191,6 +218,9 @@ public class AuditTrailServiceTest extends ServiceTest {
assertThat(((ExampleAuditedEntity) listAuditLogs(entity).get(0).getNewEntity()).getId(), is(refEntity.getId()));
}
/**
* Test embedded collections.
*/
@Test
public void testEmbeddedCollections() {
ExampleAuditedEntity entity = new ExampleAuditedEntity();
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -24,6 +24,9 @@ import org.genesys.blocks.auditlog.test.ServiceTest;
import org.genesys.blocks.model.ClassPK;
import org.junit.Test;
/**
* The Class ClassPKServiceTest.
*/
public class ClassPKServiceTest extends ServiceTest {
@Override
......@@ -33,6 +36,9 @@ public class ClassPKServiceTest extends ServiceTest {
classPkRepository.deleteAll();
}
/**
* Auto create.
*/
@Test
public void autoCreate() {
final ClassPK cpk = classPkService.getClassPk(Long.class);
......@@ -40,6 +46,11 @@ public class ClassPKServiceTest extends ServiceTest {
assertThat(cpk.getClassname(), equalTo(Long.class.getName()));
}
/**
* Gets the existing.
*
* @return the existing
*/
@Test
public void getExisting() {
final ClassPK cpk = classPkService.getClassPk(Long.class);
......@@ -51,6 +62,9 @@ public class ClassPKServiceTest extends ServiceTest {
assertThat(cpk.getId(), equalTo(cpk2.getId()));
}
/**
* Test get class pk id.
*/
@Test
public void testGetClassPkId() {
final ClassPK cpk = classPkService.getClassPk(Long.class);
......@@ -61,6 +75,9 @@ public class ClassPKServiceTest extends ServiceTest {
assertThat(id, equalTo(cpk.getId()));
}
/**
* Test create class pk id.
*/
@Test
public void testCreateClassPkId() {
final Long id = classPkService.getClassPkId(Long.class.getName());
......@@ -73,7 +90,7 @@ public class ClassPKServiceTest extends ServiceTest {
}
/**
* Test that classPk is not created for classnames that don't exist
* Test that classPk is not created for classnames that don't exist.
*/
@Test
public void testNoCreateClassPkForCNFE() {
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -18,11 +18,39 @@ package org.genesys.blocks.auditlog.service;
import org.genesys.blocks.auditlog.model.ExampleAuditedEntity;
/**
* The Interface ExampleAuditedEntityService.
*/
public interface ExampleAuditedEntityService {
/**
* Gets the.
*
* @param id the id
* @return the example audited entity
*/
ExampleAuditedEntity get(Long id);
/**
* Save.
*
* @param entity the entity
* @return the example audited entity
*/
ExampleAuditedEntity save(ExampleAuditedEntity entity);
/**
* Delete.
*
* @param entity the entity
* @return the example audited entity
*/
ExampleAuditedEntity delete(ExampleAuditedEntity entity);
/**
* Test list.
*
* @return the example audited entity
*/
ExampleAuditedEntity testList();
}
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -23,6 +23,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* The Class ExampleAuditedEntityServiceImpl.
*/
@Service
@Transactional(readOnly = true)
public class ExampleAuditedEntityServiceImpl implements ExampleAuditedEntityService {
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -34,6 +34,9 @@ import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.scheduling.annotation.EnableScheduling;
/**
* The Class ApplicationConfig.
*/
@Configuration
@EnableScheduling
@EnableAspectJAutoProxy
......@@ -43,6 +46,11 @@ import org.springframework.scheduling.annotation.EnableScheduling;
@ComponentScan(basePackages = { "org.genesys.blocks.auditlog.service" })
public class ApplicationConfig {
/**
* Property placeholder configurer.
*
* @return the property placeholder configurer
*/
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
......@@ -58,6 +66,11 @@ public class ApplicationConfig {
return propertyPlaceholderConfigurer;
}
/**
* Cache manager.
*
* @return the cache manager
*/
@Bean
public CacheManager cacheManager() {
final SimpleCacheManager cacheManager = new SimpleCacheManager();
......@@ -65,11 +78,21 @@ public class ApplicationConfig {
return cacheManager;
}
/**
* Acl service.
*
* @return the custom acl service
*/
@Bean
public CustomAclService aclService() {
return new CustomAclServiceImpl();
}
/**
* Acl assigner aspect.
*
* @return the acl assigner aspect
*/
@Bean
public AclAssignerAspect aclAssignerAspect() {
return new AclAssignerAspect();
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -37,26 +37,39 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.transaction.annotation.Transactional;
/**
* The Class BaseTest.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@EnableAspectJAutoProxy
@ContextConfiguration(name = "root", classes = { ApplicationConfig.class })
public abstract class BaseTest {
/** The log. */
protected final Logger LOG = LoggerFactory.getLogger(getClass());
/** The Constant CURENT_YEAR. */
protected static final int CURENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);
/** The audit log repository. */
@Autowired
protected AuditLogRepository auditLogRepository;
/** The audit trail service. */
@Autowired
protected AuditTrailService auditTrailService;
/** The class pk repository. */
@Autowired
protected ClassPKRepository classPkRepository;
/** The class pk service. */
@Autowired
protected ClassPKService classPkService;
/**
* Cleanup.
*/
@AfterTransaction
@Transactional
public void cleanup() {
......@@ -67,22 +80,51 @@ public abstract class BaseTest {
classPkRepository.deleteAllInBatch();
}
/**
* Last audit log.
*
* @param entity the entity
* @return the audit log
*/
protected AuditLog lastAuditLog(final EntityId entity) {
return auditLogRepository.getLastAuditLog(entity);
}
/**
* Last audit log.
*
* @param entity the entity
* @param propertyName the property name
* @return the audit log
*/
protected AuditLog lastAuditLog(final EntityId entity, final String propertyName) {
return auditLogRepository.getLastAuditLog(entity, propertyName);
}
/**
* List audit logs.
*
* @param entity the entity
* @return the list
*/
protected List<AuditLog> listAuditLogs(final EntityId entity) {
return auditTrailService.listAuditLogs(entity);
}
/**
* Prints the audit logs.
*
* @param entity the entity
*/
protected void printAuditLogs(final EntityId entity) {
printAuditLogs(listAuditLogs(entity));
}
/**
* Prints the audit logs.
*
* @param auditLogs the audit logs
*/
protected void printAuditLogs(final List<AuditLog> auditLogs) {
for (final AuditLog auditLog : auditLogs) {
LOG.debug(auditLog.getId() + " " + auditLog.getAction() + " " + auditLog.getLogDate() + " " + auditLog.getClassPk().getClassname() + "<" + auditLog.getEntityId() + ">."
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -44,6 +44,9 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* The Class DatabaseConfig.
*/
@Configuration
@EnableJpaRepositories(basePackages = { "org.genesys.blocks.persistence", "org.genesys.blocks.security.persistence",
"org.genesys.blocks.auditlog.persistence" }, repositoryImplementationPostfix = "CustomImpl", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
......@@ -79,6 +82,12 @@ public class DatabaseConfig {
@Value("${db.pool.initialSize}")
private int dbPoolInitialSize;
/**
* Data source.
*
* @return the data source
* @throws Exception the exception
*/
@Bean(name = "dataSource")
public DataSource dataSource() throws Exception {
final DataSource dataSource = new DataSource();
......@@ -96,11 +105,23 @@ public class DatabaseConfig {
return dataSource;
}
/**
* Jdbc template.
*
* @param dataSource the data source
* @return the jdbc template
*/
@Bean
public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
/**
* Entity manager factory.
*
* @return the local container entity manager factory bean
* @throws Exception the exception
*/
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
final LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
......@@ -195,12 +216,24 @@ public class DatabaseConfig {
return bean;
}
/**
* Audit trail interceptor.
*
* @return the interceptor
*/
@Bean
public Interceptor auditTrailInterceptor() {
// Has to stay here so the code in JPA Hibernate setup above can pick it up!
return new AuditTrailInterceptor();
}
/**
* Jpa transaction manager.
*
* @param entityManagerFactory the entity manager factory
* @return the platform transaction manager
* @throws Exception the exception
*/
@Bean(name = "transactionManager")
public PlatformTransactionManager jpaTransactionManager(final EntityManagerFactory entityManagerFactory) throws Exception {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
......@@ -209,6 +242,11 @@ public class DatabaseConfig {
return transactionManager;
}
/**
* Hibernate exception translator.
*
* @return the persistence exception translator
*/
@Bean
public PersistenceExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
......@@ -221,6 +259,11 @@ public class DatabaseConfig {
return jpaProp;
}
/**
* Auditor aware.
*
* @return the spring security auditor aware
*/
@Bean
public SpringSecurityAuditorAware auditorAware() {
return new SpringSecurityAuditorAware();
......
/*
* Copyright 2017 Global Crop Diversity Trust
* Copyright 2018 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -23,12 +23,22 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional;