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

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

Initial Release

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