Commit 29c33e57 authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza
Browse files

Created mock for TraitService. Change UserSerializer

parent 4ba027e8
......@@ -8,6 +8,7 @@ import org.genesys2.server.persistence.domain.MethodRepository;
import org.genesys2.server.service.impl.DirectMysqlQuery;
import org.genesys2.server.service.impl.FilterHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.BadSqlGrammarException;
......@@ -23,6 +24,7 @@ import java.sql.*;
import java.util.List;
@Repository
//@Qualifier("genesysLowlevelRepositoryMock")
public class GenesysLowlevelRepositoryMock implements GenesysLowlevelRepository {
public static final Log LOG = LogFactory.getLog(GenesysLowlevelRepositoryMock.class);
......
package org.genesys2.server.persistence.domain.mock;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.*;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.persistence.domain.*;
import org.genesys2.server.security.AuthUserDetails;
import org.genesys2.server.service.AclService;
import org.genesys2.server.service.HtmlSanitizer;
import org.genesys2.server.service.TraitService;
import org.genesys2.server.service.impl.GenesysServiceImpl;
import org.genesys2.spring.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.acls.domain.BasePermission;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional(readOnly = true)
public class TraitServiceMock implements TraitService{
public static final Log LOG = LogFactory.getLog(GenesysServiceImpl.class);
@Autowired
@Qualifier("genesysLowlevelRepositoryMock")
private GenesysLowlevelRepository genesysLowlevelRepository;
@Autowired
private AclService aclService;
@Autowired
private HtmlSanitizer htmlSanitizer;
@Autowired
private TraitValueRepository traitValueRepository;
@Autowired
private ParameterCategoryRepository parameterCategoryRepository;
@Autowired
private ParameterRepository parameterRepository;
@Autowired
private MethodRepository methodRepository;
@Autowired
private AccessionTraitRepository accessionTraitRepository;
@Autowired
private MetadataRepository metadataRepository;
@Autowired
private MetadataMethodRepository metadataMethodRepository;
@Override
public List<ParameterCategory> listCategories() {
return parameterCategoryRepository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC, "id")));
}
@Override
public List<Parameter> listTraits() {
return parameterRepository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC, "id")));
}
@Override
public Page<Parameter> listTraits(Pageable pageable) {
return parameterRepository.findAll(pageable);
}
@Override
public Page<Parameter> listTraits(Crop crop, Pageable pageable) {
return parameterRepository.findByCrop(crop, pageable);
}
@Override
public Parameter getTrait(long traitId) {
return parameterRepository.findOne(traitId);
}
@Override
public List<Method> getTraitMethods(Parameter trait) {
return methodRepository.findByParameter(trait);
}
@Override
public Method getMethod(long methodId) {
return methodRepository.findOne(methodId);
}
@Override
public List<Method> listMethods() {
return methodRepository.findAll(new Sort(new Sort.Order(Sort.Direction.ASC, "id")));
}
@Override
public Page<Method> listMethods(Pageable pageable) {
return methodRepository.findAll(pageable);
}
@Override
public List<Metadata> listMetadataByMethod(Method method) {
return metadataMethodRepository.listMetadataByMethodId(method.getId());
}
@Override
public List<Method> listMethods(AccessionId accession) {
final List<Long> x = accessionTraitRepository.listMethodIds(accession);
return x.size() == 0 ? null : methodRepository.findByIds(x);
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public ParameterCategory addCategory(String name, String i18n) {
final ParameterCategory category = new ParameterCategory();
category.setName(name);
category.setNameL(i18n);
parameterCategoryRepository.save(category);
return category;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
public ParameterCategory getCategory(String name) {
final ParameterCategory category = parameterCategoryRepository.findByName(name);
return category;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public Parameter addParameter(String rdfUri, Crop crop, String category, String title, String i18n) {
final Parameter parameter = new Parameter();
parameter.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null));
parameter.setCrop(crop);
// In an ideal world, we should search for category by URI, not name...
final ParameterCategory parameterCategory = parameterCategoryRepository.findByName(category);
parameter.setCategory(parameterCategory);
parameter.setTitle(title);
parameter.setI18n(StringUtils.defaultIfBlank(i18n, null));
parameterRepository.save(parameter);
return parameter;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
public Parameter getParameter(String rdfUri) {
if (rdfUri == null || rdfUri.isEmpty()) {
return null;
}
final Parameter parameter = parameterRepository.findByRdfUri(rdfUri); // assumes
// that
// crop
// x
// title
// is
// unique(?)
return parameter;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
public Parameter getParameter(Crop crop, String title) {
final Parameter parameter = parameterRepository.findByCropAndTitle(crop, title); // assumes
// that
// crop
// x
// title
// is
// unique(?)
return parameter;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = false)
public Method addMethod(String rdfUri, String description, String i18n, String unit, String fieldName, int fieldType, Integer fieldSize, String options,
String range, Parameter parameter) {
final Method method = new Method();
method.setRdfUri(StringUtils.defaultIfBlank(rdfUri, null));
method.setMethod(htmlSanitizer.sanitize(description));
method.setI18n(StringUtils.defaultIfBlank(i18n, null));
method.setUnit(StringUtils.defaultIfBlank(StringUtils.trimToNull(unit), null));
method.setFieldName(StringUtils.trimToNull(fieldName));
method.setFieldType(fieldType);
if (fieldType == 0) {
// Will throw NPE if not provided!
method.setFieldSize(fieldSize.toString());
}
method.setOptions(StringUtils.defaultIfBlank(StringUtils.trimToNull(options), null));
method.setRange(StringUtils.defaultIfBlank(StringUtils.trimToNull(range), null));
method.setParameter(parameter);
methodRepository.save(method);
// Ensure table for method!
genesysLowlevelRepository.ensureMethodTable(method);
return method;
}
@Override
@PreAuthorize("isAuthenticated()")
@Transactional(readOnly = true)
public Method getMethod(String rdfUri) {
if (rdfUri == null || rdfUri.isEmpty()) {
return null;
}
final Method method = methodRepository.findByRdfUri(rdfUri);
return method;
}
@Override
public Method getMethod(String description, Parameter parameter) {
if (description == null || description.isEmpty()) {
return null;
}
final Method method = methodRepository.findByMethodAndParameter(description, parameter);
return method;
}
/**
* Returns datasets to which current user has 'WRITE'
*/
@Override
@PreAuthorize("isAuthenticated()")
public List<Method> listMyMethods() {
final AuthUserDetails user = SecurityContextUtil.getAuthUser();
final List<Long> oids = aclService.listIdentitiesForSid(Method.class, user, BasePermission.WRITE);
LOG.info("Got " + oids.size() + " elements for " + user.getUsername());
if (oids.size() == 0) {
return null;
}
return methodRepository.findByIds(oids);
}
@Override
public Map<String, Long> getMethodStatistics(Method method) {
if (method.isCoded()) {
return traitValueRepository.getStatistics(method);
} else {
return null;
}
}
@Override
public Map<ParameterCategory, List<Parameter>> mapTraits(Crop crop, List<ParameterCategory> parameterCategories) {
if (parameterCategories == null || parameterCategories.size() == 0) {
return null;
}
final HashMap<ParameterCategory, List<Parameter>> catTraits = new HashMap<ParameterCategory, List<Parameter>>();
for (final ParameterCategory pc : parameterCategories) {
ArrayList<Parameter> traits;
catTraits.put(pc, traits = new ArrayList<Parameter>());
traits.addAll(parameterRepository.findByCropAndCategory(crop, pc, new Sort(new Sort.Order("title"))));
}
return catTraits;
}
@Override
public Map<Long, List<Method>> mapMethods(Crop crop) {
final HashMap<Long, List<Method>> paramMethods = new HashMap<Long, List<Method>>();
for (final Method method : methodRepository.findByCrop(crop)) {
List<Method> list = paramMethods.get(method.getParameter().getId());
if (list == null) {
paramMethods.put(method.getParameter().getId(), list = new ArrayList<Method>());
}
list.add(method);
}
return paramMethods;
}
}
......@@ -59,6 +59,7 @@ import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.GenesysService.AllStuff;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.RowCallbackHandler;
......@@ -77,6 +78,7 @@ public class DownloadServiceImpl implements DownloadService {
private GenesysService genesysService;
@Autowired
@Qualifier("genesysLowlevelRepositoryCustomImpl")
private GenesysLowlevelRepository genesysLowlevelRepository;
@Autowired
......
......@@ -103,6 +103,7 @@ import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.genesys2.spring.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.dao.IncorrectResultSizeDataAccessException;
import org.springframework.data.domain.Page;
......@@ -172,6 +173,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Autowired
private FaoInstituteRepository instituteRepository;
@Autowired
@Qualifier("genesysLowlevelRepositoryCustomImpl")
private GenesysLowlevelRepository genesysLowlevelRepository;
@Autowired
private SvalbardRepository svalbardRepository;
......
......@@ -39,6 +39,7 @@ import org.genesys2.server.service.ContentService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.spring.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
......@@ -72,6 +73,7 @@ public class InstituteServiceImpl implements InstituteService {
private ContentService contentService;
@Autowired
@Qualifier("genesysLowlevelRepositoryCustomImpl")
private GenesysLowlevelRepository genesysLowlevelRepository;
@Autowired
......
......@@ -16,34 +16,19 @@
package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.Metadata;
import org.genesys2.server.model.genesys.Method;
import org.genesys2.server.model.genesys.Parameter;
import org.genesys2.server.model.genesys.ParameterCategory;
import org.genesys2.server.model.genesys.*;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.persistence.domain.AccessionTraitRepository;
import org.genesys2.server.persistence.domain.GenesysLowlevelRepository;
import org.genesys2.server.persistence.domain.MetadataMethodRepository;
import org.genesys2.server.persistence.domain.MetadataRepository;
import org.genesys2.server.persistence.domain.MethodRepository;
import org.genesys2.server.persistence.domain.ParameterCategoryRepository;
import org.genesys2.server.persistence.domain.ParameterRepository;
import org.genesys2.server.persistence.domain.TraitValueRepository;
import org.genesys2.server.persistence.domain.*;
import org.genesys2.server.security.AuthUserDetails;
import org.genesys2.server.service.AclService;
import org.genesys2.server.service.HtmlSanitizer;
import org.genesys2.server.service.TraitService;
import org.genesys2.spring.SecurityContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
......@@ -54,12 +39,18 @@ import org.springframework.security.acls.domain.BasePermission;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Transactional(readOnly = true)
public class TraitServiceImpl implements TraitService {
public static final Log LOG = LogFactory.getLog(GenesysServiceImpl.class);
@Autowired
@Qualifier("genesysLowlevelRepositoryCustomImpl")
private GenesysLowlevelRepository genesysLowlevelRepository;
@Autowired
private AclService aclService;
......
......@@ -59,6 +59,7 @@ import org.genesys2.server.service.DescriptorService;
import org.genesys2.server.service.impl.FilterHandler.AppliedFilters;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Component;
......@@ -104,6 +105,7 @@ public class WorldClimUpdater implements InitializingBean {
private DescriptorService descriptorService;
@Autowired
@Qualifier("genesysLowlevelRepositoryCustomImpl")
private GenesysLowlevelRepository genesysLowlevel;
@Autowired
......
......@@ -16,16 +16,15 @@
package org.genesys2.server.servlet.controller.rest.serialization;
import java.io.IOException;
import java.util.Set;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
import java.io.IOException;
import java.util.Set;
public class UserSerializer extends JsonSerializer<User> {
......@@ -38,6 +37,7 @@ public class UserSerializer extends JsonSerializer<User> {
jgen.writeObjectField("uuid", user.getUuid());
jgen.writeObjectField("name", user.getName());
jgen.writeObjectField("email", user.getEmail());
jgen.writeObjectField("password", user.getPassword());
final Set<UserRole> roles = user.getRoles();
if (roles != null) {
jgen.writeArrayFieldStart("roles");
......
package org.genesys2.resttests;
package org.genesys2.tests.resttests;
import org.apache.velocity.app.VelocityEngine;
import org.genesys2.server.aspect.AsAdminAspect;
......@@ -8,6 +8,7 @@ import org.genesys2.server.persistence.acl.AclObjectIdentityPersistence;
import org.genesys2.server.persistence.acl.AclSidPersistence;
import org.genesys2.server.persistence.domain.*;
import org.genesys2.server.persistence.domain.mock.GenesysLowlevelRepositoryMock;
import org.genesys2.server.persistence.domain.mock.TraitServiceMock;
import org.genesys2.server.service.*;
import org.genesys2.server.service.impl.*;
import org.genesys2.server.servlet.controller.rest.*;
......@@ -17,6 +18,7 @@ import org.genesys2.spring.config.HazelcastConfig;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.NoOpCacheManager;
import org.springframework.context.annotation.Bean;
......@@ -26,6 +28,7 @@ import org.springframework.context.annotation.PropertySource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
......@@ -36,6 +39,7 @@ import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@ContextConfiguration(classes = AbstractRestTest.Config.class, initializers = PropertyPlacholderInitializer.class)
@ActiveProfiles("dev")
@WebAppConfiguration
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
public abstract class AbstractRestTest {
@Configuration
......@@ -90,10 +94,17 @@ public abstract class AbstractRestTest {
}
@Bean
public GenesysLowlevelRepository genesysLowlevelRepository() {
@Qualifier("genesysLowlevelRepositoryMock")
public GenesysLowlevelRepository genesysLowlevelRepositoryMock() {
return new GenesysLowlevelRepositoryMock();
}
@Bean
@Qualifier("genesysLowlevelRepositoryCustomImpl")
public GenesysLowlevelRepository genesysLowlevelRepositoryCustomImpl() {
return new GenesysLowlevelRepositoryCustomImpl();
}
@Bean
public AclService aclService() {
return new AclServiceImpl();
......@@ -145,8 +156,8 @@ public abstract class AbstractRestTest {
}
@Bean
public TraitService traitService() {
return new TraitServiceImpl();
public TraitService traitServiceMock() {
return new TraitServiceMock();
}
@Bean
......@@ -175,16 +186,39 @@ public abstract class AbstractRestTest {
}
@Bean
public LookupController lookupController(){
public LookupController lookupController() {
return new LookupController();
}
@Bean
public PermissionController permissionController(){
public PermissionController permissionController() {
return new PermissionController();
}
@Bean
public OrganizationController organizationController() {
return new OrganizationController();
}
@Bean
public BatchRESTService batchRESTService() {
return new BatchRESTServiceImpl();
}
@Bean
public AccessionCustomRepository accessionCustomRepository() {
return new AccessionCustomRepositoryImpl();
}
@Bean
public TaxonomyManager taxonomyManager() {
return new TaxonomyManager();
}
}
@Autowired
AccessionCustomRepository accessionCustomRepository;
@Autowired
PermissionController permissionController;
......@@ -203,9 +237,6 @@ public abstract class AbstractRestTest {
@Autowired
MethodRepository methodRepository;
@Autowired
GenesysLowlevelRepository genesysLowlevelRepository;