Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed May 31, 2024
1 parent 81f3098 commit a225413
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,22 @@ private Collection<ValidationError> validateSchema(final Schema schema, final St
} else {

SchemaAttributes schemaAttributes = schema.getAttributes();

List<SchemaAttributesCompositeKey> compositeKeys =
notNull(schemaAttributes.getCompositeKeys());

if (notNull(schemaAttributes.getRequired()).isEmpty()
&& notNull(schemaAttributes.getOptional()).isEmpty()) {
&& notNull(schemaAttributes.getOptional()).isEmpty() && compositeKeys.isEmpty()) {
errors.add(new ValidationErrorImpl()
.error("either 'required' or 'optional' attributes list is required"));
.error("either 'required', 'optional' or 'compositeKeys' attributes list is required"));
}

compositeKeys.forEach(keys -> {
if (notNull(keys.getAttributeKeys()).size() == 1) {
errors.add(new ValidationErrorImpl().key("compositeKeys")
.error("compositeKeys must have more than 1 value"));
}
});
}

return errors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import com.formkiq.aws.services.lambda.ApiResponseStatus;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import com.formkiq.client.invoker.ApiException;
Expand Down Expand Up @@ -347,7 +349,7 @@ public void testAddUploadDocumentWithSetSitesSchema02() throws ApiException {

assertEquals("strings", attributes.getAttributes().get(i).getKey());
assertEquals("abc,qwe",
String.join(",", attributes.getAttributes().get(i++).getStringValues()));
String.join(",", attributes.getAttributes().get(i).getStringValues()));
}
}
}
Expand Down Expand Up @@ -410,7 +412,7 @@ public void testAddUploadDocumentWithSetSitesSchema03() throws ApiException {

assertEquals("strings", attributes.getAttributes().get(i).getKey());
assertEquals("111,222",
String.join(",", attributes.getAttributes().get(i++).getStringValues()));
String.join(",", attributes.getAttributes().get(i).getStringValues()));
}
}

Expand Down Expand Up @@ -1297,7 +1299,7 @@ public void testSearchCompositeKey03() throws ApiException {

assertEquals("category#date", response0.getDocuments().get(i).getMatchedAttribute().getKey());
assertEquals("person#2024-01-10",
response0.getDocuments().get(i++).getMatchedAttribute().getStringValue());
response0.getDocuments().get(i).getMatchedAttribute().getStringValue());

// when - invalid search
DocumentSearchRequest sreq1 = new DocumentSearchRequest().query(new DocumentSearch()
Expand Down Expand Up @@ -1415,7 +1417,7 @@ public void testSearchCompositeKey05() throws ApiException {

assertEquals("category#date", response0.getDocuments().get(i).getMatchedAttribute().getKey());
assertEquals("person#000000000000200.0000",
response0.getDocuments().get(i++).getMatchedAttribute().getStringValue());
response0.getDocuments().get(i).getMatchedAttribute().getStringValue());
}
}

Expand Down Expand Up @@ -1580,6 +1582,48 @@ public void testSearchCompositeKey08() throws ApiException {
}
}

/**
* Search document by composite key not defined in optional/required attributes.
*
* @throws ApiException ApiException
*/
@Test
public void testSearchCompositeKey09() throws ApiException {
// given
for (String siteId : Arrays.asList("default", UUID.randomUUID().toString())) {

setBearerToken(siteId);

String key0 = "strings_" + siteId;
String key1 = "category_" + siteId;
addAttribute(siteId, key0, null);
addAttribute(siteId, key1, null);

SetSitesSchemaRequest req = new SetSitesSchemaRequest().name("joe")
.attributes(new SchemaAttributes().addCompositeKeysItem(
new AttributeSchemaCompositeKey().attributeKeys(Arrays.asList(key0, key1))));
this.schemasApi.setSitesSchema(siteId, req);

AddDocumentUploadRequest ureq0 = new AddDocumentUploadRequest().path("sample.txt")
.addAttributesItem(new AddDocumentAttribute().key(key1).stringValues(List.of("person")))
.addAttributesItem(
new AddDocumentAttribute().key(key0).stringValues(Arrays.asList("111", "222")));

// when
String documentId0 =
this.documentsApi.addDocumentUpload(ureq0, siteId, null, null, null).getDocumentId();

// then
DocumentSearchRequest sreq0 = new DocumentSearchRequest().query(
new DocumentSearch().addAttributesItem(new DocumentSearchAttribute().key(key0).eq("222"))
.addAttributesItem(new DocumentSearchAttribute().key(key1).eq("person")));

DocumentSearchResponse response0 =
this.searchApi.documentSearch(sreq0, siteId, null, null, null);
assertEquals(1, response0.getDocuments().size());
assertEquals(documentId0, response0.getDocuments().get(0).getDocumentId());
}
}

/**
* PUT /documents/{documentId}/attributes. Add attributes.
Expand Down Expand Up @@ -1745,10 +1789,9 @@ public void testSetSitesSchema02() {
fail();
} catch (ApiException e) {
// then
assertEquals(
"{\"errors\":[{\"key\":\"name\",\"error\":\"'name' is required\"},"
+ "{\"error\":\"either 'required' or 'optional' attributes list is required\"}]}",
e.getResponseBody());
assertEquals("{\"errors\":[{\"key\":\"name\",\"error\":\"'name' is required\"},"
+ "{\"error\":\"either 'required', 'optional' or 'compositeKeys' "
+ "attributes list is required\"}]}", e.getResponseBody());
}
}
}
Expand Down Expand Up @@ -1893,7 +1936,6 @@ public void testSetSitesSchema06() throws ApiException {
e.getResponseBody());
}
}

}

/**
Expand Down Expand Up @@ -1930,6 +1972,39 @@ public void testSetSitesSchema07() throws ApiException {
}
}

/**
* PUT /sites/{siteId}/schema/document with single composite attribute.
*
* @throws ApiException an error has occurred
*/
@Test
public void testSetSitesSchema08() throws ApiException {
// given
for (String siteId : Arrays.asList("default", UUID.randomUUID().toString())) {

setBearerToken(siteId);

addAttribute(siteId, "category", null);

SetSitesSchemaRequest req = new SetSitesSchemaRequest().name("joe").attributes(
new SchemaAttributes().allowAdditionalAttributes(Boolean.TRUE).addCompositeKeysItem(
new AttributeSchemaCompositeKey().attributeKeys(List.of("category"))));

// when
try {
this.schemasApi.setSitesSchema(siteId, req);
fail();
} catch (ApiException e) {
// then
assertEquals(ApiResponseStatus.SC_BAD_REQUEST.getStatusCode(), e.getCode());
assertEquals(
"{\"errors\":[{\"key\":\"compositeKeys\","
+ "\"error\":\"compositeKeys must have more than 1 value\"}]}",
e.getResponseBody());
}
}
}

/**
* PATCH /documents after site schema is applied and without attributes.
*
Expand Down

0 comments on commit a225413

Please sign in to comment.