source: src/test/java/geniusweb/profilesserver/AutoUpdatingProfilesFactoryTest.java@ 2

Last change on this file since 2 was 2, checked in by bart, 5 years ago

Added new parties : linear, hardliner, conceder, boulware

File size: 8.0 KB
Line 
1package geniusweb.profilesserver;
2
3import static org.junit.Assert.assertEquals;
4import static org.junit.Assert.assertNotNull;
5import static org.junit.Assert.assertNull;
6import static org.junit.Assert.assertTrue;
7
8import java.io.BufferedWriter;
9import java.io.File;
10import java.io.FileNotFoundException;
11import java.io.FileWriter;
12import java.io.IOException;
13import java.net.URISyntaxException;
14import java.nio.file.Files;
15import java.nio.file.Path;
16import java.nio.file.Paths;
17import java.util.Collections;
18import java.util.LinkedList;
19import java.util.List;
20import java.util.logging.Level;
21
22import org.junit.Before;
23import org.junit.Test;
24
25import com.fasterxml.jackson.databind.ObjectMapper;
26
27import geniusweb.profile.Profile;
28import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
29import geniusweb.profilesserver.events.ChangeEvent;
30import geniusweb.profilesserver.events.DomainChangeEvent;
31import geniusweb.profilesserver.events.ProfileChangeEvent;
32import tudelft.utilities.files.FileInfo;
33import tudelft.utilities.files.FileWatcher;
34import tudelft.utilities.immutablelist.Tuple;
35import tudelft.utilities.listener.Listenable;
36import tudelft.utilities.listener.Listener;
37import tudelft.utilities.logging.Reporter;
38
39/**
40 * Most tests are using a (copy of ) a real filesystem repository. This is
41 * because the AutoUpdatingProfilesFactory accesses the real filesystem that we
42 * can't properly mock.
43 *
44 */
45public class AutoUpdatingProfilesFactoryTest {
46 private static final String JOBS = "jobs";
47 private static final String JOB1 = "jobs/jobs1";
48 private final Path REPO_DIRECTORY = Paths
49 .get("src/main/webapp/domainsrepo/");
50 private List<String> warnings;
51 private Path copyrepodir;
52 private AutoUpdatingProfilesFactory factory;
53 private final long HIGHRATE = 500; // fast so that we can test quickly.
54 private final List<ChangeEvent> changes = new LinkedList<>();
55
56 @Before
57 public void before() throws IOException, URISyntaxException {
58 final Path dir = Files.createTempDirectory("profilestest");
59 copyrepodir = Paths.get(dir.toString(), "repo");
60 copyFolder(REPO_DIRECTORY, copyrepodir);
61
62 System.out.println("copy placed in " + copyrepodir);
63 Reporter logger = new Reporter() {
64
65 @Override
66 public void log(Level level, String string, Throwable e) {
67 if (level.intValue() >= Level.WARNING.intValue()) {
68 warnings.add(string);
69 }
70 }
71
72 @Override
73 public void log(Level level, String string) {
74 if (level.intValue() >= Level.WARNING.intValue()) {
75 warnings.add(string);
76 }
77 }
78
79 };
80
81 factory = new AutoUpdatingProfilesFactory(logger) {
82
83 @Override
84 protected Path getRootDir() {
85 return copyrepodir;
86 }
87
88 @Override
89 protected Listenable<Tuple<FileInfo, FileInfo>> getFileWatcher(
90 File file) {
91 return new FileWatcher(file, HIGHRATE - 100, 2);
92 }
93
94 };
95
96 factory.addListener(new Listener<ChangeEvent>() {
97
98 @Override
99 public void notifyChange(ChangeEvent evt) {
100 changes.add(evt);
101 }
102 });
103
104 warnings = new LinkedList<>();
105
106 }
107
108 /**
109 * test loading the repo
110 */
111 @Test
112 public void loadRepoTest() throws InterruptedException {
113 assertEquals(Collections.EMPTY_LIST, warnings);
114 assertNotNull(factory.getDomain(JOBS));
115 assertNotNull(factory.getProfile(JOB1));
116
117 }
118
119 @Test
120 public void getDomainsTest() {
121 assertEquals(2, factory.getDomains().size());
122 assertEquals(JOBS, factory.getDomains().get(0));
123 assertEquals("7issues", factory.getDomains().get(1));
124
125 }
126
127 @Test
128 public void removeProfileTest() throws InterruptedException {
129
130 copyrepodir.resolve("jobs/jobs1.json").toFile().delete();
131 Thread.sleep(HIGHRATE);
132
133 assertEquals(1, changes.size());
134 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
135 assertEquals(Collections.EMPTY_LIST, warnings);
136 assertNotNull(factory.getDomain(JOBS));
137 assertNull(factory.getProfile(JOB1));
138 }
139
140 @Test
141 public void removeDomainTest() throws InterruptedException {
142
143 copyrepodir.resolve("jobs/jobs.json").toFile().delete();
144 Thread.sleep(HIGHRATE);
145
146 assertEquals(4, changes.size());
147 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
148 assertTrue(changes.get(1) instanceof ProfileChangeEvent);
149 assertTrue(changes.get(2) instanceof ProfileChangeEvent);
150 assertTrue(changes.get(3) instanceof DomainChangeEvent);
151 assertEquals(1, warnings.size()); // jobs domain file missing
152 assertNull(factory.getDomain(JOBS));
153 assertNull(factory.getProfile(JOB1));
154
155 }
156
157 @Test
158 public void renameDomainTest() throws InterruptedException {
159
160 assertTrue(copyrepodir.resolve(JOBS).toFile()
161 .renameTo(copyrepodir.resolve("otherjobs").toFile()));
162 Thread.sleep(HIGHRATE);
163
164 assertEquals(4, changes.size());
165 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
166 assertTrue(changes.get(1) instanceof ProfileChangeEvent);
167 assertTrue(changes.get(2) instanceof ProfileChangeEvent);
168 assertTrue(changes.get(3) instanceof DomainChangeEvent);
169 assertEquals(1, warnings.size()); // wrong domain file, wrong profile
170 assertNull(factory.getDomain(JOBS));
171 assertNull(factory.getProfile(JOB1));
172 assertNull(factory.getDomain("otherjobs"));
173 }
174
175 @Test
176 public void addProfileTest() throws InterruptedException, IOException {
177 LinearAdditiveUtilitySpace profile = (LinearAdditiveUtilitySpace) factory
178 .getProfile(JOB1);
179 Profile newprofile = new LinearAdditiveUtilitySpace(profile.getDomain(),
180 "jobs9", profile.getUtilities(), profile.getWeights(),
181 profile.getReservationBid());
182
183 ObjectMapper jackson = new ObjectMapper();
184 BufferedWriter writer = new BufferedWriter(new FileWriter(
185 copyrepodir.resolve("jobs/jobs9.json").toFile()));
186 writer.write(jackson.writeValueAsString(newprofile));
187 writer.close();
188
189 Thread.sleep(HIGHRATE);
190
191 assertEquals(1, changes.size());
192 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
193 assertTrue(warnings.isEmpty());
194 assertNotNull(factory.getProfile("jobs/jobs2"));
195 }
196
197 /**
198 * We add domain as jobs8 but it has internally the name jobs9.
199 */
200 @Test
201 public void addWrongProfileTest() throws InterruptedException, IOException {
202 LinearAdditiveUtilitySpace profile = (LinearAdditiveUtilitySpace) factory
203 .getProfile(JOB1);
204 Profile newprofile = new LinearAdditiveUtilitySpace(profile.getDomain(),
205 "jobs9", profile.getUtilities(), profile.getWeights(),
206 profile.getReservationBid());
207
208 ObjectMapper jackson = new ObjectMapper();
209 BufferedWriter writer = new BufferedWriter(new FileWriter(
210 copyrepodir.resolve("jobs/jobs8.json").toFile()));
211 writer.write(jackson.writeValueAsString(newprofile));
212 writer.close();
213
214 Thread.sleep(HIGHRATE);
215
216 // the bad profile should be completely ignored but we should get
217 // warnings.
218 assertEquals(0, changes.size());
219 assertEquals(1, warnings.size());
220 }
221
222 /**
223 * We remove jobs domain.
224 */
225 @Test
226 public void removeRootdirTest() throws InterruptedException, IOException {
227 assertNotNull(factory.getDomain(JOBS));
228
229 delete(copyrepodir.resolve(JOBS).toFile());
230 Thread.sleep(HIGHRATE);
231
232 assertNull(factory.getDomain(JOBS));
233 assertNull(factory.getProfile(JOB1));
234
235 assertEquals(4, changes.size());
236 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
237 assertTrue(changes.get(1) instanceof ProfileChangeEvent);
238 assertTrue(changes.get(2) instanceof ProfileChangeEvent);
239 assertTrue(changes.get(3) instanceof DomainChangeEvent);
240 assertEquals(0, warnings.size());
241 }
242
243 /**
244 * Copy files and all sub-files.
245 *
246 * @param source
247 * @param dest
248 */
249 private void copy(Path source, Path dest) {
250 try {
251 Files.copy(source, dest);
252 } catch (IOException e) {
253 e.printStackTrace();
254 }
255 }
256
257 private void copyFolder(Path src, Path dest) throws IOException {
258 Files.walk(src).forEach(
259 source -> copy(source, dest.resolve(src.relativize(source))));
260 }
261
262 /**
263 * recursively delete file and all subfiles.
264 *
265 * @param f the file to be deleted. can be directory.
266 * @throws IOException if file can't be deleted. This halts the delete
267 * procedure immediately.
268 */
269 private void delete(File f) throws IOException {
270 if (f.isDirectory()) {
271 for (File c : f.listFiles())
272 delete(c);
273 }
274 if (!f.delete())
275 throw new FileNotFoundException("Failed to delete file: " + f);
276 }
277}
Note: See TracBrowser for help on using the repository browser.