Commit 1ba34cc8 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

BaseRestImplTest removed

parent 63a7722e
......@@ -6,10 +6,12 @@ import org.junit.runners.Suite.SuiteClasses;
import lcsb.mapviewer.model.AllModelTests;
import lcsb.mapviewer.modelutils.map.AllMapUtilTests;
import lcsb.mapviewer.modelutils.serializer.MathMLSerializerTest;
@RunWith(Suite.class)
@SuiteClasses({ AllModelTests.class,
AllMapUtilTests.class,
MathMLSerializerTest.class,
})
public class AllTests {
......
package lcsb.mapviewer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.List;
import org.apache.logging.log4j.core.LogEvent;
......@@ -30,4 +33,22 @@ public class ModelTestFunctions {
return appender.getWarnings();
}
protected String readFile(String file) throws IOException {
StringBuilder stringBuilder = new StringBuilder();
BufferedReader reader = new BufferedReader(new FileReader(file));
try {
String line = null;
String ls = System.getProperty("line.separator");
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append(ls);
}
} finally {
reader.close();
}
return stringBuilder.toString();
}
}
package lcsb.mapviewer.modelutils.serializer;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import lcsb.mapviewer.ModelTestFunctions;
public class MathMLSerializerTest extends ModelTestFunctions {
public ObjectMapper objectMapper = null;
@Before
public void setUp() {
objectMapper = new ObjectMapper();
SimpleModule s = new SimpleModule();
s.addSerializer(String.class, new MathMLSerializer());
objectMapper.registerModule(s);
}
@Test
public void testMathMLToPresentationML() throws Exception {
String content = super.readFile("src/test/resources/mathml/content.xml");
String presentationXml = objectMapper.writeValueAsString(content);
assertNotNull(presentationXml);
assertTrue("Presentation math ml doesn't contain expected mrow tag", presentationXml.indexOf("mrow") >= 0);
}
@Test
public void testMathMLToPresentationMLCalledTwice() throws Exception {
String content = super.readFile("src/test/resources/mathml/content.xml");
String presentationXml = objectMapper.writeValueAsString(content);
String presentationXml2 = objectMapper.writeValueAsString(content);
assertEquals(presentationXml, presentationXml2);
}
@Test
public void testMathMLToPresentationMLShouldntHaveXmlNode() throws Exception {
String content = super.readFile("src/test/resources/mathml/content.xml");
String presentationXml = objectMapper.writeValueAsString(content);
assertFalse(presentationXml.startsWith("<?xml"));
}
}
package lcsb.mapviewer.api;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.transaction.annotation.Transactional;
import lcsb.mapviewer.annotation.data.Target;
import lcsb.mapviewer.annotation.services.MiriamConnector;
import lcsb.mapviewer.annotation.services.PubmedParser;
import lcsb.mapviewer.common.comparator.StringComparator;
import lcsb.mapviewer.common.exception.InvalidArgumentException;
import lcsb.mapviewer.common.exception.InvalidStateException;
import lcsb.mapviewer.common.exception.InvalidXmlSchemaException;
import lcsb.mapviewer.converter.Converter;
import lcsb.mapviewer.model.Project;
import lcsb.mapviewer.model.map.BioEntity;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.reaction.Reaction;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.modelutils.map.ElementUtils;
import lcsb.mapviewer.modelutils.serializer.model.map.ElementIdentifierType;
import lcsb.mapviewer.persist.dao.map.species.ElementProperty;
import lcsb.mapviewer.services.QueryException;
import lcsb.mapviewer.services.interfaces.IConfigurationService;
import lcsb.mapviewer.services.interfaces.IElementService;
import lcsb.mapviewer.services.interfaces.IModelService;
import lcsb.mapviewer.services.interfaces.IProjectService;
import lcsb.mapviewer.services.interfaces.IUserService;
@Transactional
public abstract class BaseRestImpl {
/**
* Default class logger.
*/
private static Logger logger = LogManager.getLogger();
@Autowired
private IModelService modelService;
@Autowired
private IProjectService projectService;
@Autowired
private IUserService userService;
@Autowired
private IConfigurationService configurationService;
@Autowired
private MiriamConnector miriamConnector;
@Autowired
private PubmedParser pubmedParser;
@Autowired
private IElementService elementService;
@Autowired
private List<Converter> modelConverters;
private Transformer mathMlTransformer;
protected Map<String, Object> okStatus() {
return new TreeMap<>();
}
protected Map<String, Object> createMinifiedSearchResult(BioEntity object) {
Map<String, Object> result = new TreeMap<>();
if (object instanceof Element) {
result.put("type", ElementIdentifierType.ALIAS);
Element element = (Element) object;
result.put("id", element.getId());
result.put("modelId", element.getModelData().getId());
} else if (object instanceof Reaction) {
result.put("type", ElementIdentifierType.REACTION);
Reaction reaction = (Reaction) object;
result.put("id", reaction.getId());
result.put("modelId", reaction.getModelData().getId());
} else {
throw new InvalidStateException("Unknown type of result: " + object.getClass());
}
return result;
};
/**
* @return the modelService
* @see #modelService
*/
public IModelService getModelService() {
return modelService;
}
/**
* @param modelService
* the modelService to set
* @see #modelService
*/
public void setModelService(IModelService modelService) {
this.modelService = modelService;
}
/**
* @return the userService
* @see #userService
*/
public IUserService getUserService() {
return userService;
}
/**
* @param userService
* the userService to set
* @see #userService
*/
public void setUserService(IUserService userService) {
this.userService = userService;
}
protected List<Map<String, Object>> prepareTargets(Collection<Target> targets, Project project) {
List<Map<String, Object>> result = new ArrayList<>();
for (Target target : targets) {
result.add(prepareTarget(target, project));
}
result.sort(new Comparator<Map<String, Object>>() {
@Override
public int compare(Map<String, Object> o1, Map<String, Object> o2) {
List<?> targetedObjects1 = (List<?>) o1.get("targetElements");
List<?> targetedObjects2 = (List<?>) o2.get("targetElements");
Integer size1 = 0;
Integer size2 = 0;
if (targetedObjects1 != null) {
size1 = targetedObjects1.size();
}
if (targetedObjects2 != null) {
size2 = targetedObjects2.size();
}
if (size1 == size2) {
String name1 = (String) o1.get("name");
String name2 = (String) o2.get("name");
return new StringComparator().compare(name1, name2);
}
return -size1.compareTo(size2);
}
});
return result;
}
protected Map<String, Object> prepareTarget(Target target, Project project) {
Map<String, Object> result = new TreeMap<>();
result.put("name", target.getName());
result.put("references", target.getReferences());
result.put("targetParticipants", target.getGenes());
List<Map<String, Object>> targetedObjects = new ArrayList<>();
List<BioEntity> bioEntities = new ArrayList<>();
List<Map<ElementProperty, List<? extends Object>>> searchPropertyList = new ArrayList<>();
switch (target.getType()) {
case COMPLEX_PROTEIN: {
Map<ElementProperty, List<? extends Object>> properties = new HashMap<>();
if (target.getSource() != null) {
properties.put(ElementProperty.ANNOTATION, Arrays.asList(target.getSource()));
} else {
properties.put(ElementProperty.ANNOTATION, new ArrayList<>());
}
searchPropertyList.add(properties);
break;
}
case SINGLE_PROTEIN: {
Map<ElementProperty, List<? extends Object>> properties = getSearchByAnnotationsProperties(target);
searchPropertyList.add(properties);
properties = getSearchByProteinRnaGeneNameProperties(target);
searchPropertyList.add(properties);
break;
}
case PROTEIN_FAMILY: {
Map<ElementProperty, List<? extends Object>> properties = getSearchByProteinRnaGeneNameProperties(target);
searchPropertyList.add(properties);
break;
}
case OTHER: {
Map<ElementProperty, List<? extends Object>> properties = getSearchByAnnotationsProperties(target);
searchPropertyList.add(properties);
properties = getSearchByNameProperties(target);
searchPropertyList.add(properties);
break;
}
default:
throw new InvalidArgumentException("Unknown drug target type: " + target.getType());
}
for (Map<ElementProperty, List<? extends Object>> properties : searchPropertyList) {
properties.put(ElementProperty.PROJECT, Arrays.asList(project));
bioEntities.addAll(elementService.getElementsByFilter(properties));
}
Set<String> ids = new HashSet<>();
bioEntities.sort(BioEntity.ID_COMPARATOR);
for (BioEntity bioEntity : bioEntities) {
String id = bioEntity.getId() + getType(bioEntity);
if (!ids.contains(id)) {
Map<String, Object> elementMapping = new TreeMap<>();
elementMapping.put("id", bioEntity.getId());
elementMapping.put("type", getType(bioEntity));
elementMapping.put("modelId", bioEntity.getModelData().getId());
targetedObjects.add(elementMapping);
ids.add(id);
}
}
result.put("targetElements", targetedObjects);
return result;
}
private Map<ElementProperty, List<? extends Object>> getSearchByAnnotationsProperties(Target target) {
Map<ElementProperty, List<? extends Object>> properties = new HashMap<>();
List<MiriamData> annotations = new ArrayList<>();
if (target.getSource() != null) {
annotations.add(target.getSource());
}
annotations.addAll(target.getGenes());
properties.put(ElementProperty.ANNOTATION, annotations);
return properties;
}
private Map<ElementProperty, List<? extends Object>> getSearchByProteinRnaGeneNameProperties(Target target) {
Map<ElementProperty, List<? extends Object>> properties = getSearchByNameProperties(target);
List<Class<? extends BioEntity>> classes = new ArrayList<>();
classes.addAll(new ElementUtils().getClassesByStringTypes("Protein"));
classes.addAll(new ElementUtils().getClassesByStringTypes("RNA"));
classes.addAll(new ElementUtils().getClassesByStringTypes("Gene"));
properties.put(ElementProperty.CLASS, classes);
return properties;
}
private Map<ElementProperty, List<? extends Object>> getSearchByNameProperties(Target target) {
Map<ElementProperty, List<? extends Object>> properties = new HashMap<>();
List<String> names = new ArrayList<>();
if (target.getSource() != null) {
names.add(target.getSource().getResource());
}
for (MiriamData md : target.getGenes()) {
names.add(md.getResource());
}
properties.put(ElementProperty.NAME, names);
return properties;
}
private String getType(BioEntity object) {
if (object instanceof Reaction) {
return ElementIdentifierType.REACTION.getJsName();
} else if (object instanceof Element) {
return ElementIdentifierType.ALIAS.getJsName();
} else {
throw new InvalidArgumentException("Unknown type of element " + object.getClass());
}
}
/**
* @return the projectService
* @see #projectService
*/
public IProjectService getProjectService() {
return projectService;
}
/**
* @param projectService
* the projectService to set
* @see #projectService
*/
public void setProjectService(IProjectService projectService) {
this.projectService = projectService;
}
protected Converter getModelParser(String handlerClass) throws QueryException {
for (Converter converter : getModelConverters()) {
if (converter.getClass().getCanonicalName().equals(handlerClass)) {
try {
return (Converter) Class.forName(handlerClass).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new QueryException("Problem with handler:" + handlerClass);
}
}
}
throw new QueryException("Unknown handlerClass: " + handlerClass);
}
protected List<Converter> getModelConverters() {
return modelConverters;
}
protected String getFirstValue(List<Object> list) {
if (list == null) {
return null;
}
if (list.size() > 0) {
return (String) list.get(0);
}
return null;
}
protected String mathMLToPresentationML(String xmlContent)
throws IOException, InvalidXmlSchemaException, TransformerException {
Transformer transformer = getMathMLTransformer();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
transformer.transform(new StreamSource(new ByteArrayInputStream(xmlContent.getBytes())), new StreamResult(baos));
String result = baos.toString("UTF-8");
if (result.startsWith("<?xml")) {
result = result.substring(result.indexOf(">") + 1);
}
return result;
}
private Transformer getMathMLTransformer()
throws TransformerFactoryConfigurationError, IOException, TransformerConfigurationException {
if (this.mathMlTransformer == null) {
TransformerFactory factory = TransformerFactory.newInstance();
Resource resource = new ClassPathResource("mathmlc2p.xsl");
InputStream styleInputStream = resource.getInputStream();
StreamSource stylesource = new StreamSource(styleInputStream);
this.mathMlTransformer = factory.newTransformer(stylesource);
}
return this.mathMlTransformer;
}
public IConfigurationService getConfigurationService() {
return configurationService;
}
public void setConfigurationService(IConfigurationService configurationService) {
this.configurationService = configurationService;
}
public MiriamConnector getMiriamConnector() {
return miriamConnector;
}
public void setMiriamConnector(MiriamConnector miriamConnector) {
this.miriamConnector = miriamConnector;
}
public PubmedParser getPubmedParser() {
return pubmedParser;
}
public void setPubmedParser(PubmedParser pubmedParser) {
this.pubmedParser = pubmedParser;
}
protected Integer parseInteger(Object value, String fieldName) throws QueryException {
if (value instanceof Integer) {
return (Integer) value;
} else if (value instanceof Double) {
logger.warn("Double will be trunceted to int: " + value);
return ((Double) value).intValue();
} else if (value == null) {
return null;
} else if (value instanceof String) {
if (((String) value).equalsIgnoreCase("")) {
return null;
} else {
try {
return Integer.parseInt((String) value);
} catch (NumberFormatException e) {
throw new QueryException("Invalid " + fieldName + " value: " + value);
}
}
} else {
throw new QueryException("Invalid " + fieldName + " value: " + value);
}
}
protected Boolean parseBoolean(Object value, String fieldName) throws QueryException {
if (value instanceof Boolean) {
return (Boolean) value;
} else if (value == null) {
return null;
} else if (value instanceof String) {
if (((String) value).equalsIgnoreCase("")) {
return null;
} else {
return Boolean.parseBoolean((String) value);
}
} else {
throw new QueryException("Invalid " + fieldName + " value: " + value);
}
}
protected Boolean parseBoolean(Object value) throws QueryException {
return parseBoolean(value, "boolean");
}
protected Integer parseInteger(Object value) throws QueryException {
return parseInteger(value, "integer");
}
}
......@@ -41,6 +41,6 @@ public class TaxonomyController extends BaseController {
if (name == null) {
throw new ObjectNotFoundException("Object not found: " + id);
}
return Map.of("name", name,"id", id);
return Map.of("name", name, "id", id);
}
}
\ No newline at end of file
......@@ -4,10 +4,10 @@ import lcsb.mapviewer.model.user.UserAnnotationSchema;
public class UserPreferencesDTO {
private UserPreferencesDTO() {
protected UserPreferencesDTO() {
}
public UserPreferencesDTO(UserAnnotationSchema annotationSchema) {
this.preferences = annotationSchema;
}
......
package lcsb.mapviewer.api;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.CALLS_REAL_METHODS;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.annotation.services.MiriamConnector;
import lcsb.mapviewer.annotation.services.PubmedParser;
import lcsb.mapviewer.annotation.services.annotators.ElementAnnotator;