PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/components/blitz/test/omero/cmd/fs/OriginalMetadataRequestTest.java

https://github.com/ximenesuk/openmicroscopy
Java | 167 lines | 122 code | 21 blank | 24 comment | 5 complexity | 26af6824540ae1b3dce2877f4f9da2ac MD5 | raw file
  1. /*
  2. * Copyright (C) 2013 Glencoe Software, Inc. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. package omero.cmd.fs;
  19. import java.io.FileNotFoundException;
  20. import java.util.ArrayList;
  21. import java.util.HashMap;
  22. import java.util.List;
  23. import java.util.Map;
  24. import java.util.Map.Entry;
  25. import ome.io.nio.PixelsService;
  26. import ome.services.blitz.test.AbstractServantTest;
  27. import ome.services.util.Executor;
  28. import ome.system.ServiceFactory;
  29. import omero.RString;
  30. import omero.RType;
  31. import omero.cmd.ERR;
  32. import omero.cmd.HandleI.Cancel;
  33. import omero.cmd.Helper;
  34. import omero.cmd.OriginalMetadataResponse;
  35. import omero.cmd.Request;
  36. import omero.cmd.Response;
  37. import omero.cmd.Status;
  38. import org.apache.commons.collections.CollectionUtils;
  39. import org.hibernate.Session;
  40. import org.springframework.transaction.annotation.Transactional;
  41. import org.springframework.util.ResourceUtils;
  42. import org.testng.Assert;
  43. import org.testng.annotations.BeforeClass;
  44. import org.testng.annotations.Test;
  45. import com.google.common.collect.ImmutableMap;
  46. import com.google.common.collect.ImmutableMap.Builder;
  47. @Test(groups = "integration")
  48. public class OriginalMetadataRequestTest extends AbstractServantTest {
  49. private static final ImmutableMap<String, String> expectedGlobalMetadata;
  50. private static final ImmutableMap<String, String> expectedSeriesMetadata;
  51. static {
  52. Builder<String, String> builder;
  53. builder = ImmutableMap.builder();
  54. builder.put("a=b", "c");
  55. builder.put("(a=b)", "c");
  56. builder.put("a", "(b=c)");
  57. builder.put("{a=b", "c}");
  58. builder.put("{(a=b)", "c}");
  59. builder.put("{a", "(b=c)}");
  60. builder.put("(p=q", "r");
  61. builder.put("p=q", "r)");
  62. builder.put("((p=q)", "r");
  63. builder.put("p", "(q=r))");
  64. builder.put("p q", "r s");
  65. expectedGlobalMetadata = builder.build();
  66. builder = ImmutableMap.builder();
  67. builder.put("ein må lære seg å krype før ein lærer å gå", "learn to walk before you can run");
  68. builder.put("money doesn't grow on trees", "pengar växer inte på träd");
  69. builder.put("аб", "вг");
  70. expectedSeriesMetadata = builder.build();
  71. }
  72. @Override
  73. @BeforeClass
  74. protected void setUp() throws Exception {
  75. super.setUp();
  76. }
  77. protected OriginalMetadataResponse assertRequest(
  78. final OriginalMetadataRequestI req, Map<String, String> ctx) {
  79. final Status status = new Status();
  80. @SuppressWarnings("unchecked")
  81. List<Object> rv = (List<Object>) user.ex.execute(ctx, user
  82. .getPrincipal(), new Executor.SimpleWork(this, "testRequest") {
  83. @Transactional(readOnly = false)
  84. public List<Object> doWork(Session session, ServiceFactory sf) {
  85. // from HandleI.steps()
  86. List<Object> rv = new ArrayList<Object>();
  87. Helper helper = new Helper((Request) req, status,
  88. getSqlAction(), session, sf);
  89. req.init(helper);
  90. int j = 0;
  91. while (j < status.steps) {
  92. try {
  93. rv.add(req.step(j));
  94. } catch (Cancel c) {
  95. throw c;
  96. } catch (Throwable t) {
  97. throw helper.cancel(new ERR(), t, "bad-step", "step",
  98. "" + j);
  99. }
  100. j++;
  101. }
  102. return rv;
  103. }
  104. });
  105. // Post-process
  106. for (int step = 0; step < status.steps; step++) {
  107. Object obj = rv.get(step);
  108. req.buildResponse(step, obj);
  109. }
  110. Response rsp = req.getResponse();
  111. if (rsp instanceof ERR) {
  112. fail(rsp.toString());
  113. }
  114. return (OriginalMetadataResponse) rsp;
  115. }
  116. @Test
  117. public void testFileset() throws Exception {
  118. OriginalMetadataRequestI req = new OriginalMetadataRequestI(
  119. (PixelsService) user.ctx.getBean("/OMERO/Pixels"));
  120. req.imageId = makeImage(); // FAILING
  121. OriginalMetadataResponse rsp = assertRequest(req, null);
  122. }
  123. /**
  124. * Test that pre-FS original_metadata.txt files are parsed as expected,
  125. * including selection of which "=" to split at, and non-ASCII characters.
  126. * @throws FileNotFoundException if the test INI-style file is not accessible
  127. */
  128. @Test
  129. public void testMetadataParsing() throws FileNotFoundException {
  130. final OriginalMetadataRequestI request = new OriginalMetadataRequestI(null);
  131. request.init(new Helper(request, new Status(), null, null, null));
  132. request.parseOriginalMetadataTxt(ResourceUtils.getFile("classpath:original_metadata.txt"));
  133. request.buildResponse(0, null);
  134. final OriginalMetadataResponse response = (OriginalMetadataResponse) request.getResponse();
  135. final Map<String, String> actualGlobalMetadata = new HashMap<String, String>();
  136. for (final Entry<String, RType> keyValue : response.globalMetadata.entrySet()) {
  137. actualGlobalMetadata.put(keyValue.getKey(), ((RString) keyValue.getValue()).getValue());
  138. }
  139. Assert.assertTrue(CollectionUtils.isEqualCollection(expectedGlobalMetadata.entrySet(), actualGlobalMetadata.entrySet()));
  140. final Map<String, String> actualSeriesMetadata = new HashMap<String, String>();
  141. for (final Entry<String, RType> keyValue : response.seriesMetadata.entrySet()) {
  142. actualSeriesMetadata.put(keyValue.getKey(), ((RString) keyValue.getValue()).getValue());
  143. }
  144. Assert.assertTrue(CollectionUtils.isEqualCollection(expectedSeriesMetadata.entrySet(), actualSeriesMetadata.entrySet()));
  145. }
  146. }