From d31d8e1cc167abf7835a1771b5ddc8d78aba9ac6 Mon Sep 17 00:00:00 2001 From: "puthuparambil.aditya" Date: Thu, 6 May 2021 16:12:44 +0100 Subject: Implement service and repository layers for storing temporal data 1. Basic structure created 2. Basic tests added 3. lombok.config included for coverage Issue-ID: CPS-194 Signed-off-by: puthuparambil.aditya Change-Id: Icf23c2e647106f7985dff14d9901806f7c4aa55a --- lombok.config | 2 + pom.xml | 26 ++++++-- .../org/onap/cps/temporal/domain/NetworkData.java | 78 ++++++++++++++++++++++ .../onap/cps/temporal/domain/NetworkDataId.java | 41 ++++++++++++ .../temporal/repository/NetworkDataRepository.java | 28 ++++++++ .../cps/temporal/service/NetworkDataService.java | 31 +++++++++ .../temporal/service/NetworkDataServiceImpl.java | 41 ++++++++++++ src/main/resources/application.yml | 12 ++++ .../controller/QuerryControllerSpec.groovy | 37 ---------- .../temporal/controller/QueryControllerSpec.groovy | 37 ++++++++++ .../repository/NetworkDataRepositorySpec.groovy | 70 +++++++++++++++++++ .../service/NetworkDataServiceImplSpec.groovy | 45 +++++++++++++ .../org/onap/cps/temporal/ApplicationTest.java | 43 ------------ .../temporal/repository/TimescaleContainer.java | 68 ------------------- .../repository/containers/TimescaleContainer.java | 68 +++++++++++++++++++ src/test/resources/application.yml | 13 ++++ 16 files changed, 488 insertions(+), 152 deletions(-) create mode 100644 lombok.config create mode 100644 src/main/java/org/onap/cps/temporal/domain/NetworkData.java create mode 100644 src/main/java/org/onap/cps/temporal/domain/NetworkDataId.java create mode 100644 src/main/java/org/onap/cps/temporal/repository/NetworkDataRepository.java create mode 100644 src/main/java/org/onap/cps/temporal/service/NetworkDataService.java create mode 100644 src/main/java/org/onap/cps/temporal/service/NetworkDataServiceImpl.java delete mode 100644 src/test/groovy/org/onap/cps/temporal/controller/QuerryControllerSpec.groovy create mode 100644 src/test/groovy/org/onap/cps/temporal/controller/QueryControllerSpec.groovy create mode 100644 src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy create mode 100644 src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy delete mode 100644 src/test/java/org/onap/cps/temporal/ApplicationTest.java delete mode 100644 src/test/java/org/onap/cps/temporal/repository/TimescaleContainer.java create mode 100644 src/test/java/org/onap/cps/temporal/repository/containers/TimescaleContainer.java diff --git a/lombok.config b/lombok.config new file mode 100644 index 0000000..a23edb4 --- /dev/null +++ b/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5579823..1696458 100755 --- a/pom.xml +++ b/pom.xml @@ -37,16 +37,17 @@ org.onap.cps.temporal.Application + 1.0.1 + 1.0.1 nexus3.onap.org:10001/ nexus3.onap.org:10003/ ${docker.repository.pull}onap/integration-java11:8.0.0 ${docker.repository.push}onap/cps-temporal + 2.10.0 11 - 0.8 - 1.0.1 - 1.0.1 3.0.0 yyyyMMdd'T'HHmmss'Z' + 0.8 3.2.0 4.1.3 1.8.0-beta4 @@ -85,11 +86,24 @@ org.springframework.boot spring-boot-starter-data-jpa + + com.vladmihalcea + hibernate-types-52 + ${hibernate-types.version} + org.liquibase liquibase-core 4.3.2 + + org.projectlombok + lombok + + + org.springframework.boot + spring-boot-starter-validation + org.postgresql @@ -118,6 +132,11 @@ spock-core test + + org.spockframework + spock-spring + test + org.testcontainers junit-jupiter @@ -313,7 +332,6 @@ org.jacoco jacoco-maven-plugin - 0.8.6 coverage-prepare-agent diff --git a/src/main/java/org/onap/cps/temporal/domain/NetworkData.java b/src/main/java/org/onap/cps/temporal/domain/NetworkData.java new file mode 100644 index 0000000..c4f3176 --- /dev/null +++ b/src/main/java/org/onap/cps/temporal/domain/NetworkData.java @@ -0,0 +1,78 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.domain; + +import com.vladmihalcea.hibernate.type.json.JsonBinaryType; +import java.io.Serializable; +import java.time.OffsetDateTime; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.IdClass; +import javax.persistence.Table; +import javax.validation.constraints.NotNull; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.hibernate.annotations.CreationTimestamp; +import org.hibernate.annotations.Type; +import org.hibernate.annotations.TypeDef; + +/** + * Entity to store an anchor configuration or state along with the moment it has been observed. + */ +@Data +@NoArgsConstructor +@AllArgsConstructor +@IdClass(NetworkDataId.class) +@Builder +@Entity +@Table(name = "network_data") +@TypeDef(name = "jsonb", typeClass = JsonBinaryType.class) +public class NetworkData implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @Column(name = "timestamp") + private OffsetDateTime observedTimestamp; + + @Id + @Column + private String dataspace; + + @Id + @Column + private String anchor; + + @NotNull + @Column + private String schemaSet; + + @NotNull + @Type(type = "jsonb") + @Column(columnDefinition = "jsonb") + private String payload; + + @CreationTimestamp + @Column(name = "version", updatable = false) + private OffsetDateTime createdTimestamp; + +} diff --git a/src/main/java/org/onap/cps/temporal/domain/NetworkDataId.java b/src/main/java/org/onap/cps/temporal/domain/NetworkDataId.java new file mode 100644 index 0000000..e9742e2 --- /dev/null +++ b/src/main/java/org/onap/cps/temporal/domain/NetworkDataId.java @@ -0,0 +1,41 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.domain; + +import java.io.Serializable; +import java.time.OffsetDateTime; +import lombok.AllArgsConstructor; +import lombok.EqualsAndHashCode; +import lombok.NoArgsConstructor; + +/** + * Identifier class for network data. + */ +@NoArgsConstructor +@AllArgsConstructor +@EqualsAndHashCode +public class NetworkDataId implements Serializable { + + private static final long serialVersionUID = 1L; + + private String dataspace; + private String anchor; + private OffsetDateTime observedTimestamp; + +} \ No newline at end of file diff --git a/src/main/java/org/onap/cps/temporal/repository/NetworkDataRepository.java b/src/main/java/org/onap/cps/temporal/repository/NetworkDataRepository.java new file mode 100644 index 0000000..2e9f34b --- /dev/null +++ b/src/main/java/org/onap/cps/temporal/repository/NetworkDataRepository.java @@ -0,0 +1,28 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.repository; + +import org.onap.cps.temporal.domain.NetworkData; +import org.onap.cps.temporal.domain.NetworkDataId; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface NetworkDataRepository extends JpaRepository { +} diff --git a/src/main/java/org/onap/cps/temporal/service/NetworkDataService.java b/src/main/java/org/onap/cps/temporal/service/NetworkDataService.java new file mode 100644 index 0000000..509e470 --- /dev/null +++ b/src/main/java/org/onap/cps/temporal/service/NetworkDataService.java @@ -0,0 +1,31 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.service; + +import org.onap.cps.temporal.domain.NetworkData; + +public interface NetworkDataService { + + /** + * Add Network data. + * + * @param networkData the network data to be stored + */ + NetworkData addNetworkData(NetworkData networkData); +} diff --git a/src/main/java/org/onap/cps/temporal/service/NetworkDataServiceImpl.java b/src/main/java/org/onap/cps/temporal/service/NetworkDataServiceImpl.java new file mode 100644 index 0000000..2e7afb2 --- /dev/null +++ b/src/main/java/org/onap/cps/temporal/service/NetworkDataServiceImpl.java @@ -0,0 +1,41 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.service; + +import lombok.extern.slf4j.Slf4j; +import org.onap.cps.temporal.domain.NetworkData; +import org.onap.cps.temporal.repository.NetworkDataRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; + +/** + * Service implementation for Network Data. + */ +@Component +@Slf4j +public class NetworkDataServiceImpl implements NetworkDataService { + + @Autowired + NetworkDataRepository networkDataRepository; + + @Override + public NetworkData addNetworkData(final NetworkData networkData) { + return networkDataRepository.save(networkData); + } +} diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ff70e46..d4c799c 100755 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -15,6 +15,9 @@ # limitations under the License. # ============LICENSE_END========================================================= +server: + port: 8080 + spring: datasource: url: jdbc:postgresql://${DB_HOST}:${DB_PORT}/cpstemporaldb @@ -22,3 +25,12 @@ spring: password: ${DB_PASSWORD} liquibase: change-log: classpath:/db/changelog/changelog-master.xml + jpa: + properties: + hibernate: + dialect: org.hibernate.dialect.PostgreSQLDialect + +logging: + level: + org: + springframework: INFO diff --git a/src/test/groovy/org/onap/cps/temporal/controller/QuerryControllerSpec.groovy b/src/test/groovy/org/onap/cps/temporal/controller/QuerryControllerSpec.groovy deleted file mode 100644 index f718bf4..0000000 --- a/src/test/groovy/org/onap/cps/temporal/controller/QuerryControllerSpec.groovy +++ /dev/null @@ -1,37 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (c) 2021 Bell Canada. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.temporal.controller - -import spock.lang.Specification - -/** - * Specification for Query Controller. - */ -class QueryControllerSpec extends Specification { - - def objectUnderTest = new QueryController() - - def 'Get home returns some data'() { - when: 'get home is invoked' - def response = objectUnderTest.home() - then: 'a response is returned' - ! response.empty - } - -} \ No newline at end of file diff --git a/src/test/groovy/org/onap/cps/temporal/controller/QueryControllerSpec.groovy b/src/test/groovy/org/onap/cps/temporal/controller/QueryControllerSpec.groovy new file mode 100644 index 0000000..f718bf4 --- /dev/null +++ b/src/test/groovy/org/onap/cps/temporal/controller/QueryControllerSpec.groovy @@ -0,0 +1,37 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.controller + +import spock.lang.Specification + +/** + * Specification for Query Controller. + */ +class QueryControllerSpec extends Specification { + + def objectUnderTest = new QueryController() + + def 'Get home returns some data'() { + when: 'get home is invoked' + def response = objectUnderTest.home() + then: 'a response is returned' + ! response.empty + } + +} \ No newline at end of file diff --git a/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy b/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy new file mode 100644 index 0000000..ec976ee --- /dev/null +++ b/src/test/groovy/org/onap/cps/temporal/repository/NetworkDataRepositorySpec.groovy @@ -0,0 +1,70 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.repository + + +import org.onap.cps.temporal.domain.NetworkData +import org.onap.cps.temporal.repository.containers.TimescaleContainer +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.testcontainers.junit.jupiter.Testcontainers +import spock.lang.Shared +import spock.lang.Specification + +import java.time.OffsetDateTime + +@SpringBootTest +@Testcontainers +class NetworkDataRepositorySpec extends Specification { + + def observedTimestamp = OffsetDateTime.now() + def dataspaceName = 'TEST_DATASPACE' + def schemaSetName = 'TEST_SCHEMA_SET' + def anchorName = 'TEST_ANCHOR' + def payload = '{ \"message\": \"Hello World!\" }' + + @Autowired + NetworkDataRepository networkDataRepository + + def networkData = NetworkData.builder().observedTimestamp(observedTimestamp).dataspace(dataspaceName) + .schemaSet(schemaSetName).anchor(anchorName).payload(payload).build() + + @Shared + def databaseTestContainer = TimescaleContainer.getInstance() + + def setupSpec() { + databaseTestContainer.start() + } + + def 'Store latest network data in timeseries database.'() { + when: 'a new Network Data is stored' + NetworkData savedData = networkDataRepository.save(networkData) + then: ' the saved Network Data is returned' + savedData.getDataspace() == networkData.getDataspace() + savedData.getSchemaSet() == networkData.getSchemaSet() + savedData.getAnchor() == networkData.getAnchor() + savedData.getPayload() == networkData.getPayload() + savedData.getObservedTimestamp() == networkData.getObservedTimestamp() + and: ' createdTimestamp is auto populated by db ' + networkData.getCreatedTimestamp() == null + savedData.getCreatedTimestamp() != null + and: ' the CreationTimestamp is ahead of ObservedTimestamp' + savedData.getCreatedTimestamp() > networkData.getObservedTimestamp() + } +} diff --git a/src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy b/src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy new file mode 100644 index 0000000..70ac2bc --- /dev/null +++ b/src/test/groovy/org/onap/cps/temporal/service/NetworkDataServiceImplSpec.groovy @@ -0,0 +1,45 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.service + +import java.time.OffsetDateTime +import org.onap.cps.temporal.domain.NetworkData +import org.onap.cps.temporal.repository.NetworkDataRepository +import spock.lang.Specification + +class NetworkDataServiceImplSpec extends Specification { + + def objectUnderTest = new NetworkDataServiceImpl() + + def mockNetworkDataRepository = Mock(NetworkDataRepository) + + def networkData = new NetworkData() + + def setup() { + objectUnderTest.networkDataRepository = mockNetworkDataRepository + } + + def 'Add network data in timeseries database.'() { + when: 'a new network data is added' + objectUnderTest.addNetworkData(networkData) + then: ' repository service is called with the correct parameters' + 1 * mockNetworkDataRepository.save(networkData) + } + +} diff --git a/src/test/java/org/onap/cps/temporal/ApplicationTest.java b/src/test/java/org/onap/cps/temporal/ApplicationTest.java deleted file mode 100644 index bae6e65..0000000 --- a/src/test/java/org/onap/cps/temporal/ApplicationTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (c) 2021 Bell Canada. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.temporal; - -import org.assertj.core.util.Arrays; -import org.junit.jupiter.api.Test; -import org.onap.cps.temporal.repository.TimescaleContainer; -import org.springframework.boot.test.context.SpringBootTest; - -// This test class without any assertion is obviously not really useful. -// Its only purpose is to be able to cover current code. -// It should be deleted when more code will be added to the project. -@SpringBootTest -class ApplicationTest { - - private static final TimescaleContainer TIMESCALE_CONTAINER = TimescaleContainer.getInstance(); - - static { - TIMESCALE_CONTAINER.start(); - } - - @Test - void testMain() { - Application.main(Arrays.array()); - } - -} diff --git a/src/test/java/org/onap/cps/temporal/repository/TimescaleContainer.java b/src/test/java/org/onap/cps/temporal/repository/TimescaleContainer.java deleted file mode 100644 index 73b8c13..0000000 --- a/src/test/java/org/onap/cps/temporal/repository/TimescaleContainer.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (c) 2021 Bell Canada. - * ================================================================================ - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * ============LICENSE_END========================================================= - */ - -package org.onap.cps.temporal.repository; - -import org.testcontainers.containers.PostgreSQLContainer; -import org.testcontainers.utility.DockerImageName; - -/** - * Container for timescale database. - */ -public class TimescaleContainer extends PostgreSQLContainer { - - private static final String IMAGE_NAME = "timescale/timescaledb:2.1.1-pg13"; - private static final DockerImageName DOCKER_IMAGE_NAME = - DockerImageName.parse(IMAGE_NAME).asCompatibleSubstituteFor("postgres"); - - private static TimescaleContainer container; - - private TimescaleContainer() { - super(DOCKER_IMAGE_NAME); - } - - /** - * Get the unique container instance. - * @return the container instance. - */ - public static TimescaleContainer getInstance() { - if (container == null) { - container = new TimescaleContainer(); - Runtime.getRuntime().addShutdownHook(new Thread(container::terminate)); - } - return container; - } - - @Override - public void start() { - super.start(); - System.setProperty("DB_URL", container.getJdbcUrl()); - System.setProperty("DB_USERNAME", container.getUsername()); - System.setProperty("DB_PASSWORD", container.getPassword()); - } - - @Override - public void stop() { - // Do nothing on test completion, container removal is performed via terminate() on JVM shutdown. - } - - private void terminate() { - super.stop(); - } - -} diff --git a/src/test/java/org/onap/cps/temporal/repository/containers/TimescaleContainer.java b/src/test/java/org/onap/cps/temporal/repository/containers/TimescaleContainer.java new file mode 100644 index 0000000..a6ad5db --- /dev/null +++ b/src/test/java/org/onap/cps/temporal/repository/containers/TimescaleContainer.java @@ -0,0 +1,68 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (c) 2021 Bell Canada. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.temporal.repository.containers; + +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.utility.DockerImageName; + +/** + * Container for timescale database. + */ +public class TimescaleContainer extends PostgreSQLContainer { + + private static final String IMAGE_NAME = "timescale/timescaledb:2.1.1-pg13"; + private static final DockerImageName DOCKER_IMAGE_NAME = + DockerImageName.parse(IMAGE_NAME).asCompatibleSubstituteFor("postgres"); + + private static TimescaleContainer container; + + private TimescaleContainer() { + super(DOCKER_IMAGE_NAME); + } + + /** + * Get the unique container instance. + * @return the container instance. + */ + public static TimescaleContainer getInstance() { + if (container == null) { + container = new TimescaleContainer(); + Runtime.getRuntime().addShutdownHook(new Thread(container::terminate)); + } + return container; + } + + @Override + public void start() { + super.start(); + System.setProperty("DB_URL", container.getJdbcUrl()); + System.setProperty("DB_USERNAME", container.getUsername()); + System.setProperty("DB_PASSWORD", container.getPassword()); + } + + @Override + public void stop() { + // Do nothing on test completion, container removal is performed via terminate() on JVM shutdown. + } + + private void terminate() { + super.stop(); + } + +} diff --git a/src/test/resources/application.yml b/src/test/resources/application.yml index 5f64bd9..afaff6c 100644 --- a/src/test/resources/application.yml +++ b/src/test/resources/application.yml @@ -14,6 +14,9 @@ # limitations under the License. # ============LICENSE_END========================================================= +server: + port: 8080 + spring: datasource: url: ${DB_URL} @@ -21,3 +24,13 @@ spring: username: ${DB_USERNAME} liquibase: change-log: classpath:/db/changelog/changelog-master.xml + jpa: + open-in-view: false + properties: + hibernate: + dialect: org.hibernate.dialect.PostgreSQLDialect + +logging: + level: + org: + springframework: INFO -- cgit 1.2.3-korg