package geniusweb.profilesserver; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.IOException; import java.util.LinkedList; import java.util.List; import org.junit.Before; import org.junit.Test; import geniusweb.issuevalue.Domain; import geniusweb.profile.Profile; import geniusweb.profilesserver.events.ChangeEvent; import geniusweb.profilesserver.events.DomainChangeEvent; import geniusweb.profilesserver.events.ProfileChangeEvent; import tudelft.utilities.listener.Listener; public class DefaultProfilesFactoryTest { private static final String PROFILE1 = "profile1"; private static final String DOMAIN1 = "domain1"; private Domain domain1 = mock(Domain.class); private List events = new LinkedList<>(); private DefaultProfilesFactory factory; private Profile profile1 = mock(Profile.class); private Profile profile1a = mock(Profile.class); @Before public void before() { when(domain1.getName()).thenReturn(DOMAIN1); when(profile1.getDomain()).thenReturn(domain1); when(profile1.getName()).thenReturn(PROFILE1); // profile1 is not equal to profile1a but domain and name are same. when(profile1a.getDomain()).thenReturn(domain1); when(profile1a.getName()).thenReturn(PROFILE1); factory = new DefaultProfilesFactory(); factory.addListener(new Listener() { @Override public void notifyChange(ChangeEvent data) { System.out.println("event:" + data); events.add(data); } }); } @Test public void testAddEvent() throws IOException, InterruptedException { factory.add(domain1); assertEquals(1, events.size()); assertTrue(events.get(0) instanceof DomainChangeEvent); } @Test(expected = IllegalArgumentException.class) public void getDomainNullTest() { factory.getDomain(null); } @Test public void getDomainTest() { factory.add(domain1); assertEquals(domain1, factory.getDomain(DOMAIN1)); assertEquals(null, factory.getDomain("unknowndomain")); } @Test public void isSimpleNameTest() { assertTrue(factory.isSimpleName(DOMAIN1)); assertTrue(factory.isSimpleName("12k12iuASIU")); assertFalse(factory.isSimpleName("")); assertFalse(factory.isSimpleName("_")); assertFalse(factory.isSimpleName("\"12\"")); } @Test(expected = IllegalArgumentException.class) public void nullTest() { factory.add((Domain) null); } @Test public void getAddProfile() { factory.add(domain1); factory.add(profile1); assertEquals(2, events.size()); assertEquals(profile1, factory.getProfile(DOMAIN1 + "/" + PROFILE1)); } @Test public void readdProfileTest() { factory.add(domain1); factory.add(profile1); events.clear(); factory.add(profile1); // same profile, nothing should happen assertEquals(0, events.size()); } @Test public void readdProfileTest1() { factory.add(domain1); factory.add(profile1); events.clear(); factory.add(profile1a); // changed version of profile1 should triggern change event assertEquals(1, events.size()); } @Test(expected = IllegalArgumentException.class) public void getAddProfileUnknownDomain() { factory.add(profile1); } @Test public void getAddNullProfile() { factory.add((Profile) null); // is just ignored } @Test public void removeDomainTest() { factory.add(domain1); factory.add(profile1); events.clear(); factory.remove(profile1); assertEquals(1, events.size()); assertTrue(events.get(0) instanceof ProfileChangeEvent); } @Test public void removeDomainTest2() { factory.add(domain1); factory.add(profile1); events.clear(); factory.remove(domain1); assertEquals(2, events.size()); assertTrue(events.get(0) instanceof ProfileChangeEvent); assertTrue(events.get(1) instanceof DomainChangeEvent); } @Test(expected = IllegalArgumentException.class) public void getProfileNullTest() { factory.getProfile(null); } @Test public void getProfileWrongNameTest() { assertEquals(null, factory.getProfile("unknowndomain/" + PROFILE1)); } @Test(expected = IllegalArgumentException.class) public void getProfileWrongNameTest2() { assertEquals(null, factory.getProfile("unknowndomain")); } @Test public void getProfilesTest() { assertEquals(0, factory.getProfiles("unknowndomain").size()); } @Test public void getProfilesTest1() { factory.add(domain1); factory.add(profile1); assertEquals(1, factory.getProfiles(DOMAIN1).size()); } }