Skip to content

Commit

Permalink
Merge pull request #6221 from jburel/import_as
Browse files Browse the repository at this point in the history
Import as
  • Loading branch information
jburel authored Jul 13, 2020
2 parents 6ec1b17 + a2a68c2 commit bf26dcb
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
import loci.formats.in.FakeReader;

import ome.formats.OMEROMetadataStoreClient;
import ome.formats.importer.ImportCandidates;
import ome.formats.importer.ImportConfig;
import ome.formats.importer.ImportContainer;
import ome.formats.importer.ImportEvent;
import ome.formats.importer.ImportLibrary;
import ome.formats.importer.ImportLibrary.ImportCallback;
import ome.formats.importer.IObservable;
import ome.formats.importer.IObserver;
import ome.formats.importer.OMEROWrapper;
import ome.formats.importer.util.ProportionalTimeEstimatorImpl;
import ome.formats.importer.util.TimeEstimator;
Expand Down Expand Up @@ -112,4 +116,45 @@ protected ImportLocation importFileset(List<String> srcPaths, int numberToUpload
Assert.assertNotNull(cb.getImportResponse());
return req.location;
}

/**
* Returns the import candidates corresponding to the specified file.
*
* @param f
* The file to handle.
* @return See above.
*/
protected ImportCandidates getCandidates(File f)
throws Exception
{
ImportConfig config = new ImportConfig();
OMEROWrapper reader = new OMEROWrapper(config);
String[] paths = new String[1];
paths[0] = f.getAbsolutePath();
IObserver o = new IObserver() {
public void update(IObservable importLibrary, ImportEvent event) {

}
};
return new ImportCandidates(reader, paths, o);
}

/**
* Import the image with the specified file name
*
* @param name The name of the file
*/
protected boolean importImageFile(String name)
throws Throwable
{
File f = File.createTempFile(name + ModelMockFactory.FORMATS[0], "."
+ ModelMockFactory.FORMATS[0]);
mmFactory.createImageFile(f, ModelMockFactory.FORMATS[0]);
f.deleteOnExit();
ImportConfig config = new ImportConfig();
ImportLibrary library = new ImportLibrary(createImporter(), new OMEROWrapper(
config));
ImportCandidates candidates = getCandidates(f);
return library.importCandidates(config, candidates);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2528,4 +2528,5 @@ protected EventContext addUser(EventContext user1, boolean isAdmin, boolean isGr
}
return newUser;
}

}
69 changes: 69 additions & 0 deletions components/tools/OmeroJava/test/integration/ImportAsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
*------------------------------------------------------------------------------
* Copyright (C) 2020 University of Dundee. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*------------------------------------------------------------------------------
*/
package integration;


import omero.api.ISessionPrx;
import omero.model.Session;
import omero.sys.EventContext;
import omero.sys.Principal;

import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

/**
* Tests import as another user
*
* @author Jean-Marie Burel &nbsp;&nbsp;&nbsp;&nbsp; <a
* href="mailto:[email protected]">[email protected]</a>
* @since 5.6.1
*/
@Test(groups = { "import", "integration"})
public class ImportAsTest extends AbstractServerImportTest {


@DataProvider(name = "permission")
public Object[][] dataProviderMethod() {
return new Object[][] { { "rwr---" }, { "rwra--" }, {"rwrw--"} };
}

@Test(dataProvider = "permission")
public void testImportAsGroupOwner(String permission) throws Throwable {
final EventContext sudoer = newUserAndGroup(permission, true);
final EventContext scientist = addUser(sudoer, false, false);

/* The sudoer takes on the scientist's identity. */
loginUser(sudoer);
final Principal principal = new Principal();
principal.name = scientist.userName;
principal.group = iAdmin.getEventContext().groupName;
principal.eventType = "User";
final ISessionPrx iSession = factory.getSessionService();
final Session session = iSession.createSessionWithTimeout(principal, 50000);
final omero.client client = newOmeroClient();
client.joinSession(session.getUuid().getValue());
init(client);
// Now we create an importer for that user
boolean value = importImageFile("testImportAsGroupOwner");
Assert.assertTrue(value);
}

}
35 changes: 2 additions & 33 deletions components/tools/OmeroJava/test/integration/ImportLibraryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@

import loci.formats.in.FakeReader;

import ome.formats.importer.IObservable;
import ome.formats.importer.IObserver;
import ome.formats.importer.ImportCandidates;
import ome.formats.importer.ImportConfig;
import ome.formats.importer.ImportContainer;
import ome.formats.importer.ImportEvent;
import ome.formats.importer.ImportLibrary;
import ome.formats.importer.OMEROWrapper;
import ome.services.blitz.repo.path.ClientFilePathTransformer;
Expand Down Expand Up @@ -76,7 +73,7 @@
* @since 5.0.0
*/
@Test(groups = { "import", "integration", "fs" })
public class ImportLibraryTest extends AbstractServerTest {
public class ImportLibraryTest extends AbstractServerImportTest {

/**
* Tests the <code>ImportImage</code> method using an import container
Expand All @@ -95,15 +92,7 @@ private void importImage(String permissions, int userRole, String name)
throws Throwable {
// create a new group and user
login(permissions, userRole);
File f = File.createTempFile(name + ModelMockFactory.FORMATS[0], "."
+ ModelMockFactory.FORMATS[0]);
mmFactory.createImageFile(f, ModelMockFactory.FORMATS[0]);
f.deleteOnExit();
ImportConfig config = new ImportConfig();
ImportLibrary library = new ImportLibrary(createImporter(), new OMEROWrapper(
config));
ImportCandidates candidates = getCandidates(f);
Assert.assertTrue(library.importCandidates(config, candidates));
Assert.assertTrue(importImageFile(name));
}

/**
Expand Down Expand Up @@ -224,26 +213,6 @@ protected void setUp() throws Exception {
public void tearDown() throws Exception {
}

/**
* Returns the import candidates corresponding to the specified file.
*
* @param f
* The file to handle.
* @return See above.
*/
private ImportCandidates getCandidates(File f) throws Exception {
ImportConfig config = new ImportConfig();
OMEROWrapper reader = new OMEROWrapper(config);
String[] paths = new String[1];
paths[0] = f.getAbsolutePath();
IObserver o = new IObserver() {
public void update(IObservable importLibrary, ImportEvent event) {

}
};
return new ImportCandidates(reader, paths, o);
}

/**
* Tests the import of an image into a <code>RW----</code> group by a
* general member.
Expand Down

0 comments on commit bf26dcb

Please sign in to comment.