Search in sources :

Example 1 with EncodedHash

use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class WhenAssemblingSips method assertPackagingInformation.

private void assertPackagingInformation(ZipInputStream zip, Collection<Object> objects, EncodedHash pdiHash) throws IOException {
    ZipEntry entry = zip.getNextEntry();
    assertNotNull(entry, "Missing Packaging Information");
    assertEquals("eas_sip.xml", entry.getName(), "Zip entry");
    try (ByteArrayInputOutputStream packagingInformation = new ByteArrayInputOutputStream()) {
        IOUtils.copy(zip, packagingInformation);
        Element sipElement = assertValidXml(packagingInformation.getInputStream(), "PackagingInformation", "sip.xsd").getDocumentElement();
        assertTrue(XmlUtil.namedElementsIn(sipElement, "pdi_hash").filter(e -> equals(pdiHash, e)).findAny().isPresent(), "Missing pdi_hash: " + pdiHash);
        String aiuCount = XmlUtil.getFirstChildElement(sipElement, "aiu_count").getTextContent();
        assertEquals(objects.size(), Integer.parseInt(aiuCount), "# AIUs");
    }
    zip.closeEntry();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) ByteArrayInputOutputStream(com.opentext.ia.sdk.support.io.ByteArrayInputOutputStream) ZipInputStream(java.util.zip.ZipInputStream) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) EncodedHash(com.opentext.ia.sdk.support.io.EncodedHash) ByteArrayInputStream(java.io.ByteArrayInputStream) Map(java.util.Map) Encoding(com.opentext.ia.sdk.support.io.Encoding) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ZipEntry(java.util.zip.ZipEntry) Iterator(java.util.Iterator) DataBuffer(com.opentext.ia.sdk.support.io.DataBuffer) Collection(java.util.Collection) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) XmlUtil(com.opentext.ia.sdk.support.xml.XmlUtil) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) IOUtils(org.apache.commons.io.IOUtils) HashFunction(com.opentext.ia.sdk.support.io.HashFunction) List(java.util.List) Element(org.w3c.dom.Element) NoHashAssembler(com.opentext.ia.sdk.support.io.NoHashAssembler) MemoryBuffer(com.opentext.ia.sdk.support.io.MemoryBuffer) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Collections(java.util.Collections) HashAssembler(com.opentext.ia.sdk.support.io.HashAssembler) Mockito.mock(org.mockito.Mockito.mock) ZipEntry(java.util.zip.ZipEntry) Element(org.w3c.dom.Element) ByteArrayInputOutputStream(com.opentext.ia.sdk.support.io.ByteArrayInputOutputStream)

Example 2 with EncodedHash

use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class ContentAssemblerDefault method addContent.

protected synchronized ContentInfo addContent(String ri, DigitalObject digitalObject) throws IOException {
    if (zip == null) {
        throw new IllegalStateException("Missing zip; did youc call begin()?");
    }
    try (InputStream stream = digitalObject.get()) {
        String referenceInformation = digitalObject.getReferenceInformation();
        Collection<EncodedHash> hashes;
        long numBytesHashed;
        synchronized (zip) {
            hashes = zip.addEntry(referenceInformation, stream, contentHashAssembler);
            numBytesHashed = contentHashAssembler.numBytesHashed();
        }
        incMetric(SipMetrics.SIZE_DIGITAL_OBJECTS, numBytesHashed);
        return new ContentInfo(ri, hashes);
    }
}
Also used : EncodedHash(com.opentext.ia.sdk.support.io.EncodedHash) InputStream(java.io.InputStream)

Example 3 with EncodedHash

use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class WhenAssemblingSips method shouldZipContentsAndReportMetrics.

@Test
void shouldZipContentsAndReportMetrics() throws IOException {
    Assembler<HashedContents<Object>> pdiAssembler = mock(Assembler.class);
    HashAssembler pdiHashAssembler = mock(HashAssembler.class);
    EncodedHash hash = someHash();
    when(pdiHashAssembler.get()).thenReturn(Collections.singletonList(hash));
    long pdiSize = randomInt(7, 128);
    when(pdiHashAssembler.numBytesHashed()).thenReturn(pdiSize);
    Object object1 = "object1_" + randomString(8);
    Object object2 = "object2_" + randomString(8);
    Collection<Object> objects = Arrays.asList(object1, object2);
    DigitalObjectsExtraction<Object> contentsExtraction = mock(DigitalObjectsExtraction.class);
    String id1a = randomString(8);
    String id1b = randomString(8);
    String id2 = randomString(8);
    List<? extends DigitalObject> digitalObjects1 = Arrays.asList(someContentDataObject(id1a), someContentDataObject(id1b));
    List<? extends DigitalObject> digitalObjects2 = Collections.singletonList(someContentDataObject(id2));
    when(contentsExtraction.apply(object1)).thenAnswer(invocation -> digitalObjects1.iterator());
    when(contentsExtraction.apply(object2)).thenAnswer(invocation -> digitalObjects2.iterator());
    HashAssembler contentHashAssembler = mock(HashAssembler.class);
    Collection<EncodedHash> hashes1a = Collections.singletonList(someHash());
    Collection<EncodedHash> hashes1b = Collections.singletonList(someHash());
    Collection<EncodedHash> hashes2 = Collections.singletonList(someHash());
    Iterator<Collection<EncodedHash>> hashes = Arrays.asList(hashes1a, hashes1b, hashes2).iterator();
    when(contentHashAssembler.get()).thenAnswer(invocation -> hashes.next());
    long digitalObjectSize = randomInt(5, 255);
    when(contentHashAssembler.numBytesHashed()).thenReturn(digitalObjectSize);
    Map<String, ContentInfo> hashesById1 = new HashMap<>();
    hashesById1.put(id1a, new ContentInfo(id1a, hashes1a));
    hashesById1.put(id1b, new ContentInfo(id1b, hashes1b));
    Map<String, ContentInfo> hashesById2 = Collections.singletonMap(id2, new ContentInfo(id2, hashes2));
    PackagingInformation packagingInformationPrototype = somePackagingInformation();
    SipAssembler<Object> sipAssembler = SipAssembler.forPdiAndContentWithHashing(packagingInformationPrototype, pdiAssembler, pdiHashAssembler, contentsExtraction, contentHashAssembler);
    DataBuffer buffer = new MemoryBuffer();
    long time = System.currentTimeMillis();
    sipAssembler.start(buffer);
    for (Object object : objects) {
        sipAssembler.add(object);
    }
    sipAssembler.end();
    time = System.currentTimeMillis() - time;
    verify(pdiAssembler).start(any(DataBuffer.class));
    verify(pdiAssembler).add(eq(new HashedContents<>(object1, hashesById1)));
    verify(pdiAssembler).add(eq(new HashedContents<>(object2, hashesById2)));
    verify(pdiAssembler).end();
    try (ZipInputStream zip = new ZipInputStream(buffer.openForReading())) {
        assertContentDataObject(zip, id1a);
        assertContentDataObject(zip, id1b);
        assertContentDataObject(zip, id2);
        assertPreservationDescriptionInformation(zip);
        assertPackagingInformation(zip, objects, hash);
        assertNull(zip.getNextEntry(), "Additional zip entries");
    }
    SipMetrics metrics = sipAssembler.getMetrics();
    assertEquals(2, metrics.numAius(), SipMetrics.NUM_AIUS);
    assertEquals(3, metrics.numDigitalObjects(), SipMetrics.NUM_DIGITAL_OBJECTS);
    assertEquals(time, metrics.assemblyTime(), DELTA_MS, SipMetrics.ASSEMBLY_TIME);
    assertEquals(3 * digitalObjectSize, metrics.digitalObjectsSize(), SipMetrics.SIZE_DIGITAL_OBJECTS);
    assertEquals(pdiSize, metrics.pdiSize(), SipMetrics.SIZE_PDI);
    // long packagingInformationSize =
    // getPackagingInformationSize(packagingInformationPrototype, 2, Optional.of(hash));
    // assertEquals(pdiSize + 3 * digitalObjectSize + packagingInformationSize, metrics.sipSize(),
    // SipMetrics.SIZE_SIP);
    assertEquals(buffer.length(), metrics.sipFileSize(), SipMetrics.SIZE_SIP_FILE);
}
Also used : EncodedHash(com.opentext.ia.sdk.support.io.EncodedHash) NoHashAssembler(com.opentext.ia.sdk.support.io.NoHashAssembler) HashAssembler(com.opentext.ia.sdk.support.io.HashAssembler) HashMap(java.util.HashMap) MemoryBuffer(com.opentext.ia.sdk.support.io.MemoryBuffer) ZipInputStream(java.util.zip.ZipInputStream) Collection(java.util.Collection) DataBuffer(com.opentext.ia.sdk.support.io.DataBuffer) Test(org.junit.jupiter.api.Test)

Example 4 with EncodedHash

use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class WhenWorkingWithPackingInformation method shouldAcceptSetValues.

@Test
void shouldAcceptSetValues() {
    DataSubmissionSession dss = mock(DataSubmissionSession.class);
    Date productionDate = mock(Date.class);
    int sequenceNumber = randomInt(0, Integer.MAX_VALUE);
    boolean isLast = true;
    long aiuCount = randomInt(0, Integer.MAX_VALUE);
    long pageCount = randomInt(0, Integer.MAX_VALUE);
    Optional<EncodedHash> pdiHash = Optional.empty();
    PackagingInformation pi = new PackagingInformation(dss, productionDate, sequenceNumber, isLast, aiuCount, pageCount, pdiHash);
    long newAiuCount = randomInt(0, Integer.MAX_VALUE);
    pi.setAiuCount(newAiuCount);
    assertEquals(newAiuCount, pi.getAiuCount(), "AIU Count is changed.");
    long newPageCount = randomInt(0, Integer.MAX_VALUE);
    pi.setPageCount(newPageCount);
    assertEquals(newPageCount, pi.getPageCount(), "Page Count is changed.");
}
Also used : EncodedHash(com.opentext.ia.sdk.support.io.EncodedHash) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 5 with EncodedHash

use of com.opentext.ia.sdk.support.io.EncodedHash in project infoarchive-sip-sdk by Enterprise-Content-Management.

the class ContentAssemblerWithDedupOnHash method addContent.

@Override
protected ContentInfo addContent(String ri, DigitalObject digitalObject) throws IOException {
    RepeatableInputStream memoryStream = memoryStreamOf(digitalObject);
    // First compute hashes
    Collection<EncodedHash> hashes = contentHashFor(memoryStream);
    // Check if contentInfo exist of the collection of hashes.
    // If yes, skip adding the content and return existing content info.
    ContentInfo contentInfo = hashesToContentInfo.get(hashes);
    if (contentInfo != null) {
        return contentInfo;
    }
    try (InputStream stream = memoryStream.get()) {
        addZipEntry(ri, stream, noHashAssembler);
        incMetric(SipMetrics.SIZE_DIGITAL_OBJECTS, getContentHashAssembler().numBytesHashed());
        contentInfo = new ContentInfo(ri, hashes);
        hashesToContentInfo.put(hashes, contentInfo);
    }
    return contentInfo;
}
Also used : EncodedHash(com.opentext.ia.sdk.support.io.EncodedHash) RepeatableInputStream(com.opentext.ia.sdk.support.io.RepeatableInputStream) InputStream(java.io.InputStream) RepeatableInputStream(com.opentext.ia.sdk.support.io.RepeatableInputStream)

Aggregations

EncodedHash (com.opentext.ia.sdk.support.io.EncodedHash)5 Test (org.junit.jupiter.api.Test)3 DataBuffer (com.opentext.ia.sdk.support.io.DataBuffer)2 HashAssembler (com.opentext.ia.sdk.support.io.HashAssembler)2 MemoryBuffer (com.opentext.ia.sdk.support.io.MemoryBuffer)2 NoHashAssembler (com.opentext.ia.sdk.support.io.NoHashAssembler)2 InputStream (java.io.InputStream)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 ZipInputStream (java.util.zip.ZipInputStream)2 ByteArrayInputOutputStream (com.opentext.ia.sdk.support.io.ByteArrayInputOutputStream)1 Encoding (com.opentext.ia.sdk.support.io.Encoding)1 HashFunction (com.opentext.ia.sdk.support.io.HashFunction)1 RepeatableInputStream (com.opentext.ia.sdk.support.io.RepeatableInputStream)1 XmlUtil (com.opentext.ia.sdk.support.xml.XmlUtil)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Date (java.util.Date)1