1 package org.apache.turbine.testcontainer;
2
3 import static junit.framework.TestCase.assertEquals;
4 import static junit.framework.TestCase.assertFalse;
5 import static junit.framework.TestCase.assertTrue;
6
7 import java.io.File;
8 import java.nio.file.FileSystems;
9 import java.nio.file.Path;
10 import java.nio.file.Paths;
11 import java.sql.Connection;
12 import java.sql.DriverManager;
13 import java.sql.PreparedStatement;
14 import java.sql.ResultSet;
15 import java.sql.SQLException;
16
17 import org.apache.logging.log4j.LogManager;
18 import org.apache.logging.log4j.Logger;
19 import org.junit.jupiter.api.Assertions;
20 import org.junit.jupiter.api.BeforeAll;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.MethodOrderer.OrderAnnotation;
23 import org.junit.jupiter.api.Order;
24 import org.junit.jupiter.api.Tag;
25 import org.junit.jupiter.api.Test;
26 import org.junit.jupiter.api.TestMethodOrder;
27 import org.testcontainers.containers.GenericContainer;
28 import org.testcontainers.images.builder.ImageFromDockerfile;
29 import org.testcontainers.junit.jupiter.Container;
30 import org.testcontainers.junit.jupiter.Testcontainers;
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 @TestMethodOrder(OrderAnnotation.class)
72 @Testcontainers
73 @Tag("docker")
74 class BuildContainerWithDockerfileTest {
75
76 public static final Path RESOURCE_PATH =
77 FileSystems.getDefault().getPath(".").resolve("conf/docker-resources/db/");
78
79 private static Logger log = LogManager.getLogger();
80
81 public static int SERVICE_PORT = 3306;
82
83 public static String DATABASE_NAME = "default";
84
85 Connection connection;
86
87 @Container
88 public static GenericContainer MY_SQL_CONTAINER = new GenericContainer<>(
89 new ImageFromDockerfile()
90 .withFileFromPath(".", RESOURCE_PATH)
91 ).withExposedPorts( SERVICE_PORT )
92 .withEnv( "MYSQL_DATABASE", DATABASE_NAME )
93 .withEnv( "MYSQL_USER", "userdb" )
94 .withEnv( "MYSQL_PASSWORD", "test1234" )
95 .withEnv( "MYSQL_ROOT_PASSWORD","test1234" );
96
97
98
99
100
101
102
103 @BeforeAll
104 public static void init() {
105
106 MY_SQL_CONTAINER.setStartupAttempts( 3 );
107 }
108
109 @BeforeEach
110 public void before() throws Exception {
111 connection = getConnection();
112 }
113
114 @Test
115 @Order(2)
116 void createUser() throws SQLException {
117 if (connection == null) return;
118 try (PreparedStatement preparedStatement =
119 connection.prepareStatement(
120 "INSERT INTO TURBINE_USER (USER_ID,LOGIN_NAME,PASSWORD_VALUE,FIRST_NAME,LAST_NAME) values (?,?,?,?,?)")) {
121 preparedStatement.setString(1, "4");
122 preparedStatement.setString(2, "kzipfel");
123 preparedStatement.setString(3, "kzipfel");
124 preparedStatement.setString(4, "Konrad");
125 preparedStatement.setString(5, "Zipfel");
126 assertFalse(preparedStatement.execute());
127 Assertions.assertEquals(1, preparedStatement.getUpdateCount());
128 }
129 }
130
131 @Test
132 @Order(1)
133 void selectExistingUser() throws SQLException {
134 if (connection == null) return;
135 try (PreparedStatement preparedStatement =
136 connection.prepareStatement(
137 "select USER_ID, LAST_NAME, FIRST_NAME from TURBINE_USER where USER_ID=?")) {
138 preparedStatement.setString(1, "1");
139 ResultSet resultSet = preparedStatement.executeQuery();
140 assertTrue(resultSet.next());
141 assertEquals("Admin", resultSet.getString("LAST_NAME"));
142 assertEquals("", resultSet.getString("FIRST_NAME"));
143 resultSet.close();
144 }
145 }
146
147 @Test
148 @Order(3)
149 void selectNewUser() throws SQLException {
150 if (connection == null) return;
151 try (PreparedStatement preparedStatement =
152 connection.prepareStatement(
153 "select USER_ID, LAST_NAME, FIRST_NAME from TURBINE_USER where USER_ID=?")) {
154 preparedStatement.setString(1, "4");
155 ResultSet resultSet = preparedStatement.executeQuery();
156 assertTrue(resultSet.next());
157 assertEquals("Zipfel", resultSet.getString("LAST_NAME"));
158 assertEquals("Konrad", resultSet.getString("FIRST_NAME"));
159 }
160 }
161
162 public static Connection getConnection() throws SQLException {
163 String jdbcStr = generateJdbcUrl();
164 if (jdbcStr == null) {
165 return null;
166 }
167 return DriverManager
168 .getConnection(jdbcStr, "userdb", "test1234");
169 }
170
171
172
173
174
175 public static String generateJdbcUrl() {
176 if (MY_SQL_CONTAINER == null) { return null; }
177 if (!MY_SQL_CONTAINER.isRunning()) {
178 MY_SQL_CONTAINER.start();
179 }
180
181 String serviceHost = MY_SQL_CONTAINER.getContainerIpAddress();
182 Integer mappedPort = MY_SQL_CONTAINER.getMappedPort(SERVICE_PORT);
183 log.info("generate jdbc url from {}, mapped Port: {}, bounded port: {}", serviceHost, mappedPort, MY_SQL_CONTAINER.getBoundPortNumbers());
184
185
186
187
188
189 String targetJDBC =
190 String.format("jdbc:mysql://%s:%d/%s?loggerLevel=OFF", serviceHost,
191 mappedPort, DATABASE_NAME);
192
193
194 log.info( "used connect url: {}", targetJDBC);
195 return targetJDBC;
196 }
197
198 }