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