1. Project Clover database Wed Jan 17 2024 23:40:18 CST
  2. Package org.europa.together

File JUnit5Preperator.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
16
3
1
83
63
5
0.31
5.33
3
1.67

Classes

Class Line # Actions
JUnit5Preperator 25 16 0% 5 1
0.9523809695.2%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    package org.europa.together;
2   
3    import com.github.dockerjava.api.model.ExposedPort;
4    import com.github.dockerjava.api.model.HostConfig;
5    import com.github.dockerjava.api.model.PortBinding;
6    import com.github.dockerjava.api.model.Ports;
7    import java.sql.SQLException;
8    import org.europa.together.application.JdbcActions;
9    import org.europa.together.application.LogbackLogger;
10    import org.europa.together.business.DatabaseActions;
11    import org.europa.together.business.Logger;
12    import org.europa.together.domain.LogLevel;
13    import org.junit.ClassRule;
14    import org.junit.jupiter.api.extension.BeforeAllCallback;
15    import org.junit.jupiter.api.extension.ExtensionContext;
16    import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.GLOBAL;
17    import org.testcontainers.containers.PostgreSQLContainer;
18    import org.testcontainers.utility.DockerImageName;
19   
20    /**
21    * JUnit5 Extension to run code beforr all test, like setup test suite and
22    * shutdown after all the testenvironment.<br>
23    * <b>USAGE: </b> @ExtendWith({JUnit5DbPreperator.class})
24    */
 
25    public class JUnit5Preperator implements BeforeAllCallback, ExtensionContext.Store.CloseableResource {
26   
27    private static final Logger LOGGER = new LogbackLogger(JUnit5Preperator.class);
28    private static boolean started = false;
29   
30    @ClassRule
31    private PostgreSQLContainer<?> postgreSQLContainer;
32   
33    public static final String JDBC_CONNECTION_STRING = "jdbc:tc:postgresql:14:///together-test";
34   
 
35  38 toggle public JUnit5Preperator() {
36  38 int containerPort = 5432;
37  38 int localPort = 5432;
38  38 postgreSQLContainer
39    = new PostgreSQLContainer<>(DockerImageName.parse("postgres:14"))
40    .withExposedPorts(containerPort)
41    .withDatabaseName("together-test")
42    .withUsername("together")
43    .withPassword("together")
44    .withCreateContainerCmdModifier(cmd
45    -> cmd.withHostConfig(
46    new HostConfig().withPortBindings(
47    new PortBinding(Ports.Binding.bindPort(localPort),
48    new ExposedPort(containerPort))
49    )
50    ));
51    }
52   
 
53  38 toggle @Override
54    public void beforeAll(ExtensionContext context) throws InterruptedException {
55   
56  38 if (!started) {
57  1 LOGGER.log("TEST ENVIRONMENT will be prepared.", LogLevel.DEBUG);
58   
59  1 postgreSQLContainer.start();
60    // check if the JDBC Connection is well established
61  1 DatabaseActions jdbcConection = new JdbcActions();
62  1 try {
63  1 jdbcConection.connect("test");
64  1 LOGGER.log("JDBC: " + jdbcConection.getJdbcMetaData().toString(), LogLevel.DEBUG);
65   
66  1 started = true;
67  1 context.getRoot().getStore(GLOBAL).put("TogetherPlatform", this);
68   
69    } catch (SQLException ex) {
70  0 LOGGER.catchException(ex);
71    }
72  1 LOGGER.log("TEST ENVIRONMENT prepared...\n\n", LogLevel.DEBUG);
73    }
74    }
75   
 
76  1 toggle @Override
77    public void close() {
78  1 LOGGER.log("TEST ENVIRONMENT will be shut down.", LogLevel.DEBUG);
79   
80  1 postgreSQLContainer.stop();
81    }
82   
83    }