Example usage for org.apache.commons.collections IteratorUtils toList

List of usage examples for org.apache.commons.collections IteratorUtils toList

Introduction

In this page you can find the example usage for org.apache.commons.collections IteratorUtils toList.

Prototype

public static List toList(Iterator iterator) 

Source Link

Document

Gets a list based on an iterator.

Usage

From source file:org.pentaho.metaverse.api.analyzer.kettle.ExternalResourceCacheTest.java

@Test
public void test_caching() {
    initMetas();/*w  w  w.  j a v  a 2s  .co  m*/

    assertNull(ExternalResourceCache.getInstance().get(null, null));
    assertEquals(0, ExternalResourceCache.getInstance().transMap.size());
    assertEquals(0, ExternalResourceCache.getInstance().resourceMap.size());

    assertNull(ExternalResourceCache.getInstance().get(trans, null));
    assertEquals(0, ExternalResourceCache.getInstance().transMap.size());
    assertEquals(0, ExternalResourceCache.getInstance().resourceMap.size());

    assertNull(ExternalResourceCache.getInstance().get(trans, meta));
    assertEquals(1, ExternalResourceCache.getInstance().transMap.size());
    assertEquals(0, ExternalResourceCache.getInstance().resourceMap.size());

    // the cache contains sets, make sure another copy of the same trans is not added
    assertNull(ExternalResourceCache.getInstance().get(trans, meta));
    assertEquals(1, ExternalResourceCache.getInstance().transMap.size());
    List<ExternalResourceCache.TransValues> cachedTransformations = IteratorUtils
            .toList(ExternalResourceCache.getInstance().transMap.values().iterator());
    assertEquals(1, cachedTransformations.size());
    assertEquals(1, cachedTransformations.get(0).size());
    assertTrue(cachedTransformations.get(0).contains(trans));
    assertEquals(0, ExternalResourceCache.getInstance().resourceMap.size());

    assertNull(ExternalResourceCache.getInstance().get(trans2, meta));
    assertEquals(1, ExternalResourceCache.getInstance().transMap.size());
    cachedTransformations = IteratorUtils
            .toList(ExternalResourceCache.getInstance().transMap.values().iterator());
    assertEquals(1, cachedTransformations.size());
    assertEquals(2, cachedTransformations.get(0).size());
    assertTrue(cachedTransformations.get(0).contains(trans));
    assertTrue(cachedTransformations.get(0).contains(trans2));
    assertEquals(0, ExternalResourceCache.getInstance().resourceMap.size());

    final ExternalResourceCache.ExternalResourceValues resources = ExternalResourceCache.getInstance()
            .newExternalResourceValues();
    resources.add(Mockito.mock(IExternalResourceInfo.class));

    // cache some resources
    ExternalResourceCache.getInstance().cache(trans, meta, resources);
    assertEquals(1, ExternalResourceCache.getInstance().transMap.size());
    cachedTransformations = IteratorUtils
            .toList(ExternalResourceCache.getInstance().transMap.values().iterator());
    assertEquals(1, cachedTransformations.size());
    assertEquals(2, cachedTransformations.get(0).size());
    assertTrue(cachedTransformations.get(0).contains(trans));
    assertTrue(cachedTransformations.get(0).contains(trans2));
    assertEquals(1, ExternalResourceCache.getInstance().resourceMap.size());

    // remote trans from the cache
    ExternalResourceCache.getInstance().removeCachedResources(trans);
    // trans2 should still exist in the map
    assertEquals(1, ExternalResourceCache.getInstance().transMap.size());
    cachedTransformations = IteratorUtils
            .toList(ExternalResourceCache.getInstance().transMap.values().iterator());
    assertEquals(1, cachedTransformations.size());
    assertEquals(1, cachedTransformations.get(0).size());
    assertTrue(cachedTransformations.get(0).contains(trans2));
    // resources should not be removed as they are still being used by trans2
    assertEquals(1, ExternalResourceCache.getInstance().resourceMap.size());

    // remove trans2 from the cache
    ExternalResourceCache.getInstance().removeCachedResources(trans2);
    // transMap and resourceMap should now both be empty
    assertEquals(0, ExternalResourceCache.getInstance().transMap.size());
    assertEquals(0, ExternalResourceCache.getInstance().resourceMap.size());
}

From source file:org.pentaho.metaverse.BaseMetaverseValidationIT.java

/**
 * Call in the child class's BeforeClass method.
 *//*www .  ja v a 2s  .c  o m*/
public static void init(final String rootTestFolder, final String targetOutputFile) throws Exception {
    IntegrationTestUtil.initializePentahoSystem("src/it/resources/solution/system/pentahoObjects.spring.xml");

    // we only care about the demo folder
    FileSystemLocator fileSystemLocator = PentahoSystem.get(FileSystemLocator.class);
    IDocumentLocatorProvider provider = PentahoSystem.get(IDocumentLocatorProvider.class);
    // remove the original locator so we can set the modified one back on it
    provider.removeDocumentLocator(fileSystemLocator);
    fileSystemLocator.setRootFolder(rootTestFolder);
    provider.addDocumentLocator(fileSystemLocator);

    MetaverseUtil.setDocumentController(PentahoSystem.get(IDocumentController.class));

    // build the graph using our updated locator/provider
    graph = IntegrationTestUtil.buildMetaverseGraph(provider);
    reader = PentahoSystem.get(IMetaverseReader.class);

    framedGraphFactory = new FramedGraphFactory(new GremlinGroovyModule(), new JavaHandlerModule());
    framedGraph = framedGraphFactory.create(graph);
    root = (RootNode) framedGraph.getVertex("entity", RootNode.class);
    final List<Vertex> allVertices = IteratorUtils.toList(framedGraph.getVertices().iterator());
    for (final Vertex vertex : allVertices) {
        final String vertexId = vertex.getId().toString();
        if (vertexId.startsWith("entity_")) {
            entityNodes.put(vertexId.replace("entity_", ""),
                    (Concept) framedGraph.getVertex(vertexId, Concept.class));
        }
    }

    File exportFile = new File(targetOutputFile);
    FileUtils.writeStringToFile(exportFile, reader.exportToXml(), "UTF-8");
}

From source file:org.pentaho.metaverse.BaseMetaverseValidationIT.java

/**
 * Verifies that the transformation node with the given name exists in the graph.
 *
 * @param transformationName the transformation node name
 * @param isSubTrans         true if looking for a node corresponding to a sub-transformation (executed by some step),
 *                           false otherwise
 * @return the verified {@link TransformationNode}
 *///  w ww.  j a  v a2 s.  c o  m
protected TransformationNode verifyTransformationNode(final String transformationName,
        final boolean isSubTrans) {
    // verify the existence of two transformation nodes - one for the injector and one for the sub-transformation
    final List<TransformationNode> allTransformations = IteratorUtils
            .toList(root.getTransformations().iterator());
    final TransformationNode node = isSubTrans ? root.getSubTransformation(transformationName)
            : root.getTransformation(transformationName);
    assertNotNull(node);
    assertTrue(allTransformations.contains(node));
    assertEquals("{\"name\":\"" + REPO_ID + "\",\"type\":\"Locator\"}",
            node.getProperty(PROPERTY_NAMESPACE).toString());
    return node;
}

From source file:org.pentaho.metaverse.BaseMetaverseValidationIT.java

/**
 * Verifies that the {@link TransformationNode} contains the expected steps.
 *
 * @param transNode the {@link TransformationNode}
 * @param stepNames the expected non-virtual step names
 * @param isVirtual is true, we verify virtual steps, if false, we verify non-virtual steps
 * @return a {@link Map} of step name to stepNode, for convenient lookup by the caller.
 *///from ww  w .j a  va 2 s .  c  o  m
protected Map<String, FramedMetaverseNode> verifyTransformationSteps(final TransformationNode transNode,
        final String[] stepNames, final boolean isVirtual) {

    final List<FramedMetaverseNode> stepNodes = IteratorUtils
            .toList((isVirtual ? transNode.getVirtualStepNodes() : transNode.getStepNodes()).iterator());
    assertEquals(stepNames == null ? 0 : stepNames.length, stepNodes.size());
    // check that all the expected step nodes exist and create a node map on the fly for future use
    final Map<String, FramedMetaverseNode> stepNodeMap = verifyNodeNames(stepNodes,
            DictionaryConst.NODE_TYPE_TRANS_STEP, stepNames);

    String transPath = transNode.getProperty(PROPERTY_PATH).toString();
    transPath = KettleAnalyzerUtil.normalizeFilePathSafely(transPath);

    // traverse each step node and verify namespace
    for (final FramedMetaverseNode stepNode : stepNodes) {
        final String stepNamespace = stepNode.getProperty(PROPERTY_NAMESPACE).toString()
                .replaceAll("\\\\/", "/").replaceAll("\\\\\\\\", "\\\\");
        final String expectedStepNamespace = "{\"namespace\":{\"name\":\"" + REPO_ID
                + "\",\"type\":\"Locator\"}," + "\"path\":\"" + transPath + "\",\"type\":\"Transformation\"}";
        assertEquals(expectedStepNamespace, stepNamespace);
    }
    return stepNodeMap;
}

From source file:org.pentaho.metaverse.BaseMetaverseValidationIT.java

protected void verifyNodesTypes(final Map<String, List<String>> nodeTypeMap) {

    final List<Map.Entry<String, List<String>>> nodeTypeMapList = IteratorUtils
            .toList(nodeTypeMap.entrySet().iterator());
    for (final Map.Entry<String, List<String>> nodeTypeMapEntry : nodeTypeMapList) {
        final String nodeType = nodeTypeMapEntry.getKey();
        // get the entity node corresponding to this type
        final Concept entityNode = entityNodes.get(nodeType);
        assertNotNull(String.format("Entity node for type '%s' not found", nodeType), entityNode);
        final List<String> nodeNames = nodeTypeMapEntry.getValue();
        final List<FramedMetaverseNode> nodes = IteratorUtils.toList(entityNode.getConcreteNodes().iterator());
        verifyNodeNames(nodes, nodeType, nodeNames);
    }//from   w w  w.j a  va 2 s  . c o  m
}

From source file:org.pentaho.metaverse.BaseMetaverseValidationIT.java

/**
 * Verifies that the inputs and output of the given {@code stepNode} are linked correctly.
 *
 * @param stepNode a {@link TransformationStepNode}
 * @param links    a {@link List} of {@link TestLineageLink}s
 *///from w ww .  j  ava2s.c om
protected void verifyStepIOLinks(final TransformationStepNode stepNode, final List<TestLineageLink> links) {

    final List<FramedMetaverseNode> inputs = IteratorUtils.toList(stepNode.getInputStreamFields().iterator());
    final List<FramedMetaverseNode> outputs = IteratorUtils.toList(stepNode.getOutputStreamFields().iterator());
    final String stepName = stepNode.getProperty(PROPERTY_NAME).toString();
    final String stepNamespace = stepNode.getProperty(PROPERTY_NAMESPACE).toString();

    for (final TestLineageLink link : links) {
        // input node
        FramedMetaverseNode inputNode = findNode(inputs, link.getInputNode());
        assertNotNull(inputNode);
        // output node
        FramedMetaverseNode outptuNode = findNode(outputs, link.getOutputNode());
        assertNotNull(outptuNode);
        // verify namespace on stream fields (file and db field namespace will be different)
        final String outputNodeType = outptuNode.getProperty(PROPERTY_TYPE).toString();
        if (NODE_TYPE_TRANS_FIELD.equals(outputNodeType)) {
            final String actualNamespace = outptuNode.getProperty(PROPERTY_NAMESPACE).toString();
            final String expectedNamespace = "{\"name\":\"" + stepName + "\",\"namespace\":" + stepNamespace
                    + ",\"type" + "\":\"" + NODE_TYPE_TRANS_STEP + "\"}";
            assertEquals(expectedNamespace, actualNamespace);
        }

        final List<Concept> linkedNodes = IteratorUtils
                .toList(inputNode.getOutNodes(link.getLinkLabel()).iterator());
        assertTrue(linkedNodes.size() > 0);
        // find a match
        boolean matchFound = false;
        for (final Concept linkedNode : linkedNodes) {
            if (linkedNode.equals(outptuNode)) {
                matchFound = true;
                break;
            }
        }
        assertTrue(matchFound);
    }
}

From source file:org.pentaho.metaverse.BaseMetaverseValidationIT.java

public List<FramedMetaverseNode> verifyLinkedNodes(final FramedMetaverseNode fromNode, final String linkLabel,
        final String toNodeName) {
    final List<FramedMetaverseNode> outNodes = IteratorUtils.toList(fromNode.getOutNodes(linkLabel).iterator());
    List<FramedMetaverseNode> searchNodes = new ArrayList();
    for (final FramedMetaverseNode outNode : outNodes) {
        if (outNode.getName().equals(toNodeName)) {
            searchNodes.add(outNode);//from  w w  w .  j a va2 s. c om
        }
    }
    assertNotEquals(0, searchNodes.size());
    return searchNodes;
}

From source file:org.pentaho.metaverse.BaseMetaverseValidationIT.java

public void verifyNodeProperties(final FramedMetaverseNode node, final Map<String, Object> expectedProperties) {
    final List<String> propertyNames = IteratorUtils.toList(node.getPropertyNames().iterator());
    final Set<String> encounteredProperties = new HashSet();
    final Set<String> badProperties = new HashSet();
    for (final String propertyName : propertyNames) {
        final Object expectedValue = expectedProperties.get(propertyName);
        final Object actualValue = node.getProperty(propertyName);
        if (actualValue == null) {
            badProperties.add(propertyName);
            continue;
        }/*w w  w  . j a  v a  2  s .co  m*/
        if (!SKIP.equals(expectedValue)) {
            assertEquals(String.format("Incorrect value for property '%s'", propertyName),
                    toSafeStringSafely(expectedValue), toSafeStringSafely(actualValue));
        }
        encounteredProperties.add(propertyName);
    }
    final Set<String> missingProperties = new HashSet(expectedProperties.keySet());
    missingProperties.removeAll(encounteredProperties);
    assertEquals(String.format("Missing expected properties: %s", String.join(",", missingProperties)), 0,
            missingProperties.size());
}

From source file:org.pentaho.metaverse.graph.BaseGraphWriter.java

private static void mergeFields(final Graph graph, final Vertex documentElementVertex,
        final Direction direction, final String linkLabel, boolean isTransformationField) {
    // get all edges corresponding to the requested direction and with the requested label
    final List<Edge> links = IteratorUtils
            .toList(documentElementVertex.getEdges(direction, linkLabel).iterator());
    // traverse the links and see if there are any that point to fields with the same names, if so, they need to be
    // merged//from   w  w w  .  jav a2s.co m
    final Map<String, Set<Vertex>> fieldMap = new HashMap();
    for (final Edge link : links) {
        // get the vertex at the "other" end of this linnk ("this" end being the vertex itself)
        final Vertex vertex = link.getVertex(direction == Direction.IN ? Direction.OUT : Direction.IN);
        final String category = vertex.getProperty(DictionaryConst.PROPERTY_CATEGORY);
        final String type = vertex.getProperty(DictionaryConst.PROPERTY_TYPE);
        // verify that the vertex is a field of the desired type
        if (DictionaryConst.CATEGORY_FIELD.equals(category)
                && isTransformationField == DictionaryConst.NODE_TYPE_TRANS_FIELD.equals(type)) {
            final String fieldName = vertex.getProperty(DictionaryConst.PROPERTY_NAME);

            Set<Vertex> fieldsWithSameName = fieldMap.get(fieldName);
            if (fieldsWithSameName == null) {
                fieldsWithSameName = new HashSet();
                fieldMap.put(fieldName, fieldsWithSameName);
            }
            fieldsWithSameName.add(vertex);
        }
    }

    // traverse the map pf fields - for any field name, if more than one has been found, merge them into one
    final List<Map.Entry<String, Set<Vertex>>> fields = IteratorUtils.toList(fieldMap.entrySet().iterator());
    for (final Map.Entry<String, Set<Vertex>> fieldEntry : fields) {
        final List<Vertex> fieldVertices = new ArrayList(fieldEntry.getValue());
        if (fieldVertices.size() > 1) {
            // get the first vertex - we will keep this one and re-point links connected to all the rest back to this
            // original one, and then remove the remaining ones
            final Vertex fieldVertexToKeep = fieldVertices.get(0);
            for (int i = 1; i < fieldVertices.size(); i++) {
                final Vertex fieldVertexToMerge = fieldVertices.get(i);
                rewireEdges(graph, fieldVertexToKeep, fieldVertexToMerge, Direction.IN);
                rewireEdges(graph, fieldVertexToKeep, fieldVertexToMerge, Direction.OUT);
                // we can now safely remove 'fieldVertexToMerge'
                fieldVertexToMerge.remove();
            }
        }
    }
}

From source file:org.pentaho.metaverse.MetaverseValidationIT.java

public void testTableOutputStepNode(final TableOutputStepNode node, final String expectedTableName)
        throws Exception {

    // check the table that it writes to
    TableOutputMeta meta = (TableOutputMeta) getStepMeta(node);
    String schema = meta.getSchemaName();
    boolean truncateTable = meta.truncateTable();
    DatabaseTableNode databaseTableNode = node.getDatabaseTable();
    assertEquals(expectedTableName, databaseTableNode.getName());
    assertEquals(schema, node.getSchema());
    assertEquals(truncateTable, node.isTruncateTable());

    Iterable<StreamFieldNode> inputs = node.getInputStreamFields();
    Iterable<StreamFieldNode> outputs = node.getOutputStreamFields();

    assertEquals(getIterableSize(inputs) + meta.getFieldDatabase().length, getIterableSize(outputs));

    for (StreamFieldNode input : inputs) {
        assertEquals(input.getName(),/*  ww w .  j ava  2 s .  c  o m*/
                ((FramedMetaverseNode) IteratorUtils.toList(input.getNodesPopulatedByMe().iterator()).get(0))
                        .getName());
    }

    DatasourceNode datasource = node.getDatasource(meta.getDatabaseMeta().getName());
    assertEquals(meta.getDatabaseMeta().getHostname(), datasource.getHost());
    assertEquals(meta.getDatabaseMeta().getDatabasePortNumberString(), datasource.getPort());
    assertEquals(meta.getDatabaseMeta().getUsername(), datasource.getUserName());
    assertEquals(meta.getDatabaseMeta().getDatabaseName(), datasource.getDatabaseName());
    assertEquals(DictionaryConst.NODE_TYPE_DATASOURCE, datasource.getType());

    assertEquals(DictionaryConst.NODE_TYPE_DATA_TABLE, databaseTableNode.getType());

}