package org.gcube.informationsystem.resourceregistry.api.contexts; import java.io.File; import java.io.IOException; import java.net.URL; import java.nio.charset.Charset; import java.nio.file.Files; import java.util.List; import java.util.UUID; import org.gcube.informationsystem.contexts.reference.entities.Context; import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException; import org.gcube.informationsystem.serialization.ElementMapper; import org.gcube.informationsystem.tree.Node; import org.gcube.informationsystem.tree.NodeElaborator; import org.gcube.informationsystem.tree.Tree; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author Luca Frosini (ISTI - CNR) */ public class ContextCacheTest{ private static final Logger logger = LoggerFactory.getLogger(ContextCacheTest.class); protected File getTypesDirectory() throws Exception { URL logbackFileURL = ContextCacheTest.class.getClassLoader().getResource("logback-test.xml"); File logbackFile = new File(logbackFileURL.toURI()); File resourcesDirectory = logbackFile.getParentFile(); return new File(resourcesDirectory, "contexts"); } protected String readFile(File file) throws IOException { byte[] encoded = Files.readAllBytes(file.toPath()); return new String(encoded, Charset.defaultCharset()); } protected List getContexts() throws Exception { File typesDirectory = getTypesDirectory(); File file = new File(typesDirectory, "contexts.json"); String json = readFile(file); List contexts = ElementMapper.unmarshalList(Context.class, json); return contexts; } @Test public void test() throws Exception { List contexts = getContexts(); ContextCache contextCache = new ContextCache(); contextCache.setContexts(contexts); contextCache.setContextCacheRenewal(new ContextCacheRenewal() { @Override public List renew() throws ResourceRegistryException { try { return getContexts(); }catch (Exception e) { throw new ResourceRegistryException(e); } } }); Tree contextTree = contextCache.getContextsTree(); contextTree.elaborate(new NodeElaborator() { @Override public void elaborate(Node node, int level) throws Exception { StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i < level; ++i) { stringBuffer.append(Node.INDENTATION); } Context context = node.getNodeElement(); UUID uuid = context.getID(); String fullName = contextCache.getContextFullNameByUUID(uuid); logger.info("{}- {} (ID:{})", stringBuffer.toString(), fullName, uuid); } }); } }