package com.rjconsultores.tests;

import static org.junit.Assert.fail;

import java.time.Duration;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class SecurityEmpresaTokenTest {
	private static final Logger log = LogManager.getLogger(SecurityEmpresaTokenTest.class);
			
	@Before
	public void initApplicationContext() {
		log.info("[ BEGIN ]");
	}

	@After
	public void closeApplicationContext() {
		log.info("[ END ]");
	}

//	@Test
//	public void test_Token() throws Exception {
//		// license request -> token request -> token response -> license  
//		
//		Integer empresaId = 1313;
//		String cnpj = "00073778000120";
//
//		SecurityEmpresaToken security = new SecurityEmpresaToken();
//		
//		final String bodyRequest = security.bodyRequestGenerate(empresaId, cnpj);
//		final String request = security.requestGenerate(bodyRequest);
//		
//		final String reponse = "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI4ZTY2MGVmOTQwMGRlNjU2MmQ1MjljZTVkZDMyZDU0NmM2OGU5YTk0NTUwYjUyNzc4MDhjNWIxMjgzNzkwNjVlNzI0NWU2ZTMxNzBjMTQ2ZGVlOWUyNGQwZjc1YzMwYTVmM2JlYmY5NjY0YzZiNWNiYjU3NTk1NzVmOGYzMTk4OTljNmVmODNkZDc3NmI4YjM2MGM1NDE3N2RhMzFkMDAzNTA5ZDFlIiwiZXhwIjoxNjk5NjQ5OTQ1LCJ1c2VySWQiOiJhZG0iLCJyb2xlIjoiUk9MRV9UT0tFTiJ9.0WFPKf6RcpPYle4Rgq_D-GScwhkw_Q4pmCvNCL_INfg";
//		
//		final String license = security.tokenValidate(reponse);
//		final boolean valid = security.licenseValidate(license, empresaId, cnpj);
//		
//		log.info("Body Request: " + bodyRequest);
//		log.info("Request: " + request);
//		log.info("License: " + license);
//		log.info("Valid: " + Boolean.toString(valid));
//		
//		if (!valid) fail("Licença inválida");
//	}
/*
	@Test
	public void test_EmpresaNova() throws Exception {
		// license request -> token request -> token response -> license  
		
		Integer empresaId = 1313;
		String cnpj = "00073778000120";

		SecurityEmpresaToken security = new SecurityEmpresaToken();
		
		final String bodyRequest = security.bodyRequestGenerate(empresaId, cnpj);
		final String request = security.requestGenerate(bodyRequest);
		
		final String license = security.tokenValidate(request);
		final boolean valid = security.licenseValidate(license, empresaId, cnpj);
		
		log.info("Body Request: " + bodyRequest);
		log.info("Request: " + request);
		log.info("License: " + license);
		log.info("Valid: " + Boolean.toString(valid));
		
		if (valid) fail("Licença válida");
	}

	@Test
	public void test_Licenca() throws Exception {
		Integer empresaId = 1313;
		String cnpj = "00073778000120";

		SecurityEmpresaToken security = new SecurityEmpresaToken();
		
		final String license = security.licenseDefaultGenerate(empresaId, cnpj);
		final boolean valid = security.licenseValidate(license, empresaId, cnpj);
		
		log.info("License: " + license);
		log.info("Valid: " + Boolean.toString(valid));
		
		if (!valid) fail("Licença inválida");
	}

	@Test(expected = SecurityException.class)
	public void test_TTL_expired() throws Exception {
		Duration ttl = Duration.ofSeconds(5);
		
		Integer empresaId = 1313;
		String cnpj = "00073778000120";

		SecurityEmpresaToken security = new SecurityEmpresaToken();
		
		final String bodyRequest = security.bodyRequestGenerate(empresaId, cnpj);
		final String request = security.requestGenerate(bodyRequest, ttl);

		log.info("Body Request: " + bodyRequest);
		log.info("Request: " + request);
		
		Thread.sleep(Duration.ofSeconds(10).toMillis());
		
		final String license = security.tokenValidate(request);
		final boolean valid = security.licenseValidate(license, empresaId, cnpj);
		
		log.info("License: " + license);
		log.info("Valid: " + Boolean.toString(valid));
		
		if (valid) fail("Licença válida");
	}
	*/
}
