Skip to content

Commit

Permalink
Fixed merging SiteSchema and classification together
Browse files Browse the repository at this point in the history
  • Loading branch information
mfriesen committed Jul 10, 2024
1 parent 9c55b94 commit 1c60968
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,17 +345,16 @@ private void appendDocumentAttributes(final WriteRequestBuilder writeBuilder, fi
private List<SchemaAttributes> getSchemaAttributes(final String siteId,
final Collection<DocumentAttributeRecord> documentAttributeRecords) {

Schema schema = getSchame(siteId);

List<Schema> schemas = documentAttributeRecords.stream()
.filter(a -> DocumentAttributeValueType.CLASSIFICATION.equals(a.getValueType()))
.map(a -> this.schemaService.findClassification(siteId, a.getStringValue()))
.map(this.schemaService::getSchema).toList();
.map(this.schemaService::getSchema)
.map(a -> this.schemaService.mergeSchemaIntoClassification(schema, a)).toList();

if (schemas.isEmpty()) {
Schema schema = getSchame(siteId);

if (schema != null) {
schemas = List.of(schema);
}
if (schema != null && notNull(schemas).isEmpty()) {
schemas = List.of(schema);
}

return schemas.stream().map(Schema::getAttributes).toList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,13 @@ ClassificationRecord setClassification(String siteId, String classificationId, S
* @return Schema
*/
Schema getSchema(ClassificationRecord classification);

/**
* Merge {@link Schema} together.
*
* @param from {@link Schema}
* @param to {@link Schema}
* @return {@link ClassificationRecord}
*/
Schema mergeSchemaIntoClassification(Schema from, Schema to);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.formkiq.aws.dynamodb.BatchGetConfig;
Expand Down Expand Up @@ -238,6 +240,46 @@ public Schema getSchema(final ClassificationRecord classification) {
return gson.fromJson(classification.getSchema(), Schema.class);
}

@Override
public Schema mergeSchemaIntoClassification(final Schema from, final Schema to) {

if (from != null) {
SchemaAttributes fromAttributes = from.getAttributes();
Map<String, SchemaAttributesRequired> required =
notNull(fromAttributes.getRequired()).stream().collect(
Collectors.toMap(SchemaAttributesRequired::getAttributeKey, Function.identity()));

to.getAttributes().getRequired().forEach(a -> {

if (required.containsKey(a.getAttributeKey())) {

SchemaAttributesRequired attr = required.get(a.getAttributeKey());

List<String> allowed = Stream.concat(notNull(a.getAllowedValues()).stream(),
notNull(attr.getAllowedValues()).stream()).toList();
a.allowedValues(allowed);

required.remove(a.getAttributeKey());
}

});

// get all required attributes
List<SchemaAttributesRequired> req = Stream
.concat(to.getAttributes().getRequired().stream(), required.values().stream()).toList();
to.getAttributes().required(req);

// merge composite keys
List<SchemaAttributesCompositeKey> cks =
Stream.concat(to.getAttributes().getCompositeKeys().stream(),
from.getAttributes().getCompositeKeys().stream()).toList();
to.getAttributes().compositeKeys(cks);
// can add allowed values / not remove
}

return to;
}

/**
* Validate Classification.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -517,18 +517,35 @@ void testAddDocument04() throws ApiException {

String classificationId = addClassification(siteId, attr0);

AddDocumentAttribute attribute = createStringAttribute("invoiceNumber", "INV-001");
AddDocumentAttribute attribute0 = createStringAttribute("invoiceNumber", "INV-001");
AddDocumentAttributeClassification classification =
new AddDocumentAttributeClassification().classificationId(classificationId);

// when
String documentId =
addDocument(siteId, List.of(new AddDocumentAttribute(classification), attribute));
try {
addDocument(siteId, List.of(new AddDocumentAttribute(classification), attribute0));
fail();
} catch (ApiException e) {
// then
assertEquals(ApiResponseStatus.SC_BAD_REQUEST.getStatusCode(), e.getCode());
assertEquals(
"{\"errors\":[{\"key\":\"other\",\"error\":\"missing required attribute 'other'\"}]}",
e.getResponseBody());
}

// given
AddDocumentAttribute attribute1 = createStringAttribute("other", "thing");

// when
String documentId = addDocument(siteId,
List.of(new AddDocumentAttribute(classification), attribute0, attribute1));

// then
List<DocumentAttribute> documentAttributes = notNull(this.documentAttributesApi
.getDocumentAttributes(documentId, siteId, null, null).getAttributes());
assertEquals(2, documentAttributes.size());

final int expected = 3;
assertEquals(expected, documentAttributes.size());

assertEquals("Classification", documentAttributes.get(0).getKey());
assertEquals(classificationId, documentAttributes.get(0).getStringValue());
Expand All @@ -537,6 +554,10 @@ void testAddDocument04() throws ApiException {
assertEquals("invoiceNumber", documentAttributes.get(1).getKey());
assertEquals("INV-001", documentAttributes.get(1).getStringValue());
assertEquals(AttributeValueType.STRING, documentAttributes.get(1).getValueType());

assertEquals("other", documentAttributes.get(2).getKey());
assertEquals("thing", documentAttributes.get(2).getStringValue());
assertEquals(AttributeValueType.STRING, documentAttributes.get(2).getValueType());
}
}

Expand Down Expand Up @@ -647,6 +668,88 @@ void testAddDocument06() throws ApiException {
}
}

/**
* Add document with classification but site schema has required attribute.
*/
@Test
void testAddDocument07() throws ApiException {
// given
for (String siteId : Arrays.asList("default", UUID.randomUUID().toString())) {

setBearerToken(siteId);

addAttribute(siteId, "other");
addAttribute(siteId, "invoiceNumber");

SchemaAttributes attr = createSchemaAttributes(List.of("other"), null);
SetSitesSchemaRequest setSiteSchema =
new SetSitesSchemaRequest().name("test").attributes(attr);
this.schemasApi.setSitesSchema(siteId, setSiteSchema);

SchemaAttributes attr0 = createSchemaAttributes(List.of("invoiceNumber"), null);
String classificationId = addClassification(siteId, attr0);

AddDocumentAttribute attribute = createStringAttribute("invoiceNumber", "INV-001");
AddDocumentAttributeClassification classification =
new AddDocumentAttributeClassification().classificationId(classificationId);

// when
try {
addDocument(siteId, List.of(new AddDocumentAttribute(classification), attribute));
fail();
} catch (ApiException e) {
// then
assertEquals(ApiResponseStatus.SC_BAD_REQUEST.getStatusCode(), e.getCode());
assertEquals("{\"errors\":[{\"key\":\"other\","
+ "\"error\":\"missing required attribute 'other'\"}]}", e.getResponseBody());
}
}
}

/**
* Add document with classification, same attributes different allowed values.
*/
@Test
void testAddDocument08() throws ApiException {
// given
for (String siteId : Arrays.asList("default", UUID.randomUUID().toString())) {

setBearerToken(siteId);

addAttribute(siteId, "invoiceNumber");

SchemaAttributes attr0 = createSchemaAttributes(List.of("invoiceNumber"), null);
attr0.getRequired().get(0).addAllowedValuesItem("123");

SetSitesSchemaRequest setSiteSchema =
new SetSitesSchemaRequest().name("test").attributes(attr0);
this.schemasApi.setSitesSchema(siteId, setSiteSchema);

SchemaAttributes attr1 = createSchemaAttributes(List.of("invoiceNumber"), null);
attr1.getRequired().get(0).addAllowedValuesItem("INV-001");
String classificationId = addClassification(siteId, attr1);

AddDocumentAttribute attribute = createStringAttribute("invoiceNumber", "INV-001");
AddDocumentAttributeClassification classification =
new AddDocumentAttributeClassification().classificationId(classificationId);

// when
String documentId =
addDocument(siteId, List.of(new AddDocumentAttribute(classification), attribute));

// then
List<DocumentAttribute> documentAttributes = notNull(this.documentAttributesApi
.getDocumentAttributes(documentId, siteId, null, null).getAttributes());

final int expected = 2;
assertEquals(expected, documentAttributes.size());

int i = 0;
assertEquals("Classification", documentAttributes.get(i++).getKey());
assertEquals("invoiceNumber", documentAttributes.get(i).getKey());
}
}

private @Nullable String addDocument(final String siteId,
final List<AddDocumentAttribute> attributes) throws ApiException {
AddDocumentRequest areq = new AddDocumentRequest().content("adasd").attributes(attributes);
Expand Down

0 comments on commit 1c60968

Please sign in to comment.