Skip to content

Commit

Permalink
Merge pull request #75 from mandawilson/master
Browse files Browse the repository at this point in the history
Working unit tests
  • Loading branch information
sheridancbio authored Aug 24, 2017
2 parents 33ab0bd + 4a65b72 commit 09b44fa
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/

@RunWith(SpringRunner.class)
@Import(OncotreeUtilTestConfig.class)
@Import(OncotreeTestConfig.class)
public class MainTypesUtilTest {
@Autowired
private OncoTreeRepository mockRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Version> oncoTreeVersionRepositoryMockResponse() {
List<Version> oncoTreeVersionRepositoryMockResponse = new ArrayList<Version>();
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<OncoTreeNode> oncoTreeRepositoryMockResponse() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import static org.mockito.Matchers.*;

@RunWith(SpringRunner.class)
@Import(OncotreeUtilTestConfig.class)
@Import(OncotreeTestConfig.class)
public class TumorTypesUtilTest {
@Autowired
private ArrayList<OncoTreeNode> oncoTreeRepositoryMockResponse;
Expand Down
68 changes: 8 additions & 60 deletions core/src/test/java/org/mskcc/oncotree/utils/VersionUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Version> oncoTreeVersionRepositoryMockResponse = null;
@Autowired
private OncoTreeVersionRepository mockVersionRepository = null;
@Resource(name="oncoTreeVersionRepositoryMockResponse")
private List<Version> 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<String, Version> 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>();
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) {
Expand All @@ -107,15 +57,13 @@ private void setupExpectedVersionMap() throws Exception {

@Before
public void setupForTests() throws Exception {
setupMockVersionRepository();
setupExpectedVersionMap();
}

public String makeMismatchMessage(String versionName, String fieldName, String gotValue, String expectedValue) {
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;
Expand Down

This file was deleted.

0 comments on commit 09b44fa

Please sign in to comment.