Commit 07263a42 authored by Matija Obreza's avatar Matija Obreza
Browse files

BatchRESTService test

parent 2aac80a8
......@@ -137,7 +137,7 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
}
public boolean sameAs(Taxonomy2 taxonomy) {
return taxonomy == null ? false : taxonomy.getId().equals(id);
return taxonomy == null ? false : id.equals(taxonomy.getId());
}
public Long getTaxGenus() {
......@@ -166,7 +166,7 @@ public class Taxonomy2 extends GlobalVersionedAuditedModel {
@Override
public String toString() {
return MessageFormat.format("Tax id={0} taxonName={1}", id, getTaxonName());
return MessageFormat.format("Tax id={0} taxonName={1} genus={2} sp={3} spauthor={4} subtaxa={5} subtauthor={6}", id, getTaxonName(), genus, species, spAuthor, subtaxa, subtAuthor);
}
}
......@@ -67,8 +67,11 @@ public class GenesysLowlevelRepositoryImpl implements GenesysLowlevelRepository
@Transactional(readOnly = false)
public void updateInstituteAccessionCount(long instituteId) {
LOG.info("Updating institute accession count for instituteId=" + instituteId);
// int count = jdbcTemplate
// .update("UPDATE faoinstitute FI inner join (select instituteId, count(*) X from accession where instituteId= ? group by instituteId) A on A.instituteId=FI.id set FI.accessionCount=A.X where FI.id = ?;",
// instituteId, instituteId);
int count = jdbcTemplate
.update("UPDATE faoinstitute FI inner join (select instituteId, count(*) X from accession where instituteId= ? group by instituteId) A on A.instituteId=FI.id set FI.accessionCount=A.X where FI.id = ?;",
.update("UPDATE faoinstitute SET accessionCount = (select count(*) from accession where instituteId=?) where id = ?;",
instituteId, instituteId);
LOG.info("Institute accession counts updated: " + count);
}
......
......@@ -123,7 +123,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
accession.setInstitute(institute);
accession.setAccessionName(dataJson.acceNumb);
if (accnJson.get("genus") == null || accnJson.get("newGenus") == null) {
if (accnJson.get("genus") == null && accnJson.get("newGenus") == null) {
throw new RESTApiException("Cannot create new accession without specifying genus");
}
......@@ -444,12 +444,11 @@ public class BatchRESTServiceImpl implements BatchRESTService {
boolean updated = false;
// Do not persist this one, temporary use
Taxonomy2 current = accession.getTaxonomy();
if (current == null) {
current = new Taxonomy2();
}
Taxonomy2 current = new Taxonomy2();
Taxonomy2 taxonomy = accession.getTaxonomy();
// Load JSON values into "current"
if (taxonomy!=null)
current.setId(taxonomy.getId());
current.setGenus(stringIfProvided(accnJson.get("genus"), current.getGenus()));
current.setGenus(stringIfProvided(accnJson.get("newGenus"), current.getGenus()));
current.setSpecies(stringIfProvided(accnJson.get("species"), current.getSpecies()));
......@@ -457,10 +456,10 @@ public class BatchRESTServiceImpl implements BatchRESTService {
current.setSubtaxa(stringIfProvided(accnJson.get("subtaxa"), current.getSubtaxa()));
current.setSubtAuthor(stringIfProvided(accnJson.get("subtauthor"), current.getSubtAuthor()));
Taxonomy2 ensuredTaxonomy = null;
if (current.getId() == null
|| !((ensuredTaxonomy = taxonomyService.ensureTaxonomy2(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor())).sameAs(current))) {
Taxonomy2 ensuredTaxonomy = taxonomyService.ensureTaxonomy2(current.getGenus(), current.getSpecies(), current.getSpAuthor(),
current.getSubtaxa(), current.getSubtAuthor());
if (!(ensuredTaxonomy.sameAs(current))) {
accession.setTaxonomy(ensuredTaxonomy);
accession.setTaxGenus(ensuredTaxonomy.getTaxGenus());
......
package org.genesys2.server.test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.exception.VelocityException;
import org.genesys2.server.aspect.AsAdminAspect;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.AclService;
import org.genesys2.server.service.BatchRESTService;
import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.HtmlSanitizer;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.OrganizationService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.UserService;
import org.genesys2.server.service.impl.AclServiceImpl;
import org.genesys2.server.service.impl.BatchRESTServiceImpl;
import org.genesys2.server.service.impl.ContentServiceImpl;
import org.genesys2.server.service.impl.GenesysServiceImpl;
import org.genesys2.server.service.impl.GeoServiceImpl;
import org.genesys2.server.service.impl.InstituteServiceImpl;
import org.genesys2.server.service.impl.OWASPSanitizer;
import org.genesys2.server.service.impl.OrganizationServiceImpl;
import org.genesys2.server.service.impl.RESTApiException;
import org.genesys2.server.service.impl.TaxonomyServiceImpl;
import org.genesys2.server.service.impl.UserServiceImpl;
import org.genesys2.server.servlet.controller.rest.model.AccessionJson;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.ui.velocity.VelocityEngineFactoryBean;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = BatchRESTServiceTest.Config.class, initializers = PropertyPlacholderInitializer.class)
public class BatchRESTServiceTest {
private ObjectMapper mapper = new ObjectMapper();
@Import(JpaDataConfig.class)
@ComponentScan(basePackages = { "org.genesys2.server.persistence.domain" })
public static class Config {
@Bean
public AsAdminAspect asAdminAspect() {
return new AsAdminAspect();
}
@Bean
public UserService userService() {
return new UserServiceImpl();
}
@Bean
public AclService aclService() {
return new AclServiceImpl();
}
@Bean
public TaxonomyService taxonomyService() {
return new TaxonomyServiceImpl();
}
@Bean
public BatchRESTService batchRESTService() {
return new BatchRESTServiceImpl();
}
@Bean
public GenesysService genesysService() {
return new GenesysServiceImpl();
}
@Bean
public CacheManager cacheManager() {
return new NoOpCacheManager();
}
@Bean
public HtmlSanitizer htmlSanitizer() {
return new OWASPSanitizer();
}
@Bean
public GeoService geoService() {
return new GeoServiceImpl();
}
@Bean
public ContentService contentService() {
return new ContentServiceImpl();
}
@Bean
public VelocityEngine velocityEngine() throws VelocityException, IOException {
VelocityEngineFactoryBean vf = new VelocityEngineFactoryBean();
return vf.createVelocityEngine();
}
@Bean
public OrganizationService organizationService() {
return new OrganizationServiceImpl();
}
@Bean
public InstituteService instituteService() {
return new InstituteServiceImpl();
}
}
@Autowired
private BatchRESTService batchRESTService;
@Autowired
private GenesysService genesysService;
@Autowired
private TaxonomyService taxonomyService;
@Autowired
private InstituteService instituteService;
@Before
public void setup() {
System.err.println("Setting up");
Collection<FaoInstitute> institutes = new ArrayList<FaoInstitute>();
for (String instCode : new String[] { "SYR002", "NGA039" }) {
FaoInstitute institute = new FaoInstitute();
institute.setFullName(instCode + " institute");
institute.setCode(instCode);
institute.setUniqueAcceNumbs(true);
institutes.add(institute);
}
instituteService.update(institutes);
}
@Test
public void testNewAccession() {
String instCode = "SYR002";
FaoInstitute institute = instituteService.getInstitute(instCode);
assertTrue("institute is null", institute != null);
System.err.println(institute);
Map<AccessionJson, ObjectNode> batch = new HashMap<AccessionJson, ObjectNode>();
AccessionJson dataJson = new AccessionJson();
dataJson.acceNumb = "AC 1";
dataJson.instCode = instCode;
ObjectNode json = mapper.createObjectNode();
json.put("genus", "Hordeum");
batch.put(dataJson, json);
try {
batchRESTService.upsertAccessionData(institute, batch);
} catch (RESTApiException e) {
fail(e.getMessage());
}
Accession accession = genesysService.getAccession(instCode, "AC 1");
assertTrue(accession.getId() != null);
assertTrue(accession.getInstituteCode().equals(instCode));
assertTrue(accession.getInstitute().getId().equals(institute.getId()));
assertTrue(accession.getTaxonomy() != null);
Taxonomy2 tax = accession.getTaxonomy();
System.err.println(tax);
System.err.println(accession.getTaxGenus());
System.err.println(accession.getTaxSpecies());
// Modify taxonomy
json.put("genus", "Hordeum");
json.put("species", "vulgare");
json.put("spauthor", "L.");
json.put("subtaxa", "some subtaxa");
json.put("subtauthor", "Subtauthor");
try {
batchRESTService.upsertAccessionData(institute, batch);
} catch (RESTApiException e) {
fail(e.getMessage());
}
// reload
accession = genesysService.getAccession(instCode, "AC 1");
Taxonomy2 tax2 = accession.getTaxonomy();
System.err.println(tax2);
assertFalse(tax2.getId().equals(tax.getId()));
System.err.println(accession.getTaxGenus());
System.err.println(accession.getTaxSpecies());
assertFalse(accession.getTaxGenus() == accession.getTaxSpecies());
// test nothing
try {
System.err.println("NO UPDATE!");
batchRESTService.upsertAccessionData(institute, batch);
} catch (RESTApiException e) {
fail(e.getMessage());
}
}
}
package org.genesys2.server.test;
import java.util.Properties;
import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import org.junit.Ignore;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -16,21 +21,21 @@ 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 javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;
import java.util.Properties;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@Ignore
@Configuration
@PropertySource("classpath:/spring/spring.properties")
@EnableJpaRepositories(
basePackages = "org.genesys2.server.persistence",
repositoryImplementationPostfix = "CustomImpl",
basePackages = { "org.genesys2.server.persistence.acl", "org.genesys2.server.persistence.domain" },
repositoryImplementationPostfix = "Impl",
entityManagerFactoryRef = "entityManagerFactory",
transactionManagerRef = "transactionManager"
)
@EnableTransactionManagement
@TransactionConfiguration(transactionManager="transactionManager")
public class JpaDataConfig {
@Autowired
private Environment env;
......
......@@ -102,4 +102,28 @@ public class TaxonomyEnsureTest {
fail("This should not have any exceptions!");
}
}
@Test
public void testFullTaxonomy() {
try {
Taxonomy2 tax = taxonomyService.ensureTaxonomy2("Aaaaa", "species", "spauthor", "subtaxa", "subtauthor");
System.out.println(tax);
assertTrue("id missing", tax.getId() != null);
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()));
Taxonomy2 taxGenus = taxonomyService.get(tax.getTaxGenus());
System.out.println(taxGenus);
Taxonomy2 taxSpecies = taxonomyService.get(tax.getTaxSpecies());
System.out.println(taxSpecies);
} catch (Exception e) {
fail("This should not have any exceptions!");
}
}
}
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