From e2dcb1e9fe6246e44ea302d652ae13ee2b622fc8 Mon Sep 17 00:00:00 2001 From: tschuehly Date: Sat, 16 Mar 2024 20:23:05 +0100 Subject: [PATCH] test fixtures for thymeleaf --- .../core/ThymeleafIntegrationTestBase.kt | 56 +++++ .../KteIntegrationTest.kt | 1 - .../thymeleaf-java-example/build.gradle.kts | 3 + .../web/action/ActionViewComponent.html | 3 +- .../web/layout/LayoutViewComponent.html | 7 +- .../ThymeleafJavaIntegrationTest.java | 206 +----------------- .../thymeleaf-kotlin-example/build.gradle.kts | 3 + .../settings.gradle.kts | 30 +-- .../ThymeleafKotlinIntegrationTest.kt | 191 +--------------- 9 files changed, 89 insertions(+), 411 deletions(-) create mode 100644 core/src/testFixtures/kotlin/de/tschuehly/spring/viewcomponent/core/ThymeleafIntegrationTestBase.kt diff --git a/core/src/testFixtures/kotlin/de/tschuehly/spring/viewcomponent/core/ThymeleafIntegrationTestBase.kt b/core/src/testFixtures/kotlin/de/tschuehly/spring/viewcomponent/core/ThymeleafIntegrationTestBase.kt new file mode 100644 index 0000000..d7456b3 --- /dev/null +++ b/core/src/testFixtures/kotlin/de/tschuehly/spring/viewcomponent/core/ThymeleafIntegrationTestBase.kt @@ -0,0 +1,56 @@ +package de.tschuehly.spring.viewcomponent.core + +import org.junit.jupiter.api.Test + +abstract class ThymeleafIntegrationTestBase: IntegrationTestBase() { + @Test + override fun testNestedActionComponent() { + val expectedHtml = + //language=html + """ + + + +
+ + + + + +

ViewAction Get CountUp

+ +

0

+

ViewAction Post AddItem

+
+ +
+ + + + + +
ItemAction
+

ViewAction Put/Patch Person Form

+
+ + + + + + + +
+ + +
+ + + + """.trimIndent() + assertEndpointReturns("/nested-action", expectedHtml) + } +} \ No newline at end of file diff --git a/examples/kte-example/src/test/kotlin/de/tschuehly/kteviewcomponentexample/KteIntegrationTest.kt b/examples/kte-example/src/test/kotlin/de/tschuehly/kteviewcomponentexample/KteIntegrationTest.kt index 27d0b18..c789781 100644 --- a/examples/kte-example/src/test/kotlin/de/tschuehly/kteviewcomponentexample/KteIntegrationTest.kt +++ b/examples/kte-example/src/test/kotlin/de/tschuehly/kteviewcomponentexample/KteIntegrationTest.kt @@ -1,7 +1,6 @@ package de.tschuehly.kteviewcomponentexample -import de.tschuehly.spring.viewcomponent.core.IntegrationTestBase import de.tschuehly.spring.viewcomponent.core.JteIntegrationTestBase import org.springframework.boot.test.context.SpringBootTest diff --git a/examples/thymeleaf-java-example/build.gradle.kts b/examples/thymeleaf-java-example/build.gradle.kts index d670456..a9663bb 100644 --- a/examples/thymeleaf-java-example/build.gradle.kts +++ b/examples/thymeleaf-java-example/build.gradle.kts @@ -25,6 +25,9 @@ dependencies { implementation("org.webjars.npm:htmx.org:1.9.2") implementation("org.webjars:webjars-locator-core:0.53") testImplementation("org.springframework.boot:spring-boot-starter-test") + + testImplementation("org.springframework.boot:spring-boot-devtools") + testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.7.3-SNAPSHOT")) } tasks.withType { diff --git a/examples/thymeleaf-java-example/src/main/java/de/tschuehly/example/thymeleafjava/web/action/ActionViewComponent.html b/examples/thymeleaf-java-example/src/main/java/de/tschuehly/example/thymeleafjava/web/action/ActionViewComponent.html index cea1015..0fc5800 100644 --- a/examples/thymeleaf-java-example/src/main/java/de/tschuehly/example/thymeleafjava/web/action/ActionViewComponent.html +++ b/examples/thymeleaf-java-example/src/main/java/de/tschuehly/example/thymeleafjava/web/action/ActionViewComponent.html @@ -1,6 +1,5 @@ - + diff --git a/examples/thymeleaf-java-example/src/main/java/de/tschuehly/example/thymeleafjava/web/layout/LayoutViewComponent.html b/examples/thymeleaf-java-example/src/main/java/de/tschuehly/example/thymeleafjava/web/layout/LayoutViewComponent.html index 50cf8ca..84ce4c8 100644 --- a/examples/thymeleaf-java-example/src/main/java/de/tschuehly/example/thymeleafjava/web/layout/LayoutViewComponent.html +++ b/examples/thymeleaf-java-example/src/main/java/de/tschuehly/example/thymeleafjava/web/layout/LayoutViewComponent.html @@ -1,16 +1,15 @@ - +
- +
This is a footer
- \ No newline at end of file diff --git a/examples/thymeleaf-java-example/src/test/java/de/tschuehly/example/thymeleafjava/ThymeleafJavaIntegrationTest.java b/examples/thymeleaf-java-example/src/test/java/de/tschuehly/example/thymeleafjava/ThymeleafJavaIntegrationTest.java index a77051b..d62e0af 100644 --- a/examples/thymeleaf-java-example/src/test/java/de/tschuehly/example/thymeleafjava/ThymeleafJavaIntegrationTest.java +++ b/examples/thymeleaf-java-example/src/test/java/de/tschuehly/example/thymeleafjava/ThymeleafJavaIntegrationTest.java @@ -1,5 +1,6 @@ package de.tschuehly.example.thymeleafjava; +import de.tschuehly.spring.viewcomponent.core.ThymeleafIntegrationTestBase; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -9,206 +10,9 @@ import org.springframework.http.HttpStatus; @SpringBootTest( - webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT ) -class ThymeleafJavaIntegrationTest { +class ThymeleafJavaIntegrationTest extends ThymeleafIntegrationTestBase { @Autowired - private TestRestTemplate testRestTemplate; - - @Test - void testIndexComponent() { - var expectedHtml = - //language=html - """ - -
-

This is the IndexViewComponent

-
- IndexViewComponent
- SimpleViewComponent
- LayoutViewComponent
- ActionViewComponent
- NestedActionViewComponent
- - """.stripIndent(); - assertEndpointReturns("/", expectedHtml); - } - - @Test - void testSimpleComponent() { - //language=html - var expectedHtml = "
\n" + - "

This is the SimpleViewComponent

\n" + - "
Hello World
\n" + - "
"; - assertEndpointReturns("/simple", expectedHtml); - } - - @Test - void testActionComponent() { - var expectedHtml = - //language=HTML - """ - - - - - - -

ViewAction Get CountUp

- - -

0

- -

ViewAction Post AddItem

-
- - -
- - - - - - \s -
ItemAction
- -

ViewAction Put/Patch Person Form

- -
- - - - - -
- - - """; - assertEndpointReturns( - "/action", - expectedHtml - ); - } - - @Test - void testNestedActionComponent() { - var expectedHtml = - //language=html - """ - - - -
- - - - - - -

ViewAction Get CountUp

- - -

0

- -

ViewAction Post AddItem

-
- - -
- - - - - - \s -
ItemAction
- -

ViewAction Put/Patch Person Form

- -
- - - - - -
- - -
-
- This is a footer -
- - - - - """; - assertEndpointReturns("/nested-action", expectedHtml); - } - - @Test - void testLayoutComponent() { - //language=html - var expectedHtml = - """ - - - -
-

This is the SimpleViewComponent

-
Hello World
-
-
- This is a footer -
- - - """.stripIndent(); - assertEndpointReturns("/layout", expectedHtml); - } - - - @Test - void testResourceTemplate() { - var expectedHtml = "Hello World"; - assertEndpointReturns("/resource-template", expectedHtml); - } - - private void assertEndpointReturns(String url, String expectedHtml) { - var response = this.testRestTemplate.exchange( - url, HttpMethod.GET, null, String.class); - Assertions.assertEquals( - HttpStatus.OK, response.getStatusCode() - ); - Assertions.assertNotNull(response.getBody()); - Assertions.assertEquals( - rmWhitespaceBetweenHtmlTags(expectedHtml), - rmWhitespaceBetweenHtmlTags(response.getBody()) - ); - } - - String rmWhitespaceBetweenHtmlTags(String html) { - return html.replaceAll("(?<=>)(\\s*)(?=\\w)", "") - .replaceAll("(?<=\\w)(\\s*)(?=<)", "") - .replaceAll("(?<=>)(\\s*)(?=<)", "") - .replaceAll("\r\n", "") - .trim(); - } -} + TestRestTemplate testRestTemplate; +} \ No newline at end of file diff --git a/examples/thymeleaf-kotlin-example/build.gradle.kts b/examples/thymeleaf-kotlin-example/build.gradle.kts index f2388bf..37694ce 100644 --- a/examples/thymeleaf-kotlin-example/build.gradle.kts +++ b/examples/thymeleaf-kotlin-example/build.gradle.kts @@ -39,6 +39,9 @@ dependencies { implementation("com.fasterxml.jackson.module:jackson-module-kotlin") implementation("org.jetbrains.kotlin:kotlin-reflect") implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") + + testImplementation("org.springframework.boot:spring-boot-devtools") + testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.7.3-SNAPSHOT")) } tasks.withType { diff --git a/examples/thymeleaf-kotlin-example/settings.gradle.kts b/examples/thymeleaf-kotlin-example/settings.gradle.kts index 898a435..8c57761 100644 --- a/examples/thymeleaf-kotlin-example/settings.gradle.kts +++ b/examples/thymeleaf-kotlin-example/settings.gradle.kts @@ -1,17 +1,17 @@ rootProject.name = "thymeleaf-kotlin-example" -//includeBuild("..\\..\\thymeleaf"){ -// dependencySubstitution { -// substitute(module("de.tschuehly:spring-view-component-thymeleaf")).using(project(":")) -// } -//} -//includeBuild("..\\..\\core"){ -// dependencySubstitution { -// substitute(module("de.tschuehly:spring-view-component-core")).using(project(":")) -// } -//} -//includeBuild("..\\..\\jte\\jte-compiler"){ -// dependencySubstitution { -// substitute(module("de.tschuehly:spring-view-component-jte-compiler")).using(project(":")) -// } -//} \ No newline at end of file +includeBuild("..\\..\\thymeleaf"){ + dependencySubstitution { + substitute(module("de.tschuehly:spring-view-component-thymeleaf")).using(project(":")) + } +} +includeBuild("..\\..\\core"){ + dependencySubstitution { + substitute(module("de.tschuehly:spring-view-component-core")).using(project(":")) + } +} +includeBuild("..\\..\\jte\\jte-compiler"){ + dependencySubstitution { + substitute(module("de.tschuehly:spring-view-component-jte-compiler")).using(project(":")) + } +} \ No newline at end of file diff --git a/examples/thymeleaf-kotlin-example/src/test/kotlin/de/tschuehly/example/thymeleafkotlin/ThymeleafKotlinIntegrationTest.kt b/examples/thymeleaf-kotlin-example/src/test/kotlin/de/tschuehly/example/thymeleafkotlin/ThymeleafKotlinIntegrationTest.kt index 46facb3..c7ff3e1 100644 --- a/examples/thymeleaf-kotlin-example/src/test/kotlin/de/tschuehly/example/thymeleafkotlin/ThymeleafKotlinIntegrationTest.kt +++ b/examples/thymeleaf-kotlin-example/src/test/kotlin/de/tschuehly/example/thymeleafkotlin/ThymeleafKotlinIntegrationTest.kt @@ -1,196 +1,11 @@ package de.tschuehly.example.thymeleafkotlin -import org.assertj.core.api.Assertions.assertThat -import org.junit.jupiter.api.Assertions -import org.junit.jupiter.api.Test -import org.springframework.beans.factory.annotation.Autowired +import de.tschuehly.spring.viewcomponent.core.JteIntegrationTestBase +import de.tschuehly.spring.viewcomponent.core.ThymeleafIntegrationTestBase import org.springframework.boot.test.context.SpringBootTest -import org.springframework.boot.test.web.client.TestRestTemplate -import org.springframework.http.HttpMethod -import org.springframework.http.HttpStatus -import org.springframework.http.ResponseEntity @SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT ) -class ThymeleafKotlinIntegrationTest( - @Autowired val testRestTemplate: TestRestTemplate -) { - @Test - fun testIndexComponent() { - val expectedHtml = - //language=html - """ - -
-

This is the IndexViewComponent

-
- IndexViewComponent
- SimpleViewComponent
- LayoutViewComponent
- ActionViewComponent
- NestedActionViewComponent
- - """.trimIndent() - assertEndpointReturns("/", expectedHtml) - } - - @Test - fun testSimpleComponent() { - //language=html - val expectedHtml = """ -
-

This is the SimpleViewComponent

-
Hello World
-
- """.trimIndent() - assertEndpointReturns("/simple", expectedHtml) - } - - @Test - fun testActionComponent() { - val expectedHtml = - //language=html - """ - - - - - - -

ViewAction Get CountUp

- -

0

-

ViewAction Post AddItem

-
- - -
- - - - - -
ItemAction
-

ViewAction Put/Patch Person Form

-
- - - - - -
- - - """.trimIndent() - assertEndpointReturns( - "/action", - expectedHtml - ) - } - - @Test - fun testNestedActionComponent() { - val expectedHtml = - //language=html - """ - - - -
- - - - - -

ViewAction Get CountUp

- -

0

-

ViewAction Post AddItem

-
- -
- - - - - -
ItemAction
-

ViewAction Put/Patch Person Form

-
- - - - - - - -
- - -
-
This is a footer
- - - """.trimIndent() - assertEndpointReturns("/nested-action", expectedHtml) - } - - @Test - fun testLayoutComponent() { - //language=html - val expectedHtml = - """ - - - -
-
-

This is the SimpleViewComponent

-
Hello World
-
-
-
This is a footer
- - - """.trimIndent() - assertEndpointReturns("/layout", expectedHtml) - } - - @Test - fun testResourceTemplate() { - val expectedHtml = "Hello World" - assertEndpointReturns("/resource-template", expectedHtml) - } - - fun assertEndpointReturns(url: String, expectedHtml: String) { - val response: ResponseEntity = this.testRestTemplate - .exchange(url, HttpMethod.GET, null, String::class.java) - assertThat(response.statusCode) - .isEqualTo(HttpStatus.OK) - Assertions.assertEquals( - expectedHtml.rmWhitespaceBetweenHtmlTags(), response.body?.rmWhitespaceBetweenHtmlTags() - ) - } - - fun String.rmWhitespaceBetweenHtmlTags(): String { - // Replace whitespace between > and word - return this.replace("(?<=>)(\\s*)(?=\\w)".toRegex(), "") - .replace("(?<=\\w)(\\s*)(?=<)".toRegex(), "") - .replace("(?<=>)(\\s*)(?=<)".toRegex(), "") - .replace("\r\n","\n") - .trim() - } -} \ No newline at end of file +class ThymeleafKotlinIntegrationTest : ThymeleafIntegrationTestBase() \ No newline at end of file