From cb600a17e76d2d11e64079b3ef7ae1b3ce30c90a Mon Sep 17 00:00:00 2001 From: Supun Nakandala Date: Sun, 26 Apr 2015 07:36:04 +0530 Subject: [PATCH 1/2] Adding searchWithPagination support for the RegistryCPI and RegistryImpl --- .../airavata-jpa-registry/bin/airavata.log | 8 + .../registry/jpa/impl/ExperimentRegistry.java | 31 +- .../jpa/impl/LoggingRegistryImpl.java | 5 + .../registry/jpa/impl/ProjectRegistry.java | 54 +++- .../registry/jpa/impl/RegistryImpl.java | 34 +- .../jpa/resources/AbstractResource.java | 7 +- .../jpa/resources/WorkerResource.java | 95 +++++- .../registry/jpa/RegistryUseCaseTest.java | 290 ++++++++++++++++++ .../airavata/registry/cpi/Registry.java | 16 + .../registry/cpi/ResultOrderType.java | 29 ++ 10 files changed, 547 insertions(+), 22 deletions(-) create mode 100644 modules/registry/airavata-jpa-registry/bin/airavata.log create mode 100644 modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/RegistryUseCaseTest.java create mode 100644 modules/registry/registry-cpi/src/main/java/org/apache/airavata/registry/cpi/ResultOrderType.java diff --git a/modules/registry/airavata-jpa-registry/bin/airavata.log b/modules/registry/airavata-jpa-registry/bin/airavata.log new file mode 100644 index 0000000000..ab618a9749 --- /dev/null +++ b/modules/registry/airavata-jpa-registry/bin/airavata.log @@ -0,0 +1,8 @@ +2014-06-28 01:14:45,167 [main] INFO org.apache.airavata.common.utils.ApplicationSettings - Settings loaded from jar:file:/home/supun/Projects/airavata/modules/configuration/client/target/airavata-client-configuration-0.13-SNAPSHOT.jar!/airavata-client.properties +2014-06-28 01:15:12,077 [main] INFO org.apache.airavata.persistance.registry.jpa.util.Initialize - New Database created for Registry +2014-12-19 22:36:21,197 [main] INFO org.apache.airavata.common.utils.ApplicationSettings - Settings loaded from jar:file:/home/supun/Projects/airavata/modules/configuration/client/target/airavata-client-configuration-0.14-SNAPSHOT.jar!/airavata-client.properties +2014-12-19 22:36:58,450 [main] INFO org.apache.airavata.persistance.registry.jpa.util.Initialize - New Database created for Registry +2015-01-06 04:25:20,025 [main] INFO org.apache.airavata.common.utils.ApplicationSettings - Settings loaded from jar:file:/home/supun/Projects/airavata/modules/configuration/client/target/airavata-client-configuration-0.14-SNAPSHOT.jar!/airavata-client.properties +2015-01-06 04:25:47,018 [main] INFO org.apache.airavata.persistance.registry.jpa.util.Initialize - New Database created for Registry +2015-04-18 01:18:17,329 [main] INFO org.apache.airavata.common.utils.ApplicationSettings - Settings loaded from jar:file:/home/supun/airavata/modules/configuration/client/target/airavata-client-configuration-0.15-SNAPSHOT.jar!/airavata-client.properties +2015-04-18 01:18:40,413 [main] INFO org.apache.airavata.persistance.registry.jpa.util.Initialize - New Database created for Registry diff --git a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/ExperimentRegistry.java b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/ExperimentRegistry.java index ec2c081c6d..2ad9d665f4 100644 --- a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/ExperimentRegistry.java +++ b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/ExperimentRegistry.java @@ -35,6 +35,7 @@ import org.apache.airavata.registry.cpi.CompositeIdentifier; import org.apache.airavata.registry.cpi.RegistryException; import org.apache.airavata.registry.cpi.RegistryModelType; +import org.apache.airavata.registry.cpi.ResultOrderType; import org.apache.airavata.registry.cpi.utils.Constants; import org.apache.airavata.registry.cpi.utils.StatusType; @@ -2795,7 +2796,33 @@ public void updateQOSParams(QualityOfServiceParams params, String id, String typ } } + /** + * To search experiments of user with the given filter criteria. All the matching results will be sent. + * Results are not ordered in any order + * @param filters + * @return + * @throws RegistryException + */ public List searchExperiments(Map filters) throws RegistryException { + return searchExperimentsWithPagination(filters, -1, -1, null, null); + } + + + /** + * To search the experiments of user with the given filter criteria and retrieve the results with + * pagination support. Results can be ordered based on an identifier (i.e column) either ASC or + * DESC. + * + * @param filters + * @param limit + * @param offset + * @param orderByIdentifier + * @param resultOrderType + * @return + * @throws RegistryException + */ + public List searchExperimentsWithPagination(Map filters, int limit, + int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException { Map fil = new HashMap(); if (filters != null && filters.size() != 0) { List experimentSummaries = new ArrayList(); @@ -2827,7 +2854,8 @@ public List searchExperiments(Map filters) th if (fil.containsKey(AbstractResource.ExperimentConstants.APPLICATION_ID)) { return searchExperimentsByApplication(fil); } else { - List experimentResources = workerResource.searchExperiments(fil); + List experimentResources = workerResource + .searchExperimentsWithPagination(fil, limit, offset, orderByIdentifier, resultOrderType); if (experimentResources != null && !experimentResources.isEmpty()) { for (ExperimentResource ex : experimentResources) { experimentSummaries.add(ThriftDataModelConversion.getExperimentSummary(ex)); @@ -2907,5 +2935,4 @@ public List searchExperimentsByCreationTime(Timestamp fromTim throw new RegistryException(e); } } - } diff --git a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/LoggingRegistryImpl.java b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/LoggingRegistryImpl.java index a288884766..d7e9c0adda 100644 --- a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/LoggingRegistryImpl.java +++ b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/LoggingRegistryImpl.java @@ -65,6 +65,11 @@ public List search(RegistryModelType dataType, Map filte return null; } + @Override + public List searchWithPagination(RegistryModelType dataType, Map filters, int limit, int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException { + return null; + } + @Override public Object getValue(RegistryModelType dataType, Object identifier, String field) throws RegistryException { return null; diff --git a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/ProjectRegistry.java b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/ProjectRegistry.java index ad2a32dd40..5bfcc3da4b 100644 --- a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/ProjectRegistry.java +++ b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/ProjectRegistry.java @@ -21,20 +21,20 @@ package org.apache.airavata.persistance.registry.jpa.impl; -import java.util.*; - import org.apache.airavata.common.utils.AiravataUtils; import org.apache.airavata.model.workspace.Project; -import org.apache.airavata.persistance.registry.jpa.Resource; import org.apache.airavata.persistance.registry.jpa.ResourceType; import org.apache.airavata.persistance.registry.jpa.ResourceUtils; import org.apache.airavata.persistance.registry.jpa.resources.*; import org.apache.airavata.persistance.registry.jpa.utils.ThriftDataModelConversion; import org.apache.airavata.registry.cpi.RegistryException; +import org.apache.airavata.registry.cpi.ResultOrderType; import org.apache.airavata.registry.cpi.utils.Constants; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.util.*; + public class ProjectRegistry { private GatewayResource gatewayResource; private WorkerResource workerResource; @@ -49,7 +49,8 @@ public ProjectRegistry(GatewayResource gatewayResource, UserResource user) throw if (!gatewayResource.isExists(ResourceType.GATEWAY_WORKER, user.getUserName())){ workerResource = ResourceUtils.addGatewayWorker(gatewayResource, user); }else { - workerResource = (WorkerResource)ResourceUtils.getWorker(gatewayResource.getGatewayId(), user.getUserName()); + workerResource = (WorkerResource)ResourceUtils.getWorker(gatewayResource.getGatewayId(), + user.getUserName()); } } @@ -71,7 +72,8 @@ public String addProject (Project project, String gatewayId) throws RegistryExce WorkerResource worker = new WorkerResource(project.getOwner(), workerResource.getGateway()); projectResource.setWorker(worker); projectResource.save(); - ProjectUserResource resource = (ProjectUserResource)projectResource.create(ResourceType.PROJECT_USER); + ProjectUserResource resource = (ProjectUserResource)projectResource.create( + ResourceType.PROJECT_USER); resource.setProjectId(project.getProjectID()); resource.setUserName(project.getOwner()); resource.save(); @@ -86,7 +88,8 @@ public String addProject (Project project, String gatewayId) throws RegistryExce List sharedUsers = project.getSharedUsers(); if (sharedUsers != null && !sharedUsers.isEmpty()){ for (String username : sharedUsers){ - ProjectUserResource pr = (ProjectUserResource)projectResource.create(ResourceType.PROJECT_USER); + ProjectUserResource pr = (ProjectUserResource)projectResource. + create(ResourceType.PROJECT_USER); pr.setUserName(username); pr.save(); } @@ -114,12 +117,14 @@ public void updateProject (Project project, String projectId) throws RegistryExc if (!gatewayResource.isExists(ResourceType.GATEWAY_WORKER, user.getUserName())){ workerResource = ResourceUtils.addGatewayWorker(gatewayResource, user); }else { - workerResource = (WorkerResource)ResourceUtils.getWorker(gatewayResource.getGatewayName(), user.getUserName()); + workerResource = (WorkerResource)ResourceUtils.getWorker( + gatewayResource.getGatewayName(), user.getUserName()); } WorkerResource worker = new WorkerResource(project.getOwner(), gatewayResource); existingProject.setWorker(worker); existingProject.save(); - ProjectUserResource resource = (ProjectUserResource)existingProject.create(ResourceType.PROJECT_USER); + ProjectUserResource resource = (ProjectUserResource)existingProject.create( + ResourceType.PROJECT_USER); resource.setProjectId(projectId); resource.setUserName(project.getOwner()); resource.save(); @@ -134,7 +139,8 @@ public void updateProject (Project project, String projectId) throws RegistryExc List sharedUsers = project.getSharedUsers(); if (sharedUsers != null && !sharedUsers.isEmpty()){ for (String username : sharedUsers){ - ProjectUserResource pr = (ProjectUserResource)existingProject.create(ResourceType.PROJECT_USER); + ProjectUserResource pr = (ProjectUserResource)existingProject.create( + ResourceType.PROJECT_USER); pr.setUserName(username); pr.save(); } @@ -178,7 +184,32 @@ public List getProjectList (String fieldName, Object value) throws Regi return projects; } + /** + * To search projects of user with the given filter criteria. All the matching results will be sent. + * Results are not ordered in any order + * @param filters + * @return + * @throws RegistryException + */ public List searchProjects (Map filters) throws RegistryException{ + return searchProjectsWithPagination(filters, -1, -1, null, null); + } + + /** + * To search the projects of user with the given filter criteria and retrieve the results with + * pagination support. Results can be ordered based on an identifier (i.e column) either ASC or + * DESC. + * + * @param filters + * @param limit + * @param offset + * @param orderByIdentifier + * @param resultOrderType + * @return + * @throws RegistryException + */ + public List searchProjectsWithPagination(Map filters, int limit, + int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException { Map fil = new HashMap(); if (filters != null && filters.size() != 0){ List projects = new ArrayList(); @@ -194,7 +225,8 @@ public List searchProjects (Map filters) throws Registr fil.put(AbstractResource.ProjectConstants.GATEWAY_ID, filters.get(field)); } } - List projectResources = workerResource.searchProjects(fil); + List projectResources = workerResource + .searchProjectsWithPagination(fil, limit, offset, orderByIdentifier, resultOrderType); if (projectResources != null && !projectResources.isEmpty()){ for (ProjectResource pr : projectResources){ projects.add(ThriftDataModelConversion.getProject(pr)); @@ -203,7 +235,7 @@ public List searchProjects (Map filters) throws Registr return projects; }catch (Exception e){ logger.error("Error while retrieving project from registry", e); - throw new RegistryException(e); + throw new RegistryException(e); } } return null; diff --git a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/RegistryImpl.java b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/RegistryImpl.java index 9dcf96fea3..a2f1e11377 100644 --- a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/RegistryImpl.java +++ b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/impl/RegistryImpl.java @@ -453,18 +453,48 @@ public List get(RegistryModelType dataType, String fieldName, Object val } + /** + * This method is to retrieve list of objects according to a given criteria + * @param dataType Data type is a predefined type which the programmer should choose according to the object he + * is going to save in to registry + * @param filters filters is a map of field name and value that you need to use for search filtration + * @return List of objects according to the given criteria + */ + @Override public List search(RegistryModelType dataType, Map filters) throws RegistryException { + return searchWithPagination(dataType, filters, -1, -1, null, null); + } + + /** + * This method is to retrieve list of objects with pagination according to a given criteria sorted + * according by the specified identified and specified ordering (i.e either ASC or DESC) + * @param dataType Data type is a predefined type which the programmer should choose according to the object he + * is going to save in to registry + * @param filters filters is a map of field name and value that you need to use for search filtration + * @param limit amount of the results to be returned + * @param offset offset of the results from the sorted list to be fetched from + * @param orderByIdentifier identifier (i.e the column) which will be used as the basis to sort the results + * @param resultOrderType The type of ordering (i.e ASC or DESC) that has to be used when retrieving the results + * @return List of objects according to the given criteria + */ + @Override + public List searchWithPagination(RegistryModelType dataType, Map filters, int limit, + int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException { try { List result = new ArrayList(); switch (dataType) { case PROJECT: - List projectList = projectRegistry.searchProjects(filters); + List projectList + = projectRegistry.searchProjectsWithPagination(filters, limit, offset, + orderByIdentifier, resultOrderType ); for (Project project : projectList ){ result.add(project); } return result; case EXPERIMENT: - List experimentSummaries = experimentRegistry.searchExperiments(filters); + List experimentSummaries = experimentRegistry + .searchExperimentsWithPagination(filters, limit, offset, orderByIdentifier, + resultOrderType ); for (ExperimentSummary ex : experimentSummaries){ result.add(ex); } diff --git a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/resources/AbstractResource.java b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/resources/AbstractResource.java index cb45087220..e05d59d07e 100644 --- a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/resources/AbstractResource.java +++ b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/resources/AbstractResource.java @@ -20,13 +20,13 @@ */ package org.apache.airavata.persistance.registry.jpa.resources; -import java.util.ArrayList; -import java.util.List; - import org.apache.airavata.persistance.registry.jpa.Resource; import org.apache.airavata.persistance.registry.jpa.ResourceType; import org.apache.airavata.registry.cpi.RegistryException; +import java.util.ArrayList; +import java.util.List; + public abstract class AbstractResource implements Resource { // table names public static final String GATEWAY = "Gateway"; @@ -94,6 +94,7 @@ public final class ProjectConstants { public static final String PROJECT_NAME = "project_name"; public static final String PROJECT_ID = "project_id"; public static final String DESCRIPTION = "description"; + public static final String CREATION_TIME = "creationTime"; } // Project table diff --git a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/resources/WorkerResource.java b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/resources/WorkerResource.java index 2654596f13..dfa887f64d 100644 --- a/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/resources/WorkerResource.java +++ b/modules/registry/airavata-jpa-registry/src/main/java/org/apache/airavata/persistance/registry/jpa/resources/WorkerResource.java @@ -26,6 +26,7 @@ import org.apache.airavata.persistance.registry.jpa.ResourceUtils; import org.apache.airavata.persistance.registry.jpa.model.*; import org.apache.airavata.persistance.registry.jpa.utils.QueryGenerator; +import org.apache.airavata.registry.cpi.ResultOrderType; import org.apache.airavata.registry.cpi.utils.StatusType; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -451,7 +452,33 @@ public void removeExperiment(String experimentId) throws RegistryException{ remove(ResourceType.EXPERIMENT, experimentId); } + /** + * To search projects of user with the given filter criteria. All the matching results will be sent. + * Results are not ordered in any order + * @param filters + * @return + * @throws RegistryException + */ public List searchProjects (Map filters) throws RegistryException{ + return searchProjectsWithPagination(filters, -1, -1, null, null); + } + + /** + * To search the projects of user with the given filter criteria and retrieve the results with + * pagination support. Results can be ordered based on an identifier (i.e column) either ASC or + * DESC. But in the current implementation ordering is only supported based on the project + * creation time + * + * @param filters + * @param limit + * @param offset + * @param orderByIdentifier + * @param resultOrderType + * @return + * @throws RegistryException + */ + public List searchProjectsWithPagination(Map filters, int limit, + int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException { List result = new ArrayList(); EntityManager em = null; try { @@ -472,13 +499,30 @@ public List searchProjects (Map filters) throws } } query = query.substring(0, query.length() - 5); + + //ordering + if( orderByIdentifier != null && resultOrderType != null + && orderByIdentifier.equals(ProjectConstants.CREATION_TIME)){ + String order = (resultOrderType == ResultOrderType.ASC) ? "ASC" : "DESC"; + query += " ORDER BY p." + ProjectConstants.CREATION_TIME + " " + order; + } + em = ResourceUtils.getEntityManager(); em.getTransaction().begin(); - Query q = em.createQuery(query); + Query q; + + //pagination + if(offset>=0 && limit >=0){ + q = em.createQuery(query).setFirstResult(offset).setMaxResults(limit); + }else{ + q = em.createQuery(query); + } + List resultList = q.getResultList(); for (Object o : resultList) { Project project = (Project) o; - ProjectResource projectResource = (ProjectResource) Utils.getResource(ResourceType.PROJECT, project); + ProjectResource projectResource = + (ProjectResource) Utils.getResource(ResourceType.PROJECT, project); result.add(projectResource); } em.getTransaction().commit(); @@ -497,7 +541,33 @@ public List searchProjects (Map filters) throws return result; } + /** + * To search experiments of user with the given filter criteria. All the matching results will be sent. + * Results are not ordered in any order + * @param filters + * @return + * @throws RegistryException + */ public List searchExperiments (Map filters) throws RegistryException{ + return searchExperimentsWithPagination(filters, -1, -1, null, null); + } + + /** + * To search the experiments of user with the given filter criteria and retrieve the results with + * pagination support. Results can be ordered based on an identifier (i.e column) either ASC or + * DESC. But in the current implementation ordering is only supported based on creationTime + * + * @param filters + * @param limit + * @param offset + * @param orderByIdentifier + * @param resultOrderType + * @return + * @throws RegistryException + */ + public List searchExperimentsWithPagination(Map filters, int limit, + int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException { + List result = new ArrayList(); EntityManager em = null; try { @@ -518,13 +588,30 @@ public List searchExperiments (Map filters) } } query = query.substring(0, query.length() - 5); + + //ordering + if( orderByIdentifier != null && resultOrderType != null + && orderByIdentifier.equals(ExperimentConstants.CREATION_TIME)){ + String order = (resultOrderType == ResultOrderType.ASC) ? "ASC" : "DESC"; + query += " ORDER BY e." + ExperimentConstants.CREATION_TIME + " " + order; + } + em = ResourceUtils.getEntityManager(); em.getTransaction().begin(); - Query q = em.createQuery(query); + Query q; + + //pagination + if(offset>=0 && limit >=0){ + q = em.createQuery(query).setFirstResult(offset).setMaxResults(limit); + }else{ + q = em.createQuery(query); + } + List resultList = q.getResultList(); for (Object o : resultList) { Experiment experiment = (Experiment) o; - ExperimentResource experimentResource = (ExperimentResource) Utils.getResource(ResourceType.EXPERIMENT, experiment); + ExperimentResource experimentResource = + (ExperimentResource) Utils.getResource(ResourceType.EXPERIMENT, experiment); result.add(experimentResource); } em.getTransaction().commit(); diff --git a/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/RegistryUseCaseTest.java b/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/RegistryUseCaseTest.java new file mode 100644 index 0000000000..17f4e71b64 --- /dev/null +++ b/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/RegistryUseCaseTest.java @@ -0,0 +1,290 @@ +/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.airavata.persistance.registry.jpa; + +import junit.framework.Assert; +import org.apache.airavata.model.appcatalog.appinterface.InputDataObjectType; +import org.apache.airavata.model.appcatalog.appinterface.OutputDataObjectType; +import org.apache.airavata.model.workspace.Project; +import org.apache.airavata.model.workspace.experiment.ComputationalResourceScheduling; +import org.apache.airavata.model.workspace.experiment.Experiment; +import org.apache.airavata.model.workspace.experiment.ExperimentSummary; +import org.apache.airavata.model.workspace.experiment.UserConfigurationData; +import org.apache.airavata.persistance.registry.jpa.impl.RegistryFactory; +import org.apache.airavata.persistance.registry.jpa.resources.AbstractResource; +import org.apache.airavata.persistance.registry.jpa.util.Initialize; +import org.apache.airavata.registry.cpi.*; +import org.apache.airavata.registry.cpi.utils.Constants; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; + +/** + * This class contains test cases for the RegistryImpl class which is the default registry + * implementation. These test cases are written from the perspective of the Airavata API + * such as creating/updating/deleting/searching projects and experiments etc. + */ +public class RegistryUseCaseTest { + + private static Registry registry; + private static Initialize initialize; + + @BeforeClass + public static void setupBeforeClass() throws RegistryException, SQLException { + initialize = new Initialize("registry-derby.sql"); + initialize.initializeDB(); + registry = RegistryFactory.getDefaultRegistry(); + } + + @Test + public void testProject(){ + try { + String TAG = System.currentTimeMillis() + ""; + + String gatewayId = "default"; + + //testing the creation of a project + Project project = new Project(); + project.setOwner("TestUser"+TAG); + project.setName("TestProject"+TAG); + project.setDescription("This is a test project"+TAG); + String projectId1 = (String)registry.add(ParentDataType.PROJECT, project, gatewayId); + Assert.assertNotNull(projectId1); + + //testing the update of a project + Project updatedProject = new Project(); + updatedProject.setProjectID(projectId1); + updatedProject.setOwner("TestUser"+TAG); + updatedProject.setName("UpdatedTestProject"+TAG); + updatedProject.setDescription("This is an updated test project"+TAG); + registry.update(RegistryModelType.PROJECT, updatedProject, projectId1); + + //testing project retrieval + Project retrievedProject = (Project)registry.get(RegistryModelType.PROJECT, projectId1); + Assert.assertEquals(updatedProject.getProjectID(), retrievedProject.getProjectID()); + Assert.assertEquals(updatedProject.getOwner(), retrievedProject.getOwner()); + Assert.assertEquals(updatedProject.getName(), retrievedProject.getName()); + Assert.assertEquals(updatedProject.getDescription(), retrievedProject.getDescription()); + Assert.assertNotNull(retrievedProject.getCreationTime()); + //created users should be in the shared users list + Assert.assertTrue(retrievedProject.getSharedUsers().size()==1); + + //creating more projects for the same user + project = new Project(); + project.setOwner("TestUser"+TAG); + project.setName("Project Terrible"+TAG); + project.setDescription("This is a test project_2"+TAG); + String projectId2 = (String)registry.add(ParentDataType.PROJECT, project, gatewayId); + Assert.assertNotNull(projectId2); + + project = new Project(); + project.setOwner("TestUser"+TAG); + project.setName("Project Funny"+TAG); + project.setDescription("This is a test project_3"+TAG); + String projectId3 = (String)registry.add(ParentDataType.PROJECT, project, gatewayId); + Assert.assertNotNull(projectId3); + + project = new Project(); + project.setOwner("TestUser"+TAG); + project.setName("Project Stupid"+TAG); + project.setDescription("This is a test project_4"+TAG); + String projectId4 = (String)registry.add(ParentDataType.PROJECT, project, gatewayId); + Assert.assertNotNull(projectId4); + + project = new Project(); + project.setOwner("TestUser"+TAG); + project.setName("Project Boring"+TAG); + project.setDescription("This is a test project_5"+TAG); + String projectId5 = (String)registry.add(ParentDataType.PROJECT, project, gatewayId); + Assert.assertNotNull(projectId5); + + //test get all projects created by the user + List list = registry.get(RegistryModelType.PROJECT, + Constants.FieldConstants.ProjectConstants.OWNER, "TestUser"+TAG); + Assert.assertTrue(list.size()==5); + + //search project by project name + Map filters = new HashMap(); + filters.put(Constants.FieldConstants.ProjectConstants.OWNER, "TestUser"+TAG); + filters.put(Constants.FieldConstants.ProjectConstants.PROJECT_NAME, "Terrible"+TAG); + list = registry.search(RegistryModelType.PROJECT, filters); + Assert.assertTrue(list.size()==1); + + //search project by project description + filters = new HashMap(); + filters.put(Constants.FieldConstants.ProjectConstants.OWNER, "TestUser"+TAG); + filters.put(Constants.FieldConstants.ProjectConstants.DESCRIPTION, "test project_2"+TAG); + list = registry.search(RegistryModelType.PROJECT, filters); + Assert.assertTrue(list.size()==1); + + //search project with only ownername + filters = new HashMap(); + filters.put(Constants.FieldConstants.ProjectConstants.OWNER, "TestUser"+TAG); + list = registry.search(RegistryModelType.PROJECT, filters); + Assert.assertTrue(list.size()==5); + + //search projects with pagination + filters = new HashMap(); + filters.put(Constants.FieldConstants.ProjectConstants.OWNER, "TestUser"+TAG); + list = registry.searchWithPagination(RegistryModelType.PROJECT, filters, 2, 2, + AbstractResource.ProjectConstants.CREATION_TIME, ResultOrderType.DESC); + Assert.assertTrue(list.size()==2); + Project project1 = (Project)list.get(0); + Project project2 = (Project)list.get(1); + Assert.assertTrue(project1.getCreationTime()-project2.getCreationTime() > 0); + } catch (RegistryException e) { + e.printStackTrace(); + Assert.fail(); + } + } + + @Test + public void testExperiment(){ + try { + String TAG = System.currentTimeMillis() + ""; + + String gatewayId = "default"; + + //creating project + Project project = new Project(); + project.setOwner("TestUser"+TAG); + project.setName("TestProject"+TAG); + project.setDescription("This is a test project"+TAG); + String projectId1 = (String)registry.add(ParentDataType.PROJECT, project, gatewayId); + Assert.assertNotNull(projectId1); + + //creating sample echo experiment. assumes echo application is already defined + InputDataObjectType inputDataObjectType = new InputDataObjectType(); + inputDataObjectType.setName("Input_to_Echo"); + inputDataObjectType.setValue("Hello World"); + + ComputationalResourceScheduling scheduling = new ComputationalResourceScheduling(); + scheduling.setResourceHostId(UUID.randomUUID().toString()); + scheduling.setComputationalProjectAccount("TG-STA110014S"); + scheduling.setTotalCPUCount(1); + scheduling.setNodeCount(1); + scheduling.setWallTimeLimit(15); + scheduling.setQueueName("normal"); + + UserConfigurationData userConfigurationData = new UserConfigurationData(); + userConfigurationData.setAiravataAutoSchedule(false); + userConfigurationData.setOverrideManualScheduledParams(false); + userConfigurationData.setComputationalResourceScheduling(scheduling); + + Experiment experiment = new Experiment(); + experiment.setProjectID(projectId1); + experiment.setUserName("TestUser" + TAG); + experiment.setName("TestExperiment"+TAG); + experiment.setDescription("Test 1 experiment"); + experiment.setApplicationId(UUID.randomUUID().toString()); + experiment.setUserConfigurationData(userConfigurationData); + experiment.addToExperimentInputs(inputDataObjectType); + + String experimentId1 = (String)registry.add(ParentDataType.EXPERIMENT, experiment, gatewayId); + Assert.assertNotNull(experimentId1); + + //retrieving the stored experiment + Experiment retrievedExperiment = (Experiment)registry.get(RegistryModelType.EXPERIMENT, + experimentId1); + Assert.assertNotNull(retrievedExperiment); + Assert.assertEquals(retrievedExperiment.getProjectID(), experiment.getProjectID()); + Assert.assertEquals(retrievedExperiment.getDescription(), experiment.getDescription()); + Assert.assertEquals(retrievedExperiment.getName(), experiment.getName()); + Assert.assertEquals(retrievedExperiment.getApplicationId(), experiment.getApplicationId()); + Assert.assertNotNull(retrievedExperiment.getUserConfigurationData()); + Assert.assertNotNull(retrievedExperiment.getExperimentInputs()); + + //updating an existing experiment + experiment.setName("NewExperimentName"+TAG); + OutputDataObjectType outputDataObjectType = new OutputDataObjectType(); + outputDataObjectType.setName("Output_to_Echo"); + outputDataObjectType.setValue("Hello World"); + experiment.addToExperimentOutputs(outputDataObjectType); + registry.update(RegistryModelType.EXPERIMENT, experiment, experimentId1); + + //creating more experiments + experiment = new Experiment(); + experiment.setProjectID(projectId1); + experiment.setUserName("TestUser" + TAG); + experiment.setName("TestExperiment2" + TAG); + experiment.setDescription("Test 2 experiment"); + experiment.setApplicationId(UUID.randomUUID().toString()); + experiment.setUserConfigurationData(userConfigurationData); + experiment.addToExperimentInputs(inputDataObjectType); + + String experimentId2 = (String)registry.add(ParentDataType.EXPERIMENT, experiment, gatewayId); + Assert.assertNotNull(experimentId1); + + experiment = new Experiment(); + experiment.setProjectID(projectId1); + experiment.setUserName("TestUser" + TAG); + experiment.setName("TestExperiment3"+TAG); + experiment.setDescription("Test 3 experiment"); + experiment.setApplicationId(UUID.randomUUID().toString()); + experiment.setUserConfigurationData(userConfigurationData); + experiment.addToExperimentInputs(inputDataObjectType); + + String experimentId3 = (String)registry.add(ParentDataType.EXPERIMENT, experiment, gatewayId); + Assert.assertNotNull(experimentId1); + + //searching experiments by name + Map filters = new HashMap(); + filters.put(Constants.FieldConstants.ExperimentConstants.USER_NAME, "TestUser" + TAG); + filters.put(Constants.FieldConstants.ExperimentConstants.GATEWAY, gatewayId); + filters.put(Constants.FieldConstants.ExperimentConstants.EXPERIMENT_NAME, "Experiment2"); + List results = registry.search(RegistryModelType.EXPERIMENT, filters); + Assert.assertTrue(results.size()==1); + + //retrieving all experiments in project + List list = registry.get(RegistryModelType.EXPERIMENT, + Constants.FieldConstants.ExperimentConstants.PROJECT_ID, projectId1); + Assert.assertTrue(list.size()==3); + + //searching all user experiments + filters = new HashMap(); + filters.put(Constants.FieldConstants.ExperimentConstants.USER_NAME, "TestUser" + TAG); + filters.put(Constants.FieldConstants.ExperimentConstants.GATEWAY, gatewayId); + list = registry.search(RegistryModelType.EXPERIMENT, filters); + Assert.assertTrue(list.size()==3); + + //searching user experiemets with pagination + filters = new HashMap(); + filters.put(Constants.FieldConstants.ExperimentConstants.USER_NAME, "TestUser" + TAG); + filters.put(Constants.FieldConstants.ExperimentConstants.GATEWAY, gatewayId); + list = registry.searchWithPagination(RegistryModelType.EXPERIMENT, filters, 2, 1, + AbstractResource.ExperimentConstants.CREATION_TIME, ResultOrderType.DESC); + Assert.assertTrue(list.size()==2); + ExperimentSummary exp1 = (ExperimentSummary)list.get(0); + ExperimentSummary exp2 = (ExperimentSummary)list.get(1); + Assert.assertTrue(exp1.getCreationTime()-exp2.getCreationTime() > 0); + + } catch (RegistryException e) { + e.printStackTrace(); + Assert.fail(); + } + } + +} \ No newline at end of file diff --git a/modules/registry/registry-cpi/src/main/java/org/apache/airavata/registry/cpi/Registry.java b/modules/registry/registry-cpi/src/main/java/org/apache/airavata/registry/cpi/Registry.java index 6d0ad4fc4f..c0bb0594fc 100644 --- a/modules/registry/registry-cpi/src/main/java/org/apache/airavata/registry/cpi/Registry.java +++ b/modules/registry/registry-cpi/src/main/java/org/apache/airavata/registry/cpi/Registry.java @@ -111,6 +111,22 @@ public interface Registry { */ public List search(RegistryModelType dataType, Map filters) throws RegistryException; + /** + * This method is to retrieve list of objects with pagination according to a given criteria sorted + * according by the specified identified and specified ordering (i.e either ASC or DESC) + * @param dataType Data type is a predefined type which the programmer should choose according to the object he + * is going to save in to registry + * @param filters filters is a map of field name and value that you need to use for search filtration + * @param limit amount of the results to be returned + * @param offset offset of the results from the sorted list to be fetched from + * @param orderByIdentifier identifier (i.e the column) which will be used as the basis to sort the results + * @param resultOrderType The type of ordering (i.e ASC or DESC) that has to be used when retrieving the results + * @return List of objects according to the given criteria + */ + public List searchWithPagination(RegistryModelType dataType, Map filters, + int limit, int offset, Object orderByIdentifier, + ResultOrderType resultOrderType) throws RegistryException; + /** * This method is to retrieve a specific value for a given field. * @param dataType Data type is a predefined type which the programmer should choose according to the object he diff --git a/modules/registry/registry-cpi/src/main/java/org/apache/airavata/registry/cpi/ResultOrderType.java b/modules/registry/registry-cpi/src/main/java/org/apache/airavata/registry/cpi/ResultOrderType.java new file mode 100644 index 0000000000..3988094302 --- /dev/null +++ b/modules/registry/registry-cpi/src/main/java/org/apache/airavata/registry/cpi/ResultOrderType.java @@ -0,0 +1,29 @@ +package org.apache.airavata.registry.cpi;/* + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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. + * +*/ + +/** + * Enum to specify the order type for a specific column when + * retrieving results + */ +public enum ResultOrderType { + ASC, + DESC +} From 7e3478e58f74de54869e2c3d83243e3350761995 Mon Sep 17 00:00:00 2001 From: Supun Nakandala Date: Sun, 26 Apr 2015 08:23:28 +0530 Subject: [PATCH 2/2] removing mistakenly commited log file --- modules/registry/airavata-jpa-registry/bin/airavata.log | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 modules/registry/airavata-jpa-registry/bin/airavata.log diff --git a/modules/registry/airavata-jpa-registry/bin/airavata.log b/modules/registry/airavata-jpa-registry/bin/airavata.log deleted file mode 100644 index ab618a9749..0000000000 --- a/modules/registry/airavata-jpa-registry/bin/airavata.log +++ /dev/null @@ -1,8 +0,0 @@ -2014-06-28 01:14:45,167 [main] INFO org.apache.airavata.common.utils.ApplicationSettings - Settings loaded from jar:file:/home/supun/Projects/airavata/modules/configuration/client/target/airavata-client-configuration-0.13-SNAPSHOT.jar!/airavata-client.properties -2014-06-28 01:15:12,077 [main] INFO org.apache.airavata.persistance.registry.jpa.util.Initialize - New Database created for Registry -2014-12-19 22:36:21,197 [main] INFO org.apache.airavata.common.utils.ApplicationSettings - Settings loaded from jar:file:/home/supun/Projects/airavata/modules/configuration/client/target/airavata-client-configuration-0.14-SNAPSHOT.jar!/airavata-client.properties -2014-12-19 22:36:58,450 [main] INFO org.apache.airavata.persistance.registry.jpa.util.Initialize - New Database created for Registry -2015-01-06 04:25:20,025 [main] INFO org.apache.airavata.common.utils.ApplicationSettings - Settings loaded from jar:file:/home/supun/Projects/airavata/modules/configuration/client/target/airavata-client-configuration-0.14-SNAPSHOT.jar!/airavata-client.properties -2015-01-06 04:25:47,018 [main] INFO org.apache.airavata.persistance.registry.jpa.util.Initialize - New Database created for Registry -2015-04-18 01:18:17,329 [main] INFO org.apache.airavata.common.utils.ApplicationSettings - Settings loaded from jar:file:/home/supun/airavata/modules/configuration/client/target/airavata-client-configuration-0.15-SNAPSHOT.jar!/airavata-client.properties -2015-04-18 01:18:40,413 [main] INFO org.apache.airavata.persistance.registry.jpa.util.Initialize - New Database created for Registry