Java tutorial
/* * Copyright 2016 EMBL - European Bioinformatics Institute * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package uk.ac.ebi.eva.pipeline.jobs.steps; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.opencb.biodata.models.variant.VariantSource; import org.opencb.biodata.models.variant.VariantStudy; import org.opencb.datastore.core.ObjectMap; import org.opencb.opencga.storage.core.variant.VariantStorageManager; import org.springframework.batch.core.BatchStatus; import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.JobExecution; import org.springframework.batch.test.JobLauncherTestUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import uk.ac.ebi.eva.pipeline.configuration.CommonConfiguration; import uk.ac.ebi.eva.pipeline.configuration.JobOptions; import uk.ac.ebi.eva.pipeline.jobs.PopulationStatisticsJob; import uk.ac.ebi.eva.test.utils.JobTestUtils; import java.io.File; import java.io.IOException; import java.nio.file.Paths; import static junit.framework.TestCase.assertFalse; import static junit.framework.TestCase.assertTrue; import static org.junit.Assert.assertEquals; import static org.opencb.opencga.storage.core.variant.VariantStorageManager.VARIANT_SOURCE; import static uk.ac.ebi.eva.test.utils.JobTestUtils.restoreMongoDbFromDump; /** * @author Diego Poggioli * * Test for {@link PopulationStatisticsGeneratorStep} */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = { JobOptions.class, PopulationStatisticsJob.class, CommonConfiguration.class, JobLauncherTestUtils.class }) public class PopulationStatisticsGeneratorStepTest { private static final String SMALL_VCF_FILE = "/small20.vcf.gz"; private static final String STATS_DB = "VariantStatsConfigurationTest_vl"; //this name should be the same of the dump DB in /dump @Autowired private JobLauncherTestUtils jobLauncherTestUtils; @Autowired private JobOptions jobOptions; private ObjectMap variantOptions; private ObjectMap pipelineOptions; private File statsFile; @Test public void statisticsGeneratorStepShouldCalculateStats() throws IOException, InterruptedException { //and a valid variants load step already completed String dump = PopulationStatisticsGeneratorStepTest.class.getResource("/dump/").getFile(); restoreMongoDbFromDump(dump); //Given a valid VCF input file String input = SMALL_VCF_FILE; pipelineOptions.put("input.vcf", input); variantOptions.put(VariantStorageManager.DB_NAME, STATS_DB); VariantSource source = new VariantSource(input, "1", "1", "studyName", VariantStudy.StudyType.COLLECTION, VariantSource.Aggregation.NONE); variantOptions.put(VARIANT_SOURCE, source); statsFile = new File(Paths.get(pipelineOptions.getString("output.dir.statistics")) .resolve(VariantStorageManager.buildFilename(source)) + ".variants.stats.json.gz"); statsFile.delete(); assertFalse(statsFile.exists()); // ensure the stats file doesn't exist from previous executions // When the execute method in variantsStatsCreate is executed JobExecution jobExecution = jobLauncherTestUtils.launchStep(PopulationStatisticsJob.CALCULATE_STATISTICS); //Then variantsStatsCreate step should complete correctly assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus()); assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); //and the file containing statistics should exist assertTrue(statsFile.exists()); //delete created files statsFile.delete(); new File(Paths.get(pipelineOptions.getString("output.dir.statistics")) .resolve(VariantStorageManager.buildFilename(source)) + ".source.stats.json.gz").delete(); } /** * This test has to fail because it will try to extract variants from a non-existent DB. * Variants not loaded.. so nothing to query! */ @Test public void statisticsGeneratorStepShouldFailIfVariantLoadStepIsNotCompleted() throws Exception { //Given a valid VCF input file String input = SMALL_VCF_FILE; pipelineOptions.put("input.vcf", input); variantOptions.put(VariantStorageManager.DB_NAME, STATS_DB); VariantSource source = new VariantSource(input, "1", "1", "studyName", VariantStudy.StudyType.COLLECTION, VariantSource.Aggregation.NONE); variantOptions.put(VARIANT_SOURCE, source); statsFile = new File(Paths.get(pipelineOptions.getString("output.dir.statistics")) .resolve(VariantStorageManager.buildFilename(source)) + ".variants.stats.json.gz"); statsFile.delete(); assertFalse(statsFile.exists()); // ensure the stats file doesn't exist from previous executions // When the execute method in variantsStatsCreate is executed JobExecution jobExecution = jobLauncherTestUtils.launchStep(PopulationStatisticsJob.CALCULATE_STATISTICS); assertEquals(ExitStatus.FAILED.getExitCode(), jobExecution.getExitStatus().getExitCode()); } @Before public void setUp() throws Exception { jobOptions.loadArgs(); pipelineOptions = jobOptions.getPipelineOptions(); variantOptions = jobOptions.getVariantOptions(); } @After public void tearDown() throws Exception { JobTestUtils.cleanDBs(STATS_DB); } }