Commit 09f1327a authored by Matija Obreza's avatar Matija Obreza

Merge branch '25-add-unit-tests-for-core' into 'master'

Resolve "Add Unit tests for core"

Closes #25

See merge request genesys-pgr/application-blocks!25
parents 9b573166 28871709
......@@ -19,11 +19,15 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.target=System.out
log4j.appender.stdout.encoding=UTF-8
log4j.appender.stdout.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %t %5p %c{1.}:%L - %m%n
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %t %5p %c:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=error, stdout
log4j.category.org.genesys.blocks=debug
#log4j.category.org.genesys.blocks.auditlog=trace
\ No newline at end of file
#log4j.category.org.genesys.blocks=debug
#log4j.category.org.genesys.blocks.auditlog=trace
## Shut up hsql
log4j.category.org.hibernate.tool.hbm2ddl.SchemaExport=fatal
log4j.category.org.hibernate.engine.jdbc.spi.SqlExceptionHelper=fatal
......@@ -75,6 +75,67 @@
</build>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
<version>7.0.42</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>${hsqldb.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring.data.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
<version>${querydsl.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>${hibernate.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
......@@ -40,7 +40,7 @@ public class BasicModel implements EntityId, Serializable {
@JsonView({ JsonViews.Update.class, JsonViews.Protected.class })
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false, length = 20)
@Column(name = "id", unique = true, nullable = false)
private Long id = InMemoryIdGenerator.nextId();
/*
......
......@@ -40,19 +40,19 @@ public class ClassPK implements Serializable {
private static final long serialVersionUID = 4850290657706341853L;
/** The id. */
@JsonView(JsonViews.Internal.class)
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", nullable = false, length = 20)
@JsonView(JsonViews.Internal.class)
protected Long id;
/** The classname. */
@JsonView(JsonViews.Public.class)
@Column(length = 200, updatable = false, unique = true, nullable = false)
@JsonView(JsonViews.Public.class)
protected String classname;
/** The short name. */
@Column(length = 50, unique = true)
@Column(length = DefaultModelConstants.SHORTNAME_LENGTH, unique = true)
private String shortName;
/**
......
......@@ -20,39 +20,12 @@ package org.genesys.blocks.model;
*
* @author Matija Obreza
*/
public interface ModelConstants {
/** The businesscard position len. */
int BUSINESSCARD_POSITION_LEN = 100;
/** The person name len. */
int PERSON_NAME_LEN = 200;
/** The address len. */
int ADDRESS_LEN = 400;
/** The countrycode len. */
int COUNTRYCODE_LEN = 3;
/** The emailaddress len. */
int EMAILADDRESS_LEN = 100;
/** The phonenumber len. */
int PHONENUMBER_LEN = 50;
/** The currency len. */
int CURRENCY_LEN = 3;
/** The custom label len. */
int CUSTOM_LABEL_LEN = 100;
/** The contact detail len. */
int CONTACT_DETAIL_LEN = 200;
/** The subject length. */
int SUBJECT_LENGTH = 200;
public interface DefaultModelConstants {
/** The title length. */
int TITLE_LENGTH = 200;
/** The shortname length. */
int SHORTNAME_LENGTH = 50;
}
......@@ -27,7 +27,7 @@ import com.fasterxml.jackson.annotation.JsonView;
* The Class UuidEntity.
*/
@MappedSuperclass
public abstract class UuidEntity extends AuditedVersionedModel implements IdUUID {
public abstract class UuidModel extends AuditedVersionedModel implements IdUUID {
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 2639238742778129554L;
......@@ -38,10 +38,10 @@ public abstract class UuidEntity extends AuditedVersionedModel implements IdUUID
protected UUID uuid;
/**
* Prepersist.
* {@inheritDoc}
*/
@PrePersist
private void prepersist() {
private void prepersistUuidModel() {
if (this.uuid == null) {
this.uuid = UUID.randomUUID();
}
......
......@@ -17,6 +17,7 @@ package org.genesys.blocks.model;
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.Version;
import com.fasterxml.jackson.annotation.JsonView;
......@@ -27,6 +28,8 @@ import com.fasterxml.jackson.annotation.JsonView;
@MappedSuperclass
public abstract class VersionedModel extends BasicModel implements Activatable {
static final boolean DEFAULT_ACTIVE_VALUE = true;
/** The Constant serialVersionUID. */
private static final long serialVersionUID = 6486704708017489425L;
......@@ -38,7 +41,15 @@ public abstract class VersionedModel extends BasicModel implements Activatable {
/** Active by default!. */
@Column(nullable = false)
protected boolean active = true;
protected boolean active = DEFAULT_ACTIVE_VALUE;
/**
* First persisted version is 1
*/
@PrePersist
private void prepersistVersionedModel() {
version = 1;
}
/**
* Gets the version.
......
......@@ -40,7 +40,7 @@ public abstract class VersionedModelWithoutId implements Serializable, Activatab
/** Active by default!. */
@Column(nullable = false)
protected boolean active = true;
protected boolean active = VersionedModel.DEFAULT_ACTIVE_VALUE;
/**
* Gets the version.
......
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.config;
import java.util.Arrays;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.Import;
import org.springframework.core.io.ClassPathResource;
@Configuration
@EnableAspectJAutoProxy
@Import({ DatabaseConfig.class })
@ComponentScan(basePackages = { })
public class ApplicationConfig {
@Bean
public static PropertyPlaceholderConfigurer propertyPlaceholderConfigurer() {
final PropertyPlaceholderConfigurer propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
propertyPlaceholderConfigurer.setIgnoreResourceNotFound(true);
propertyPlaceholderConfigurer.setFileEncoding("utf-8");
propertyPlaceholderConfigurer.setLocations(new ClassPathResource("application.properties"));
return propertyPlaceholderConfigurer;
}
@Bean
public CacheManager cacheManager() {
final SimpleCacheManager cacheManager = new SimpleCacheManager();
cacheManager.setCaches(Arrays.asList(new ConcurrentMapCache("default")));
return cacheManager;
}
}
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.config;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.HibernateExceptionTranslator;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
/**
* @author Matija Obreza
* @author Maxym Borodenko
*/
@Configuration
@EnableJpaRepositories(basePackages = { "org.genesys.blocks.persistence" }, repositoryImplementationPostfix = "CustomImpl", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
@EnableTransactionManagement
@EnableJpaAuditing(modifyOnCreate = true)
public class DatabaseConfig {
private static final String[] JPA_PACKAGES = { "org.genesys.blocks.model" };
@Value("${db.url}")
private String dbUrl;
@Value("${db.driverClassName}")
private String dbDriverClassName;
@Value("${db.username}")
private String dbUsername;
@Value("${db.password}")
private String dbPassword;
@Value("${db.showSql}")
private boolean dbShowSql;
@Value("${db.updateSchema}")
private boolean dbGenerateDdl;
@Value("${db.hibernate.dialect}")
private String hibernateDialect;
@Value("${db.pool.maxActive}")
private int dbPoolMaxActive;
@Value("${db.pool.initialSize}")
private int dbPoolInitialSize;
@Bean(name = "dataSource")
public DataSource dataSource() throws Exception {
final DataSource dataSource = new DataSource();
dataSource.setUrl(dbUrl);
dataSource.setDriverClassName(dbDriverClassName);
dataSource.setUsername(dbUsername);
dataSource.setPassword(dbPassword);
dataSource.setValidationQuery("SELECT 1");
dataSource.setTestWhileIdle(true);
dataSource.setTestOnBorrow(true);
dataSource.setMaxActive(dbPoolMaxActive);
dataSource.setMaxIdle(dbPoolInitialSize);
dataSource.setInitialSize(dbPoolInitialSize);
dataSource.setMinIdle(dbPoolInitialSize);
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(final DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean(name = "entityManagerFactory")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
final LocalContainerEntityManagerFactoryBean bean = new LocalContainerEntityManagerFactoryBean();
bean.setDataSource(dataSource());
bean.setPersistenceUnitName("spring-jpa");
bean.setPackagesToScan(JPA_PACKAGES);
bean.setPersistenceProvider(new HibernatePersistenceProvider());
final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(dbShowSql);
jpaVendorAdapter.setGenerateDdl(dbGenerateDdl);
final Properties jpaProperties = jpaProperties();
bean.setJpaProperties(jpaProperties);
bean.setJpaVendorAdapter(jpaVendorAdapter);
return bean;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager jpaTransactionManager(final EntityManagerFactory entityManagerFactory) throws Exception {
final JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setDataSource(dataSource());
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
@Bean
public PersistenceExceptionTranslator hibernateExceptionTranslator() {
return new HibernateExceptionTranslator();
}
private Properties jpaProperties() throws Exception {
final Properties jpaProp = new Properties();
jpaProp.load(getClass().getResourceAsStream("/hibernate.properties"));
jpaProp.put("hibernate.dialect", hibernateDialect);
return jpaProp;
}
}
\ No newline at end of file
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.model;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class UuidEntity extends UuidModel {
private static final long serialVersionUID = 1L;
@Column(nullable = false)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.model;
import javax.persistence.Column;
import javax.persistence.Entity;
@Entity
public class VersionedEntity extends VersionedModel {
private static final long serialVersionUID = 1L;
@Column(nullable = false)
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.persistence;
import org.genesys.blocks.model.UuidEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UuidEntityRepository extends JpaRepository<UuidEntity, Long> {
}
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.persistence;
import org.genesys.blocks.model.VersionedEntity;
import org.springframework.data.jpa.repository.JpaRepository;
public interface VersionedEntityRepository extends JpaRepository<VersionedEntity, Long> {
}
/*
* Copyright 2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys.blocks.tests;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import org.genesys.blocks.config.ApplicationConfig;
import org.genesys.blocks.persistence.UuidEntityRepository;
import org.genesys.blocks.persistence.VersionedEntityRepository;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Matija Obreza
* @author Maxym Borodenko
*/
@RunWith(SpringJUnit4ClassRunner.class)
@EnableAspectJAutoProxy
@ContextConfiguration(name = "root", classes = { ApplicationConfig.class })
public abstract class BaseTest {
protected final Logger LOG = LoggerFactory.getLogger(getClass());