package eu.stork.documentservice.tests;

import static org.junit.Assert.*;

import java.util.Arrays;

import org.junit.Test;

import eu.stork.documentservice.exceptions.EncryptionException;
import eu.stork.documentservice.utils.EncryptionHelper;
import eu.stork.documentservice.utils.Utils;

public class EncryptionTests {

	private String pdfLoc = "C:/Temp/temp.pdf";
	private String certLocation = "C:/Temp/testcert.pem";
	private static String encKey;
	private static String encIv;
	
	@Test
	//Test encrypting some data
	public void test1() {
		try {
			EncryptionHelper enc = new EncryptionHelper();
			byte[] data = (Utils.readData(pdfLoc));
			byte[] encdata = enc.encrypt(data);			
			assertNotNull(encdata);
		} catch (EncryptionException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	//Test encrypting and decrypting some data
	public void test2() {
		try {
			EncryptionHelper enc = new EncryptionHelper();
			byte[] data = (Utils.readData(pdfLoc));
			byte[] encdata = enc.encrypt(data);
			encKey = enc.getKey();
			encIv = enc.getIv();
			
			EncryptionHelper enc1 = new EncryptionHelper(encKey, encIv);
			byte[] decdata = enc1.decrypt(encdata);
			assertTrue(Arrays.equals(data, decdata));
		} catch (EncryptionException e) {
			e.printStackTrace();
		}
	}

	@Test
	//Test encrypting with a certificate
	public void test3() {
		try {
			EncryptionHelper enc = new EncryptionHelper(encKey, encIv);
			String certString = Utils.readString(certLocation);
			String secret = enc.encryptWithCert(certString, encKey);
			assertTrue((secret != null && !secret.isEmpty()));
		} catch (EncryptionException e) {
			e.printStackTrace();
		}
	}
}