PageRenderTime 157ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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