Adding tests for Project
MODIFY
modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/expcatalog/ProjectRepository.java
modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/repositories/expcatalog/ProjectRepository.java
@@ -45,13 +45,20 @@ public class ProjectRepository extends ExpCatAbstractRepository<Project, Project
}
protected ProjectEntity saveProject(Project project, String gatewayId) throws RegistryException {
- String projectID = getProjectId(project.getName());
+ if (project.getProjectID() == null) {
+ logger.debug("Setting the Project's ProjectId");
+ project.setProjectID(getProjectId(project.getName()));
+ }
+
Mapper mapper = ObjectMapperSingleton.getInstance();
ProjectEntity projectEntity = mapper.map(project, ProjectEntity.class);
- projectEntity.setProjectID(projectID);
- projectEntity.setGatewayId(gatewayId);
- if (!isProjectExist(projectID)) {
+ if (project.getGatewayId() == null) {
+ logger.debug("Setting the Project's GatewayId");
+ projectEntity.setGatewayId(project.getGatewayId());
+ }
+
+ if (!isProjectExist(projectEntity.getProjectID())) {
logger.debug("Checking if the Project already exists");
projectEntity.setCreationTime(new Timestamp((System.currentTimeMillis())));
}
@@ -64,7 +71,7 @@ public class ProjectRepository extends ExpCatAbstractRepository<Project, Project
}
public void updateProject(Project project, String projectId) throws RegistryException {
- saveProjectData(project, null);
+ saveProjectData(project, projectId);
}
private String getProjectId(String projectName){
@@ -122,30 +129,35 @@ public class ProjectRepository extends ExpCatAbstractRepository<Project, Project
public List<Project> searchAllAccessibleProjects(List<String> accessibleProjectIds, Map<String, String> filters, int limit,
int offset, Object orderByIdentifier, ResultOrderType resultOrderType) throws RegistryException {
- String query = "SELECT DISTINCT P FROM " + ProjectEntity.class.getSimpleName() + " P WHERE ";
+ String query = "SELECT P FROM " + ProjectEntity.class.getSimpleName() + " P WHERE ";
+ Map<String, Object> queryParameters = new HashMap<>();
- if (!filters.isEmpty() && filters != null) {
+ if (filters != null && !filters.isEmpty()) {
for (String field : filters.keySet()) {
if (field.equals(DBConstants.Project.GATEWAY_ID)) {
logger.debug("Filter Projects by Gateway ID");
- query += "P.gatewayId LIKE :" + filters.get(field) + " AND ";
+ queryParameters.put(DBConstants.Project.GATEWAY_ID, filters.get(field));
+ query += "P.gatewayId LIKE :" + DBConstants.Project.GATEWAY_ID + " AND ";
}
else if (field.equals(DBConstants.Project.OWNER)) {
logger.debug("Filter Projects by Owner");
- query += "P.owner LIKE :" + filters.get(field) + " AND ";
+ queryParameters.put(DBConstants.Project.OWNER, filters.get(field));
+ query += "P.owner LIKE :" + DBConstants.Project.OWNER + " AND ";
}
else if (field.equals(DBConstants.Project.PROJECT_NAME)) {
logger.debug("Filter Projects by Project Name");
- query += "P.name LIKE :" + filters.get(field) + " AND ";
+ queryParameters.put(DBConstants.Project.PROJECT_NAME, filters.get(field));
+ query += "P.name LIKE :" + DBConstants.Project.PROJECT_NAME + " AND ";
}
else if (field.equals(DBConstants.Project.DESCRIPTION)) {
logger.debug("Filter Projects by Description");
- query += "P.description LIKE :" + filters.get(field) + " AND ";
+ queryParameters.put(DBConstants.Project.DESCRIPTION, filters.get(field));
+ query += "P.description LIKE :" + DBConstants.Project.DESCRIPTION + " AND ";
}
else {
@@ -159,11 +171,8 @@ public class ProjectRepository extends ExpCatAbstractRepository<Project, Project
if (accessibleProjectIds != null && !accessibleProjectIds.isEmpty()) {
logger.debug("Filter Projects by Accessible Project IDs");
- query += "P.projectId IN (";
- for(String projectId : accessibleProjectIds) {
- query += (":" + projectId + ",");
- }
- query = query.substring(0, query.length() - 1) + ")";
+ queryParameters.put(DBConstants.Project.ACCESSIBLE_PROJECT_IDS, accessibleProjectIds);
+ query += "P.projectID IN :" + DBConstants.Project.ACCESSIBLE_PROJECT_IDS;
}
else {
@@ -171,7 +180,7 @@ public class ProjectRepository extends ExpCatAbstractRepository<Project, Project
query = query.substring(0, query.length() - 5);
}
- List<Project> projectList = select(query, offset);
+ List<Project> projectList = select(query, limit, offset, queryParameters);
return projectList;
}

MODIFY
modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/utils/DBConstants.java
modules/registry-refactoring/src/main/java/org/apache/airavata/registry/core/utils/DBConstants.java
@@ -96,6 +96,7 @@ public class DBConstants {
public static final String OWNER = "owner";
public static final String PROJECT_NAME = "name";
public static final String DESCRIPTION = "description";
+ public static final String ACCESSIBLE_PROJECT_IDS = "accessibleProjectIds";
}
public static class Experiment {

MODIFY
modules/registry-refactoring/src/test/java/org/apache/airavata/registry/core/repositories/expcatalog/ProjectRepositoryTest.java
modules/registry-refactoring/src/test/java/org/apache/airavata/registry/core/repositories/expcatalog/ProjectRepositoryTest.java
package org.apache.airavata.registry.core.repositories.expcatalog;
+import org.apache.airavata.model.workspace.Gateway;
+import org.apache.airavata.model.workspace.Project;
import org.apache.airavata.registry.core.repositories.expcatalog.util.Initialize;
import org.apache.airavata.registry.core.utils.DBConstants;
import org.apache.airavata.registry.cpi.RegistryException;
@@ -9,6 +11,11 @@ import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
@@ -16,6 +23,8 @@ import static org.junit.Assert.assertFalse;
public class ProjectRepositoryTest {
private static Initialize initialize;
+ private String testGateway = "testGateway";
+ GatewayRepository gatewayRepository;
ProjectRepository projectRepository;
private static final Logger logger = LoggerFactory.getLogger(ProjectRepositoryTest.class);
@@ -24,6 +33,7 @@ public class ProjectRepositoryTest {
try {
initialize = new Initialize("expcatalog-derby.sql");
initialize.initializeDB();
+ gatewayRepository = new GatewayRepository();
projectRepository = new ProjectRepository();
} catch (Exception e) {
logger.error(e.getMessage(), e);
@@ -38,7 +48,42 @@ public class ProjectRepositoryTest {
@Test
public void ProjectRepositoryTest() throws RegistryException {
+ Gateway gateway = new Gateway();
+ gateway.setGatewayId(testGateway);
+ gateway.setDomain("SEAGRID");
+ gateway.setEmailAddress("abc@d.com");
+ String gatewayId = gatewayRepository.addGateway(gateway);
+
+ Project project = new Project();
+ project.setName("projectName");
+ project.setOwner("user");
+ project.setGatewayId(gatewayId);
+
+ String projectId = projectRepository.addProject(project, gatewayId);
+ assertTrue(projectId != null);
+
+ project.setDescription("projectDescription");
+ projectRepository.updateProject(project, null);
+
+ Project retrievedProject = projectRepository.getProject(projectId);
+ assertEquals(gatewayId, retrievedProject.getGatewayId());
+
+ assertTrue(projectRepository.getProjectIDs(DBConstants.Project.OWNER, "user").contains(projectId));
+
+ List<String> accessibleProjectIds = new ArrayList<>();
+ accessibleProjectIds.add(projectId);
+
+ Map<String, String> filters = new HashMap<>();
+ filters.put(DBConstants.Project.GATEWAY_ID, retrievedProject.getGatewayId());
+ filters.put(DBConstants.Project.OWNER, retrievedProject.getOwner());
+ filters.put(DBConstants.Project.PROJECT_NAME, retrievedProject.getName());
+ filters.put(DBConstants.Project.DESCRIPTION, retrievedProject.getDescription());
+
+ assertTrue(projectRepository.searchAllAccessibleProjects(accessibleProjectIds, filters,
+ -1, 0, null, null).size() == 1);
+ projectRepository.removeProject(projectId);
+ assertFalse(projectRepository.isProjectExist(projectId));
}
}

Adding tests for Project