|
| 1 | +package com._4point.aem.fluentforms.sampleapp.resources; |
| 2 | + |
| 3 | +import static com._4point.testing.matchers.jaxrs.ResponseMatchers.*; |
| 4 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 5 | +import static org.hamcrest.Matchers.*; |
| 6 | +import static org.junit.jupiter.api.Assertions.*; |
| 7 | + |
| 8 | +import java.net.URI; |
| 9 | +import java.util.List; |
| 10 | + |
| 11 | +import org.glassfish.jersey.media.multipart.FormDataMultiPart; |
| 12 | +import org.junit.jupiter.api.AfterEach; |
| 13 | +import org.junit.jupiter.api.BeforeEach; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.boot.test.context.SpringBootTest; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; |
| 17 | +import org.springframework.boot.test.web.server.LocalServerPort; |
| 18 | + |
| 19 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 20 | +import com.github.tomakehurst.wiremock.http.ResponseDefinition; |
| 21 | +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; |
| 22 | +import com.github.tomakehurst.wiremock.junit5.WireMockTest; |
| 23 | +import com.github.tomakehurst.wiremock.recording.SnapshotRecordResult; |
| 24 | +import com.github.tomakehurst.wiremock.stubbing.StubMapping; |
| 25 | + |
| 26 | +import jakarta.ws.rs.client.ClientBuilder; |
| 27 | +import jakarta.ws.rs.client.Entity; |
| 28 | +import jakarta.ws.rs.core.MediaType; |
| 29 | +import jakarta.ws.rs.core.Response; |
| 30 | +import jakarta.ws.rs.core.Response.Status; |
| 31 | + |
| 32 | +@WireMockTest(httpPort = FluentFormsResourcesTest.WIREMOCK_PORT) |
| 33 | +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) |
| 34 | +class AfSubmissionHandlerTest { |
| 35 | + public static final String AF_TEMPLATE_NAME = "sample00002test"; |
| 36 | + private static final String SUBMIT_ADAPTIVE_FORM_SERVICE_PATH = "/aem/content/forms/af/" + AF_TEMPLATE_NAME + "/jcr:content/guideContainer.af.submit.jsp"; |
| 37 | + |
| 38 | + private static final boolean WIREMOCK_RECORDING = false; |
| 39 | + static final int WIREMOCK_PORT = 5502; |
| 40 | + |
| 41 | + private static final String APPLICATION_PDF = "application/pdf"; |
| 42 | + private static final MediaType APPLICATION_PDF_TYPE = MediaType.valueOf(APPLICATION_PDF); |
| 43 | + |
| 44 | + @LocalServerPort |
| 45 | + private int port; |
| 46 | + |
| 47 | + @BeforeEach |
| 48 | + void setUp(WireMockRuntimeInfo wmRuntimeInfo) throws Exception { |
| 49 | + if (WIREMOCK_RECORDING) { |
| 50 | + WireMock.startRecording(getBaseUriString(4502)); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @AfterEach |
| 55 | + void tearDown() throws Exception { |
| 56 | + if (WIREMOCK_RECORDING) { |
| 57 | + SnapshotRecordResult recordings = WireMock.stopRecording(); |
| 58 | + List<StubMapping> mappings = recordings.getStubMappings(); |
| 59 | + System.out.println("Found " + mappings.size() + " recordings."); |
| 60 | + for (StubMapping mapping : mappings) { |
| 61 | + ResponseDefinition response = mapping.getResponse(); |
| 62 | + var jsonBody = response.getJsonBody(); |
| 63 | + System.out.println(jsonBody == null ? "JsonBody is null" : jsonBody.toPrettyString()); |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void test() { |
| 70 | + var mockData = mockFormData("http://localhost:8080/redirect", "{ \"foo\" : \"bar\"}"); |
| 71 | + |
| 72 | + Response response = ClientBuilder.newClient() |
| 73 | + .target(getBaseUri(port)) |
| 74 | + .path(SUBMIT_ADAPTIVE_FORM_SERVICE_PATH) |
| 75 | + .queryParam("form", "sample_template.xdp") |
| 76 | + .request() |
| 77 | + .post(Entity.entity(mockData, mockData.getMediaType())); |
| 78 | + |
| 79 | + assertThat(response, allOf(isStatus(Status.OK), hasMediaType(MediaType.TEXT_PLAIN_TYPE), hasEntityEqualTo("Successful Submit".getBytes()))); |
| 80 | + } |
| 81 | + |
| 82 | + private static FormDataMultiPart mockFormData(String redirect, String data) { |
| 83 | + final FormDataMultiPart getPdfForm = new FormDataMultiPart(); |
| 84 | + getPdfForm.field("guideContainerPath", "/aem/content/forms/af/" + AF_TEMPLATE_NAME + "/jcr:content/guideContainer") |
| 85 | + .field("aemFormComponentPath", "") |
| 86 | + .field("_asyncSubmit", "false") |
| 87 | + .field("_charset_", "UTF-8") |
| 88 | + .field("runtimeLocale", "en") |
| 89 | + .field("fileAttachmentMap", "{}") |
| 90 | + .field("afSubmissionInfo", "{\"computedMetaInfo\":{},\"stateOverrides\":{},\"signers\":{}}") |
| 91 | + .field("TextField1", "TextField1 Contents") |
| 92 | + .field("TextField2", "TextField2 Contents") |
| 93 | + .field("jcr:data", data) |
| 94 | + .field(":redirect", redirect) |
| 95 | + .field(":selfUrl", "/aem/content/forms/af/" + AF_TEMPLATE_NAME) |
| 96 | + .field("_guideValueMap", "yes") |
| 97 | + .field("_guideValuesMap", "{\"textdraw1555538078737\":\"<p style=\\\"text-align: center;\\\"><b>Sample Form</b></p>\\n\",\"TextField1\":\"DFGDFG\",\"TextField2\":\"DFGDG 233\",\"submit\":null}") |
| 98 | + .field("_guideAttachments", "") |
| 99 | + .field(":cq_csrf_token", "eyJleHAiOjE1NjU2MzUzNzcsImlhdCI6MTU2NTYzNDc3N30.9KB9yPr_mvIfyiwzn5S8mMh-yUzD0-BF99cJR7vW49M"); |
| 100 | + return getPdfForm; |
| 101 | + } |
| 102 | + |
| 103 | + private static String getBaseUriString(int port) { |
| 104 | + return getBaseUri(port).toString(); |
| 105 | + } |
| 106 | + |
| 107 | + private static URI getBaseUri(int port) { |
| 108 | + return URI.create("http://localhost:" + port); |
| 109 | + } |
| 110 | + |
| 111 | +} |
0 commit comments