Added: airavata/trunk/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/WorkflowDataResourceTest.java URL: http://svn.apache.org/viewvc/airavata/trunk/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/WorkflowDataResourceTest.java?rev=1397168&view=auto ============================================================================== --- airavata/trunk/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/WorkflowDataResourceTest.java (added) +++ airavata/trunk/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/WorkflowDataResourceTest.java Thu Oct 11 17:06:09 2012 @@ -0,0 +1,112 @@ +/* +* +* 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.TestCase; +import org.apache.airavata.persistance.registry.jpa.resources.*; + +import java.sql.Timestamp; +import java.util.Calendar; + +public class WorkflowDataResourceTest extends TestCase { + private GatewayResource gatewayResource; + private ExperimentResource experimentResource; + private WorkerResource workerResource; + private ExperimentDataResource experimentDataResource; + private WorkflowDataResource workflowDataResource; + + @Override + public void setUp() throws Exception { + gatewayResource = (GatewayResource)ResourceUtils.getGateway("gateway1"); + workerResource = (WorkerResource)ResourceUtils.getWorker(gatewayResource.getGatewayName(), "testUser"); + + experimentResource = (ExperimentResource)gatewayResource.create(ResourceType.EXPERIMENT); + experimentResource.setExpID("testExpID"); + experimentResource.setWorker(workerResource); + + experimentDataResource = (ExperimentDataResource)experimentResource.create(ResourceType.EXPERIMENT_DATA); + experimentDataResource.setExpName("testExp"); + experimentDataResource.setUserName(workerResource.getUser()); + experimentDataResource.save(); + + workflowDataResource = (WorkflowDataResource)experimentDataResource.create(ResourceType.WORKFLOW_DATA); + workflowDataResource.setWorkflowInstanceID("testWFInstance"); + workflowDataResource.setTemplateName("testTemplate"); + workflowDataResource.setExperimentID("testExpID"); + Calendar calender = Calendar.getInstance(); + java.util.Date d = calender.getTime(); + Timestamp timestamp = new Timestamp(d.getTime()); + workflowDataResource.setLastUpdatedTime(timestamp); + + super.setUp(); + } + + public void testCreate() throws Exception { + NodeDataResource nodeDataResource = workflowDataResource.createNodeData("testNodeID"); + GramDataResource gramDataResource = workflowDataResource.createGramData("testNodeID"); + + nodeDataResource.setWorkflowDataResource(workflowDataResource); + nodeDataResource.setInputs("testInput"); + nodeDataResource.setOutputs("testOutput"); + nodeDataResource.setStatus("testStatus"); + nodeDataResource.save(); + + gramDataResource.setRsl("testRSL"); + gramDataResource.setWorkflowDataResource(workflowDataResource); + gramDataResource.save(); + + assertNotNull("node data resource created successfully", nodeDataResource); + assertNotNull("gram data resource created successfully", gramDataResource); + } + + public void testGet() throws Exception { + assertNotNull("Node data retrieved successfully", workflowDataResource.getNodeData("testNodeID")); + assertNotNull("Gram data retrieved successfully", workflowDataResource.getGramData("testNodeID")); + } + + public void testGetList() throws Exception { + assertNotNull("Node data retrieved successfully", workflowDataResource.getNodeData()); + assertNotNull("Gram data retrieved successfully", workflowDataResource.getGramData()); + } + + public void testRemove() throws Exception { + workflowDataResource.removeNodeData("testNodeID"); + workflowDataResource.removeGramData("testNodeID"); + if(!workflowDataResource.isNodeExists("testNodeID")){ + assertTrue("node date removed successfully", true); + } + if(!workflowDataResource.isGramDataExists("testNodeID")){ + assertTrue("gram date removed successfully", true); + } + } + + public void testSave() throws Exception { + workflowDataResource.save(); + if(experimentDataResource.isWorkflowInstancePresent("testWFInstance")){ + assertTrue("workflow data saved successfully", true); + } + } + + + + +}
Added: airavata/trunk/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/util/Initialize.java URL: http://svn.apache.org/viewvc/airavata/trunk/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/util/Initialize.java?rev=1397168&view=auto ============================================================================== --- airavata/trunk/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/util/Initialize.java (added) +++ airavata/trunk/modules/registry/airavata-jpa-registry/src/test/java/org/apache/airavata/persistance/registry/jpa/util/Initialize.java Thu Oct 11 17:06:09 2012 @@ -0,0 +1,258 @@ +/* + * + * 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.util; + +import org.apache.airavata.persistance.registry.jpa.ResourceType; +import org.apache.airavata.persistance.registry.jpa.ResourceUtils; +import org.apache.airavata.persistance.registry.jpa.resources.GatewayResource; +import org.apache.airavata.persistance.registry.jpa.resources.UserResource; +import org.apache.airavata.persistance.registry.jpa.resources.Utils; +import org.apache.airavata.persistance.registry.jpa.resources.WorkerResource; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.apache.derby.drda.NetworkServerControl; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.InetAddress; +import java.net.URL; +import java.sql.*; +import java.util.Properties; +import java.util.StringTokenizer; + +public class Initialize { + private static final Logger logger = LoggerFactory.getLogger(Initialize.class); + public static final String GATEWAY_ID = "gateway.id"; + public static final String REGISTRY_USER = "registry.user"; + public static final String REGISTRY_PASSWORD = "registry.password"; + public static final String DERBY_SERVER_MODE_SYS_PROPERTY = "derby.drda.startNetworkServer"; + private NetworkServerControl server; + private static final String delimiter = ";"; + + public static boolean checkStringBufferEndsWith(StringBuffer buffer, String suffix) { + if (suffix.length() > buffer.length()) { + return false; + } + // this loop is done on purpose to avoid memory allocation performance + // problems on various JDKs + // StringBuffer.lastIndexOf() was introduced in jdk 1.4 and + // implementation is ok though does allocation/copying + // StringBuffer.toString().endsWith() does massive memory + // allocation/copying on JDK 1.5 + // See http://issues.apache.org/bugzilla/show_bug.cgi?id=37169 + int endIndex = suffix.length() - 1; + int bufferIndex = buffer.length() - 1; + while (endIndex >= 0) { + if (buffer.charAt(bufferIndex) != suffix.charAt(endIndex)) { + return false; + } + bufferIndex--; + endIndex--; + } + return true; + } + + public void initializeDB() { + String jdbcUrl = null; + String jdbcDriver = null; + URL resource = this.getClass().getClassLoader().getResource("repository.properties"); + Properties properties = new Properties(); + try { + properties.load(resource.openStream()); + } catch (IOException e) { + System.out.println("Unable to read repository properties"); + } + jdbcDriver = properties.getProperty("registry.jdbc.driver"); + jdbcUrl = properties.getProperty("registry.jdbc.url"); + String jdbcUser = properties.getProperty("registry.jdbc.user"); + String jdbcPassword = properties.getProperty("registry.jdbc.password"); + jdbcUrl = jdbcUrl + "?" + "user=" + jdbcUser + "&" + "password=" + jdbcPassword; + + + if (Utils.getDBType().equals("derby")) { + startDerbyInServerMode(); + } + + + Connection conn = null; + try { + Class.forName(Utils.getJDBCDriver()).newInstance(); + conn = DriverManager.getConnection(jdbcUrl, jdbcUser, jdbcPassword); + executeSQLScript(conn); + System.out.println("New Database created for Registry"); + } catch (Exception e) { + logger.error(e.getMessage(), e); + throw new RuntimeException("Database failure"); + } finally { + try { + if (!conn.getAutoCommit()) { + conn.commit(); + } + conn.close(); + } catch (SQLException e) { + logger.error(e.getMessage(), e); + e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. + } + } + + GatewayResource gatewayResource = new GatewayResource(); + gatewayResource.setGatewayName((String) properties.get(GATEWAY_ID)); + gatewayResource.setOwner((String) properties.get(GATEWAY_ID)); + gatewayResource.save(); + + UserResource userResource = (UserResource) gatewayResource.create(ResourceType.USER); + userResource.setUserName((String) properties.get(REGISTRY_USER)); + userResource.setPassword((String) properties.get(REGISTRY_PASSWORD)); + userResource.save(); + + WorkerResource workerResource = (WorkerResource) gatewayResource.create(ResourceType.GATEWAY_WORKER); + workerResource.setUser(userResource.getUserName()); + workerResource.save(); + + } + + private void executeSQLScript(Connection conn) throws Exception { + StringBuffer sql = new StringBuffer(); + BufferedReader reader = null; + try{ + + InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("data-derby.sql"); + reader = new BufferedReader(new InputStreamReader(inputStream)); + String line; + while ((line = reader.readLine()) != null) { + line = line.trim(); + if (line.startsWith("//")) { + continue; + } + if (line.startsWith("--")) { + continue; + } + StringTokenizer st = new StringTokenizer(line); + if (st.hasMoreTokens()) { + String token = st.nextToken(); + if ("REM".equalsIgnoreCase(token)) { + continue; + } + } + sql.append(" ").append(line); + + // SQL defines "--" as a comment to EOL + // and in Oracle it may contain a hint + // so we cannot just remove it, instead we must end it + if (line.indexOf("--") >= 0) { + sql.append("\n"); + } + if ((checkStringBufferEndsWith(sql, delimiter))) { + executeSQL(sql.substring(0, sql.length() - delimiter.length()), conn); + sql.replace(0, sql.length(), ""); + } + } + // Catch any statements not followed by ; + if (sql.length() > 0) { + executeSQL(sql.toString(), conn); + } + }catch (IOException e){ + logger.error("Error occurred while executing SQL script for creating Airavata database", e); + throw new Exception("Error occurred while executing SQL script for creating Airavata database", e); + }finally { + if (reader != null) { + reader.close(); + } + + } + + } + + private static void executeSQL(String sql, Connection conn) throws Exception { + // Check and ignore empty statements + if ("".equals(sql.trim())) { + return; + } + + Statement statement = null; + try { + logger.debug("SQL : " + sql); + + boolean ret; + int updateCount = 0, updateCountTotal = 0; + statement = conn.createStatement(); + ret = statement.execute(sql); + updateCount = statement.getUpdateCount(); + do { + if (!ret) { + if (updateCount != -1) { + updateCountTotal += updateCount; + } + } + ret = statement.getMoreResults(); + if (ret) { + updateCount = statement.getUpdateCount(); + } + } while (ret); + + logger.debug(sql + " : " + updateCountTotal + " rows affected"); + + SQLWarning warning = conn.getWarnings(); + while (warning != null) { + logger.info(warning + " sql warning"); + warning = warning.getNextWarning(); + } + conn.clearWarnings(); + } catch (SQLException e) { + if (e.getSQLState().equals("X0Y32")) { + // eliminating the table already exception for the derby + // database + logger.info("Table Already Exists", e); + } else { + throw new Exception("Error occurred while executing : " + sql, e); + } + } finally { + if (statement != null) { + try { + statement.close(); + } catch (SQLException e) { + logger.error("Error occurred while closing result set.", e); + } + } + } + } + + private void startDerbyInServerMode() { + try { + System.setProperty(DERBY_SERVER_MODE_SYS_PROPERTY, "true"); + server = new NetworkServerControl(InetAddress.getByName(Utils.getHost()), + Utils.getPort(), + Utils.getJDBCUser(), Utils.getJDBCUser()); + java.io.PrintWriter consoleWriter = new java.io.PrintWriter(System.out, true); + server.start(consoleWriter); + } catch (IOException e) { + logger.error("Unable to start Apache derby in the server mode! Check whether " + + "specified port is available"); + } catch (Exception e) { + logger.error("Unable to start Apache derby in the server mode! Check whether " + + "specified port is available"); + } + + } +} Added: airavata/trunk/modules/registry/airavata-jpa-registry/src/test/resources/repository.properties URL: http://svn.apache.org/viewvc/airavata/trunk/modules/registry/airavata-jpa-registry/src/test/resources/repository.properties?rev=1397168&view=auto ============================================================================== --- airavata/trunk/modules/registry/airavata-jpa-registry/src/test/resources/repository.properties (added) +++ airavata/trunk/modules/registry/airavata-jpa-registry/src/test/resources/repository.properties Thu Oct 11 17:06:09 2012 @@ -0,0 +1,93 @@ +# +# +# 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. +# +# + +# +# Properties for JCR Registry interface. By default, Apache Jackrabbit is used. +# +#jcr.class=org.apache.jackrabbit.core.RepositoryFactoryImpl +jcr.class=org.apache.jackrabbit.rmi.repository.RmiRepositoryFactory +org.apache.jackrabbit.repository.uri=http://localhost:8081/rmi +jcr.user=admin +jcr.pass=admin + + +# +# Class which implemented Scheduler interface. It will be used to determine a Provider +# +scheduler.class= org.apache.airavata.core.gfac.scheduler.impl.SchedulerImpl + +# +# Data Service Plugins classes +# +datachain.classes= org.apache.airavata.core.gfac.extension.data.RegistryDataService + +# +# Pre execution Plugins classes. For example, GridFTP Input Staging +# +prechain.classes= org.apache.airavata.core.gfac.extension.pre.GridFtpInputStaging +prechain.classes= org.apache.airavata.core.gfac.extension.pre.HttpInputStaging + +# +# Post execution Plugins classes. For example, GridFTP Output Staging +# +postchain.classes= org.apache.airavata.core.gfac.extension.post.GridFtpOutputStaging +postchain.classes= org.apache.airavata.core.gfac.extension.post.OutputRegister +trusted.cert.location=/Users/lahirugunathilake/Downloads/certificates + +#Configuring your implementation for Provenance Registry, if you have a implementation for AiravataProvenanceRegstry put the +#full qualified path name to property name class.provenance.registry.accessor, +#We have a mysql JPA provenanceRegistry implementation built in but you can write your own +#class.provenance.registry.accessor=org.apache.airavata.provenance.impl.jpa.AiravataJPAProvenanceRegistry + +# +# SSH private key location. It will be used by SSHProvider +# +# ssh.key=/home/user/.ssh/id_rsa +# ssh.keypass= +# ssh.username=usernameAtHost + +# +# MyProxy credential. It will be used by GridFTP Plugins and GramProvider. +# +myproxy.server=myproxy.teragrid.org +myproxy.user=username +myproxy.pass=password +myproxy.life=3600 +#Port details will be used by gfac-axis2 when deployed on tomcat +port=8080 +#ip=192.2.33.12 if you configure the ip we will pick this ip address rather picking it from axisconfiguration + +#Registry Configuration +class.registry.accessor=org.apache.airavata.persistance.registry.jpa.impl.AiravataJPARegistry +registry.user=testUser +registry.password=testUser +gateway.id=gateway1 +registry.jdbc.user=airavata +registry.jdbc.password=airavata + +#for mysql +#registry.jdbc.driver=com.mysql.jdbc.Driver +#registry.jdbc.url=jdbc:mysql://localhost:3306/persistent_data + +#for derby +registry.jdbc.driver=org.apache.derby.jdbc.ClientDriver +registry.jdbc.url=jdbc:derby://localhost:20000/persistent_data;create=true;user=airavata;password=airavata +
