package org.spongycastle.asn1.test;

import java.io.IOException;

import org.spongycastle.asn1.ASN1Encodable;
import org.spongycastle.asn1.ASN1InputStream;
import org.spongycastle.asn1.ASN1Sequence;
import org.spongycastle.asn1.DERObjectIdentifier;
import org.spongycastle.asn1.esf.CommitmentTypeIdentifier;
import org.spongycastle.asn1.esf.CommitmentTypeQualifier;
import org.spongycastle.util.test.SimpleTest;

public class CommitmentTypeQualifierUnitTest 
    extends SimpleTest
{
    public String getName()
    {
        return "CommitmentTypeQualifier";
    }

    public void performTest() 
        throws Exception
    {
        CommitmentTypeQualifier ctq = new CommitmentTypeQualifier(CommitmentTypeIdentifier.proofOfOrigin);
        
        checkConstruction(ctq, CommitmentTypeIdentifier.proofOfOrigin, null);
        
        ASN1Encodable info = new DERObjectIdentifier("1.2");
        
        ctq = new CommitmentTypeQualifier(CommitmentTypeIdentifier.proofOfOrigin, info);

        checkConstruction(ctq, CommitmentTypeIdentifier.proofOfOrigin, info);
        
        ctq = CommitmentTypeQualifier.getInstance(null);
        
        if (ctq != null)
        {
            fail("null getInstance() failed.");
        }
        
        try
        {
            CommitmentTypeQualifier.getInstance(new Object());
            
            fail("getInstance() failed to detect bad object.");
        }
        catch (IllegalArgumentException e)
        {
            // expected
        }
    }

    private void checkConstruction(
         CommitmentTypeQualifier mv,
         DERObjectIdentifier commitmenttTypeId,
         ASN1Encodable qualifier) 
         throws IOException
    {
        checkStatement(mv, commitmenttTypeId, qualifier);
        
        mv = CommitmentTypeQualifier.getInstance(mv);
        
        checkStatement(mv, commitmenttTypeId, qualifier);
        
        ASN1InputStream aIn = new ASN1InputStream(mv.toASN1Object().getEncoded());

        ASN1Sequence seq = (ASN1Sequence)aIn.readObject();
        
        mv = CommitmentTypeQualifier.getInstance(seq);
        
        checkStatement(mv, commitmenttTypeId, qualifier);
    }

    private void checkStatement(
        CommitmentTypeQualifier ctq,
        DERObjectIdentifier     commitmentTypeId,
        ASN1Encodable           qualifier)
    {
        if (!ctq.getCommitmentTypeIdentifier().equals(commitmentTypeId))
        {
            fail("commitmentTypeIds don't match.");
        }
        
        if (qualifier != null)
        {
            if (!ctq.getQualifier().equals(qualifier))
            {
                fail("qualifiers don't match.");
            }
        }
        else if (ctq.getQualifier() != null)
        {
            fail("qualifier found when none expected.");
        }
    }
    
    public static void main(
        String[]    args)
    {
        runTest(new CommitmentTypeQualifierUnitTest());
    }
}
