Skip to content
Snippets Groups Projects
Unverified Commit ece5d031 authored by Martin Weise's avatar Martin Weise
Browse files

Added tests to table service

parent 01333dff
No related branches found
No related tags found
4 merge requests!129New module for citation as they occur multiple,!121Modified logging, modified logging level, modified flasgger endpoint,!113Resolve "Bugs related with Query Service",!109Resolve "Use MariaDB for metadata database"
......@@ -97,11 +97,6 @@
<scope>compile</scope>
</dependency>
<!-- DataSource -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>${postgresql.version}</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
......@@ -192,9 +187,6 @@
<version>${jacoco.version}</version>
<configuration>
<excludes>
<exclude>at/tuwien/utils/**/*</exclude>
<exclude>at/tuwien/handlers/**/*</exclude>
<exclude>at/tuwien/seeder/**/*</exclude>
<exclude>at/tuwien/mapper/**/*</exclude>
<exclude>at/tuwien/exception/**/*</exclude>
<exclude>at/tuwien/config/**/*</exclude>
......
......@@ -96,11 +96,11 @@
<artifactId>hibernate-c3p0</artifactId>
<version>${hibernate-c3po.version}</version>
</dependency>
<!-- DataSource -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- DataSource -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
......@@ -133,17 +133,6 @@
<artifactId>btrace-client</artifactId>
<version>${btrace-client.version}</version>
</dependency>
<!-- Testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.aventrix.jnanoid</groupId>
<artifactId>jnanoid</artifactId>
<scope>test</scope>
<version>${random-utils.version}</version>
</dependency>
<!-- AMPQ -->
<dependency>
<groupId>com.rabbitmq</groupId>
......@@ -159,10 +148,6 @@
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
......@@ -218,6 +203,15 @@
</dependencies>
<build>
<resources>
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/application*.yml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
......@@ -227,16 +221,8 @@
<excludes>
<exclude>at/tuwien/mapper/**/*</exclude>
<exclude>at/tuwien/exception/**/*</exclude>
<exclude>at/tuwien/utils/**/*</exclude>
<exclude>at/tuwien/api/**/*</exclude>
<exclude>at/tuwien/config/**/*</exclude>
<exclude>at/tuwien/handlers/**/*</exclude>
<exclude>**/FdaTableServiceApplication.class</exclude>
<exclude>**/RabbitMqService.class</exclude>
<exclude>**/TableSeeder.class</exclude>
<exclude>**/DataSeeder.class</exclude>
<exclude>**/ServiceSeeder.class</exclude>
<exclude>**/JdbcConnector.class</exclude>
<exclude>**/FdaContainerManagingApplication.class</exclude>
</excludes>
</configuration>
<executions>
......@@ -247,7 +233,7 @@
</goals>
</execution>
<execution>
<id>default-report</id>
<id>report</id>
<phase>verify</phase>
<goals>
<goal>report</goal>
......@@ -255,20 +241,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version>
<executions>
<execution>
<id>aggregate</id>
<goals>
<goal>aggregate</goal>
</goals>
<phase>site</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
......
This diff is collapsed.
package at.tuwien.endpoint;
import at.tuwien.BaseUnitTest;
import at.tuwien.config.ReadyConfig;
import at.tuwien.endpoints.TableEndpoint;
import at.tuwien.exception.DatabaseNotFoundException;
import at.tuwien.service.DatabaseService;
import com.rabbitmq.client.Channel;
import org.apache.http.auth.BasicUserPrincipal;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
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.test.context.junit.jupiter.SpringExtension;
import javax.ws.rs.NotAllowedException;
import java.security.Principal;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@ExtendWith(SpringExtension.class)
@SpringBootTest
public class TableEndpointUnitTest extends BaseUnitTest {
/**
* RabbitMQ not required in this test
*/
@MockBean
private ReadyConfig readyConfig;
/**
* RabbitMQ not required in this test
*/
@MockBean
private Channel channel;
@MockBean
private DatabaseService databaseService;
@Autowired
private TableEndpoint tableEndpoint;
@Test
public void list_databaseNotFound_fails() throws DatabaseNotFoundException {
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* mock */
when(databaseService.find(CONTAINER_1_ID, DATABASE_1_ID))
.thenThrow(DatabaseNotFoundException.class);
/* test */
assertThrows(NotAllowedException.class, () -> {
tableEndpoint.list(CONTAINER_1_ID, DATABASE_1_ID, principal);
});
}
}
package at.tuwien.service;
import at.tuwien.BaseUnitTest;
import at.tuwien.config.DockerConfig;
import at.tuwien.config.ReadyConfig;
import at.tuwien.entities.database.table.Table;
import at.tuwien.exception.*;
import at.tuwien.repository.jpa.*;
import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.NotModifiedException;
import com.github.dockerjava.api.model.Bind;
import com.github.dockerjava.api.model.Network;
import com.rabbitmq.client.Channel;
import lombok.extern.log4j.Log4j2;
import org.apache.http.auth.BasicUserPrincipal;
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.extension.ExtendWith;
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.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.io.File;
import java.security.Principal;
import java.util.Arrays;
import java.util.List;
import static at.tuwien.config.DockerConfig.dockerClient;
import static at.tuwien.config.DockerConfig.hostConfig;
import static org.junit.jupiter.api.Assertions.assertEquals;
@Log4j2
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class TableServiceIntegrationTest extends BaseUnitTest {
@MockBean
private ReadyConfig readyConfig;
@MockBean
private Channel channel;
@Autowired
private ImageRepository imageRepository;
@Autowired
private ContainerRepository containerRepository;
@Autowired
private DatabaseRepository databaseRepository;
@Autowired
private TableRepository tableRepository;
@Autowired
private TableService tableService;
@Autowired
private UserRepository userRepository;
@BeforeAll
public static void beforeAll() throws InterruptedException {
afterAll();
/* create network */
dockerClient.createNetworkCmd()
.withName("fda-userdb")
.withIpam(new Network.Ipam()
.withConfig(new Network.Ipam.Config()
.withSubnet("172.28.0.0/16")))
.withEnableIpv6(false)
.exec();
dockerClient.createNetworkCmd()
.withName("fda-public")
.withIpam(new Network.Ipam()
.withConfig(new Network.Ipam.Config()
.withSubnet("172.29.0.0/16")))
.withEnableIpv6(false)
.exec();
/* create container */
final String bind = new File(
"./src/test/resources/weather").toPath().toAbsolutePath() + ":/docker-entrypoint-initdb.d";
log.trace("container bind {}", bind);
final CreateContainerResponse response = dockerClient.createContainerCmd(IMAGE_1_REPOSITORY + ":" + IMAGE_1_TAG)
.withHostConfig(hostConfig.withNetworkMode("fda-userdb").withBinds(Bind.parse(bind)))
.withName(CONTAINER_1_INTERNALNAME)
.withIpv4Address(CONTAINER_1_IP)
.withHostName(CONTAINER_1_INTERNALNAME)
.withEnv("MARIADB_USER=mariadb", "MARIADB_PASSWORD=mariadb", "MARIADB_ROOT_PASSWORD=mariadb",
"MARIADB_DATABASE=weather")
.exec();
/* start */
CONTAINER_1.setHash(response.getId());
DockerConfig.startContainer(CONTAINER_1);
}
@AfterAll
public static void afterAll() {
/* stop containers and remove them */
dockerClient.listContainersCmd()
.withShowAll(true)
.exec()
.forEach(container -> {
log.info("Delete container {}", Arrays.asList(container.getNames()));
try {
dockerClient.stopContainerCmd(container.getId()).exec();
} catch (NotModifiedException e) {
// ignore
}
dockerClient.removeContainerCmd(container.getId()).exec();
});
/* remove networks */
dockerClient.listNetworksCmd()
.exec()
.stream()
.filter(n -> n.getName().startsWith("fda"))
.forEach(network -> {
log.info("Delete network {}", network.getName());
dockerClient.removeNetworkCmd(network.getId()).exec();
});
}
@BeforeEach
public void beforeEach() {
userRepository.save(USER_1);
userRepository.save(USER_2);
imageRepository.save(IMAGE_1);
containerRepository.save(CONTAINER_1);
containerRepository.save(CONTAINER_2);
databaseRepository.save(DATABASE_1) /* public */;
databaseRepository.save(DATABASE_2) /* private */;
tableRepository.save(TABLE_1);
tableRepository.save(TABLE_2);
}
@Test
public void findAll_succeeds() throws DatabaseNotFoundException {
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* test */
final List<Table> response = tableService.findAll(CONTAINER_1_ID, DATABASE_1_ID, principal);
assertEquals(1, response.size());
}
@Test
public void findAll_fails() throws DatabaseNotFoundException {
final Principal principal = new BasicUserPrincipal(USER_2_USERNAME) /* not owner */;
/* test */
final List<Table> response = tableService.findAll(CONTAINER_2_ID, DATABASE_2_ID, principal);
assertEquals(0, response.size());
}
}
spring.profiles.active=test-noelastic
# enable local spring profile
spring.profiles.active=local
# disable discovery
spring.cloud.discovery.enabled = false
......@@ -14,14 +15,8 @@ spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.show-sql=false
spring.jpa.show-sql=true
# logging
logging.level.at.tuwien.service.impl.HibernateConnector=debug
logging.level.at.tuwien.=trace
# disable elasticsearch
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchDataAutoConfiguration,org.springframework.boot.autoconfigure.data.elasticsearch.ElasticsearchRepositoriesAutoConfiguration,org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchRestClientAutoConfiguration
fda.mapping.path: /tmp
fda.table.path: /tmp
\ No newline at end of file
# additional logging
logging.level.org.hibernate.SQL=debug
logging.level.org.hibernate.type=trace
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment