Skip to content

Commit

Permalink
new spring boot, query count test fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
gtiwari333 committed Sep 22, 2024
1 parent 43161a7 commit ded60bc
Show file tree
Hide file tree
Showing 13 changed files with 442 additions and 508 deletions.
21 changes: 19 additions & 2 deletions .mvn/wrapper/maven-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.8.2/apache-maven-3.8.2-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.6/maven-wrapper-0.5.6.jar
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
wrapperVersion=3.3.2
distributionType=only-script
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip
28 changes: 14 additions & 14 deletions config/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,24 @@ services:
activemq_artemis:
# its not supported in M1 Mac, workaround is to enable Rosetta in Docker
# Docker settings → Features in development → check ☑ Use Rosetta for x86/amd64 emulation on Apple Silicon, and then restart Docker.
image: 'apache/activemq-artemis:2.31.2-alpine'
image: 'apache/activemq-artemis:2.37.0'
container_name: activemqArtemis
environment:
- ARTEMIS_USER=admin
- ARTEMIS_PASSWORD=admin
- "ARTEMIS_USER=admin"
- "ARTEMIS_PASSWORD=admin"
ports:
- 8161:8161 # use this to access from browser
- 61616:61616
networks:
- seedappnet
mysql:
image: 'mysql:8.0.35'
image: 'mysql'
environment:
- MYSQL_ROOT_PASSWORD=password
- MYSQL_DATABASE=seedapp
- "MYSQL_ROOT_PASSWORD=password"
- "MYSQL_DATABASE=seedapp"
ports:
- 3306:3306
command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8mb4 --explicit_defaults_for_timestamp
command: mysqld --lower_case_table_names=1 --character_set_server=utf8mb4 --explicit_defaults_for_timestamp
networks:
- seedappnet
emailhog:
Expand All @@ -39,16 +39,16 @@ services:
networks:
- seedappnet
keycloak:
image: 'quay.io/keycloak/keycloak:23.0.3'
image: 'quay.io/keycloak/keycloak:25.0.6'
container_name: keycloak
command: start-dev --import-realm
environment:
- KEYCLOAK_DB=dev-file
- KEYCLOAK_ADMIN=admin
- KEYCLOAK_ADMIN_PASSWORD=admin
- KEYCLOAK_FEATURES=scripts
- KEYCLOAK_HTTP_PORT=8080
- KEYCLOAK_HTTPS_PORT=9443
- "KEYCLOAK_DB=dev-file"
- "KEYCLOAK_ADMIN=admin"
- "KEYCLOAK_ADMIN_PASSWORD=admin"
- "KEYCLOAK_FEATURES=scripts"
- "KEYCLOAK_HTTP_PORT=8080"
- "KEYCLOAK_HTTPS_PORT=9443"
# entrypoint: /tmp/keycloak/config/docker-compose-entrypoint.sh --hostname host.docker.internal:8080
volumes:
- ../main-app/main-webapp/src/main/resources/keycloak/:/opt/keycloak/data/import
Expand Down
4 changes: 1 addition & 3 deletions main-app/main-webapp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@
<artifactId>jquery</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<groupId>org.webjars.npm</groupId>
<artifactId>bootstrap</artifactId>
</dependency>
<dependency>
Expand Down Expand Up @@ -237,12 +237,10 @@
<dependency>
<groupId>com.github.dasniko</groupId>
<artifactId>testcontainers-keycloak</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ class DockerContainerConfig {

String userPwd = "admin";//use same for all

var mysql = new MySQLContainer<>("mysql:8.0.35").withDatabaseName("seedapp").withUsername(userPwd).withPassword(userPwd);
var mysql = new MySQLContainer<>("mysql").withDatabaseName("seedapp").withUsername(userPwd).withPassword(userPwd);
mysql.start();

var activeMQ = new GenericContainer<>("apache/activemq-artemis:2.31.2-alpine");
var activeMQ = new GenericContainer<>("apache/activemq-artemis:2.37.0");
activeMQ.setEnv(List.of("ARTEMIS_USER=admin", "ARTEMIS_PASSWORD=admin"));
activeMQ.withExposedPorts(61616);
activeMQ.start(); //using default ports

var kc = new KeycloakContainer("quay.io/keycloak/keycloak:23.0.3").withRealmImportFile("keycloak/realm-export.json");
var kc = new KeycloakContainer("quay.io/keycloak/keycloak:25.0.6").withRealmImportFile("keycloak/realm-export.json");
kc.start();

setProperty("KEYCLOAK_PORT", Integer.toString(kc.getHttpPort()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

<div th:fragment="js-imports(scripts)">
<script src="/webjars/jquery/3.6.1/jquery.min.js"></script>
<script src="/webjars/bootstrap/5.0.1/js/bootstrap.min.js"></script>
<script src="/webjars/bootstrap/5.0.1/dist/js/bootstrap.min.js"></script>

<script src="https://cdn.jsdelivr.net/npm/@stomp/[email protected]/bundles/stomp.umd.min.js" integrity="sha512-vVSeibxEBKAfSkPFQ6vluowRjL1BsFLHz0yYyIq60UGN/aOtm4n5u5jHHaLtvc8pgkNAYsRkwvpkgEcBcefaPQ==" crossorigin="anonymous"></script>
<script src="/webjars/jquery-toast-plugin/1.3.2/dist/jquery.toast.min.js"></script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<meta content="" name="generator">
<title th:replace="${title}"> Title ...</title>

<link href="/webjars/bootstrap/5.0.1/css/bootstrap.css" rel="stylesheet">
<link href="/webjars/bootstrap/5.0.1/dist/css/bootstrap.css" rel="stylesheet">
<link href="/webjars/jquery-toast-plugin/1.3.2/dist/jquery.toast.min.css" rel="stylesheet">
<link rel="stylesheet" th:href="@{/static/css/wro-commons.css}"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:th="http://www.thymeleaf.org">

<head>
<link href="/webjars/bootstrap/5.0.1/css/bootstrap.css" rel="stylesheet">
<link href="/webjars/bootstrap/5.0.1/dist/css/bootstrap.css" rel="stylesheet">
<link href="/wro4j/commons.css" rel="stylesheet">
<title></title>
</head>
Expand Down
46 changes: 26 additions & 20 deletions main-app/main-webapp/src/test/java/gt/app/e2e/PublicPageIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import gt.app.config.AppProperties;
import gt.app.frwk.TestDataManager;
import io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator;
import net.ttddyy.dsproxy.QueryCountHolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -14,7 +14,7 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import static io.hypersistence.utils.jdbc.validator.SQLStatementCountValidator.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
Expand All @@ -31,18 +31,20 @@ class PublicPageIT {
@BeforeEach
void cleanDB() {
testDataManager.cleanDataAndCache();
SQLStatementCountValidator.reset();

}

@Test
void loadIndexPageAndVerifyResultIsCached(@Autowired MockMvc mvc) throws Exception {
SQLStatementCountValidator.reset();
//gt.app.DataCreator creates test data
long selectCount = QueryCountHolder.getGrandTotal().getSelect();
long deleteCount = QueryCountHolder.getGrandTotal().getDelete();
long insertCount = QueryCountHolder.getGrandTotal().getInsert();
long updateCount = QueryCountHolder.getGrandTotal().getUpdate();

MvcResult result = mvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML_VALUE))
.andReturn();
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML_VALUE))
.andReturn();

String content = result.getResponse().getContentAsString();
assertTrue(content.contains("Article App - HOME"));
Expand All @@ -54,20 +56,24 @@ void loadIndexPageAndVerifyResultIsCached(@Autowired MockMvc mvc) throws Excepti
mvc.perform(get("/")).andExpect(status().isOk());

//only one select
// assertSelectCount(1);
// assertDeleteCount(0);
// assertInsertCount(0);
// assertUpdateCount(0);
assertEquals(selectCount + 1, QueryCountHolder.getGrandTotal().getSelect());
assertEquals(deleteCount, QueryCountHolder.getGrandTotal().getDelete());
assertEquals(insertCount, QueryCountHolder.getGrandTotal().getInsert());
assertEquals(updateCount, QueryCountHolder.getGrandTotal().getUpdate());
}

@Test
void testCacheAndDBBothAreResetBetweenTests(@Autowired MockMvc mvc) throws Exception {
SQLStatementCountValidator.reset();
//gt.app.DataCreator creates test data
long selectCount = QueryCountHolder.getGrandTotal().getSelect();
long deleteCount = QueryCountHolder.getGrandTotal().getDelete();
long insertCount = QueryCountHolder.getGrandTotal().getInsert();
long updateCount = QueryCountHolder.getGrandTotal().getUpdate();

MvcResult result = mvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML_VALUE))
.andReturn();
.andExpect(status().isOk())
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_HTML_VALUE))
.andReturn();

String content = result.getResponse().getContentAsString();
assertTrue(content.contains("Article App - HOME"));
Expand All @@ -79,9 +85,9 @@ void testCacheAndDBBothAreResetBetweenTests(@Autowired MockMvc mvc) throws Excep
mvc.perform(get("/")).andExpect(status().isOk());

//only one select
// assertSelectCount(1);
// assertDeleteCount(0);
// assertInsertCount(0);
// assertUpdateCount(0);
assertEquals(selectCount + 1, QueryCountHolder.getGrandTotal().getSelect());
assertEquals(deleteCount, QueryCountHolder.getGrandTotal().getDelete());
assertEquals(insertCount, QueryCountHolder.getGrandTotal().getInsert());
assertEquals(updateCount, QueryCountHolder.getGrandTotal().getUpdate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ public class TestContainerConfig {
static {
log.info("Starting docker containers using TestContainers");

var activeMQ = new GenericContainer<>("apache/activemq-artemis:2.31.2-alpine");
var activeMQ = new GenericContainer<>("apache/activemq-artemis:2.37.0");
activeMQ.withExposedPorts(61616);
activeMQ.setEnv(List.of("ARTEMIS_USER=admin", "ARTEMIS_PASSWORD=admin"));

activeMQ.start(); //using default ports

var kc = new KeycloakContainer("quay.io/keycloak/keycloak:23.0.3").withRealmImportFile("keycloak/realm-export.json");
var kc = new KeycloakContainer("quay.io/keycloak/keycloak:25.0.6").withRealmImportFile("keycloak/realm-export.json");
kc.start();

setProperty("KEYCLOAK_PORT", Integer.toString(kc.getHttpPort()));
Expand Down
3 changes: 2 additions & 1 deletion main-app/main-webapp/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ spring:
datasource:
url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MYSQL
jpa:
show-sql: false
show-sql: true
open-in-view: false
hibernate:
ddl-auto: none # we use liquibase
Expand Down Expand Up @@ -73,6 +73,7 @@ decorator:
datasource:
enabled: true
datasource-proxy:
count-query: true
slow-query:
threshold: 1
query:
Expand Down
Loading

0 comments on commit ded60bc

Please sign in to comment.