Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

basic integration tests for the API's endpoints #44

Merged
merged 4 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ server.port=8081
[email protected]@
[email protected]@

config.scaffolds.enabled = @contig-alias.scaffolds-enabled@
config.scaffolds.enabled=@contig-alias.scaffolds-enabled@

asm.file.download.dir=/tmp
service.info.file.path=src/main/resources/static/service-info.json
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package uk.ac.ebi.eva.evaseqcol.controller;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.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.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.service.SeqColService;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
public class AdminControllerIntegrationTest {

@LocalServerPort
private int port;

private String baseUrl = "http://localhost";

@Value("${server.servlet.context-path}")
private String contextPath;

@Value("${controller.auth.admin.username}")
private String USERNAME_ADMIN;

@Value("${controller.auth.admin.password}")
private String PASSWORD_ADMIN;

private final String ADMIN_PATH = "/admin/seqcols";

private final String ASM_ACCESSION = "GCA_000146045.2";

private final String insertedSeqColDigest = "3mTg0tAA3PS-R1TzelLVWJ2ilUzoWfVq"; // The digest of the seqCol that will be inserted by the PUT method

private static RestTemplate restTemplate;

@Autowired
private SeqColService seqColService;

@Container
static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:14.0");

@DynamicPropertySource
static void dataSourceProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
registry.add("spring.jpa.hibernate.ddl-auto", () -> "update");
}

@BeforeEach
void setUp() {
restTemplate = new RestTemplate();
restTemplate.getInterceptors().add(
new BasicAuthenticationInterceptor(USERNAME_ADMIN, PASSWORD_ADMIN)
);
baseUrl = baseUrl + ":" + port + contextPath + ADMIN_PATH;
}

@Test
@Transactional
/**
* Ingest all possible seqCol objects given the assembly accession*/
void ingestSeqColsTest() {
String finalRequest = baseUrl + "/{asmAccession}";
restTemplate.put(finalRequest, null, ASM_ACCESSION);
Optional<SeqColLevelOneEntity> levelOneEntity = (Optional<SeqColLevelOneEntity>)
seqColService.getSeqColByDigestAndLevel(insertedSeqColDigest, 1);
Optional<SeqColLevelTwoEntity> levelTwoEntity = (Optional<SeqColLevelTwoEntity>)
seqColService.getSeqColByDigestAndLevel(insertedSeqColDigest, 2);
assertTrue(levelOneEntity.isPresent());
assertTrue(levelTwoEntity.isPresent());
assertEquals(insertedSeqColDigest,levelOneEntity.get().getDigest());
assertNotNull(levelTwoEntity.get().getLengths());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package uk.ac.ebi.eva.evaseqcol.controller;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
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.SeqColLevelTwoEntity;
import uk.ac.ebi.eva.evaseqcol.io.SeqColGenerator;
import uk.ac.ebi.eva.evaseqcol.io.SeqColWriter;

import java.io.IOException;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
public class SeqColComparisonControllerIntegrationTest {

@LocalServerPort
private int port;

private String baseUrl = "http://localhost";

@Value("${server.servlet.context-path}")
private String contextPath;

private final String COMPARISON_PATH = "/comparison";

private final String SEQCOL_A_DIGEST = "3mTg0tAA3PS-R1TzelLVWJ2ilUzoWfVq"; // seqCol test digest

private final String SEQCOL_B_DIGEST = "rkTW1yZ0e22IN8K-0frqoGOMT8dynNyE";

@Autowired
private SeqColWriter seqColWriter;

@Autowired
private SeqColGenerator seqColGenerator;

private static RestTemplate restTemplate;

@Container
static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:14.0");

@DynamicPropertySource
static void dataSourceProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
registry.add("spring.jpa.hibernate.ddl-auto", () -> "update");
}

@BeforeAll
static void init() {
restTemplate = new RestTemplate();
}

@BeforeEach
void setUp() throws IOException {
seqColWriter.write(); // Save some seqCol objects into the database
baseUrl = baseUrl + ":" + port + contextPath + COMPARISON_PATH;
}

@AfterEach
void tearDown() {
seqColWriter.clearData();
}

@Test
void compareLocalSeqColsTest() {
String finalRequest = baseUrl + "/" + SEQCOL_A_DIGEST + "/" + SEQCOL_B_DIGEST;
Map<String, Object> comparisonResult = restTemplate.getForObject(finalRequest, Map.class);
assertNotNull(comparisonResult);
assertNotNull(comparisonResult.get("digests"));
assertNotNull(comparisonResult.get("arrays"));
assertNotNull(comparisonResult.get("elements"));
}

@Test
/**
* Compare a local seqCol object (identified by digest) with a user provided one,
* provided in the body of the POST request*/
void compareALocalSeqColWithProvidedOneTest() {
String finlRequest = baseUrl + "/" + SEQCOL_A_DIGEST;
SeqColLevelTwoEntity seqColLevelTwoPostBody = seqColGenerator.generateLevelTwoEntity();
Map<String, Object> comparisonResult = restTemplate.postForObject(finlRequest, seqColLevelTwoPostBody, Map.class);
assertNotNull(comparisonResult);
assertNotNull(comparisonResult.get("digests"));
assertNotNull(comparisonResult.get("arrays"));
assertNotNull(comparisonResult.get("elements"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package uk.ac.ebi.eva.evaseqcol.controller;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
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.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
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.io.SeqColWriter;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Testcontainers
public class SeqColControllerIntegrationTest {

@LocalServerPort
private int port;

private String baseUrl = "http://localhost";

private final String RETRIEVAL_PATH = "/collection";

private final String SEQCOL_DIGEST = "3mTg0tAA3PS-R1TzelLVWJ2ilUzoWfVq"; // seqCol test digest

private final String SERVICE_INFO_PATH = "/service-info";

@Value("${server.servlet.context-path}")
private String contextPath;

private static RestTemplate restTemplate;


@Autowired
private SeqColWriter seqColWriter;

@Container
static PostgreSQLContainer<?> postgreSQLContainer = new PostgreSQLContainer<>("postgres:14.0");

@DynamicPropertySource
static void dataSourceProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
registry.add("spring.jpa.hibernate.ddl-auto", () -> "update");
}

@BeforeAll
static void init() {
restTemplate = new RestTemplate();
}

@BeforeEach
void setUp() throws IOException {
seqColWriter.write(); // Save some seqCol objects into the database
baseUrl = baseUrl + ":" + port + contextPath ;
}

@AfterEach
void tearDown() {
seqColWriter.clearData();
}

@Test
void getSeqColByDigestTest() {
String level_1_path = "?level=1"; // can be left to default
String level_2_path = "?level=2";
String finalRequest = baseUrl + RETRIEVAL_PATH + "/" + SEQCOL_DIGEST;
Map<String, Object> levelOneEntity = restTemplate.getForObject(finalRequest + level_1_path, Map.class);
Map<String, List<String>> levelTwoEntity = restTemplate.getForObject(finalRequest + level_2_path, Map.class);
assertNotNull(levelOneEntity);
assertNotNull(levelTwoEntity);
assertNotNull(levelOneEntity.get("digest"));
assertNotNull(levelTwoEntity.get("sequences"));
}

@Test
void getServiceInfoTest() {
String finalRequest = baseUrl + SERVICE_INFO_PATH;
Map<String, Object> serviceInfoMap = restTemplate.getForObject(finalRequest, Map.class);
assertNotNull(serviceInfoMap);
assertNotNull(serviceInfoMap.get("id"));
}
}

Loading