Skip to content

Commit

Permalink
test that default UriBuilder is injected when running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gbourant committed Jan 4, 2024
1 parent a1c58da commit 4d6d52b
Show file tree
Hide file tree
Showing 2 changed files with 187 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.quarkiverse.renarde.test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Duration;
import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkiverse.renarde.Controller;
import io.quarkus.mailer.Mail;
import io.quarkus.mailer.MailTemplate;
import io.quarkus.mailer.MockMailbox;
import io.quarkus.qute.CheckedTemplate;
import io.quarkus.test.QuarkusUnitTest;

public class UriBuilderWithPrefixTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TestService.class)
.addAsResource(new StringAsset("quarkus.http.root-path=/support"), "application.properties")
.addAsResource(new StringAsset(
"{uri:MyController.endpoint()}\n{uriabs:MyController.endpoint()}\n{uri:MyController.endpoint('param')}\n{uriabs:MyController.endpoint('param')}"),
"templates/MyController/email.html"));

public static class MyController extends Controller {

@CheckedTemplate
public static class Templates {
public static native MailTemplate.MailTemplateInstance email();
}

@Path("/endpoint-url")
public void endpoint(@QueryParam("t") String testParam) {
}
}

@Inject
TestService testService;

@Inject
MockMailbox mockMailbox;

@Test
void testDefaultUriBuilderIsUsed() {
testService.sendQuteEmail();

var email = "[email protected]";
List<Mail> sent = mockMailbox.getMailsSentTo(email);
assertEquals(1, sent.size(), "It has send one email");

Mail mail = sent.get(0);
assertEquals(email, mail.getTo().get(0), "Email should be " + email);

String html = mail.getHtml();

var splittedPaths = html.split("\n");
var relativePath = splittedPaths[0];
var absolutePath = splittedPaths[1];
var relativePathWithParam = splittedPaths[2];
var absolutePathWithParam = splittedPaths[3];

assertEquals("/support/endpoint-url", relativePath);
assertEquals("http://localhost:8081/support/endpoint-url", absolutePath);
assertEquals("/support/endpoint-url?t=param", relativePathWithParam);
assertEquals("http://localhost:8081/support/endpoint-url?t=param", absolutePathWithParam);
}

@ApplicationScoped
public static class TestService {

public void sendQuteEmail() {
MyController.Templates
.email()
.to("[email protected]")
.send()
.await()
.atMost(Duration.ofSeconds(10));
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package io.quarkiverse.renarde.test;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.time.Duration;
import java.util.List;

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.QueryParam;

import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkiverse.renarde.Controller;
import io.quarkus.mailer.Mail;
import io.quarkus.mailer.MailTemplate;
import io.quarkus.mailer.MockMailbox;
import io.quarkus.qute.CheckedTemplate;
import io.quarkus.test.QuarkusUnitTest;

public class UriBuilderWithoutPrefixTest {

@RegisterExtension
static final QuarkusUnitTest config = new QuarkusUnitTest()
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(TestService.class)
.addAsResource(new StringAsset(
"{uri:MyController.endpoint()}\n{uriabs:MyController.endpoint()}\n{uri:MyController.endpoint('param')}\n{uriabs:MyController.endpoint('param')}"),
"templates/MyController/email.html"));

public static class MyController extends Controller {

@CheckedTemplate
public static class Templates {
public static native MailTemplate.MailTemplateInstance email();
}

@Path("/endpoint-url")
public void endpoint(@QueryParam("t") String testParam) {
}
}

@Inject
TestService testService;

@Inject
MockMailbox mockMailbox;

@Test
void testDefaultUriBuilderIsUsed() {
testService.sendQuteEmail();

var email = "[email protected]";
List<Mail> sent = mockMailbox.getMailsSentTo(email);
assertEquals(1, sent.size(), "It has send one email");

Mail mail = sent.get(0);
assertEquals(email, mail.getTo().get(0), "Email should be " + email);

String html = mail.getHtml();

var splittedPaths = html.split("\n");
var relativePath = splittedPaths[0];
var absolutePath = splittedPaths[1];
var relativePathWithParam = splittedPaths[2];
var absolutePathWithParam = splittedPaths[3];

assertEquals("/endpoint-url", relativePath);
assertEquals("http://localhost:8081/endpoint-url", absolutePath);
assertEquals("/endpoint-url?t=param", relativePathWithParam);
assertEquals("http://localhost:8081/endpoint-url?t=param", absolutePathWithParam);
}

@ApplicationScoped
public static class TestService {

public void sendQuteEmail() {
MyController.Templates
.email()
.to("[email protected]")
.send()
.await()
.atMost(Duration.ofSeconds(10));
}

}

}

0 comments on commit 4d6d52b

Please sign in to comment.