package oving4;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class PartnerTest {

	private Partner p1;
	private Partner p2;

	@BeforeEach
	public void setUp() {
		p1 = new Partner("P1");
		p2 = new Partner("P2");
	}

	@Test
	@DisplayName("Check that the constructor initializes correctly")
	public void testConstructor() {
		assertNull(p1.getPartner());
		assertNull(p2.getPartner());

		assertThrows(IllegalArgumentException.class, () -> {
			new Partner(null);
		}, "Name cannot be null");
	}

	@Test
	@DisplayName("Check that P1 and P2 are partners after p1.setPartner(p2)")
	public void simplePartnership() {
		assertNull(p1.getPartner());
		assertNull(p2.getPartner());

		p1.setPartner(p2);
		assertEquals(p1.getPartner(), p2, "P1 should be partner to P2");
		assertEquals(p2.getPartner(), p1, "P2 should be partner to P1");
	}

	@Test
	@DisplayName("Check that one can split up a partnership")
	public void partnershipWithDivorce() {
		p1.setPartner(p2);
		assertEquals(p1.getPartner(), p2, "P1 should be partner to P2");
		assertEquals(p2.getPartner(), p1, "P2 should be partner to P1");

		p1.setPartner(null);
		assertNull(p1.getPartner());
		assertNull(p2.getPartner());
	}

	@Test
	@DisplayName("Check that combined breakup followed by the creation of a new partnership works")
	public void swinger() {
		Partner p3 = new Partner("P3");
		Partner p4 = new Partner("P4");

		p1.setPartner(p2);
		p3.setPartner(p4);
		assertEquals(p1.getPartner(), p2, "P1 should be the partner of P2");
		assertEquals(p2.getPartner(), p1, "P2 should be the partner of P1");
		assertEquals(p3.getPartner(), p4, "P3 should be the partner of P4");
		assertEquals(p4.getPartner(), p3, "P4 should be the partner of P3");

		p1.setPartner(p4);
		assertEquals(p1.getPartner(), p4, "P4 should be the partner of P1");
		assertEquals(p4.getPartner(), p1, "P1 should be the partner of P4");
		assertNull(p2.getPartner());
		assertNull(p3.getPartner());
	}
}
