001package edu.pdx.cs.joy.family;
002
003import org.junit.jupiter.api.Assertions;
004import org.junit.jupiter.api.Test;
005
006import java.rmi.RemoteException;
007
008import static org.junit.jupiter.api.Assertions.assertNotNull;
009import static org.junit.jupiter.api.Assertions.assertNull;
010
011/**
012 * This class tests the functionality of the various implementation of
013 * <code>RemoteFamilyTree</code>.
014 */
015public class RemoteFamilyTreeTest extends RemoteTestCase {
016
017  @Test
018  public void testCreatePerson() throws RemoteException {
019    RemoteFamilyTree tree = getTree();
020    tree.createPerson(Person.MALE);
021  }
022
023  @Test
024  public void testCreatePersonBadGender() throws RemoteException {
025    try {
026      RemoteFamilyTree tree = getTree();
027      tree.createPerson(Person.Gender.UNKNOWN);
028      Assertions.fail("Should have thrown a FamilyTreeException");
029
030    } catch (FamilyTreeException ex) {
031      // pass...
032    }
033  }
034
035  @Test
036  public void testGetPersonById() throws RemoteException {
037    RemoteFamilyTree tree = getTree();
038    RemotePerson person = tree.createPerson(Person.MALE);
039    int id = person.getId();
040    RemotePerson person2 = tree.getPerson(id);
041    assertNotNull(person2);
042    Assertions.assertEquals(person, person2);
043  }
044
045  @Test
046  public void testGetNonexistingPersonById() throws RemoteException {
047    assertNull(getTree().getPerson(-79));
048  }
049
050  @Test
051  public void testGetPersonByName() throws RemoteException {
052    RemoteFamilyTree tree = getTree();
053    RemotePerson person = tree.createPerson(Person.MALE);
054    String firstName = "Test";
055    String lastName = "Person" + System.currentTimeMillis();
056    person.setFirstName(firstName);
057    person.setLastName(lastName);
058
059    RemotePerson person2 = tree.getPerson(firstName, lastName);
060    assertNotNull(person2);
061    Assertions.assertEquals(person, person2);
062  }
063
064  @Test
065  public void testGetPersonSameNameTwice() throws RemoteException {
066    RemoteFamilyTree tree = getTree();
067    RemotePerson person = tree.createPerson(Person.MALE);
068    String firstName = "Test";
069    String lastName = "Person" + System.currentTimeMillis();
070    person.setFirstName(firstName);
071    person.setLastName(lastName);
072
073    RemotePerson person2 = tree.createPerson(Person.MALE);
074    person2.setFirstName(firstName);
075    person2.setLastName(lastName);
076
077    try {
078      tree.getPerson(firstName, lastName);
079      Assertions.fail("Should have thrown an IllegalArgumentException");
080
081    } catch (IllegalArgumentException ex) {
082      // pass...
083    }
084
085  }
086
087  @Test
088  public void testGetNonexistingPersonByName()
089    throws RemoteException {
090    assertNull(getTree().getPerson("No such", "Person"));
091  }
092
093  ////////  RemoteFamilyTree.createMarriage()
094
095    @Test
096  public void testCreateMarriage() throws RemoteException {
097    RemoteFamilyTree tree = getTree();
098    RemotePerson husband = tree.createPerson(Person.MALE);
099    RemotePerson wife = tree.createPerson(Person.FEMALE);
100    RemoteMarriage marriage = 
101      tree.createMarriage(husband.getId(), wife.getId());
102    assertNotNull(marriage);
103  }
104
105    @Test
106  public void testCreateMarriageNoSuchHusband() throws RemoteException {
107    RemoteFamilyTree tree = getTree();
108    RemotePerson wife = tree.createPerson(Person.FEMALE);
109    try {
110      tree.createMarriage(4444, wife.getId());
111      Assertions.fail("Should have thrown an IllegalArgumentException");
112
113    } catch (IllegalArgumentException ex) {
114      // pass...
115    }
116  }
117
118    @Test
119  public void testCreateMarriageNoSuchWife() throws RemoteException {
120    RemoteFamilyTree tree = getTree();
121    RemotePerson husband = tree.createPerson(Person.MALE);
122    try {
123      tree.createMarriage(husband.getId(), 4444);
124      Assertions.fail("Should have thrown an IllegalArgumentException");
125
126    } catch (IllegalArgumentException ex) {
127      // pass...
128    }
129  }
130
131    @Test
132  public void testCreateMarriageHusbandNotMale() throws RemoteException {
133    RemoteFamilyTree tree = getTree();
134    RemotePerson husband = tree.createPerson(Person.FEMALE);
135    RemotePerson wife = tree.createPerson(Person.FEMALE);
136
137    try {
138      tree.createMarriage(husband.getId(), wife.getId());
139      Assertions.fail("Should have thrown an IllegalArgumentException");
140
141    } catch (IllegalArgumentException ex) {
142      // pass...
143    }
144  }
145
146    @Test
147  public void testCreateMarriageWifeNotFemale() throws RemoteException {
148    RemoteFamilyTree tree = getTree();
149    RemotePerson husband = tree.createPerson(Person.MALE);
150    RemotePerson wife = tree.createPerson(Person.MALE);
151
152    try {
153      tree.createMarriage(husband.getId(), wife.getId());
154      Assertions.fail("Should have thrown an IllegalArgumentException");
155
156    } catch (IllegalArgumentException ex) {
157      // pass...
158    }
159  }
160
161  ////////  RemoteFamilyTree.getMarriage()
162
163    @Test
164  public void testGetExistingMarriage() throws RemoteException {
165    RemoteFamilyTree tree = getTree();
166    RemotePerson husband = tree.createPerson(Person.MALE);
167    RemotePerson wife = tree.createPerson(Person.FEMALE);
168    RemoteMarriage marriage = 
169      tree.createMarriage(husband.getId(), wife.getId());
170    assertNotNull(marriage);
171
172    RemoteMarriage marriage2 = 
173      tree.getMarriage(husband.getId(), wife.getId());
174    assertNotNull(marriage2);
175    Assertions.assertEquals(marriage, marriage2);
176  }
177
178  public void testGetMarriageNoSuchHusband() throws RemoteException {
179    RemoteFamilyTree tree = getTree();
180    RemotePerson wife = tree.createPerson(Person.FEMALE);
181    try {
182      tree.getMarriage(4444, wife.getId());
183      Assertions.fail("Should have thrown IllegalArgumentException");
184
185    } catch (IllegalArgumentException ex) {
186      // pass...
187    }
188  }
189
190    @Test
191  public void testGetMarriageNoSuchWife() throws RemoteException {
192    RemoteFamilyTree tree = getTree();
193    RemotePerson husband = tree.createPerson(Person.MALE);
194    try {
195      tree.getMarriage(husband.getId(), 4444);
196      Assertions.fail("Should have thrown IllegalArgumentException");
197
198    } catch (IllegalArgumentException ex) {
199      // pass...
200    }
201  }
202
203    @Test
204  public void testGetMarriageNeverBeenMarried() throws RemoteException {
205    RemoteFamilyTree tree = getTree();
206    RemotePerson husband = tree.createPerson(Person.MALE);
207    RemotePerson wife = tree.createPerson(Person.FEMALE);
208    try {
209      tree.getMarriage(husband.getId(), wife.getId());
210
211    } catch (IllegalArgumentException ex) {
212      // pass...
213    }
214  }
215
216}