Skip to content

Commit 5228f0d

Browse files
committed
Added some convenience methods for creating AdSubmissionHandlers.
1 parent b7c59f2 commit 5228f0d

2 files changed

Lines changed: 210 additions & 1 deletion

File tree

spring/fluentforms-spring-boot-autoconfigure/src/main/java/com/_4point/aem/fluentforms/spring/AemProxyAfSubmission.java

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
import java.util.List;
1010
import java.util.Map;
1111
import java.util.Map.Entry;
12+
import java.util.Objects;
1213
import java.util.Optional;
1314
import java.util.function.Function;
15+
import java.util.regex.Pattern;
1416

1517
import org.glassfish.jersey.client.ChunkedInput;
1618
import org.glassfish.jersey.client.ClientProperties;
@@ -267,6 +269,12 @@ private static byte[] transferFromAem(Response result, Logger logger) {
267269
* Implement this interface in order to provide code that will handle an Adaptive Form submission
268270
*
269271
*/
272+
/**
273+
*
274+
*/
275+
/**
276+
*
277+
*/
270278
public interface AfSubmissionHandler {
271279
/**
272280
* Object that contains the data submitted by the Adaptive Form
@@ -347,6 +355,125 @@ public record Redirect(URI redirectUrl) implements SubmitResponse {};
347355
* a SubmitResponse object that will be turned into an HTTP response to the submission.
348356
*/
349357
SubmitResponse processSubmission(Submission submission);
358+
359+
/**
360+
* Creates an AfSubmissionHandler that handles form submissions from a specific form
361+
*
362+
* @param formName
363+
* name of the form (so the form name under FormsAndDocuments)
364+
* @param handlerLogic
365+
* function that will be called when a form submission for occurs from that form
366+
* @return
367+
* an AfSubmissionHandler object
368+
*/
369+
public static AfSubmissionHandler canHandleFormNameEquals(String formName, Function<Submission, SubmitResponse> handlerLogic) {
370+
Objects.requireNonNull(formName, "Form Name for submission handler cannot be null.");
371+
return new AfSubmissionHandler() {
372+
373+
@Override
374+
public boolean canHandle(String formNameIn) {
375+
return formName.equals(formNameIn);
376+
}
377+
378+
@Override
379+
public SubmitResponse processSubmission(Submission submission) {
380+
return handlerLogic.apply(submission);
381+
}
382+
};
383+
}
384+
385+
/**
386+
* Creates an AfSubmissionHandler that handles form submissions from one or more specific forms
387+
*
388+
* @param handlerLogic
389+
* function that will be called when a form submission for occurs from that form
390+
* @param formNames
391+
* one or more form names (can be zero, but then this handler will never be called)
392+
* @return
393+
* an AfSubmissionHandler object
394+
*/
395+
public static AfSubmissionHandler canHandleFormNameAnyOf(Function<Submission, SubmitResponse> handlerLogic, String... formNames) {
396+
if (formNames.length < 1) {
397+
logger.atWarn().log("No form names were supplied, so this handler will never be called.");
398+
}
399+
return new AfSubmissionHandler() {
400+
401+
@Override
402+
public boolean canHandle(String formNameIn) {
403+
for (String formName : formNames) {
404+
if (Objects.equals(formName, formNameIn)) {
405+
return true;
406+
}
407+
}
408+
return false;
409+
}
410+
411+
@Override
412+
public SubmitResponse processSubmission(Submission submission) {
413+
return handlerLogic.apply(submission);
414+
}
415+
};
416+
}
417+
418+
/**
419+
* Creates an AfSubmissionHandler that handles form submissions from one or more specific forms
420+
*
421+
* @param formNames
422+
* list of one or more form names (can be empty, but then this handler will never be called)
423+
* @param handlerLogic
424+
* function that will be called when a form submission for occurs from that form
425+
* @return
426+
* an AfSubmissionHandler object
427+
*/
428+
public static AfSubmissionHandler canHandleFormNameAnyOf(List<String> formNames, Function<Submission, SubmitResponse> handlerLogic) {
429+
if (formNames.size() < 1) {
430+
logger.atWarn().log("No form names were supplied, so this handler will never be called.");
431+
}
432+
return new AfSubmissionHandler() {
433+
434+
@Override
435+
public boolean canHandle(String formNameIn) {
436+
for (String formName : formNames) {
437+
if (Objects.equals(formName, formNameIn)) {
438+
return true;
439+
}
440+
}
441+
return false;
442+
}
443+
444+
@Override
445+
public SubmitResponse processSubmission(Submission submission) {
446+
return handlerLogic.apply(submission);
447+
}
448+
};
449+
}
450+
451+
/**
452+
* Creates an AfSubmissionHandler that handles form submissions from one or more specific forms
453+
*
454+
* @param formNameRegEx
455+
* a regex that will be applied to the name of the form, if it matches, then the handler will apply
456+
* @param handlerLogic
457+
* function that will be called when a form submission for occurs from that form
458+
* @return
459+
* an AfSubmissionHandler object
460+
*/
461+
public static AfSubmissionHandler canHandleFormNameMatchesRegex(String formNameRegEx, Function<Submission, SubmitResponse> handlerLogic) {
462+
final var pattern = Pattern.compile(Objects.requireNonNull(formNameRegEx, "Form Name RegEx for submission handler cannot be null."));
463+
464+
return new AfSubmissionHandler() {
465+
466+
@Override
467+
public boolean canHandle(String formNameIn) {
468+
return pattern.matcher(formNameIn).matches();
469+
}
470+
471+
@Override
472+
public SubmitResponse processSubmission(Submission submission) {
473+
return handlerLogic.apply(submission);
474+
}
475+
};
476+
}
350477
}
351478

352479
/**

spring/fluentforms-spring-boot-autoconfigure/src/test/java/com/_4point/aem/fluentforms/spring/AemProxyAfSubmissionTest.java

Lines changed: 83 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,26 @@
22

33
import static com._4point.aem.fluentforms.spring.AemProxyAfSubmissionTest.TestApplication.JerseyConfig;
44
import static com._4point.testing.matchers.jaxrs.ResponseMatchers.*;
5+
import static com._4point.testing.matchers.javalang.ExceptionMatchers.*;
56
import static org.hamcrest.MatcherAssert.assertThat;
67
import static org.hamcrest.Matchers.*;
78
import static org.junit.jupiter.api.Assertions.*;
89

910
import java.net.URI;
1011
import java.net.URISyntaxException;
1112
import java.nio.charset.StandardCharsets;
13+
import java.util.List;
1214
import java.util.function.Function;
1315

1416
import org.glassfish.jersey.client.ClientProperties;
1517
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
1618
import org.glassfish.jersey.media.multipart.MultiPartFeature;
1719
import org.glassfish.jersey.server.ResourceConfig;
1820
import org.junit.jupiter.api.BeforeEach;
21+
import org.junit.jupiter.api.DisplayName;
1922
import org.junit.jupiter.api.Test;
2023
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.CsvSource;
2125
import org.junit.jupiter.params.provider.EnumSource;
2226
import org.slf4j.Logger;
2327
import org.slf4j.LoggerFactory;
@@ -329,12 +333,90 @@ private TestScenario(String expectedContentType, Function<String, SubmitResponse
329333

330334
@ParameterizedTest
331335
@EnumSource
332-
void testText(TestScenario scenario) {
336+
void testResponseCreationMethod(TestScenario scenario) {
333337
var result = scenario.methodUnderTest.apply(SAMPLE_TEXT);
334338
assertAll(
335339
()->assertArrayEquals(SAMPLE_TEXT_BYTES, result.responseBytes()),
336340
()->assertEquals(scenario.expectedContentType, result.mediaType())
337341
);
338342
}
339343
}
344+
345+
public static class AfSubmissionHandlerTests {
346+
// In this class we test the static convenience methods that generate AfSubmissionHandlers. Since the
347+
// processSubmission logic is one line (and trivial) we don't test it, however we do test the canHandle()
348+
// method generated by the convenience method.
349+
350+
@ParameterizedTest
351+
@CsvSource({
352+
"formName, true",
353+
"notFormName, false"
354+
})
355+
void testcanHandleFormNameEquals(String formNameIn, boolean expectedResult) {
356+
var underTest = AfSubmissionHandler.canHandleFormNameEquals("formName", t->null);
357+
assertEquals(expectedResult, underTest.canHandle(formNameIn));
358+
}
359+
360+
@DisplayName("Passing in null should produce a null pointer exception")
361+
@Test
362+
void testcanHandleFormNameEquals_Null() {
363+
NullPointerException ex = assertThrows(NullPointerException.class, ()->AfSubmissionHandler.canHandleFormNameEquals(null, t->null));
364+
assertThat(ex, exceptionMsgContainsAll("Form Name for submission handler cannot be null"));
365+
}
366+
367+
@ParameterizedTest
368+
@CsvSource({
369+
"formName1, true",
370+
"formName2, true",
371+
"notFormName, false"
372+
})
373+
void testcanHandleFormNameAnyOf(String formNameIn, boolean expectedResult) {
374+
var underTest = AfSubmissionHandler.canHandleFormNameAnyOf(t->null, "formName1", "formName2");
375+
assertEquals(expectedResult, underTest.canHandle(formNameIn));
376+
}
377+
378+
@ParameterizedTest
379+
@CsvSource({
380+
"formName1, false",
381+
"formName2, false",
382+
"notFormName, false"
383+
})
384+
void testcanHandleFormNameAnyOf_NoNames(String formNameIn, boolean expectedResult) {
385+
var underTest = AfSubmissionHandler.canHandleFormNameAnyOf(t->null);
386+
assertEquals(expectedResult, underTest.canHandle(formNameIn));
387+
}
388+
389+
@ParameterizedTest
390+
@CsvSource({
391+
"formName1, true",
392+
"formName2, true",
393+
"notFormName, false"
394+
})
395+
void testcanHandleFormNameAnyOf_List(String formNameIn, boolean expectedResult) {
396+
var underTest = AfSubmissionHandler.canHandleFormNameAnyOf(List.of("formName1", "formName2"), t->null);
397+
assertEquals(expectedResult, underTest.canHandle(formNameIn));
398+
}
399+
400+
@ParameterizedTest
401+
@CsvSource({
402+
"formName1, false",
403+
"formName2, false",
404+
"notFormName, false"
405+
})
406+
void testcanHandleFormNameAnyOf__List_NoNames(String formNameIn, boolean expectedResult) {
407+
var underTest = AfSubmissionHandler.canHandleFormNameAnyOf(List.of(), t->null);
408+
assertEquals(expectedResult, underTest.canHandle(formNameIn));
409+
}
410+
411+
@ParameterizedTest
412+
@CsvSource({
413+
"formName1, true",
414+
"formName2, true",
415+
"notFormName, false"
416+
})
417+
void testcanHandleFormNameMatchesRegEx(String formNameIn, boolean expectedResult) {
418+
var underTest = AfSubmissionHandler.canHandleFormNameMatchesRegex("formName.*", t->null);
419+
assertEquals(expectedResult, underTest.canHandle(formNameIn));
420+
}
421+
}
340422
}

0 commit comments

Comments
 (0)