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