AggregationHashChainBuilder { public AggregationHashChain build(TreeNode leaf) throws KSIException { Util.notNull(leaf, "TreeNode"); if (!leaf.isLeaf() || leaf.isRoot() || leaf instanceof MetadataNode) { throw new IllegalArgumentException("Aggregation hash chain can be built only from leaf nodes"); } LinkedList<AggregationChainLink> links = new LinkedList<>(); long levelCorrection = 0L; TreeNode node; if (leaf.getParent().getRightChildNode() instanceof MetadataNode) { links.add(createMetadataChainLink((MetadataNode) leaf.getParent().getRightChildNode(), leaf.getLevel())); node = leaf.getParent(); } else { node = leaf; levelCorrection = node.getLevel(); } createChainLinks(links, levelCorrection, node); LinkedList<Long> chainIndex = new LinkedList<>(singletonList(calculateIndex(links))); HashAlgorithm aggregationAlgorithm = new DataHash(leaf.getParent().getValue()).getAlgorithm(); return SIGNATURE_COMPONENT_FACTORY.createAggregationHashChain( new DataHash(leaf.getValue()), new Date(), chainIndex, links, aggregationAlgorithm); }  AggregationHashChain build(TreeNode leaf);  }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Aggregation hash chain can be built only from leaf nodes") public void testCreateAggregationHashChainFromNonLeafNode_throwsIllegalArgumentException() throws KSIException { HashTreeBuilder treeBuilder = new HashTreeBuilder(HashAlgorithm.SHA2_256); ImprintNode node1 = new ImprintNode(new DataHash(Base16.decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2"))); ImprintNode node2 = new ImprintNode(new DataHash(Base16.decode("018D982C6911831201C5CF15E937514686A2169E2AD57BA36FD92CBEBD99A67E34"))); treeBuilder.add(node1, node2); new AggregationHashChainBuilder().build(node1.getParent()); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Aggregation hash chain can be built only from leaf nodes") public void testCreateAggregationHashChainFromRootNode_throwsIllegalArgumentException() throws KSIException { ImprintNode node = new ImprintNode(new DataHash(Base16.decode("01580192B0D06E48884432DFFC26A67C6C685BEAF0252B9DD2A0B4B05D1724C5F2"))); new AggregationHashChainBuilder().build(node); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Aggregation hash chain can be built only from leaf nodes") public void testCreateAggregationHashChainFromMetadataNode_throwsIllegalArgumentException() throws KSIException { MetadataNode node = new MetadataNode(new byte[]{1}, 0); new AggregationHashChainBuilder().build(node); }