diff --git a/.gitignore b/.gitignore index add6044..c1bfe9d 100644 --- a/.gitignore +++ b/.gitignore @@ -32,6 +32,11 @@ ContributorsCheck/contributors-check/src/main/resources/auth.properties .project .settings */target/* +target/ .recommenders NullCheck/src/main/resources/auth.properties auth.properties + +# ItelliJ Files +.idea/ +release-qa.iml diff --git a/pom.xml b/pom.xml index b26a597..98956db 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ reactome-release-qa org.reactome - UTF-8 4.2 @@ -159,6 +159,19 @@ 4.12 test + + org.hamcrest + hamcrest + 2.2 + test + + + org.mockito + mockito-core + 2.18.3 + test + + com.opencsv @@ -182,7 +195,7 @@ log4j-core ${log4j2.version} - org.apache.logging.log4j @@ -200,8 +213,8 @@ org.reactome.base reactome-base ${reactome.base.version} - diff --git a/src/main/java/org/reactome/release/qa/check/EHLDSubpathwayChangeCheck.java b/src/main/java/org/reactome/release/qa/check/EHLDSubpathwayChangeCheck.java index 9f5e732..4ce9fa3 100644 --- a/src/main/java/org/reactome/release/qa/check/EHLDSubpathwayChangeCheck.java +++ b/src/main/java/org/reactome/release/qa/check/EHLDSubpathwayChangeCheck.java @@ -5,17 +5,10 @@ import org.gk.persistence.MySQLAdaptor; import org.reactome.release.qa.annotations.ReleaseQACheck; import org.reactome.release.qa.common.AbstractQACheck; -import org.reactome.release.qa.common.QACheckerHelper; import org.reactome.release.qa.common.QAReport; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStreamReader; -import java.net.URL; import java.util.*; import java.util.function.Function; -import java.util.regex.Matcher; -import java.util.regex.Pattern; import java.util.stream.Collectors; /** @@ -26,7 +19,12 @@ @ReleaseQACheck public class EHLDSubpathwayChangeCheck extends AbstractQACheck implements ChecksTwoDatabases { - final private String NOT_AVAILABLE = "N/A"; + private final String NOT_AVAILABLE = "N/A"; + // Need to create the following value in org/gk/model/ReactomeJavaConstants.java in the Curator Tool project + // source code + public static final String HAS_EHLD = "hasEHLD"; + + private MySQLAdaptor olderDatabase; @Override @@ -42,7 +40,7 @@ public QAReport executeQACheck() throws Exception "Subpathway Names removed in " + this.dba.getDBName() ); - List pathwayIds = new ArrayList<>(getPathwayIDsWithEHLD()); + List pathwayIds = new ArrayList<>(getPathwayIDsWithEHLD(this.dba)); List oldPathways = getEHLDPathways(pathwayIds, getOtherDBAdaptor()); List newPathways = getEHLDPathways(pathwayIds, this.dba); @@ -73,54 +71,28 @@ public QAReport executeQACheck() throws Exception } @Override - public void setOtherDBAdaptor(MySQLAdaptor olderDatabase) { this.olderDatabase = olderDatabase; } - - public MySQLAdaptor getOtherDBAdaptor() { return this.olderDatabase; }; - - private List getPathwayIDsWithEHLD() throws EHLDPathwayIDRetrievalException { - final String reactomeEHLDURL = "https://reactome.org/download/current/ehld/"; - List pathwayIds = new ArrayList<>(); - try { - BufferedReader ehldWebSource = new BufferedReader( - new InputStreamReader(new URL(reactomeEHLDURL).openStream()) - ); - - pathwayIds.addAll(parsePathwayIds(ehldWebSource)); - pathwayIds.sort(Comparator.comparing(Long::longValue)); - - ehldWebSource.close(); - } catch (IOException e) { - e.printStackTrace(); - } - - if (pathwayIds.isEmpty()) { - throw new EHLDPathwayIDRetrievalException("Unable to retrieve pathway ids from " + reactomeEHLDURL); - } - - return pathwayIds; + public void setOtherDBAdaptor(MySQLAdaptor olderDatabase) { + this.olderDatabase = olderDatabase; } - private List parsePathwayIds(BufferedReader ehldWebSource) throws IOException { - List pathwayIds = new ArrayList<>(); - - Pattern svgFileName = getSVGFileNamePattern(); - String sourceLine; - while ((sourceLine = ehldWebSource.readLine()) != null) { - Matcher svgMatcher = svgFileName.matcher(sourceLine); - if (svgMatcher.find()) { - Long pathwayId = Long.parseLong(svgMatcher.group(1)); - pathwayIds.add(pathwayId); - } - } - - return pathwayIds; + public MySQLAdaptor getOtherDBAdaptor() { + return this.olderDatabase; } - private Pattern getSVGFileNamePattern() { - return Pattern.compile("\"(\\d+)\\.svg\""); + List getPathwayIDsWithEHLD(MySQLAdaptor newerDatabase) throws EHLDPathwayIDRetrievalException { + try { + return asGKInstanceCollection( + newerDatabase.fetchInstanceByAttribute(ReactomeJavaConstants.Pathway, HAS_EHLD, "=", true) + ) + .stream() + .map(GKInstance::getDBID) + .collect(Collectors.toList()); + } catch (Exception e) { + throw new EHLDPathwayIDRetrievalException("Unable to retrieve pathway ids from " + newerDatabase, e); + } } - private List getEHLDPathways(Collection dbIds, MySQLAdaptor database) { + List getEHLDPathways(Collection dbIds, MySQLAdaptor database) { List ehldPathways = new ArrayList<>(); try { ehldPathways.addAll( @@ -178,13 +150,13 @@ private boolean isPathway(GKInstance instance) { return instance.getSchemClass().isa(ReactomeJavaConstants.Pathway); } - private class EHLDPathwayIDRetrievalException extends Exception { - public EHLDPathwayIDRetrievalException(String retrievalError) { - super(retrievalError); + private static class EHLDPathwayIDRetrievalException extends Exception { + public EHLDPathwayIDRetrievalException(String retrievalError, Throwable cause) { + super(retrievalError, cause); } } - private class EHLDPathway { + class EHLDPathway { final private GKInstance pathway; final private List subPathways; final private Integer reactomeVersion; diff --git a/src/test/java/org/reactome/release/qa/check/EHLDSubpathwayChangeCheckTest.java b/src/test/java/org/reactome/release/qa/check/EHLDSubpathwayChangeCheckTest.java new file mode 100644 index 0000000..69afa9c --- /dev/null +++ b/src/test/java/org/reactome/release/qa/check/EHLDSubpathwayChangeCheckTest.java @@ -0,0 +1,85 @@ +package org.reactome.release.qa.check; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.equalTo; +import static org.reactome.release.qa.check.EHLDSubpathwayChangeCheck.HAS_EHLD; + +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import org.gk.model.GKInstance; +import org.gk.model.ReactomeJavaConstants; +import org.gk.persistence.MySQLAdaptor; +import org.gk.schema.SchemaClass; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.reactome.release.qa.check.EHLDSubpathwayChangeCheck.EHLDPathway; + +public class EHLDSubpathwayChangeCheckTest { + @Mock + MySQLAdaptor adaptor; + + @Mock + SchemaClass pathwaySchemaClass; + + @Mock + GKInstance firstPathway; + + @Mock + GKInstance secondPathway; + + private static final long FIRST_PATHWAY_DB_ID = 1L; + private static final long SECOND_PATHWAY_DB_ID = 2L; + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + } + + @Test + public void testGetEHLDPathwayIDs() throws Exception { + setUpMockPathways(); + + EHLDSubpathwayChangeCheck ehldSubpathwayChangeCheck = new EHLDSubpathwayChangeCheck(); + List pathwayIDs = ehldSubpathwayChangeCheck.getPathwayIDsWithEHLD(adaptor); + + assertThat(pathwayIDs, contains(firstPathway.getDBID())); // Contains firstPathway id only + } + + @Test + public void testGetEHLDSubPathways() throws Exception { + setUpMockPathways(); + Mockito.when(adaptor.fetchInstance((Collection) Collections.singletonList(FIRST_PATHWAY_DB_ID))) + .thenReturn(Collections.singletonList(firstPathway)); + Mockito.when(firstPathway.getAttributeValuesList(ReactomeJavaConstants.hasEvent)) + .thenReturn(Collections.singletonList(secondPathway)); + + EHLDSubpathwayChangeCheck ehldSubpathwayChangeCheck = new EHLDSubpathwayChangeCheck(); + EHLDPathway firstEHLDPathway = ehldSubpathwayChangeCheck.getEHLDPathways( + Collections.singletonList(FIRST_PATHWAY_DB_ID), adaptor + ).get(0); + + assertThat(firstEHLDPathway.getPathway(), equalTo(firstPathway)); + assertThat(firstEHLDPathway.getSubPathways(), contains(secondPathway)); + } + + private void setUpMockPathways() throws Exception { + setUpMockPathway(firstPathway, FIRST_PATHWAY_DB_ID, true); + setUpMockPathway(secondPathway, SECOND_PATHWAY_DB_ID, false); + Collection ehldPathways = Arrays.asList(firstPathway); + + Mockito.when(adaptor.fetchInstanceByAttribute(ReactomeJavaConstants.Pathway, HAS_EHLD, "=", true)) + .thenReturn(ehldPathways); + } + + private void setUpMockPathway(GKInstance pathway, long dbId, boolean hasEHLD) throws Exception { + Mockito.when(pathway.getSchemClass()).thenReturn(pathwaySchemaClass); + Mockito.when(pathwaySchemaClass.isa(ReactomeJavaConstants.Pathway)).thenReturn(true); + Mockito.when(pathway.getDBID()).thenReturn(dbId); + } +}