Commit 0127df09 authored by Matija Obreza's avatar Matija Obreza

Fixed bug affecting Taxonomy2#taxSpecies

parent f79f60fd
......@@ -41,7 +41,7 @@ import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
@Entity
// Add index on all fields
@Table(name = "taxonomy2", uniqueConstraints = { @UniqueConstraint(columnNames = { "genus", "species", "spAuthor", "subtaxa", "subtAuthor" }) })
@JsonIgnoreProperties(ignoreUnknown=true)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Taxonomy2 extends GlobalVersionedAuditedModel {
private static final long serialVersionUID = 8881324404490162933L;
......@@ -70,7 +70,7 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
private Long taxSpecies;
@JsonIgnore
@JsonIgnore
@OneToMany(cascade = {}, fetch = FetchType.LAZY, mappedBy = "taxonomy")
private List<CropTaxonomy> cropTaxonomies;
......
......@@ -147,15 +147,18 @@ public class TaxonomyServiceImpl implements TaxonomyService {
// LOCK
if (taxonomyUpdateLock.tryLock(10, TimeUnit.SECONDS)) {
try {
if (!StringUtils.equals(species, "sp.") && !(subtaxa.equals("") && spAuthor.equals("") && subtAuthor.equals(""))) {
// Direct species
if (subtaxa.equals("") && spAuthor.equals("") && subtAuthor.equals("")) {
if (StringUtils.equals(species, "sp.")) {
// Self
} else {
final Taxonomy2 genusTaxa = internalEnsure(genus, "sp.", "", "", "");
taxGenusId = genusTaxa.getId();
}
} else {
final Taxonomy2 speciesTaxa = internalEnsure(genus, species, "", "", "");
taxSpeciesId = speciesTaxa.getId();
taxGenusId = speciesTaxa.getTaxGenus();
} else if (!StringUtils.equals(species, "sp.") && subtaxa.equals("") && spAuthor.equals("") && subtAuthor.equals("")) {
final Taxonomy2 genusTaxa = internalEnsure(genus, "sp.", "", "", "");
taxGenusId = genusTaxa.getId();
taxSpeciesId = genusTaxa.getId();
}
Taxonomy2 taxonomy = null;
......
/**
* Copyright 2014 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.genesys2.spring.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import com.hazelcast.config.Config;
import com.hazelcast.config.EvictionPolicy;
import com.hazelcast.config.ExecutorConfig;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.config.JoinConfig;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.MaxSizeConfig;
import com.hazelcast.config.MaxSizeConfig.MaxSizePolicy;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.config.QueueConfig;
import com.hazelcast.config.TcpIpConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ManagedContext;
@Configuration
@Profile({ "cacher" })
public class HazelcastConfigCacher extends HazelcastConfig {
@Bean
public HazelcastInstance hazelcast(ManagedContext managedContext) {
Config cfg = new Config();
cfg.setManagedContext(managedContext);
cfg.setInstanceName(instanceName);
GroupConfig groupConfig = cfg.getGroupConfig();
groupConfig.setName(name);
groupConfig.setPassword(password);
cfg.setProperty("hazelcast.merge.first.run.delay.seconds", "5");
cfg.setProperty("hazelcast.merge.next.run.delay.seconds", "5");
cfg.setProperty("hazelcast.logging.type", "log4j");
cfg.setProperty("hazelcast.icmp.enabled", "true");
NetworkConfig network = cfg.getNetworkConfig();
network.setPort(hazelPort);
network.setPortAutoIncrement(true);
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.setConnectionTimeoutSeconds(20);
// See if there's a local HZ running
tcpIpConfig.addMember("127.0.0.1:5702");
MapConfig defaultMapConfig = new MapConfig();
defaultMapConfig.setName("default");
// defaultMapConfig.setTimeToLiveSeconds(0);
defaultMapConfig.setEvictionPolicy(EvictionPolicy.LFU);
// defaultMapConfig.setMaxIdleSeconds();
MaxSizeConfig defaultMaxSizeConfig = new MaxSizeConfig();
defaultMaxSizeConfig.setSize(2000);
defaultMaxSizeConfig.setMaxSizePolicy(MaxSizePolicy.PER_NODE);
defaultMapConfig.setMaxSizeConfig(defaultMaxSizeConfig);
cfg.addMapConfig(defaultMapConfig);
MapConfig tileserverMapConfig = new MapConfig();
tileserverMapConfig.setName("tileserver");
tileserverMapConfig.setTimeToLiveSeconds(tileserverTTL);
tileserverMapConfig.setEvictionPolicy(tileserverEvictionPolicy);
tileserverMapConfig.setMaxIdleSeconds(tileserverMaxIdle);
MaxSizeConfig tileserverMaxSizeConfig = new MaxSizeConfig();
tileserverMaxSizeConfig.setSize(tileserverMaxSize);
tileserverMaxSizeConfig.setMaxSizePolicy(MaxSizePolicy.PER_NODE);
tileserverMapConfig.setMaxSizeConfig(tileserverMaxSizeConfig);
cfg.addMapConfig(tileserverMapConfig);
ExecutorConfig execConfig = new ExecutorConfig();
execConfig.setName("hazel-exec");
execConfig.setPoolSize(4);
execConfig.setQueueCapacity(2);
execConfig.setStatisticsEnabled(true);
cfg.addExecutorConfig(execConfig);
QueueConfig queueConfig = new QueueConfig();
queueConfig.setName("elasticsearchQueue");
queueConfig.setMaxSize(100);
cfg.addQueueConfig(queueConfig);
HazelcastInstance instance = Hazelcast.newHazelcastInstance(cfg);
return instance;
}
}
......@@ -36,7 +36,7 @@ import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.ManagedContext;
@Configuration
@Profile({ "dev", "cacher" })
@Profile({ "dev" })
public class HazelcastConfigDev extends HazelcastConfig {
@Bean
......@@ -55,16 +55,12 @@ public class HazelcastConfigDev extends HazelcastConfig {
cfg.setProperty("hazelcast.icmp.enabled", "true");
NetworkConfig network = cfg.getNetworkConfig();
network.setPort(hazelPort);
network.setPortAutoIncrement(false);
JoinConfig join = network.getJoin();
join.getMulticastConfig().setEnabled(false);
TcpIpConfig tcpIpConfig = join.getTcpIpConfig();
tcpIpConfig.setEnabled(true);
tcpIpConfig.setEnabled(false);
tcpIpConfig.setConnectionTimeoutSeconds(20);
// See if there's a local HZ running
tcpIpConfig.addMember("127.0.0.1:5702");
MapConfig defaultMapConfig = new MapConfig();
defaultMapConfig.setName("default");
......
/**
* Copyright 2014 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.genesys2.server.test;
import java.util.ArrayList;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.hibernate.jpa.HibernatePersistenceProvider;
import org.junit.Ignore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
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.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Ignore
@Configuration
@PropertySource("classpath:/spring/spring.properties")
@EnableJpaRepositories(basePackages = { "org.genesys2.server.persistence.acl", "org.genesys2.server.persistence.domain" }, repositoryImplementationPostfix = "CustomImpl", entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager")
@EnableTransactionManagement
@TransactionConfiguration(defaultRollback = false, transactionManager = "transactionManager")
public class JpaNoCacheDataConfig {
@Autowired
private Environment env;
@Bean(name = "dataSource")
public DataSource dataSource() throws Exception {
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(env.getProperty("db.driverClassName"));
dataSource.setUrl(env.getProperty("db.url"));
dataSource.setUsername(env.getProperty("db.username"));
dataSource.setPassword(env.getProperty("db.password"));
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(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("org.genesys2.server.model");
bean.setPersistenceProvider(new HibernatePersistenceProvider());
final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
jpaVendorAdapter.setShowSql(env.getProperty("db.showSql", Boolean.TYPE, true));
jpaVendorAdapter.setGenerateDdl(env.getProperty("db.hbm2ddl", Boolean.TYPE, true));
Properties jpaProperties = jpaProperties();
for (Object key : jpaProperties.keySet()) {
System.err.println("JPA: " + key + " = " + jpaProperties.get(key));
}
bean.setJpaProperties(jpaProperties);
bean.setJpaVendorAdapter(jpaVendorAdapter);
return bean;
}
@Bean(name = "transactionManager")
public PlatformTransactionManager jpaTransactionManager(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 {
Properties jpaProp = new Properties();
jpaProp.load(getClass().getResourceAsStream("/spring/hibernate.properties"));
for (Object key : new ArrayList<Object>(jpaProp.keySet())) {
if (((String) key).startsWith("hibernate.cache.") || ("javax.persistence.sharedCache.mode".equals(key))) {
System.err.println("Removing: " + key);
jpaProp.remove(key);
}
}
jpaProp.setProperty("hibernate.cache.use_second_level_cache", "false");
return jpaProp;
}
}
......@@ -16,26 +16,34 @@
package org.genesys2.server.test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.apache.commons.lang.StringUtils;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.service.CropService;
import org.genesys2.server.service.HtmlSanitizer;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.impl.CropServiceImpl;
import org.genesys2.server.service.impl.OWASPSanitizer;
import org.genesys2.server.service.impl.TaxonomyServiceImpl;
import org.genesys2.spring.config.SpringCacheConfig;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@ActiveProfiles("dev")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TaxonomyEnsureTest.Config.class, initializers = PropertyPlacholderInitializer.class)
public class TaxonomyEnsureTest {
@Import(JpaDataConfig.class)
@Import({ JpaNoCacheDataConfig.class, SpringCacheConfig.class })
public static class Config {
@Bean
......@@ -43,6 +51,16 @@ public class TaxonomyEnsureTest {
return new TaxonomyServiceImpl();
}
@Bean
public CropService cropService() {
return new CropServiceImpl();
}
@Bean
public HtmlSanitizer htmlSanitizer() {
return new OWASPSanitizer();
}
}
// use it without @Scheduled
......@@ -72,9 +90,9 @@ public class TaxonomyEnsureTest {
assertTrue("id missing", tax.getId() != null);
assertTrue("taxGenus should not be null", tax.getTaxGenus() != null);
assertTrue("taxSpecies should not be null", tax.getTaxSpecies() != null);
assertTrue("taxGenus should not be this", !tax.getTaxGenus().equals(tax.getId()));
assertTrue("taxSpecies should not be this", !tax.getTaxSpecies().equals(tax.getId()));
assertTrue("taxSpecies should not match taxGenus", !tax.getTaxSpecies().equals(tax.getTaxGenus()));
assertFalse("taxGenus should not be this", tax.getTaxGenus().equals(tax.getId()));
assertFalse("taxSpecies should not be this", tax.getTaxSpecies().equals(tax.getId()));
assertFalse("taxSpecies should not match taxGenus", tax.getTaxSpecies().equals(tax.getTaxGenus()));
final Taxonomy2 taxGenus = taxonomyService.get(tax.getTaxGenus());
assertTrue("taxGenus should have species=sp. and not " + taxGenus.getSpecies(), taxGenus.getSpecies().equals("sp."));
......@@ -105,7 +123,7 @@ public class TaxonomyEnsureTest {
@Test
public void testSpSpecies() {
try {
final Taxonomy2 tax = taxonomyService.ensureTaxonomy2("Aaaaa", "sp", null, null, null);
final Taxonomy2 tax = taxonomyService.ensureTaxonomy2("Aaaaa", "sp.", null, null, null);
assertTrue("id missing", tax.getId() != null);
assertTrue("species must be sp.", tax.getSpecies().equals("sp."));
assertTrue("taxGenus should not be null", tax.getTaxGenus() != null);
......@@ -118,6 +136,22 @@ public class TaxonomyEnsureTest {
}
}
@Test
public void testSpecies() {
try {
final Taxonomy2 tax = taxonomyService.ensureTaxonomy2("Aaaaa", "species", null, null, null);
assertTrue("id missing", tax.getId() != null);
assertTrue("species must be species", tax.getSpecies().equals("species"));
assertTrue("taxGenus should not be null", tax.getTaxGenus() != null);
assertTrue("taxSpecies should not be null", tax.getTaxSpecies() != null);
assertFalse("taxGenus must not be this", tax.getTaxGenus().equals(tax.getId()));
assertTrue("taxSpecies must be this", tax.getTaxSpecies().equals(tax.getId()));
assertFalse("taxSpecies should not match taxGenus", tax.getTaxSpecies().equals(tax.getTaxGenus()));
} catch (final Exception e) {
fail("This should not have any exceptions!");
}
}
@Test
public void testFullTaxonomy() {
try {
......@@ -127,9 +161,9 @@ public class TaxonomyEnsureTest {
assertTrue("species must be sp.", tax.getSpecies().equals("species"));
assertTrue("taxGenus should not be null", tax.getTaxGenus() != null);
assertTrue("taxSpecies should not be null", tax.getTaxSpecies() != null);
assertTrue("taxGenus must not be this", !tax.getTaxGenus().equals(tax.getId()));
assertTrue("taxSpecies must not be this", !tax.getTaxSpecies().equals(tax.getId()));
assertTrue("taxSpecies should not match taxGenus", !tax.getTaxSpecies().equals(tax.getTaxGenus()));
assertFalse("taxGenus must not be this", tax.getTaxGenus().equals(tax.getId()));
assertFalse("taxSpecies must not be this", tax.getTaxSpecies().equals(tax.getId()));
assertFalse("taxSpecies should not match taxGenus", tax.getTaxSpecies().equals(tax.getTaxGenus()));
final Taxonomy2 taxGenus = taxonomyService.get(tax.getTaxGenus());
System.out.println(taxGenus);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment