Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Due to  ports exposed for a test containers (part of DB_URL) are variable (to allow concurrent tests executions and to avoid conflicts with
existing containers) the expected variables are set after the test container being initialized directly from TestContainer wrapper artifact

...

See https://docs.spring.io/spring-framework/docs/current/reference/html/testing.html#testcontext-executing-sql-declaratively

Test class

...

template

Below is the an example of test class (setup ) which can be used as a template /reference for new database tests being added to CPS project.

Code Block
languagejava
package org.onap.cps.spi.impl;

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.cps.DatabaseTestContainer;
import org.onap.cps.spi.CpsAdminPersistenceService;
import org.onap.cps.spi.repository.DataspaceRepository;
import org.onap.cps.spi.repository.SchemaSetRepository;
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.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class CpsAdminPersistenceServiceTest {

    private static final String CLEAR_DATA = "/data/clear-all.sql";
    private static final String SET_DATA = "/data/anchor.sql";

    @ClassRule
    public static DatabaseTestContainer databaseTestContainer = DatabaseTestContainer.getInstance();

    @Autowired
    private CpsAdminPersistenceService cpsAdminPersistenceService;

    @Autowired
    private DataspaceRepository dataspaceRepository;

    @Autowired
    private SchemaSetRepository schemaSetRepository;

    @Test
    @Sql({CLEAR_DATA, SET_DATA})
    public void testCreateAnchor() {
        // test ...
    }
}


Resources

Frameworks and libraries:

...