diff --git a/fda-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java b/fda-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java index e9ac17495553e415d209d3955cd3969abbd847cc..5736265039d11360ac8447d9dc0902bdeb8b4389 100644 --- a/fda-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java +++ b/fda-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java @@ -46,7 +46,7 @@ public class TableEndpoint { @ApiResponse(code = 401, message = "Not authorized to list all tables."), }) public ResponseEntity<List<TableBriefDto>> findAll(@PathVariable("id") Long databaseId) - throws DatabaseNotFoundException { + throws DatabaseNotFoundException, TableNotFoundException { final List<Table> tables = tableService.findAll(databaseId); log.debug("received tables {}", tables); return ResponseEntity.ok(tables.stream() diff --git a/fda-table-service/rest-service/src/test/java/at/tuwien/BaseUnitTest.java b/fda-table-service/rest-service/src/test/java/at/tuwien/BaseUnitTest.java index ac7327bca19963b7a854775efcdcf64d5e68f1a1..590d23f804441a15d37e84bd42f87d3460d0ce82 100644 --- a/fda-table-service/rest-service/src/test/java/at/tuwien/BaseUnitTest.java +++ b/fda-table-service/rest-service/src/test/java/at/tuwien/BaseUnitTest.java @@ -197,6 +197,7 @@ public abstract class BaseUnitTest { .isPublic(false) .name(DATABASE_1_NAME) .container(CONTAINER_1) + .tables(List.of(TABLE_1)) .internalName(DATABASE_1_INTERNALNAME) .build(); @@ -207,6 +208,7 @@ public abstract class BaseUnitTest { .lastModified(Instant.now()) .isPublic(false) .name(DATABASE_2_NAME) + .tables(List.of()) .container(CONTAINER_2) .internalName(DATABASE_2_INTERNALNAME) .build(); diff --git a/fda-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointUnitTest.java b/fda-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointUnitTest.java index 0ca5b7b01e1334a62482237bf75d9ad13cd95efa..419680cbc05d4dac6d743547bd24cb9663f25e7e 100644 --- a/fda-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointUnitTest.java +++ b/fda-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointUnitTest.java @@ -48,7 +48,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { private TableEndpoint tableEndpoint; @Test - public void findAll_succeeds() throws DatabaseNotFoundException { + public void findAll_succeeds() throws DatabaseNotFoundException, TableNotFoundException { when(tableService.findAll(DATABASE_1_ID)) .thenReturn(List.of(TABLE_1)); diff --git a/fda-table-service/rest-service/src/test/java/at/tuwien/service/PostgresIntegrationTest.java b/fda-table-service/rest-service/src/test/java/at/tuwien/service/PostgresServiceIntegrationTest.java similarity index 98% rename from fda-table-service/rest-service/src/test/java/at/tuwien/service/PostgresIntegrationTest.java rename to fda-table-service/rest-service/src/test/java/at/tuwien/service/PostgresServiceIntegrationTest.java index 1093a4b4753a88bbcdfe6057a29fdf97cb82d50f..e17b1f2bdba429d213a330e2be7b472a22cb0cce 100644 --- a/fda-table-service/rest-service/src/test/java/at/tuwien/service/PostgresIntegrationTest.java +++ b/fda-table-service/rest-service/src/test/java/at/tuwien/service/PostgresServiceIntegrationTest.java @@ -32,7 +32,7 @@ import static org.mockito.Mockito.*; @SpringBootTest @ExtendWith(SpringExtension.class) -public class PostgresIntegrationTest extends BaseUnitTest { +public class PostgresServiceIntegrationTest extends BaseUnitTest { @Autowired private PostgresService postgresService; diff --git a/fda-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceUnitTest.java b/fda-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceUnitTest.java new file mode 100644 index 0000000000000000000000000000000000000000..8cc8006d8fd99e718b5997f28c1ce99823ca3476 --- /dev/null +++ b/fda-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceUnitTest.java @@ -0,0 +1,218 @@ +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); + }); + } + +} diff --git a/fda-table-service/services/src/main/java/at/tuwien/service/TableService.java b/fda-table-service/services/src/main/java/at/tuwien/service/TableService.java index fada3f8f12b999c4dc4cbb88fcba59ab5d59f247..d1e22344f9299888abc3a2b6304be25f20d47408 100644 --- a/fda-table-service/services/src/main/java/at/tuwien/service/TableService.java +++ b/fda-table-service/services/src/main/java/at/tuwien/service/TableService.java @@ -47,7 +47,7 @@ public class TableService { this.tableMapper = tableMapper; } - public List<Table> findAll(Long databaseId) throws DatabaseNotFoundException { + public List<Table> findAll(Long databaseId) throws DatabaseNotFoundException, TableNotFoundException { final Optional<Database> database; try { database = databaseRepository.findById(databaseId); @@ -59,14 +59,11 @@ public class TableService { log.error("Unable to find database {}", databaseId); throw new DatabaseNotFoundException("Unable to find database."); } - final List<Table> tables; - try { - tables = tableRepository.findByDatabase(database.get()); - } catch (EntityNotFoundException e) { + if (database.get().getTables().size() == 0) { 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 {