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

Added tests

parent f6b265cb
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"
...@@ -2,6 +2,7 @@ package at.tuwien; ...@@ -2,6 +2,7 @@ package at.tuwien;
import at.tuwien.api.database.query.QueryBriefDto; import at.tuwien.api.database.query.QueryBriefDto;
import at.tuwien.api.database.query.QueryDto; import at.tuwien.api.database.query.QueryDto;
import at.tuwien.api.database.table.TableCreateDto;
import at.tuwien.api.user.UserDto; import at.tuwien.api.user.UserDto;
import at.tuwien.entities.container.image.*; import at.tuwien.entities.container.image.*;
import at.tuwien.entities.database.table.columns.concepts.Concept; import at.tuwien.entities.database.table.columns.concepts.Concept;
...@@ -735,6 +736,7 @@ public abstract class BaseUnitTest { ...@@ -735,6 +736,7 @@ public abstract class BaseUnitTest {
.id(CONTAINER_1_ID) .id(CONTAINER_1_ID)
.name(CONTAINER_1_NAME) .name(CONTAINER_1_NAME)
.internalName(CONTAINER_1_INTERNALNAME) .internalName(CONTAINER_1_INTERNALNAME)
.imageId(IMAGE_1_ID)
.image(CONTAINER_1_IMAGE) .image(CONTAINER_1_IMAGE)
.hash(CONTAINER_1_HASH) .hash(CONTAINER_1_HASH)
.created(CONTAINER_1_CREATED) .created(CONTAINER_1_CREATED)
...@@ -745,6 +747,7 @@ public abstract class BaseUnitTest { ...@@ -745,6 +747,7 @@ public abstract class BaseUnitTest {
.id(CONTAINER_2_ID) .id(CONTAINER_2_ID)
.name(CONTAINER_2_NAME) .name(CONTAINER_2_NAME)
.internalName(CONTAINER_2_INTERNALNAME) .internalName(CONTAINER_2_INTERNALNAME)
.imageId(IMAGE_1_ID)
.image(CONTAINER_2_IMAGE) .image(CONTAINER_2_IMAGE)
.hash(CONTAINER_2_HASH) .hash(CONTAINER_2_HASH)
.created(CONTAINER_2_CREATED) .created(CONTAINER_2_CREATED)
...@@ -755,6 +758,7 @@ public abstract class BaseUnitTest { ...@@ -755,6 +758,7 @@ public abstract class BaseUnitTest {
.id(CONTAINER_3_ID) .id(CONTAINER_3_ID)
.name(CONTAINER_3_NAME) .name(CONTAINER_3_NAME)
.internalName(CONTAINER_3_INTERNALNAME) .internalName(CONTAINER_3_INTERNALNAME)
.imageId(IMAGE_1_ID)
.image(CONTAINER_3_IMAGE) .image(CONTAINER_3_IMAGE)
.hash(CONTAINER_3_HASH) .hash(CONTAINER_3_HASH)
.created(CONTAINER_3_CREATED) .created(CONTAINER_3_CREATED)
...@@ -1842,7 +1846,7 @@ public abstract class BaseUnitTest { ...@@ -1842,7 +1846,7 @@ public abstract class BaseUnitTest {
.description(TABLE_2_DESCRIPTION) .description(TABLE_2_DESCRIPTION)
.name(TABLE_2_NAME) .name(TABLE_2_NAME)
.lastModified(TABLE_2_LAST_MODIFIED) .lastModified(TABLE_2_LAST_MODIFIED)
.tdbid(DATABASE_2_ID) .tdbid(DATABASE_1_ID)
.topic(TABLE_2_TOPIC) .topic(TABLE_2_TOPIC)
.build(); .build();
...@@ -1857,6 +1861,12 @@ public abstract class BaseUnitTest { ...@@ -1857,6 +1861,12 @@ public abstract class BaseUnitTest {
.topic(TABLE_3_TOPIC) .topic(TABLE_3_TOPIC)
.build(); .build();
public final static TableCreateDto TABLE_3_CREATE_DTO = TableCreateDto.builder()
.name(TABLE_5_NAME)
.description(TABLE_5_DESCRIPTION)
.columns(List.of())
.build();
public final static Table TABLE_4 = Table.builder() public final static Table TABLE_4 = Table.builder()
.id(TABLE_4_ID) .id(TABLE_4_ID)
.created(Instant.now()) .created(Instant.now())
......
...@@ -4,10 +4,11 @@ import at.tuwien.BaseUnitTest; ...@@ -4,10 +4,11 @@ import at.tuwien.BaseUnitTest;
import at.tuwien.config.DockerConfig; import at.tuwien.config.DockerConfig;
import at.tuwien.config.IndexInitializer; import at.tuwien.config.IndexInitializer;
import at.tuwien.config.ReadyConfig; import at.tuwien.config.ReadyConfig;
import at.tuwien.entities.container.Container;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.table.Table; import at.tuwien.entities.database.table.Table;
import at.tuwien.entities.database.table.columns.TableColumn;
import at.tuwien.exception.*; import at.tuwien.exception.*;
import at.tuwien.repository.elastic.TableColumnidxRepository;
import at.tuwien.repository.elastic.TableidxRepository;
import at.tuwien.repository.jpa.*; import at.tuwien.repository.jpa.*;
import com.github.dockerjava.api.command.CreateContainerResponse; import com.github.dockerjava.api.command.CreateContainerResponse;
import com.github.dockerjava.api.exception.NotModifiedException; import com.github.dockerjava.api.exception.NotModifiedException;
...@@ -24,16 +25,19 @@ import org.springframework.boot.test.mock.mockito.MockBean; ...@@ -24,16 +25,19 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.annotation.DirtiesContext; import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.SpringExtension; import org.springframework.test.context.junit.jupiter.SpringExtension;
import javax.transaction.Transactional;
import java.io.File; import java.io.File;
import java.security.Principal; import java.security.Principal;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors;
import static at.tuwien.config.DockerConfig.dockerClient; import static at.tuwien.config.DockerConfig.dockerClient;
import static at.tuwien.config.DockerConfig.hostConfig; import static at.tuwien.config.DockerConfig.hostConfig;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.when;
@Log4j2 @Log4j2
@DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD)
...@@ -50,6 +54,12 @@ public class TableServiceIntegrationTest extends BaseUnitTest { ...@@ -50,6 +54,12 @@ public class TableServiceIntegrationTest extends BaseUnitTest {
@MockBean @MockBean
private IndexInitializer indexInitializer; private IndexInitializer indexInitializer;
@MockBean
private TableidxRepository tableidxRepository;
@MockBean
private TableColumnidxRepository tableColumnidxRepository;
@Autowired @Autowired
private ImageRepository imageRepository; private ImageRepository imageRepository;
...@@ -134,12 +144,12 @@ public class TableServiceIntegrationTest extends BaseUnitTest { ...@@ -134,12 +144,12 @@ public class TableServiceIntegrationTest extends BaseUnitTest {
@BeforeEach @BeforeEach
public void beforeEach() { public void beforeEach() {
CONTAINER_1.setDatabase(DATABASE_1); imageRepository.save(IMAGE_1);
CONTAINER_2.setDatabase(DATABASE_2); containerRepository.save(CONTAINER_1);
TABLE_1.setDatabase(DATABASE_1); containerRepository.save(CONTAINER_2);
TABLE_2.setDatabase(DATABASE_2); databaseRepository.save(DATABASE_1) /* will have 2 tables */;
tableRepository.save(TABLE_1) /* public */; tableRepository.save(TABLE_1);
tableRepository.save(TABLE_2) /* private */; tableRepository.save(TABLE_2);
} }
@Test @Test
...@@ -148,16 +158,89 @@ public class TableServiceIntegrationTest extends BaseUnitTest { ...@@ -148,16 +158,89 @@ public class TableServiceIntegrationTest extends BaseUnitTest {
/* test */ /* test */
final List<Table> response = tableService.findAll(CONTAINER_1_ID, DATABASE_1_ID, principal); final List<Table> response = tableService.findAll(CONTAINER_1_ID, DATABASE_1_ID, principal);
assertEquals(1, response.size()); assertEquals(2, response.size());
}
@Test
public void findAll_fails() {
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* test */
assertThrows(DatabaseNotFoundException.class, () -> {
tableService.findAll(CONTAINER_2_ID, DATABASE_2_ID, principal);
});
}
@Test
public void findById_succeeds() throws TableNotFoundException, DatabaseNotFoundException,
ContainerNotFoundException {
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* test */
final Table response = tableService.findById(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, principal);
assertEquals(TABLE_1_ID, response.getId());
assertEquals(TABLE_1_NAME, response.getName());
assertEquals(TABLE_1_INTERNALNAME, response.getInternalName());
}
@Test
public void findById_tableNotFound_fails() {
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* test */
assertThrows(TableNotFoundException.class, () -> {
tableService.findById(CONTAINER_1_ID, DATABASE_1_ID, TABLE_3_ID, principal);
});
} }
@Test @Test
public void findAll_fails() throws DatabaseNotFoundException { public void findById_databaseNotFound_fails() {
final Principal principal = new BasicUserPrincipal(USER_2_USERNAME) /* not owner */; final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* test */
assertThrows(DatabaseNotFoundException.class, () -> {
tableService.findById(CONTAINER_2_ID, DATABASE_3_ID, TABLE_3_ID, principal);
});
}
@Test
public void findById_containerNotFound_fails() {
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* test */
assertThrows(ContainerNotFoundException.class, () -> {
tableService.findById(CONTAINER_3_ID, DATABASE_3_ID, TABLE_3_ID, principal);
});
}
@Test
public void create_succeeds() throws UserNotFoundException, TableMalformedException, QueryMalformedException,
DatabaseNotFoundException, ImageNotSupportedException, TableNameExistsException,
ContainerNotFoundException {
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* mock */
when(tableidxRepository.save(any(Table.class)))
.thenReturn(TABLE_1);
when(tableColumnidxRepository.saveAll(anyList()))
.thenReturn(List.of());
/* test */
tableService.createTable(CONTAINER_1_ID, DATABASE_1_ID, TABLE_3_CREATE_DTO, principal);
}
@Test
public void delete_succeeds() throws TableMalformedException, QueryMalformedException, DatabaseNotFoundException,
ImageNotSupportedException, ContainerNotFoundException, TableNotFoundException, DataProcessingException {
final Principal principal = new BasicUserPrincipal(USER_1_USERNAME);
/* mock */
doNothing()
.when(tableidxRepository)
.delete(any(Table.class));
/* test */ /* test */
final List<Table> response = tableService.findAll(CONTAINER_2_ID, DATABASE_2_ID, principal); tableService.deleteTable(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, principal);
assertEquals(0, response.size());
} }
} }
...@@ -33,7 +33,7 @@ public class DatabaseServiceImpl implements DatabaseService { ...@@ -33,7 +33,7 @@ public class DatabaseServiceImpl implements DatabaseService {
database = databaseRepository.findPublicOrMine(containerId, databaseId, principal.getName()); database = databaseRepository.findPublicOrMine(containerId, databaseId, principal.getName());
} }
if (database.isEmpty()) { if (database.isEmpty()) {
log.error("Failed to find database"); log.error("Failed to find database with id {}", databaseId);
throw new DatabaseNotFoundException("could not find database with this id"); throw new DatabaseNotFoundException("could not find database with this id");
} }
return database.get(); return database.get();
......
...@@ -2,6 +2,7 @@ package at.tuwien.service.impl; ...@@ -2,6 +2,7 @@ package at.tuwien.service.impl;
import at.tuwien.CreateTableRawQuery; import at.tuwien.CreateTableRawQuery;
import at.tuwien.api.database.table.TableCreateDto; import at.tuwien.api.database.table.TableCreateDto;
import at.tuwien.entities.container.Container;
import at.tuwien.entities.database.Database; import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.table.Table; import at.tuwien.entities.database.table.Table;
import at.tuwien.entities.user.User; import at.tuwien.entities.user.User;
...@@ -10,6 +11,7 @@ import at.tuwien.mapper.TableMapper; ...@@ -10,6 +11,7 @@ import at.tuwien.mapper.TableMapper;
import at.tuwien.repository.elastic.TableColumnidxRepository; import at.tuwien.repository.elastic.TableColumnidxRepository;
import at.tuwien.repository.elastic.TableidxRepository; import at.tuwien.repository.elastic.TableidxRepository;
import at.tuwien.repository.jpa.TableRepository; import at.tuwien.repository.jpa.TableRepository;
import at.tuwien.service.ContainerService;
import at.tuwien.service.DatabaseService; import at.tuwien.service.DatabaseService;
import at.tuwien.service.TableService; import at.tuwien.service.TableService;
import at.tuwien.service.UserService; import at.tuwien.service.UserService;
...@@ -34,17 +36,19 @@ public class TableServiceImpl extends HibernateConnector implements TableService ...@@ -34,17 +36,19 @@ public class TableServiceImpl extends HibernateConnector implements TableService
private final UserService userService; private final UserService userService;
private final TableRepository tableRepository; private final TableRepository tableRepository;
private final DatabaseService databaseService; private final DatabaseService databaseService;
private final ContainerService containerService;
private final TableidxRepository tableidxRepository; private final TableidxRepository tableidxRepository;
private final TableColumnidxRepository tableColumnidxRepository; private final TableColumnidxRepository tableColumnidxRepository;
@Autowired @Autowired
public TableServiceImpl(TableMapper tableMapper, UserService userService, TableRepository tableRepository, public TableServiceImpl(TableMapper tableMapper, UserService userService, TableRepository tableRepository,
DatabaseService databaseService, TableidxRepository tableidxRepository, DatabaseService databaseService, ContainerService containerService,
TableColumnidxRepository tableColumnidxRepository) { TableidxRepository tableidxRepository, TableColumnidxRepository tableColumnidxRepository) {
this.tableMapper = tableMapper; this.tableMapper = tableMapper;
this.userService = userService; this.userService = userService;
this.tableRepository = tableRepository; this.tableRepository = tableRepository;
this.databaseService = databaseService; this.databaseService = databaseService;
this.containerService = containerService;
this.tableidxRepository = tableidxRepository; this.tableidxRepository = tableidxRepository;
this.tableColumnidxRepository = tableColumnidxRepository; this.tableColumnidxRepository = tableColumnidxRepository;
} }
...@@ -61,7 +65,7 @@ public class TableServiceImpl extends HibernateConnector implements TableService ...@@ -61,7 +65,7 @@ public class TableServiceImpl extends HibernateConnector implements TableService
@Transactional @Transactional
public void deleteTable(Long containerId, Long databaseId, Long tableId, Principal principal) public void deleteTable(Long containerId, Long databaseId, Long tableId, Principal principal)
throws TableNotFoundException, DatabaseNotFoundException, ImageNotSupportedException, throws TableNotFoundException, DatabaseNotFoundException, ImageNotSupportedException,
TableMalformedException, QueryMalformedException { TableMalformedException, QueryMalformedException, ContainerNotFoundException {
/* find */ /* find */
final Database database = databaseService.findPublicOrMineById(containerId, databaseId, principal); final Database database = databaseService.findPublicOrMineById(containerId, databaseId, principal);
final Table table = findById(containerId, databaseId, tableId, principal); final Table table = findById(containerId, databaseId, tableId, principal);
...@@ -90,7 +94,8 @@ public class TableServiceImpl extends HibernateConnector implements TableService ...@@ -90,7 +94,8 @@ public class TableServiceImpl extends HibernateConnector implements TableService
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public Table findById(Long containerId, Long databaseId, Long tableId, Principal principal) public Table findById(Long containerId, Long databaseId, Long tableId, Principal principal)
throws TableNotFoundException, DatabaseNotFoundException { throws TableNotFoundException, DatabaseNotFoundException, ContainerNotFoundException {
final Container container = containerService.find(containerId);
final Database database = databaseService.findPublicOrMineById(containerId, databaseId, principal); final Database database = databaseService.findPublicOrMineById(containerId, databaseId, principal);
final Optional<Table> optional = tableRepository.findByDatabaseAndId(database, tableId); final Optional<Table> optional = tableRepository.findByDatabaseAndId(database, tableId);
if (optional.isEmpty()) { if (optional.isEmpty()) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment