com.kenshoo.pl.entity.spi.helpers.ChangeResultInspector Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of persistence-layer Show documentation
Show all versions of persistence-layer Show documentation
A Java persistence layer based on JOOQ for high performance and business flow support.
package com.kenshoo.pl.entity.spi.helpers;
import com.google.common.collect.Sets;
import com.kenshoo.pl.entity.ChangeEntityCommand;
import com.kenshoo.pl.entity.ChangeResult;
import com.kenshoo.pl.entity.CurrentEntityState;
import com.kenshoo.pl.entity.EntityChangeResult;
import com.kenshoo.pl.entity.EntityField;
import com.kenshoo.pl.entity.EntityType;
import com.kenshoo.pl.entity.FieldsValueMap;
import com.kenshoo.pl.entity.Identifier;
import com.kenshoo.pl.entity.ValidationError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
public class ChangeResultInspector> {
private static final Logger logger = LoggerFactory.getLogger(ChangeResultInspector.class);
public static final String VALUE_NOT_FOUND = "not found";
public static final ValidationError UNEXPECTED_ERROR = new ValidationError("Unexpected error");
private final Set> inspectedFields;
private final String inspectedFlow;
public ChangeResultInspector(Set> inspectedFields, String inspectedFlow) {
this.inspectedFields = inspectedFields;
this.inspectedFlow = inspectedFlow;
}
public void inspect(Map extends Identifier, CurrentEntityState> originalState, ChangeResult results, List> observedResults) {
Iterator extends EntityChangeResult> resultIterator = results.iterator();
Iterator> observedResultIterator = observedResults.iterator();
while(resultIterator.hasNext() && observedResultIterator.hasNext()) {
EntityChangeResult result = resultIterator.next();
ObservedResult observedResult = observedResultIterator.next();
if(result.isSuccess() && observedResult.isSuccess()) {
inspectResult(originalState.get(result.getIdentifier()), result, observedResult);
} else if(result.isSuccess()){
logResultWarning(observedResult.getIdentifier(), observedResult.getErrorCode().orElse(""), true, false);
observedResult.setInspectedStatus(ObservedResult.InspectedStatus.LEGACY_ERROR_MISMATCH);
} else if(observedResult.isSuccess()) {
Optional firstError = result.getErrors().stream().findAny();
logResultWarning(observedResult.getIdentifier(), firstError.orElse(UNEXPECTED_ERROR).getErrorCode(), false, true);
observedResult.setInspectedStatus(ObservedResult.InspectedStatus.PERSISTENCE_ERROR_MISMATCH);
}
}
}
public static void logException(String inspectedFlow, Throwable t) {
logger.warn("Change result inspector can't inspect keyword changes for flow " + inspectedFlow, t);
}
private void inspectResult(CurrentEntityState originalEntity, EntityChangeResult result, ObservedResult observedResult) {
ChangeEntityCommand command = result.getCommand();
inspectedFields.forEach(field -> {
boolean fieldChanged = command.isFieldChanged(field);
boolean containsObservedField = observedResult.containsField(field);
if(fieldChanged && containsObservedField) {
Object value = getValue(command, field);
Object observedValue = getValue(observedResult, field);
if(!Objects.equals(value, observedValue)) {
logFieldWarning(observedResult.getIdentifier(), field.toString(), value, observedValue);
observedResult.setInspectedStatus(ObservedResult.InspectedStatus.VALUE_MISMATCH);
}
} else if(fieldChanged) {
Object value = getValue(command, field);
logFieldWarning(observedResult.getIdentifier(), field.toString(), value, VALUE_NOT_FOUND);
observedResult.setInspectedStatus(ObservedResult.InspectedStatus.VALUE_MISMATCH);
} else if(containsObservedField) {
Object originalValue = originalEntity.containsField(field) ? getValue(originalEntity, field) : null;
Object observedValue = getValue(observedResult, field);
if(!Objects.equals(originalValue, observedValue)) {
logFieldWarning(observedResult.getIdentifier(), field.toString(), VALUE_NOT_FOUND, observedValue);
observedResult.setInspectedStatus(ObservedResult.InspectedStatus.VALUE_MISMATCH);
}
}
});
}
private Object getValue(CurrentEntityState fieldsValueMap, EntityField field) {
return field.getDbAdapter().getDbValues(fieldsValueMap.get(field)).collect(Collectors.toList()).get(0);
}
private Object getValue(FieldsValueMap fieldsValueMap, EntityField field) {
return field.getDbAdapter().getDbValues(fieldsValueMap.get(field)).collect(Collectors.toList()).get(0);
}
private void logFieldWarning(Identifier identifier, String fieldName, Object value, Object observedValue) {
logger.warn("Change result inspector reports different value result for flow {} and identifier {} for field {} : legacy {}, persistent {}", inspectedFlow, identifier.toString(), fieldName, observedValue, value);
}
private void logResultWarning(Identifier identifier, String errorCode, boolean success, boolean inspectedSuccess) {
logger.warn("Change result inspector reports validation mismatch for flow {} and identifier {} : legacy result [{}], persistent result [{}], error {}", inspectedFlow, identifier.toString(), inspectedSuccess, success, errorCode);
}
public static class Builder> {
private Set> inspectedFields = Sets.newHashSet();
private String inspectedFlow;
public final Builder withInspectedFields(Collection> fields) {
inspectedFields.addAll(fields);
return this;
}
public Builder inspectedFlow(String inspectedFlow) {
this.inspectedFlow = inspectedFlow;
return this;
}
public ChangeResultInspector build() {
return new ChangeResultInspector<>(inspectedFields, inspectedFlow);
}
}
}