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

test that default UriBuilder is injected when running tests #187

Merged
merged 3 commits into from
Jan 4, 2024
Merged
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
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 sent 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 sent 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));
}

}

}