Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nitin-ebi committed Jan 23, 2024
1 parent d9d4179 commit 3e4a0b8
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package uk.ac.ebi.eva.contigalias.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ContigAliasConfiguration {

@Bean
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import uk.ac.ebi.eva.contigalias.exception.AssemblyNotFoundException;

import java.io.IOException;
import java.util.List;
Expand Down Expand Up @@ -95,10 +96,16 @@ public ResponseEntity<?> fetchAndInsertAssemblyByAccession(
public ResponseEntity<String> retrieveAndInsertMd5ChecksumForAssembly(@PathVariable(name = "accession")
@ApiParam(value = "INSDC or RefSeq assembly accession. Eg: " +
"GCA_000001405.10") String asmAccession) {
handler.retrieveAndInsertMd5ChecksumForAssembly(asmAccession);
return ResponseEntity.ok("A task has been submitted for updating md5checksum for all chromosomes " +
"in assembly " + asmAccession + ". Depending upon the number of chromosomes present in assembly, " +
"this might take some time to complete");
try {
handler.getAssemblyByAccession(asmAccession);
handler.retrieveAndInsertMd5ChecksumForAssembly(asmAccession);
return ResponseEntity.ok("A task has been submitted for updating md5checksum for all chromosomes " +
"in assembly " + asmAccession + ". Depending upon the number of chromosomes present in assembly, " +
"this might take some time to complete");
} catch (AssemblyNotFoundException e) {
return ResponseEntity.ok("Could not find assembly " + asmAccession +
". Please insert the assembly first (md5checksum will be updated as part of the insertion process");
}
}

@ApiOperation(value = "Retrieve list of assemblies for which MD5 Checksum updates are running/going-to-run ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

@Service
Expand All @@ -47,6 +48,10 @@ public AdminHandler(AssemblyService assemblyService,
this.assemblyAssembler = assemblyAssembler;
}

public Optional<AssemblyEntity> getAssemblyByAccession(String accession) {
return assemblyService.getAssemblyByAccession(accession);
}

public void fetchAndInsertAssemblyByAccession(String accession) throws IOException {
assemblyService.fetchAndInsertAssembly(accession);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;
Expand All @@ -13,7 +14,13 @@ public class Md5ChecksumRetriever {
private final Logger logger = LoggerFactory.getLogger(Md5ChecksumRetriever.class);
private String INSDC_ACCESSION_PLACE_HOLDER = "INSDC_ACCESSION_PLACE_HOLDER";
private String INSDC_CHECKSUM_URL = "https://www.ebi.ac.uk/ena/cram/sequence/insdc:" + INSDC_ACCESSION_PLACE_HOLDER + "/metadata";
private RestTemplate restTemplate = new RestTemplate();

private RestTemplate restTemplate;

@Autowired
public Md5ChecksumRetriever(RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

@Retryable(value = Exception.class, maxAttempts = 5, backoff = @Backoff(delay = 2000, multiplier = 2))
public String retrieveMd5Checksum(String insdcAccession) {
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/uk/ac/ebi/eva/contigalias/datasource/RetryTest.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package uk.ac.ebi.eva.contigalias.datasource;

import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.net.ftp.FTPFile;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.test.context.TestPropertySource;
import org.springframework.web.client.RestTemplate;
import uk.ac.ebi.eva.contigalias.dus.NCBIBrowser;
import uk.ac.ebi.eva.contigalias.exception.DownloadFailedException;
import uk.ac.ebi.eva.contigalias.scheduler.Md5ChecksumRetriever;

import java.io.IOException;
import java.nio.file.Path;
Expand All @@ -20,6 +24,8 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -38,6 +44,12 @@ public class RetryTest {
@Autowired
private NCBIAssemblyDataSource dataSource;

@Autowired
private Md5ChecksumRetriever md5ChecksumRetriever;

@MockBean
private RestTemplate restTemplate;


@Test
public void fileDownloadSuccessfulTest() throws IOException {
Expand Down Expand Up @@ -108,4 +120,20 @@ public void fileDownloadFailedRetryTest2() throws IOException {
verify(ncbiBrowser, times(5)).getGenomeReportDirectory(mockAccession);
}


@Test
public void retrieveMd5ChecksumRetry() {
String insdcAccession = "TEST_ACCESSION";
when(restTemplate.getForObject(anyString(), eq(JsonNode.class)))
.thenThrow(new RuntimeException("Simulated network issue"));

Md5ChecksumRetriever anotherObjSpy = Mockito.spy(md5ChecksumRetriever);
RuntimeException thrown = Assertions.assertThrows(RuntimeException.class, () -> {
anotherObjSpy.retrieveMd5Checksum(insdcAccession);
});

assertEquals("Simulated network issue", thrown.getMessage());
verify(restTemplate, times(5)).getForObject(anyString(), eq(JsonNode.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static ChromosomeEntity generate(long id) {
.setRefseq("refseq" + id)
.setUcscName("ucsc" + id)
.setEnaSequenceName("ena" + id)
.setMd5checksum("md5" + id)
.setMd5checksum(null)
.setTrunc512checksum("trunc512" + id)
.setAssembly(null);
}
Expand Down Expand Up @@ -80,7 +80,7 @@ void generateTest() {
assertTrue(entity.getInsdcAccession().endsWith(sId));
assertTrue(entity.getRefseq().endsWith(sId));
assertTrue(entity.getUcscName().endsWith(sId));
assertTrue(entity.getMd5checksum().endsWith(sId));
assertEquals(null, entity.getMd5checksum());
assertTrue(entity.getTrunc512checksum().endsWith(sId));
assertNull(entity.getAssembly());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.ActiveProfiles;

import uk.ac.ebi.eva.contigalias.entities.ChromosomeEntity;
import uk.ac.ebi.eva.contigalias.entitygenerator.AssemblyGenerator;
import uk.ac.ebi.eva.contigalias.entitygenerator.ChromosomeGenerator;

import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -80,6 +84,31 @@ void putChromosomeChecksumsByAccession() {
});
}

@Test
void testGetAssemblyWhereChromosomeMd5ChecksumIsNullOrEmpty() {
List<String> asmList = service.getAssembliesWhereChromosomeMd5ChecksumIsNull();
assertEquals(entity.getAssembly().getInsdcAccession(), asmList.get(0));
}

@Test
void testGetChromosomesByAssemblyInsdcAccessionWhereMd5ChecksumIsNull() {
Page<ChromosomeEntity> chrPage = service.getChromosomesByAssemblyInsdcAccessionWhereMd5ChecksumIsNull(entity.getAssembly().getInsdcAccession(), PageRequest.of(0, 100));
assertChromosomePageIdenticalToEntity(chrPage);
assertEquals(null, chrPage.getContent().get(0).getMd5checksum());
}

@Test
void testUpdateMD5ChecksumForAllChromosomesInAssembly() {
String testMD5Checksum = "testmd5checksum";
entity.setMd5checksum(testMD5Checksum);
service.updateMd5ChecksumForAll(Collections.singletonList(entity));

Page<ChromosomeEntity> chrPage = service.getChromosomesByInsdcAccession(entity.getInsdcAccession(), Pageable.unpaged());
assertChromosomePageIdenticalToEntity(chrPage);
assertEquals(testMD5Checksum, chrPage.getContent().get(0).getMd5checksum());

}

void assertChromosomePageIdenticalToEntity(Page<ChromosomeEntity> page) {
assertNotNull(page);
assertTrue(page.getTotalElements() > 0);
Expand Down

0 comments on commit 3e4a0b8

Please sign in to comment.