package exo1.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

import exo1.entreprise.Entreprise;
import exo1.entreprise.IService;
import exo1.entreprise.IServiceFactory;
import exo1.entreprise.ServiceExistantException;
import exo2.service.ServiceInexistantException;

/**
 * @author ouziri
 * @version 1.0
 *
 */

public class TestsCreationServices {

	private Entreprise e;
	private IServiceFactory factory;

	@BeforeEach
	public void init() {
		e = Entreprise.getInstance();
		e = e.reset();
		factory = new ServiceFactory();
	}

	@Test
	public void testCreationServiceAdministratif() {
		String nomS = "Recrutement";
		String typeS = "Administratif";
		e.addService(factory.creerService(nomS, typeS));
		IService s = e.searchServiceByName(nomS);
		assertNotNull(s);
		assertEquals(nomS, s.getNom());
		assertTrue(s instanceof ServAdministratif);
	}

	@Test
	public void testCreationServiceTechnique() {
		String nomS = "Realisation";
		String typeS = "Technique";
		e.addService(factory.creerService(nomS, typeS));
		IService s = e.searchServiceByName(nomS);
		assertNotNull(s);
		assertEquals(nomS, s.getNom());
		assertEquals(s.getClass(), ServTechnique.class);
	}

	@Test
	public void testCreationServiceTechniqueExistant() {
		Assertions.assertThrows(ServiceExistantException.class, new Executable() {

			@Override
			public void execute() throws Throwable {
				String nomS = "Realisation";
				String typeS = "Technique";
				e.addService(factory.creerService(nomS, typeS));
				e.addService(factory.creerService(nomS, typeS));
			}

		});

	}

	@Test
	public void testCreationServiceTypeInconnu() {
		Assertions.assertThrows(IllegalArgumentException.class, new Executable() {

			@Override
			public void execute() throws Throwable {
				String nomS = "Realisation";
				String typeS = "???????";
				e.addService(factory.creerService(nomS, typeS));
			}

		});
	}

}
