From c0c0cc9aa8912faa67ee6b1956b4ac8f5de3ef44 Mon Sep 17 00:00:00 2001
From: pkupczyk <pkupczyk>
Date: Fri, 17 Oct 2014 14:44:54 +0000
Subject: [PATCH] SSDM-942 : V3 AS API - authorization predicates - test
 context description for experiment operations

SVN: 32626
---
 .../experiment/CreateExperimentExecutor.java  | 11 +++---
 .../experiment/UpdateExperimentExecutor.java  |  7 +++-
 .../ExperimentContextDescription.java         | 38 +++++++++++++++++++
 .../sample/SampleContextDescription.java      | 38 +++++++++++++++++++
 .../systemtest/api/v3/AbstractTest.java       | 19 ++++++++++
 .../api/v3/CreateExperimentTest.java          |  5 ++-
 .../api/v3/UpdateExperimentTest.java          | 15 +++++---
 .../exceptions/ObjectNotFoundException.java   |  2 +-
 .../UnauthorizedObjectAccessException.java    |  2 +-
 9 files changed, 121 insertions(+), 16 deletions(-)
 create mode 100644 openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/experiment/ExperimentContextDescription.java
 create mode 100644 openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sample/SampleContextDescription.java

diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/experiment/CreateExperimentExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/experiment/CreateExperimentExecutor.java
index b95e45aeded..d46166f53db 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/experiment/CreateExperimentExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/experiment/CreateExperimentExecutor.java
@@ -28,6 +28,7 @@ import ch.ethz.sis.openbis.generic.server.api.v3.executor.entity.IGetEntityTypeB
 import ch.ethz.sis.openbis.generic.server.api.v3.executor.project.IGetProjectByIdExecutor;
 import ch.ethz.sis.openbis.generic.server.api.v3.executor.property.IUpdateEntityPropertyExecutor;
 import ch.ethz.sis.openbis.generic.server.api.v3.executor.tag.IAddTagToEntityExecutor;
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.experiment.ExperimentContextDescription;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.experiment.ExperimentCreation;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.entitytype.IEntityTypeId;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.experiment.ExperimentPermId;
@@ -88,14 +89,14 @@ public class CreateExperimentExecutor implements ICreateExperimentExecutor
     {
         List<ExperimentPermId> result = new LinkedList<ExperimentPermId>();
 
-        for (ExperimentCreation experimentCreation : creations)
+        for (ExperimentCreation creation : creations)
         {
-            context.pushContextDescription("register experiment " + experimentCreation.getCode());
+            context.pushContextDescription(ExperimentContextDescription.creating(creation));
 
-            ExperimentPE experiment = createExperimentPE(context, experimentCreation);
+            ExperimentPE experiment = createExperimentPE(context, creation);
             daoFactory.getExperimentDAO().createOrUpdateExperiment(experiment, context.getSession().tryGetPerson());
-            createAttachmentExecutor.create(context, experiment, experimentCreation.getAttachments());
-            addTagToEntityExecutor.add(context, experiment, experimentCreation.getTagIds());
+            createAttachmentExecutor.create(context, experiment, creation.getAttachments());
+            addTagToEntityExecutor.add(context, experiment, creation.getTagIds());
             result.add(new ExperimentPermId(experiment.getPermId()));
 
             context.popContextDescription();
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/experiment/UpdateExperimentExecutor.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/experiment/UpdateExperimentExecutor.java
index fb9c075a022..127f213a88e 100644
--- a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/experiment/UpdateExperimentExecutor.java
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/executor/experiment/UpdateExperimentExecutor.java
@@ -32,6 +32,7 @@ import org.springframework.stereotype.Component;
 import ch.ethz.sis.openbis.generic.server.api.v3.executor.IOperationContext;
 import ch.ethz.sis.openbis.generic.server.api.v3.executor.property.IUpdateEntityPropertyExecutor;
 import ch.ethz.sis.openbis.generic.server.api.v3.executor.tag.IUpdateTagForEntityExecutor;
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.experiment.ExperimentContextDescription;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.experiment.ExperimentUpdate;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.experiment.IExperimentId;
 import ch.ethz.sis.openbis.generic.shared.api.v3.exceptions.ObjectNotFoundException;
@@ -112,6 +113,8 @@ public class UpdateExperimentExecutor implements IUpdateExperimentExecutor
 
         for (IExperimentId experimentId : experimentIds)
         {
+            context.pushContextDescription(ExperimentContextDescription.updating(experimentId));
+
             ExperimentPE experiment = experimentMap.get(experimentId);
             if (experiment == null)
             {
@@ -121,6 +124,8 @@ public class UpdateExperimentExecutor implements IUpdateExperimentExecutor
             {
                 throw new UnauthorizedObjectAccessException(experimentId);
             }
+
+            context.popContextDescription();
         }
 
         Map<ExperimentUpdate, ExperimentPE> result = new HashMap<ExperimentUpdate, ExperimentPE>();
@@ -134,7 +139,7 @@ public class UpdateExperimentExecutor implements IUpdateExperimentExecutor
 
     private void updateExperiment(IOperationContext context, ExperimentUpdate update, ExperimentPE experiment)
     {
-        context.pushContextDescription("update experiment " + update.getExperimentId());
+        context.pushContextDescription(ExperimentContextDescription.updating(update.getExperimentId()));
 
         updateExperimentProjectExecutor.update(context, experiment, update.getProjectId());
         updateEntityPropertyExecutor.update(context, experiment, experiment.getEntityType(), update.getProperties());
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/experiment/ExperimentContextDescription.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/experiment/ExperimentContextDescription.java
new file mode 100644
index 00000000000..e696a73f105
--- /dev/null
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/experiment/ExperimentContextDescription.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2014 ETH Zuerich, Scientific IT Services
+ *
+ * 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.ethz.sis.openbis.generic.server.api.v3.helper.experiment;
+
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.experiment.ExperimentCreation;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.experiment.IExperimentId;
+
+/**
+ * @author pkupczyk
+ */
+public class ExperimentContextDescription
+{
+
+    public static String creating(ExperimentCreation creation)
+    {
+        return "Creating experiment: " + creation.getCode() + " in project: " + creation.getProjectId();
+    }
+
+    public static String updating(IExperimentId experimentId)
+    {
+        return "Updating experiment: " + experimentId;
+    }
+
+}
diff --git a/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sample/SampleContextDescription.java b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sample/SampleContextDescription.java
new file mode 100644
index 00000000000..6a4dfb98a9c
--- /dev/null
+++ b/openbis/source/java/ch/ethz/sis/openbis/generic/server/api/v3/helper/sample/SampleContextDescription.java
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2014 ETH Zuerich, Scientific IT Services
+ *
+ * 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.ethz.sis.openbis.generic.server.api.v3.helper.sample;
+
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.sample.SampleCreation;
+import ch.ethz.sis.openbis.generic.shared.api.v3.dto.id.sample.ISampleId;
+
+/**
+ * @author pkupczyk
+ */
+public class SampleContextDescription
+{
+
+    public static String creating(SampleCreation creation)
+    {
+        return "Creating sample: " + creation.getCode() + " in space: " + creation.getSpaceId() + " in experiment: " + creation.getExperimentId();
+    }
+
+    public static String updating(ISampleId sampleId)
+    {
+        return "Updating sample: " + sampleId;
+    }
+
+}
diff --git a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/AbstractTest.java b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/AbstractTest.java
index 56bc7f47602..92ca9ff9e2f 100644
--- a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/AbstractTest.java
+++ b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/AbstractTest.java
@@ -38,6 +38,7 @@ import ch.ethz.sis.openbis.generic.shared.api.v3.exceptions.ObjectNotFoundExcept
 import ch.ethz.sis.openbis.generic.shared.api.v3.exceptions.UnauthorizedObjectAccessException;
 import ch.systemsx.cisd.common.action.IDelegatedAction;
 import ch.systemsx.cisd.common.logging.BufferedAppender;
+import ch.systemsx.cisd.common.test.AssertionUtil;
 import ch.systemsx.cisd.openbis.systemtest.SystemTestCase;
 
 /**
@@ -363,6 +364,11 @@ public class AbstractTest extends SystemTestCase
     }
 
     protected void assertUnauthorizedObjectAccessException(IDelegatedAction action, IObjectId expectedObjectId)
+    {
+        assertUnauthorizedObjectAccessException(action, expectedObjectId, null);
+    }
+
+    protected void assertUnauthorizedObjectAccessException(IDelegatedAction action, IObjectId expectedObjectId, String expectedContextDescription)
     {
         try
         {
@@ -373,10 +379,19 @@ public class AbstractTest extends SystemTestCase
             assertNotNull(e.getCause());
             assertEquals(e.getCause().getClass(), UnauthorizedObjectAccessException.class);
             assertEquals(((UnauthorizedObjectAccessException) e.getCause()).getObjectId(), expectedObjectId);
+            if (expectedContextDescription != null)
+            {
+                AssertionUtil.assertContains("(Context: [" + expectedContextDescription + "])", e.getMessage());
+            }
         }
     }
 
     protected void assertObjectNotFoundException(IDelegatedAction action, IObjectId expectedObjectId)
+    {
+        assertObjectNotFoundException(action, expectedObjectId, null);
+    }
+
+    protected void assertObjectNotFoundException(IDelegatedAction action, IObjectId expectedObjectId, String expectedContextDescription)
     {
         try
         {
@@ -387,6 +402,10 @@ public class AbstractTest extends SystemTestCase
             assertNotNull(e.getCause());
             assertEquals(e.getCause().getClass(), ObjectNotFoundException.class);
             assertEquals(((ObjectNotFoundException) e.getCause()).getObjectId(), expectedObjectId);
+            if (expectedContextDescription != null)
+            {
+                AssertionUtil.assertContains("(Context: [" + expectedContextDescription + "])", e.getMessage());
+            }
         }
     }
 
diff --git a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/CreateExperimentTest.java b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/CreateExperimentTest.java
index c1c556d8f25..8daa862e5e4 100644
--- a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/CreateExperimentTest.java
+++ b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/CreateExperimentTest.java
@@ -28,6 +28,7 @@ import junit.framework.Assert;
 
 import org.testng.annotations.Test;
 
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.experiment.ExperimentContextDescription;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.attachment.Attachment;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.attachment.AttachmentCreation;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.experiment.Experiment;
@@ -67,7 +68,7 @@ public class CreateExperimentTest extends AbstractExperimentTest
                 {
                     v3api.createExperiments(sessionToken, Arrays.asList(experiment));
                 }
-            }, projectId);
+            }, projectId, ExperimentContextDescription.creating(experiment));
     }
 
     @Test
@@ -88,7 +89,7 @@ public class CreateExperimentTest extends AbstractExperimentTest
                 {
                     v3api.createExperiments(sessionToken, Arrays.asList(experiment));
                 }
-            }, projectId);
+            }, projectId, ExperimentContextDescription.creating(experiment));
     }
 
     @Test
diff --git a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/UpdateExperimentTest.java b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/UpdateExperimentTest.java
index 4d82484f8d9..877f46f2e7e 100644
--- a/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/UpdateExperimentTest.java
+++ b/openbis/sourceTest/java/ch/ethz/sis/openbis/systemtest/api/v3/UpdateExperimentTest.java
@@ -23,6 +23,7 @@ import java.util.List;
 
 import org.testng.annotations.Test;
 
+import ch.ethz.sis.openbis.generic.server.api.v3.helper.experiment.ExperimentContextDescription;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.experiment.Experiment;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.experiment.ExperimentCreation;
 import ch.ethz.sis.openbis.generic.shared.api.v3.dto.entity.experiment.ExperimentUpdate;
@@ -86,7 +87,7 @@ public class UpdateExperimentTest extends AbstractExperimentTest
                 {
                     v3api.updateExperiments(sessionToken, Arrays.asList(update));
                 }
-            }, experimentId);
+            }, experimentId, ExperimentContextDescription.updating(experimentId));
     }
 
     @Test
@@ -105,7 +106,7 @@ public class UpdateExperimentTest extends AbstractExperimentTest
                 {
                     v3api.updateExperiments(sessionToken, Arrays.asList(update));
                 }
-            }, experimentId);
+            }, experimentId, ExperimentContextDescription.updating(experimentId));
     }
 
     @Test
@@ -113,9 +114,10 @@ public class UpdateExperimentTest extends AbstractExperimentTest
     {
         final String sessionToken = v3api.login(TEST_SPACE_USER, PASSWORD);
 
+        final IExperimentId experimentId = new ExperimentPermId("200902091255058-1037");
         final IProjectId projectId = new ProjectIdentifier("/CISD/NEMO");
         final ExperimentUpdate update = new ExperimentUpdate();
-        update.setExperimentId(new ExperimentPermId("200902091255058-1037"));
+        update.setExperimentId(experimentId);
         update.setProjectId(projectId);
 
         assertUnauthorizedObjectAccessException(new IDelegatedAction()
@@ -125,7 +127,7 @@ public class UpdateExperimentTest extends AbstractExperimentTest
                 {
                     v3api.updateExperiments(sessionToken, Arrays.asList(update));
                 }
-            }, projectId);
+            }, projectId, ExperimentContextDescription.updating(experimentId));
     }
 
     @Test
@@ -133,9 +135,10 @@ public class UpdateExperimentTest extends AbstractExperimentTest
     {
         final String sessionToken = v3api.login(TEST_SPACE_USER, PASSWORD);
 
+        final IExperimentId experimentId = new ExperimentPermId("200902091255058-1037");
         final IProjectId projectId = new ProjectIdentifier("IDONTEXIST");
         final ExperimentUpdate update = new ExperimentUpdate();
-        update.setExperimentId(new ExperimentPermId("200902091255058-1037"));
+        update.setExperimentId(experimentId);
         update.setProjectId(projectId);
 
         assertObjectNotFoundException(new IDelegatedAction()
@@ -145,7 +148,7 @@ public class UpdateExperimentTest extends AbstractExperimentTest
                 {
                     v3api.updateExperiments(sessionToken, Arrays.asList(update));
                 }
-            }, projectId);
+            }, projectId, ExperimentContextDescription.updating(experimentId));
     }
 
     @Test
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/exceptions/ObjectNotFoundException.java b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/exceptions/ObjectNotFoundException.java
index 966dd8d3c53..c9903a645c2 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/exceptions/ObjectNotFoundException.java
+++ b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/exceptions/ObjectNotFoundException.java
@@ -31,7 +31,7 @@ public class ObjectNotFoundException extends UserFailureException
 
     public ObjectNotFoundException(IObjectId id)
     {
-        super("Object with id [" + id + "] has not been found.");
+        super("Object with " + id.getClass().getSimpleName() + " = [" + id + "] has not been found.");
         this.objectId = id;
     }
 
diff --git a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/exceptions/UnauthorizedObjectAccessException.java b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/exceptions/UnauthorizedObjectAccessException.java
index 415184838d8..4bbf3d9952f 100644
--- a/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/exceptions/UnauthorizedObjectAccessException.java
+++ b/openbis_api/source/java/ch/ethz/sis/openbis/generic/shared/api/v3/exceptions/UnauthorizedObjectAccessException.java
@@ -31,7 +31,7 @@ public class UnauthorizedObjectAccessException extends UserFailureException
 
     public UnauthorizedObjectAccessException(IObjectId id)
     {
-        super("Object with id [" + id + "]  cannot be accessed.");
+        super("Access denied to object with " + id.getClass().getSimpleName() + " = [" + id + "].");
         this.objectId = id;
     }
 
-- 
GitLab