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

somehow tried to test the table service

Former-commit-id: 0551c67f
parent ed177bfa
No related branches found
No related tags found
1 merge request!23Sprint results
...@@ -46,7 +46,7 @@ public class TableEndpoint { ...@@ -46,7 +46,7 @@ public class TableEndpoint {
@ApiResponse(code = 401, message = "Not authorized to list all tables."), @ApiResponse(code = 401, message = "Not authorized to list all tables."),
}) })
public ResponseEntity<List<TableBriefDto>> findAll(@PathVariable("id") Long databaseId) public ResponseEntity<List<TableBriefDto>> findAll(@PathVariable("id") Long databaseId)
throws DatabaseNotFoundException { throws DatabaseNotFoundException, TableNotFoundException {
final List<Table> tables = tableService.findAll(databaseId); final List<Table> tables = tableService.findAll(databaseId);
log.debug("received tables {}", tables); log.debug("received tables {}", tables);
return ResponseEntity.ok(tables.stream() return ResponseEntity.ok(tables.stream()
......
...@@ -197,6 +197,7 @@ public abstract class BaseUnitTest { ...@@ -197,6 +197,7 @@ public abstract class BaseUnitTest {
.isPublic(false) .isPublic(false)
.name(DATABASE_1_NAME) .name(DATABASE_1_NAME)
.container(CONTAINER_1) .container(CONTAINER_1)
.tables(List.of(TABLE_1))
.internalName(DATABASE_1_INTERNALNAME) .internalName(DATABASE_1_INTERNALNAME)
.build(); .build();
...@@ -207,6 +208,7 @@ public abstract class BaseUnitTest { ...@@ -207,6 +208,7 @@ public abstract class BaseUnitTest {
.lastModified(Instant.now()) .lastModified(Instant.now())
.isPublic(false) .isPublic(false)
.name(DATABASE_2_NAME) .name(DATABASE_2_NAME)
.tables(List.of())
.container(CONTAINER_2) .container(CONTAINER_2)
.internalName(DATABASE_2_INTERNALNAME) .internalName(DATABASE_2_INTERNALNAME)
.build(); .build();
......
...@@ -48,7 +48,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { ...@@ -48,7 +48,7 @@ public class TableEndpointUnitTest extends BaseUnitTest {
private TableEndpoint tableEndpoint; private TableEndpoint tableEndpoint;
@Test @Test
public void findAll_succeeds() throws DatabaseNotFoundException { public void findAll_succeeds() throws DatabaseNotFoundException, TableNotFoundException {
when(tableService.findAll(DATABASE_1_ID)) when(tableService.findAll(DATABASE_1_ID))
.thenReturn(List.of(TABLE_1)); .thenReturn(List.of(TABLE_1));
......
...@@ -32,7 +32,7 @@ import static org.mockito.Mockito.*; ...@@ -32,7 +32,7 @@ import static org.mockito.Mockito.*;
@SpringBootTest @SpringBootTest
@ExtendWith(SpringExtension.class) @ExtendWith(SpringExtension.class)
public class PostgresIntegrationTest extends BaseUnitTest { public class PostgresServiceIntegrationTest extends BaseUnitTest {
@Autowired @Autowired
private PostgresService postgresService; private PostgresService postgresService;
......
package at.tuwien.service;
import at.tuwien.BaseUnitTest;
import at.tuwien.api.database.table.TableBriefDto;
import at.tuwien.api.database.table.TableCreateDto;
import at.tuwien.api.database.table.TableDto;
import at.tuwien.endpoints.TableEndpoint;
import at.tuwien.entities.database.Database;
import at.tuwien.entities.database.table.Table;
import at.tuwien.exception.*;
import at.tuwien.repository.DatabaseRepository;
import at.tuwien.repository.TableRepository;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import java.sql.SQLException;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.*;
@SpringBootTest
@ExtendWith(SpringExtension.class)
public class TableServiceUnitTest extends BaseUnitTest {
@Autowired
private TableService tableService;
@MockBean
private DatabaseRepository databaseRepository;
@MockBean
private TableRepository tableRepository;
@MockBean
private PostgresService postgresService;
@BeforeAll
public static void beforeAll() {
TABLE_1.setDatabase(DATABASE_1);
}
@Test
public void findAll_succeeds() throws TableNotFoundException, DatabaseNotFoundException {
when(databaseRepository.findById(DATABASE_1_ID))
.thenReturn(Optional.of(DATABASE_1));
/* test */
final List<Table> response = tableService.findAll(DATABASE_1_ID);
assertEquals(1, response.size());
assertEquals(TABLE_1_ID, response.get(0).getId());
}
@Test
public void findAll_notFound_fails() {
when(databaseRepository.findById(DATABASE_1_ID))
.thenReturn(Optional.empty());
/* test */
assertThrows(DatabaseNotFoundException.class, () -> {
tableService.findAll(DATABASE_1_ID);
});
}
@Test
public void findAll_noTable_fails() {
when(databaseRepository.findById(DATABASE_2_ID))
.thenReturn(Optional.of(DATABASE_2));
/* test */
assertThrows(TableNotFoundException.class, () -> {
tableService.findAll(DATABASE_2_ID);
});
}
@Disabled("invalid mock")
@Test
public void delete_succeeds() throws TableNotFoundException, DatabaseConnectionException, TableMalformedException,
DataProcessingException {
when(tableRepository.findById(TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
doNothing()
.when(postgresService)
.deleteTable(TABLE_1);
doNothing()
.when(tableRepository)
.deleteById(TABLE_1_ID);
/* test */
tableService.delete(DATABASE_1_ID, TABLE_1_ID);
}
@Test
public void delete_notFound_fails() {
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.empty());
/* test */
assertThrows(TableNotFoundException.class, () -> {
tableService.delete(DATABASE_1_ID, TABLE_1_ID);
});
}
@Test
public void delete_noConnection_fails() throws DatabaseConnectionException, TableMalformedException,
DataProcessingException {
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.empty());
doAnswer(invocation -> new TableMalformedException("no connection"))
.when(postgresService)
.deleteTable(TABLE_1);
/* test */
assertThrows(TableNotFoundException.class, () -> {
tableService.delete(DATABASE_1_ID, TABLE_1_ID);
});
}
@Test
public void delete_noSql_fails() throws DataProcessingException {
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.empty());
/* test */
assertThrows(TableNotFoundException.class, () -> {
tableService.delete(DATABASE_1_ID, TABLE_1_ID);
});
}
@Test
public void findById_succeeds() throws TableNotFoundException {
when(tableRepository.findByDatabaseAndId(DATABASE_1, TABLE_1_ID))
.thenReturn(Optional.of(TABLE_1));
/* test */
final Table response = tableService.findById(DATABASE_1_ID, TABLE_1_ID);
assertEquals(TABLE_1_ID, response.getId());
assertEquals(TABLE_1_NAME, response.getName());
}
@Test
public void findById_noTable_fails() {
when(tableRepository.findByDatabaseAndId(DATABASE_1, 9999L))
.thenReturn(Optional.empty());
/* test */
assertThrows(TableNotFoundException.class, () -> {
tableService.findById(DATABASE_1_ID, 9999L);
});
}
@Test
public void create_succeeds() throws DatabaseConnectionException, TableMalformedException,
DatabaseNotFoundException, ImageNotSupportedException, DataProcessingException {
final TableCreateDto request = TableCreateDto.builder()
.name(TABLE_1_NAME)
.columns(COLUMNS5)
.description(TABLE_1_DESCRIPTION)
.build();
when(tableRepository.save(any()))
.thenReturn(TABLE_1);
when(databaseRepository.findById(DATABASE_1_ID))
.thenReturn(Optional.of(DATABASE_1));
doNothing()
.when(postgresService)
.createTable(DATABASE_1, request);
/* test */
final Table response = tableService.create(DATABASE_1_ID, request);
assertEquals(TABLE_1_ID, response.getId());
assertEquals(TABLE_1_NAME, response.getName());
}
@Disabled("cannot yet test private method")
@Test
public void create_noPostgres_fails() {
}
@Disabled("cannot yet test private method")
@Test
public void create_noConnection_fails() {
}
@Disabled("invalid mock")
@Test
public void create_noSql_fails() throws DataProcessingException {
final TableCreateDto request = TableCreateDto.builder()
.name(TABLE_1_NAME)
.columns(COLUMNS5)
.description(TABLE_1_DESCRIPTION)
.build();
when(tableRepository.save(any()))
.thenReturn(TABLE_1);
when(databaseRepository.findById(DATABASE_1_ID))
.thenReturn(Optional.of(DATABASE_1));
doAnswer(invocation -> new TableMalformedException("no sql"))
.when(postgresService.getCreateTableStatement(any(), request));
/* test */
assertThrows(TableMalformedException.class, () -> {
tableService.create(DATABASE_1_ID, request);
});
}
}
...@@ -47,7 +47,7 @@ public class TableService { ...@@ -47,7 +47,7 @@ public class TableService {
this.tableMapper = tableMapper; this.tableMapper = tableMapper;
} }
public List<Table> findAll(Long databaseId) throws DatabaseNotFoundException { public List<Table> findAll(Long databaseId) throws DatabaseNotFoundException, TableNotFoundException {
final Optional<Database> database; final Optional<Database> database;
try { try {
database = databaseRepository.findById(databaseId); database = databaseRepository.findById(databaseId);
...@@ -59,14 +59,11 @@ public class TableService { ...@@ -59,14 +59,11 @@ public class TableService {
log.error("Unable to find database {}", databaseId); log.error("Unable to find database {}", databaseId);
throw new DatabaseNotFoundException("Unable to find database."); throw new DatabaseNotFoundException("Unable to find database.");
} }
final List<Table> tables; if (database.get().getTables().size() == 0) {
try {
tables = tableRepository.findByDatabase(database.get());
} catch (EntityNotFoundException e) {
log.error("Unable to find tables for database {}.", database); log.error("Unable to find tables for database {}.", database);
throw new DatabaseNotFoundException("Unable to find tables."); throw new TableNotFoundException("Unable to find tables.");
} }
return tables; return database.get().getTables();
} }
public void delete(Long databaseId, Long tableId) throws TableNotFoundException, DatabaseConnectionException, TableMalformedException, DataProcessingException { public void delete(Long databaseId, Long tableId) throws TableNotFoundException, DatabaseConnectionException, TableMalformedException, DataProcessingException {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment