From b780377b940f17c96152b1f96064a90cbaba4ddc Mon Sep 17 00:00:00 2001
From: cramakri <cramakri>
Date: Mon, 22 Nov 2010 11:43:17 +0000
Subject: [PATCH] LMS-1887 Initial work on the download replica command.

SVN: 18833
---
 .../client/util/cli/CinaCommandFactory.java   |   7 +-
 .../client/util/cli/CommandGetReplica.java    | 120 +++++++++++++
 .../util/cli/CommandGetReplicaTest.java       | 161 ++++++++++++++++++
 3 files changed, 286 insertions(+), 2 deletions(-)
 create mode 100644 rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CommandGetReplica.java
 create mode 100644 rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/cli/CommandGetReplicaTest.java

diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CinaCommandFactory.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CinaCommandFactory.java
index cfe4134a05e..a323750f4d2 100644
--- a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CinaCommandFactory.java
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CinaCommandFactory.java
@@ -32,13 +32,13 @@ public class CinaCommandFactory extends AbstractCommandFactory
 
     private static enum Command
     {
-        LISTSAMPS, GENCODE, LISTEXPS, HELP
+        LISTSAMPS, GENCODE, LISTEXPS, GETREPLICA, HELP
     }
 
     public List<String> getKnownCommands()
     {
         String[] commands =
-            { "listsamps", "gencode", "listexps" };
+            { "listsamps", "gencode", "listexps", "getreplica" };
         return Arrays.asList(commands);
     }
 
@@ -76,6 +76,9 @@ public class CinaCommandFactory extends AbstractCommandFactory
             case LISTEXPS:
                 result = new CommandExperimentLister();
                 break;
+            case GETREPLICA:
+                result = new CommandGetReplica();
+                break;
             case HELP:
                 result = new CommandHelp(this, PROGRAM_CALL_STRING);
                 break;
diff --git a/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CommandGetReplica.java b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CommandGetReplica.java
new file mode 100644
index 00000000000..fbf3ad367c5
--- /dev/null
+++ b/rtd_cina/source/java/ch/systemsx/cisd/cina/client/util/cli/CommandGetReplica.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.cina.client.util.cli;
+
+import java.util.ArrayList;
+
+import ch.systemsx.cisd.args4j.Option;
+import ch.systemsx.cisd.cina.client.util.v1.ICinaUtilities;
+import ch.systemsx.cisd.common.exceptions.EnvironmentFailureException;
+import ch.systemsx.cisd.common.exceptions.UserFailureException;
+import ch.systemsx.cisd.openbis.dss.client.api.cli.GlobalArguments;
+import ch.systemsx.cisd.openbis.dss.client.api.cli.ResultCode;
+
+/**
+ * @author Chandrasekhar Ramakrishnan
+ */
+public class CommandGetReplica extends
+        AbstractCinaCommand<CommandGetReplica.CommandGetReplicaArguments>
+{
+    static class CommandGetReplicaArguments extends GlobalArguments
+    {
+        @Option(name = "o", longName = "output", usage = "Output folder")
+        private String outputFolder = "";
+
+        public ArrayList<String> getReplicaIdentifiers()
+        {
+            ArrayList<String> replicaIds = new ArrayList<String>();
+            for (String replicaId : getArguments())
+            {
+                if (replicaId.length() > 0)
+                {
+                    replicaIds.add(replicaId.toUpperCase());
+                }
+            }
+            return replicaIds;
+        }
+
+        public String getOutputFolder()
+        {
+            return outputFolder;
+        }
+
+        @Override
+        public boolean isComplete()
+        {
+            if (getArguments().size() < 1)
+            {
+                return false;
+            }
+
+            if (getReplicaIdentifiers().size() < 1)
+            {
+                return false;
+            }
+
+            if (false == super.isComplete())
+                return false;
+
+            return true;
+        }
+    }
+
+    private static class GetReplicaExecutor extends AbstractExecutor<CommandGetReplicaArguments>
+    {
+        /**
+         * @param command The parent command
+         */
+        GetReplicaExecutor(CommandGetReplica command)
+        {
+            super(command);
+        }
+
+        @Override
+        protected ResultCode doExecute(ICinaUtilities component)
+        {
+            // Find all datasets connected to this sample
+            // Download the raw-data dataset
+            // Download the ...
+            // List<Experiment> results =
+            // component.listVisibleExperiments(arguments.getReplicaIdentifier());
+            return ResultCode.OK;
+        }
+    }
+
+    public CommandGetReplica()
+    {
+        super(new CommandGetReplicaArguments());
+    }
+
+    public ResultCode execute(String[] args) throws UserFailureException,
+            EnvironmentFailureException
+    {
+        return new GetReplicaExecutor(this).execute(args);
+    }
+
+    public String getName()
+    {
+        return "getreplica";
+    }
+
+    @Override
+    protected String getRequiredArgumentsString()
+    {
+        return "<replica identifier>";
+    }
+}
diff --git a/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/cli/CommandGetReplicaTest.java b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/cli/CommandGetReplicaTest.java
new file mode 100644
index 00000000000..f2fb0d7da05
--- /dev/null
+++ b/rtd_cina/sourceTest/java/ch/systemsx/cisd/cina/client/util/cli/CommandGetReplicaTest.java
@@ -0,0 +1,161 @@
+/*
+ * Copyright 2010 ETH Zuerich, CISD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package ch.systemsx.cisd.cina.client.util.cli;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.testng.AssertJUnit;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import ch.systemsx.cisd.cina.client.util.v1.ICinaUtilities;
+import ch.systemsx.cisd.openbis.dss.client.api.cli.ICommand;
+import ch.systemsx.cisd.openbis.dss.client.api.cli.ResultCode;
+import ch.systemsx.cisd.openbis.generic.shared.IETLLIMSService;
+import ch.systemsx.cisd.openbis.generic.shared.api.v1.IGeneralInformationService;
+
+/**
+ * @author Chandrasekhar Ramakrishnan
+ */
+public class CommandGetReplicaTest extends AssertJUnit
+{
+    private final class MockCommandGetReplica extends CommandGetReplica
+    {
+        @Override
+        protected ICinaUtilities login()
+        {
+            facade =
+                    ch.systemsx.cisd.cina.client.util.v1.impl.CinaUtilitiesFacadeTest.createFacade(
+                            service, openbisService, USER_ID, PASSWORD);
+            return facade;
+        }
+    }
+
+    private final static String USER_ID = "userid";
+
+    private final static String PASSWORD = "password";
+
+    private final static String SESSION_TOKEN = "sessionToken";
+
+    private Mockery context;
+
+    private ICinaUtilities facade;
+
+    private IGeneralInformationService service;
+
+    private IETLLIMSService openbisService;
+
+    @BeforeMethod
+    public void setUp()
+    {
+        context = new Mockery();
+        service = context.mock(IGeneralInformationService.class);
+        openbisService = context.mock(IETLLIMSService.class);
+    }
+
+    @AfterMethod
+    public void tearDown()
+    {
+        // To following line of code should also be called at the end of each test method.
+        // Otherwise one does not known which test failed.
+        context.assertIsSatisfied();
+    }
+
+    private void setupAuthenticationExpectations()
+    {
+        context.checking(new Expectations()
+            {
+                {
+                    one(service).tryToAuthenticateForAllServices(USER_ID, PASSWORD);
+                    will(returnValue(SESSION_TOKEN));
+                    one(service).logout(SESSION_TOKEN);
+                }
+            });
+
+    }
+
+    @Test
+    public void testCodePath()
+    {
+        setupAuthenticationExpectations();
+        context.checking(new Expectations()
+            {
+                {
+                    // final ArrayList<Project> projects = new ArrayList<Project>();
+                    // Project project = new Project("PROJECT-1", "SPACE-1");
+                    // projects.add(project);
+                    //
+                    // final ArrayList<Experiment> experiments = new ArrayList<Experiment>();
+                    //
+                    // final ArrayList<SpaceWithProjectsAndRoleAssignments> spaces =
+                    // new ArrayList<SpaceWithProjectsAndRoleAssignments>();
+                    // SpaceWithProjectsAndRoleAssignments space =
+                    // new SpaceWithProjectsAndRoleAssignments("SPACE-1");
+                    // space.add(project);
+                    // space.add("user", new Role("ADMIN", true));
+                    // spaces.add(space);
+
+                    // one(service).getMinorVersion();
+                    // will(returnValue(2));
+
+                    // one(service).listSpacesWithProjectsAndRoleAssignments(SESSION_TOKEN, null);
+                    // will(returnValue(spaces));
+                    //
+                    // one(service).listExperiments(SESSION_TOKEN, projects, "EXP-TYPE");
+                    // will(returnValue(experiments));
+
+                }
+            });
+
+        ICommand command = new MockCommandGetReplica();
+
+        ResultCode exitCode = command.execute(new String[]
+            { "-s", "url", "-u", USER_ID, "-p", PASSWORD, "REPLICA-ID" });
+
+        assertEquals(ResultCode.OK, exitCode);
+        context.assertIsSatisfied();
+    }
+
+    @Test
+    public void testOutputFolder()
+    {
+        setupAuthenticationExpectations();
+
+        ICommand command = new MockCommandGetReplica();
+
+        ResultCode exitCode = command.execute(new String[]
+            { "-s", "url", "-u", USER_ID, "-p", PASSWORD, "-o", "Foo.bundle/", "REPLICA-ID" });
+
+        assertEquals(ResultCode.OK, exitCode);
+        context.assertIsSatisfied();
+    }
+
+    @Test
+    public void testMultipleReplicas()
+    {
+        setupAuthenticationExpectations();
+
+        ICommand command = new MockCommandGetReplica();
+
+        ResultCode exitCode = command.execute(new String[]
+            { "-s", "url", "-u", USER_ID, "-p", PASSWORD, "REPLICA-ID1", "REPLICA-ID2" });
+
+        assertEquals(ResultCode.OK, exitCode);
+        context.assertIsSatisfied();
+    }
+}
-- 
GitLab