Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Jun 1, 2024
1 parent 1caa07e commit b56e40e
Showing 1 changed file with 95 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,14 @@
import com.formkiq.client.api.MappingsApi;
import com.formkiq.client.invoker.ApiClient;
import com.formkiq.client.invoker.ApiException;
import com.formkiq.client.model.AddAction;
import com.formkiq.client.model.AddActionParameters;
import com.formkiq.client.model.AddMapping;
import com.formkiq.client.model.AddMappingRequest;
import com.formkiq.client.model.AddMappingResponse;
import com.formkiq.client.model.AttributeDataType;
import com.formkiq.client.model.DocumentActionType;
import com.formkiq.client.model.Mapping;
import com.formkiq.client.model.MappingAttribute;
import com.formkiq.client.model.MappingAttributeLabelMatchingType;
import com.formkiq.client.model.MappingAttributeSourceType;
Expand All @@ -38,9 +43,16 @@

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static com.formkiq.aws.dynamodb.objects.Objects.notNull;
import static com.formkiq.testutils.aws.FkqAttributeService.addAttribute;
import static com.formkiq.testutils.aws.FkqDocumentService.addDocument;
import static com.formkiq.testutils.aws.FkqDocumentService.getDocumentAttribute;
import static com.formkiq.testutils.aws.FkqDocumentService.waitForActionsComplete;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;

/**
Expand Down Expand Up @@ -73,7 +85,7 @@ public class MappingRequestTest extends AbstractAwsIntegrationTest {
*/
@Test
@Timeout(value = TEST_TIMEOUT)
public void addMappingRequest01() throws ApiException {
void addMappingRequest01() throws ApiException {
// given
for (String siteId : Arrays.asList(null, SITE_ID)) {

Expand All @@ -96,32 +108,109 @@ public void addMappingRequest01() throws ApiException {
}

/**
* Add Attribute empty request.
* Add Attribute.
*
* @throws ApiException ApiException
*/
@Test
@Timeout(value = TEST_TIMEOUT)
public void addMappingRequest02() throws ApiException {
void addMappingRequest02() throws ApiException {
// given
for (String siteId : Arrays.asList(null, SITE_ID)) {

List<ApiClient> apiClients = getApiClients(siteId);
ApiClient client = apiClients.get(0);
String attributeKey = "invoice_" + UUID.randomUUID();

addAttribute(client, siteId, "invoice", AttributeDataType.STRING, null);
addAttribute(client, siteId, attributeKey, AttributeDataType.STRING, null);
MappingsApi api = new MappingsApi(client);

AddMapping addMapping = new AddMapping().name("test")
.addAttributesItem(new MappingAttribute().attributeKey("invoice")
.addAttributesItem(new MappingAttribute().attributeKey(attributeKey)
.sourceType(MappingAttributeSourceType.CONTENT)
.labelMatchingType(MappingAttributeLabelMatchingType.CONTAINS)
.labelTexts(List.of("invoice")));

// when
api.addMapping(new AddMappingRequest().mapping(addMapping), siteId);
AddMappingResponse addMappingResponse =
api.addMapping(new AddMappingRequest().mapping(addMapping), siteId);

// then
final String mappingId = addMappingResponse.getMappingId();

List<Mapping> mappings = notNull(api.getMappings(siteId, null, null).getMappings());
assertFalse(mappings.isEmpty());
assertNotNull(mappings.get(0).getName());
assertNotNull(mappings.get(0).getDescription());

List<MappingAttribute> attributes = notNull(mappings.get(0).getAttributes());
assertNotNull(attributes.get(0).getAttributeKey());

Mapping mapping = api.getMapping(mappingId, siteId).getMapping();
assertNotNull(mapping);
assertEquals("test", mapping.getName());
assertEquals("", mapping.getDescription());

attributes = notNull(mapping.getAttributes());
assertEquals(1, attributes.size());
assertEquals(attributeKey, attributes.get(0).getAttributeKey());

api.deleteMapping(mappingId, siteId);

try {
api.getMapping(mappingId, siteId).getMapping();
} catch (ApiException e) {
assertEquals("{\"message\":\"Mapping '" + mappingId + "' not found\"}",
e.getResponseBody());
}
}
}

/**
* Test IDP action.
*
* @throws ApiException ApiException
*/
@Test
@Timeout(value = TEST_TIMEOUT)
void addIdpAction01() throws ApiException, InterruptedException {
// given
final String attributeKey = "document_invoice_" + UUID.randomUUID();

List<ApiClient> apiClients = getApiClients(null);
ApiClient client = apiClients.get(0);

addAttribute(client, null, attributeKey, AttributeDataType.STRING, null);
MappingsApi api = new MappingsApi(client);

AddMapping addMapping = new AddMapping().name("Document Invoice")
.addAttributesItem(new MappingAttribute().attributeKey(attributeKey)
.sourceType(MappingAttributeSourceType.CONTENT)
.labelMatchingType(MappingAttributeLabelMatchingType.CONTAINS)
.validationRegex("INV-\\d+").labelTexts(List.of("invoice", "invoice no")));

AddMappingResponse addMappingResponse =
api.addMapping(new AddMappingRequest().mapping(addMapping), null);

final String mappingId = addMappingResponse.getMappingId();

String content = """
From:
DEMO - Sliced Invoices
Order Number 12345
Invoice Number INV-3337
123 Somewhere Street Your City AZ 12345 admin@slicedinvoices.com""";

// when
AddAction action = new AddAction().type(DocumentActionType.IDP)
.parameters(new AddActionParameters().mappingId(mappingId));
String documentId =
addDocument(client, null, "document_invoice.txt", content, "text/plain", List.of(action));

// then
waitForActionsComplete(client, null, documentId);

assertEquals("INV-3337",
getDocumentAttribute(client, null, documentId, attributeKey).getStringValue());
}
}

0 comments on commit b56e40e

Please sign in to comment.