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

service tests checking connection with DAPI for ctd database

parent a14445fe
......@@ -204,6 +204,7 @@ public class ChemicalParser {
if (disease == null) {
throw new InvalidArgumentException("disease cannot be null");
}
if (!dapiConnector.isValidConnection()) {
return null;
}
......
......@@ -63,6 +63,9 @@ public class ChemicalService extends DbSearchService implements IChemicalService
}
try {
List<Chemical> result = chemicalParser.getChemicalsByName(searchCriteria.getDisease(), name);
if (result == null) {
return null;
}
if (result.size() == 0) {
return null;
} else {
......@@ -80,11 +83,11 @@ public class ChemicalService extends DbSearchService implements IChemicalService
@Override
public List<Chemical> getForTargets(Collection<Element> targets, DbSearchCriteria searchCriteria) {
List<Chemical> chemicalList = new ArrayList<>();
Set<MiriamData> targetsMiriam = new HashSet<MiriamData>();
for (Element alias : targets) {
if (alias instanceof Protein || alias instanceof Gene || alias instanceof Rna) {
Set<MiriamData> targetsMiriam = new HashSet<>();
for (Element element : targets) {
if (element instanceof Protein || element instanceof Gene || element instanceof Rna) {
boolean hgncFound = false;
for (MiriamData md : alias.getMiriamData()) {
for (MiriamData md : element.getMiriamData()) {
if (MiriamType.HGNC_SYMBOL.equals(md.getDataType())) {
targetsMiriam.add(md);
hgncFound = true;
......@@ -95,7 +98,7 @@ public class ChemicalService extends DbSearchService implements IChemicalService
}
if (!hgncFound) {
MiriamData md = new MiriamData(MiriamType.HGNC_SYMBOL, alias.getName());
MiriamData md = new MiriamData(MiriamType.HGNC_SYMBOL, element.getName());
try {
if (hgncAnnotator.isValidHgncMiriam(md)) {
targetsMiriam.add(md);
......
package lcsb.mapviewer.services.search.chemical;
import static org.junit.Assert.*;
import static org.junit.Assume.assumeTrue;
import java.util.ArrayList;
import java.util.List;
......@@ -8,6 +9,8 @@ import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.*;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import lcsb.mapviewer.annotation.data.Chemical;
import lcsb.mapviewer.model.map.MiriamData;
......@@ -17,12 +20,14 @@ import lcsb.mapviewer.model.map.species.Rna;
import lcsb.mapviewer.services.ServiceTestFunctions;
import lcsb.mapviewer.services.search.DbSearchCriteria;
@Ignore("ctd decided to block us due to too many requests")
@Rollback(true)
@ActiveProfiles("ctdTestProfile")
public class ChemicalServiceTest extends ServiceTestFunctions {
Logger logger = LogManager.getLogger(ChemicalServiceTest.class);
Logger logger = LogManager.getLogger();
@Before
public void setUp() throws Exception {
assumeTrue("DAPI credentials are not provided", isDapiConfigurationAvailable());
}
@After
......@@ -37,6 +42,7 @@ public class ChemicalServiceTest extends ServiceTestFunctions {
assertNotNull(result);
assertEquals("D019803", result.getChemicalId().getResource());
logger.debug(result.getTargets());
assertEquals("No warnings expected.", 0, getWarnings().size());
}
......@@ -44,13 +50,13 @@ public class ChemicalServiceTest extends ServiceTestFunctions {
public void testSearchByElements() throws Exception {
List<Element> elements = new ArrayList<>();
Rna protein = new Rna("id");
protein.setName("GCH1");
MiriamData dystoniaDisease = new MiriamData(MiriamType.MESH_2012, "C538007");
protein.setName("RAB40AL");
MiriamData retardationSyndrome = new MiriamData(MiriamType.MESH_2012, "C564495");
elements.add(protein);
List<Chemical> chemicals = chemicalService.getForTargets(elements,
new DbSearchCriteria().disease(dystoniaDisease));
new DbSearchCriteria().disease(retardationSyndrome));
assertNotNull(chemicals);
assertTrue(chemicals.size() > 0);
......
package lcsb.mapviewer.services.search.chemical;
import static org.junit.Assert.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.*;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import lcsb.mapviewer.annotation.data.Chemical;
import lcsb.mapviewer.model.map.MiriamData;
import lcsb.mapviewer.model.map.MiriamType;
import lcsb.mapviewer.model.map.species.Element;
import lcsb.mapviewer.model.map.species.Rna;
import lcsb.mapviewer.services.ServiceTestFunctions;
import lcsb.mapviewer.services.search.DbSearchCriteria;
@Rollback(true)
@ActiveProfiles("dapiUnavailableTestProfile")
public class ChemicalServiceWithDapiUnavailableTest extends ServiceTestFunctions {
Logger logger = LogManager.getLogger();
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void testgetChemicalByName() throws Exception {
MiriamData pdDiseaseID = new MiriamData(MiriamType.MESH_2012, "D010300");
String name = "GSSG";
Chemical result = chemicalService.getByName(name, new DbSearchCriteria().disease(pdDiseaseID));
assertNull(result);
}
@Test
public void testSearchByElements() throws Exception {
List<Element> elements = new ArrayList<>();
Rna protein = new Rna("id");
protein.setName("RAB40AL");
MiriamData retardationSyndrome = new MiriamData(MiriamType.MESH_2012, "C564495");
elements.add(protein);
List<Chemical> chemicals = chemicalService.getForTargets(elements,
new DbSearchCriteria().disease(retardationSyndrome));
assertNotNull(chemicals);
assertEquals(0, chemicals.size());
}
}
package lcsb.mapviewer.services.search.chemical;
import static org.mockito.ArgumentMatchers.anyString;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import lcsb.mapviewer.annotation.services.dapi.*;
@Profile("ctdTestProfile")
@Configuration
public class CtdDapiConfiguration {
@Autowired
private DapiConnector dapiConnector;
private String dapiLogin;
private String dapiPassword;
public CtdDapiConfiguration() {
dapiLogin = System.getenv("DAPI_TEST_LOGIN");
dapiPassword = System.getenv("DAPI_TEST_PASSWORD");
}
@Bean
@Primary
public DapiConnector dapiConnector() throws DapiConnectionException {
DapiConnectorImpl mock = (DapiConnectorImpl) Mockito.spy(dapiConnector);
Mockito.doReturn("http://dapi.lcsb.uni.lu/api/database/CTD/releases/2019.02/").when(mock)
.getLatestReleaseUrl(anyString());
Mockito.doReturn(true).when(mock).isValidConnection();
Mockito.doReturn(dapiLogin).when(mock).getDapiLogin();
Mockito.doReturn(dapiPassword).when(mock).getDapiPassword();
return mock;
}
}
package lcsb.mapviewer.services.search.chemical;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.*;
import lcsb.mapviewer.annotation.services.dapi.*;
@Profile("dapiUnavailableTestProfile")
@Configuration
public class DapiUnavailableConfiguration {
@Autowired
private DapiConnector dapiConnector;
public DapiUnavailableConfiguration() {
}
@Bean
@Primary
public DapiConnector dapiConnector() throws DapiConnectionException {
DapiConnectorImpl mock = (DapiConnectorImpl) Mockito.spy(dapiConnector);
Mockito.doReturn(false).when(mock).isValidConnection();
Mockito.doReturn("").when(mock).getDapiLogin();
Mockito.doReturn("").when(mock).getDapiPassword();
return mock;
}
}
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