net.n2oapp.framework.config.selective.ION2oMetadataTester Maven / Gradle / Ivy
package net.n2oapp.framework.config.selective;
import net.n2oapp.framework.api.metadata.aware.NamespaceUriAware;
import net.n2oapp.framework.api.metadata.io.NamespaceIO;
import net.n2oapp.framework.api.pack.MetadataPack;
import net.n2oapp.framework.api.pack.XmlIOBuilder;
import net.n2oapp.framework.config.util.FileSystemUtil;
import org.springframework.core.io.ClassPathResource;
import java.util.function.Consumer;
import java.util.stream.Stream;
/**
* Сервис тестирования чтения/записи метаданных
*/
public class ION2oMetadataTester implements XmlIOBuilder {
private XmlIOReader xmlIOReader = new XmlIOReader();
public ION2oMetadataTester() {
}
public ION2oMetadataTester(XmlIOReader xmlIOReader) {
this.xmlIOReader = xmlIOReader;
}
public ION2oMetadataTester addIO(NamespaceIO io) {
this.xmlIOReader.addIO(io);
return this;
}
public ION2oMetadataTester addPack(MetadataPack super ION2oMetadataTester> pack) {
pack.build(this);
return this;
}
public boolean isCheck(String path, Consumer checker) {
String source = FileSystemUtil.getContentFromResource(new ClassPathResource(path));
T t = xmlIOReader.read(source);
if (checker != null)
checker.accept(t);
return xmlIOReader.persistAndCompareWithSample(t, source);
}
public boolean check(String path, Consumer checker) {
boolean check = isCheck(path, checker);
assert check;
return true;
}
public boolean check(String path) {
assert isCheck(path, null);
return true;
}
@Override
@SafeVarargs
public final ION2oMetadataTester ios(NamespaceIO extends NamespaceUriAware>... ios) {
Stream.of(ios).forEach(this::addIO);
return this;
}
@Override
@SafeVarargs
public final ION2oMetadataTester packs(MetadataPack super ION2oMetadataTester>... packs) {
Stream.of(packs).forEach(p -> p.build(this));
return this;
}
}