summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRuslan Kashapov <ruslan.kashapov@pantheon.tech>2020-11-24 11:13:43 +0200
committerRuslan Kashapov <ruslan.kashapov@pantheon.tech>2020-12-08 11:01:31 +0200
commit7cca0296e855bbfaff7d14769db2fc76c5c06892 (patch)
tree4758811c99748c082b8496e05263c2c84674e926
parent0cd5910ca2c949bab95ec73591729c4b7230fdd1 (diff)
Persistence layer testing incl CpsModulePersistenceService testing
Issue-ID: CPS-95 Change-Id: I5eba5f6953f483304ff3864914e7a6c9b2bb3f56 Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
-rwxr-xr-xcps-dependencies/pom.xml8
-rw-r--r--cps-parent/pom.xml20
-rw-r--r--cps-ri/pom.xml11
-rw-r--r--cps-ri/src/test/java/org/onap/cps/DatabaseTestContainer.java64
-rw-r--r--cps-ri/src/test/java/org/onap/cps/TestApplication.java30
-rw-r--r--cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java141
-rw-r--r--cps-ri/src/test/resources/application.yml13
-rw-r--r--cps-ri/src/test/resources/data/clear-all.sql5
-rw-r--r--cps-ri/src/test/resources/data/schemaset.sql14
9 files changed, 305 insertions, 1 deletions
diff --git a/cps-dependencies/pom.xml b/cps-dependencies/pom.xml
index 9e0269b3a2..3e5df3f95b 100755
--- a/cps-dependencies/pom.xml
+++ b/cps-dependencies/pom.xml
@@ -27,6 +27,7 @@
<springfox.version>3.0.0</springfox.version>
<stagingNexusPath>/content/repositories/staging/</stagingNexusPath>
<swagger.version>2.1.4</swagger.version>
+ <testcontainers.version>1.15.0</testcontainers.version>
<yangtools.version>5.0.6</yangtools.version>
</properties>
@@ -99,6 +100,11 @@
<artifactId>modelmapper</artifactId>
<version>${modelmapper.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>postgresql</artifactId>
+ <version>${testcontainers.version}</version>
+ </dependency>
</dependencies>
</dependencyManagement>
-</project> \ No newline at end of file
+</project>
diff --git a/cps-parent/pom.xml b/cps-parent/pom.xml
index b7b64af3b6..0071360504 100644
--- a/cps-parent/pom.xml
+++ b/cps-parent/pom.xml
@@ -96,6 +96,26 @@
</pluginManagement>
</build>
</profile>
+ <profile>
+ <id>skip-db-tests</id>
+ <activation>
+ <!-- exclude module tests by default till Jenkins is configured to support test containers -->
+ <activeByDefault>true</activeByDefault>
+ </activation>
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <excludes>
+ <exclude>**/spi/impl/*Test.java</exclude>
+ </excludes>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
+ </profile>
</profiles>
<dependencyManagement>
diff --git a/cps-ri/pom.xml b/cps-ri/pom.xml
index 2c28212afa..b5fe933818 100644
--- a/cps-ri/pom.xml
+++ b/cps-ri/pom.xml
@@ -36,5 +36,16 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
+ <!-- Test dependencies -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-test</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.testcontainers</groupId>
+ <artifactId>postgresql</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/cps-ri/src/test/java/org/onap/cps/DatabaseTestContainer.java b/cps-ri/src/test/java/org/onap/cps/DatabaseTestContainer.java
new file mode 100644
index 0000000000..759cc95319
--- /dev/null
+++ b/cps-ri/src/test/java/org/onap/cps/DatabaseTestContainer.java
@@ -0,0 +1,64 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Pantheon.tech
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps;
+
+import org.testcontainers.containers.PostgreSQLContainer;
+
+/**
+ * The Postgresql database test container wrapper.
+ * Singleton implementation allows saving time on database initialization which
+ * otherwise would occur on each test.
+ */
+public class DatabaseTestContainer extends PostgreSQLContainer<DatabaseTestContainer> {
+ private static final String IMAGE_VERSION = "postgres:11.1";
+ private static DatabaseTestContainer databaseTestContainer;
+
+ private DatabaseTestContainer() {
+ super(IMAGE_VERSION);
+ }
+
+ /**
+ * Provides an instance of test container wrapper.
+ * The returned value expected to be assigned to static variable annotated with @ClassRule.
+ * This will allow to initialize DB connection env variables before DataSource object
+ * is initialized by Spring framework.
+ *
+ */
+ public static DatabaseTestContainer getInstance() {
+ if (databaseTestContainer == null) {
+ databaseTestContainer = new DatabaseTestContainer();
+ }
+ return databaseTestContainer;
+ }
+
+ @Override
+ public void start() {
+ super.start();
+ System.setProperty("DB_URL", databaseTestContainer.getJdbcUrl());
+ System.setProperty("DB_USERNAME", databaseTestContainer.getUsername());
+ System.setProperty("DB_PASSWORD", databaseTestContainer.getPassword());
+ }
+
+ @Override
+ public void stop() {
+ //do nothing, JVM handles shut down
+ }
+
+}
diff --git a/cps-ri/src/test/java/org/onap/cps/TestApplication.java b/cps-ri/src/test/java/org/onap/cps/TestApplication.java
new file mode 100644
index 0000000000..5e0e3679ee
--- /dev/null
+++ b/cps-ri/src/test/java/org/onap/cps/TestApplication.java
@@ -0,0 +1,30 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Pantheon.tech
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps;
+
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+/**
+ * The @SpringBootApplication annotated class is required in order to run tests
+ * marked with @SpringBootTest annotation.
+ */
+@SpringBootApplication
+public class TestApplication {
+}
diff --git a/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java b/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
new file mode 100644
index 0000000000..4fc576ab64
--- /dev/null
+++ b/cps-ri/src/test/java/org/onap/cps/spi/impl/CpsModulePersistenceServiceTest.java
@@ -0,0 +1,141 @@
+/*
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 2020 Pantheon.tech
+ * ================================================================================
+ * 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.
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.cps.spi.impl;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import com.google.common.collect.ImmutableSet;
+import java.util.Set;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.onap.cps.DatabaseTestContainer;
+import org.onap.cps.exceptions.CpsValidationException;
+import org.onap.cps.spi.CpsModulePersistenceService;
+import org.onap.cps.spi.entities.Dataspace;
+import org.onap.cps.spi.entities.SchemaSet;
+import org.onap.cps.spi.entities.YangResource;
+import org.onap.cps.spi.repository.DataspaceRepository;
+import org.onap.cps.spi.repository.SchemaSetRepository;
+import org.onap.cps.spi.repository.YangResourceRepository;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.test.context.jdbc.Sql;
+import org.springframework.test.context.jdbc.SqlGroup;
+import org.springframework.test.context.junit4.SpringRunner;
+
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class CpsModulePersistenceServiceTest {
+
+ private static final String CLEAR_DATA = "/data/clear-all.sql";
+ private static final String SET_DATA = "/data/schemaset.sql";
+
+ private static final String DATASPACE_NAME = "DATASPACE-001";
+ private static final String DATASPACE_NAME_INVALID = "DATASPACE-X";
+ private static final String SCHEMA_SET_NAME = "SCHEMA-SET-001";
+ private static final String SCHEMA_SET_NAME_NEW = "SCHEMA-SET-NEW";
+ private static final String OLD_RESOURCE_CONTENT = "CONTENT-001";
+ private static final String OLD_RESOURCE_CHECKSUM = "877e65a9f36d54e7702c3f073f6bc42b";
+ private static final String NEW_RESOURCE_CONTENT = "CONTENT-NEW";
+ private static final String NEW_RESOURCE_CHECKSUM = "c94d40a1350eb1c0b1c1949eac84fc59";
+ private static final Long NEW_RESOURCE_ABSTRACT_ID = 0L;
+
+ @ClassRule
+ public static DatabaseTestContainer testContainer = DatabaseTestContainer.getInstance();
+
+ @Autowired
+ private CpsModulePersistenceService cpsModulePersistenceService;
+
+ @Autowired
+ private DataspaceRepository dataspaceRepository;
+
+ @Autowired
+ private YangResourceRepository yangResourceRepository;
+
+ @Autowired
+ private SchemaSetRepository schemaSetRepository;
+
+
+ @Test(expected = CpsValidationException.class)
+ @Sql(CLEAR_DATA)
+ public void testStoreSchemaSetToInvalidDataspace() {
+ cpsModulePersistenceService
+ .storeSchemaSet(DATASPACE_NAME_INVALID, SCHEMA_SET_NAME_NEW, toSet(NEW_RESOURCE_CONTENT));
+ }
+
+ @Test(expected = CpsValidationException.class)
+ @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+ public void testStoreDuplicateSchemaSet() {
+ cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME, toSet(NEW_RESOURCE_CONTENT));
+ }
+
+ @Test
+ @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+ public void testStoreSchemaSetWithNewYangResource() {
+ cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, toSet(NEW_RESOURCE_CONTENT));
+ assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
+ NEW_RESOURCE_ABSTRACT_ID, NEW_RESOURCE_CONTENT, NEW_RESOURCE_CHECKSUM);
+ }
+
+ @Test
+ @SqlGroup({@Sql(CLEAR_DATA), @Sql(SET_DATA)})
+ public void testStoreSchemaSetWithExistingYangResourceReuse() {
+ cpsModulePersistenceService.storeSchemaSet(DATASPACE_NAME, SCHEMA_SET_NAME_NEW, toSet(OLD_RESOURCE_CONTENT));
+ assertSchemaSetPersisted(DATASPACE_NAME, SCHEMA_SET_NAME_NEW,
+ 3001L, OLD_RESOURCE_CONTENT, OLD_RESOURCE_CHECKSUM);
+ }
+
+ private void assertSchemaSetPersisted(final String expectedDataspaceName, final String expectedSchemaSetName,
+ final Long expectedYangResourceId, final String expectedYangResourceContent,
+ final String expectedYangResourceChecksum) {
+
+ // assert the schema set is persisted
+ final SchemaSet schemaSet = getSchemaSetFromDatabase(expectedDataspaceName, expectedSchemaSetName);
+ assertEquals(expectedDataspaceName, schemaSet.getDataspace().getName());
+ assertEquals(expectedSchemaSetName, schemaSet.getName());
+
+ // assert the attached yang resource is persisted
+ final Set<YangResource> yangResources = schemaSet.getYangResources();
+ assertNotNull(yangResources);
+ assertEquals(1, yangResources.size());
+
+ // assert the attached yang resource content
+ final YangResource yangResource = yangResources.iterator().next();
+ assertNotNull(yangResource.getId());
+ if (expectedYangResourceId != NEW_RESOURCE_ABSTRACT_ID) {
+ // existing resource with known id
+ assertEquals(expectedYangResourceId, yangResource.getId());
+ }
+ assertEquals(expectedYangResourceContent, yangResource.getContent());
+ assertEquals(expectedYangResourceChecksum, yangResource.getChecksum());
+ }
+
+ private static Set<String> toSet(final String... elements) {
+ return ImmutableSet.<String>builder().add(elements).build();
+ }
+
+ private SchemaSet getSchemaSetFromDatabase(final String dataspaceName, final String schemaSetName) {
+ final Dataspace dataspace = dataspaceRepository.findByName(dataspaceName).orElseThrow();
+ return schemaSetRepository.findByDataspaceAndName(dataspace, schemaSetName).orElseThrow();
+ }
+}
diff --git a/cps-ri/src/test/resources/application.yml b/cps-ri/src/test/resources/application.yml
new file mode 100644
index 0000000000..f6a5dfb0df
--- /dev/null
+++ b/cps-ri/src/test/resources/application.yml
@@ -0,0 +1,13 @@
+spring:
+ jpa:
+ ddl-auto: create
+ properties:
+ hibernate:
+ enable_lazy_load_no_trans: true
+ dialect: org.hibernate.dialect.PostgreSQLDialect
+ datasource:
+ url: ${DB_URL}
+ username: ${DB_USERNAME}
+ password: ${DB_PASSWORD}
+ driverClassName: org.postgresql.Driver
+ initialization-mode: always
diff --git a/cps-ri/src/test/resources/data/clear-all.sql b/cps-ri/src/test/resources/data/clear-all.sql
new file mode 100644
index 0000000000..3d93825ba1
--- /dev/null
+++ b/cps-ri/src/test/resources/data/clear-all.sql
@@ -0,0 +1,5 @@
+-- clear all via dataspace table cleanup
+-- all other data will be removed by cascade
+DELETE FROM DATASPACE;
+-- explicit clear
+DELETE FROM YANG_RESOURCE;
diff --git a/cps-ri/src/test/resources/data/schemaset.sql b/cps-ri/src/test/resources/data/schemaset.sql
new file mode 100644
index 0000000000..23788c3c00
--- /dev/null
+++ b/cps-ri/src/test/resources/data/schemaset.sql
@@ -0,0 +1,14 @@
+INSERT INTO DATASPACE (ID, NAME) VALUES
+ (1001, 'DATASPACE-001'), (1002, 'DATASPACE-002');
+
+INSERT INTO SCHEMA_SET (ID, NAME, DATASPACE_ID) VALUES
+ (2001, 'SCHEMA-SET-001', 1001), (2002, 'SCHEMA-SET-002', 1001);
+
+INSERT INTO YANG_RESOURCE (ID, CONTENT, CHECKSUM) VALUES
+ (3001, 'CONTENT-001', '877e65a9f36d54e7702c3f073f6bc42b'),
+ (3002, 'CONTENT-002', '88892586b1f23fe8c1595759784a18f8'),
+ (3003, 'CONTENT-003', 'fc5740499a09a48e0c95d6fc45d4bde8'),
+ (3004, 'CONTENT-004', '3801280fe532f5cbf535695cf6122026');
+
+INSERT INTO SCHEMA_SET_YANG_RESOURCES (SCHEMA_SET_ID, YANG_RESOURCE_ID) VALUES
+ (2001, 3001), (2001, 3002), (2002, 3003), (2002, 3004);