/*
 * Copyright 2016 Datalogics Inc.
 */

package com.datalogics.pdf.hsm.samples;

import static org.junit.Assert.assertFalse;

import com.datalogics.pdf.security.HsmLoginParameters;
import com.datalogics.pdf.security.HsmManager;

import org.junit.Test;

import java.io.File;
import java.net.URL;
import java.security.Key;
import java.security.cert.Certificate;

/**
 * Unit tests for the HsmSignDocument class.
 */
public class HsmSignDocumentTest extends SampleTest {
    static final String FILE_NAME = "SignedField.pdf";
    static final String QUALIFIED_SIGNATURE_FIELD_NAME = "Approver";

    @Test
    public void failWithUnconnectedHsmManager() throws Exception {
        final URL inputUrl = HsmSignDocument.class.getResource(HsmSignDocument.INPUT_UNSIGNED_PDF_PATH);

        final File file = SampleTest.newOutputFileWithDelete(FILE_NAME);

        // The complete file name will be set in the HsmSignDocument class.
        final URL outputUrl = file.toURI().toURL();

        // Create an unconnected HsmManager
        final HsmManager unconnectedHsmManager = new MockHsmManager() {
            @Override
            public ConnectionState getConnectionState() {
                return ConnectionState.READY;
            }
        };

        HsmSignDocument.signExistingSignatureFields(unconnectedHsmManager, inputUrl, outputUrl);
        // Make sure the Output file does not exist.
        assertFalse(file.getPath() + " must not exist after run", file.exists());
    }

    private class MockHsmManager implements HsmManager {
        /*
         * (non-Javadoc)
         *
         * @see com.datalogics.pdf.security.HsmManager#hsmLogin(com.datalogics.pdf.security.HsmLoginParameters)
         */
        @Override
        public void hsmLogin(final HsmLoginParameters parms) {}

        /*
         * (non-Javadoc)
         *
         * @see com.datalogics.pdf.security.HsmManager#hsmLogout()
         */
        @Override
        public void hsmLogout() {}

        /*
         * (non-Javadoc)
         *
         * @see com.datalogics.pdf.security.HsmManager#getConnectionState()
         */
        @Override
        public ConnectionState getConnectionState() {
            return null;
        }

        /*
         * (non-Javadoc)
         *
         * @see com.datalogics.pdf.security.HsmManager#getKey(java.lang.String, java.lang.String)
         */
        @Override
        public Key getKey(final String password, final String keyLabel) {
            return null;
        }

        /*
         * (non-Javadoc)
         *
         * @see com.datalogics.pdf.security.HsmManager#getCertificateChain(java.lang.String)
         */
        @Override
        public Certificate[] getCertificateChain(final String certLabel) {
            return new Certificate[0];
        }

        /*
         * (non-Javadoc)
         *
         * @see com.datalogics.pdf.security.HsmManager#getProviderName()
         */
        @Override
        public String getProviderName() {
            return null;
        }

    }
}

@@ -117,15 +117,127 @@
 sert
-False;%0A
+Equals;%0Aimport static org.junit.Assert.assertFalse;%0A%0Aimport com.datalogics.pdf.hsm.samples.util.LogRecordListCollector;
 %0Aimp
@@ -353,20 +353,85 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.ExpectedException
 ;%0A%0Aimpor
@@ -531,16 +531,145 @@
 ificate;
+%0Aimport java.util.ArrayList;%0Aimport java.util.logging.Level;%0Aimport java.util.logging.LogRecord;%0Aimport java.util.logging.Logger;
 %0A%0A/**%0A *
@@ -894,16 +894,93 @@
 over%22;%0A%0A
+    @Rule%0A    public ExpectedException expected = ExpectedException.none();%0A%0A
     @Tes
@@ -1637,117 +1637,906 @@
     
-HsmSignDocument.signExistingSignatureFields(unconnectedHsmManager, inputUrl, outputUrl);%0A        // Make sure
+// Expect an IllegalStateException to be thrown%0A        expected.expect(IllegalStateException.class);%0A        expected.expectMessage(%22HsmManager is not connected to HSM device.%22);%0A%0A        final ArrayList%3CLogRecord%3E logRecords = new ArrayList%3CLogRecord%3E();%0A        final Logger logger = Logger.getLogger(HsmSignDocument.class.getName());%0A        try (LogRecordListCollector collector = new LogRecordListCollector(logger, logRecords)) %7B%0A            HsmSignDocument.signExistingSignatureFields(unconnectedHsmManager, inputUrl, outputUrl);%0A        %7D%0A%0A        // Verify that we got the expected log message%0A        assertEquals(%22Must have one log record%22, 1, logRecords.size());%0A        final LogRecord logRecord = logRecords.get(0);%0A        assertEquals(%22HsmManager is not connected to HSM device.%22, logRecord.getMessage());%0A        assertEquals(Level.SEVERE, logRecord.getLevel());%0A%0A        // Verify
  the

Verify exception and log record for unconnected HSM test