Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add simple mustache mockup. #100

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.7.3")
implementation("com.itextpdf:itext7-core:7.2.6")
implementation("com.itextpdf:html2pdf:5.0.4")
implementation("org.springframework.boot:spring-boot-starter-mustache")
implementation("com.github.spullara.mustache.java:compiler:0.9.5")
implementation("org.apache.commons:commons-lang3:3.12.0")
implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.14.2")
implementation("io.sentry:sentry-spring-boot-starter-jakarta:6.34.0")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package uk.gov.justice.digital.hmpps.hmppssubjectaccessrequestworker.utils
import com.github.mustachejava.DefaultMustacheFactory
import com.github.mustachejava.Mustache
import com.github.mustachejava.MustacheFactory
import com.itextpdf.html2pdf.HtmlConverter
import com.itextpdf.kernel.pdf.PdfDocument
import com.itextpdf.kernel.pdf.PdfWriter
import com.itextpdf.layout.Document
import com.itextpdf.layout.element.IBlockElement
import com.itextpdf.layout.element.Paragraph
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.PrintWriter

class Mustache {

fun compile(file: String): Int {
val mf: MustacheFactory = DefaultMustacheFactory()
val m: Mustache? = mf.compile(file)
println(m)
return 0
}

fun execute(dataObject: Any): Int {
val mf = DefaultMustacheFactory()
val mustache = mf.compile(mf.getReader("template.mustache"), "hello")
mustache.execute(PrintWriter("test.html"), dataObject).flush()
return 0
}

fun convertToPdf(htmlFileName: String, pdfFileName: String): String {
val writer = PdfWriter(FileOutputStream("dummy.pdf"))
val pdfDocument = PdfDocument(writer)
val document = Document(pdfDocument)
document.add(Paragraph("This is an example title").setFontSize(16f).setBold())
document.add(Paragraph(""))
document.add(Paragraph("This is an example text").setFontSize(10f))
document.add(Paragraph(""))
val elements = HtmlConverter.convertToElements(FileInputStream(htmlFileName))
for (element in elements) {
document.add(element as IBlockElement)
}
document.close()
return pdfFileName
}
}

data class Item(val title: String, val createdOn: String, val text: String)
data class Keyworker(
val offenderKeyworkerId: String?,
val offenderNo: String?,
val staffId: String?,
val assignedDateTime: String?,
val active: String?,
val allocationReason: String?,
val allocationType: String?,
val userId: String?,
val prisonId: String?,
val expiryDateTime: String?,
val deallocationReason: String?,
val creationDateTime: String?,
val createUserId: String?,
val modifyDateTime: String?,
val modifyUserId: String?,
)
24 changes: 24 additions & 0 deletions src/main/resources/template.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<style>
body
{
font-family: 'Helvetica', 'Arial', sans-serif;
font-size: 12pt;
}
table {
border-collapse: collapse;
border-style: hidden;
width: 50%
}
table td, table th {
border: 1px solid black;
}
</style>

{{#.}}
<h2>Keyworker</h2>
<table>
<tr><td>Offender Keyworker ID</td><td>{{offenderKeyworkerId}}</td></tr>
<tr><td>Offender Number</td><td>{{offenderNo}}</td></tr>
<tr><td>User ID</td><td>{{userId}}</td></tr>
</table>
{{/.}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package uk.gov.justice.digital.hmpps.hmppssubjectaccessrequestworker.utils

import org.assertj.core.api.Assertions
import org.json.JSONArray
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows
import uk.gov.justice.digital.hmpps.hmppssubjectaccessrequestworker.integration.IntegrationTestBase

class MustacheTest : IntegrationTestBase() {

@Test
fun `Mustache compiles and returns 0`() {
val response = Mustache().compile("template.mustache")
Assertions.assertThat(response).isEqualTo(0)
assertThrows<Exception> { Mustache().compile("notemplate.mustache") }
}

@Test
fun `Template executes`() {
val testItem = Item("Item 1", "02/02/02", "This is the first item.")
val response = Mustache().execute(testItem)
Assertions.assertThat(response).isEqualTo(0)
}

@Test
fun `keyworker executes`() {
val dummyData = """[{"offenderKeyworkerId":23264,"offenderNo":"A8610DY","staffId":485588,"assignedDateTime":"2022-07-06T14:42:50.621973","active":true,"allocationReason":"AUTO","allocationType":"A","userId":"TWRIGHT","prisonId":"MDI","expiryDateTime":null,"deallocationReason":null,"creationDateTime":"2022-07-06T14:42:50.625039","createUserId":"TWRIGHT","modifyDateTime":"2022-07-06T14:42:50.625039","modifyUserId":"TWRIGHT"},{"offenderKeyworkerId":23264,"offenderNo":"A8610DY","staffId":485588,"assignedDateTime":"2022-07-06T14:42:50.621973","active":true,"allocationReason":"AUTO","allocationType":"A","userId":"TWRIGHT","prisonId":"MDI","expiryDateTime":null,"deallocationReason":null,"creationDateTime":"2022-07-06T14:42:50.625039","createUserId":"TWRIGHT","modifyDateTime":"2022-07-06T14:42:50.625039","modifyUserId":"TWRIGHT"}]""".trimMargin()
val data = JSONArray(dummyData).toList()
val response = Mustache().execute(data)
Assertions.assertThat(response).isEqualTo(0)
}

@Test
fun `convertToPdf returns filename`() {
val htmlTestFileName = "test.html"
val pdfTestFileName = "test.pdf"
val response = Mustache().convertToPdf(htmlTestFileName, pdfTestFileName)
Assertions.assertThat(response).isEqualTo(pdfTestFileName)
}
}
28 changes: 28 additions & 0 deletions test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<style>
body
{
font-family: 'Helvetica', 'Arial', sans-serif;
font-size: 12pt;
}
table {
border-collapse: collapse;
border-style: hidden;
width: 50%
}
table td, table th {
border: 1px solid black;
}
</style>

<h2>Keyworker</h2>
<table>
<tr><td>Offender Keyworker ID</td><td>23264</td></tr>
<tr><td>Offender Number</td><td>A8610DY</td></tr>
<tr><td>User ID</td><td>TWRIGHT</td></tr>
</table>
<h2>Keyworker</h2>
<table>
<tr><td>Offender Keyworker ID</td><td>23264</td></tr>
<tr><td>Offender Number</td><td>A8610DY</td></tr>
<tr><td>User ID</td><td>TWRIGHT</td></tr>
</table>
13 changes: 13 additions & 0 deletions test.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2></h2>
<small>Created on </small>
<p></p>

{{#.}}
<h1>Keyworker</h1>
<table>
<tr>
<td>Offender Keyworker ID 23264</td>
<td>Offender Number A8610DY</td>
</tr>
</table>
{{/.}}
Binary file added test.pdf
Binary file not shown.