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

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

Small fixes

File size: 8.1 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 assertTrue(factory.getDomains().size() > 2);
122 assertTrue(factory.getDomains().contains(JOBS));
123 assertTrue(factory.getDomains().contains(JOBS));
124 assertTrue(factory.getDomains().contains("7issues"));
125
126 }
127
128 @Test
129 public void removeProfileTest() throws InterruptedException {
130
131 copyrepodir.resolve("jobs/jobs1.json").toFile().delete();
132 Thread.sleep(HIGHRATE);
133
134 assertEquals(1, changes.size());
135 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
136 assertEquals(Collections.EMPTY_LIST, warnings);
137 assertNotNull(factory.getDomain(JOBS));
138 assertNull(factory.getProfile(JOB1));
139 }
140
141 @Test
142 public void removeDomainTest() throws InterruptedException {
143
144 copyrepodir.resolve("jobs/jobs.json").toFile().delete();
145 Thread.sleep(HIGHRATE);
146
147 assertEquals(4, changes.size());
148 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
149 assertTrue(changes.get(1) instanceof ProfileChangeEvent);
150 assertTrue(changes.get(2) instanceof ProfileChangeEvent);
151 assertTrue(changes.get(3) instanceof DomainChangeEvent);
152 assertEquals(1, warnings.size()); // jobs domain file missing
153 assertNull(factory.getDomain(JOBS));
154 assertNull(factory.getProfile(JOB1));
155
156 }
157
158 @Test
159 public void renameDomainTest() throws InterruptedException {
160
161 assertTrue(copyrepodir.resolve(JOBS).toFile()
162 .renameTo(copyrepodir.resolve("otherjobs").toFile()));
163 Thread.sleep(HIGHRATE);
164
165 assertEquals(4, changes.size());
166 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
167 assertTrue(changes.get(1) instanceof ProfileChangeEvent);
168 assertTrue(changes.get(2) instanceof ProfileChangeEvent);
169 assertTrue(changes.get(3) instanceof DomainChangeEvent);
170 assertEquals(1, warnings.size()); // wrong domain file, wrong profile
171 assertNull(factory.getDomain(JOBS));
172 assertNull(factory.getProfile(JOB1));
173 assertNull(factory.getDomain("otherjobs"));
174 }
175
176 @Test
177 public void addProfileTest() throws InterruptedException, IOException {
178 LinearAdditiveUtilitySpace profile = (LinearAdditiveUtilitySpace) factory
179 .getProfile(JOB1);
180 Profile newprofile = new LinearAdditiveUtilitySpace(profile.getDomain(),
181 "jobs9", profile.getUtilities(), profile.getWeights(),
182 profile.getReservationBid());
183
184 ObjectMapper jackson = new ObjectMapper();
185 BufferedWriter writer = new BufferedWriter(new FileWriter(
186 copyrepodir.resolve("jobs/jobs9.json").toFile()));
187 writer.write(jackson.writeValueAsString(newprofile));
188 writer.close();
189
190 Thread.sleep(HIGHRATE);
191
192 assertEquals(1, changes.size());
193 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
194 assertTrue(warnings.isEmpty());
195 assertNotNull(factory.getProfile("jobs/jobs2"));
196 }
197
198 /**
199 * We add domain as jobs8 but it has internally the name jobs9.
200 */
201 @Test
202 public void addWrongProfileTest() throws InterruptedException, IOException {
203 LinearAdditiveUtilitySpace profile = (LinearAdditiveUtilitySpace) factory
204 .getProfile(JOB1);
205 Profile newprofile = new LinearAdditiveUtilitySpace(profile.getDomain(),
206 "jobs9", profile.getUtilities(), profile.getWeights(),
207 profile.getReservationBid());
208
209 ObjectMapper jackson = new ObjectMapper();
210 BufferedWriter writer = new BufferedWriter(new FileWriter(
211 copyrepodir.resolve("jobs/jobs8.json").toFile()));
212 writer.write(jackson.writeValueAsString(newprofile));
213 writer.close();
214
215 Thread.sleep(HIGHRATE);
216
217 // the bad profile should be completely ignored but we should get
218 // warnings.
219 assertEquals(0, changes.size());
220 assertEquals(1, warnings.size());
221 }
222
223 /**
224 * We remove jobs domain.
225 */
226 @Test
227 public void removeRootdirTest() throws InterruptedException, IOException {
228 assertNotNull(factory.getDomain(JOBS));
229
230 delete(copyrepodir.resolve(JOBS).toFile());
231 Thread.sleep(HIGHRATE);
232
233 assertNull(factory.getDomain(JOBS));
234 assertNull(factory.getProfile(JOB1));
235
236 assertEquals(4, changes.size());
237 assertTrue(changes.get(0) instanceof ProfileChangeEvent);
238 assertTrue(changes.get(1) instanceof ProfileChangeEvent);
239 assertTrue(changes.get(2) instanceof ProfileChangeEvent);
240 assertTrue(changes.get(3) instanceof DomainChangeEvent);
241 assertEquals(0, warnings.size());
242 }
243
244 /**
245 * Copy files and all sub-files.
246 *
247 * @param source
248 * @param dest
249 */
250 private void copy(Path source, Path dest) {
251 try {
252 Files.copy(source, dest);
253 } catch (IOException e) {
254 e.printStackTrace();
255 }
256 }
257
258 private void copyFolder(Path src, Path dest) throws IOException {
259 Files.walk(src).forEach(
260 source -> copy(source, dest.resolve(src.relativize(source))));
261 }
262
263 /**
264 * recursively delete file and all subfiles.
265 *
266 * @param f the file to be deleted. can be directory.
267 * @throws IOException if file can't be deleted. This halts the delete
268 * procedure immediately.
269 */
270 private void delete(File f) throws IOException {
271 if (f.isDirectory()) {
272 for (File c : f.listFiles())
273 delete(c);
274 }
275 if (!f.delete())
276 throw new FileNotFoundException("Failed to delete file: " + f);
277 }
278}
Note: See TracBrowser for help on using the repository browser.