From 4a65b729b2a2360a89eb5f2ece33df5fa5604020 Mon Sep 17 00:00:00 2001 From: Manda Wilson Date: Wed, 23 Aug 2017 17:44:10 -0400 Subject: [PATCH] got unit tests working --- .../oncotree/utils/MainTypesUtilTest.java | 2 +- ...estConfig.java => OncotreeTestConfig.java} | 68 ++++++++++++++++++- .../oncotree/utils/TumorTypesUtilTest.java | 2 +- .../mskcc/oncotree/utils/VersionUtilTest.java | 68 +++---------------- .../oncotree/utils/VersionUtilTestConfig.java | 40 ----------- 5 files changed, 75 insertions(+), 105 deletions(-) rename core/src/test/java/org/mskcc/oncotree/utils/{OncotreeUtilTestConfig.java => OncotreeTestConfig.java} (95%) delete mode 100644 core/src/test/java/org/mskcc/oncotree/utils/VersionUtilTestConfig.java diff --git a/core/src/test/java/org/mskcc/oncotree/utils/MainTypesUtilTest.java b/core/src/test/java/org/mskcc/oncotree/utils/MainTypesUtilTest.java index 2f8f041a..68d7f206 100644 --- a/core/src/test/java/org/mskcc/oncotree/utils/MainTypesUtilTest.java +++ b/core/src/test/java/org/mskcc/oncotree/utils/MainTypesUtilTest.java @@ -35,7 +35,7 @@ */ @RunWith(SpringRunner.class) -@Import(OncotreeUtilTestConfig.class) +@Import(OncotreeTestConfig.class) public class MainTypesUtilTest { @Autowired private OncoTreeRepository mockRepository; diff --git a/core/src/test/java/org/mskcc/oncotree/utils/OncotreeUtilTestConfig.java b/core/src/test/java/org/mskcc/oncotree/utils/OncotreeTestConfig.java similarity index 95% rename from core/src/test/java/org/mskcc/oncotree/utils/OncotreeUtilTestConfig.java rename to core/src/test/java/org/mskcc/oncotree/utils/OncotreeTestConfig.java index 60ef315f..99921104 100644 --- a/core/src/test/java/org/mskcc/oncotree/utils/OncotreeUtilTestConfig.java +++ b/core/src/test/java/org/mskcc/oncotree/utils/OncotreeTestConfig.java @@ -19,23 +19,85 @@ import java.util.*; import org.mockito.Mockito; +import org.mskcc.oncotree.crosswalk.MSKConcept; +import org.mskcc.oncotree.crosswalk.MSKConceptCache; +import org.mskcc.oncotree.crosswalk.CrosswalkRepository; import org.mskcc.oncotree.model.MainType; import org.mskcc.oncotree.model.TumorType; import org.mskcc.oncotree.model.Version; +import org.mskcc.oncotree.utils.VersionUtil; import org.mskcc.oncotree.topbraid.OncoTreeNode; import org.mskcc.oncotree.topbraid.OncoTreeRepository; +import org.mskcc.oncotree.topbraid.OncoTreeVersionRepository; +import org.mskcc.oncotree.topbraid.TopBraidSessionConfiguration; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import static org.junit.Assert.fail; +import static org.mockito.Matchers.*; /** * * @author heinsz */ @Configuration -@ComponentScan(basePackages = {"org.mskcc.oncotree.utils","org.mskcc.oncotree.topbraid"}) -public class OncotreeUtilTestConfig { +public class OncotreeTestConfig { + + @Bean + public OncoTreeVersionRepository oncoTreeVersionRepository() { + OncoTreeVersionRepository repository = Mockito.mock(OncoTreeVersionRepository.class); + Mockito.when(repository.getOncoTreeVersions()).thenReturn(oncoTreeVersionRepositoryMockResponse()); + return repository; + } + + @Bean + public List oncoTreeVersionRepositoryMockResponse() { + List oncoTreeVersionRepositoryMockResponse = new ArrayList(); + Version nextVersion = new Version(); + nextVersion.setVersion("oncotree_latest_stable"); + nextVersion.setGraphURI("urn:x-evn-master:oncotree_2017_06_21"); + nextVersion.setDescription("This is an alias for whatever OncoTree version is the latest stable (timestamped) release."); + oncoTreeVersionRepositoryMockResponse.add(nextVersion); + nextVersion = new Version(); + nextVersion.setVersion("oncotree_development"); + nextVersion.setGraphURI("urn:x-evn-master:oncotree_current"); + nextVersion.setDescription("Latest OncoTree under development (subject to change without notice)"); + oncoTreeVersionRepositoryMockResponse.add(nextVersion); + nextVersion = new Version(); + nextVersion.setVersion("oncotree_2017_06_21"); + nextVersion.setGraphURI("urn:x-evn-master:oncotree_2017_06_21"); + nextVersion.setDescription("Stable OncoTree released on date 2017-06-21"); + oncoTreeVersionRepositoryMockResponse.add(nextVersion); + nextVersion = new Version(); + nextVersion.setVersion("oncotree_legacy_1.1"); + nextVersion.setGraphURI("urn:x-evn-master:oncotree_legacy_1_1"); + nextVersion.setDescription("This is the closest match in TopBraid for the TumorTypes_txt file associated with release 1.1 of OncoTree (approved by committee)"); + oncoTreeVersionRepositoryMockResponse.add(nextVersion); + return oncoTreeVersionRepositoryMockResponse; + } + + @Bean + public TopBraidSessionConfiguration topBraidSessionConfiguration() { + return new TopBraidSessionConfiguration(); + } + + @Bean + public MSKConceptCache mskConceptCache() { + MSKConceptCache mskConceptCache = Mockito.mock(MSKConceptCache.class); + MSKConcept mskConcept = new MSKConcept(); + mskConcept.setConceptIds(Arrays.asList("MSK00001", "MSK00002")); + Mockito.when(mskConceptCache.get(any(String.class))).thenReturn(mskConcept); + return mskConceptCache; + } + + @Bean + public CrosswalkRepository crosswalkRepository() { + return Mockito.mock(CrosswalkRepository.class); + } + + @Bean + public VersionUtil versionUtil() { + return new VersionUtil(); + } @Bean public List oncoTreeRepositoryMockResponse() throws Exception { diff --git a/core/src/test/java/org/mskcc/oncotree/utils/TumorTypesUtilTest.java b/core/src/test/java/org/mskcc/oncotree/utils/TumorTypesUtilTest.java index 45658c6b..73a72771 100644 --- a/core/src/test/java/org/mskcc/oncotree/utils/TumorTypesUtilTest.java +++ b/core/src/test/java/org/mskcc/oncotree/utils/TumorTypesUtilTest.java @@ -36,7 +36,7 @@ import static org.mockito.Matchers.*; @RunWith(SpringRunner.class) -@Import(OncotreeUtilTestConfig.class) +@Import(OncotreeTestConfig.class) public class TumorTypesUtilTest { @Autowired private ArrayList oncoTreeRepositoryMockResponse; diff --git a/core/src/test/java/org/mskcc/oncotree/utils/VersionUtilTest.java b/core/src/test/java/org/mskcc/oncotree/utils/VersionUtilTest.java index 4ec0e958..516ea23d 100644 --- a/core/src/test/java/org/mskcc/oncotree/utils/VersionUtilTest.java +++ b/core/src/test/java/org/mskcc/oncotree/utils/VersionUtilTest.java @@ -20,83 +20,33 @@ import java.io.*; import java.nio.file.*; import java.util.*; -import org.junit.AfterClass; +import javax.annotation.Resource; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.runner.RunWith; import org.junit.Test; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.Mockito; -import org.mockito.MockitoAnnotations; import org.mskcc.oncotree.error.InvalidVersionException; -import org.mskcc.oncotree.model.MainType; import org.mskcc.oncotree.model.Version; -import org.mskcc.oncotree.topbraid.OncoTreeNode; -import org.mskcc.oncotree.topbraid.OncoTreeVersionRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.AnnotationConfigApplicationContext; -import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; -import org.springframework.context.support.ClassPathXmlApplicationContext; -import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; -import org.springframework.test.context.web.WebAppConfiguration; -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 static org.junit.Assert.fail; import static org.mockito.Matchers.*; -import static org.mockito.Mockito.*; - @RunWith(SpringRunner.class) -@Import(VersionUtilTestConfig.class) +@Import(OncotreeTestConfig.class) public class VersionUtilTest { - private List oncoTreeVersionRepositoryMockResponse = null; - @Autowired - private OncoTreeVersionRepository mockVersionRepository = null; + @Resource(name="oncoTreeVersionRepositoryMockResponse") + private List oncoTreeVersionRepositoryMockResponse; + @Autowired - private VersionUtil versionUtil = null; - //TODO convert this class to use the common OncotreeConfiguration class for unit tests. Proviede the expectedVersionMap as a @Resource + private VersionUtil versionUtil; + + //TODO convert this class to use the common OncotreeConfiguration class for unit tests. Provide the expectedVersionMap as a @Resource private HashMap expectedVersionMap = null; //TODO: move towards a more fine-grained test definition --- each condition being tested should be a single test function, and needed expected and actual data structures should be set up using the Before or BeforeClass annotation - private void setupOncoTreeVersionRepositoryMockResponse() throws Exception { - if (oncoTreeVersionRepositoryMockResponse == null) { - oncoTreeVersionRepositoryMockResponse = new ArrayList(); - Version nextVersion = new Version(); - nextVersion.setVersion("oncotree_latest_stable"); - nextVersion.setGraphURI("urn:x-evn-master:oncotree_2017_06_21"); - nextVersion.setDescription("This is an alias for whatever OncoTree version is the latest stable (timestamped) release."); - oncoTreeVersionRepositoryMockResponse.add(nextVersion); - nextVersion = new Version(); - nextVersion.setVersion("oncotree_development"); - nextVersion.setGraphURI("urn:x-evn-master:oncotree_current"); - nextVersion.setDescription("Latest OncoTree under development (subject to change without notice)"); - oncoTreeVersionRepositoryMockResponse.add(nextVersion); - nextVersion = new Version(); - nextVersion.setVersion("oncotree_2017_06_21"); - nextVersion.setGraphURI("urn:x-evn-master:oncotree_2017_06_21"); - nextVersion.setDescription("Stable OncoTree released on date 2017-06-21"); - oncoTreeVersionRepositoryMockResponse.add(nextVersion); - nextVersion = new Version(); - nextVersion.setVersion("oncotree_legacy_1.1"); - nextVersion.setGraphURI("urn:x-evn-master:oncotree_legacy_1_1"); - nextVersion.setDescription("This is the closest match in TopBraid for the TumorTypes_txt file associated with release 1.1 of OncoTree (approved by committee)"); - oncoTreeVersionRepositoryMockResponse.add(nextVersion); - } - } - - private void setupMockVersionRepository() throws Exception { - setupOncoTreeVersionRepositoryMockResponse(); - Mockito.when(mockVersionRepository.getOncoTreeVersions()).thenReturn(oncoTreeVersionRepositoryMockResponse); - } - private void setupExpectedVersionMap() throws Exception { - setupOncoTreeVersionRepositoryMockResponse(); if (expectedVersionMap == null) { expectedVersionMap = new HashMap<>(oncoTreeVersionRepositoryMockResponse.size()); for (Version version : oncoTreeVersionRepositoryMockResponse) { @@ -107,7 +57,6 @@ private void setupExpectedVersionMap() throws Exception { @Before public void setupForTests() throws Exception { - setupMockVersionRepository(); setupExpectedVersionMap(); } @@ -115,7 +64,6 @@ public String makeMismatchMessage(String versionName, String fieldName, String g return versionName + " : mismatch in " + fieldName + " (got:" + gotValue + ") (expected:" + expectedValue + ")\n"; } - private boolean testValuesMatch(String value1, String value2) { if (value1 == null || value1.trim().length() == 0) { return value2 == null || value2.trim().length() == 0; diff --git a/core/src/test/java/org/mskcc/oncotree/utils/VersionUtilTestConfig.java b/core/src/test/java/org/mskcc/oncotree/utils/VersionUtilTestConfig.java deleted file mode 100644 index 0f3c098d..00000000 --- a/core/src/test/java/org/mskcc/oncotree/utils/VersionUtilTestConfig.java +++ /dev/null @@ -1,40 +0,0 @@ -/** Copyright (c) 2017 Memorial Sloan-Kettering Cancer Center. - * - * This library is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY, WITHOUT EVEN THE IMPLIED WARRANTY OF - * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. The software and - * documentation provided hereunder is on an "as is" basis, and - * Memorial Sloan-Kettering Cancer Center - * has no obligations to provide maintenance, support, - * updates, enhancements or modifications. In no event shall - * Memorial Sloan-Kettering Cancer Center - * be liable to any party for direct, indirect, special, - * incidental or consequential damages, including lost profits, arising - * out of the use of this software and its documentation, even if - * Memorial Sloan-Kettering Cancer Center - * has been advised of the possibility of such damage. -*/ - -package org.mskcc.oncotree.utils; - -import org.mockito.Mockito; -import org.mskcc.oncotree.topbraid.OncoTreeVersionRepository; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; -import org.springframework.context.annotation.Configuration; -import org.springframework.context.annotation.ImportResource; - -@Configuration -@ComponentScan(basePackages = {"org.mskcc.oncotree.utils","org.mskcc.oncotree.topbraid"}) -public class VersionUtilTestConfig { - - @Bean - public OncoTreeVersionRepository oncoTreeVersionRepository() { - return Mockito.mock(OncoTreeVersionRepository.class); - } - @Bean - public VersionUtil versionUtil() { - return new VersionUtil(); - } - -}