Skip to content

Commit

Permalink
clean up persistence configuration; move unit test to internal testco…
Browse files Browse the repository at this point in the history
…ntainer
  • Loading branch information
clezag committed Jan 17, 2025
1 parent 7d3bfd5 commit a3f3a09
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 133 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ jobs:
- name: REUSE Compliance Check
uses: fsfe/reuse-action@v1

- name: Start postgis for testing
run: docker run --rm -d --name postgres -e POSTGRES_DB=bdp -e POSTGRES_USER=bdp -e POSTGRES_PASSWORD=password -p "5555:5432" postgis/postgis:16-3.5-alpine

- name: Test code
uses: noi-techpark/github-actions/maven-test@v2
with:
java-version: ${{ env.JAVA_VERSION }}
test-command: 'mvn -B test -Dspring.flyway.enabled=true'
test-command: 'mvn -B test'

- name: Stop postgis
run: docker kill postgres
Expand Down
72 changes: 43 additions & 29 deletions writer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ SPDX-FileCopyrightText: NOI Techpark <digital@noi.bz.it>
SPDX-License-Identifier: CC0-1.0
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>com.opendatahub.timeseries.bdp</groupId>
<artifactId>core</artifactId>
<groupId>com.opendatahub.timeseries.bdp</groupId>
<artifactId>core</artifactId>
<version>9.0.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<relativePath>../pom.xml</relativePath>
</parent>

<packaging>jar</packaging> <!-- jar packaging needed by infrastructure/utils/schema-generator -->
<groupId>com.opendatahub.timeseries.bdp</groupId>
Expand All @@ -22,7 +24,7 @@ SPDX-License-Identifier: CC0-1.0
<properties>
<finalName>writer</finalName>
<geotools.version>30.0</geotools.version>
<hibernate.version>6.3.1.Final</hibernate.version>
<hibernate.version>6.6.4.Final</hibernate.version>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -57,10 +59,10 @@ SPDX-License-Identifier: CC0-1.0
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
Expand All @@ -70,18 +72,18 @@ SPDX-License-Identifier: CC0-1.0
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Keycloak UMA To request Resource level authorization -->
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-core</artifactId>
<version>26.0.6</version>
</dependency>
</dependency>
<dependency>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-authz-client</artifactId>
<groupId>org.keycloak</groupId>
<artifactId>keycloak-authz-client</artifactId>
<version>26.0.3</version>
</dependency>
</dependency>

<!-- To create JSON schema descriptions out of classes -->
<dependency>
Expand All @@ -98,10 +100,10 @@ SPDX-License-Identifier: CC0-1.0
</dependency>

<!-- DB migration - Flyway -->
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>

<!-- Hibernate: ORM with spatial entity support (Postgis, Geometry, ...) -->
<dependency>
Expand All @@ -114,13 +116,13 @@ SPDX-License-Identifier: CC0-1.0
<artifactId>hibernate-spatial</artifactId>
<version>${hibernate.version}</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>

<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
</dependency>

<!--
Second level cache, to store entities that never change as
for example provenance records.
Expand Down Expand Up @@ -183,6 +185,18 @@ SPDX-License-Identifier: CC0-1.0
<artifactId>commons-text</artifactId>
<version>1.10.0</version>
</dependency>
<!-- Container support for testing -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>postgresql</artifactId>
<version>1.20.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down Expand Up @@ -215,4 +229,4 @@ SPDX-License-Identifier: CC0-1.0
<id>generator</id>
</profile>
</profiles>
</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,10 @@

package com.opendatahub.timeseries.bdp.writer.writer.config;

import org.springframework.context.annotation.Bean;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.Configuration;
import com.opendatahub.timeseries.bdp.writer.dal.util.PropertiesWithEnv;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.Persistence;

@Configuration
@EntityScan("com.opendatahub.timeseries.bdp.writer.dal")
public class PersistenceConfig {

public PersistenceConfig() {
super();
}

@Bean
public EntityManagerFactory entityManagerFactory() {
try {
PropertiesWithEnv properties = PropertiesWithEnv.fromActiveSpringProfile();
return Persistence.createEntityManagerFactory(
"jpa-persistence", // This must correspond to the persistence.xml persistence-unit tag
properties.getStringMap()
);
} catch (Exception ex) {
throw new RuntimeException("PersistenceConfig: Cannot create EntityManagerFactory", ex);
}
}
}
6 changes: 0 additions & 6 deletions writer/src/main/resources/META-INF/persistence.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,10 @@ SPDX-License-Identifier: CC0-1.0
<persistence-unit name="jpa-persistence" transaction-type="RESOURCE_LOCAL">
<properties>
<property name="hibernate.default_schema" value="intimev2"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="none"/>
<property name="hibernate.connection.provider_class" value="org.hibernate.hikaricp.internal.HikariCPConnectionProvider"/>
<property name="hibernate.hikari.dataSourceClassName" value="org.postgresql.ds.PGSimpleDataSource"/>
<property name="hibernate.hikari.maximumPoolSize" value="2" />
<property name="hibernate.hikari.dataSource.serverName" value="postgres"/>
<property name="hibernate.hikari.dataSource.portNumber" value="5432"/>
<property name="hibernate.hikari.dataSource.databaseName" value="bdp"/>
<property name="hibernate.hikari.dataSource.user" value="bdp"/>
<property name="hibernate.hikari.dataSource.password" value='password'/>
<property name="hibernate.id.new_generator_mappings" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2dll.extra_physical_table_types" value="MATERIALIZED VIEW"/>
Expand Down
16 changes: 8 additions & 8 deletions writer/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ auth.client.id=${KEYCLOAK_CLIENT_ID:odh-mobility-writer-development}

### Database (see persistence.xml for details; .properties values override .xml values)
# The default is set to the docker postgres connection (see docker-compose.yml)
hibernate.hikari.dataSource.serverName=${POSTGRES_SERVER:localhost}
hibernate.hikari.dataSource.portNumber=${POSTGRES_PORT:5555}
hibernate.hikari.dataSource.databaseName=${POSTGRES_DB:bdp}
hibernate.default_schema=${POSTGRES_SCHEMA:intimev2}
hibernate.hikari.dataSource.user=${POSTGRES_USERNAME:bdp}
hibernate.hikari.dataSource.password=${POSTGRES_PASSWORD:password}
hibernate.hikari.maximumPoolSize=${HIBERNATE_MAX_POOL_SIZE:2}
# hibernate.hikari.dataSource.serverName=${POSTGRES_SERVER:localhost}
# hibernate.hikari.dataSource.portNumber=${POSTGRES_PORT:5555}
# hibernate.hikari.dataSource.databaseName=${POSTGRES_DB:bdp}
# hibernate.default_schema=${POSTGRES_SCHEMA:intimev2}
# hibernate.hikari.dataSource.user=${POSTGRES_USERNAME:bdp}
# hibernate.hikari.dataSource.password=${POSTGRES_PASSWORD:password}
# hibernate.hikari.maximumPoolSize=${HIBERNATE_MAX_POOL_SIZE:2}

# FIXME: These values should be used, not hibernate.* (remove also persistence.xml)
# Needed only for flyway for now
Expand All @@ -52,7 +52,7 @@ spring.datasource.hikari.maximum-pool-size=${HIBERNATE_MAX_POOL_SIZE:2}
spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.implicit-strategy=com.opendatahub.timeseries.bdp.writer.dal.util.SchemaGeneratorImplicitNamingStrategy
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=com.opendatahub.timeseries.bdp.writer.dal.util.PostgisJsonDialect
# spring.jpa.properties.hibernate.dialect=com.opendatahub.timeseries.bdp.writer.dal.util.PostgisJsonDialect

# Database migrations
spring.flyway.enabled=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.PrecisionModel;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;

import com.opendatahub.timeseries.bdp.dto.dto.DataMapDto;
Expand All @@ -39,10 +39,8 @@
import com.opendatahub.timeseries.bdp.writer.dal.MeasurementAbstract;
import com.opendatahub.timeseries.bdp.writer.dal.Station;
import com.opendatahub.timeseries.bdp.writer.writer.Application;
import com.opendatahub.timeseries.bdp.writer.writer.config.PersistenceConfig;

@SpringBootTest
@Import(PersistenceConfig.class)
@ContextConfiguration(classes = Application.class)
public class DataRetrievalITTest extends WriterSetupTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@
import java.util.Date;
import java.util.List;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Import;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

import com.opendatahub.timeseries.bdp.dto.dto.DataMapDto;
import com.opendatahub.timeseries.bdp.dto.dto.RecordDtoImpl;
Expand All @@ -23,7 +30,6 @@
import com.opendatahub.timeseries.bdp.writer.dal.Provenance;
import com.opendatahub.timeseries.bdp.writer.dal.Station;
import com.opendatahub.timeseries.bdp.writer.writer.DataManager;
import com.opendatahub.timeseries.bdp.writer.writer.config.PersistenceConfig;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
Expand All @@ -35,7 +41,10 @@
*
* Abstract, because we do not want to run this class itself.
*/
@Import(PersistenceConfig.class)
@TestPropertySource(properties = {
"spring.flyway.enabled=true",
})
@DirtiesContext
public abstract class WriterSetupTest extends AbstractJUnit4SpringContextTests {

@PersistenceUnit
Expand All @@ -59,6 +68,26 @@ public abstract class WriterSetupTest extends AbstractJUnit4SpringContextTests {
protected Measurement measurementOld;
protected Provenance provenance;

static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
DockerImageName.parse("postgis/postgis:16-3.5-alpine").asCompatibleSubstituteFor("postgres"));

@BeforeAll
static void startPG() {
postgres.start();
}

@AfterAll
static void stopPG() {
postgres.stop();
}

@DynamicPropertySource
static void configureProperties(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", () -> postgres.getJdbcUrl() + "?currentSchema=intimev2,public");
registry.add("spring.datasource.username", postgres::getUsername);
registry.add("spring.datasource.password", postgres::getPassword);
}

@BeforeEach
public void setup() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,15 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ContextConfiguration;

import com.opendatahub.timeseries.bdp.writer.writer.Application;
import com.opendatahub.timeseries.bdp.writer.writer.config.PersistenceConfig;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;
import jakarta.persistence.PersistenceUnit;

@DataJpaTest
@Import(PersistenceConfig.class)
@ContextConfiguration(classes = Application.class)
public class PersistenceIT {

Expand Down
48 changes: 0 additions & 48 deletions writer/src/test/resources/META-INF/persistence.xml

This file was deleted.

Loading

0 comments on commit a3f3a09

Please sign in to comment.