PageRenderTime 51ms CodeModel.GetById 37ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 1ms

/components/blitz/test/ome/services/blitz/test/utests/ManagedRepositoryITest.java

https://github.com/ximenesuk/openmicroscopy
Java | 346 lines | 273 code | 45 blank | 28 comment | 4 complexity | 775bc7d4168c281ddc34645c9520511b MD5 | raw file
  1package ome.services.blitz.test.utests;
  2
  3import java.io.File;
  4import java.text.DateFormatSymbols;
  5import java.util.ArrayList;
  6import java.util.Calendar;
  7import java.util.Collections;
  8import java.util.HashMap;
  9import java.util.List;
 10import java.util.Set;
 11import java.util.UUID;
 12
 13import junit.framework.Assert;
 14import loci.formats.FormatReader;
 15import ome.services.blitz.fire.Registry;
 16import ome.services.blitz.repo.FileMaker;
 17import ome.services.blitz.repo.ManagedRepositoryI;
 18import ome.services.blitz.repo.RepositoryDao;
 19import ome.services.blitz.repo.path.FsFile;
 20import ome.services.blitz.util.ChecksumAlgorithmMapper;
 21import omero.grid.ImportLocation;
 22import omero.model.ChecksumAlgorithm;
 23import omero.model.ChecksumAlgorithmI;
 24import omero.model.PermissionsI;
 25import static omero.rtypes.rstring;
 26import omero.sys.EventContext;
 27import omero.util.TempFileManager;
 28
 29import com.google.common.collect.Sets;
 30
 31import org.jmock.Mock;
 32import org.jmock.MockObjectTestCase;
 33import org.testng.annotations.BeforeMethod;
 34import org.testng.annotations.Test;
 35
 36@Test(groups = {"fs"})
 37public class ManagedRepositoryITest extends MockObjectTestCase {
 38
 39    Mock daoMock;
 40
 41    /**
 42     * The temporary directory which is equivalent to /OMERO/ManagedRepository
 43     */
 44    File tmpDir;
 45
 46    /**
 47     * The "expanded" template directory which here is mocked to simply
 48     * "template". This should be used when touch()-ing files under
 49     * tmpDir.
 50     */
 51    File templateDir;
 52
 53    TestManagedRepositoryI tmri;
 54    Registry reg;
 55    Ice.Current curr;
 56    Calendar cal;
 57    long uniqueId;
 58
 59    /**
 60     * Overrides protected methods from parent class for testing
 61     *
 62     * @author bpindelski
 63     */
 64    public class TestManagedRepositoryI extends ManagedRepositoryI {
 65        public static final String UUID = "fake-uuid";
 66
 67        public TestManagedRepositoryI(String template,
 68                RepositoryDao repositoryDao) throws Exception {
 69            super(template, repositoryDao);
 70            File dir = TempFileManager.create_path("mng-repo.", ".test", true);
 71            initialize(new FileMaker(dir.getAbsolutePath()),
 72                    -1L /*id*/, UUID);
 73        }
 74
 75        @Override
 76        public ImportLocation suggestImportPaths(FsFile relPath, FsFile basePath,
 77                List<FsFile> paths, Class<? extends FormatReader> reader,
 78                ChecksumAlgorithm checksumAlgorithm, Ice.Current curr)
 79                        throws omero.ServerError {
 80            return super.suggestImportPaths(relPath, basePath, paths, reader,
 81                    checksumAlgorithm, curr);
 82        }
 83
 84        @Override
 85        public FsFile commonRoot(List<FsFile> paths) {
 86            return super.commonRoot(paths);
 87        }
 88
 89        @Override
 90        public String expandTemplate(String template, EventContext ec) {
 91            return super.expandTemplate(template, ec);
 92        }
 93
 94        @Override
 95        public void createTemplateDir(FsFile template, Ice.Current curr) throws omero.ServerError {
 96            super.createTemplateDir(template, curr);
 97        }
 98    }
 99
100    @BeforeMethod(alwaysRun=true)
101    public void setup() throws Exception {
102        this.cal = Calendar.getInstance();
103        this.tmpDir = TempFileManager.create_path("repo", "test", true);
104        this.templateDir = new File(this.tmpDir, "template");
105        Mock mockReg = mock(Registry.class);
106        this.daoMock = mock(RepositoryDao.class);
107        this.reg = (Registry) mockReg.proxy();
108        this.tmri = new TestManagedRepositoryI("/%year%/%month%/%day%",
109                (RepositoryDao) daoMock.proxy());
110        this.curr = new Ice.Current();
111        this.curr.ctx = new HashMap<String, String>();
112        this.curr.ctx.put(omero.constants.SESSIONUUID.value, "TEST");
113        this.uniqueId = System.nanoTime();
114
115    }
116
117    private EventContext newEventContext() {
118        EventContext ec = new EventContext();
119        ec.userName = "";
120        ec.userId = -1L;
121        ec.groupName = "";
122        ec.groupId = -1L;
123        ec.sessionUuid = "";
124        ec.sessionId = -1L;
125        ec.eventId = -1L;
126        ec.groupPermissions = new PermissionsI();
127        this.daoMock.expects(atLeastOnce()).method("getEventContext")
128            .with(ANYTHING).will(returnValue(ec));
129        return ec;
130    }
131
132    private static List<FsFile> toFsFileList(String... paths) {
133        final List<FsFile> fsFiles = new ArrayList<FsFile>(paths.length);
134        for (final String path : paths)
135            fsFiles.add(new FsFile(path));
136        return fsFiles;
137    }
138
139    @Test
140    public void testCommonRootReturnsTopLevelWithUncommonPaths() {
141        FsFile expectedCommonRoot = new FsFile();
142        FsFile actualCommonRoot = this.tmri.commonRoot(
143                toFsFileList("/home/bob/1.jpg", "/data/alice/1.jpg"));
144        Assert.assertEquals(expectedCommonRoot, actualCommonRoot);
145    }
146
147    @Test
148    public void testCommonRootReturnsCommonRootForPathList() {
149        FsFile expectedCommonRoot = new FsFile("/bob/files/dv");
150        FsFile actualCommonRoot = this.tmri.commonRoot(toFsFileList(
151                expectedCommonRoot + "/file1.dv",
152                expectedCommonRoot + "/file2.dv"));
153        Assert.assertEquals(expectedCommonRoot, actualCommonRoot);
154    }
155
156    @Test
157    public void testExpandTemplateEmptyStringOnNullToken() {
158        EventContext ecStub = newEventContext();
159        String actual = this.tmri.expandTemplate(null, ecStub);
160        Assert.assertEquals(0, actual.length());
161    }
162
163    @Test
164    public void testExpandTemplateTokenOnMalformedToken() {
165        EventContext ecStub = newEventContext();
166        String expected = "foo";
167        String actual = this.tmri.expandTemplate(expected, ecStub);
168        Assert.assertEquals(expected, actual);
169    }
170
171    @Test
172    public void testExpandTemplateYear() {
173        EventContext ecStub = newEventContext();
174        String expected = Integer.toString(cal.get(Calendar.YEAR));
175        String actual = this.tmri.expandTemplate("%year%", ecStub);
176        Assert.assertEquals(expected, actual);
177    }
178
179    @Test
180    public void testExpandTemplateMonth() {
181        EventContext ecStub = newEventContext();
182        String expected = Integer.toString(cal.get(Calendar.MONTH)+1);
183        if (expected.length() == 1)
184            expected = '0' + expected;
185        String actual = this.tmri.expandTemplate("%month%", ecStub);
186        Assert.assertEquals(expected, actual);
187    }
188
189    @Test
190    public void testExpandTemplateMonthName() {
191        EventContext ecStub = newEventContext();
192        DateFormatSymbols dateFormat = new DateFormatSymbols();
193        String expected = dateFormat.getMonths()
194                [cal.get(Calendar.MONTH)];
195        String actual = this.tmri.expandTemplate("%monthname%", ecStub);
196        Assert.assertEquals(expected, actual);
197    }
198
199    @Test
200    public void testExpandTemplateDay() {
201        EventContext ecStub = newEventContext();
202        String expected = String.format("%02d", cal.get(Calendar.DAY_OF_MONTH));
203        String actual = this.tmri.expandTemplate("%day%", ecStub);
204        Assert.assertEquals(expected, actual);
205    }
206
207    @Test
208    public void testExpandTemplateUserName() {
209        String expected = "user-1";
210        EventContext ecStub = newEventContext();
211        ecStub.userName = expected;
212        String actual = this.tmri.expandTemplate("%user%", ecStub);
213        Assert.assertEquals(expected, actual);
214    }
215
216    @Test
217    public void testExpandTemplateGroupName() {
218        String expected = "group-1";
219        EventContext ecStub = newEventContext();
220        ecStub.groupName = expected;
221        String actual = this.tmri.expandTemplate("%group%", ecStub);
222        Assert.assertEquals(expected, actual);
223    }
224    @Test
225    public void testExpandTemplateGroupNamePerms() {
226        String expected = "group-1-rwrwrw";
227        EventContext ecStub = newEventContext();
228        ecStub.groupName = "group-1";
229        ecStub.groupPermissions = new PermissionsI("rwrwrw");
230        String actual = this.tmri.expandTemplate("%group%-%perms%", ecStub);
231        Assert.assertEquals(expected, actual);
232    }
233
234    @Test
235    public void testExpandTemplateSession() {
236        String expected = UUID.randomUUID().toString();
237        EventContext ecStub = newEventContext();
238        ecStub.sessionUuid = expected;
239        String actual = this.tmri.expandTemplate("%session%", ecStub);
240        Assert.assertEquals(expected, actual);
241    }
242
243    @Test
244    public void testExpandTemplateEscape() {
245        String expected = "%%";
246        EventContext ecStub = newEventContext();
247        String actual = this.tmri.expandTemplate("%%", ecStub);
248        Assert.assertEquals(expected, actual);
249    }
250
251    @Test
252    public void testExpandTemplateEscape2() {
253        String expected = "%%-grp";
254        EventContext ecStub = newEventContext();
255        ecStub.groupName = "grp";
256        String actual = this.tmri.expandTemplate("%%-%group%", ecStub);
257        Assert.assertEquals(expected, actual);
258    }
259
260    @Test
261    public void testExpandTemplateEscape3() {
262        String expected = "%%george";
263        EventContext ecStub = newEventContext();
264        String actual = this.tmri.expandTemplate("%%george", ecStub);
265        Assert.assertEquals(expected, actual);
266    }
267
268    @Test
269    public void testExpandTemplateUnknown() {
270        String expected = "%björk%";
271        EventContext ecStub = newEventContext();
272        String actual = this.tmri.expandTemplate("%björk%", ecStub);
273        Assert.assertEquals(expected, actual);
274    }
275
276    /**
277     * Test that the checksum algorithms offered by the managed repository
278     * correspond to those listed for enum id
279     * <tt>ome.model.enums.ChecksumAlgorithm</tt> in
280     * <tt>acquisition.ome.xml</tt>.
281     */
282    @Test
283    public void testListChecksumAlgorithms() {
284        final Set<String> expectedAlgorithmNames =
285                Sets.newHashSet("Adler-32", "CRC-32", "MD5-128", "Murmur3-32",
286                        "Murmur3-128", "SHA1-160");
287        for (final ChecksumAlgorithm algorithm :
288            this.tmri.listChecksumAlgorithms(curr)) {
289            Assert.assertTrue(expectedAlgorithmNames.remove(
290                    algorithm.getValue().getValue()));
291        }
292        Assert.assertTrue(expectedAlgorithmNames.isEmpty());
293    }
294
295    /**
296     * Test that the server does give checksum algorithm suggestions in accordance with its preferred algorithm.
297     */
298    @Test
299    public void testSuggestFavoredChecksumAlgorithm() {
300        final List<ChecksumAlgorithm> configured = this.tmri.listChecksumAlgorithms(curr);
301        final ChecksumAlgorithm favored = configured.get(0);
302        final String favoredName = ChecksumAlgorithmMapper.CHECKSUM_ALGORITHM_NAMER.apply(favored);
303
304        ChecksumAlgorithm suggestion;
305        String suggestionName;
306
307        suggestion = this.tmri.suggestChecksumAlgorithm(Collections.singletonList(favored), curr);
308        suggestionName = ChecksumAlgorithmMapper.CHECKSUM_ALGORITHM_NAMER.apply(suggestion);
309        Assert.assertEquals(favoredName, suggestionName);
310
311        suggestion = this.tmri.suggestChecksumAlgorithm(configured, curr);
312        suggestionName = ChecksumAlgorithmMapper.CHECKSUM_ALGORITHM_NAMER.apply(suggestion);
313        Assert.assertEquals(favoredName, suggestionName);
314    }
315
316    /**
317     * Test that the server does suggest a less-preferred checksum algorithm if the client does not support the preferred.
318     */
319    @Test
320    public void testSuggestUnfavoredChecksumAlgorithm() {
321        final List<ChecksumAlgorithm> configured = this.tmri.listChecksumAlgorithms(curr);
322        final ChecksumAlgorithm unfavored = configured.get(configured.size() - 1);
323        final String unfavoredName = ChecksumAlgorithmMapper.CHECKSUM_ALGORITHM_NAMER.apply(unfavored);
324
325        ChecksumAlgorithm suggestion;
326        String suggestionName;
327
328        suggestion = this.tmri.suggestChecksumAlgorithm(Collections.singletonList(unfavored), curr);
329        suggestionName = ChecksumAlgorithmMapper.CHECKSUM_ALGORITHM_NAMER.apply(suggestion);
330        Assert.assertEquals(unfavoredName, suggestionName);
331    }
332
333    /**
334     * Test that the server does report when no checksum algorithm is acceptable.
335     */
336    @Test
337    public void testSuggestNoChecksumAlgorithm() {
338        final ChecksumAlgorithm badAlgorithm = new ChecksumAlgorithmI();
339        badAlgorithm.setValue(rstring(UUID.randomUUID().toString()));
340
341        ChecksumAlgorithm suggestion;
342
343        suggestion = this.tmri.suggestChecksumAlgorithm(Collections.singletonList(badAlgorithm), curr);
344        Assert.assertNull(suggestion);
345    }
346}