[1] | 1 | package geniusweb.profilesserver;
|
---|
| 2 |
|
---|
| 3 | import static org.junit.Assert.assertEquals;
|
---|
| 4 | import static org.junit.Assert.assertNotNull;
|
---|
| 5 | import static org.junit.Assert.assertNull;
|
---|
| 6 | import static org.junit.Assert.assertTrue;
|
---|
| 7 |
|
---|
| 8 | import java.io.BufferedWriter;
|
---|
| 9 | import java.io.File;
|
---|
| 10 | import java.io.FileNotFoundException;
|
---|
| 11 | import java.io.FileWriter;
|
---|
| 12 | import java.io.IOException;
|
---|
| 13 | import java.net.URISyntaxException;
|
---|
| 14 | import java.nio.file.Files;
|
---|
| 15 | import java.nio.file.Path;
|
---|
| 16 | import java.nio.file.Paths;
|
---|
| 17 | import java.util.Collections;
|
---|
| 18 | import java.util.LinkedList;
|
---|
| 19 | import java.util.List;
|
---|
| 20 | import java.util.logging.Level;
|
---|
| 21 |
|
---|
| 22 | import org.junit.Before;
|
---|
| 23 | import org.junit.Test;
|
---|
| 24 |
|
---|
| 25 | import com.fasterxml.jackson.databind.ObjectMapper;
|
---|
| 26 |
|
---|
| 27 | import geniusweb.profile.Profile;
|
---|
| 28 | import geniusweb.profile.utilityspace.LinearAdditiveUtilitySpace;
|
---|
| 29 | import geniusweb.profilesserver.events.ChangeEvent;
|
---|
| 30 | import geniusweb.profilesserver.events.DomainChangeEvent;
|
---|
| 31 | import geniusweb.profilesserver.events.ProfileChangeEvent;
|
---|
| 32 | import tudelft.utilities.files.FileInfo;
|
---|
| 33 | import tudelft.utilities.files.FileWatcher;
|
---|
| 34 | import tudelft.utilities.immutablelist.Tuple;
|
---|
| 35 | import tudelft.utilities.listener.Listenable;
|
---|
| 36 | import tudelft.utilities.listener.Listener;
|
---|
| 37 | import 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 | */
|
---|
| 45 | public 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 | }
|
---|