Commit d3103943 authored by Piotr Gawron's avatar Piotr Gawron
Browse files

test for mesh parser

parent d24abf44
......@@ -38,7 +38,6 @@ public class MeSHParser extends CachableInterface implements IExternalService {
/**
* Default class logger.
*/
@SuppressWarnings("unused")
private Logger logger = Logger.getLogger(MeSHParser.class);
/**
......@@ -135,10 +134,6 @@ public class MeSHParser extends CachableInterface implements IExternalService {
* thrown when there is a problem with accessing mesh db
*/
private MeSH getMeSHByIdFromDB(MiriamData meshID) throws IOException {
if (meshID == null || meshID.getResource() == null) {
throw new InvalidArgumentException("Mesh cannot be null");
}
MeSH result = new MeSH();
String page = getWebPageContent(URL_MESH_DATABASE + meshID.getResource());
......@@ -188,6 +183,9 @@ public class MeSHParser extends CachableInterface implements IExternalService {
j = page.indexOf("</TD>", i);
result.setMeSHId(cleanHtml(page.substring(i, j)));
}
if (result.getMeSHId() == null) {
return null;
}
return result;
}
......@@ -200,10 +198,11 @@ public class MeSHParser extends CachableInterface implements IExternalService {
status.setStatus(ExternalServiceStatusType.OK);
MiriamData meshId = new MiriamData(MiriamType.MESH_2012, "D010300");
MeSH mesh = getMeSH(meshId);
if (mesh == null || (mesh != null && !mesh.getMeSHId().equals(meshId.getResource()))) {
if (mesh == null || !meshId.getResource().equals(mesh.getMeSHId())) {
status.setStatus(ExternalServiceStatusType.CHANGED);
}
} catch (Exception e) {
logger.error(e, e);
status.setStatus(ExternalServiceStatusType.DOWN);
}
this.setCache(cacheCopy);
......
package lcsb.mapviewer.annotation.services;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
......@@ -18,6 +19,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import lcsb.mapviewer.annotation.AnnotationTestFunctions;
import lcsb.mapviewer.annotation.cache.GeneralCacheInterface;
import lcsb.mapviewer.annotation.cache.GeneralCacheWithExclusion;
import lcsb.mapviewer.annotation.cache.SourceNotAvailable;
import lcsb.mapviewer.annotation.cache.WebPageDownloader;
import lcsb.mapviewer.annotation.data.MeSH;
......@@ -32,20 +34,19 @@ public class MeSHParserTest extends AnnotationTestFunctions {
@Autowired
MeSHParser meshParser;
@Autowired
private GeneralCacheInterface cache;
@Before
public void setUp() throws Exception {
meshParser.setCache(new GeneralCacheWithExclusion(cache, 1));
}
@After
public void tearDown() throws Exception {
meshParser.setCache(cache);
}
/**
* @throws Exception
*/
/**
* @throws Exception
*/
@Test
public void testCreateChemicalListFromDB() throws Exception {
try {
......@@ -59,19 +60,96 @@ public class MeSHParserTest extends AnnotationTestFunctions {
assertTrue(mesh.getDescription() != null);
assertTrue(mesh.getSynonyms().size() > 0);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testIsValidMesh() throws Exception {
try {
MiriamData meshID = new MiriamData(MiriamType.MESH_2012, "D004298");
assertTrue(meshParser.isValidMeshId(meshID));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testIsValidMesh3() throws Exception {
try {
MiriamData meshID = new MiriamData(MiriamType.MESH_2012, "blablabla");
assertFalse(meshParser.isValidMeshId(meshID));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testIsValidMesh2() throws Exception {
try {
assertFalse(meshParser.isValidMeshId(null));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testGetInvalidMesh() throws Exception {
try {
meshParser.getMeSH(null);
fail("Exception expected");
} catch (InvalidArgumentException e) {
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testRefreshCacheQuery() throws Exception {
try {
Object res = meshParser.refreshCacheQuery("http://google.pl/");
assertNotNull(res);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testCreateChemicalListFromDB2() throws Exception {
try {
// Rotenone disease -no synonyms
meshID = new MiriamData(MiriamType.MESH_2012, "D012402");
mesh = meshParser.getMeSH(meshID);
MiriamData meshID = new MiriamData(MiriamType.MESH_2012, "D012402");
MeSH mesh = meshParser.getMeSH(meshID);
assertTrue(mesh != null);
assertTrue(mesh.getMeSHId() != null);
assertTrue(mesh.getName() != null);
assertTrue(mesh.getName().toLowerCase().equals("rotenone"));
assertTrue(mesh.getDescription() != null);
assertTrue(mesh.getSynonyms().size() == 0);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
@Test
public void testCreateChemicalListFromDB3() throws Exception {
try {
// One synonyms
meshID = new MiriamData(MiriamType.MESH_2012, "C503102");
mesh = meshParser.getMeSH(meshID);
MiriamData meshID = new MiriamData(MiriamType.MESH_2012, "C503102");
MeSH mesh = meshParser.getMeSH(meshID);
assertTrue(mesh != null);
assertTrue(mesh.getMeSHId() != null);
assertTrue(mesh.getName() != null);
......@@ -216,4 +294,36 @@ public class MeSHParserTest extends AnnotationTestFunctions {
}
}
@Test
public void testSimulateDownStatus() throws Exception {
WebPageDownloader downloader = meshParser.getWebPageDownloader();
try {
WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class);
when(mockDownloader.getFromNetwork(anyString())).thenThrow(new IOException());
meshParser.setWebPageDownloader(mockDownloader);
assertEquals(ExternalServiceStatusType.DOWN, meshParser.getServiceStatus().getStatus());
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
meshParser.setWebPageDownloader(downloader);
}
}
@Test
public void testSimulateChangeStatus() throws Exception {
WebPageDownloader downloader = meshParser.getWebPageDownloader();
try {
WebPageDownloader mockDownloader = Mockito.mock(WebPageDownloader.class);
when(mockDownloader.getFromNetwork(anyString())).thenReturn("");
meshParser.setWebPageDownloader(mockDownloader);
assertEquals(ExternalServiceStatusType.CHANGED, meshParser.getServiceStatus().getStatus());
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
meshParser.setWebPageDownloader(downloader);
}
}
}
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