Skip to content

Commit

Permalink
Add Testcontainer MySQL🫸🌀✏️📗 :octocat:🐧🐳⬆
Browse files Browse the repository at this point in the history
  • Loading branch information
hendisantika committed Jun 25, 2024
1 parent d2e4443 commit 441c930
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@
package com.hendisantika.adminlte;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.shaded.com.fasterxml.jackson.core.JsonProcessingException;

@SpringBootTest
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

@Testcontainers
@SpringBootTest(
properties = {
"management.endpoint.health.show-details=always",
"spring.datasource.url=jdbc:tc:mysql:8.4.0:///test"
},
webEnvironment = RANDOM_PORT
)
public class SistemApplicationTests {

@Autowired
private TestRestTemplate restTemplate;

@Test
public void contextLoads() {
}
@DisplayName("Database status will be UP and Database name should be MySQL")
void databaseIsAvailable() throws JsonProcessingException, com.fasterxml.jackson.core.JsonProcessingException {
var response = restTemplate.getForEntity("/actuator/health", String.class);

assertThat(response.getBody()).isNotNull();

JsonNode root = new ObjectMapper().readTree(response.getBody());
JsonNode dbComponentNode = root.get("components").get("db");

String dbStatus = dbComponentNode.get("status").asText();
String dbName = dbComponentNode.get("details").get("database").asText();

assertThat(dbStatus).isEqualTo("UP");
assertThat(dbName).isEqualTo("MySQL");
}
}
44 changes: 44 additions & 0 deletions src/test/resources/migration/V1__Init_Table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
CREATE TABLE users
(
username VARCHAR(50) NOT NULL PRIMARY KEY,
password VARCHAR(255) NOT NULL,
enabled boolean NOT NULL
) ENGINE = InnoDb;
CREATE TABLE authorities
(
username VARCHAR(50) NOT NULL,
authority VARCHAR(50) NOT NULL,
FOREIGN KEY (username) REFERENCES users (username),
UNIQUE INDEX authorities_idx_1 (username, authority)
) ENGINE = InnoDb;

CREATE TABLE customers
(
id bigint(50) NOT NULL PRIMARY KEY AUTO_INCREMENT,
firstname VARCHAR(50) NOT NULL,
lastname VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE,
added_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
) ENGINE = InnoDb;

INSERT INTO customers (id, firstname, lastname, email, added_date)
VALUES (1, 'Uchiha', 'Sasuke', '[email protected]', '2020-03-29 07:52:34'),
(2, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(3, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(4, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(5, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(6, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(7, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(8, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(9, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(10, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(11, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(12, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(13, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(14, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(15, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(16, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(17, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(18, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(19, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59'),
(20, 'Uzumaki', 'Naruto', '[email protected]', '2020-03-29 08:18:59');

0 comments on commit 441c930

Please sign in to comment.