[41] | 1 | package geniusweb.profilesserver;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.junit.Assert.assertFalse;
|
---|
| 5 | import static org.junit.Assert.assertTrue;
|
---|
| 6 | import static org.mockito.Mockito.mock;
|
---|
| 7 | import static org.mockito.Mockito.when;
|
---|
| 8 |
|
---|
| 9 | import java.io.IOException;
|
---|
| 10 | import java.util.LinkedList;
|
---|
| 11 | import java.util.List;
|
---|
| 12 |
|
---|
| 13 | import org.junit.Before;
|
---|
| 14 | import org.junit.Test;
|
---|
| 15 |
|
---|
| 16 | import geniusweb.issuevalue.Domain;
|
---|
| 17 | import geniusweb.profile.Profile;
|
---|
| 18 | import geniusweb.profilesserver.events.ChangeEvent;
|
---|
| 19 | import geniusweb.profilesserver.events.DomainChangeEvent;
|
---|
| 20 | import geniusweb.profilesserver.events.ProfileChangeEvent;
|
---|
| 21 | import tudelft.utilities.listener.Listener;
|
---|
| 22 |
|
---|
| 23 | public class DefaultProfilesFactoryTest {
|
---|
| 24 |
|
---|
| 25 | private static final String PROFILE1 = "profile1";
|
---|
| 26 | private static final String DOMAIN1 = "domain1";
|
---|
| 27 | private Domain domain1 = mock(Domain.class);
|
---|
| 28 | private List<ChangeEvent> events = new LinkedList<>();
|
---|
| 29 | private DefaultProfilesRepository factory;
|
---|
| 30 |
|
---|
| 31 | private Profile profile1 = mock(Profile.class);
|
---|
| 32 | private Profile profile1a = mock(Profile.class);
|
---|
| 33 |
|
---|
| 34 | @Before
|
---|
| 35 | public void before() {
|
---|
| 36 | when(domain1.getName()).thenReturn(DOMAIN1);
|
---|
| 37 | when(profile1.getDomain()).thenReturn(domain1);
|
---|
| 38 | when(profile1.getName()).thenReturn(PROFILE1);
|
---|
| 39 | // profile1 is not equal to profile1a but domain and name are same.
|
---|
| 40 | when(profile1a.getDomain()).thenReturn(domain1);
|
---|
| 41 | when(profile1a.getName()).thenReturn(PROFILE1);
|
---|
| 42 | factory = new DefaultProfilesRepository();
|
---|
| 43 | factory.addListener(new Listener<ChangeEvent>() {
|
---|
| 44 |
|
---|
| 45 | @Override
|
---|
| 46 | public void notifyChange(ChangeEvent data) {
|
---|
| 47 | System.out.println("event:" + data);
|
---|
| 48 | events.add(data);
|
---|
| 49 | }
|
---|
| 50 | });
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | @Test
|
---|
| 54 | public void testAddEvent() throws IOException, InterruptedException {
|
---|
| 55 | factory.add(domain1);
|
---|
| 56 | assertEquals(1, events.size());
|
---|
| 57 | assertTrue(events.get(0) instanceof DomainChangeEvent);
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | @Test(expected = IllegalArgumentException.class)
|
---|
| 61 | public void getDomainNullTest() {
|
---|
| 62 | factory.getDomain(null);
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | @Test
|
---|
| 66 | public void getDomainTest() {
|
---|
| 67 | factory.add(domain1);
|
---|
| 68 | assertEquals(domain1, factory.getDomain(DOMAIN1));
|
---|
| 69 | assertEquals(null, factory.getDomain("unknowndomain"));
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | @Test
|
---|
| 73 | public void isSimpleNameTest() {
|
---|
| 74 | assertTrue(factory.isSimpleName(DOMAIN1));
|
---|
| 75 | assertTrue(factory.isSimpleName("12k12iuASIU"));
|
---|
| 76 | assertFalse(factory.isSimpleName(""));
|
---|
| 77 | assertFalse(factory.isSimpleName("_"));
|
---|
| 78 | assertFalse(factory.isSimpleName("\"12\""));
|
---|
| 79 |
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | @Test(expected = IllegalArgumentException.class)
|
---|
| 83 | public void nullTest() {
|
---|
| 84 | factory.add((Domain) null);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | @Test
|
---|
| 88 | public void getAddProfile() {
|
---|
| 89 | factory.add(domain1);
|
---|
| 90 | factory.add(profile1);
|
---|
| 91 | assertEquals(2, events.size());
|
---|
| 92 |
|
---|
| 93 | assertEquals(profile1, factory.getProfile(DOMAIN1 + "/" + PROFILE1));
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | @Test
|
---|
| 97 | public void readdProfileTest() {
|
---|
| 98 | factory.add(domain1);
|
---|
| 99 | factory.add(profile1);
|
---|
| 100 | events.clear();
|
---|
| 101 |
|
---|
| 102 | factory.add(profile1);
|
---|
| 103 | // same profile, nothing should happen
|
---|
| 104 | assertEquals(0, events.size());
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | @Test
|
---|
| 108 | public void readdProfileTest1() {
|
---|
| 109 | factory.add(domain1);
|
---|
| 110 | factory.add(profile1);
|
---|
| 111 | events.clear();
|
---|
| 112 |
|
---|
| 113 | factory.add(profile1a);
|
---|
| 114 | // changed version of profile1 should triggern change event
|
---|
| 115 | assertEquals(1, events.size());
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | @Test(expected = IllegalArgumentException.class)
|
---|
| 119 | public void getAddProfileUnknownDomain() {
|
---|
| 120 | factory.add(profile1);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | @Test
|
---|
| 124 | public void getAddNullProfile() {
|
---|
| 125 | factory.add((Profile) null); // is just ignored
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | @Test
|
---|
| 129 | public void removeDomainTest() {
|
---|
| 130 | factory.add(domain1);
|
---|
| 131 | factory.add(profile1);
|
---|
| 132 | events.clear();
|
---|
| 133 | factory.remove(profile1);
|
---|
| 134 | assertEquals(1, events.size());
|
---|
| 135 | assertTrue(events.get(0) instanceof ProfileChangeEvent);
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | @Test
|
---|
| 139 | public void removeDomainTest2() {
|
---|
| 140 | factory.add(domain1);
|
---|
| 141 | factory.add(profile1);
|
---|
| 142 | events.clear();
|
---|
| 143 | factory.remove(domain1);
|
---|
| 144 | assertEquals(2, events.size());
|
---|
| 145 | assertTrue(events.get(0) instanceof ProfileChangeEvent);
|
---|
| 146 | assertTrue(events.get(1) instanceof DomainChangeEvent);
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 | @Test(expected = IllegalArgumentException.class)
|
---|
| 150 | public void getProfileNullTest() {
|
---|
| 151 | factory.getProfile(null);
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | @Test
|
---|
| 155 | public void getProfileWrongNameTest() {
|
---|
| 156 | assertEquals(null, factory.getProfile("unknowndomain/" + PROFILE1));
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | @Test(expected = IllegalArgumentException.class)
|
---|
| 160 | public void getProfileWrongNameTest2() {
|
---|
| 161 | assertEquals(null, factory.getProfile("unknowndomain"));
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | @Test
|
---|
| 165 | public void getProfilesTest() {
|
---|
| 166 | assertEquals(0, factory.getProfiles("unknowndomain").size());
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | @Test
|
---|
| 170 | public void getProfilesTest1() {
|
---|
| 171 | factory.add(domain1);
|
---|
| 172 | factory.add(profile1);
|
---|
| 173 | assertEquals(1, factory.getProfiles(DOMAIN1).size());
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | }
|
---|