Skip to content

Commit

Permalink
removed unnecessary json response for some ingestion cases and set th…
Browse files Browse the repository at this point in the history
…e right response status codes
  • Loading branch information
waterflow80 committed Jan 13, 2024
1 parent c762c3e commit 6b5917d
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import uk.ac.ebi.eva.evaseqcol.exception.AssemblyAlreadyIngestedException;
import uk.ac.ebi.eva.evaseqcol.exception.AssemblyNotFoundException;
import uk.ac.ebi.eva.evaseqcol.exception.DuplicateSeqColException;
import uk.ac.ebi.eva.evaseqcol.exception.IncorrectAccessionException;
Expand Down Expand Up @@ -42,7 +43,7 @@ public AdminController(SeqColService seqColService) {
"contained in the assembly report) and eventually save these seqCol objects into the database. " +
"This is an authenticated endpoint, so it requires admin privileges to run it.")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "seqCol object(s) successfully inserted"),
@ApiResponse(responseCode = "201", description = "seqCol object(s) successfully inserted"),
@ApiResponse(responseCode = "409", description = "seqCol object(s) already exist(s)"),
@ApiResponse(responseCode = "404", description = "Assembly not found"),
@ApiResponse(responseCode = "400", description = "Bad request. (It can be a bad accession value)"),
Expand All @@ -56,7 +57,7 @@ public ResponseEntity<?> fetchAndInsertSeqColByAssemblyAccession(
required = true) @PathVariable String asmAccession) {
try {
IngestionResultEntity ingestionResult = seqColService.fetchAndInsertAllSeqColByAssemblyAccession(asmAccession);
return ResponseEntity.ok(ingestionResult);
return new ResponseEntity<>(ingestionResult, HttpStatus.CREATED);
} catch (IncorrectAccessionException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
} catch (IllegalArgumentException e) {
Expand All @@ -69,6 +70,8 @@ public ResponseEntity<?> fetchAndInsertSeqColByAssemblyAccession(
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
} catch (AssemblyNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
} catch (AssemblyAlreadyIngestedException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.CONFLICT);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package uk.ac.ebi.eva.evaseqcol.exception;

public class AssemblyAlreadyIngestedException extends RuntimeException{
public AssemblyAlreadyIngestedException(String assemblyAccession) {
super("Seqcol objects for assembly " + assemblyAccession + " has been already ingested");
}
}
37 changes: 14 additions & 23 deletions src/main/java/uk/ac/ebi/eva/evaseqcol/service/SeqColService.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import uk.ac.ebi.eva.evaseqcol.entities.SeqColLevelOneEntity;
import uk.ac.ebi.eva.evaseqcol.entities.SeqColExtendedDataEntity;
import uk.ac.ebi.eva.evaseqcol.entities.SeqColLevelTwoEntity;
import uk.ac.ebi.eva.evaseqcol.exception.AssemblyAlreadyIngestedException;
import uk.ac.ebi.eva.evaseqcol.exception.AssemblyNotFoundException;
import uk.ac.ebi.eva.evaseqcol.exception.AttributeNotDefinedException;
import uk.ac.ebi.eva.evaseqcol.exception.DuplicateSeqColException;
import uk.ac.ebi.eva.evaseqcol.exception.SeqColNotFoundException;
Expand Down Expand Up @@ -158,17 +160,17 @@ public void removeAllSeqCol() {
* assembly report.
* Return the list of level 0 digests of the inserted seqcol objects*/
public IngestionResultEntity fetchAndInsertAllSeqColByAssemblyAccession(
String assemblyAccession) throws IOException, DuplicateSeqColException {
IngestionResultEntity ingestionResultEntity = new IngestionResultEntity();
ingestionResultEntity.setAssemblyAccession(assemblyAccession);
String assemblyAccession) throws IOException, DuplicateSeqColException, AssemblyNotFoundException,
AssemblyAlreadyIngestedException{
Optional<Map<String, Object>> seqColDataMap = ncbiSeqColDataSource
.getAllPossibleSeqColExtendedData(assemblyAccession);
if (!seqColDataMap.isPresent()) {
logger.warn("No seqCol data corresponding to assemblyAccession " + assemblyAccession + " could be found on NCBI datasource");
ingestionResultEntity.setErrorMessage("No seqCol data corresponding to assemblyAccession " + assemblyAccession + " could be found on NCBI datasource");
return ingestionResultEntity;
throw new AssemblyNotFoundException(assemblyAccession);
}

IngestionResultEntity ingestionResultEntity = new IngestionResultEntity();
ingestionResultEntity.setAssemblyAccession(assemblyAccession);
// Retrieving the Map's data
List<SeqColExtendedDataEntity<List<String>>> possibleSequencesNamesList =
(List<SeqColExtendedDataEntity<List<String>>>) seqColDataMap.get().get("namesAttributes");
Expand All @@ -185,10 +187,6 @@ public IngestionResultEntity fetchAndInsertAllSeqColByAssemblyAccession(
SeqColExtendedDataEntity<List<String>> extendedSortedNameLengthPair = SeqColExtendedDataEntity.
constructSeqColSortedNameLengthPairs(extendedNamesEntity, extendedLengthsEntity);


// seqColExtendedDataEntities.add(extendedNamesEntity);
// seqColExtendedDataEntities.add(seqColSortedNameLengthPairEntity);

// Constructing a list of seqColExtData that has the type List<String>
List<SeqColExtendedDataEntity<List<String>>> seqColStringListExtDataEntities =
levelOneService.constructStringListExtDataEntities(sameValueAttributesMap, extendedNamesEntity,
Expand Down Expand Up @@ -218,23 +216,16 @@ public IngestionResultEntity fetchAndInsertAllSeqColByAssemblyAccession(
} catch (DuplicateSeqColException e) {
logger.info("Seqcol for " + assemblyAccession + " and naming convention " + extendedNamesEntity.getNamingConvention() +
" already exists. Skipping.");
continue;
}
}
return ingestionResultEntity;
}

/**
* Return the extended data entity that corresponds to the seqCol lengths attribute*/
// TODO: REFACTOR
/*public SeqColExtendedDataEntity retrieveExtendedLengthEntity(List<SeqColExtendedDataEntity> extendedDataEntities) {
for (SeqColExtendedDataEntity entity: extendedDataEntities) {
if (entity.getAttributeType() == SeqColExtendedDataEntity.AttributeType.lengths) {
return entity;
}
if (ingestionResultEntity.getNumberOfInsertedSeqcols() == 0) {
logger.warn("Seqcol objects for assembly " + assemblyAccession + " has been already ingested");
throw new AssemblyAlreadyIngestedException(assemblyAccession);
} else {
return ingestionResultEntity;
}
return null;
}*/

}

@Transactional
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
package uk.ac.ebi.eva.evaseqcol.controller;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import uk.ac.ebi.eva.evaseqcol.entities.SeqColLevelOneEntity;
import uk.ac.ebi.eva.evaseqcol.entities.SeqColLevelTwoEntity;
import uk.ac.ebi.eva.evaseqcol.exception.AssemblyAlreadyIngestedException;
import uk.ac.ebi.eva.evaseqcol.service.SeqColService;

import java.util.Optional;
Expand Down Expand Up @@ -72,6 +80,11 @@ void setUp() {
baseUrl = baseUrl + ":" + port + contextPath + ADMIN_PATH;
}

@AfterEach
void tearDown() {
seqColService.removeAllSeqCol();
}

@Test
@Transactional
/**
Expand All @@ -89,4 +102,35 @@ void ingestSeqColsTest() {
assertNotNull(levelTwoEntity.get().getLengths());
}

@Test
/**
* Testing the response for the ingestion endpoint for
* different kind of ingestion cases:
* 1. Not existed
* 2. Already existed
* 3. Invalid assembly accession
* 4. Not found assembly accession
* Note: the order of execution is important */
void ingestionResponseTest() {
// 1. Testing Valid Non-Existing Accession
String finalRequest1 = baseUrl + "/" + ASM_ACCESSION;
ResponseEntity<String> putResponse1 = restTemplate.exchange(finalRequest1, HttpMethod.PUT, null, String.class);
assertEquals(HttpStatus.CREATED, putResponse1.getStatusCode());

// 2. Testing Valid Existing Accession
final String finalRequest2 = baseUrl + "/" + ASM_ACCESSION; // Same as above
assertThrows(HttpClientErrorException.Conflict.class,
() -> restTemplate.exchange(finalRequest2, HttpMethod.PUT, null, String.class));

// 3. Testing Invalid Assembly Accession
final String finalRequest3 = baseUrl + "/" + ASM_ACCESSION.substring(0, ASM_ACCESSION.length()-4); // Less than 15 characters
assertThrows(HttpClientErrorException.BadRequest.class,
() -> restTemplate.exchange(finalRequest3, HttpMethod.PUT, null, String.class));

// 4. Testing Assembly Not Found
final String finalRequest4 = baseUrl + "/" + ASM_ACCESSION + "55"; // Accession doesn't correspond to any assembly
assertThrows(HttpClientErrorException.NotFound.class,
() -> restTemplate.exchange(finalRequest4, HttpMethod.PUT, null, String.class));
}

}

0 comments on commit 6b5917d

Please sign in to comment.