diff --git a/dbrepo-table-service/pom.xml b/dbrepo-table-service/pom.xml index b686d22c3b998f5a21b851b03cae9c6361d52150..3ba58968ee792e4dd166579af50d5af58ceee98f 100644 --- a/dbrepo-table-service/pom.xml +++ b/dbrepo-table-service/pom.xml @@ -25,7 +25,6 @@ <java.version>17</java.version> <spring-cloud.version>4.0.2</spring-cloud.version> <mapstruct.version>1.5.5.Final</mapstruct.version> - <docker.version>3.3.0</docker.version> <jacoco.version>0.8.10</jacoco.version> <jwt.version>4.3.0</jwt.version> <opencsv.version>5.7.1</opencsv.version> @@ -34,6 +33,8 @@ <c3p0.version>0.9.5.5</c3p0.version> <c3p0-hibernate.version>6.2.2.Final</c3p0-hibernate.version> <springdoc-openapi.version>2.1.0</springdoc-openapi.version> + <hsqldb.version>2.7.2</hsqldb.version> + <testcontainers.version>1.18.3</testcontainers.version> <opensearch-client.version>1.1.0</opensearch-client.version> </properties> @@ -172,8 +173,27 @@ </exclusions> </dependency> <dependency> - <groupId>com.h2database</groupId> - <artifactId>h2</artifactId> + <groupId>org.hsqldb</groupId> + <artifactId>hsqldb</artifactId> + <version>${hsqldb.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>junit-jupiter</artifactId> + <version>${testcontainers.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>mariadb</artifactId> + <version>${testcontainers.version}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.testcontainers</groupId> + <artifactId>rabbitmq</artifactId> + <version>${testcontainers.version}</version> <scope>test</scope> </dependency> <dependency> @@ -191,23 +211,6 @@ <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> - <!-- Docker --> - <dependency> - <groupId>com.github.docker-java</groupId> - <artifactId>docker-java</artifactId> - <version>${docker.version}</version> - <exclusions> - <exclusion> - <groupId>javax.ws.rs</groupId> - <artifactId>jsr311-api</artifactId> - </exclusion> - </exclusions> - </dependency> - <dependency> - <groupId>com.github.docker-java</groupId> - <artifactId>docker-java-transport-httpclient5</artifactId> - <version>${docker.version}</version> - </dependency> <!-- Mapping --> <dependency> <groupId>org.mapstruct</groupId> diff --git a/dbrepo-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableColumnEndpoint.java b/dbrepo-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableColumnEndpoint.java index 5d025939ba6a1ab45725474ad375a0070df26f68..2ec824e75864f149bd77aee77739b54616c8da99 100644 --- a/dbrepo-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableColumnEndpoint.java +++ b/dbrepo-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableColumnEndpoint.java @@ -16,6 +16,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; @@ -23,15 +25,12 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; -import jakarta.validation.Valid; -import jakarta.validation.constraints.NotNull; - import java.security.Principal; @Log4j2 @CrossOrigin(origins = "*") @RestController -@RequestMapping("/api/container/{id}/database/{databaseId}/table/{tableId}/column/{columnId}") +@RequestMapping("/api/database/{id}/table/{tableId}/column/{columnId}") public class TableColumnEndpoint { private final TableMapper tableMapper; @@ -77,8 +76,7 @@ public class TableColumnEndpoint { mediaType = "application/json", schema = @Schema(implementation = ColumnDto.class))}), }) - public ResponseEntity<ColumnDto> update(@NotNull @PathVariable("id") Long containerId, - @NotNull @PathVariable("databaseId") Long databaseId, + public ResponseEntity<ColumnDto> update(@NotNull @PathVariable("id") Long id, @NotNull @PathVariable("tableId") Long tableId, @NotNull @PathVariable("columnId") Long columnId, @NotNull @Valid @RequestBody ColumnSemanticsUpdateDto updateDto, @@ -87,14 +85,13 @@ public class TableColumnEndpoint { throws TableNotFoundException, TableMalformedException, DatabaseNotFoundException, ContainerNotFoundException, NotAllowedException, SemanticEntityPersistException, SemanticEntityNotFoundException { - log.debug("endpoint update table, containerId={}, databaseId={}, tableId={}, principal={}", containerId, - databaseId, tableId, principal); + log.debug("endpoint update table, id={}, tableId={}, principal={}", id, tableId, principal); if (!User.hasRole(principal, "modify-foreign-table-column-semantics")) { - endpointValidator.validateOnlyAccess(containerId, databaseId, principal, true); - endpointValidator.validateOnlyOwnerOrWriteAll(containerId, databaseId, tableId, principal); + endpointValidator.validateOnlyAccess(id, principal, true); + endpointValidator.validateOnlyOwnerOrWriteAll(id, tableId, principal); } - final TableColumn column = tableService.update(containerId, databaseId, tableId, columnId, updateDto, authorization); - log.info("Updated table semantics of table with id {} and database with id {}", tableId, databaseId); + final TableColumn column = tableService.update(id, tableId, columnId, updateDto, authorization); + log.info("Updated table semantics of table with id {} and database with id {}", tableId, id); final ColumnDto dto = tableMapper.tableColumnToColumnDto(column); return ResponseEntity.accepted() .body(dto); diff --git a/dbrepo-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java b/dbrepo-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java index a5d7bf648c6df653f428dd6414a7957f9d3a3a90..81239bc43c06afbaff7648b59a545a89b9a9e2ed 100644 --- a/dbrepo-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java +++ b/dbrepo-table-service/rest-service/src/main/java/at/tuwien/endpoints/TableEndpoint.java @@ -1,7 +1,8 @@ package at.tuwien.endpoints; -import at.tuwien.api.container.ContainerBriefDto; -import at.tuwien.api.database.table.*; +import at.tuwien.api.database.table.TableBriefDto; +import at.tuwien.api.database.table.TableCreateDto; +import at.tuwien.api.database.table.TableDto; import at.tuwien.api.error.ApiErrorDto; import at.tuwien.entities.database.table.Table; import at.tuwien.exception.*; @@ -17,6 +18,8 @@ import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.security.SecurityRequirement; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; @@ -25,8 +28,6 @@ import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.*; -import jakarta.validation.Valid; -import jakarta.validation.constraints.NotNull; import java.security.Principal; import java.util.List; import java.util.stream.Collectors; @@ -34,7 +35,7 @@ import java.util.stream.Collectors; @Log4j2 @CrossOrigin(origins = "*") @RestController -@RequestMapping("/api/container/{id}/database/{databaseId}/table") +@RequestMapping("/api/database/{databaseId}/table") public class TableEndpoint { private final TableMapper tableMapper; @@ -72,15 +73,13 @@ public class TableEndpoint { mediaType = "application/json", schema = @Schema(implementation = ApiErrorDto.class))}), }) - public ResponseEntity<List<TableBriefDto>> list(@NotNull @PathVariable("id") Long containerId, - @NotNull @PathVariable("databaseId") Long databaseId, + public ResponseEntity<List<TableBriefDto>> list(@NotNull @PathVariable("databaseId") Long databaseId, Principal principal) throws DatabaseNotFoundException, NotAllowedException { - log.debug("endpoint list tables, containerId={}, databaseId={}, principal={}", containerId, databaseId, - principal); - endpointValidator.validateOnlyPrivateAccess(containerId, databaseId, principal); - endpointValidator.validateOnlyPrivateHasRole(containerId, databaseId, principal, "list-tables"); - final List<TableBriefDto> dto = tableService.findAll(containerId, databaseId) + log.debug("endpoint list tables, databaseId={}, principal={}", databaseId, principal); + endpointValidator.validateOnlyPrivateAccess(databaseId, principal); + endpointValidator.validateOnlyPrivateHasRole(databaseId, principal, "list-tables"); + final List<TableBriefDto> dto = tableService.findAll(databaseId) .stream() .map(tableMapper::tableToTableBriefDto) .collect(Collectors.toList()); @@ -130,17 +129,15 @@ public class TableEndpoint { mediaType = "application/json", schema = @Schema(implementation = ApiErrorDto.class))}), }) - public ResponseEntity<TableBriefDto> create(@NotNull @PathVariable("id") Long containerId, - @NotNull @PathVariable("databaseId") Long databaseId, + public ResponseEntity<TableBriefDto> create(@NotNull @PathVariable("databaseId") Long databaseId, @NotNull @Valid @RequestBody TableCreateDto createDto, @NotNull Principal principal) throws ImageNotSupportedException, DatabaseNotFoundException, TableMalformedException, AmqpException, TableNameExistsException, ContainerNotFoundException, UserNotFoundException, QueryMalformedException, NotAllowedException { - log.debug("endpoint create table, containerId={}, databaseId={}, createDto={}, principal={}", containerId, - databaseId, createDto, principal); - endpointValidator.validateOnlyAccess(containerId, databaseId, principal, true); - final Table table = tableService.createTable(containerId, databaseId, createDto, principal); + log.debug("endpoint create table, databaseId={}, createDto={}, principal={}", databaseId, createDto, principal); + endpointValidator.validateOnlyAccess(databaseId, principal, true); + final Table table = tableService.createTable(databaseId, createDto, principal); amqpService.create(table); final TableBriefDto dto = tableMapper.tableToTableBriefDto(table); log.trace("create table resulted in table {}", dto); @@ -175,14 +172,12 @@ public class TableEndpoint { mediaType = "application/json", schema = @Schema(implementation = ApiErrorDto.class))}), }) - public ResponseEntity<TableDto> findById(@NotNull @PathVariable("id") Long containerId, - @NotNull @PathVariable("databaseId") Long databaseId, + public ResponseEntity<TableDto> findById(@NotNull @PathVariable("databaseId") Long databaseId, @NotNull @PathVariable("tableId") Long tableId, Principal principal) throws TableNotFoundException, DatabaseNotFoundException, ContainerNotFoundException { - log.debug("endpoint find table, containerId={}, databaseId={}, tableId={}, principal={}", containerId, - databaseId, tableId, principal); - final Table table = tableService.findById(containerId, databaseId, tableId); + log.debug("endpoint find table, databaseId={}, tableId={}, principal={}", databaseId, tableId, principal); + final Table table = tableService.findById(databaseId, tableId); final TableDto dto = tableMapper.tableToTableDto(table); log.trace("find table resulted in table {}", dto); return ResponseEntity.ok(dto); @@ -230,15 +225,13 @@ public class TableEndpoint { mediaType = "application/json", schema = @Schema(implementation = ApiErrorDto.class))}), }) - public ResponseEntity<Void> delete(@NotNull @PathVariable("id") Long containerId, - @NotNull @PathVariable("databaseId") Long databaseId, + public ResponseEntity<Void> delete(@NotNull @PathVariable("databaseId") Long databaseId, @NotNull @PathVariable("tableId") Long tableId, @NotNull Principal principal) throws TableNotFoundException, DatabaseNotFoundException, ImageNotSupportedException, DataProcessingException, ContainerNotFoundException, TableMalformedException, QueryMalformedException { - log.debug("endpoint delete table, containerId={}, databaseId={}, tableId={}, principal={}", containerId, - databaseId, tableId, principal); - tableService.deleteTable(containerId, databaseId, tableId); + log.debug("endpoint delete table, databaseId={}, tableId={}, principal={}", databaseId, tableId, principal); + tableService.deleteTable(databaseId, tableId); return ResponseEntity.accepted() .build(); } diff --git a/dbrepo-table-service/rest-service/src/main/java/at/tuwien/validation/EndpointValidator.java b/dbrepo-table-service/rest-service/src/main/java/at/tuwien/validation/EndpointValidator.java index 006f3b30a740aca837a50e657258848b83a1a6dc..7b4ff76b07a2cb15e4e2e8329a32d9344a017759 100644 --- a/dbrepo-table-service/rest-service/src/main/java/at/tuwien/validation/EndpointValidator.java +++ b/dbrepo-table-service/rest-service/src/main/java/at/tuwien/validation/EndpointValidator.java @@ -33,25 +33,25 @@ public class EndpointValidator { this.tableService = tableService; } - public void validateOnlyPrivateAccess(Long containerId, Long databaseId, Principal principal, boolean writeAccessOnly) throws NotAllowedException, DatabaseNotFoundException { - final Database database = databaseService.find(containerId, databaseId); + public void validateOnlyPrivateAccess(Long databaseId, Principal principal, boolean writeAccessOnly) throws NotAllowedException, DatabaseNotFoundException { + final Database database = databaseService.find(databaseId); if (database.getIsPublic()) { log.trace("database with id {} is public: no access needed", databaseId); return; } - validateOnlyAccess(containerId, databaseId, principal, writeAccessOnly); + validateOnlyAccess(databaseId, principal, writeAccessOnly); } - public void validateOnlyPrivateAccess(Long containerId, Long databaseId, Principal principal) throws NotAllowedException, DatabaseNotFoundException { - validateOnlyPrivateAccess(containerId, databaseId, principal, false); + public void validateOnlyPrivateAccess(Long databaseId, Principal principal) throws NotAllowedException, DatabaseNotFoundException { + validateOnlyPrivateAccess(databaseId, principal, false); } - public void validateOnlyAccess(Long containerId, Long databaseId, Principal principal, boolean writeAccessOnly) throws NotAllowedException, DatabaseNotFoundException { + public void validateOnlyAccess(Long databaseId, Principal principal, boolean writeAccessOnly) throws NotAllowedException, DatabaseNotFoundException { if (principal == null) { log.error("Access not allowed: database with id {} is not public and no authorization provided", databaseId); throw new NotAllowedException("Access not allowed: database with id " + databaseId + " is not public and no authorization provided"); } - databaseService.find(containerId, databaseId); + databaseService.find(databaseId); log.trace("principal: {}", principal.getName()); final DatabaseAccess access = accessService.find(databaseId, principal.getName()); log.trace("found access: {}", access); @@ -61,13 +61,13 @@ public class EndpointValidator { } } - public void validateOnlyOwnerOrWriteAll(Long containerId, Long databaseId, Long tableId, Principal principal) + public void validateOnlyOwnerOrWriteAll(Long databaseId, Long tableId, Principal principal) throws DatabaseNotFoundException, NotAllowedException, TableNotFoundException, ContainerNotFoundException { if (principal == null) { log.error("Access not allowed: no authorization provided"); throw new NotAllowedException("Access not allowed: no authorization provided"); } - final Table table = tableService.findById(containerId, databaseId, tableId); + final Table table = tableService.findById(databaseId, tableId); log.trace("principal: {}", principal.getName()); log.trace("table creator: {}", table.getCreator().getUsername()); final DatabaseAccess access = accessService.find(databaseId, principal.getName()); @@ -88,9 +88,9 @@ public class EndpointValidator { throw new NotAllowedException("Access not allowed: insufficient access (neither creator nor write-all access)"); } - public void validateOnlyPrivateHasRole(Long containerId, Long databaseId, Principal principal, String role) + public void validateOnlyPrivateHasRole(Long databaseId, Principal principal, String role) throws DatabaseNotFoundException, NotAllowedException { - final Database database = databaseService.find(containerId, databaseId); + final Database database = databaseService.find(databaseId); if (database.getIsPublic()) { log.trace("database with id {} is public: no access needed", databaseId); return; diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/config/GatewayConfig.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/config/GatewayConfig.java deleted file mode 100644 index 659ac26ff1cbe9a09e46d794a991f112a3f60c3c..0000000000000000000000000000000000000000 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/config/GatewayConfig.java +++ /dev/null @@ -1,33 +0,0 @@ -package at.tuwien.config; - -import lombok.Getter; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.client.support.BasicAuthenticationInterceptor; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.DefaultUriBuilderFactory; - -@Getter -@Configuration -public class GatewayConfig { - - @Value("${spring.rabbitmq.host}") - private String hostname; - - @Value("${spring.rabbitmq.username}") - private String brokerUsername; - - @Value("${spring.rabbitmq.password}") - private String brokerPassword; - - @Bean("brokerRestTemplate") - public RestTemplate brokerRestTemplate() { - final RestTemplate restTemplate = new RestTemplate(); - restTemplate.setUriTemplateHandler(new DefaultUriBuilderFactory("http://" + hostname + ":15672")); - restTemplate.getInterceptors() - .add(new BasicAuthenticationInterceptor(brokerUsername, brokerPassword)); - return restTemplate; - } - -} \ No newline at end of file diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/config/MariaDbConfig.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/config/MariaDbConfig.java index a469eadf815d07d9a2fbfbde175a74dd15202145..236dd9cf171d41cd133a130132c9b8bc72386b45 100644 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/config/MariaDbConfig.java +++ b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/config/MariaDbConfig.java @@ -1,14 +1,8 @@ package at.tuwien.config; -import at.tuwien.api.database.AccessTypeDto; -import at.tuwien.api.database.DatabaseGiveAccessDto; import at.tuwien.entities.container.Container; import at.tuwien.entities.database.Database; -import at.tuwien.entities.user.User; -import at.tuwien.exception.QueryMalformedException; -import at.tuwien.mapper.DatabaseMapper; import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; @@ -22,15 +16,11 @@ import java.util.List; @Configuration public class MariaDbConfig { - @Autowired - private DatabaseMapper databaseMapper; - /** * Inserts a query into a created database with given hostname and database name. The method uses the JDBC in-out * notation <a href="#{@link}">{@link https://learn.microsoft.com/en-us/sql/connect/jdbc/using-sql-escape-sequences?view=sql-server-ver16#stored-procedure-calls}</a> * - * @param hostname The hostname. - * @param database The database name. + * @param database The database. * @param query The query. * @param username The connection username. * @param password The connection password. @@ -117,24 +107,6 @@ public class MariaDbConfig { } } - public void mockGrantUserPermissions(Container container, Database database, User user) throws SQLException, - QueryMalformedException { - final String jdbc = "jdbc:mariadb://" + container.getHost() + ":" + container.getPort() + "/" + database.getInternalName(); - log.trace("connect to database {}", jdbc); - try (Connection connection = DriverManager.getConnection(jdbc, container.getPrivilegedUsername(), container.getPrivilegedPassword())) { - final DatabaseGiveAccessDto access = DatabaseGiveAccessDto.builder() - .username(user.getUsername()) - .type(AccessTypeDto.WRITE_ALL) - .build(); - final PreparedStatement statement1 = databaseMapper.rawGrantUserAccessQuery(connection, access); - statement1.executeUpdate(); - final PreparedStatement statement2 = databaseMapper.rawGrantUserProcedure(connection, user); - statement2.executeUpdate(); - final PreparedStatement statement3 = databaseMapper.rawFlushPrivileges(connection); - statement3.executeUpdate(); - } - } - public static List<String> getUsernames(String hostname, String database, String username, String password) throws SQLException { final String jdbc = "jdbc:mariadb://" + hostname + "/" + database; @@ -187,8 +159,7 @@ public class MariaDbConfig { * Inserts a query into a created database with given hostname and database name. The method uses the JDBC in-out * notation <a href="#{@link}">{@link https://learn.microsoft.com/en-us/sql/connect/jdbc/using-sql-escape-sequences?view=sql-server-ver16#stored-procedure-calls}</a> * - * @param hostname The hostname. - * @param database The database name. + * @param database The database. * @param query The query. * @param username The connection username. * @param password The connection password. @@ -218,8 +189,7 @@ public class MariaDbConfig { * Inserts a query into a created database with given hostname and database name. The method uses the JDBC in-out * notation <a href="#{@link}">{@link https://learn.microsoft.com/en-us/sql/connect/jdbc/using-sql-escape-sequences?view=sql-server-ver16#stored-procedure-calls}</a> * - * @param hostname The hostname. - * @param database The database name. + * @param database The database. * @param query The query. * @return The generated or retrieved query id. * @throws SQLException The procedure did not succeed. diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableColumnEndpointUnitTest.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableColumnEndpointUnitTest.java index 6ba5c6fee755d26f86237f5a0d864f59a5434e53..9e7b741ec0e02cc599057738d9fa1787e1d99ba2 100644 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableColumnEndpointUnitTest.java +++ b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableColumnEndpointUnitTest.java @@ -33,8 +33,9 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import java.security.Principal; -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; @Log4j2 @EnableAutoConfiguration(exclude = RabbitAutoConfiguration.class) @@ -75,7 +76,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(AccessDeniedException.class, () -> { - generic_update(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, null, null, null); + generic_update(DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, null, null, null); }); } @@ -85,7 +86,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_update(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, null); + generic_update(DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, null); }); } @@ -95,7 +96,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_update(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_READ_ACCESS); + generic_update(DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_READ_ACCESS); }); } @@ -106,7 +107,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { SemanticEntityPersistException, SemanticEntityNotFoundException { /* test */ - generic_update(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, COLUMN_8_2_WITH_SEMANTICS, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); + generic_update(DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, COLUMN_8_2_WITH_SEMANTICS, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); } @Test @@ -115,7 +116,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_update(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_3_USER_2_WRITE_OWN_ACCESS); + generic_update(DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_3_USER_2_WRITE_OWN_ACCESS); }); } @@ -125,7 +126,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - generic_update(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, null, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); + generic_update(DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, null, TABLE_8, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); }); } @@ -135,7 +136,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(TableNotFoundException.class, () -> { - generic_update(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, null, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); + generic_update(DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, null, null, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); }); } @@ -146,7 +147,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { ContainerNotFoundException, SemanticEntityPersistException, SemanticEntityNotFoundException { /* test */ - generic_update(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, COLUMN_8_2_WITH_SEMANTICS, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_3_USER_2_WRITE_ALL_ACCESS); + generic_update(DATABASE_3_ID, TABLE_8_ID, COLUMN_1_1_ID, DATABASE_3, TABLE_8, COLUMN_8_2_WITH_SEMANTICS, COLUMN_8_2_SEMANTICS_UPDATE_DTO, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_3_USER_2_WRITE_ALL_ACCESS); } /* ################################################################################################### */ @@ -159,7 +160,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(AccessDeniedException.class, () -> { - generic_update(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, null, null, null); + generic_update(DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, null, null, null); }); } @@ -169,7 +170,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_update(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, null); + generic_update(DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, null); }); } @@ -179,7 +180,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_update(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + generic_update(DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); }); } @@ -190,7 +191,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { SemanticEntityPersistException, SemanticEntityNotFoundException { /* test */ - generic_update(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, COLUMN_1_4_WITH_SEMANTICS, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); + generic_update(DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, COLUMN_1_4_WITH_SEMANTICS, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); } @Test @@ -199,7 +200,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_update(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_1_USER_2_WRITE_OWN_ACCESS); + generic_update(DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_1_USER_2_WRITE_OWN_ACCESS); }); } @@ -209,7 +210,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - generic_update(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, null, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); + generic_update(DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, null, TABLE_1, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); }); } @@ -219,7 +220,7 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(TableNotFoundException.class, () -> { - generic_update(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, null, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); + generic_update(DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, null, null, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); }); } @@ -230,14 +231,14 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { ContainerNotFoundException, SemanticEntityPersistException, SemanticEntityNotFoundException { /* test */ - generic_update(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, COLUMN_1_4_WITH_SEMANTICS, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_1_USER_2_WRITE_ALL_ACCESS); + generic_update(DATABASE_1_ID, TABLE_1_ID, COLUMN_1_1_ID, DATABASE_1, TABLE_1, COLUMN_1_4_WITH_SEMANTICS, COLUMN_1_4_SEMANTICS_UPDATE_DTO, USER_2_USERNAME, USER_2_PRINCIPAL, DATABASE_1_USER_2_WRITE_ALL_ACCESS); } /* ################################################################################################### */ /* ## GENERIC TEST CASES ## */ /* ################################################################################################### */ - protected ResponseEntity<ColumnDto> generic_update(Long containerId, Long databaseId, Long tableId, Long columnId, + protected ResponseEntity<ColumnDto> generic_update(Long databaseId, Long tableId, Long columnId, Database database, Table table, TableColumn column, ColumnSemanticsUpdateDto data, String username, Principal principal, DatabaseAccess access) @@ -246,36 +247,36 @@ public class TableColumnEndpointUnitTest extends BaseUnitTest { /* mock */ if (database != null) { - when(databaseService.find(containerId, databaseId)) + when(databaseService.find(databaseId)) .thenReturn(database); } else { doThrow(DatabaseNotFoundException.class) .when(databaseService) - .find(containerId, databaseId); + .find(databaseId); } if (table != null) { - when(tableService.findById(containerId, databaseId, tableId)) + when(tableService.findById(databaseId, tableId)) .thenReturn(table); - when(tableService.update(containerId, databaseId, tableId, columnId, data, "abc")) + when(tableService.update(databaseId, tableId, columnId, data, "abc")) .thenReturn(column); } else { doThrow(TableNotFoundException.class) .when(tableService) - .update(containerId, databaseId, tableId, columnId, data, "abc"); + .update(databaseId, tableId, columnId, data, "abc"); doThrow(TableNotFoundException.class) .when(tableService) - .findById(containerId, databaseId, tableId); + .findById(databaseId, tableId); } if (access != null) { - when(accessService.find(containerId, username)) + when(accessService.find(databaseId, username)) .thenReturn(access); } else { doThrow(NotAllowedException.class) .when(accessService) - .find(containerId, username); + .find(databaseId, username); } /* test */ - return tableColumnEndpoint.update(containerId, databaseId, tableId, columnId, data, principal, "abc"); + return tableColumnEndpoint.update(databaseId, tableId, columnId, data, principal, "abc"); } } diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointIntegrationTest.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointIntegrationTest.java index ab7b30e6a3f35428b557a8255e9c08b1af81505f..4f4cd00d01df49be8c27983b3d99f48aa86b9ba5 100644 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointIntegrationTest.java +++ b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointIntegrationTest.java @@ -1,9 +1,8 @@ package at.tuwien.endpoint; import at.tuwien.BaseUnitTest; -import at.tuwien.config.DockerConfig; -import at.tuwien.config.H2Utils; import at.tuwien.config.IndexConfig; +import at.tuwien.config.MariaDbConfig; import at.tuwien.config.ReadyConfig; import at.tuwien.endpoints.TableEndpoint; import at.tuwien.exception.*; @@ -12,7 +11,6 @@ import at.tuwien.repository.sdb.TableIdxRepository; import at.tuwien.repository.mdb.*; import com.rabbitmq.client.Channel; import lombok.extern.log4j.Log4j2; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -23,8 +21,14 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.mock.mockito.MockBean; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.testcontainers.containers.MariaDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import java.sql.SQLException; @Log4j2 +@Testcontainers @EnableAutoConfiguration(exclude = RabbitAutoConfiguration.class) @SpringBootTest @ExtendWith(SpringExtension.class) @@ -66,40 +70,32 @@ public class TableEndpointIntegrationTest extends BaseUnitTest { @Autowired private TableEndpoint tableEndpoint; + @Container @Autowired - private H2Utils h2Utils; + private MariaDBContainer<?> mariaDBContainer; @BeforeEach - public void beforeEach() { - afterEach(); - /* create network */ - DockerConfig.createAllNetworks(); + public void beforeEach() throws SQLException { /* metadata database */ imageRepository.save(IMAGE_1); realmRepository.save(REALM_DBREPO); userRepository.save(USER_1_SIMPLE); containerRepository.save(CONTAINER_1_SIMPLE); databaseRepository.save(DATABASE_1_SIMPLE); - } - - @AfterEach - public void afterEach() { - DockerConfig.removeAllContainers(); - DockerConfig.removeAllNetworks(); + MariaDbConfig.dropAllDatabases(CONTAINER_1); + MariaDbConfig.createInitDatabase(CONTAINER_1, DATABASE_1); } @Test @WithMockUser(username = USER_1_USERNAME, authorities = {"create-table"}) public void create_hasRoleHasAccess_succeeds() throws UserNotFoundException, TableMalformedException, NotAllowedException, QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException, AmqpException, - TableNameExistsException, ContainerNotFoundException, InterruptedException { + TableNameExistsException, ContainerNotFoundException { /* mock */ - DockerConfig.createContainer(null, CONTAINER_1, CONTAINER_1_ENV); - DockerConfig.startContainer(CONTAINER_1); accessRepository.save(DATABASE_1_USER_1_WRITE_OWN_ACCESS); /* test */ - tableEndpoint.create(CONTAINER_1_ID, DATABASE_1_ID, TABLE_3_CREATE_DTO, USER_1_PRINCIPAL); + tableEndpoint.create(DATABASE_1_ID, TABLE_3_CREATE_DTO, USER_1_PRINCIPAL); } } diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointUnitTest.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointUnitTest.java index 49b1b0a66998bcf27080618c80bc4475b46f6922..f9bd3dd4001f0a767d2794bcfb87347fc424132b 100644 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointUnitTest.java +++ b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/endpoint/TableEndpointUnitTest.java @@ -83,7 +83,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void list_publicAnonymous_succeeds() throws NotAllowedException, DatabaseNotFoundException { /* test */ - generic_list(CONTAINER_3_ID, DATABASE_3_ID, DATABASE_3, null, null, null); + generic_list(DATABASE_3_ID, DATABASE_3, null, null, null); } @Test @@ -92,7 +92,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - generic_list(CONTAINER_3_ID, DATABASE_3_ID, null, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_READ_ACCESS); + generic_list(DATABASE_3_ID, null, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_READ_ACCESS); }); } @@ -101,7 +101,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void list_publicHasRole_succeeds() throws DatabaseNotFoundException, NotAllowedException { /* test */ - final ResponseEntity<List<TableBriefDto>> response = generic_list(CONTAINER_3_ID, DATABASE_3_ID, DATABASE_3, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + final ResponseEntity<List<TableBriefDto>> response = generic_list(DATABASE_3_ID, DATABASE_3, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); assertEquals(HttpStatus.OK, response.getStatusCode()); final List<TableBriefDto> body = response.getBody(); assertNotNull(body); @@ -113,7 +113,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void list_publicNoRole_succeeds() throws NotAllowedException, DatabaseNotFoundException { /* test */ - generic_list(CONTAINER_3_ID, DATABASE_3_ID, DATABASE_3, USER_4_USERNAME, USER_4_PRINCIPAL, null); + generic_list(DATABASE_3_ID, DATABASE_3, USER_4_USERNAME, USER_4_PRINCIPAL, null); } @Test @@ -122,7 +122,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(org.springframework.security.access.AccessDeniedException.class, () -> { - generic_create(CONTAINER_3_ID, DATABASE_3_ID, DATABASE_3, TABLE_4_CREATE_DTO, null, null, null); + generic_create(DATABASE_3_ID, DATABASE_3, TABLE_4_CREATE_DTO, null, null, null); }); } @@ -132,7 +132,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - generic_create(CONTAINER_3_ID, DATABASE_3_ID, null, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); + generic_create(DATABASE_3_ID, null, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); }); } @@ -142,7 +142,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_create(CONTAINER_3_ID, DATABASE_3_ID, DATABASE_3, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, null); + generic_create(DATABASE_3_ID, DATABASE_3, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, null); }); } @@ -152,7 +152,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(org.springframework.security.access.AccessDeniedException.class, () -> { - generic_create(CONTAINER_3_ID, DATABASE_3_ID, DATABASE_3, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); + generic_create(DATABASE_3_ID, DATABASE_3, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_WRITE_OWN_ACCESS); }); } @@ -162,7 +162,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_create(CONTAINER_3_ID, DATABASE_3_ID, DATABASE_3, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_READ_ACCESS); + generic_create(DATABASE_3_ID, DATABASE_3, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_3_USER_1_READ_ACCESS); }); } @@ -171,7 +171,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void findById_publicAnonymous_succeeds() throws UserNotFoundException, TableNotFoundException, NotAllowedException, TableMalformedException, QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* test */ - generic_findById(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, null, null, null); + generic_findById(DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, null, null, null); } @Test @@ -180,7 +180,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(TableNotFoundException.class, () -> { - generic_findById(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, DATABASE_3, null, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + generic_findById(DATABASE_3_ID, TABLE_8_ID, DATABASE_3, null, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); }); } @@ -191,7 +191,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* test */ - generic_findById(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, null, TABLE_8, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + generic_findById(DATABASE_3_ID, TABLE_8_ID, null, TABLE_8, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); } @Test @@ -199,7 +199,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void findById_publicHasRole_succeeds() throws DatabaseNotFoundException, NotAllowedException, UserNotFoundException, TableNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* test */ - final ResponseEntity<TableDto> response = generic_findById(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + final ResponseEntity<TableDto> response = generic_findById(DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); assertEquals(HttpStatus.OK, response.getStatusCode()); final TableDto body = response.getBody(); assertNotNull(body); @@ -210,7 +210,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void findById_publicNoRole_succeeds() throws UserNotFoundException, TableNotFoundException, NotAllowedException, TableMalformedException, QueryMalformedException, DatabaseNotFoundException, ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* test */ - generic_findById(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, USER_1_USERNAME, USER_1_PRINCIPAL, null); + generic_findById(DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, USER_1_USERNAME, USER_1_PRINCIPAL, null); } @Test @@ -219,7 +219,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(AccessDeniedException.class, () -> { - generic_delete(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_3, null); + generic_delete(DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_3, null); }); } @@ -229,7 +229,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(TableNotFoundException.class, () -> { - generic_delete(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, DATABASE_3, null, USER_1_PRINCIPAL); + generic_delete(DATABASE_3_ID, TABLE_8_ID, DATABASE_3, null, USER_1_PRINCIPAL); }); } @@ -239,7 +239,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - generic_delete(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, null, TABLE_8, USER_1_PRINCIPAL); + generic_delete(DATABASE_3_ID, TABLE_8_ID, null, TABLE_8, USER_1_PRINCIPAL); }); } @@ -248,7 +248,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void delete_publicHasRole_succeeds() throws DatabaseNotFoundException, NotAllowedException, TableNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, ContainerNotFoundException, DataProcessingException { /* test */ - final ResponseEntity<?> response = generic_delete(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, USER_1_PRINCIPAL); + final ResponseEntity<?> response = generic_delete(DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, USER_1_PRINCIPAL); assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); } @@ -258,7 +258,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(AccessDeniedException.class, () -> { - generic_delete(CONTAINER_3_ID, DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, USER_4_PRINCIPAL); + generic_delete(DATABASE_3_ID, TABLE_8_ID, DATABASE_3, TABLE_8, USER_4_PRINCIPAL); }); } @@ -272,7 +272,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_list(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, null, null, null); + generic_list(DATABASE_1_ID, DATABASE_1, null, null, null); }); } @@ -282,7 +282,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - generic_list(CONTAINER_1_ID, DATABASE_1_ID, null, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + generic_list(DATABASE_1_ID, null, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); }); } @@ -291,7 +291,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void list_privateHasRole_succeeds() throws DatabaseNotFoundException, NotAllowedException { /* test */ - final ResponseEntity<List<TableBriefDto>> response = generic_list(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + final ResponseEntity<List<TableBriefDto>> response = generic_list(DATABASE_1_ID, DATABASE_1, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); assertEquals(HttpStatus.OK, response.getStatusCode()); final List<TableBriefDto> body = response.getBody(); assertNotNull(body); @@ -304,7 +304,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_list(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, USER_4_USERNAME, USER_4_PRINCIPAL, null); + generic_list(DATABASE_1_ID, DATABASE_1, USER_4_USERNAME, USER_4_PRINCIPAL, null); }); } @@ -314,7 +314,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(org.springframework.security.access.AccessDeniedException.class, () -> { - generic_create(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, TABLE_4_CREATE_DTO, null, null, null); + generic_create(DATABASE_1_ID, DATABASE_1, TABLE_4_CREATE_DTO, null, null, null); }); } @@ -324,7 +324,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - generic_create(CONTAINER_1_ID, DATABASE_1_ID, null, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); + generic_create(DATABASE_1_ID, null, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); }); } @@ -334,7 +334,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_create(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, null); + generic_create(DATABASE_1_ID, DATABASE_1, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, null); }); } @@ -344,7 +344,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(org.springframework.security.access.AccessDeniedException.class, () -> { - generic_create(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); + generic_create(DATABASE_1_ID, DATABASE_1, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_WRITE_OWN_ACCESS); }); } @@ -354,7 +354,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(NotAllowedException.class, () -> { - generic_create(CONTAINER_1_ID, DATABASE_1_ID, DATABASE_1, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + generic_create(DATABASE_1_ID, DATABASE_1, TABLE_4_CREATE_DTO, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); }); } @@ -365,7 +365,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* test */ - generic_findById(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, null, null, null); + generic_findById(DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, null, null, null); } @Test @@ -374,7 +374,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(TableNotFoundException.class, () -> { - generic_findById(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, DATABASE_1, null, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + generic_findById(DATABASE_1_ID, TABLE_1_ID, DATABASE_1, null, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); }); } @@ -385,7 +385,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* test */ - generic_findById(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, null, TABLE_1, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + generic_findById(DATABASE_1_ID, TABLE_1_ID, null, TABLE_1, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); } @Test @@ -393,7 +393,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void findById_privateHasRole_succeeds() throws DatabaseNotFoundException, NotAllowedException, UserNotFoundException, TableNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* test */ - final ResponseEntity<TableDto> response = generic_findById(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); + final ResponseEntity<TableDto> response = generic_findById(DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, USER_1_USERNAME, USER_1_PRINCIPAL, DATABASE_1_USER_1_READ_ACCESS); assertEquals(HttpStatus.OK, response.getStatusCode()); final TableDto body = response.getBody(); assertNotNull(body); @@ -406,7 +406,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* test */ - generic_findById(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, USER_4_USERNAME, USER_4_PRINCIPAL, null); + generic_findById(DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, USER_4_USERNAME, USER_4_PRINCIPAL, null); } @Test @@ -415,7 +415,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(AccessDeniedException.class, () -> { - generic_delete(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, null); + generic_delete(DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, null); }); } @@ -425,7 +425,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(TableNotFoundException.class, () -> { - generic_delete(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, DATABASE_1, null, USER_1_PRINCIPAL); + generic_delete(DATABASE_1_ID, TABLE_1_ID, DATABASE_1, null, USER_1_PRINCIPAL); }); } @@ -435,7 +435,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - generic_delete(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, null, TABLE_1, USER_1_PRINCIPAL); + generic_delete(DATABASE_1_ID, TABLE_1_ID, null, TABLE_1, USER_1_PRINCIPAL); }); } @@ -444,7 +444,7 @@ public class TableEndpointUnitTest extends BaseUnitTest { public void delete_privateHasRole_succeeds() throws DatabaseNotFoundException, NotAllowedException, TableNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, ContainerNotFoundException, DataProcessingException { /* test */ - final ResponseEntity<?> response = generic_delete(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, USER_1_PRINCIPAL); + final ResponseEntity<?> response = generic_delete(DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, USER_1_PRINCIPAL); assertEquals(HttpStatus.ACCEPTED, response.getStatusCode()); } @@ -455,27 +455,27 @@ public class TableEndpointUnitTest extends BaseUnitTest { ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* test */ - generic_findById(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, USER_4_USERNAME, USER_4_PRINCIPAL, null); + generic_findById(DATABASE_1_ID, TABLE_1_ID, DATABASE_1, TABLE_1, USER_4_USERNAME, USER_4_PRINCIPAL, null); } /* ################################################################################################### */ /* ## GENERIC TEST CASES ## */ /* ################################################################################################### */ - protected ResponseEntity<List<TableBriefDto>> generic_list(Long containerId, Long databaseId, Database database, String username, Principal principal, DatabaseAccess access) throws DatabaseNotFoundException, NotAllowedException { + protected ResponseEntity<List<TableBriefDto>> generic_list(Long databaseId, Database database, String username, Principal principal, DatabaseAccess access) throws DatabaseNotFoundException, NotAllowedException { /* mock */ if (database != null) { - when(databaseService.find(containerId, databaseId)) + when(databaseService.find(databaseId)) .thenReturn(database); - when(tableService.findAll(containerId, databaseId)) + when(tableService.findAll(databaseId)) .thenReturn(database.getTables()); log.trace("mock {} table(s)", database.getTables().size()); } else { doThrow(DatabaseNotFoundException.class) .when(databaseService) - .find(containerId, databaseId); - when(tableService.findAll(containerId, databaseId)) + .find(databaseId); + when(tableService.findAll(databaseId)) .thenReturn(List.of()); log.trace("mock 0 tables"); } @@ -489,23 +489,23 @@ public class TableEndpointUnitTest extends BaseUnitTest { } /* test */ - return tableEndpoint.list(containerId, databaseId, principal); + return tableEndpoint.list(databaseId, principal); } - protected ResponseEntity<TableBriefDto> generic_create(Long containerId, Long databaseId, Database database, TableCreateDto data, String username, Principal principal, DatabaseAccess access) throws DatabaseNotFoundException, NotAllowedException, UserNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { + protected ResponseEntity<TableBriefDto> generic_create(Long databaseId, Database database, TableCreateDto data, String username, Principal principal, DatabaseAccess access) throws DatabaseNotFoundException, NotAllowedException, UserNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException { /* mock */ if (database != null) { - when(databaseService.find(containerId, databaseId)) + when(databaseService.find(databaseId)) .thenReturn(database); log.trace("mock {} tables", database.getTables().size()); - when(tableService.findAll(containerId, databaseId)) + when(tableService.findAll(databaseId)) .thenReturn(database.getTables()); } else { doThrow(DatabaseNotFoundException.class) .when(databaseService) - .find(containerId, databaseId); - when(tableService.findAll(containerId, databaseId)) + .find(databaseId); + when(tableService.findAll(databaseId)) .thenReturn(List.of()); } if (access != null) { @@ -518,31 +518,31 @@ public class TableEndpointUnitTest extends BaseUnitTest { } /* test */ - return tableEndpoint.create(containerId, databaseId, data, principal); + return tableEndpoint.create(databaseId, data, principal); } - protected ResponseEntity<TableDto> generic_findById(Long containerId, Long databaseId, Long tableId, Database database, Table table, String username, Principal principal, DatabaseAccess access) throws DatabaseNotFoundException, NotAllowedException, UserNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException, TableNotFoundException { + protected ResponseEntity<TableDto> generic_findById(Long databaseId, Long tableId, Database database, Table table, String username, Principal principal, DatabaseAccess access) throws DatabaseNotFoundException, NotAllowedException, UserNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, AmqpException, TableNameExistsException, ContainerNotFoundException, TableNotFoundException { /* mock */ if (table != null) { - when(tableService.findById(containerId, databaseId, tableId)) + when(tableService.findById(databaseId, tableId)) .thenReturn(table); - when(databaseService.find(containerId, databaseId)) + when(databaseService.find(databaseId)) .thenReturn(database); } else { doThrow(TableNotFoundException.class) .when(tableService) - .findById(containerId, databaseId, tableId); - when(tableService.findAll(containerId, databaseId)) + .findById(databaseId, tableId); + when(tableService.findAll(databaseId)) .thenReturn(List.of()); } if (database != null) { - when(databaseService.find(containerId, databaseId)) + when(databaseService.find(databaseId)) .thenReturn(database); } else { doThrow(DatabaseNotFoundException.class) .when(databaseService) - .find(containerId, databaseId); + .find(databaseId); } if (access != null) { when(accessService.find(databaseId, username)) @@ -554,28 +554,28 @@ public class TableEndpointUnitTest extends BaseUnitTest { } /* test */ - return tableEndpoint.findById(containerId, databaseId, tableId, principal); + return tableEndpoint.findById(databaseId, tableId, principal); } - protected ResponseEntity<?> generic_delete(Long containerId, Long databaseId, Long tableId, Database database, Table table, Principal principal) throws DatabaseNotFoundException, NotAllowedException, ContainerNotFoundException, TableNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, DataProcessingException { + protected ResponseEntity<?> generic_delete(Long databaseId, Long tableId, Database database, Table table, Principal principal) throws DatabaseNotFoundException, NotAllowedException, ContainerNotFoundException, TableNotFoundException, TableMalformedException, QueryMalformedException, ImageNotSupportedException, DataProcessingException { /* mock */ if (table != null) { doNothing() .when(tableService) - .deleteTable(containerId, databaseId, tableId); + .deleteTable(databaseId, tableId); } else { doThrow(TableNotFoundException.class) .when(tableService) - .deleteTable(containerId, databaseId, tableId); + .deleteTable(databaseId, tableId); } if (database == null) { doThrow(DatabaseNotFoundException.class) .when(tableService) - .deleteTable(containerId, databaseId, tableId); + .deleteTable(databaseId, tableId); } /* test */ - return tableEndpoint.delete(containerId, databaseId, tableId, principal); + return tableEndpoint.delete(databaseId, tableId, principal); } } diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/gateway/QueryServiceGatewayUnitTest.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/gateway/QueryServiceGatewayUnitTest.java index 3d27d0c923279e4cf3075b29998e44dfe07775cf..11e958ded82da9285c2259aab31af497883dc8b5 100644 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/gateway/QueryServiceGatewayUnitTest.java +++ b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/gateway/QueryServiceGatewayUnitTest.java @@ -11,6 +11,7 @@ import lombok.extern.log4j.Log4j2; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; @@ -24,7 +25,6 @@ import org.springframework.web.client.RestTemplate; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.*; -import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; @Log4j2 @@ -49,6 +49,7 @@ public class QueryServiceGatewayUnitTest extends BaseUnitTest { private TableColumnIdxRepository tableColumnidxRepository; @MockBean + @Qualifier("restTemplate") private RestTemplate restTemplate; @Autowired @@ -64,7 +65,7 @@ public class QueryServiceGatewayUnitTest extends BaseUnitTest { .thenReturn(mock); /* test */ - queryServiceGateway.declareConsumer(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, "abc"); + queryServiceGateway.declareConsumer(DATABASE_1_ID, TABLE_1_ID, "abc"); } @Test @@ -78,7 +79,7 @@ public class QueryServiceGatewayUnitTest extends BaseUnitTest { /* test */ assertThrows(AmqpException.class, () -> { - queryServiceGateway.declareConsumer(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID, "abc"); + queryServiceGateway.declareConsumer(DATABASE_1_ID, TABLE_1_ID, "abc"); }); } diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/MessageQueueServiceIntegrationTest.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/MessageQueueServiceIntegrationTest.java index 29b42b6f6f8eb0a1238e42845b7b7bfe9bd3ad9d..76bedc997ee04990cac8fa120162643ff1d615ce 100644 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/MessageQueueServiceIntegrationTest.java +++ b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/MessageQueueServiceIntegrationTest.java @@ -4,30 +4,33 @@ package at.tuwien.service; import at.tuwien.BaseUnitTest; import at.tuwien.config.IndexConfig; import at.tuwien.config.ReadyConfig; -import at.tuwien.config.DockerConfig; -import at.tuwien.exception.*; +import at.tuwien.exception.AmqpException; import at.tuwien.repository.sdb.TableColumnIdxRepository; import at.tuwien.repository.sdb.TableIdxRepository; import at.tuwien.utils.AmqpUtils; import com.rabbitmq.client.BuiltinExchangeType; import com.rabbitmq.client.Channel; import lombok.extern.log4j.Log4j2; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; 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.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.context.junit.jupiter.SpringExtension; +import org.testcontainers.containers.RabbitMQContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; import java.io.IOException; import java.util.List; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.when; @Log4j2 +@Testcontainers @SpringBootTest @ExtendWith(SpringExtension.class) public class MessageQueueServiceIntegrationTest extends BaseUnitTest { @@ -56,21 +59,17 @@ public class MessageQueueServiceIntegrationTest extends BaseUnitTest { @Autowired private MessageQueueService messageQueueService; - @BeforeAll - public static void beforeAll() throws InterruptedException { - afterAll(); - /* create network */ - DockerConfig.createAllNetworks(); - /* create container */ - DockerConfig.createContainer(null, CONTAINER_BROKER, 15672, CONTAINER_BROKER_ENV); - DockerConfig.startContainer(CONTAINER_BROKER); - } - - @AfterAll - public static void afterAll() { - /* stop containers and remove them */ - DockerConfig.removeAllContainers(); - DockerConfig.removeAllNetworks(); + @Container + private static final RabbitMQContainer rabbitMQContainer = new RabbitMQContainer("rabbitmq:3-management-alpine") + .withVhost("/"); + + @DynamicPropertySource + static void rabbitMQProperties(DynamicPropertyRegistry registry) { + registry.add("fda.gateway.endpoint", () -> "http://" + rabbitMQContainer.getHost() + ":" + rabbitMQContainer.getHttpPort()); + registry.add("spring.rabbitmq.host", rabbitMQContainer::getHost); + registry.add("spring.rabbitmq.port", rabbitMQContainer::getAmqpPort); + registry.add("spring.rabbitmq.username", rabbitMQContainer::getAdminUsername); + registry.add("spring.rabbitmq.password", rabbitMQContainer::getAdminPassword); } @Test diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceIntegrationReadTest.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceIntegrationReadTest.java index e65f776d75a274c1e9ca50ef5e1193a1b39b9bda..4e14fb55ba162f880b18aae1d86631fd0996bdc6 100644 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceIntegrationReadTest.java +++ b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceIntegrationReadTest.java @@ -1,18 +1,21 @@ package at.tuwien.service; import at.tuwien.BaseUnitTest; -import at.tuwien.config.DockerConfig; -import at.tuwien.config.H2Utils; import at.tuwien.config.IndexConfig; +import at.tuwien.config.MariaDbConfig; import at.tuwien.config.ReadyConfig; import at.tuwien.entities.database.table.Table; -import at.tuwien.exception.*; +import at.tuwien.exception.ContainerNotFoundException; +import at.tuwien.exception.DatabaseNotFoundException; +import at.tuwien.exception.TableNotFoundException; +import at.tuwien.repository.mdb.*; import at.tuwien.repository.sdb.TableColumnIdxRepository; import at.tuwien.repository.sdb.TableIdxRepository; -import at.tuwien.repository.mdb.*; import com.rabbitmq.client.Channel; import lombok.extern.log4j.Log4j2; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -21,14 +24,18 @@ 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 org.testcontainers.containers.MariaDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; -import java.io.File; +import java.sql.SQLException; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; @Log4j2 +@Testcontainers @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) @EnableAutoConfiguration(exclude = RabbitAutoConfiguration.class) @SpringBootTest @@ -71,27 +78,16 @@ public class TableServiceIntegrationReadTest extends BaseUnitTest { @Autowired private TableService tableService; + @Container @Autowired - private H2Utils h2Utils; - - private static final String BIND_WEATHER = new File("../../dbrepo-metadata-db/test/src/test/resources/weather").toPath().toAbsolutePath() + ":/docker-entrypoint-initdb.d"; + private MariaDBContainer<?> mariaDBContainer; @BeforeAll public static void beforeAll() throws InterruptedException { - afterAll(); - DockerConfig.createAllNetworks(); - DockerConfig.createContainer(BIND_WEATHER, CONTAINER_1, CONTAINER_1_ENV); - DockerConfig.startContainer(CONTAINER_1); - } - - @AfterAll - public static void afterAll() { - DockerConfig.removeAllContainers(); - DockerConfig.removeAllNetworks(); } @BeforeEach - public void beforeEach() { + public void beforeEach() throws SQLException { imageRepository.save(IMAGE_1); realmRepository.save(REALM_DBREPO); userRepository.save(USER_1_SIMPLE); @@ -101,13 +97,15 @@ public class TableServiceIntegrationReadTest extends BaseUnitTest { databaseRepository.save(DATABASE_1_SIMPLE); tableRepository.save(TABLE_1); tableRepository.save(TABLE_2); + MariaDbConfig.dropAllDatabases(CONTAINER_1); + MariaDbConfig.createInitDatabase(CONTAINER_1, DATABASE_1); } @Test public void findAll_succeeds() throws DatabaseNotFoundException { /* test */ - final List<Table> response = tableService.findAll(CONTAINER_1_ID, DATABASE_1_ID); + final List<Table> response = tableService.findAll(DATABASE_1_ID); assertEquals(2, response.size()); } @@ -116,7 +114,7 @@ public class TableServiceIntegrationReadTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - tableService.findAll(CONTAINER_2_ID, DATABASE_2_ID); + tableService.findAll(DATABASE_2_ID); }); } @@ -125,7 +123,7 @@ public class TableServiceIntegrationReadTest extends BaseUnitTest { ContainerNotFoundException { /* test */ - final Table response = tableService.findById(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID); + final Table response = tableService.findById(DATABASE_1_ID, TABLE_1_ID); assertEquals(TABLE_1_ID, response.getId()); assertEquals(TABLE_1_NAME, response.getName()); assertEquals(TABLE_1_INTERNALNAME, response.getInternalName()); @@ -136,7 +134,7 @@ public class TableServiceIntegrationReadTest extends BaseUnitTest { /* test */ assertThrows(TableNotFoundException.class, () -> { - tableService.findById(CONTAINER_1_ID, DATABASE_1_ID, 99999L); + tableService.findById(DATABASE_1_ID, 99999L); }); } @@ -145,16 +143,7 @@ public class TableServiceIntegrationReadTest extends BaseUnitTest { /* test */ assertThrows(DatabaseNotFoundException.class, () -> { - tableService.findById(CONTAINER_2_ID, 99999L, TABLE_3_ID); - }); - } - - @Test - public void findById_containerNotFound_fails() { - - /* test */ - assertThrows(ContainerNotFoundException.class, () -> { - tableService.findById(99999L, DATABASE_3_ID, TABLE_3_ID); + tableService.findById(99999L, TABLE_3_ID); }); } diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceIntegrationWriteTest.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceIntegrationWriteTest.java index 254d3c2d60d2aa7b7b35fc70813e730d9f95de08..4cc9adf2722ce22dfa7ee2eef0455dda75ecd70e 100644 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceIntegrationWriteTest.java +++ b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/service/TableServiceIntegrationWriteTest.java @@ -3,9 +3,8 @@ package at.tuwien.service; import at.tuwien.BaseUnitTest; import at.tuwien.api.database.table.TableDto; -import at.tuwien.config.DockerConfig; -import at.tuwien.config.H2Utils; import at.tuwien.config.IndexConfig; +import at.tuwien.config.MariaDbConfig; import at.tuwien.config.ReadyConfig; import at.tuwien.exception.*; import at.tuwien.repository.sdb.TableColumnIdxRepository; @@ -14,7 +13,8 @@ import at.tuwien.repository.mdb.*; 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.BeforeEach; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -23,9 +23,12 @@ 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 org.testcontainers.containers.MariaDBContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; -import java.io.File; import java.security.Principal; +import java.sql.SQLException; import java.util.List; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -36,6 +39,7 @@ import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.when; @Log4j2 +@Testcontainers @DirtiesContext(classMode = DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD) @EnableAutoConfiguration(exclude = RabbitAutoConfiguration.class) @SpringBootTest @@ -78,19 +82,12 @@ public class TableServiceIntegrationWriteTest extends BaseUnitTest { @Autowired private RealmRepository realmRepository; + @Container @Autowired - private H2Utils h2Utils; - - private static final String BIND_WEATHER = new File("../../dbrepo-metadata-db/test/src/test/resources/weather").toPath().toAbsolutePath() + ":/docker-entrypoint-initdb.d"; + private MariaDBContainer<?> mariaDBContainer; @BeforeEach - public void beforeEach() throws InterruptedException { - afterEach(); - /* create networks */ - DockerConfig.createAllNetworks(); - /* create user container */ - DockerConfig.createContainer(BIND_WEATHER, CONTAINER_1, CONTAINER_1_ENV); - DockerConfig.startContainer(CONTAINER_1); + public void beforeEach() throws SQLException { /* metadata database */ imageRepository.save(IMAGE_1); realmRepository.save(REALM_DBREPO); @@ -101,12 +98,8 @@ public class TableServiceIntegrationWriteTest extends BaseUnitTest { databaseRepository.save(DATABASE_1_SIMPLE); tableRepository.save(TABLE_1); tableRepository.save(TABLE_2); - } - - @AfterEach - public void afterEach() { - DockerConfig.removeAllContainers(); - DockerConfig.removeAllNetworks(); + MariaDbConfig.dropAllDatabases(CONTAINER_1); + MariaDbConfig.createInitDatabase(CONTAINER_1, DATABASE_1); } @Test @@ -121,7 +114,7 @@ public class TableServiceIntegrationWriteTest extends BaseUnitTest { .thenReturn(List.of()); /* test */ - tableService.createTable(CONTAINER_1_ID, DATABASE_1_ID, TABLE_3_CREATE_DTO, USER_1_PRINCIPAL); + tableService.createTable(DATABASE_1_ID, TABLE_3_CREATE_DTO, USER_1_PRINCIPAL); } @Test @@ -138,11 +131,11 @@ public class TableServiceIntegrationWriteTest extends BaseUnitTest { /* test */ try { - tableService.createTable(CONTAINER_1_ID, DATABASE_1_ID, TABLE_3_INVALID_CREATE_DTO, principal); + tableService.createTable(DATABASE_1_ID, TABLE_3_INVALID_CREATE_DTO, principal); } catch (TableMalformedException e) { /* ignore */ } - tableService.createTable(CONTAINER_1_ID, DATABASE_1_ID, TABLE_3_CREATE_DTO, principal); + tableService.createTable(DATABASE_1_ID, TABLE_3_CREATE_DTO, principal); } @Test @@ -157,8 +150,8 @@ public class TableServiceIntegrationWriteTest extends BaseUnitTest { .thenReturn(List.of()); /* test */ - tableService.createTable(CONTAINER_1_ID, DATABASE_1_ID, TABLE_4_CREATE_DTO, USER_1_PRINCIPAL); // table to reference - tableService.createTable(CONTAINER_1_ID, DATABASE_1_ID, TABLE_5_CREATE_DTO, USER_1_PRINCIPAL); + tableService.createTable(DATABASE_1_ID, TABLE_4_CREATE_DTO, USER_1_PRINCIPAL); // table to reference + tableService.createTable(DATABASE_1_ID, TABLE_5_CREATE_DTO, USER_1_PRINCIPAL); } @Test @@ -166,7 +159,7 @@ public class TableServiceIntegrationWriteTest extends BaseUnitTest { /* test */ assertThrows(TableMalformedException.class, () -> { - tableService.createTable(CONTAINER_1_ID, DATABASE_1_ID, TABLE_5_CREATE_DTO, USER_1_PRINCIPAL); + tableService.createTable(DATABASE_1_ID, TABLE_5_CREATE_DTO, USER_1_PRINCIPAL); }); } @@ -180,7 +173,7 @@ public class TableServiceIntegrationWriteTest extends BaseUnitTest { .delete(any(TableDto.class)); /* test */ - tableService.deleteTable(CONTAINER_1_ID, DATABASE_1_ID, TABLE_1_ID); + tableService.deleteTable(DATABASE_1_ID, TABLE_1_ID); assertTrue(userRepository.findById(TABLE_1_CREATED_BY).isPresent()); assertTrue(databaseRepository.findById(TABLE_1_DATABASE_ID).isPresent()); } @@ -195,7 +188,7 @@ public class TableServiceIntegrationWriteTest extends BaseUnitTest { /* test */ assertThrows(TableNotFoundException.class, () -> { - tableService.deleteTable(CONTAINER_1_ID, DATABASE_1_ID, 9999L); + tableService.deleteTable(DATABASE_1_ID, 9999L); }); } diff --git a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/utils/AmqpUtils.java b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/utils/AmqpUtils.java index 63a8ca04afa38c5c3976be442a56c53514239f1a..b11964128cf3121617305fdd1478f6b1b38a2acc 100644 --- a/dbrepo-table-service/rest-service/src/test/java/at/tuwien/utils/AmqpUtils.java +++ b/dbrepo-table-service/rest-service/src/test/java/at/tuwien/utils/AmqpUtils.java @@ -2,10 +2,10 @@ package at.tuwien.utils; import at.tuwien.api.amqp.ExchangeDto; import at.tuwien.api.amqp.QueueDto; -import at.tuwien.config.AmqpConfig; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -13,7 +13,6 @@ import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; -import java.net.URI; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -23,12 +22,10 @@ import java.util.stream.Collectors; public class AmqpUtils { private final RestTemplate restTemplate; - private final AmqpConfig amqpConfig; @Autowired - public AmqpUtils(@Qualifier("brokerRestTemplate") RestTemplate restTemplate, AmqpConfig amqpConfig) { + public AmqpUtils(@Qualifier("brokerRestTemplate") RestTemplate restTemplate) { this.restTemplate = restTemplate; - this.amqpConfig = amqpConfig; } public boolean exchangeExists(String exchange) { @@ -49,9 +46,11 @@ public class AmqpUtils { return true; } + @Value("${fda.gateway.endpoint}") + private String gatewayEndpoint; + public boolean queueExists(String queue) { - final URI uri = URI.create("http://" + amqpConfig.getAmpqHost() + ":15672/api/queues/%2F/"); - final ResponseEntity<QueueDto[]> response = restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(null), QueueDto[].class); + final ResponseEntity<QueueDto[]> response = restTemplate.exchange("/api/queues/{1}/", HttpMethod.GET, new HttpEntity<>(null), QueueDto[].class, "/"); if (!response.getStatusCode().equals(HttpStatus.OK)) { log.error("Failed to find queue, code is {}", response.getStatusCode()); throw new RuntimeException("Failed to find queue"); diff --git a/dbrepo-table-service/rest-service/src/test/resources/application.properties b/dbrepo-table-service/rest-service/src/test/resources/application.properties index 65007f07659129fba0dbf7f2f6dc6f2c29d2ca7d..302797747d9c1a746cdc6a930fd21b847535210a 100644 --- a/dbrepo-table-service/rest-service/src/test/resources/application.properties +++ b/dbrepo-table-service/rest-service/src/test/resources/application.properties @@ -9,14 +9,14 @@ spring.cloud.config.discovery.enabled=false spring.cloud.config.enabled=false # internal datasource -# spring 6 fix https://github.com/h2database/h2database/issues/3363 -spring.datasource.url=jdbc:h2:mem:testdb;NON_KEYWORDS=VALUE,KEY;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS FDA -spring.datasource.driverClassName=org.h2.Driver -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.datasource.driver-class-name=org.hsqldb.jdbc.JDBCDriver +spring.datasource.url=jdbc:hsqldb:mem:testdb;db_close_delay=-1;sql.syntax_mys=true +spring.jpa.database-platform=org.hibernate.dialect.HSQLDialect +spring.datasource.username=root +spring.datasource.password=dbrepo +spring.sql.init.mode=always +spring.sql.init.schema-locations=classpath*:init/schema.sql +spring.jpa.hibernate.ddl-auto=create # log logging.level.org.hibernate.SQL=trace diff --git a/dbrepo-table-service/rest-service/src/test/resources/init/querystore.sql b/dbrepo-table-service/rest-service/src/test/resources/init/querystore.sql new file mode 100644 index 0000000000000000000000000000000000000000..1bf8b26c5580b343a7e75864931f2f8584932843 --- /dev/null +++ b/dbrepo-table-service/rest-service/src/test/resources/init/querystore.sql @@ -0,0 +1,5 @@ +CREATE SEQUENCE `qs_queries_seq`; +CREATE TABLE `qs_queries` ( `id` bigint not null primary key default nextval(`qs_queries_seq`), `created` datetime not null default now(), `executed` datetime not null default now(), `created_by` varchar(255) not null, `query` text not null, `query_normalized` text not null, `is_persisted` boolean not null, `query_hash` varchar(255) not null, `result_hash` varchar(255), `result_number` bigint ); +CREATE PROCEDURE hash_table(IN name VARCHAR(255), OUT hash VARCHAR(255), OUT count BIGINT) BEGIN DECLARE _sql TEXT; SELECT CONCAT('SELECT SHA2(GROUP_CONCAT(CONCAT_WS(\'\',', GROUP_CONCAT(CONCAT('`', column_name, '`') ORDER BY column_name), ') SEPARATOR \',\'), 256) AS hash, COUNT(*) AS count FROM `', name, '` INTO @hash, @count;') FROM `information_schema`.`columns` WHERE `table_schema` = DATABASE() AND `table_name` = name INTO _sql; PREPARE stmt FROM _sql; EXECUTE stmt; DEALLOCATE PREPARE stmt; SET hash = @hash; SET count = @count; END; +CREATE PROCEDURE store_query(IN query TEXT, IN executed DATETIME, OUT queryId BIGINT) BEGIN DECLARE _queryhash varchar(255) DEFAULT SHA2(query, 256); DECLARE _username varchar(255) DEFAULT REGEXP_REPLACE(current_user(), '@.*', ''); DECLARE _query TEXT DEFAULT CONCAT('CREATE OR REPLACE TABLE _tmp AS (', query, ')'); PREPARE stmt FROM _query; EXECUTE stmt; DEALLOCATE PREPARE stmt; CALL hash_table('_tmp', @hash, @count); IF @hash IS NULL THEN INSERT INTO `qs_queries` (`created_by`, `query`, `query_normalized`, `is_persisted`, `query_hash`, `result_hash`, `result_number`, `executed`) SELECT _username, query, query, false, _queryhash, @hash, @count, executed WHERE NOT EXISTS (SELECT `id` FROM `qs_queries` WHERE `query_hash` = _queryhash AND `result_hash` IS NULL); SET queryId = (SELECT `id` FROM `qs_queries` WHERE `query_hash` = _queryhash AND `result_hash` IS NULL); ELSE INSERT INTO `qs_queries` (`created_by`, `query`, `query_normalized`, `is_persisted`, `query_hash`, `result_hash`, `result_number`, `executed`) SELECT _username, query, query, false, _queryhash, @hash, @count, executed WHERE NOT EXISTS (SELECT `id` FROM `qs_queries` WHERE `query_hash` = _queryhash AND `result_hash` = @hash); SET queryId = (SELECT `id` FROM `qs_queries` WHERE `query_hash` = _queryhash AND `result_hash` = @hash); END IF; END; +CREATE DEFINER = 'root' PROCEDURE _store_query(IN _username VARCHAR(255), IN query TEXT, IN executed DATETIME, OUT queryId BIGINT) BEGIN DECLARE _queryhash varchar(255) DEFAULT SHA2(query, 256); DECLARE _query TEXT DEFAULT CONCAT('CREATE OR REPLACE TABLE _tmp AS (', query, ')'); PREPARE stmt FROM _query; EXECUTE stmt; DEALLOCATE PREPARE stmt; CALL hash_table('_tmp', @hash, @count); IF @hash IS NULL THEN INSERT INTO `qs_queries` (`created_by`, `query`, `query_normalized`, `is_persisted`, `query_hash`, `result_hash`, `result_number`, `executed`) SELECT _username, query, query, false, _queryhash, @hash, @count, executed WHERE NOT EXISTS (SELECT `id` FROM `qs_queries` WHERE `query_hash` = _queryhash AND `result_hash` IS NULL); SET queryId = (SELECT `id` FROM `qs_queries` WHERE `query_hash` = _queryhash AND `result_hash` IS NULL); ELSE INSERT INTO `qs_queries` (`created_by`, `query`, `query_normalized`, `is_persisted`, `query_hash`, `result_hash`, `result_number`, `executed`) SELECT _username, query, query, false, _queryhash, @hash, @count, executed WHERE NOT EXISTS (SELECT `id` FROM `qs_queries` WHERE `query_hash` = _queryhash AND `result_hash` = @hash); SET queryId = (SELECT `id` FROM `qs_queries` WHERE `query_hash` = _queryhash AND `result_hash` = @hash); END IF; END; \ No newline at end of file diff --git a/dbrepo-table-service/rest-service/src/test/resources/species/Species.sql b/dbrepo-table-service/rest-service/src/test/resources/species/Species.sql deleted file mode 100644 index 957368fcc773ecf22f419585b50206ad56359bc9..0000000000000000000000000000000000000000 --- a/dbrepo-table-service/rest-service/src/test/resources/species/Species.sql +++ /dev/null @@ -1,7 +0,0 @@ -/* https://sandbox.zenodo.org/api/files/6aca3421-add3-489b-8c4a-35228fe5c683/species_id.csv */ -CREATE TABLE maldi_ms_data -( - qu VARCHAR(255) NOT NULL, - species VARCHAR(255) NOT NULL, - score VARCHAR(255) NOT NULL -) WITH SYSTEM VERSIONING; \ No newline at end of file diff --git a/dbrepo-table-service/rest-service/src/test/resources/traffic/TrafficCh.sql b/dbrepo-table-service/rest-service/src/test/resources/traffic/TrafficCh.sql deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/dbrepo-table-service/rest-service/src/test/resources/weather/WeatherAus.sql b/dbrepo-table-service/rest-service/src/test/resources/weather/WeatherAus.sql deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/config/AmqpConfig.java b/dbrepo-table-service/services/src/main/java/at/tuwien/config/AmqpConfig.java index e44b5c5ddce5ea79728fa7261b63ba8ae5661bd3..5f959e6bf1284bc48a8850473d5ce107fdf1236c 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/config/AmqpConfig.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/config/AmqpConfig.java @@ -20,6 +20,9 @@ public class AmqpConfig { @Value("${spring.rabbitmq.host}") private String ampqHost; + @Value("${spring.rabbitmq.port:5672}") + private int ampqPort; + @Value("${spring.rabbitmq.virtual-host}") private String virtualHost; @@ -33,6 +36,7 @@ public class AmqpConfig { public Channel getChannel() throws IOException, TimeoutException { final ConnectionFactory factory = new ConnectionFactory(); factory.setHost(ampqHost); + factory.setPort(ampqPort); factory.setVirtualHost(virtualHost); factory.setUsername(amqpUsername); factory.setPassword(amqpPassword); diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/config/WebSecurityConfig.java b/dbrepo-table-service/services/src/main/java/at/tuwien/config/WebSecurityConfig.java index 5c4cfc9bf3afca6e79e06cd68f7808a61777d775..a3feadeae5963188ca476842be099bd121554ecd 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/config/WebSecurityConfig.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/config/WebSecurityConfig.java @@ -3,6 +3,7 @@ package at.tuwien.config; import at.tuwien.auth.AuthTokenFilter; import io.swagger.v3.oas.annotations.enums.SecuritySchemeType; import io.swagger.v3.oas.annotations.security.SecurityScheme; +import jakarta.servlet.http.HttpServletResponse; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; @@ -17,8 +18,6 @@ import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; -import jakarta.servlet.http.HttpServletResponse; - @Configuration @EnableWebSecurity @EnableGlobalMethodSecurity(prePostEnabled = true) @@ -45,7 +44,7 @@ public class WebSecurityConfig { new AntPathRequestMatcher("/swagger-ui.html") ); final OrRequestMatcher publicEndpoints = new OrRequestMatcher( - new AntPathRequestMatcher("/api/container/**/database/**/table/**", "GET") + new AntPathRequestMatcher("/api/database/**/table/**", "GET") ); /* enable CORS and disable CSRF */ http = http.cors().and().csrf().disable(); diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/gateway/QueryServiceGateway.java b/dbrepo-table-service/services/src/main/java/at/tuwien/gateway/QueryServiceGateway.java index f5ebe4683d95dfeb44c36e438ed2f89797e120ec..cc9d4fbc98f4210ec486236ddca885ed32709a6e 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/gateway/QueryServiceGateway.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/gateway/QueryServiceGateway.java @@ -5,13 +5,12 @@ import at.tuwien.exception.AmqpException; public interface QueryServiceGateway { /** - * Publish new data into a table with given container id, database id, table id. + * Publish new data into a table with given database id, table id. * - * @param containerId The container id. * @param databaseId The database id. * @param tableId The table id. * @param authorization The authentication token. */ - void declareConsumer(Long containerId, Long databaseId, Long tableId, String authorization) throws AmqpException; + void declareConsumer(Long databaseId, Long tableId, String authorization) throws AmqpException; } diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/gateway/impl/QueryServiceGatewayImpl.java b/dbrepo-table-service/services/src/main/java/at/tuwien/gateway/impl/QueryServiceGatewayImpl.java index e61b367b993f58945645ee7e00e64cfe17962af7..60b685c5e8a81377b952ac90905460add2ae6ba6 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/gateway/impl/QueryServiceGatewayImpl.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/gateway/impl/QueryServiceGatewayImpl.java @@ -20,15 +20,14 @@ public class QueryServiceGatewayImpl implements QueryServiceGateway { } @Override - public void declareConsumer(Long containerId, Long databaseId, Long tableId, String authorization) throws AmqpException { - final String url = "/api/container/" + containerId + "/database/" + databaseId + "/table/" + tableId + "/consumer"; + public void declareConsumer(Long databaseId, Long tableId, String authorization) throws AmqpException { + final String url = "/api/database/" + databaseId + "/table/" + tableId + "/consumer"; final HttpHeaders headers = new HttpHeaders(); headers.set("Authorization", authorization); final ResponseEntity<Void> response = restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<>(null, headers), Void.class); if (!response.getStatusCode().equals(HttpStatus.ACCEPTED)) { - log.error("Failed to declare consumer for container with id {} database with id {} table with id {}", - containerId, databaseId, tableId); + log.error("Failed to declare consumer for database with id {} table with id {}", databaseId, tableId); throw new AmqpException("Failed to declare consumer"); } } diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/mapper/TableMapper.java b/dbrepo-table-service/services/src/main/java/at/tuwien/mapper/TableMapper.java index 6c2d65f211cd4ea6ff3a501ba3e8d4b5cc6c70bb..10ec6fb3b9a6eb8f607b859d72ff83285390dc39 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/mapper/TableMapper.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/mapper/TableMapper.java @@ -54,7 +54,6 @@ public interface TableMapper { @Mappings({ @Mapping(source = "id", target = "id"), - @Mapping(source = "tdbid", target = "containerId"), @Mapping(source = "tdbid", target = "databaseId"), @Mapping(target = "name", expression = "java(data.getName())"), @Mapping(target = "internalName", expression = "java(data.getInternalName())"), @@ -69,7 +68,6 @@ public interface TableMapper { /* keep */ @Mappings({ @Mapping(target = "tableId", source = "tid"), - @Mapping(target = "containerId", source = "cdbid"), @Mapping(target = "databaseId", source = "cdbid"), @Mapping(target = "isPublic", source = "table.database.isPublic"), }) @@ -298,7 +296,7 @@ public interface TableMapper { * @return The drop table query */ default PreparedStatement tableToDropTableRawQuery(Connection connection, Table data) throws ImageNotSupportedException, QueryMalformedException { - if (!data.getDatabase().getContainer().getImage().getRepository().equals("mariadb")) { + if (!data.getDatabase().getContainer().getImage().getName().equals("mariadb")) { log.error("Currently only MariaDB is supported"); throw new ImageNotSupportedException("Currently only MariaDB is supported"); } @@ -325,7 +323,7 @@ public interface TableMapper { */ default TableCreateRawQuery tableToCreateTableRawQuery(Connection connection, Database database, TableCreateDto data) throws ImageNotSupportedException, TableMalformedException, QueryMalformedException { - if (!database.getContainer().getImage().getRepository().equals("mariadb")) { + if (!database.getContainer().getImage().getName().equals("mariadb")) { log.error("Currently only MariaDB is supported"); throw new ImageNotSupportedException("Currently only MariaDB is supported"); } @@ -443,7 +441,7 @@ public interface TableMapper { default PreparedStatement tableToCreateSequenceRawQuery(Connection connection, Database database, TableCreateDto data) throws ImageNotSupportedException, QueryMalformedException { - if (!database.getContainer().getImage().getRepository().equals("mariadb")) { + if (!database.getContainer().getImage().getName().equals("mariadb")) { log.error("Currently only MariaDB is supported"); throw new ImageNotSupportedException("Currently only MariaDB is supported"); } @@ -462,7 +460,7 @@ public interface TableMapper { default PreparedStatement tableToDropSequenceRawQuery(Connection connection, Database database, TableCreateDto data) throws ImageNotSupportedException, QueryMalformedException { - if (!database.getContainer().getImage().getRepository().equals("mariadb")) { + if (!database.getContainer().getImage().getName().equals("mariadb")) { log.error("Currently only MariaDB is supported"); throw new ImageNotSupportedException("Currently only MariaDB is supported"); } diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/repository/mdb/DatabaseRepository.java b/dbrepo-table-service/services/src/main/java/at/tuwien/repository/mdb/DatabaseRepository.java index 2ea6e5234ac29a4c791c56a609a29da59e6ee0ac..9949a9277072e6789488547a2857333a05451d49 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/repository/mdb/DatabaseRepository.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/repository/mdb/DatabaseRepository.java @@ -11,13 +11,13 @@ import java.util.Optional; @Repository public interface DatabaseRepository extends JpaRepository<Database, Long> { - @Query("select d from Database d where d.container.id = :containerId and d.id = :databaseId and (d.isPublic = true or d.creator.username = :username)") - Optional<Database> findPublicOrMine(@Param("containerId") Long containerId, @Param("databaseId") Long databaseId, @Param("username") String username); + @Query("select d from Database d where d.id = :databaseId and (d.isPublic = true or d.creator.username = :username)") + Optional<Database> findPublicOrMine(@Param("databaseId") Long databaseId, @Param("username") String username); - @Query("select d from Database d where d.container.id = :containerId and d.id = :databaseId and d.isPublic = true") - Optional<Database> findPublic(@Param("containerId") Long containerId, @Param("databaseId") Long databaseId); + @Query("select d from Database d where d.id = :databaseId and d.isPublic = true") + Optional<Database> findPublic(@Param("databaseId") Long databaseId); - @Query("select d from Database d where d.container.id = :containerId and d.id = :databaseId") - Optional<Database> findByContainerIdAndDatabaseId(@Param("containerId") Long containerId, @Param("databaseId") Long databaseId); + @Query("select d from Database d where d.id = :databaseId") + Optional<Database> findByContainerIdAndDatabaseId(@Param("databaseId") Long databaseId); } diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/repository/mdb/ImageEnvironmentRepository.java b/dbrepo-table-service/services/src/main/java/at/tuwien/repository/mdb/ImageEnvironmentRepository.java deleted file mode 100644 index 6bb192292f755b8e3706820c337a0e0b1dc0557b..0000000000000000000000000000000000000000 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/repository/mdb/ImageEnvironmentRepository.java +++ /dev/null @@ -1,11 +0,0 @@ -package at.tuwien.repository.mdb; - -import at.tuwien.entities.container.image.ContainerImageEnvironmentItem; -import at.tuwien.entities.container.image.ContainerImageEnvironmentItemKey; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.stereotype.Repository; - -@Repository -public interface ImageEnvironmentRepository extends JpaRepository<ContainerImageEnvironmentItem, ContainerImageEnvironmentItemKey> { - -} diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/service/DatabaseService.java b/dbrepo-table-service/services/src/main/java/at/tuwien/service/DatabaseService.java index 45c526537650822e2bffd3b8b45b4ee7a1afaba3..4564509ff95683695567fd1dfcf26785abe78bb9 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/service/DatabaseService.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/service/DatabaseService.java @@ -1,19 +1,16 @@ package at.tuwien.service; import at.tuwien.entities.database.Database; -import at.tuwien.exception.*; - -import java.security.Principal; +import at.tuwien.exception.DatabaseNotFoundException; public interface DatabaseService { /** * Finds a specific database for a given id in the metadata database. * - * @param containerId The container id. * @param databaseId The database id. * @return The database if found. * @throws DatabaseNotFoundException The database was not found. */ - Database find(Long containerId, Long databaseId) throws DatabaseNotFoundException; + Database find(Long databaseId) throws DatabaseNotFoundException; } diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/service/TableService.java b/dbrepo-table-service/services/src/main/java/at/tuwien/service/TableService.java index 3bc0a73af55fa381d5b035cf214f943c155f766b..491053eb68a983db0cae5f6caeb9e811cebcb525 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/service/TableService.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/service/TableService.java @@ -5,7 +5,6 @@ import at.tuwien.api.database.table.columns.concepts.ColumnSemanticsUpdateDto; import at.tuwien.entities.database.table.Table; import at.tuwien.entities.database.table.columns.TableColumn; import at.tuwien.exception.*; -import org.springframework.transaction.annotation.Transactional; import java.security.Principal; import java.util.List; @@ -15,16 +14,14 @@ public interface TableService { /** * Select all tables from the metadata database. * - * @param containerId The container id. * @param databaseId The database id. * @return The list of tables. */ - List<Table> findAll(Long containerId, Long databaseId) throws DatabaseNotFoundException; + List<Table> findAll(Long databaseId) throws DatabaseNotFoundException; /** * Deletes a table for a fiven database-table id pair. * - * @param containerId The container id. * @param databaseId The database id. * @param tableId The table id. * @throws TableNotFoundException The table was not found in the metadata database. @@ -32,7 +29,7 @@ public interface TableService { * @throws ImageNotSupportedException The image is not supported. * @throws DataProcessingException The deletion did not work. */ - void deleteTable(Long containerId, Long databaseId, Long tableId) + void deleteTable(Long databaseId, Long tableId) throws TableNotFoundException, DatabaseNotFoundException, ImageNotSupportedException, DataProcessingException, ContainerNotFoundException, TableMalformedException, QueryMalformedException; @@ -40,21 +37,19 @@ public interface TableService { /** * Find a table by database-table id pair * - * @param containerId The container id. * @param databaseId The database id. * @param tableId The table id. * @return The table. * @throws TableNotFoundException The table was not found in the metadata database. * @throws DatabaseNotFoundException The database was not found in the metadata database. */ - Table findById(Long containerId, Long databaseId, Long tableId) - throws TableNotFoundException, DatabaseNotFoundException, ContainerNotFoundException; + Table findById(Long databaseId, Long tableId) + throws TableNotFoundException, DatabaseNotFoundException; /** * Creates a table for a database id with given schema as data * - * @param containerId The container id. * @param databaseId The database id. * @param createDto The schema (as data). * @param principal The principal. @@ -65,7 +60,7 @@ public interface TableService { * @throws ContainerNotFoundException The container was not found. * @throws TableMalformedException The table seems malformed by the mapper. */ - Table createTable(Long containerId, Long databaseId, TableCreateDto createDto, Principal principal) + Table createTable(Long databaseId, TableCreateDto createDto, Principal principal) throws ImageNotSupportedException, DatabaseNotFoundException, TableMalformedException, TableNameExistsException, ContainerNotFoundException, UserNotFoundException, QueryMalformedException; @@ -73,7 +68,6 @@ public interface TableService { /** * Updates a table column * - * @param containerId The container id. * @param databaseId The database id. * @param tableId The table id. * @param columnId The column id. @@ -84,7 +78,7 @@ public interface TableService { * @throws ContainerNotFoundException The container was not found. * @throws TableMalformedException The table seems malformed by the mapper. */ - TableColumn update(Long containerId, Long databaseId, Long tableId, Long columnId, + TableColumn update(Long databaseId, Long tableId, Long columnId, ColumnSemanticsUpdateDto updateDto, String authorization) throws TableNotFoundException, DatabaseNotFoundException, ContainerNotFoundException, TableMalformedException, SemanticEntityPersistException, SemanticEntityNotFoundException; diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/DatabaseServiceImpl.java b/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/DatabaseServiceImpl.java index a4437d6461f47a9bf441eb448b9574974e0acc64..4bbf56e20ace77276c17b60b6cdc96b50cfa96e7 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/DatabaseServiceImpl.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/DatabaseServiceImpl.java @@ -22,8 +22,8 @@ public class DatabaseServiceImpl implements DatabaseService { } @Override - public Database find(Long containerId, Long databaseId) throws DatabaseNotFoundException { - final Optional<Database> database = databaseRepository.findByContainerIdAndDatabaseId(containerId, databaseId); + public Database find(Long databaseId) throws DatabaseNotFoundException { + final Optional<Database> database = databaseRepository.findByContainerIdAndDatabaseId(databaseId); if (database.isEmpty()) { log.error("Failed to find database with id {}", databaseId); throw new DatabaseNotFoundException("could not find database with this id"); diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/HibernateConnector.java b/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/HibernateConnector.java index 3656f0a6efdc2d0d110f315502e7e795cc02850b..144112ade064e0c0e938a326cfca0d71cd377250 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/HibernateConnector.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/HibernateConnector.java @@ -2,41 +2,25 @@ package at.tuwien.service.impl; import at.tuwien.entities.container.Container; import at.tuwien.entities.container.image.ContainerImage; -import at.tuwien.entities.container.image.ContainerImageEnvironmentItem; -import at.tuwien.entities.container.image.ContainerImageEnvironmentItemType; import at.tuwien.entities.database.Database; import com.mchange.v2.c3p0.ComboPooledDataSource; import lombok.extern.log4j.Log4j2; import org.springframework.stereotype.Service; -import java.util.stream.Collectors; - @Log4j2 @Service public abstract class HibernateConnector { - protected static ComboPooledDataSource getDataSource(ContainerImage image, Container container) { - return getDataSource(image, container, null); + protected static ComboPooledDataSource getPrivilegedDataSource(ContainerImage image, Container container) { + return getPrivilegedDataSource(image, container, null); } - protected static ComboPooledDataSource getDataSource(ContainerImage image, Container container, Database database) { + protected static ComboPooledDataSource getPrivilegedDataSource(ContainerImage image, Container container, Database database) { final ComboPooledDataSource dataSource = new ComboPooledDataSource(); - final String url = "jdbc:" + image.getJdbcMethod() + "://" + container.getInternalName() + "/" + (database != null ? database.getInternalName() : ""); + final String url = "jdbc:" + image.getJdbcMethod() + "://" + container.getHost() + ":" + container.getPort() + "/" + (database != null ? database.getInternalName() : ""); dataSource.setJdbcUrl(url); - final String username = image.getEnvironment() - .stream() - .filter(e -> e.getType().equals(ContainerImageEnvironmentItemType.PRIVILEGED_USERNAME)) - .map(ContainerImageEnvironmentItem::getValue) - .collect(Collectors.toList()) - .get(0); - dataSource.setUser(username); - final String password = image.getEnvironment() - .stream() - .filter(e -> e.getType().equals(ContainerImageEnvironmentItemType.PRIVILEGED_PASSWORD)) - .map(ContainerImageEnvironmentItem::getValue) - .collect(Collectors.toList()) - .get(0); - dataSource.setPassword(password); + dataSource.setUser(container.getPrivilegedUsername()); + dataSource.setPassword(container.getPrivilegedPassword()); dataSource.setInitialPoolSize(5); dataSource.setMinPoolSize(5); dataSource.setAcquireIncrement(5); diff --git a/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/TableServiceImpl.java b/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/TableServiceImpl.java index 463dc7edd8f4c699d2c90cd2c80b69bbe83a66b8..28e8a43567bd70538e6f4b29313df5fa1803b6c8 100644 --- a/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/TableServiceImpl.java +++ b/dbrepo-table-service/services/src/main/java/at/tuwien/service/impl/TableServiceImpl.java @@ -3,7 +3,6 @@ package at.tuwien.service.impl; import at.tuwien.api.database.table.TableCreateDto; import at.tuwien.api.database.table.TableCreateRawQuery; import at.tuwien.api.database.table.columns.concepts.ColumnSemanticsUpdateDto; -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.columns.TableColumn; @@ -25,7 +24,8 @@ import java.security.Principal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; -import java.util.*; +import java.util.List; +import java.util.Optional; import java.util.stream.Collectors; @Log4j2 @@ -61,8 +61,8 @@ public class TableServiceImpl extends HibernateConnector implements TableService @Override @Transactional(readOnly = true) - public List<Table> findAll(Long containerId, Long databaseId) throws DatabaseNotFoundException { - final Database database = databaseService.find(containerId, databaseId); + public List<Table> findAll(Long databaseId) throws DatabaseNotFoundException { + final Database database = databaseService.find(databaseId); final List<Table> tables = tableRepository.findByDatabaseOrderByCreatedDesc(database); log.trace("found {} table(s) in database with id {}", tables.size(), databaseId); return tables; @@ -70,14 +70,14 @@ public class TableServiceImpl extends HibernateConnector implements TableService @Override @Transactional - public void deleteTable(Long containerId, Long databaseId, Long tableId) + public void deleteTable(Long databaseId, Long tableId) throws TableNotFoundException, DatabaseNotFoundException, ImageNotSupportedException, TableMalformedException, QueryMalformedException, ContainerNotFoundException { /* find */ - final Database database = databaseService.find(containerId, databaseId); - final Table table = findById(containerId, databaseId, tableId); + final Database database = databaseService.find(databaseId); + final Table table = findById(databaseId, tableId); /* run query */ - final ComboPooledDataSource dataSource = getDataSource(database.getContainer().getImage(), database.getContainer(), database); + final ComboPooledDataSource dataSource = getPrivilegedDataSource(database.getContainer().getImage(), database.getContainer(), database); try { final Connection connection = dataSource.getConnection(); final PreparedStatement preparedStatement = tableMapper.tableToDropTableRawQuery(connection, table); @@ -96,10 +96,9 @@ public class TableServiceImpl extends HibernateConnector implements TableService @Override @Transactional(readOnly = true) - public Table findById(Long containerId, Long databaseId, Long tableId) - throws TableNotFoundException, DatabaseNotFoundException, ContainerNotFoundException { - final Container container = containerService.find(containerId); - final Database database = databaseService.find(containerId, databaseId); + public Table findById(Long databaseId, Long tableId) + throws TableNotFoundException, DatabaseNotFoundException { + final Database database = databaseService.find(databaseId); final Optional<Table> optional = tableRepository.findByDatabaseAndId(database, tableId); if (optional.isEmpty()) { log.error("Failed to find table with id {} in metadata database", tableId); @@ -110,11 +109,11 @@ public class TableServiceImpl extends HibernateConnector implements TableService @Override @Transactional - public Table createTable(Long containerId, Long databaseId, TableCreateDto createDto, Principal principal) + public Table createTable(Long databaseId, TableCreateDto createDto, Principal principal) throws ImageNotSupportedException, DatabaseNotFoundException, TableMalformedException, TableNameExistsException, UserNotFoundException, QueryMalformedException { /* find */ - final Database database = databaseService.find(containerId, databaseId); + final Database database = databaseService.find(databaseId); final Optional<Table> optional = tableRepository.findByDatabaseAndInternalName(database, tableMapper.nameToInternalName(createDto.getName())); if (optional.isPresent()) { @@ -122,7 +121,7 @@ public class TableServiceImpl extends HibernateConnector implements TableService throw new TableNameExistsException("Table exists in metadata database"); } /* run query */ - final ComboPooledDataSource dataSource = getDataSource(database.getContainer().getImage(), database.getContainer(), database); + final ComboPooledDataSource dataSource = getPrivilegedDataSource(database.getContainer().getImage(), database.getContainer(), database); final TableCreateRawQuery query; try { final Connection connection = dataSource.getConnection(); @@ -177,7 +176,7 @@ public class TableServiceImpl extends HibernateConnector implements TableService /* set constraints */ entity.setConstraints(tableMapper.constraintsCreateDtoToConstraints(tableRepository, entity, createDto.getConstraints())); /* create history view */ - final ComboPooledDataSource dataSource1 = getDataSource(database.getContainer().getImage(), database.getContainer(), database); + final ComboPooledDataSource dataSource1 = getPrivilegedDataSource(database.getContainer().getImage(), database.getContainer(), database); try { final Connection connection = dataSource1.getConnection(); final PreparedStatement preparedStatement = tableMapper.tableToCreateHistoryViewRawQuery(connection, entity); @@ -202,11 +201,11 @@ public class TableServiceImpl extends HibernateConnector implements TableService @Override @Transactional - public TableColumn update(Long containerId, Long databaseId, Long tableId, Long columnId, + public TableColumn update(Long databaseId, Long tableId, Long columnId, ColumnSemanticsUpdateDto updateDto, String authorization) throws TableNotFoundException, DatabaseNotFoundException, ContainerNotFoundException, TableMalformedException, SemanticEntityNotFoundException { - final Table table = findById(containerId, databaseId, tableId); + final Table table = findById(databaseId, tableId); final TableColumn column = findColumn(table, columnId); /* assign */ if (updateDto.getUnitUri() != null) {